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