libnl  3.4.0
nl-classid-lookup.c
1 /*
2  * src/nl-classid-lookup.c Lookup classid
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) 2010 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <linux/pkt_sched.h>
14 
15 static void print_usage(void)
16 {
17  printf(
18 "Usage: nl-classid-lookup [OPTIONS]... NAME\n"
19 "\n"
20 "OPTIONS\n"
21 " -h, --help Show this help text.\n"
22 " -v, --version Show versioning information.\n"
23 " -r, --reverse Do a reverse lookup, i.e. classid to name.\n"
24 " --raw Print the raw classid, not pretty printed.\n"
25 "\n"
26 "EXAMPLE\n"
27 " $ nl-classid-lookup low_latency\n"
28 " $ nl-classid-lookup -r 1:12\n"
29 "\n"
30  );
31  exit(0);
32 }
33 
34 int main(int argc, char *argv[])
35 {
36  uint32_t classid;
37  char *name;
38  int err, reverse = 0, raw = 0;
39 
40  for (;;) {
41  int c, optidx = 0;
42  enum {
43  ARG_RAW = 257,
44  };
45  static struct option long_opts[] = {
46  { "help", 0, 0, 'h' },
47  { "version", 0, 0, 'v' },
48  { "reverse", 0, 0, 'r' },
49  { "raw", 0, 0, ARG_RAW },
50  { 0, 0, 0, 0 }
51  };
52 
53  c = getopt_long(argc, argv, "hvr", long_opts, &optidx);
54  if (c == -1)
55  break;
56 
57  switch (c) {
58  case 'h': print_usage(); break;
59  case 'v': nl_cli_print_version(); break;
60  case 'r': reverse = 1; break;
61  case ARG_RAW: raw = 1; break;
62  }
63  }
64 
65  if (optind >= argc)
66  print_usage();
67 
68  name = argv[optind++];
69 
70  /*
71  * We use rtnl_tc_str2handle() even while doing a reverse lookup. This
72  * allows for name -> name lookups. This is intentional, it does not
73  * do any harm and avoids duplicating a lot of code.
74  */
75  if ((err = rtnl_tc_str2handle(name, &classid)) < 0)
76  nl_cli_fatal(err, "Unable to lookup classid \"%s\": %s",
77  name, nl_geterror(err));
78 
79  if (reverse) {
80  char buf[64];
81  printf("%s\n", rtnl_tc_handle2str(classid, buf, sizeof(buf)));
82  } else if (raw)
83  printf("%#x\n", classid);
84  else
85  printf("%x:%x\n", TC_H_MAJ(classid) >> 16, TC_H_MIN(classid));
86 
87  return 0;
88 }
int rtnl_tc_str2handle(const char *str, uint32_t *res)
Convert a charactering strint to a traffic control handle.
Definition: classid.c:154
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:77
char * rtnl_tc_handle2str(uint32_t handle, char *buf, size_t len)
Convert a traffic control handle to a character string (Reentrant).
Definition: classid.c:109