libnl 3.5.0

nl-pktloc-lookup.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3 * src/nl-pktloc-lookup.c Lookup packet location alias
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 <netlink/route/pktloc.h>
15 #include <linux/tc_ematch/tc_em_cmp.h>
16
17 static void print_usage(void)
18 {
19 printf(
20 "Usage: nl-pktloc-lookup [OPTIONS] <name>\n"
21 "\n"
22 "OPTIONS\n"
23 " -h, --help Show this help text.\n"
24 " -v, --version Show versioning information.\n"
25 " -l, --list List all packet location definitions.\n"
26 " --u32=VALUE Print in iproute2's u32 selector style\n"
27 "\n"
28 "\n"
29 "EXAMPLE\n"
30 " $ nl-pktloc-lookup ip.dst\n"
31 " $ nl-pktloc-lookup --list\n"
32 "\n"
33 );
34 exit(0);
35 }
36
37 static const char *align_txt[] = {
38 [TCF_EM_ALIGN_U8] = "u8",
39 [TCF_EM_ALIGN_U16] = "u16",
40 [TCF_EM_ALIGN_U32] = "u32"
41 };
42
43 static uint32_t align_mask[] = {
44 [TCF_EM_ALIGN_U8] = 0xff,
45 [TCF_EM_ALIGN_U16] = 0xffff,
46 [TCF_EM_ALIGN_U32] = 0xffffffff,
47 };
48
49 static const char *layer_txt[] = {
50 [TCF_LAYER_LINK] = "eth",
51 [TCF_LAYER_NETWORK] = "ip",
52 [TCF_LAYER_TRANSPORT] = "tcp"
53 };
54
55 static void dump_u32_style(struct rtnl_pktloc *loc, uint32_t value)
56 {
57 if (loc->align > 4)
58 nl_cli_fatal(EINVAL, "u32 only supports alignments u8|u16|u32.");
59
60 if (loc->layer == TCF_LAYER_LINK)
61 nl_cli_fatal(EINVAL, "u32 does not support link "
62 "layer locations.");
63
64 if (loc->shift > 0)
65 nl_cli_fatal(EINVAL, "u32 does not support shifting.");
66
67 printf("%s %x %x at %s%u\n",
68 align_txt[loc->align],
69 value, loc->mask ? loc->mask : align_mask[loc->align],
70 loc->layer == TCF_LAYER_TRANSPORT ? "nexthdr+" : "",
71 loc->offset);
72 }
73
74 static char *get_align_txt(struct rtnl_pktloc *loc)
75 {
76 static char buf[16];
77
78 if (loc->align <= 4)
79 strcpy(buf, align_txt[loc->align]);
80 else
81 snprintf(buf, sizeof(buf), "%u", loc->align);
82
83 return buf;
84 }
85
86 static void dump_loc(struct rtnl_pktloc *loc)
87 {
88 printf("%s = %s at %s+%u & %#x >> %u\n",
89 loc->name, get_align_txt(loc), layer_txt[loc->layer],
90 loc->offset, loc->mask, loc->shift);
91 }
92
93 static void list_cb(struct rtnl_pktloc *loc, void *arg)
94 {
95 printf("%-26s %-5s %3s+%-4u %#-10x %-8u %u\n",
96 loc->name, get_align_txt(loc), layer_txt[loc->layer],
97 loc->offset, loc->mask, loc->shift, loc->refcnt);
98 }
99
100 static void do_list(void)
101 {
102 printf(
103 "name align offset mask shift refcnt\n");
104 printf("---------------------------------------------------------\n");
105
106 rtnl_pktloc_foreach(&list_cb, NULL);
107 }
108
109 int main(int argc, char *argv[])
110 {
111 struct rtnl_pktloc *loc;
112 int err, ustyle = 0;
113 uint32_t uvalue = 0;
114
115 for (;;) {
116 int c, optidx = 0;
117 enum {
118 ARG_U32 = 257,
119 };
120 static struct option long_opts[] = {
121 { "help", 0, 0, 'h' },
122 { "version", 0, 0, 'v' },
123 { "list", 0, 0, 'l' },
124 { "u32", 1, 0, ARG_U32 },
125 { 0, 0, 0, 0 }
126 };
127
128 c = getopt_long(argc, argv, "hvl", long_opts, &optidx);
129 if (c == -1)
130 break;
131
132 switch (c) {
133 case 'h': print_usage(); break;
134 case 'v': nl_cli_print_version(); break;
135 case 'l': do_list(); exit(0);
136 case ARG_U32:
137 ustyle = 1;
138 uvalue = nl_cli_parse_u32(optarg);
139 break;
140 }
141 }
142
143 if (optind >= argc)
144 print_usage();
145
146 if ((err = rtnl_pktloc_lookup(argv[optind++], &loc)) < 0)
147 nl_cli_fatal(err, "Unable to lookup packet location: %s",
148 nl_geterror(err));
149
150 if (ustyle)
151 dump_u32_style(loc, uvalue);
152 else
153 dump_loc(loc);
154
155 return 0;
156 }
rtnl_pktloc
Definition: pktloc.h:25
nl_cli_parse_u32
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
Definition: utils.c:43
nl_cli_fatal
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
Definition: utils.c:78
rtnl_pktloc_lookup
int rtnl_pktloc_lookup(const char *name, struct rtnl_pktloc **result)
Lookup packet location alias.
Definition: pktloc.c:174