libnl: /data/src/libnl3/src/nl-classid-lookup.c Source File

libnl 3.7.0

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