Skip to content

Commit dc378a1

Browse files
teknoraverAlexei Starovoitov
authored andcommitted
samples: bpf: get ifindex from ifname
Find the ifindex with if_nametoindex() instead of requiring the numeric ifindex. Signed-off-by: Matteo Croce <mcroce@redhat.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent d606ee5 commit dc378a1

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

samples/bpf/xdp1_user.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <unistd.h>
1616
#include <libgen.h>
1717
#include <sys/resource.h>
18+
#include <net/if.h>
1819

1920
#include "bpf_util.h"
2021
#include "bpf/bpf.h"
@@ -59,7 +60,7 @@ static void poll_stats(int map_fd, int interval)
5960
static void usage(const char *prog)
6061
{
6162
fprintf(stderr,
62-
"usage: %s [OPTS] IFINDEX\n\n"
63+
"usage: %s [OPTS] IFACE\n\n"
6364
"OPTS:\n"
6465
" -S use skb-mode\n"
6566
" -N enforce native mode\n",
@@ -102,7 +103,11 @@ int main(int argc, char **argv)
102103
return 1;
103104
}
104105

105-
ifindex = strtoul(argv[optind], NULL, 0);
106+
ifindex = if_nametoindex(argv[1]);
107+
if (!ifindex) {
108+
perror("if_nametoindex");
109+
return 1;
110+
}
106111

107112
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
108113
prog_load_attr.file = filename;

0 commit comments

Comments
 (0)