1 // SPDX-License-Identifier: GPL-2.0
2 #include <uapi/linux/bpf.h>
3 #include <uapi/linux/netdev.h>
4 #include <linux/if_link.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10 #include <netinet/tcp.h>
12 #include <arpa/inet.h>
14 #include <bpf/libbpf.h>
17 #include <network_helpers.h>
19 #include "xdp_features.skel.h"
20 #include "xdp_features.h"
22 #define RED(str) "\033[0;31m" str "\033[0m"
23 #define GREEN(str) "\033[0;32m" str "\033[0m"
24 #define YELLOW(str) "\033[0;33m" str "\033[0m"
31 enum netdev_xdp_act drv_feature;
32 enum xdp_action action;
34 struct sockaddr_storage dut_ctrl_addr;
35 struct sockaddr_storage dut_addr;
36 struct sockaddr_storage tester_addr;
41 void test__fail(void) { /* for network_helpers.c */ }
43 static int libbpf_print_fn(enum libbpf_print_level level,
44 const char *format, va_list args)
46 if (level == LIBBPF_DEBUG && !env.verbosity)
48 return vfprintf(stderr, format, args);
51 static volatile bool exiting;
53 static void sig_handler(int sig)
58 const char *argp_program_version = "xdp-features 0.0";
59 const char argp_program_doc[] =
60 "XDP features detection application.\n"
62 "XDP features application checks the XDP advertised features match detected ones.\n"
64 "USAGE: ./xdp-features [-vt] [-f <xdp-feature>] [-D <dut-data-ip>] [-T <tester-data-ip>] [-C <dut-ctrl-ip>] <iface-name>\n"
66 "dut-data-ip, tester-data-ip, dut-ctrl-ip: IPv6 or IPv4-mapped-IPv6 addresses;\n"
76 static const struct argp_option opts[] = {
77 { "verbose", 'v', NULL, 0, "Verbose debug output" },
78 { "tester", 't', NULL, 0, "Tester mode" },
79 { "feature", 'f', "XDP-FEATURE", 0, "XDP feature to test" },
80 { "dut_data_ip", 'D', "DUT-DATA-IP", 0, "DUT IP data channel" },
81 { "dut_ctrl_ip", 'C', "DUT-CTRL-IP", 0, "DUT IP control channel" },
82 { "tester_data_ip", 'T', "TESTER-DATA-IP", 0, "Tester IP data channel" },
86 static int get_xdp_feature(const char *arg)
88 if (!strcmp(arg, "XDP_PASS")) {
89 env.feature.action = XDP_PASS;
90 env.feature.drv_feature = NETDEV_XDP_ACT_BASIC;
91 } else if (!strcmp(arg, "XDP_DROP")) {
92 env.feature.drv_feature = NETDEV_XDP_ACT_BASIC;
93 env.feature.action = XDP_DROP;
94 } else if (!strcmp(arg, "XDP_ABORTED")) {
95 env.feature.drv_feature = NETDEV_XDP_ACT_BASIC;
96 env.feature.action = XDP_ABORTED;
97 } else if (!strcmp(arg, "XDP_TX")) {
98 env.feature.drv_feature = NETDEV_XDP_ACT_BASIC;
99 env.feature.action = XDP_TX;
100 } else if (!strcmp(arg, "XDP_REDIRECT")) {
101 env.feature.drv_feature = NETDEV_XDP_ACT_REDIRECT;
102 env.feature.action = XDP_REDIRECT;
103 } else if (!strcmp(arg, "XDP_NDO_XMIT")) {
104 env.feature.drv_feature = NETDEV_XDP_ACT_NDO_XMIT;
112 static char *get_xdp_feature_str(void)
114 switch (env.feature.action) {
116 return YELLOW("XDP_PASS");
118 return YELLOW("XDP_DROP");
120 return YELLOW("XDP_ABORTED");
122 return YELLOW("XDP_TX");
124 return YELLOW("XDP_REDIRECT");
129 if (env.feature.drv_feature == NETDEV_XDP_ACT_NDO_XMIT)
130 return YELLOW("XDP_NDO_XMIT");
135 static error_t parse_arg(int key, char *arg, struct argp_state *state)
139 env.verbosity = true;
142 env.is_tester = true;
145 if (get_xdp_feature(arg) < 0) {
146 fprintf(stderr, "Invalid xdp feature: %s\n", arg);
148 return ARGP_ERR_UNKNOWN;
152 if (make_sockaddr(AF_INET6, arg, DUT_ECHO_PORT,
153 &env.dut_addr, NULL)) {
154 fprintf(stderr, "Invalid DUT address: %s\n", arg);
155 return ARGP_ERR_UNKNOWN;
159 if (make_sockaddr(AF_INET6, arg, DUT_CTRL_PORT,
160 &env.dut_ctrl_addr, NULL)) {
161 fprintf(stderr, "Invalid DUT CTRL address: %s\n", arg);
162 return ARGP_ERR_UNKNOWN;
166 if (make_sockaddr(AF_INET6, arg, 0, &env.tester_addr, NULL)) {
167 fprintf(stderr, "Invalid Tester address: %s\n", arg);
168 return ARGP_ERR_UNKNOWN;
173 if (strlen(arg) >= IF_NAMESIZE) {
174 fprintf(stderr, "Invalid device name: %s\n", arg);
176 return ARGP_ERR_UNKNOWN;
179 env.ifindex = if_nametoindex(arg);
181 env.ifindex = strtoul(arg, NULL, 0);
184 "Bad interface index or name (%d): %s\n",
185 errno, strerror(errno));
187 return ARGP_ERR_UNKNOWN;
191 return ARGP_ERR_UNKNOWN;
197 static const struct argp argp = {
200 .doc = argp_program_doc,
203 static void set_env_default(void)
205 env.feature.drv_feature = NETDEV_XDP_ACT_NDO_XMIT;
206 env.feature.action = -EINVAL;
207 env.ifindex = -ENODEV;
208 make_sockaddr(AF_INET6, "::ffff:127.0.0.1", DUT_CTRL_PORT,
209 &env.dut_ctrl_addr, NULL);
210 make_sockaddr(AF_INET6, "::ffff:127.0.0.1", DUT_ECHO_PORT,
211 &env.dut_addr, NULL);
212 make_sockaddr(AF_INET6, "::ffff:127.0.0.1", 0, &env.tester_addr, NULL);
215 static void *dut_echo_thread(void *arg)
217 unsigned char buf[sizeof(struct tlv_hdr)];
218 int sockfd = *(int *)arg;
221 struct tlv_hdr *tlv = (struct tlv_hdr *)buf;
222 struct sockaddr_storage addr;
226 n = recvfrom(sockfd, buf, sizeof(buf), MSG_WAITALL,
227 (struct sockaddr *)&addr, &addrlen);
228 if (n != ntohs(tlv->len))
231 if (ntohs(tlv->type) != CMD_ECHO)
234 sendto(sockfd, buf, sizeof(buf), MSG_NOSIGNAL | MSG_CONFIRM,
235 (struct sockaddr *)&addr, addrlen);
238 pthread_exit((void *)0);
244 static int dut_run_echo_thread(pthread_t *t, int *sockfd)
248 sockfd = start_reuseport_server(AF_INET6, SOCK_DGRAM, NULL,
249 DUT_ECHO_PORT, 0, 1);
251 fprintf(stderr, "Failed to create echo socket\n");
255 /* start echo channel */
256 err = pthread_create(t, NULL, dut_echo_thread, sockfd);
258 fprintf(stderr, "Failed creating dut_echo thread: %s\n",
267 static int dut_attach_xdp_prog(struct xdp_features *skel, int flags)
269 enum xdp_action action = env.feature.action;
270 struct bpf_program *prog;
271 unsigned int key = 0;
274 if (env.feature.drv_feature == NETDEV_XDP_ACT_NDO_XMIT) {
275 struct bpf_devmap_val entry = {
276 .ifindex = env.ifindex,
279 err = bpf_map__update_elem(skel->maps.dev_map,
281 &entry, sizeof(entry), 0);
285 fd = bpf_program__fd(skel->progs.xdp_do_redirect_cpumap);
286 action = XDP_REDIRECT;
291 prog = skel->progs.xdp_do_tx;
294 prog = skel->progs.xdp_do_drop;
297 prog = skel->progs.xdp_do_aborted;
300 prog = skel->progs.xdp_do_pass;
303 struct bpf_cpumap_val entry = {
308 err = bpf_map__update_elem(skel->maps.cpu_map,
310 &entry, sizeof(entry), 0);
314 prog = skel->progs.xdp_do_redirect;
321 err = bpf_xdp_attach(env.ifindex, bpf_program__fd(prog), flags, NULL);
324 "Failed to attach XDP program to ifindex %d\n",
329 static int recv_msg(int sockfd, void *buf, size_t bufsize, void *val,
332 struct tlv_hdr *tlv = (struct tlv_hdr *)buf;
335 len = recv(sockfd, buf, bufsize, 0);
336 if (len != ntohs(tlv->len) || len < sizeof(*tlv))
344 memcpy(val, tlv->data, len);
350 static int dut_run(struct xdp_features *skel)
352 int flags = XDP_FLAGS_UPDATE_IF_NOEXIST | XDP_FLAGS_DRV_MODE;
353 int state, err, *sockfd, ctrl_sockfd, echo_sockfd;
354 struct sockaddr_storage ctrl_addr;
355 pthread_t dut_thread;
358 sockfd = start_reuseport_server(AF_INET6, SOCK_STREAM, NULL,
359 DUT_CTRL_PORT, 0, 1);
361 fprintf(stderr, "Failed to create DUT socket\n");
365 ctrl_sockfd = accept(*sockfd, (struct sockaddr *)&ctrl_addr, &addrlen);
366 if (ctrl_sockfd < 0) {
367 fprintf(stderr, "Failed to accept connection on DUT socket\n");
374 unsigned char buf[BUFSIZE] = {};
375 struct tlv_hdr *tlv = (struct tlv_hdr *)buf;
377 err = recv_msg(ctrl_sockfd, buf, BUFSIZE, NULL, 0);
381 switch (ntohs(tlv->type)) {
383 if (state == CMD_START)
387 /* Load the XDP program on the DUT */
388 err = dut_attach_xdp_prog(skel, flags);
392 err = dut_run_echo_thread(&dut_thread, &echo_sockfd);
396 tlv->type = htons(CMD_ACK);
397 tlv->len = htons(sizeof(*tlv));
398 err = send(ctrl_sockfd, buf, sizeof(*tlv), 0);
404 if (state != CMD_START)
410 bpf_xdp_detach(env.ifindex, flags, NULL);
412 tlv->type = htons(CMD_ACK);
413 tlv->len = htons(sizeof(*tlv));
414 err = send(ctrl_sockfd, buf, sizeof(*tlv), 0);
416 case CMD_GET_XDP_CAP: {
417 LIBBPF_OPTS(bpf_xdp_query_opts, opts);
418 unsigned long long val;
421 err = bpf_xdp_query(env.ifindex, XDP_FLAGS_DRV_MODE,
425 "Failed to query XDP cap for ifindex %d\n",
430 tlv->type = htons(CMD_ACK);
431 n = sizeof(*tlv) + sizeof(opts.feature_flags);
434 val = htobe64(opts.feature_flags);
435 memcpy(tlv->data, &val, sizeof(val));
437 err = send(ctrl_sockfd, buf, n, 0);
442 case CMD_GET_STATS: {
443 unsigned int key = 0, val;
446 err = bpf_map__lookup_elem(skel->maps.dut_stats,
448 &val, sizeof(val), 0);
450 fprintf(stderr, "bpf_map_lookup_elem failed\n");
454 tlv->type = htons(CMD_ACK);
455 n = sizeof(*tlv) + sizeof(val);
459 memcpy(tlv->data, &val, sizeof(val));
461 err = send(ctrl_sockfd, buf, n, 0);
472 pthread_join(dut_thread, NULL);
474 bpf_xdp_detach(env.ifindex, flags, NULL);
481 static bool tester_collect_detected_cap(struct xdp_features *skel,
482 unsigned int dut_stats)
484 unsigned int err, key = 0, val;
489 err = bpf_map__lookup_elem(skel->maps.stats, &key, sizeof(key),
490 &val, sizeof(val), 0);
492 fprintf(stderr, "bpf_map_lookup_elem failed\n");
496 switch (env.feature.action) {
508 if (env.feature.drv_feature == NETDEV_XDP_ACT_NDO_XMIT)
514 static int send_and_recv_msg(int sockfd, enum test_commands cmd, void *val,
517 unsigned char buf[BUFSIZE] = {};
518 struct tlv_hdr *tlv = (struct tlv_hdr *)buf;
521 tlv->type = htons(cmd);
522 tlv->len = htons(sizeof(*tlv));
524 err = send(sockfd, buf, sizeof(*tlv), 0);
528 err = recv_msg(sockfd, buf, BUFSIZE, val, val_size);
532 return ntohs(tlv->type) == CMD_ACK ? 0 : -EINVAL;
535 static int send_echo_msg(void)
537 unsigned char buf[sizeof(struct tlv_hdr)];
538 struct tlv_hdr *tlv = (struct tlv_hdr *)buf;
541 sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
543 fprintf(stderr, "Failed to create echo socket\n");
547 tlv->type = htons(CMD_ECHO);
548 tlv->len = htons(sizeof(*tlv));
550 n = sendto(sockfd, buf, sizeof(*tlv), MSG_NOSIGNAL | MSG_CONFIRM,
551 (struct sockaddr *)&env.dut_addr, sizeof(env.dut_addr));
554 return n == ntohs(tlv->len) ? 0 : -EINVAL;
557 static int tester_run(struct xdp_features *skel)
559 int flags = XDP_FLAGS_UPDATE_IF_NOEXIST | XDP_FLAGS_DRV_MODE;
560 unsigned long long advertised_feature;
561 struct bpf_program *prog;
566 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
568 fprintf(stderr, "Failed to create tester socket\n");
572 if (settimeo(sockfd, 1000) < 0)
575 err = connect(sockfd, (struct sockaddr *)&env.dut_ctrl_addr,
576 sizeof(env.dut_ctrl_addr));
578 fprintf(stderr, "Failed to connect to the DUT\n");
582 err = send_and_recv_msg(sockfd, CMD_GET_XDP_CAP, &advertised_feature,
583 sizeof(advertised_feature));
589 advertised_feature = be64toh(advertised_feature);
591 if (env.feature.drv_feature == NETDEV_XDP_ACT_NDO_XMIT ||
592 env.feature.action == XDP_TX)
593 prog = skel->progs.xdp_tester_check_tx;
595 prog = skel->progs.xdp_tester_check_rx;
597 err = bpf_xdp_attach(env.ifindex, bpf_program__fd(prog), flags, NULL);
599 fprintf(stderr, "Failed to attach XDP program to ifindex %d\n",
604 err = send_and_recv_msg(sockfd, CMD_START, NULL, 0);
608 for (i = 0; i < 10 && !exiting; i++) {
609 err = send_echo_msg();
616 err = send_and_recv_msg(sockfd, CMD_GET_STATS, &stats, sizeof(stats));
621 err = send_and_recv_msg(sockfd, CMD_STOP, NULL, 0);
622 /* send a new echo message to wake echo thread of the dut */
625 detected_cap = tester_collect_detected_cap(skel, ntohl(stats));
627 fprintf(stdout, "Feature %s: [%s][%s]\n", get_xdp_feature_str(),
628 detected_cap ? GREEN("DETECTED") : RED("NOT DETECTED"),
629 env.feature.drv_feature & advertised_feature ? GREEN("ADVERTISED")
630 : RED("NOT ADVERTISED"));
632 bpf_xdp_detach(env.ifindex, flags, NULL);
634 return err < 0 ? err : 0;
637 int main(int argc, char **argv)
639 struct xdp_features *skel;
642 libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
643 libbpf_set_print(libbpf_print_fn);
645 signal(SIGINT, sig_handler);
646 signal(SIGTERM, sig_handler);
650 /* Parse command line arguments */
651 err = argp_parse(&argp, argc, argv, 0, NULL, NULL);
655 if (env.ifindex < 0) {
656 fprintf(stderr, "Invalid ifindex\n");
660 /* Load and verify BPF application */
661 skel = xdp_features__open();
663 fprintf(stderr, "Failed to open and load BPF skeleton\n");
667 skel->rodata->tester_addr =
668 ((struct sockaddr_in6 *)&env.tester_addr)->sin6_addr;
669 skel->rodata->dut_addr =
670 ((struct sockaddr_in6 *)&env.dut_addr)->sin6_addr;
672 /* Load & verify BPF programs */
673 err = xdp_features__load(skel);
675 fprintf(stderr, "Failed to load and verify BPF skeleton\n");
679 err = xdp_features__attach(skel);
681 fprintf(stderr, "Failed to attach BPF skeleton\n");
687 fprintf(stdout, "Starting tester on device %d\n", env.ifindex);
688 err = tester_run(skel);
691 fprintf(stdout, "Starting DUT on device %d\n", env.ifindex);
696 xdp_features__destroy(skel);
698 return err < 0 ? -err : 0;