8#include <linux/tc_ematch/tc_em_cmp.h>
10#include <netlink/cli/utils.h>
11#include <netlink/route/pktloc.h>
13static void print_usage(
void)
16"Usage: nl-pktloc-lookup [OPTIONS] <name>\n"
19" -h, --help Show this help text.\n"
20" -v, --version Show versioning information.\n"
21" -l, --list List all packet location definitions.\n"
22" --u32=VALUE Print in iproute2's u32 selector style\n"
26" $ nl-pktloc-lookup ip.dst\n"
27" $ nl-pktloc-lookup --list\n"
33static const char *align_txt[] = {
34 [TCF_EM_ALIGN_U8] =
"u8",
35 [TCF_EM_ALIGN_U16] =
"u16",
36 [TCF_EM_ALIGN_U32] =
"u32"
39static uint32_t align_mask[] = {
40 [TCF_EM_ALIGN_U8] = 0xff,
41 [TCF_EM_ALIGN_U16] = 0xffff,
42 [TCF_EM_ALIGN_U32] = 0xffffffff,
45static const char *layer_txt[] = {
46 [TCF_LAYER_LINK] =
"eth",
47 [TCF_LAYER_NETWORK] =
"ip",
48 [TCF_LAYER_TRANSPORT] =
"tcp"
51static const char *get_align_txt(
struct rtnl_pktloc *loc,
char buf[
static 16])
53 if (loc->align < _NL_N_ELEMENTS(align_txt))
54 return align_txt[loc->align];
56 snprintf(buf, 16,
"%u", loc->align);
60static void dump_u32_style(
struct rtnl_pktloc *loc, uint32_t value)
65 nl_cli_fatal(EINVAL,
"u32 only supports alignments u8|u16|u32.");
67 if (loc->layer == TCF_LAYER_LINK)
74 printf(
"%s %x %x at %s%u\n", get_align_txt(loc, buf), value,
75 loc->mask ? loc->mask :
76 (loc->align < _NL_N_ELEMENTS(align_mask) ?
77 align_mask[loc->align] :
79 loc->layer == TCF_LAYER_TRANSPORT ?
"nexthdr+" :
"",
87 printf(
"%s = %s at %s+%u & %#x >> %u\n", loc->name,
88 get_align_txt(loc, buf),
89 loc->layer < _NL_N_ELEMENTS(layer_txt) ? layer_txt[loc->layer] :
91 loc->offset, loc->mask, loc->shift);
94static void list_cb(
struct rtnl_pktloc *loc,
void *arg)
98 printf(
"%-26s %-5s %3s+%-4u %#-10x %-8u %u\n", loc->name,
99 get_align_txt(loc, buf),
100 loc->layer < _NL_N_ELEMENTS(layer_txt) ? layer_txt[loc->layer] :
102 loc->offset, loc->mask, loc->shift, loc->refcnt);
105static void do_list(
void)
108"name align offset mask shift refcnt\n");
109 printf(
"---------------------------------------------------------\n");
111 rtnl_pktloc_foreach(&list_cb, NULL);
114int main(
int argc,
char *argv[])
125 static struct option long_opts[] = {
126 {
"help", 0, 0,
'h' },
127 {
"version", 0, 0,
'v' },
128 {
"list", 0, 0,
'l' },
129 {
"u32", 1, 0, ARG_U32 },
133 c = getopt_long(argc, argv,
"hvl", long_opts, &optidx);
138 case 'h': print_usage();
break;
139 case 'v': nl_cli_print_version();
break;
140 case 'l': do_list(); exit(0);
152 nl_cli_fatal(err,
"Unable to lookup packet location: %s",
156 dump_u32_style(loc, uvalue);
void nl_cli_fatal(int err, const char *fmt,...)
Print error message and quit application.
uint32_t nl_cli_parse_u32(const char *arg)
Parse a text based 32 bit unsigned integer argument.
int rtnl_pktloc_lookup(const char *name, struct rtnl_pktloc **result)
Lookup packet location alias.