Skip to content

Commit 27a36bc

Browse files
LorenzoBianconiborkmann
authored andcommitted
selftests/bpf: Use ifname instead of ifindex in XDP compliance test tool
Rely on interface name instead of interface index in error messages or logs from XDP compliance test tool. Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/7dc5a8ff56c252b1a7ae29b059d0b2b1543c8b5d.1678382940.git.lorenzo@kernel.org
1 parent 5a70f4a commit 27a36bc

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

tools/testing/selftests/bpf/xdp_features.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
static struct env {
2727
bool verbosity;
28+
char ifname[IF_NAMESIZE];
2829
int ifindex;
2930
bool is_tester;
3031
struct {
@@ -179,7 +180,7 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
179180
env.ifindex = if_nametoindex(arg);
180181
if (!env.ifindex)
181182
env.ifindex = strtoul(arg, NULL, 0);
182-
if (!env.ifindex) {
183+
if (!env.ifindex || !if_indextoname(env.ifindex, env.ifname)) {
183184
fprintf(stderr,
184185
"Bad interface index or name (%d): %s\n",
185186
errno, strerror(errno));
@@ -205,6 +206,7 @@ static void set_env_default(void)
205206
env.feature.drv_feature = NETDEV_XDP_ACT_NDO_XMIT;
206207
env.feature.action = -EINVAL;
207208
env.ifindex = -ENODEV;
209+
strcpy(env.ifname, "unknown");
208210
make_sockaddr(AF_INET6, "::ffff:127.0.0.1", DUT_CTRL_PORT,
209211
&env.dut_ctrl_addr, NULL);
210212
make_sockaddr(AF_INET6, "::ffff:127.0.0.1", DUT_ECHO_PORT,
@@ -248,15 +250,18 @@ static int dut_run_echo_thread(pthread_t *t, int *sockfd)
248250
sockfd = start_reuseport_server(AF_INET6, SOCK_DGRAM, NULL,
249251
DUT_ECHO_PORT, 0, 1);
250252
if (!sockfd) {
251-
fprintf(stderr, "Failed to create echo socket\n");
253+
fprintf(stderr,
254+
"Failed creating data UDP socket on device %s\n",
255+
env.ifname);
252256
return -errno;
253257
}
254258

255259
/* start echo channel */
256260
err = pthread_create(t, NULL, dut_echo_thread, sockfd);
257261
if (err) {
258-
fprintf(stderr, "Failed creating dut_echo thread: %s\n",
259-
strerror(-err));
262+
fprintf(stderr,
263+
"Failed creating data UDP thread on device %s: %s\n",
264+
env.ifname, strerror(-err));
260265
free_fds(sockfd, 1);
261266
return -EINVAL;
262267
}
@@ -320,9 +325,8 @@ static int dut_attach_xdp_prog(struct xdp_features *skel, int flags)
320325

321326
err = bpf_xdp_attach(env.ifindex, bpf_program__fd(prog), flags, NULL);
322327
if (err)
323-
fprintf(stderr,
324-
"Failed to attach XDP program to ifindex %d\n",
325-
env.ifindex);
328+
fprintf(stderr, "Failed attaching XDP program to device %s\n",
329+
env.ifname);
326330
return err;
327331
}
328332

@@ -358,13 +362,16 @@ static int dut_run(struct xdp_features *skel)
358362
sockfd = start_reuseport_server(AF_INET6, SOCK_STREAM, NULL,
359363
DUT_CTRL_PORT, 0, 1);
360364
if (!sockfd) {
361-
fprintf(stderr, "Failed to create DUT socket\n");
365+
fprintf(stderr,
366+
"Failed creating control socket on device %s\n", env.ifname);
362367
return -errno;
363368
}
364369

365370
ctrl_sockfd = accept(*sockfd, (struct sockaddr *)&ctrl_addr, &addrlen);
366371
if (ctrl_sockfd < 0) {
367-
fprintf(stderr, "Failed to accept connection on DUT socket\n");
372+
fprintf(stderr,
373+
"Failed accepting connections on device %s control socket\n",
374+
env.ifname);
368375
free_fds(sockfd, 1);
369376
return -errno;
370377
}
@@ -422,8 +429,8 @@ static int dut_run(struct xdp_features *skel)
422429
&opts);
423430
if (err) {
424431
fprintf(stderr,
425-
"Failed to query XDP cap for ifindex %d\n",
426-
env.ifindex);
432+
"Failed querying XDP cap for device %s\n",
433+
env.ifname);
427434
goto end_thread;
428435
}
429436

@@ -540,7 +547,9 @@ static int send_echo_msg(void)
540547

541548
sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
542549
if (sockfd < 0) {
543-
fprintf(stderr, "Failed to create echo socket\n");
550+
fprintf(stderr,
551+
"Failed creating data UDP socket on device %s\n",
552+
env.ifname);
544553
return -errno;
545554
}
546555

@@ -596,8 +605,8 @@ static int tester_run(struct xdp_features *skel)
596605

597606
err = bpf_xdp_attach(env.ifindex, bpf_program__fd(prog), flags, NULL);
598607
if (err) {
599-
fprintf(stderr, "Failed to attach XDP program to ifindex %d\n",
600-
env.ifindex);
608+
fprintf(stderr, "Failed attaching XDP program to device %s\n",
609+
env.ifname);
601610
goto out;
602611
}
603612

@@ -653,7 +662,7 @@ int main(int argc, char **argv)
653662
return err;
654663

655664
if (env.ifindex < 0) {
656-
fprintf(stderr, "Invalid ifindex\n");
665+
fprintf(stderr, "Invalid device name %s\n", env.ifname);
657666
return -ENODEV;
658667
}
659668

@@ -684,11 +693,12 @@ int main(int argc, char **argv)
684693

685694
if (env.is_tester) {
686695
/* Tester */
687-
fprintf(stdout, "Starting tester on device %d\n", env.ifindex);
696+
fprintf(stdout, "Starting tester service on device %s\n",
697+
env.ifname);
688698
err = tester_run(skel);
689699
} else {
690700
/* DUT */
691-
fprintf(stdout, "Starting DUT on device %d\n", env.ifindex);
701+
fprintf(stdout, "Starting test on device %s\n", env.ifname);
692702
err = dut_run(skel);
693703
}
694704

0 commit comments

Comments
 (0)