Skip to content

Commit

Permalink
samples/bpf: tc_bench01_redirect add option for listing
Browse files Browse the repository at this point in the history
Add option for listing tc ingress filters, as I constantly forget
the exact tc command for viewing it.

Cmdline option --list with optional_argument.  Listing uses ingress ifname
if argument not specified for --list=dev.  Notice, getopts is annoying, and
need an equal-sign "=" for assigning an optional_argument.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
  • Loading branch information
netoptimizer committed Jul 3, 2017
1 parent 9d4a5ee commit 152b488
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions kernel/samples/bpf/tc_bench01_redirect_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static const struct option long_options[] = {
{"ifindex-egress", required_argument, NULL, 'x' },
/* Allow specifying tc cmd via argument */
{"tc-cmd", required_argument, NULL, 't' },
/* HINT assign: optional_arguments with '=' */
{"list", optional_argument, NULL, 'l' },
{0, 0, NULL, 0 }
};

Expand Down Expand Up @@ -106,6 +108,26 @@ static int tc_ingress_attach_bpf(const char* dev, const char* bpf_obj)
return ret;
}

static int tc_list_ingress_filter(const char* dev)
{
char cmd[CMD_MAX];
int ret = 0;

memset(&cmd, 0, CMD_MAX);
snprintf(cmd, CMD_MAX,
"%s filter show dev %s ingress",
tc_cmd, dev);
if (verbose) printf(" - Run: %s\n", cmd);
ret = system(cmd);
if (ret) {
fprintf(stderr,
"ERR(%d): tc cannot list filters\n Cmdline:%s\n",
ret, cmd);
exit(EXIT_FAILURE);
}
return ret;
}

static char ingress_ifname[IF_NAMESIZE];
static char egress_ifname[IF_NAMESIZE];
static char buf_ifname[IF_NAMESIZE] = "(unknown-dev)";
Expand All @@ -131,6 +153,7 @@ bool validate_ifname(const char* input_ifname, char *output_ifname)

int main(int argc, char **argv)
{
bool list_ingress_tc_filter = false;
int longindex = 0, opt, fd = -1;
int egress_ifindex = -1;
int ingress_ifindex = 0;
Expand All @@ -141,6 +164,8 @@ int main(int argc, char **argv)
char bpf_obj[256];
snprintf(bpf_obj, sizeof(bpf_obj), "%s_kern.o", argv[0]);

memset(ingress_ifname, 0, IF_NAMESIZE); /* Can be used uninitialized */

/* Parse commands line args */
while ((opt = getopt_long(argc, argv, "h",
long_options, &longindex)) != -1) {
Expand Down Expand Up @@ -173,6 +198,20 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
break;
case 'l':
if (optarg &&
!validate_ifname(optarg,(char *)&ingress_ifname)) {
fprintf(stderr,
"ERR: input --list=ifname invalid\n");
return EXIT_FAILURE;
}
if (strlen(ingress_ifname) == 0) {
fprintf(stderr,
"ERR: need input --list=ifname\n");
return EXIT_FAILURE;
}
list_ingress_tc_filter = true;
break;
case 't':
len = strlen(optarg);
if (len >= CMD_MAX_TC) {
Expand All @@ -197,6 +236,13 @@ int main(int argc, char **argv)
}
}

if (list_ingress_tc_filter) {
if (verbose)
printf("TC list ingress filters on device %s\n",
ingress_ifname);
tc_list_ingress_filter(ingress_ifname);
}

fd = bpf_obj_get(mapfile);
if (fd < 0) {
fprintf(stderr, "ERROR: cannot open bpf_obj_get(%s): %s(%d)\n",
Expand Down

0 comments on commit 152b488

Please sign in to comment.