1 // SPDX-License-Identifier: GPL-2.0
4 * Test XDP bonding support
6 * Sets up two bonded veth pairs between two fresh namespaces
7 * and verifies that XDP_TX program loaded on a bond device
8 * are correctly loaded onto the slave devices and XDP_TX'd
9 * packets are balanced using bonding.
15 #include <linux/if_link.h>
16 #include "test_progs.h"
17 #include "network_helpers.h"
18 #include <linux/if_bonding.h>
19 #include <linux/limits.h>
20 #include <linux/udp.h>
21 #include <uapi/linux/netdev.h>
23 #include "xdp_dummy.skel.h"
24 #include "xdp_redirect_multi_kern.skel.h"
25 #include "xdp_tx.skel.h"
27 #define BOND1_MAC {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}
28 #define BOND1_MAC_STR "00:11:22:33:44:55"
29 #define BOND2_MAC {0x00, 0x22, 0x33, 0x44, 0x55, 0x66}
30 #define BOND2_MAC_STR "00:22:33:44:55:66"
33 static int root_netns_fd = -1;
35 static void restore_root_netns(void)
37 ASSERT_OK(setns(root_netns_fd, CLONE_NEWNET), "restore_root_netns");
40 static int setns_by_name(char *name)
43 char nspath[PATH_MAX];
45 snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name);
46 nsfd = open(nspath, O_RDONLY | O_CLOEXEC);
50 err = setns(nsfd, CLONE_NEWNET);
55 static int get_rx_packets(const char *iface)
59 int iface_len = strlen(iface);
61 f = fopen("/proc/net/dev", "r");
65 while (fgets(line, sizeof(line), f)) {
69 p++; /* skip whitespace */
70 if (!strncmp(p, iface, iface_len)) {
75 p++; /* skip whitespace */
76 while (*p && *p != ' ')
77 p++; /* skip rx bytes */
79 p++; /* skip whitespace */
88 #define MAX_BPF_LINKS 8
91 struct xdp_dummy *xdp_dummy;
92 struct xdp_tx *xdp_tx;
93 struct xdp_redirect_multi_kern *xdp_redirect_multi_kern;
96 struct bpf_link *links[MAX_BPF_LINKS];
99 static int xdp_attach(struct skeletons *skeletons, struct bpf_program *prog, char *iface)
101 struct bpf_link *link;
104 ifindex = if_nametoindex(iface);
105 if (!ASSERT_GT(ifindex, 0, "get ifindex"))
108 if (!ASSERT_LE(skeletons->nlinks+1, MAX_BPF_LINKS, "too many XDP programs attached"))
111 link = bpf_program__attach_xdp(prog, ifindex);
112 if (!ASSERT_OK_PTR(link, "attach xdp program"))
115 skeletons->links[skeletons->nlinks++] = link;
120 BOND_ONE_NO_ATTACH = 0,
121 BOND_BOTH_AND_ATTACH,
124 static const char * const mode_names[] = {
125 [BOND_MODE_ROUNDROBIN] = "balance-rr",
126 [BOND_MODE_ACTIVEBACKUP] = "active-backup",
127 [BOND_MODE_XOR] = "balance-xor",
128 [BOND_MODE_BROADCAST] = "broadcast",
129 [BOND_MODE_8023AD] = "802.3ad",
130 [BOND_MODE_TLB] = "balance-tlb",
131 [BOND_MODE_ALB] = "balance-alb",
134 static const char * const xmit_policy_names[] = {
135 [BOND_XMIT_POLICY_LAYER2] = "layer2",
136 [BOND_XMIT_POLICY_LAYER34] = "layer3+4",
137 [BOND_XMIT_POLICY_LAYER23] = "layer2+3",
138 [BOND_XMIT_POLICY_ENCAP23] = "encap2+3",
139 [BOND_XMIT_POLICY_ENCAP34] = "encap3+4",
142 static int bonding_setup(struct skeletons *skeletons, int mode, int xmit_policy,
143 int bond_both_attach)
145 SYS(fail, "ip netns add ns_dst");
146 SYS(fail, "ip link add veth1_1 type veth peer name veth2_1 netns ns_dst");
147 SYS(fail, "ip link add veth1_2 type veth peer name veth2_2 netns ns_dst");
149 SYS(fail, "ip link add bond1 type bond mode %s xmit_hash_policy %s",
150 mode_names[mode], xmit_policy_names[xmit_policy]);
151 SYS(fail, "ip link set bond1 up address " BOND1_MAC_STR " addrgenmode none");
152 SYS(fail, "ip -netns ns_dst link add bond2 type bond mode %s xmit_hash_policy %s",
153 mode_names[mode], xmit_policy_names[xmit_policy]);
154 SYS(fail, "ip -netns ns_dst link set bond2 up address " BOND2_MAC_STR " addrgenmode none");
156 SYS(fail, "ip link set veth1_1 master bond1");
157 if (bond_both_attach == BOND_BOTH_AND_ATTACH) {
158 SYS(fail, "ip link set veth1_2 master bond1");
160 SYS(fail, "ip link set veth1_2 up addrgenmode none");
162 if (xdp_attach(skeletons, skeletons->xdp_dummy->progs.xdp_dummy_prog, "veth1_2"))
166 SYS(fail, "ip -netns ns_dst link set veth2_1 master bond2");
168 if (bond_both_attach == BOND_BOTH_AND_ATTACH)
169 SYS(fail, "ip -netns ns_dst link set veth2_2 master bond2");
171 SYS(fail, "ip -netns ns_dst link set veth2_2 up addrgenmode none");
173 /* Load a dummy program on sending side as with veth peer needs to have a
174 * XDP program loaded as well.
176 if (xdp_attach(skeletons, skeletons->xdp_dummy->progs.xdp_dummy_prog, "bond1"))
179 if (bond_both_attach == BOND_BOTH_AND_ATTACH) {
180 if (!ASSERT_OK(setns_by_name("ns_dst"), "set netns to ns_dst"))
183 if (xdp_attach(skeletons, skeletons->xdp_tx->progs.xdp_tx, "bond2"))
186 restore_root_netns();
194 static void bonding_cleanup(struct skeletons *skeletons)
196 restore_root_netns();
197 while (skeletons->nlinks) {
199 bpf_link__destroy(skeletons->links[skeletons->nlinks]);
201 ASSERT_OK(system("ip link delete bond1"), "delete bond1");
202 ASSERT_OK(system("ip link delete veth1_1"), "delete veth1_1");
203 ASSERT_OK(system("ip link delete veth1_2"), "delete veth1_2");
204 ASSERT_OK(system("ip netns delete ns_dst"), "delete ns_dst");
207 static int send_udp_packets(int vary_dst_ip)
210 .h_source = BOND1_MAC,
212 .h_proto = htons(ETH_P_IP),
214 struct iphdr iph = {};
215 struct udphdr uh = {};
220 s = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW);
221 if (!ASSERT_GE(s, 0, "socket"))
224 ifindex = if_nametoindex("bond1");
225 if (!ASSERT_GT(ifindex, 0, "get bond1 ifindex"))
233 iph.protocol = IPPROTO_UDP;
236 iph.tot_len = htons(sizeof(buf) - ETH_HLEN);
239 for (i = 1; i <= NPACKETS; i++) {
241 struct sockaddr_ll saddr_ll = {
242 .sll_ifindex = ifindex,
243 .sll_halen = ETH_ALEN,
244 .sll_addr = BOND2_MAC,
247 /* vary the UDP destination port for even distribution with roundrobin/xor modes */
253 /* construct a packet */
254 memcpy(buf, &eh, sizeof(eh));
255 memcpy(buf + sizeof(eh), &iph, sizeof(iph));
256 memcpy(buf + sizeof(eh) + sizeof(iph), &uh, sizeof(uh));
258 n = sendto(s, buf, sizeof(buf), 0, (struct sockaddr *)&saddr_ll, sizeof(saddr_ll));
259 if (!ASSERT_EQ(n, sizeof(buf), "sendto"))
271 static void test_xdp_bonding_with_mode(struct skeletons *skeletons, int mode, int xmit_policy)
275 if (bonding_setup(skeletons, mode, xmit_policy, BOND_BOTH_AND_ATTACH))
278 if (send_udp_packets(xmit_policy != BOND_XMIT_POLICY_LAYER34))
281 bond1_rx = get_rx_packets("bond1");
282 ASSERT_EQ(bond1_rx, NPACKETS, "expected more received packets");
285 case BOND_MODE_ROUNDROBIN:
286 case BOND_MODE_XOR: {
287 int veth1_rx = get_rx_packets("veth1_1");
288 int veth2_rx = get_rx_packets("veth1_2");
289 int diff = abs(veth1_rx - veth2_rx);
291 ASSERT_GE(veth1_rx + veth2_rx, NPACKETS, "expected more packets");
293 switch (xmit_policy) {
294 case BOND_XMIT_POLICY_LAYER2:
295 ASSERT_GE(diff, NPACKETS,
296 "expected packets on only one of the interfaces");
298 case BOND_XMIT_POLICY_LAYER23:
299 case BOND_XMIT_POLICY_LAYER34:
300 ASSERT_LT(diff, NPACKETS/2,
301 "expected even distribution of packets");
304 PRINT_FAIL("Unimplemented xmit_policy=%d\n", xmit_policy);
309 case BOND_MODE_ACTIVEBACKUP: {
310 int veth1_rx = get_rx_packets("veth1_1");
311 int veth2_rx = get_rx_packets("veth1_2");
312 int diff = abs(veth1_rx - veth2_rx);
314 ASSERT_GE(diff, NPACKETS,
315 "expected packets on only one of the interfaces");
319 PRINT_FAIL("Unimplemented xmit_policy=%d\n", xmit_policy);
324 bonding_cleanup(skeletons);
327 /* Test the broadcast redirection using xdp_redirect_map_multi_prog and adding
328 * all the interfaces to it and checking that broadcasting won't send the packet
329 * to neither the ingress bond device (bond2) or its slave (veth2_1).
331 static void test_xdp_bonding_redirect_multi(struct skeletons *skeletons)
333 static const char * const ifaces[] = {"bond2", "veth2_1", "veth2_2"};
334 int veth1_1_rx, veth1_2_rx;
337 if (bonding_setup(skeletons, BOND_MODE_ROUNDROBIN, BOND_XMIT_POLICY_LAYER23,
342 if (!ASSERT_OK(setns_by_name("ns_dst"), "could not set netns to ns_dst"))
345 /* populate the devmap with the relevant interfaces */
346 for (int i = 0; i < ARRAY_SIZE(ifaces); i++) {
347 int ifindex = if_nametoindex(ifaces[i]);
348 int map_fd = bpf_map__fd(skeletons->xdp_redirect_multi_kern->maps.map_all);
350 if (!ASSERT_GT(ifindex, 0, "could not get interface index"))
353 err = bpf_map_update_elem(map_fd, &ifindex, &ifindex, 0);
354 if (!ASSERT_OK(err, "add interface to map_all"))
358 if (xdp_attach(skeletons,
359 skeletons->xdp_redirect_multi_kern->progs.xdp_redirect_map_multi_prog,
363 restore_root_netns();
365 if (send_udp_packets(BOND_MODE_ROUNDROBIN))
368 veth1_1_rx = get_rx_packets("veth1_1");
369 veth1_2_rx = get_rx_packets("veth1_2");
371 ASSERT_EQ(veth1_1_rx, 0, "expected no packets on veth1_1");
372 ASSERT_GE(veth1_2_rx, NPACKETS, "expected packets on veth1_2");
375 restore_root_netns();
376 bonding_cleanup(skeletons);
379 /* Test that XDP programs cannot be attached to both the bond master and slaves simultaneously */
380 static void test_xdp_bonding_attach(struct skeletons *skeletons)
382 struct bpf_link *link = NULL;
383 struct bpf_link *link2 = NULL;
386 if (!ASSERT_OK(system("ip link add veth type veth"), "add veth"))
388 if (!ASSERT_OK(system("ip link add bond type bond"), "add bond"))
391 veth = if_nametoindex("veth");
392 if (!ASSERT_GE(veth, 0, "if_nametoindex veth"))
394 bond = if_nametoindex("bond");
395 if (!ASSERT_GE(bond, 0, "if_nametoindex bond"))
398 /* enslaving with a XDP program loaded is allowed */
399 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, veth);
400 if (!ASSERT_OK_PTR(link, "attach program to veth"))
403 err = system("ip link set veth master bond");
404 if (!ASSERT_OK(err, "set veth master"))
407 bpf_link__destroy(link);
410 /* attaching to slave when master has no program is allowed */
411 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, veth);
412 if (!ASSERT_OK_PTR(link, "attach program to slave when enslaved"))
415 /* attaching to master not allowed when slave has program loaded */
416 link2 = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, bond);
417 if (!ASSERT_ERR_PTR(link2, "attach program to master when slave has program"))
420 bpf_link__destroy(link);
423 /* attaching XDP program to master allowed when slave has no program */
424 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, bond);
425 if (!ASSERT_OK_PTR(link, "attach program to master"))
428 /* attaching to slave not allowed when master has program loaded */
429 link2 = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, veth);
430 if (!ASSERT_ERR_PTR(link2, "attach program to slave when master has program"))
433 bpf_link__destroy(link);
436 /* test program unwinding with a non-XDP slave */
437 if (!ASSERT_OK(system("ip link add vxlan type vxlan id 1 remote 1.2.3.4 dstport 0 dev lo"),
441 err = system("ip link set vxlan master bond");
442 if (!ASSERT_OK(err, "set vxlan master"))
445 /* attaching not allowed when one slave does not support XDP */
446 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, bond);
447 if (!ASSERT_ERR_PTR(link, "attach program to master when slave does not support XDP"))
451 bpf_link__destroy(link);
452 bpf_link__destroy(link2);
454 system("ip link del veth");
455 system("ip link del bond");
456 system("ip link del vxlan");
459 /* Test with nested bonding devices to catch issue with negative jump label count */
460 static void test_xdp_bonding_nested(struct skeletons *skeletons)
462 struct bpf_link *link = NULL;
465 if (!ASSERT_OK(system("ip link add bond type bond"), "add bond"))
468 bond = if_nametoindex("bond");
469 if (!ASSERT_GE(bond, 0, "if_nametoindex bond"))
472 if (!ASSERT_OK(system("ip link add bond_nest1 type bond"), "add bond_nest1"))
475 err = system("ip link set bond_nest1 master bond");
476 if (!ASSERT_OK(err, "set bond_nest1 master"))
479 if (!ASSERT_OK(system("ip link add bond_nest2 type bond"), "add bond_nest1"))
482 err = system("ip link set bond_nest2 master bond_nest1");
483 if (!ASSERT_OK(err, "set bond_nest2 master"))
486 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, bond);
487 ASSERT_OK_PTR(link, "attach program to master");
490 bpf_link__destroy(link);
491 system("ip link del bond");
492 system("ip link del bond_nest1");
493 system("ip link del bond_nest2");
496 static void test_xdp_bonding_features(struct skeletons *skeletons)
498 LIBBPF_OPTS(bpf_xdp_query_opts, query_opts);
499 int bond_idx, veth1_idx, err;
500 struct bpf_link *link = NULL;
502 if (!ASSERT_OK(system("ip link add bond type bond"), "add bond"))
505 bond_idx = if_nametoindex("bond");
506 if (!ASSERT_GE(bond_idx, 0, "if_nametoindex bond"))
509 /* query default xdp-feature for bond device */
510 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts);
511 if (!ASSERT_OK(err, "bond bpf_xdp_query"))
514 if (!ASSERT_EQ(query_opts.feature_flags, 0,
515 "bond query_opts.feature_flags"))
518 if (!ASSERT_OK(system("ip link add veth0 type veth peer name veth1"),
519 "add veth{0,1} pair"))
522 if (!ASSERT_OK(system("ip link add veth2 type veth peer name veth3"),
523 "add veth{2,3} pair"))
526 if (!ASSERT_OK(system("ip link set veth0 master bond"),
527 "add veth0 to master bond"))
530 /* xdp-feature for bond device should be obtained from the single slave
533 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts);
534 if (!ASSERT_OK(err, "bond bpf_xdp_query"))
537 if (!ASSERT_EQ(query_opts.feature_flags,
538 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
539 NETDEV_XDP_ACT_RX_SG,
540 "bond query_opts.feature_flags"))
543 veth1_idx = if_nametoindex("veth1");
544 if (!ASSERT_GE(veth1_idx, 0, "if_nametoindex veth1"))
547 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog,
549 if (!ASSERT_OK_PTR(link, "attach program to veth1"))
552 /* xdp-feature for veth0 are changed */
553 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts);
554 if (!ASSERT_OK(err, "bond bpf_xdp_query"))
557 if (!ASSERT_EQ(query_opts.feature_flags,
558 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
559 NETDEV_XDP_ACT_RX_SG | NETDEV_XDP_ACT_NDO_XMIT |
560 NETDEV_XDP_ACT_NDO_XMIT_SG,
561 "bond query_opts.feature_flags"))
564 if (!ASSERT_OK(system("ip link set veth2 master bond"),
565 "add veth2 to master bond"))
568 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts);
569 if (!ASSERT_OK(err, "bond bpf_xdp_query"))
572 /* xdp-feature for bond device should be set to the most restrict
573 * value obtained from attached slave devices (veth0 and veth2)
575 if (!ASSERT_EQ(query_opts.feature_flags,
576 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
577 NETDEV_XDP_ACT_RX_SG,
578 "bond query_opts.feature_flags"))
581 if (!ASSERT_OK(system("ip link set veth2 nomaster"),
582 "del veth2 to master bond"))
585 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts);
586 if (!ASSERT_OK(err, "bond bpf_xdp_query"))
589 if (!ASSERT_EQ(query_opts.feature_flags,
590 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
591 NETDEV_XDP_ACT_RX_SG | NETDEV_XDP_ACT_NDO_XMIT |
592 NETDEV_XDP_ACT_NDO_XMIT_SG,
593 "bond query_opts.feature_flags"))
596 if (!ASSERT_OK(system("ip link set veth0 nomaster"),
597 "del veth0 to master bond"))
600 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts);
601 if (!ASSERT_OK(err, "bond bpf_xdp_query"))
604 ASSERT_EQ(query_opts.feature_flags, 0,
605 "bond query_opts.feature_flags");
607 bpf_link__destroy(link);
608 system("ip link del veth0");
609 system("ip link del veth2");
610 system("ip link del bond");
613 static int libbpf_debug_print(enum libbpf_print_level level,
614 const char *format, va_list args)
616 if (level != LIBBPF_WARN)
617 vprintf(format, args);
621 struct bond_test_case {
627 static struct bond_test_case bond_test_cases[] = {
628 { "xdp_bonding_roundrobin", BOND_MODE_ROUNDROBIN, BOND_XMIT_POLICY_LAYER23, },
629 { "xdp_bonding_activebackup", BOND_MODE_ACTIVEBACKUP, BOND_XMIT_POLICY_LAYER23 },
631 { "xdp_bonding_xor_layer2", BOND_MODE_XOR, BOND_XMIT_POLICY_LAYER2, },
632 { "xdp_bonding_xor_layer23", BOND_MODE_XOR, BOND_XMIT_POLICY_LAYER23, },
633 { "xdp_bonding_xor_layer34", BOND_MODE_XOR, BOND_XMIT_POLICY_LAYER34, },
636 void serial_test_xdp_bonding(void)
638 libbpf_print_fn_t old_print_fn;
639 struct skeletons skeletons = {};
642 old_print_fn = libbpf_set_print(libbpf_debug_print);
644 root_netns_fd = open("/proc/self/ns/net", O_RDONLY);
645 if (!ASSERT_GE(root_netns_fd, 0, "open /proc/self/ns/net"))
648 skeletons.xdp_dummy = xdp_dummy__open_and_load();
649 if (!ASSERT_OK_PTR(skeletons.xdp_dummy, "xdp_dummy__open_and_load"))
652 skeletons.xdp_tx = xdp_tx__open_and_load();
653 if (!ASSERT_OK_PTR(skeletons.xdp_tx, "xdp_tx__open_and_load"))
656 skeletons.xdp_redirect_multi_kern = xdp_redirect_multi_kern__open_and_load();
657 if (!ASSERT_OK_PTR(skeletons.xdp_redirect_multi_kern,
658 "xdp_redirect_multi_kern__open_and_load"))
661 if (test__start_subtest("xdp_bonding_attach"))
662 test_xdp_bonding_attach(&skeletons);
664 if (test__start_subtest("xdp_bonding_nested"))
665 test_xdp_bonding_nested(&skeletons);
667 if (test__start_subtest("xdp_bonding_features"))
668 test_xdp_bonding_features(&skeletons);
670 for (i = 0; i < ARRAY_SIZE(bond_test_cases); i++) {
671 struct bond_test_case *test_case = &bond_test_cases[i];
673 if (test__start_subtest(test_case->name))
674 test_xdp_bonding_with_mode(
677 test_case->xmit_policy);
680 if (test__start_subtest("xdp_bonding_redirect_multi"))
681 test_xdp_bonding_redirect_multi(&skeletons);
684 xdp_dummy__destroy(skeletons.xdp_dummy);
685 xdp_tx__destroy(skeletons.xdp_tx);
686 xdp_redirect_multi_kern__destroy(skeletons.xdp_redirect_multi_kern);
688 libbpf_set_print(old_print_fn);
689 if (root_netns_fd >= 0)
690 close(root_netns_fd);