xref: /aosp_15_r20/external/libnl/src/nl-link-list.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2010 Thomas Graf <[email protected]>
4  */
5 
6 #include "nl-default.h"
7 
8 #include <linux/netlink.h>
9 
10 #include <netlink/cli/utils.h>
11 #include <netlink/cli/link.h>
12 
print_usage(void)13 static void print_usage(void)
14 {
15 	printf(
16 "Usage: nl-link-list [OPTIONS]... \n"
17 "\n"
18 "OPTIONS\n"
19 "     --details             Show detailed information of each link\n"
20 "     --stats               Show statistics, implies --details\n"
21 " -h, --help                Show this help text.\n"
22 " -v, --version             Show versioning information.\n"
23 "\n"
24 " -n, --name=NAME	    Name of link\n"
25 " -i, --index               Interface index (unique identifier)\n"
26 "     --family=NAME         Link address family\n"
27 "     --mtu=NUM             MTU value\n"
28 "     --txqlen=NUM          TX queue length\n"
29 "     --weight=NUM          Weight\n"
30 	);
31 	exit(0);
32 }
33 
main(int argc,char * argv[])34 int main(int argc, char *argv[])
35 {
36 	struct nl_sock *sock;
37 	struct nl_cache *link_cache;
38 	struct rtnl_link *link;
39 	struct nl_dump_params params = {
40 		.dp_type = NL_DUMP_LINE,
41 		.dp_fd = stdout,
42 	};
43 
44 	sock = nl_cli_alloc_socket();
45 	nl_cli_connect(sock, NETLINK_ROUTE);
46 	link = nl_cli_link_alloc();
47 
48 	for (;;) {
49 		int c, optidx = 0;
50 		enum {
51 			ARG_FAMILY = 257,
52 			ARG_MTU = 258,
53 			ARG_TXQLEN,
54 			ARG_WEIGHT,
55 			ARG_DETAILS,
56 			ARG_STATS,
57 		};
58 		static struct option long_opts[] = {
59 			{ "details", 0, 0, ARG_DETAILS },
60 			{ "stats", 0, 0, ARG_STATS },
61 			{ "help", 0, 0, 'h' },
62 			{ "version", 0, 0, 'v' },
63 			{ "name", 1, 0, 'n' },
64 			{ "index", 1, 0, 'i' },
65 			{ "family", 1, 0, ARG_FAMILY },
66 			{ "mtu", 1, 0, ARG_MTU },
67 			{ "txqlen", 1, 0, ARG_TXQLEN },
68 			{ "weight", 1, 0, ARG_WEIGHT },
69 			{ 0, 0, 0, 0 }
70 		};
71 
72 		c = getopt_long(argc, argv, "hvn:i:", long_opts, &optidx);
73 		if (c == -1)
74 			break;
75 
76 		switch (c) {
77 		case ARG_DETAILS: params.dp_type = NL_DUMP_DETAILS; break;
78 		case ARG_STATS: params.dp_type = NL_DUMP_STATS; break;
79 		case 'h': print_usage(); break;
80 		case 'v': nl_cli_print_version(); break;
81 		case 'n': nl_cli_link_parse_name(link, optarg); break;
82 		case 'i': nl_cli_link_parse_ifindex(link, optarg); break;
83 		case ARG_FAMILY: nl_cli_link_parse_family(link, optarg); break;
84 		case ARG_MTU: nl_cli_link_parse_mtu(link, optarg); break;
85 		case ARG_TXQLEN: nl_cli_link_parse_txqlen(link, optarg); break;
86 		case ARG_WEIGHT: nl_cli_link_parse_weight(link, optarg); break;
87 		}
88 	}
89 
90 	link_cache = nl_cli_link_alloc_cache_family(sock,
91 				rtnl_link_get_family(link));
92 
93 	nl_cache_dump_filter(link_cache, &params, OBJ_CAST(link));
94 
95 	return 0;
96 }
97