libnl  3.7.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 #include "netlink-private/utils.h"
11 
12 static void print_usage(void)
13 {
14 printf(
15 "Usage: nl-pktloc-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 " -l, --list List all packet location definitions.\n"
21 " --u32=VALUE Print in iproute2's u32 selector style\n"
22 "\n"
23 "\n"
24 "EXAMPLE\n"
25 " $ nl-pktloc-lookup ip.dst\n"
26 " $ nl-pktloc-lookup --list\n"
27 "\n"
28 );
29  exit(0);
30 }
31 
32 static const char *align_txt[] = {
33  [TCF_EM_ALIGN_U8] = "u8",
34  [TCF_EM_ALIGN_U16] = "u16",
35  [TCF_EM_ALIGN_U32] = "u32"
36 };
37 
38 static uint32_t align_mask[] = {
39  [TCF_EM_ALIGN_U8] = 0xff,
40  [TCF_EM_ALIGN_U16] = 0xffff,
41  [TCF_EM_ALIGN_U32] = 0xffffffff,
42 };
43 
44 static const char *layer_txt[] = {
45  [TCF_LAYER_LINK] = "eth",
46  [TCF_LAYER_NETWORK] = "ip",
47  [TCF_LAYER_TRANSPORT] = "tcp"
48 };
49 
50 static const char *get_align_txt(struct rtnl_pktloc *loc, char buf[static 16])
51 {
52  if (loc->align < _NL_N_ELEMENTS(align_txt))
53  return align_txt[loc->align];
54 
55  snprintf(buf, 16, "%u", loc->align);
56  return buf;
57 }
58 
59 static void dump_u32_style(struct rtnl_pktloc *loc, uint32_t value)
60 {
61  char buf[16];
62 
63  if (loc->align > 4)
64  nl_cli_fatal(EINVAL, "u32 only supports alignments u8|u16|u32.");
65 
66  if (loc->layer == TCF_LAYER_LINK)
67  nl_cli_fatal(EINVAL, "u32 does not support link "
68  "layer locations.");
69 
70  if (loc->shift > 0)
71  nl_cli_fatal(EINVAL, "u32 does not support shifting.");
72 
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] :
77  0),
78  loc->layer == TCF_LAYER_TRANSPORT ? "nexthdr+" : "",
79  loc->offset);
80 }
81 
82 static void dump_loc(struct rtnl_pktloc *loc)
83 {
84  char buf[16];
85 
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] :
89  "???",
90  loc->offset, loc->mask, loc->shift);
91 }
92 
93 static void list_cb(struct rtnl_pktloc *loc, void *arg)
94 {
95  char buf[16];
96 
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] :
100  "???",
101  loc->offset, loc->mask, loc->shift, loc->refcnt);
102 }
103 
104 static void do_list(void)
105 {
106  printf(
107 "name align offset mask shift refcnt\n");
108  printf("---------------------------------------------------------\n");
109 
110  rtnl_pktloc_foreach(&list_cb, NULL);
111 }
112 
113 int main(int argc, char *argv[])
114 {
115  struct rtnl_pktloc *loc;
116  int err, ustyle = 0;
117  uint32_t uvalue = 0;
118 
119  for (;;) {
120  int c, optidx = 0;
121  enum {
122  ARG_U32 = 257,
123  };
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 },
129  { 0, 0, 0, 0 }
130  };
131 
132  c = getopt_long(argc, argv, "hvl", long_opts, &optidx);
133  if (c == -1)
134  break;
135 
136  switch (c) {
137  case 'h': print_usage(); break;
138  case 'v': nl_cli_print_version(); break;
139  case 'l': do_list(); exit(0);
140  case ARG_U32:
141  ustyle = 1;
142  uvalue = nl_cli_parse_u32(optarg);
143  break;
144  }
145  }
146 
147  if (optind >= argc)
148  print_usage();
149 
150  if ((err = rtnl_pktloc_lookup(argv[optind++], &loc)) < 0)
151  nl_cli_fatal(err, "Unable to lookup packet location: %s",
152  nl_geterror(err));
153 
154  if (ustyle)
155  dump_u32_style(loc, uvalue);
156  else
157  dump_loc(loc);
158 
159  return 0;
160 }
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