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