|
9 | 9 | #define TX_NETNS "xdp_context_tx"
|
10 | 10 | #define RX_NETNS "xdp_context_rx"
|
11 | 11 | #define TAP_NAME "tap0"
|
| 12 | +#define DUMMY_NAME "dum0" |
12 | 13 | #define TAP_NETNS "xdp_context_tuntap"
|
13 | 14 |
|
14 | 15 | #define TEST_PAYLOAD_LEN 32
|
@@ -156,6 +157,22 @@ static int send_test_packet(int ifindex)
|
156 | 157 | return -1;
|
157 | 158 | }
|
158 | 159 |
|
| 160 | +static int write_test_packet(int tap_fd) |
| 161 | +{ |
| 162 | + __u8 packet[sizeof(struct ethhdr) + TEST_PAYLOAD_LEN]; |
| 163 | + int n; |
| 164 | + |
| 165 | + /* The ethernet header doesn't need to be valid for this test */ |
| 166 | + memset(packet, 0, sizeof(struct ethhdr)); |
| 167 | + memcpy(packet + sizeof(struct ethhdr), test_payload, TEST_PAYLOAD_LEN); |
| 168 | + |
| 169 | + n = write(tap_fd, packet, sizeof(packet)); |
| 170 | + if (!ASSERT_EQ(n, sizeof(packet), "write packet")) |
| 171 | + return -1; |
| 172 | + |
| 173 | + return 0; |
| 174 | +} |
| 175 | + |
159 | 176 | static void assert_test_result(const struct bpf_map *result_map)
|
160 | 177 | {
|
161 | 178 | int err;
|
@@ -276,7 +293,6 @@ static void test_tuntap(struct bpf_program *xdp_prog,
|
276 | 293 | LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_INGRESS);
|
277 | 294 | LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 1);
|
278 | 295 | struct netns_obj *ns = NULL;
|
279 |
| - __u8 packet[sizeof(struct ethhdr) + TEST_PAYLOAD_LEN]; |
280 | 296 | int tap_fd = -1;
|
281 | 297 | int tap_ifindex;
|
282 | 298 | int ret;
|
@@ -322,19 +338,82 @@ static void test_tuntap(struct bpf_program *xdp_prog,
|
322 | 338 | if (!ASSERT_GE(ret, 0, "bpf_xdp_attach"))
|
323 | 339 | goto close;
|
324 | 340 |
|
325 |
| - /* The ethernet header is not relevant for this test and doesn't need to |
326 |
| - * be meaningful. |
327 |
| - */ |
328 |
| - struct ethhdr eth = { 0 }; |
| 341 | + ret = write_test_packet(tap_fd); |
| 342 | + if (!ASSERT_OK(ret, "write_test_packet")) |
| 343 | + goto close; |
329 | 344 |
|
330 |
| - memcpy(packet, ð, sizeof(eth)); |
331 |
| - memcpy(packet + sizeof(eth), test_payload, TEST_PAYLOAD_LEN); |
| 345 | + assert_test_result(result_map); |
| 346 | + |
| 347 | +close: |
| 348 | + if (tap_fd >= 0) |
| 349 | + close(tap_fd); |
| 350 | + netns_free(ns); |
| 351 | +} |
| 352 | + |
| 353 | +/* Write a packet to a tap dev and copy it to ingress of a dummy dev */ |
| 354 | +static void test_tuntap_mirred(struct bpf_program *xdp_prog, |
| 355 | + struct bpf_program *tc_prog, |
| 356 | + bool *test_pass) |
| 357 | +{ |
| 358 | + LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_INGRESS); |
| 359 | + LIBBPF_OPTS(bpf_tc_opts, tc_opts, .handle = 1, .priority = 1); |
| 360 | + struct netns_obj *ns = NULL; |
| 361 | + int dummy_ifindex; |
| 362 | + int tap_fd = -1; |
| 363 | + int tap_ifindex; |
| 364 | + int ret; |
332 | 365 |
|
333 |
| - ret = write(tap_fd, packet, sizeof(packet)); |
334 |
| - if (!ASSERT_EQ(ret, sizeof(packet), "write packet")) |
| 366 | + *test_pass = false; |
| 367 | + |
| 368 | + ns = netns_new(TAP_NETNS, true); |
| 369 | + if (!ASSERT_OK_PTR(ns, "netns_new")) |
| 370 | + return; |
| 371 | + |
| 372 | + /* Setup dummy interface */ |
| 373 | + SYS(close, "ip link add name " DUMMY_NAME " type dummy"); |
| 374 | + SYS(close, "ip link set dev " DUMMY_NAME " up"); |
| 375 | + |
| 376 | + dummy_ifindex = if_nametoindex(DUMMY_NAME); |
| 377 | + if (!ASSERT_GE(dummy_ifindex, 0, "if_nametoindex")) |
335 | 378 | goto close;
|
336 | 379 |
|
337 |
| - assert_test_result(result_map); |
| 380 | + tc_hook.ifindex = dummy_ifindex; |
| 381 | + ret = bpf_tc_hook_create(&tc_hook); |
| 382 | + if (!ASSERT_OK(ret, "bpf_tc_hook_create")) |
| 383 | + goto close; |
| 384 | + |
| 385 | + tc_opts.prog_fd = bpf_program__fd(tc_prog); |
| 386 | + ret = bpf_tc_attach(&tc_hook, &tc_opts); |
| 387 | + if (!ASSERT_OK(ret, "bpf_tc_attach")) |
| 388 | + goto close; |
| 389 | + |
| 390 | + /* Setup TAP interface */ |
| 391 | + tap_fd = open_tuntap(TAP_NAME, true); |
| 392 | + if (!ASSERT_GE(tap_fd, 0, "open_tuntap")) |
| 393 | + goto close; |
| 394 | + |
| 395 | + SYS(close, "ip link set dev " TAP_NAME " up"); |
| 396 | + |
| 397 | + tap_ifindex = if_nametoindex(TAP_NAME); |
| 398 | + if (!ASSERT_GE(tap_ifindex, 0, "if_nametoindex")) |
| 399 | + goto close; |
| 400 | + |
| 401 | + ret = bpf_xdp_attach(tap_ifindex, bpf_program__fd(xdp_prog), 0, NULL); |
| 402 | + if (!ASSERT_GE(ret, 0, "bpf_xdp_attach")) |
| 403 | + goto close; |
| 404 | + |
| 405 | + /* Copy all packets received from TAP to dummy ingress */ |
| 406 | + SYS(close, "tc qdisc add dev " TAP_NAME " clsact"); |
| 407 | + SYS(close, "tc filter add dev " TAP_NAME " ingress " |
| 408 | + "protocol all matchall " |
| 409 | + "action mirred ingress mirror dev " DUMMY_NAME); |
| 410 | + |
| 411 | + /* Receive a packet on TAP */ |
| 412 | + ret = write_test_packet(tap_fd); |
| 413 | + if (!ASSERT_OK(ret, "write_test_packet")) |
| 414 | + goto close; |
| 415 | + |
| 416 | + ASSERT_TRUE(*test_pass, "test_pass"); |
338 | 417 |
|
339 | 418 | close:
|
340 | 419 | if (tap_fd >= 0)
|
@@ -385,6 +464,30 @@ void test_xdp_context_tuntap(void)
|
385 | 464 | skel->progs.ing_cls_dynptr_offset_oob,
|
386 | 465 | skel->progs.ing_cls,
|
387 | 466 | skel->maps.test_result);
|
| 467 | + if (test__start_subtest("clone_data_meta_empty_on_data_write")) |
| 468 | + test_tuntap_mirred(skel->progs.ing_xdp, |
| 469 | + skel->progs.clone_data_meta_empty_on_data_write, |
| 470 | + &skel->bss->test_pass); |
| 471 | + if (test__start_subtest("clone_data_meta_empty_on_meta_write")) |
| 472 | + test_tuntap_mirred(skel->progs.ing_xdp, |
| 473 | + skel->progs.clone_data_meta_empty_on_meta_write, |
| 474 | + &skel->bss->test_pass); |
| 475 | + if (test__start_subtest("clone_dynptr_empty_on_data_slice_write")) |
| 476 | + test_tuntap_mirred(skel->progs.ing_xdp, |
| 477 | + skel->progs.clone_dynptr_empty_on_data_slice_write, |
| 478 | + &skel->bss->test_pass); |
| 479 | + if (test__start_subtest("clone_dynptr_empty_on_meta_slice_write")) |
| 480 | + test_tuntap_mirred(skel->progs.ing_xdp, |
| 481 | + skel->progs.clone_dynptr_empty_on_meta_slice_write, |
| 482 | + &skel->bss->test_pass); |
| 483 | + if (test__start_subtest("clone_dynptr_rdonly_before_data_dynptr_write")) |
| 484 | + test_tuntap_mirred(skel->progs.ing_xdp, |
| 485 | + skel->progs.clone_dynptr_rdonly_before_data_dynptr_write, |
| 486 | + &skel->bss->test_pass); |
| 487 | + if (test__start_subtest("clone_dynptr_rdonly_before_meta_dynptr_write")) |
| 488 | + test_tuntap_mirred(skel->progs.ing_xdp, |
| 489 | + skel->progs.clone_dynptr_rdonly_before_meta_dynptr_write, |
| 490 | + &skel->bss->test_pass); |
388 | 491 |
|
389 | 492 | test_xdp_meta__destroy(skel);
|
390 | 493 | }
|
0 commit comments