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