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