6#include <netlink/cli/utils.h>
7#include <netlink/route/pktloc.h>
8#include <linux/tc_ematch/tc_em_cmp.h>
10#include "netlink-private/utils.h"
12static void print_usage(
void)
15"Usage: nl-pktloc-lookup [OPTIONS] <name>\n"
18" -h, --help Show this help text.\n"
19" -v, --version Show versioning information.\n"
20" -l, --list List all packet location definitions.\n"
21" --u32=VALUE Print in iproute2's u32 selector style\n"
25" $ nl-pktloc-lookup ip.dst\n"
26" $ nl-pktloc-lookup --list\n"
32static const char *align_txt[] = {
33 [TCF_EM_ALIGN_U8] =
"u8",
34 [TCF_EM_ALIGN_U16] =
"u16",
35 [TCF_EM_ALIGN_U32] =
"u32"
38static uint32_t align_mask[] = {
39 [TCF_EM_ALIGN_U8] = 0xff,
40 [TCF_EM_ALIGN_U16] = 0xffff,
41 [TCF_EM_ALIGN_U32] = 0xffffffff,
44static const char *layer_txt[] = {
45 [TCF_LAYER_LINK] =
"eth",
46 [TCF_LAYER_NETWORK] =
"ip",
47 [TCF_LAYER_TRANSPORT] =
"tcp"
50static const char *get_align_txt(
struct rtnl_pktloc *loc,
char buf[
static 16])
52 if (loc->align < _NL_N_ELEMENTS(align_txt))
53 return align_txt[loc->align];
55 snprintf(buf, 16,
"%u", loc->align);
59static void dump_u32_style(
struct rtnl_pktloc *loc, uint32_t value)
64 nl_cli_fatal(EINVAL,
"u32 only supports alignments u8|u16|u32.");
66 if (loc->layer == TCF_LAYER_LINK)
73 printf(
"%s %x %x at %s%u\n", get_align_txt(loc, buf), value,
74 loc->mask ? loc->mask :
75 (loc->align < _NL_N_ELEMENTS(align_mask) ?
76 align_mask[loc->align] :
78 loc->layer == TCF_LAYER_TRANSPORT ?
"nexthdr+" :
"",
86 printf(
"%s = %s at %s+%u & %#x >> %u\n", loc->name,
87 get_align_txt(loc, buf),
88 loc->layer < _NL_N_ELEMENTS(layer_txt) ? layer_txt[loc->layer] :
90 loc->offset, loc->mask, loc->shift);
93static void list_cb(
struct rtnl_pktloc *loc,
void *arg)
97 printf(
"%-26s %-5s %3s+%-4u %#-10x %-8u %u\n", loc->name,
98 get_align_txt(loc, buf),
99 loc->layer < _NL_N_ELEMENTS(layer_txt) ? layer_txt[loc->layer] :
101 loc->offset, loc->mask, loc->shift, loc->refcnt);
104static void do_list(
void)
107"name align offset mask shift refcnt\n");
108 printf(
"---------------------------------------------------------\n");
110 rtnl_pktloc_foreach(&list_cb, NULL);
113int main(
int argc,
char *argv[])
124 static struct option long_opts[] = {
125 {
"help", 0, 0,
'h' },
126 {
"version", 0, 0,
'v' },
127 {
"list", 0, 0,
'l' },
128 {
"u32", 1, 0, ARG_U32 },
132 c = getopt_long(argc, argv,
"hvl", long_opts, &optidx);
137 case 'h': print_usage();
break;
138 case 'v': nl_cli_print_version();
break;
139 case 'l': do_list(); exit(0);
151 nl_cli_fatal(err,
"Unable to lookup packet location: %s",
155 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.