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