|
Packit Service |
f991dd |
/* SPDX-License-Identifier: LGPL-2.1-only */
|
|
Packit Service |
f991dd |
/*
|
|
Packit Service |
f991dd |
* src/nl-pktloc-lookup.c Lookup packet location alias
|
|
Packit Service |
f991dd |
*
|
|
Packit Service |
f991dd |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
f991dd |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
f991dd |
* License as published by the Free Software Foundation version 2.1
|
|
Packit Service |
f991dd |
* of the License.
|
|
Packit Service |
f991dd |
*
|
|
Packit Service |
f991dd |
* Copyright (c) 2010 Thomas Graf <tgraf@suug.ch>
|
|
Packit Service |
f991dd |
*/
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#include <netlink/cli/utils.h>
|
|
Packit Service |
f991dd |
#include <netlink/route/pktloc.h>
|
|
Packit Service |
f991dd |
#include <linux/tc_ematch/tc_em_cmp.h>
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void print_usage(void)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
printf(
|
|
Packit Service |
f991dd |
"Usage: nl-pktloc-lookup [OPTIONS] <name>\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"OPTIONS\n"
|
|
Packit Service |
f991dd |
" -h, --help Show this help text.\n"
|
|
Packit Service |
f991dd |
" -v, --version Show versioning information.\n"
|
|
Packit Service |
f991dd |
" -l, --list List all packet location definitions.\n"
|
|
Packit Service |
f991dd |
" --u32=VALUE Print in iproute2's u32 selector style\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"EXAMPLE\n"
|
|
Packit Service |
f991dd |
" $ nl-pktloc-lookup ip.dst\n"
|
|
Packit Service |
f991dd |
" $ nl-pktloc-lookup --list\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
);
|
|
Packit Service |
f991dd |
exit(0);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static const char *align_txt[] = {
|
|
Packit Service |
f991dd |
[TCF_EM_ALIGN_U8] = "u8",
|
|
Packit Service |
f991dd |
[TCF_EM_ALIGN_U16] = "u16",
|
|
Packit Service |
f991dd |
[TCF_EM_ALIGN_U32] = "u32"
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static uint32_t align_mask[] = {
|
|
Packit Service |
f991dd |
[TCF_EM_ALIGN_U8] = 0xff,
|
|
Packit Service |
f991dd |
[TCF_EM_ALIGN_U16] = 0xffff,
|
|
Packit Service |
f991dd |
[TCF_EM_ALIGN_U32] = 0xffffffff,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static const char *layer_txt[] = {
|
|
Packit Service |
f991dd |
[TCF_LAYER_LINK] = "eth",
|
|
Packit Service |
f991dd |
[TCF_LAYER_NETWORK] = "ip",
|
|
Packit Service |
f991dd |
[TCF_LAYER_TRANSPORT] = "tcp"
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void dump_u32_style(struct rtnl_pktloc *loc, uint32_t value)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
if (loc->align > 4)
|
|
Packit Service |
f991dd |
nl_cli_fatal(EINVAL, "u32 only supports alignments u8|u16|u32.");
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (loc->layer == TCF_LAYER_LINK)
|
|
Packit Service |
f991dd |
nl_cli_fatal(EINVAL, "u32 does not support link "
|
|
Packit Service |
f991dd |
"layer locations.");
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (loc->shift > 0)
|
|
Packit Service |
f991dd |
nl_cli_fatal(EINVAL, "u32 does not support shifting.");
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
printf("%s %x %x at %s%u\n",
|
|
Packit Service |
f991dd |
align_txt[loc->align],
|
|
Packit Service |
f991dd |
value, loc->mask ? loc->mask : align_mask[loc->align],
|
|
Packit Service |
f991dd |
loc->layer == TCF_LAYER_TRANSPORT ? "nexthdr+" : "",
|
|
Packit Service |
f991dd |
loc->offset);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static char *get_align_txt(struct rtnl_pktloc *loc)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
static char buf[16];
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (loc->align <= 4)
|
|
Packit Service |
f991dd |
strcpy(buf, align_txt[loc->align]);
|
|
Packit Service |
f991dd |
else
|
|
Packit Service |
f991dd |
snprintf(buf, sizeof(buf), "%u", loc->align);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
return buf;
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void dump_loc(struct rtnl_pktloc *loc)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
printf("%s = %s at %s+%u & %#x >> %u\n",
|
|
Packit Service |
f991dd |
loc->name, get_align_txt(loc), layer_txt[loc->layer],
|
|
Packit Service |
f991dd |
loc->offset, loc->mask, loc->shift);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void list_cb(struct rtnl_pktloc *loc, void *arg)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
printf("%-26s %-5s %3s+%-4u %#-10x %-8u %u\n",
|
|
Packit Service |
f991dd |
loc->name, get_align_txt(loc), layer_txt[loc->layer],
|
|
Packit Service |
f991dd |
loc->offset, loc->mask, loc->shift, loc->refcnt);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void do_list(void)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
printf(
|
|
Packit Service |
f991dd |
"name align offset mask shift refcnt\n");
|
|
Packit Service |
f991dd |
printf("---------------------------------------------------------\n");
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
rtnl_pktloc_foreach(&list_cb, NULL);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
int main(int argc, char *argv[])
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
struct rtnl_pktloc *loc;
|
|
Packit Service |
f991dd |
int err, ustyle = 0;
|
|
Packit Service |
f991dd |
uint32_t uvalue = 0;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
for (;;) {
|
|
Packit Service |
f991dd |
int c, optidx = 0;
|
|
Packit Service |
f991dd |
enum {
|
|
Packit Service |
f991dd |
ARG_U32 = 257,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
static struct option long_opts[] = {
|
|
Packit Service |
f991dd |
{ "help", 0, 0, 'h' },
|
|
Packit Service |
f991dd |
{ "version", 0, 0, 'v' },
|
|
Packit Service |
f991dd |
{ "list", 0, 0, 'l' },
|
|
Packit Service |
f991dd |
{ "u32", 1, 0, ARG_U32 },
|
|
Packit Service |
f991dd |
{ 0, 0, 0, 0 }
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
c = getopt_long(argc, argv, "hvl", long_opts, &optidx);
|
|
Packit Service |
f991dd |
if (c == -1)
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
switch (c) {
|
|
Packit Service |
f991dd |
case 'h': print_usage(); break;
|
|
Packit Service |
f991dd |
case 'v': nl_cli_print_version(); break;
|
|
Packit Service |
f991dd |
case 'l': do_list(); exit(0);
|
|
Packit Service |
f991dd |
case ARG_U32:
|
|
Packit Service |
f991dd |
ustyle = 1;
|
|
Packit Service |
f991dd |
uvalue = nl_cli_parse_u32(optarg);
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (optind >= argc)
|
|
Packit Service |
f991dd |
print_usage();
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if ((err = rtnl_pktloc_lookup(argv[optind++], &loc)) < 0)
|
|
Packit Service |
f991dd |
nl_cli_fatal(err, "Unable to lookup packet location: %s",
|
|
Packit Service |
f991dd |
nl_geterror(err));
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (ustyle)
|
|
Packit Service |
f991dd |
dump_u32_style(loc, uvalue);
|
|
Packit Service |
f991dd |
else
|
|
Packit Service |
f991dd |
dump_loc(loc);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
return 0;
|
|
Packit Service |
f991dd |
}
|