Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 841 lines 24 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2 3/** 4 * Test XDP bonding support 5 * 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. 10 */ 11 12#define _GNU_SOURCE 13#include <sched.h> 14#include <net/if.h> 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 <netinet/udp.h> 21#include <uapi/linux/netdev.h> 22 23#include "xdp_dummy.skel.h" 24#include "xdp_redirect_multi_kern.skel.h" 25#include "xdp_tx.skel.h" 26 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" 31#define NPACKETS 100 32 33static int root_netns_fd = -1; 34 35static void restore_root_netns(void) 36{ 37 ASSERT_OK(setns(root_netns_fd, CLONE_NEWNET), "restore_root_netns"); 38} 39 40static int setns_by_name(char *name) 41{ 42 int nsfd, err; 43 char nspath[PATH_MAX]; 44 45 snprintf(nspath, sizeof(nspath), "%s/%s", "/var/run/netns", name); 46 nsfd = open(nspath, O_RDONLY | O_CLOEXEC); 47 if (nsfd < 0) 48 return -1; 49 50 err = setns(nsfd, CLONE_NEWNET); 51 close(nsfd); 52 return err; 53} 54 55static int get_rx_packets(const char *iface) 56{ 57 FILE *f; 58 char line[512]; 59 int iface_len = strlen(iface); 60 61 f = fopen("/proc/net/dev", "r"); 62 if (!f) 63 return -1; 64 65 while (fgets(line, sizeof(line), f)) { 66 char *p = line; 67 68 while (*p == ' ') 69 p++; /* skip whitespace */ 70 if (!strncmp(p, iface, iface_len)) { 71 p += iface_len; 72 if (*p++ != ':') 73 continue; 74 while (*p == ' ') 75 p++; /* skip whitespace */ 76 while (*p && *p != ' ') 77 p++; /* skip rx bytes */ 78 while (*p == ' ') 79 p++; /* skip whitespace */ 80 fclose(f); 81 return atoi(p); 82 } 83 } 84 fclose(f); 85 return -1; 86} 87 88#define MAX_BPF_LINKS 8 89 90struct skeletons { 91 struct xdp_dummy *xdp_dummy; 92 struct xdp_tx *xdp_tx; 93 struct xdp_redirect_multi_kern *xdp_redirect_multi_kern; 94 95 int nlinks; 96 struct bpf_link *links[MAX_BPF_LINKS]; 97}; 98 99static int xdp_attach(struct skeletons *skeletons, struct bpf_program *prog, char *iface) 100{ 101 struct bpf_link *link; 102 int ifindex; 103 104 ifindex = if_nametoindex(iface); 105 if (!ASSERT_GT(ifindex, 0, "get ifindex")) 106 return -1; 107 108 if (!ASSERT_LE(skeletons->nlinks+1, MAX_BPF_LINKS, "too many XDP programs attached")) 109 return -1; 110 111 link = bpf_program__attach_xdp(prog, ifindex); 112 if (!ASSERT_OK_PTR(link, "attach xdp program")) 113 return -1; 114 115 skeletons->links[skeletons->nlinks++] = link; 116 return 0; 117} 118 119enum { 120 BOND_ONE_NO_ATTACH = 0, 121 BOND_BOTH_AND_ATTACH, 122}; 123 124static 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", 132}; 133 134static 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", 140}; 141 142static int bonding_setup(struct skeletons *skeletons, int mode, int xmit_policy, 143 int bond_both_attach) 144{ 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"); 148 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"); 155 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"); 159 } else { 160 SYS(fail, "ip link set veth1_2 up addrgenmode none"); 161 162 if (xdp_attach(skeletons, skeletons->xdp_dummy->progs.xdp_dummy_prog, "veth1_2")) 163 return -1; 164 } 165 166 SYS(fail, "ip -netns ns_dst link set veth2_1 master bond2"); 167 168 if (bond_both_attach == BOND_BOTH_AND_ATTACH) 169 SYS(fail, "ip -netns ns_dst link set veth2_2 master bond2"); 170 else 171 SYS(fail, "ip -netns ns_dst link set veth2_2 up addrgenmode none"); 172 173 /* Load a dummy program on sending side as with veth peer needs to have a 174 * XDP program loaded as well. 175 */ 176 if (xdp_attach(skeletons, skeletons->xdp_dummy->progs.xdp_dummy_prog, "bond1")) 177 return -1; 178 179 if (bond_both_attach == BOND_BOTH_AND_ATTACH) { 180 if (!ASSERT_OK(setns_by_name("ns_dst"), "set netns to ns_dst")) 181 return -1; 182 183 if (xdp_attach(skeletons, skeletons->xdp_tx->progs.xdp_tx, "bond2")) 184 return -1; 185 186 restore_root_netns(); 187 } 188 189 return 0; 190fail: 191 return -1; 192} 193 194static void link_cleanup(struct skeletons *skeletons) 195{ 196 while (skeletons->nlinks) { 197 skeletons->nlinks--; 198 bpf_link__destroy(skeletons->links[skeletons->nlinks]); 199 } 200} 201 202static void bonding_cleanup(struct skeletons *skeletons) 203{ 204 restore_root_netns(); 205 link_cleanup(skeletons); 206 ASSERT_OK(system("ip link delete bond1"), "delete bond1"); 207 ASSERT_OK(system("ip link delete veth1_1"), "delete veth1_1"); 208 ASSERT_OK(system("ip link delete veth1_2"), "delete veth1_2"); 209 ASSERT_OK(system("ip netns delete ns_dst"), "delete ns_dst"); 210} 211 212static int send_udp_packets(int vary_dst_ip) 213{ 214 struct ethhdr eh = { 215 .h_source = BOND1_MAC, 216 .h_dest = BOND2_MAC, 217 .h_proto = htons(ETH_P_IP), 218 }; 219 struct iphdr iph = {}; 220 struct udphdr uh = {}; 221 uint8_t buf[128]; 222 int i, s = -1; 223 int ifindex; 224 225 s = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW); 226 if (!ASSERT_GE(s, 0, "socket")) 227 goto err; 228 229 ifindex = if_nametoindex("bond1"); 230 if (!ASSERT_GT(ifindex, 0, "get bond1 ifindex")) 231 goto err; 232 233 iph.ihl = 5; 234 iph.version = 4; 235 iph.tos = 16; 236 iph.id = 1; 237 iph.ttl = 64; 238 iph.protocol = IPPROTO_UDP; 239 iph.saddr = 1; 240 iph.daddr = 2; 241 iph.tot_len = htons(sizeof(buf) - ETH_HLEN); 242 iph.check = 0; 243 244 for (i = 1; i <= NPACKETS; i++) { 245 int n; 246 struct sockaddr_ll saddr_ll = { 247 .sll_ifindex = ifindex, 248 .sll_halen = ETH_ALEN, 249 .sll_addr = BOND2_MAC, 250 }; 251 252 /* vary the UDP destination port for even distribution with roundrobin/xor modes */ 253 uh.dest++; 254 255 if (vary_dst_ip) 256 iph.daddr++; 257 258 /* construct a packet */ 259 memcpy(buf, &eh, sizeof(eh)); 260 memcpy(buf + sizeof(eh), &iph, sizeof(iph)); 261 memcpy(buf + sizeof(eh) + sizeof(iph), &uh, sizeof(uh)); 262 263 n = sendto(s, buf, sizeof(buf), 0, (struct sockaddr *)&saddr_ll, sizeof(saddr_ll)); 264 if (!ASSERT_EQ(n, sizeof(buf), "sendto")) 265 goto err; 266 } 267 268 return 0; 269 270err: 271 if (s >= 0) 272 close(s); 273 return -1; 274} 275 276static void test_xdp_bonding_with_mode(struct skeletons *skeletons, int mode, int xmit_policy) 277{ 278 int bond1_rx; 279 280 if (bonding_setup(skeletons, mode, xmit_policy, BOND_BOTH_AND_ATTACH)) 281 goto out; 282 283 if (send_udp_packets(xmit_policy != BOND_XMIT_POLICY_LAYER34)) 284 goto out; 285 286 bond1_rx = get_rx_packets("bond1"); 287 ASSERT_EQ(bond1_rx, NPACKETS, "expected more received packets"); 288 289 switch (mode) { 290 case BOND_MODE_ROUNDROBIN: 291 case BOND_MODE_XOR: { 292 int veth1_rx = get_rx_packets("veth1_1"); 293 int veth2_rx = get_rx_packets("veth1_2"); 294 int diff = abs(veth1_rx - veth2_rx); 295 296 ASSERT_GE(veth1_rx + veth2_rx, NPACKETS, "expected more packets"); 297 298 switch (xmit_policy) { 299 case BOND_XMIT_POLICY_LAYER2: 300 ASSERT_GE(diff, NPACKETS, 301 "expected packets on only one of the interfaces"); 302 break; 303 case BOND_XMIT_POLICY_LAYER23: 304 case BOND_XMIT_POLICY_LAYER34: 305 ASSERT_LT(diff, NPACKETS/2, 306 "expected even distribution of packets"); 307 break; 308 default: 309 PRINT_FAIL("Unimplemented xmit_policy=%d\n", xmit_policy); 310 break; 311 } 312 break; 313 } 314 case BOND_MODE_ACTIVEBACKUP: { 315 int veth1_rx = get_rx_packets("veth1_1"); 316 int veth2_rx = get_rx_packets("veth1_2"); 317 int diff = abs(veth1_rx - veth2_rx); 318 319 ASSERT_GE(diff, NPACKETS, 320 "expected packets on only one of the interfaces"); 321 break; 322 } 323 default: 324 PRINT_FAIL("Unimplemented xmit_policy=%d\n", xmit_policy); 325 break; 326 } 327 328out: 329 bonding_cleanup(skeletons); 330} 331 332/* Test the broadcast redirection using xdp_redirect_map_multi_prog and adding 333 * all the interfaces to it and checking that broadcasting won't send the packet 334 * to neither the ingress bond device (bond2) or its slave (veth2_1). 335 */ 336static void test_xdp_bonding_redirect_multi(struct skeletons *skeletons) 337{ 338 static const char * const ifaces[] = {"bond2", "veth2_1", "veth2_2"}; 339 int veth1_1_rx, veth1_2_rx; 340 int err; 341 342 if (bonding_setup(skeletons, BOND_MODE_ROUNDROBIN, BOND_XMIT_POLICY_LAYER23, 343 BOND_ONE_NO_ATTACH)) 344 goto out; 345 346 347 if (!ASSERT_OK(setns_by_name("ns_dst"), "could not set netns to ns_dst")) 348 goto out; 349 350 /* populate the devmap with the relevant interfaces */ 351 for (int i = 0; i < ARRAY_SIZE(ifaces); i++) { 352 int ifindex = if_nametoindex(ifaces[i]); 353 int map_fd = bpf_map__fd(skeletons->xdp_redirect_multi_kern->maps.map_all); 354 355 if (!ASSERT_GT(ifindex, 0, "could not get interface index")) 356 goto out; 357 358 err = bpf_map_update_elem(map_fd, &ifindex, &ifindex, 0); 359 if (!ASSERT_OK(err, "add interface to map_all")) 360 goto out; 361 } 362 363 if (xdp_attach(skeletons, 364 skeletons->xdp_redirect_multi_kern->progs.xdp_redirect_map_multi_prog, 365 "bond2")) 366 goto out; 367 368 restore_root_netns(); 369 370 if (send_udp_packets(BOND_MODE_ROUNDROBIN)) 371 goto out; 372 373 veth1_1_rx = get_rx_packets("veth1_1"); 374 veth1_2_rx = get_rx_packets("veth1_2"); 375 376 ASSERT_EQ(veth1_1_rx, 0, "expected no packets on veth1_1"); 377 ASSERT_GE(veth1_2_rx, NPACKETS, "expected packets on veth1_2"); 378 379out: 380 restore_root_netns(); 381 bonding_cleanup(skeletons); 382} 383 384/* Test that XDP programs cannot be attached to both the bond master and slaves simultaneously */ 385static void test_xdp_bonding_attach(struct skeletons *skeletons) 386{ 387 struct bpf_link *link = NULL; 388 struct bpf_link *link2 = NULL; 389 int veth, bond, err; 390 391 if (!ASSERT_OK(system("ip link add veth type veth"), "add veth")) 392 goto out; 393 if (!ASSERT_OK(system("ip link add bond type bond"), "add bond")) 394 goto out; 395 396 veth = if_nametoindex("veth"); 397 if (!ASSERT_GE(veth, 0, "if_nametoindex veth")) 398 goto out; 399 bond = if_nametoindex("bond"); 400 if (!ASSERT_GE(bond, 0, "if_nametoindex bond")) 401 goto out; 402 403 /* enslaving with a XDP program loaded is allowed */ 404 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, veth); 405 if (!ASSERT_OK_PTR(link, "attach program to veth")) 406 goto out; 407 408 err = system("ip link set veth master bond"); 409 if (!ASSERT_OK(err, "set veth master")) 410 goto out; 411 412 bpf_link__destroy(link); 413 link = NULL; 414 415 /* attaching to slave when master has no program is allowed */ 416 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, veth); 417 if (!ASSERT_OK_PTR(link, "attach program to slave when enslaved")) 418 goto out; 419 420 /* attaching to master not allowed when slave has program loaded */ 421 link2 = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, bond); 422 if (!ASSERT_ERR_PTR(link2, "attach program to master when slave has program")) 423 goto out; 424 425 bpf_link__destroy(link); 426 link = NULL; 427 428 /* attaching XDP program to master allowed when slave has no program */ 429 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, bond); 430 if (!ASSERT_OK_PTR(link, "attach program to master")) 431 goto out; 432 433 /* attaching to slave not allowed when master has program loaded */ 434 link2 = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, veth); 435 if (!ASSERT_ERR_PTR(link2, "attach program to slave when master has program")) 436 goto out; 437 438 bpf_link__destroy(link); 439 link = NULL; 440 441 /* test program unwinding with a non-XDP slave */ 442 if (!ASSERT_OK(system("ip link add vxlan type vxlan id 1 remote 1.2.3.4 dstport 0 dev lo"), 443 "add vxlan")) 444 goto out; 445 446 err = system("ip link set vxlan master bond"); 447 if (!ASSERT_OK(err, "set vxlan master")) 448 goto out; 449 450 /* attaching not allowed when one slave does not support XDP */ 451 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, bond); 452 if (!ASSERT_ERR_PTR(link, "attach program to master when slave does not support XDP")) 453 goto out; 454 455out: 456 bpf_link__destroy(link); 457 bpf_link__destroy(link2); 458 459 system("ip link del veth"); 460 system("ip link del bond"); 461 system("ip link del vxlan"); 462} 463 464/* Test with nested bonding devices to catch issue with negative jump label count */ 465static void test_xdp_bonding_nested(struct skeletons *skeletons) 466{ 467 struct bpf_link *link = NULL; 468 int bond, err; 469 470 if (!ASSERT_OK(system("ip link add bond type bond"), "add bond")) 471 goto out; 472 473 bond = if_nametoindex("bond"); 474 if (!ASSERT_GE(bond, 0, "if_nametoindex bond")) 475 goto out; 476 477 if (!ASSERT_OK(system("ip link add bond_nest1 type bond"), "add bond_nest1")) 478 goto out; 479 480 err = system("ip link set bond_nest1 master bond"); 481 if (!ASSERT_OK(err, "set bond_nest1 master")) 482 goto out; 483 484 if (!ASSERT_OK(system("ip link add bond_nest2 type bond"), "add bond_nest1")) 485 goto out; 486 487 err = system("ip link set bond_nest2 master bond_nest1"); 488 if (!ASSERT_OK(err, "set bond_nest2 master")) 489 goto out; 490 491 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, bond); 492 ASSERT_OK_PTR(link, "attach program to master"); 493 494out: 495 bpf_link__destroy(link); 496 system("ip link del bond"); 497 system("ip link del bond_nest1"); 498 system("ip link del bond_nest2"); 499} 500 501/* 502 * Test that XDP redirect via xdp_master_redirect() does not crash when 503 * the bond master device is not up. When bond is in round-robin mode but 504 * never opened, rr_tx_counter is NULL. 505 */ 506static void test_xdp_bonding_redirect_no_up(struct skeletons *skeletons) 507{ 508 struct nstoken *nstoken = NULL; 509 int xdp_pass_fd; 510 int veth1_ifindex; 511 int err; 512 char pkt[ETH_HLEN + 1]; 513 struct xdp_md ctx_in = {}; 514 515 DECLARE_LIBBPF_OPTS(bpf_test_run_opts, opts, 516 .data_in = &pkt, 517 .data_size_in = sizeof(pkt), 518 .ctx_in = &ctx_in, 519 .ctx_size_in = sizeof(ctx_in), 520 .flags = BPF_F_TEST_XDP_LIVE_FRAMES, 521 .repeat = 1, 522 .batch_size = 1, 523 ); 524 525 /* We can't use bonding_setup() because bond will be active */ 526 SYS(out, "ip netns add ns_rr_no_up"); 527 nstoken = open_netns("ns_rr_no_up"); 528 if (!ASSERT_OK_PTR(nstoken, "open ns_rr_no_up")) 529 goto out; 530 531 /* bond0: active-backup, UP with slave veth0. 532 * Attaching native XDP to bond0 enables bpf_master_redirect_enabled_key 533 * globally. 534 */ 535 SYS(out, "ip link add bond0 type bond mode active-backup"); 536 SYS(out, "ip link add veth0 type veth peer name veth0p"); 537 SYS(out, "ip link set veth0 master bond0"); 538 SYS(out, "ip link set bond0 up"); 539 SYS(out, "ip link set veth0p up"); 540 541 /* bond1: round-robin, never UP -> rr_tx_counter stays NULL */ 542 SYS(out, "ip link add bond1 type bond mode balance-rr"); 543 SYS(out, "ip link add veth1 type veth peer name veth1p"); 544 SYS(out, "ip link set veth1 master bond1"); 545 546 veth1_ifindex = if_nametoindex("veth1"); 547 if (!ASSERT_GT(veth1_ifindex, 0, "veth1_ifindex")) 548 goto out; 549 550 /* Attach native XDP to bond0 -> enables global redirect key */ 551 if (xdp_attach(skeletons, skeletons->xdp_tx->progs.xdp_tx, "bond0")) 552 goto out; 553 554 /* Attach generic XDP (XDP_TX) to veth1. 555 * When packets arrive at veth1 via netif_receive_skb, do_xdp_generic() 556 * runs this program. XDP_TX + bond slave triggers xdp_master_redirect(). 557 */ 558 err = bpf_xdp_attach(veth1_ifindex, 559 bpf_program__fd(skeletons->xdp_tx->progs.xdp_tx), 560 XDP_FLAGS_SKB_MODE, NULL); 561 if (!ASSERT_OK(err, "attach generic XDP to veth1")) 562 goto out; 563 564 /* Run BPF_PROG_TEST_RUN with XDP_PASS live frames on veth1. 565 * XDP_PASS frames become SKBs with skb->dev = veth1, entering 566 * netif_receive_skb -> do_xdp_generic -> xdp_master_redirect. 567 * Without the fix, bond_rr_gen_slave_id() dereferences NULL 568 * rr_tx_counter and crashes. 569 */ 570 xdp_pass_fd = bpf_program__fd(skeletons->xdp_dummy->progs.xdp_dummy_prog); 571 572 memset(pkt, 0, sizeof(pkt)); 573 ctx_in.data_end = sizeof(pkt); 574 ctx_in.ingress_ifindex = veth1_ifindex; 575 576 err = bpf_prog_test_run_opts(xdp_pass_fd, &opts); 577 ASSERT_OK(err, "xdp_pass test_run should not crash"); 578 579out: 580 link_cleanup(skeletons); 581 close_netns(nstoken); 582 SYS_NOFAIL("ip netns del ns_rr_no_up"); 583} 584 585static void test_xdp_bonding_features(struct skeletons *skeletons) 586{ 587 LIBBPF_OPTS(bpf_xdp_query_opts, query_opts); 588 int bond_idx, veth1_idx, err; 589 struct bpf_link *link = NULL; 590 591 if (!ASSERT_OK(system("ip link add bond type bond"), "add bond")) 592 goto out; 593 594 bond_idx = if_nametoindex("bond"); 595 if (!ASSERT_GE(bond_idx, 0, "if_nametoindex bond")) 596 goto out; 597 598 /* query default xdp-feature for bond device */ 599 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts); 600 if (!ASSERT_OK(err, "bond bpf_xdp_query")) 601 goto out; 602 603 if (!ASSERT_EQ(query_opts.feature_flags, 0, 604 "bond query_opts.feature_flags")) 605 goto out; 606 607 if (!ASSERT_OK(system("ip link add veth0 type veth peer name veth1"), 608 "add veth{0,1} pair")) 609 goto out; 610 611 if (!ASSERT_OK(system("ip link add veth2 type veth peer name veth3"), 612 "add veth{2,3} pair")) 613 goto out; 614 615 if (!ASSERT_OK(system("ip link set veth0 master bond"), 616 "add veth0 to master bond")) 617 goto out; 618 619 /* xdp-feature for bond device should be obtained from the single slave 620 * device (veth0) 621 */ 622 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts); 623 if (!ASSERT_OK(err, "bond bpf_xdp_query")) 624 goto out; 625 626 if (!ASSERT_EQ(query_opts.feature_flags, 627 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 628 NETDEV_XDP_ACT_RX_SG, 629 "bond query_opts.feature_flags")) 630 goto out; 631 632 veth1_idx = if_nametoindex("veth1"); 633 if (!ASSERT_GE(veth1_idx, 0, "if_nametoindex veth1")) 634 goto out; 635 636 link = bpf_program__attach_xdp(skeletons->xdp_dummy->progs.xdp_dummy_prog, 637 veth1_idx); 638 if (!ASSERT_OK_PTR(link, "attach program to veth1")) 639 goto out; 640 641 /* xdp-feature for veth0 are changed */ 642 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts); 643 if (!ASSERT_OK(err, "bond bpf_xdp_query")) 644 goto out; 645 646 if (!ASSERT_EQ(query_opts.feature_flags, 647 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 648 NETDEV_XDP_ACT_RX_SG | NETDEV_XDP_ACT_NDO_XMIT | 649 NETDEV_XDP_ACT_NDO_XMIT_SG, 650 "bond query_opts.feature_flags")) 651 goto out; 652 653 if (!ASSERT_OK(system("ip link set veth2 master bond"), 654 "add veth2 to master bond")) 655 goto out; 656 657 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts); 658 if (!ASSERT_OK(err, "bond bpf_xdp_query")) 659 goto out; 660 661 /* xdp-feature for bond device should be set to the most restrict 662 * value obtained from attached slave devices (veth0 and veth2) 663 */ 664 if (!ASSERT_EQ(query_opts.feature_flags, 665 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 666 NETDEV_XDP_ACT_RX_SG, 667 "bond query_opts.feature_flags")) 668 goto out; 669 670 if (!ASSERT_OK(system("ip link set veth2 nomaster"), 671 "del veth2 to master bond")) 672 goto out; 673 674 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts); 675 if (!ASSERT_OK(err, "bond bpf_xdp_query")) 676 goto out; 677 678 if (!ASSERT_EQ(query_opts.feature_flags, 679 NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 680 NETDEV_XDP_ACT_RX_SG | NETDEV_XDP_ACT_NDO_XMIT | 681 NETDEV_XDP_ACT_NDO_XMIT_SG, 682 "bond query_opts.feature_flags")) 683 goto out; 684 685 if (!ASSERT_OK(system("ip link set veth0 nomaster"), 686 "del veth0 to master bond")) 687 goto out; 688 689 err = bpf_xdp_query(bond_idx, XDP_FLAGS_DRV_MODE, &query_opts); 690 if (!ASSERT_OK(err, "bond bpf_xdp_query")) 691 goto out; 692 693 ASSERT_EQ(query_opts.feature_flags, 0, 694 "bond query_opts.feature_flags"); 695out: 696 bpf_link__destroy(link); 697 system("ip link del veth0"); 698 system("ip link del veth2"); 699 system("ip link del bond"); 700} 701 702/* 703 * Test that changing xmit_hash_policy to vlan+srcmac is rejected when a 704 * native XDP program is loaded on a bond in 802.3ad or balance-xor mode. 705 * These modes support XDP only when xmit_hash_policy != vlan+srcmac; freely 706 * changing the policy creates an inconsistency that triggers a WARNING in 707 * dev_xdp_uninstall() during device teardown. 708 */ 709static void test_xdp_bonding_xmit_policy_compat(struct skeletons *skeletons) 710{ 711 struct nstoken *nstoken = NULL; 712 int bond_ifindex = -1; 713 int xdp_fd, err; 714 715 SYS(out, "ip netns add ns_xmit_policy"); 716 nstoken = open_netns("ns_xmit_policy"); 717 if (!ASSERT_OK_PTR(nstoken, "open ns_xmit_policy")) 718 goto out; 719 720 /* 802.3ad with layer2+3 policy: native XDP is supported */ 721 SYS(out, "ip link add bond0 type bond mode 802.3ad xmit_hash_policy layer2+3"); 722 SYS(out, "ip link add veth0 type veth peer name veth0p"); 723 SYS(out, "ip link set veth0 master bond0"); 724 SYS(out, "ip link set bond0 up"); 725 726 bond_ifindex = if_nametoindex("bond0"); 727 if (!ASSERT_GT(bond_ifindex, 0, "bond0 ifindex")) 728 goto out; 729 730 xdp_fd = bpf_program__fd(skeletons->xdp_dummy->progs.xdp_dummy_prog); 731 if (!ASSERT_GE(xdp_fd, 0, "xdp_dummy fd")) 732 goto out; 733 734 err = bpf_xdp_attach(bond_ifindex, xdp_fd, XDP_FLAGS_DRV_MODE, NULL); 735 if (!ASSERT_OK(err, "attach XDP to bond0")) 736 goto out; 737 738 /* With XDP loaded, switching to vlan+srcmac must be rejected */ 739 err = system("ip link set bond0 type bond xmit_hash_policy vlan+srcmac 2>/dev/null"); 740 ASSERT_NEQ(err, 0, "vlan+srcmac change with XDP loaded should fail"); 741 742 /* Detach XDP first, then the same change must succeed */ 743 ASSERT_OK(bpf_xdp_detach(bond_ifindex, XDP_FLAGS_DRV_MODE, NULL), 744 "detach XDP from bond0"); 745 746 bond_ifindex = -1; 747 err = system("ip link set bond0 type bond xmit_hash_policy vlan+srcmac 2>/dev/null"); 748 ASSERT_OK(err, "vlan+srcmac change without XDP should succeed"); 749 750out: 751 if (bond_ifindex > 0) 752 bpf_xdp_detach(bond_ifindex, XDP_FLAGS_DRV_MODE, NULL); 753 close_netns(nstoken); 754 SYS_NOFAIL("ip netns del ns_xmit_policy"); 755} 756 757static int libbpf_debug_print(enum libbpf_print_level level, 758 const char *format, va_list args) 759{ 760 if (level != LIBBPF_WARN) 761 vprintf(format, args); 762 return 0; 763} 764 765struct bond_test_case { 766 char *name; 767 int mode; 768 int xmit_policy; 769}; 770 771static struct bond_test_case bond_test_cases[] = { 772 { "xdp_bonding_roundrobin", BOND_MODE_ROUNDROBIN, BOND_XMIT_POLICY_LAYER23, }, 773 { "xdp_bonding_activebackup", BOND_MODE_ACTIVEBACKUP, BOND_XMIT_POLICY_LAYER23 }, 774 775 { "xdp_bonding_xor_layer2", BOND_MODE_XOR, BOND_XMIT_POLICY_LAYER2, }, 776 { "xdp_bonding_xor_layer23", BOND_MODE_XOR, BOND_XMIT_POLICY_LAYER23, }, 777 { "xdp_bonding_xor_layer34", BOND_MODE_XOR, BOND_XMIT_POLICY_LAYER34, }, 778}; 779 780void serial_test_xdp_bonding(void) 781{ 782 libbpf_print_fn_t old_print_fn; 783 struct skeletons skeletons = {}; 784 int i; 785 786 old_print_fn = libbpf_set_print(libbpf_debug_print); 787 788 root_netns_fd = open("/proc/self/ns/net", O_RDONLY); 789 if (!ASSERT_GE(root_netns_fd, 0, "open /proc/self/ns/net")) 790 goto out; 791 792 skeletons.xdp_dummy = xdp_dummy__open_and_load(); 793 if (!ASSERT_OK_PTR(skeletons.xdp_dummy, "xdp_dummy__open_and_load")) 794 goto out; 795 796 skeletons.xdp_tx = xdp_tx__open_and_load(); 797 if (!ASSERT_OK_PTR(skeletons.xdp_tx, "xdp_tx__open_and_load")) 798 goto out; 799 800 skeletons.xdp_redirect_multi_kern = xdp_redirect_multi_kern__open_and_load(); 801 if (!ASSERT_OK_PTR(skeletons.xdp_redirect_multi_kern, 802 "xdp_redirect_multi_kern__open_and_load")) 803 goto out; 804 805 if (test__start_subtest("xdp_bonding_attach")) 806 test_xdp_bonding_attach(&skeletons); 807 808 if (test__start_subtest("xdp_bonding_nested")) 809 test_xdp_bonding_nested(&skeletons); 810 811 if (test__start_subtest("xdp_bonding_features")) 812 test_xdp_bonding_features(&skeletons); 813 814 for (i = 0; i < ARRAY_SIZE(bond_test_cases); i++) { 815 struct bond_test_case *test_case = &bond_test_cases[i]; 816 817 if (test__start_subtest(test_case->name)) 818 test_xdp_bonding_with_mode( 819 &skeletons, 820 test_case->mode, 821 test_case->xmit_policy); 822 } 823 824 if (test__start_subtest("xdp_bonding_xmit_policy_compat")) 825 test_xdp_bonding_xmit_policy_compat(&skeletons); 826 827 if (test__start_subtest("xdp_bonding_redirect_multi")) 828 test_xdp_bonding_redirect_multi(&skeletons); 829 830 if (test__start_subtest("xdp_bonding_redirect_no_up")) 831 test_xdp_bonding_redirect_no_up(&skeletons); 832 833out: 834 xdp_dummy__destroy(skeletons.xdp_dummy); 835 xdp_tx__destroy(skeletons.xdp_tx); 836 xdp_redirect_multi_kern__destroy(skeletons.xdp_redirect_multi_kern); 837 838 libbpf_set_print(old_print_fn); 839 if (root_netns_fd >= 0) 840 close(root_netns_fd); 841}