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