@@ -534,85 +534,6 @@ static void ping6_dev1(void)
534534 close_netns (nstoken );
535535}
536536
537- static int attach_tc_prog (int ifindex , int igr_fd , int egr_fd )
538- {
539- DECLARE_LIBBPF_OPTS (bpf_tc_hook , hook , .ifindex = ifindex ,
540- .attach_point = BPF_TC_INGRESS | BPF_TC_EGRESS );
541- DECLARE_LIBBPF_OPTS (bpf_tc_opts , opts1 , .handle = 1 ,
542- .priority = 1 , .prog_fd = igr_fd );
543- DECLARE_LIBBPF_OPTS (bpf_tc_opts , opts2 , .handle = 1 ,
544- .priority = 1 , .prog_fd = egr_fd );
545- int ret ;
546-
547- ret = bpf_tc_hook_create (& hook );
548- if (!ASSERT_OK (ret , "create tc hook" ))
549- return ret ;
550-
551- if (igr_fd >= 0 ) {
552- hook .attach_point = BPF_TC_INGRESS ;
553- ret = bpf_tc_attach (& hook , & opts1 );
554- if (!ASSERT_OK (ret , "bpf_tc_attach" )) {
555- bpf_tc_hook_destroy (& hook );
556- return ret ;
557- }
558- }
559-
560- if (egr_fd >= 0 ) {
561- hook .attach_point = BPF_TC_EGRESS ;
562- ret = bpf_tc_attach (& hook , & opts2 );
563- if (!ASSERT_OK (ret , "bpf_tc_attach" )) {
564- bpf_tc_hook_destroy (& hook );
565- return ret ;
566- }
567- }
568-
569- return 0 ;
570- }
571-
572- static int generic_attach (const char * dev , int igr_fd , int egr_fd )
573- {
574- int ifindex ;
575-
576- if (!ASSERT_OK_FD (igr_fd , "check ingress fd" ))
577- return -1 ;
578- if (!ASSERT_OK_FD (egr_fd , "check egress fd" ))
579- return -1 ;
580-
581- ifindex = if_nametoindex (dev );
582- if (!ASSERT_NEQ (ifindex , 0 , "get ifindex" ))
583- return -1 ;
584-
585- return attach_tc_prog (ifindex , igr_fd , egr_fd );
586- }
587-
588- static int generic_attach_igr (const char * dev , int igr_fd )
589- {
590- int ifindex ;
591-
592- if (!ASSERT_OK_FD (igr_fd , "check ingress fd" ))
593- return -1 ;
594-
595- ifindex = if_nametoindex (dev );
596- if (!ASSERT_NEQ (ifindex , 0 , "get ifindex" ))
597- return -1 ;
598-
599- return attach_tc_prog (ifindex , igr_fd , -1 );
600- }
601-
602- static int generic_attach_egr (const char * dev , int egr_fd )
603- {
604- int ifindex ;
605-
606- if (!ASSERT_OK_FD (egr_fd , "check egress fd" ))
607- return -1 ;
608-
609- ifindex = if_nametoindex (dev );
610- if (!ASSERT_NEQ (ifindex , 0 , "get ifindex" ))
611- return -1 ;
612-
613- return attach_tc_prog (ifindex , -1 , egr_fd );
614- }
615-
616537static void test_vxlan_tunnel (void )
617538{
618539 struct test_tunnel_kern * skel = NULL ;
@@ -635,20 +556,20 @@ static void test_vxlan_tunnel(void)
635556 goto done ;
636557 get_src_prog_fd = bpf_program__fd (skel -> progs .vxlan_get_tunnel_src );
637558 set_src_prog_fd = bpf_program__fd (skel -> progs .vxlan_set_tunnel_src );
638- if (generic_attach (VXLAN_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
559+ if (tc_prog_attach (VXLAN_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
639560 goto done ;
640561
641562 /* load and attach bpf prog to veth dev tc hook point */
642563 set_dst_prog_fd = bpf_program__fd (skel -> progs .veth_set_outer_dst );
643- if (generic_attach_igr ("veth1" , set_dst_prog_fd ))
564+ if (tc_prog_attach ("veth1" , set_dst_prog_fd , -1 ))
644565 goto done ;
645566
646567 /* load and attach prog set_md to tunnel dev tc hook point at_ns0 */
647568 nstoken = open_netns ("at_ns0" );
648569 if (!ASSERT_OK_PTR (nstoken , "setns src" ))
649570 goto done ;
650571 set_dst_prog_fd = bpf_program__fd (skel -> progs .vxlan_set_tunnel_dst );
651- if (generic_attach_egr (VXLAN_TUNL_DEV0 , set_dst_prog_fd ))
572+ if (tc_prog_attach (VXLAN_TUNL_DEV0 , -1 , set_dst_prog_fd ))
652573 goto done ;
653574 close_netns (nstoken );
654575
@@ -695,15 +616,15 @@ static void test_ip6vxlan_tunnel(void)
695616 goto done ;
696617 get_src_prog_fd = bpf_program__fd (skel -> progs .ip6vxlan_get_tunnel_src );
697618 set_src_prog_fd = bpf_program__fd (skel -> progs .ip6vxlan_set_tunnel_src );
698- if (generic_attach (IP6VXLAN_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
619+ if (tc_prog_attach (IP6VXLAN_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
699620 goto done ;
700621
701622 /* load and attach prog set_md to tunnel dev tc hook point at_ns0 */
702623 nstoken = open_netns ("at_ns0" );
703624 if (!ASSERT_OK_PTR (nstoken , "setns src" ))
704625 goto done ;
705626 set_dst_prog_fd = bpf_program__fd (skel -> progs .ip6vxlan_set_tunnel_dst );
706- if (generic_attach_egr (IP6VXLAN_TUNL_DEV0 , set_dst_prog_fd ))
627+ if (tc_prog_attach (IP6VXLAN_TUNL_DEV0 , -1 , set_dst_prog_fd ))
707628 goto done ;
708629 close_netns (nstoken );
709630
@@ -764,7 +685,7 @@ static void test_ipip_tunnel(enum ipip_encap encap)
764685 skel -> progs .ipip_set_tunnel );
765686 }
766687
767- if (generic_attach (IPIP_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
688+ if (tc_prog_attach (IPIP_TUNL_DEV1 , get_src_prog_fd , set_src_prog_fd ))
768689 goto done ;
769690
770691 ping_dev0 ();
@@ -797,7 +718,7 @@ static void test_xfrm_tunnel(void)
797718
798719 /* attach tc prog to tunnel dev */
799720 tc_prog_fd = bpf_program__fd (skel -> progs .xfrm_get_state );
800- if (generic_attach_igr ("veth1" , tc_prog_fd ))
721+ if (tc_prog_attach ("veth1" , tc_prog_fd , -1 ))
801722 goto done ;
802723
803724 /* attach xdp prog to tunnel dev */
@@ -870,7 +791,7 @@ static void test_gre_tunnel(enum gre_test test)
870791 if (!ASSERT_OK (err , "add tunnel" ))
871792 goto done ;
872793
873- if (generic_attach (GRE_TUNL_DEV1 , get_fd , set_fd ))
794+ if (tc_prog_attach (GRE_TUNL_DEV1 , get_fd , set_fd ))
874795 goto done ;
875796
876797 ping_dev0 ();
@@ -911,7 +832,7 @@ static void test_ip6gre_tunnel(enum ip6gre_test test)
911832
912833 set_fd = bpf_program__fd (skel -> progs .ip6gretap_set_tunnel );
913834 get_fd = bpf_program__fd (skel -> progs .ip6gretap_get_tunnel );
914- if (generic_attach (IP6GRE_TUNL_DEV1 , get_fd , set_fd ))
835+ if (tc_prog_attach (IP6GRE_TUNL_DEV1 , get_fd , set_fd ))
915836 goto done ;
916837
917838 ping6_veth0 ();
@@ -954,7 +875,7 @@ static void test_erspan_tunnel(enum erspan_test test)
954875
955876 set_fd = bpf_program__fd (skel -> progs .erspan_set_tunnel );
956877 get_fd = bpf_program__fd (skel -> progs .erspan_get_tunnel );
957- if (generic_attach (ERSPAN_TUNL_DEV1 , get_fd , set_fd ))
878+ if (tc_prog_attach (ERSPAN_TUNL_DEV1 , get_fd , set_fd ))
958879 goto done ;
959880
960881 ping_dev0 ();
@@ -990,7 +911,7 @@ static void test_ip6erspan_tunnel(enum erspan_test test)
990911
991912 set_fd = bpf_program__fd (skel -> progs .ip4ip6erspan_set_tunnel );
992913 get_fd = bpf_program__fd (skel -> progs .ip4ip6erspan_get_tunnel );
993- if (generic_attach (IP6ERSPAN_TUNL_DEV1 , get_fd , set_fd ))
914+ if (tc_prog_attach (IP6ERSPAN_TUNL_DEV1 , get_fd , set_fd ))
994915 goto done ;
995916
996917 ping6_veth0 ();
@@ -1017,7 +938,7 @@ static void test_geneve_tunnel(void)
1017938
1018939 set_fd = bpf_program__fd (skel -> progs .geneve_set_tunnel );
1019940 get_fd = bpf_program__fd (skel -> progs .geneve_get_tunnel );
1020- if (generic_attach (GENEVE_TUNL_DEV1 , get_fd , set_fd ))
941+ if (tc_prog_attach (GENEVE_TUNL_DEV1 , get_fd , set_fd ))
1021942 goto done ;
1022943
1023944 ping_dev0 ();
@@ -1044,7 +965,7 @@ static void test_ip6geneve_tunnel(void)
1044965
1045966 set_fd = bpf_program__fd (skel -> progs .ip6geneve_set_tunnel );
1046967 get_fd = bpf_program__fd (skel -> progs .ip6geneve_get_tunnel );
1047- if (generic_attach (IP6GENEVE_TUNL_DEV1 , get_fd , set_fd ))
968+ if (tc_prog_attach (IP6GENEVE_TUNL_DEV1 , get_fd , set_fd ))
1048969 goto done ;
1049970
1050971 ping_dev0 ();
@@ -1083,7 +1004,7 @@ static void test_ip6tnl_tunnel(enum ip6tnl_test test)
10831004 get_fd = bpf_program__fd (skel -> progs .ip6ip6_get_tunnel );
10841005 break ;
10851006 }
1086- if (generic_attach (IP6TNL_TUNL_DEV1 , get_fd , set_fd ))
1007+ if (tc_prog_attach (IP6TNL_TUNL_DEV1 , get_fd , set_fd ))
10871008 goto done ;
10881009
10891010 ping6_veth0 ();
0 commit comments