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.

bpftool: Add net attach/detach command to tcx prog

Now, attach/detach tcx prog supported in libbpf, so we can add new
command 'bpftool attach/detach tcx' to attach tcx prog with bpftool
for user.

# bpftool prog load tc_prog.bpf.o /sys/fs/bpf/tc_prog
# bpftool prog show
...
192: sched_cls name tc_prog tag 187aeb611ad00cfc gpl
loaded_at 2024-07-11T15:58:16+0800 uid 0
xlated 152B jited 97B memlock 4096B map_ids 100,99,97
btf_id 260
# bpftool net attach tcx_ingress name tc_prog dev lo
# bpftool net
...
tc:
lo(1) tcx/ingress tc_prog prog_id 29

# bpftool net detach tcx_ingress dev lo
# bpftool net
...
tc:
# bpftool net attach tcx_ingress name tc_prog dev lo
# bpftool net
tc:
lo(1) tcx/ingress tc_prog prog_id 29

Test environment: ubuntu_22_04, 6.7.0-060700-generic

Signed-off-by: Tao Chen <chen.dylane@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Quentin Monnet <qmo@kernel.org>
Link: https://lore.kernel.org/bpf/20240721144221.96228-1-chen.dylane@gmail.com
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

authored by

Tao Chen and committed by
Andrii Nakryiko
3b9d4fee b7264f87

+42 -1
+42 -1
tools/bpf/bpftool/net.c
··· 67 67 NET_ATTACH_TYPE_XDP_GENERIC, 68 68 NET_ATTACH_TYPE_XDP_DRIVER, 69 69 NET_ATTACH_TYPE_XDP_OFFLOAD, 70 + NET_ATTACH_TYPE_TCX_INGRESS, 71 + NET_ATTACH_TYPE_TCX_EGRESS, 70 72 }; 71 73 72 74 static const char * const attach_type_strings[] = { ··· 76 74 [NET_ATTACH_TYPE_XDP_GENERIC] = "xdpgeneric", 77 75 [NET_ATTACH_TYPE_XDP_DRIVER] = "xdpdrv", 78 76 [NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload", 77 + [NET_ATTACH_TYPE_TCX_INGRESS] = "tcx_ingress", 78 + [NET_ATTACH_TYPE_TCX_EGRESS] = "tcx_egress", 79 79 }; 80 80 81 81 static const char * const attach_loc_strings[] = { ··· 651 647 return bpf_xdp_attach(ifindex, progfd, flags, NULL); 652 648 } 653 649 650 + static int get_tcx_type(enum net_attach_type attach_type) 651 + { 652 + switch (attach_type) { 653 + case NET_ATTACH_TYPE_TCX_INGRESS: 654 + return BPF_TCX_INGRESS; 655 + case NET_ATTACH_TYPE_TCX_EGRESS: 656 + return BPF_TCX_EGRESS; 657 + default: 658 + return -1; 659 + } 660 + } 661 + 662 + static int do_attach_tcx(int progfd, enum net_attach_type attach_type, int ifindex) 663 + { 664 + int type = get_tcx_type(attach_type); 665 + 666 + return bpf_prog_attach(progfd, ifindex, type, 0); 667 + } 668 + 669 + static int do_detach_tcx(int targetfd, enum net_attach_type attach_type) 670 + { 671 + int type = get_tcx_type(attach_type); 672 + 673 + return bpf_prog_detach(targetfd, type); 674 + } 675 + 654 676 static int do_attach(int argc, char **argv) 655 677 { 656 678 enum net_attach_type attach_type; ··· 721 691 case NET_ATTACH_TYPE_XDP_DRIVER: 722 692 case NET_ATTACH_TYPE_XDP_OFFLOAD: 723 693 err = do_attach_detach_xdp(progfd, attach_type, ifindex, overwrite); 694 + break; 695 + /* attach tcx prog */ 696 + case NET_ATTACH_TYPE_TCX_INGRESS: 697 + case NET_ATTACH_TYPE_TCX_EGRESS: 698 + err = do_attach_tcx(progfd, attach_type, ifindex); 724 699 break; 725 700 default: 726 701 break; ··· 772 737 case NET_ATTACH_TYPE_XDP_OFFLOAD: 773 738 progfd = -1; 774 739 err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL); 740 + break; 741 + /* detach tcx prog */ 742 + case NET_ATTACH_TYPE_TCX_INGRESS: 743 + case NET_ATTACH_TYPE_TCX_EGRESS: 744 + err = do_detach_tcx(ifindex, attach_type); 775 745 break; 776 746 default: 777 747 break; ··· 984 944 " %1$s %2$s help\n" 985 945 "\n" 986 946 " " HELP_SPEC_PROGRAM "\n" 987 - " ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n" 947 + " ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload | tcx_ingress\n" 948 + " | tcx_egress }\n" 988 949 " " HELP_SPEC_OPTIONS " }\n" 989 950 "\n" 990 951 "Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n"