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