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