@@ -364,51 +364,95 @@ static int test_ping(int family, const char *addr)
364
364
return -1 ;
365
365
}
366
366
367
- static int attach_tc_prog (struct bpf_tc_hook * hook , int igr_fd , int egr_fd )
367
+ static int attach_tc_prog (int ifindex , int igr_fd , int egr_fd )
368
368
{
369
+ DECLARE_LIBBPF_OPTS (bpf_tc_hook , hook , .ifindex = ifindex ,
370
+ .attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS );
369
371
DECLARE_LIBBPF_OPTS (bpf_tc_opts , opts1 , .handle = 1 ,
370
372
.priority = 1 , .prog_fd = igr_fd );
371
373
DECLARE_LIBBPF_OPTS (bpf_tc_opts , opts2 , .handle = 1 ,
372
374
.priority = 1 , .prog_fd = egr_fd );
373
375
int ret ;
374
376
375
- ret = bpf_tc_hook_create (hook );
377
+ ret = bpf_tc_hook_create (& hook );
376
378
if (!ASSERT_OK (ret , "create tc hook" ))
377
379
return ret ;
378
380
379
381
if (igr_fd >= 0 ) {
380
- hook -> attach_point = BPF_TC_INGRESS ;
381
- ret = bpf_tc_attach (hook , & opts1 );
382
+ hook . attach_point = BPF_TC_INGRESS ;
383
+ ret = bpf_tc_attach (& hook , & opts1 );
382
384
if (!ASSERT_OK (ret , "bpf_tc_attach" )) {
383
- bpf_tc_hook_destroy (hook );
385
+ bpf_tc_hook_destroy (& hook );
384
386
return ret ;
385
387
}
386
388
}
387
389
388
390
if (egr_fd >= 0 ) {
389
- hook -> attach_point = BPF_TC_EGRESS ;
390
- ret = bpf_tc_attach (hook , & opts2 );
391
+ hook . attach_point = BPF_TC_EGRESS ;
392
+ ret = bpf_tc_attach (& hook , & opts2 );
391
393
if (!ASSERT_OK (ret , "bpf_tc_attach" )) {
392
- bpf_tc_hook_destroy (hook );
394
+ bpf_tc_hook_destroy (& hook );
393
395
return ret ;
394
396
}
395
397
}
396
398
397
399
return 0 ;
398
400
}
399
401
402
+ static int generic_attach (const char * dev , int igr_fd , int egr_fd )
403
+ {
404
+ int ifindex ;
405
+
406
+ if (!ASSERT_OK_FD (igr_fd , "check ingress fd" ))
407
+ return -1 ;
408
+ if (!ASSERT_OK_FD (egr_fd , "check egress fd" ))
409
+ return -1 ;
410
+
411
+ ifindex = if_nametoindex (dev );
412
+ if (!ASSERT_NEQ (ifindex , 0 , "get ifindex" ))
413
+ return -1 ;
414
+
415
+ return attach_tc_prog (ifindex , igr_fd , egr_fd );
416
+ }
417
+
418
+ static int generic_attach_igr (const char * dev , int igr_fd )
419
+ {
420
+ int ifindex ;
421
+
422
+ if (!ASSERT_OK_FD (igr_fd , "check ingress fd" ))
423
+ return -1 ;
424
+
425
+ ifindex = if_nametoindex (dev );
426
+ if (!ASSERT_NEQ (ifindex , 0 , "get ifindex" ))
427
+ return -1 ;
428
+
429
+ return attach_tc_prog (ifindex , igr_fd , -1 );
430
+ }
431
+
432
+ static int generic_attach_egr (const char * dev , int egr_fd )
433
+ {
434
+ int ifindex ;
435
+
436
+ if (!ASSERT_OK_FD (egr_fd , "check egress fd" ))
437
+ return -1 ;
438
+
439
+ ifindex = if_nametoindex (dev );
440
+ if (!ASSERT_NEQ (ifindex , 0 , "get ifindex" ))
441
+ return -1 ;
442
+
443
+ return attach_tc_prog (ifindex , -1 , egr_fd );
444
+ }
445
+
400
446
static void test_vxlan_tunnel (void )
401
447
{
402
448
struct test_tunnel_kern * skel = NULL ;
403
449
struct nstoken * nstoken ;
404
450
int local_ip_map_fd = -1 ;
405
451
int set_src_prog_fd , get_src_prog_fd ;
406
452
int set_dst_prog_fd ;
407
- int key = 0 , ifindex = -1 ;
453
+ int key = 0 ;
408
454
uint local_ip ;
409
455
int err ;
410
- DECLARE_LIBBPF_OPTS (bpf_tc_hook , tc_hook ,
411
- .attach_point = BPF_TC_INGRESS );
412
456
413
457
/* add vxlan tunnel */
414
458
err = add_vxlan_tunnel ();
@@ -419,42 +463,22 @@ static void test_vxlan_tunnel(void)
419
463
skel = test_tunnel_kern__open_and_load ();
420
464
if (!ASSERT_OK_PTR (skel , "test_tunnel_kern__open_and_load" ))
421
465
goto done ;
422
- ifindex = if_nametoindex (VXLAN_TUNL_DEV1 );
423
- if (!ASSERT_NEQ (ifindex , 0 , "vxlan11 ifindex" ))
424
- goto done ;
425
- tc_hook .ifindex = ifindex ;
426
466
get_src_prog_fd = bpf_program__fd (skel -> progs .vxlan_get_tunnel_src );
427
467
set_src_prog_fd = bpf_program__fd (skel -> progs .vxlan_set_tunnel_src );
428
- if (!ASSERT_GE (get_src_prog_fd , 0 , "bpf_program__fd" ))
429
- goto done ;
430
- if (!ASSERT_GE (set_src_prog_fd , 0 , "bpf_program__fd" ))
431
- goto done ;
432
- if (attach_tc_prog (& tc_hook , get_src_prog_fd , set_src_prog_fd ))
468
+ if (generic_attach (VXLAN_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
433
469
goto done ;
434
470
435
471
/* load and attach bpf prog to veth dev tc hook point */
436
- ifindex = if_nametoindex ("veth1" );
437
- if (!ASSERT_NEQ (ifindex , 0 , "veth1 ifindex" ))
438
- goto done ;
439
- tc_hook .ifindex = ifindex ;
440
472
set_dst_prog_fd = bpf_program__fd (skel -> progs .veth_set_outer_dst );
441
- if (!ASSERT_GE (set_dst_prog_fd , 0 , "bpf_program__fd" ))
442
- goto done ;
443
- if (attach_tc_prog (& tc_hook , set_dst_prog_fd , -1 ))
473
+ if (generic_attach_igr ("veth1" , set_dst_prog_fd ))
444
474
goto done ;
445
475
446
476
/* load and attach prog set_md to tunnel dev tc hook point at_ns0 */
447
477
nstoken = open_netns ("at_ns0" );
448
478
if (!ASSERT_OK_PTR (nstoken , "setns src" ))
449
479
goto done ;
450
- ifindex = if_nametoindex (VXLAN_TUNL_DEV0 );
451
- if (!ASSERT_NEQ (ifindex , 0 , "vxlan00 ifindex" ))
452
- goto done ;
453
- tc_hook .ifindex = ifindex ;
454
480
set_dst_prog_fd = bpf_program__fd (skel -> progs .vxlan_set_tunnel_dst );
455
- if (!ASSERT_GE (set_dst_prog_fd , 0 , "bpf_program__fd" ))
456
- goto done ;
457
- if (attach_tc_prog (& tc_hook , -1 , set_dst_prog_fd ))
481
+ if (generic_attach_egr (VXLAN_TUNL_DEV0 , set_dst_prog_fd ))
458
482
goto done ;
459
483
close_netns (nstoken );
460
484
@@ -488,11 +512,9 @@ static void test_ip6vxlan_tunnel(void)
488
512
int local_ip_map_fd = -1 ;
489
513
int set_src_prog_fd , get_src_prog_fd ;
490
514
int set_dst_prog_fd ;
491
- int key = 0 , ifindex = -1 ;
515
+ int key = 0 ;
492
516
uint local_ip ;
493
517
int err ;
494
- DECLARE_LIBBPF_OPTS (bpf_tc_hook , tc_hook ,
495
- .attach_point = BPF_TC_INGRESS );
496
518
497
519
/* add vxlan tunnel */
498
520
err = add_ip6vxlan_tunnel ();
@@ -503,31 +525,17 @@ static void test_ip6vxlan_tunnel(void)
503
525
skel = test_tunnel_kern__open_and_load ();
504
526
if (!ASSERT_OK_PTR (skel , "test_tunnel_kern__open_and_load" ))
505
527
goto done ;
506
- ifindex = if_nametoindex (IP6VXLAN_TUNL_DEV1 );
507
- if (!ASSERT_NEQ (ifindex , 0 , "ip6vxlan11 ifindex" ))
508
- goto done ;
509
- tc_hook .ifindex = ifindex ;
510
528
get_src_prog_fd = bpf_program__fd (skel -> progs .ip6vxlan_get_tunnel_src );
511
529
set_src_prog_fd = bpf_program__fd (skel -> progs .ip6vxlan_set_tunnel_src );
512
- if (!ASSERT_GE (set_src_prog_fd , 0 , "bpf_program__fd" ))
513
- goto done ;
514
- if (!ASSERT_GE (get_src_prog_fd , 0 , "bpf_program__fd" ))
515
- goto done ;
516
- if (attach_tc_prog (& tc_hook , get_src_prog_fd , set_src_prog_fd ))
530
+ if (generic_attach (IP6VXLAN_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
517
531
goto done ;
518
532
519
533
/* load and attach prog set_md to tunnel dev tc hook point at_ns0 */
520
534
nstoken = open_netns ("at_ns0" );
521
535
if (!ASSERT_OK_PTR (nstoken , "setns src" ))
522
536
goto done ;
523
- ifindex = if_nametoindex (IP6VXLAN_TUNL_DEV0 );
524
- if (!ASSERT_NEQ (ifindex , 0 , "ip6vxlan00 ifindex" ))
525
- goto done ;
526
- tc_hook .ifindex = ifindex ;
527
537
set_dst_prog_fd = bpf_program__fd (skel -> progs .ip6vxlan_set_tunnel_dst );
528
- if (!ASSERT_GE (set_dst_prog_fd , 0 , "bpf_program__fd" ))
529
- goto done ;
530
- if (attach_tc_prog (& tc_hook , -1 , set_dst_prog_fd ))
538
+ if (generic_attach_egr (IP6VXLAN_TUNL_DEV0 , set_dst_prog_fd ))
531
539
goto done ;
532
540
close_netns (nstoken );
533
541
@@ -559,10 +567,7 @@ static void test_ipip_tunnel(enum ipip_encap encap)
559
567
struct test_tunnel_kern * skel = NULL ;
560
568
struct nstoken * nstoken ;
561
569
int set_src_prog_fd , get_src_prog_fd ;
562
- int ifindex = -1 ;
563
570
int err ;
564
- DECLARE_LIBBPF_OPTS (bpf_tc_hook , tc_hook ,
565
- .attach_point = BPF_TC_INGRESS );
566
571
567
572
/* add ipip tunnel */
568
573
err = add_ipip_tunnel (encap );
@@ -573,10 +578,6 @@ static void test_ipip_tunnel(enum ipip_encap encap)
573
578
skel = test_tunnel_kern__open_and_load ();
574
579
if (!ASSERT_OK_PTR (skel , "test_tunnel_kern__open_and_load" ))
575
580
goto done ;
576
- ifindex = if_nametoindex (IPIP_TUNL_DEV1 );
577
- if (!ASSERT_NEQ (ifindex , 0 , "ipip11 ifindex" ))
578
- goto done ;
579
- tc_hook .ifindex = ifindex ;
580
581
581
582
switch (encap ) {
582
583
case FOU :
@@ -598,11 +599,7 @@ static void test_ipip_tunnel(enum ipip_encap encap)
598
599
skel -> progs .ipip_set_tunnel );
599
600
}
600
601
601
- if (!ASSERT_GE (set_src_prog_fd , 0 , "bpf_program__fd" ))
602
- goto done ;
603
- if (!ASSERT_GE (get_src_prog_fd , 0 , "bpf_program__fd" ))
604
- goto done ;
605
- if (attach_tc_prog (& tc_hook , get_src_prog_fd , set_src_prog_fd ))
602
+ if (generic_attach (IPIP_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
606
603
goto done ;
607
604
608
605
/* ping from root namespace test */
@@ -628,8 +625,6 @@ static void test_ipip_tunnel(enum ipip_encap encap)
628
625
629
626
static void test_xfrm_tunnel (void )
630
627
{
631
- DECLARE_LIBBPF_OPTS (bpf_tc_hook , tc_hook ,
632
- .attach_point = BPF_TC_INGRESS );
633
628
LIBBPF_OPTS (bpf_xdp_attach_opts , opts );
634
629
struct test_tunnel_kern * skel = NULL ;
635
630
struct nstoken * nstoken ;
@@ -646,19 +641,16 @@ static void test_xfrm_tunnel(void)
646
641
if (!ASSERT_OK_PTR (skel , "test_tunnel_kern__open_and_load" ))
647
642
goto done ;
648
643
649
- ifindex = if_nametoindex ("veth1" );
650
- if (!ASSERT_NEQ (ifindex , 0 , "veth1 ifindex" ))
651
- goto done ;
652
644
653
645
/* attach tc prog to tunnel dev */
654
- tc_hook .ifindex = ifindex ;
655
646
tc_prog_fd = bpf_program__fd (skel -> progs .xfrm_get_state );
656
- if (!ASSERT_GE (tc_prog_fd , 0 , "bpf_program__fd" ))
657
- goto done ;
658
- if (attach_tc_prog (& tc_hook , tc_prog_fd , -1 ))
647
+ if (generic_attach_igr ("veth1" , tc_prog_fd ))
659
648
goto done ;
660
649
661
650
/* attach xdp prog to tunnel dev */
651
+ ifindex = if_nametoindex ("veth1" );
652
+ if (!ASSERT_NEQ (ifindex , 0 , "veth1 ifindex" ))
653
+ goto done ;
662
654
xdp_prog_fd = bpf_program__fd (skel -> progs .xfrm_get_state_xdp );
663
655
if (!ASSERT_GE (xdp_prog_fd , 0 , "bpf_program__fd" ))
664
656
goto done ;
0 commit comments