diff mbox series

[RFC,bpf-next,1/3] bpftool: add net attach/detach command to tcx prog

Message ID 20240715113704.1279881-2-chen.dylane@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpftool: add tcx subcommand in net | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 7 maintainers not CCed: kpsingh@kernel.org haoluo@google.com hawk@kernel.org kuba@kernel.org netdev@vger.kernel.org sdf@fomichev.me jolsa@kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: line length of 83 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc

Commit Message

Tao Chen July 15, 2024, 11:37 a.m. UTC
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 tcxingress name tc_prog dev lo
 # bpftool net
	...
	tc:
	lo(1) tcx/ingress tc_prog prog_id 29

 # bpftool net detach tcxingress dev lo
 # bpftool net
	...
	tc:
 # bpftool net attach tcxingress 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>
---
 tools/bpf/bpftool/net.c | 52 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

Comments

Quentin Monnet July 16, 2024, 5:23 p.m. UTC | #1
2024-07-15 12:37 UTC+0100 ~ Tao Chen <chen.dylane@gmail.com>
> 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 tcxingress name tc_prog dev lo
>  # bpftool net
> 	...
> 	tc:
> 	lo(1) tcx/ingress tc_prog prog_id 29
> 
>  # bpftool net detach tcxingress dev lo
>  # bpftool net
> 	...
> 	tc:
>  # bpftool net attach tcxingress 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>
> ---
>  tools/bpf/bpftool/net.c | 52 ++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 51 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
> index 968714b4c3d4..be7fd76202f1 100644
> --- a/tools/bpf/bpftool/net.c
> +++ b/tools/bpf/bpftool/net.c
> @@ -67,6 +67,8 @@ enum net_attach_type {
>  	NET_ATTACH_TYPE_XDP_GENERIC,
>  	NET_ATTACH_TYPE_XDP_DRIVER,
>  	NET_ATTACH_TYPE_XDP_OFFLOAD,
> +	NET_ATTACH_TYPE_TCX_INGRESS,
> +	NET_ATTACH_TYPE_TCX_EGRESS,
>  };
>  
>  static const char * const attach_type_strings[] = {
> @@ -74,6 +76,8 @@ static const char * const attach_type_strings[] = {
>  	[NET_ATTACH_TYPE_XDP_GENERIC]	= "xdpgeneric",
>  	[NET_ATTACH_TYPE_XDP_DRIVER]	= "xdpdrv",
>  	[NET_ATTACH_TYPE_XDP_OFFLOAD]	= "xdpoffload",
> +	[NET_ATTACH_TYPE_TCX_INGRESS]	= "tcxingress",
> +	[NET_ATTACH_TYPE_TCX_EGRESS]	= "tcxegress",


Hi, thanks for this work!

I wonder whether "tcx_ingress" and "tcx_egress" might be more readable?
I know we don't have underscores for XDP types but I'd be tempted to add
it for the tcx types, what do you think?


>  };
>  
>  static const char * const attach_loc_strings[] = {
> @@ -647,6 +651,32 @@ static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
>  	return bpf_xdp_attach(ifindex, progfd, flags, NULL);
>  }
>  
> +static int get_tcx_type(enum net_attach_type attach_type)
> +{
> +	int type = 0;
> +
> +	if (attach_type == NET_ATTACH_TYPE_TCX_INGRESS)
> +		type |= BPF_TCX_INGRESS;
> +	else if (attach_type == NET_ATTACH_TYPE_TCX_EGRESS)
> +		type |= BPF_TCX_EGRESS;


Why the logical OR in this function? This seems to be copied from the
XDP code, where we need to set flags. Here we just need the type, if I
remember correctly, so we could have:

	switch (attach_type) {
	case (NET_ATTACH_TYPE_TCX_INGRESS):
		return BPF_TXC_INGRESS;
	case (NET_ATTACH_TYPE_TCX_EGRESS):
		return BPF_TCX_EGRESS;
	}

(or if/else, works as well) which would be easier to understand in my
opinion.

> +
> +	return type;
> +}
> +
> +static int do_attach_tcx(int progfd, enum net_attach_type attach_type, int ifindex)
> +{
> +	int type = get_tcx_type(attach_type);
> +
> +	return bpf_prog_attach(progfd, ifindex, type, 0);
> +}
> +
> +static int do_detach_tcx(int targetfd, enum net_attach_type attach_type)
> +{
> +	int type = get_tcx_type(attach_type);
> +
> +	return bpf_prog_detach(targetfd, type);
> +}
> +
>  static int do_attach(int argc, char **argv)
>  {
>  	enum net_attach_type attach_type;
> @@ -694,6 +724,15 @@ static int do_attach(int argc, char **argv)
>  		goto cleanup;
>  	}
>  
> +	/* attach tcx prog */
> +	if (is_prefix("tcx", attach_type_strings[attach_type]))
> +		err = do_attach_tcx(progfd, attach_type, ifindex);
> +	if (err) {
> +		p_err("interface %s attach failed: %s",
> +		      attach_type_strings[attach_type], strerror(-err));
> +		goto cleanup;
> +	}


This introduces a second check on "err" in the function: if we attach an
XDP program we'll try to attach then check "err" twice. Same for a TCX
program, we'll check "err" before even trying to attach.

I understand this replicates what we do for XDP, but I'm not sure the
sequential calls to 'is_prefix("...")' is the cleanest approach. We
should probably change the XDP case a bit and integrate with TCX better.
Expanding the different attach types is more verbose, but remains the
most straightforward way in my opinion.

	switch (attach_type) {
	case NET_ATTACH_TYPE_XDP:
	case NET_ATTACH_TYPE_XDP_GENERIC:
	case NET_ATTACH_TYPE_XDP_DRIVER:
	case NET_ATTACH_TYPE_XDP_OFFLOAD:
		err = do_attach_xdp(...);
		break;
	case NET_ATTACH_TYPE_TCX_INGRESS:
	case NET_ATTACH_TYPE_TCX_EGRESS:
		err = do_attach_tcx(...);
		break;
	}

	// Single check on "err" for both XDP and TCX here;
	// Or moving it to the switch statement if checks/error messages
	// needed to be different, but that's not the case in your patch
	if (err) {
		p_err(...);
		goto cleanup;
	}


> +
>  	if (json_output)
>  		jsonw_null(json_wtr);
>  cleanup:
> @@ -732,6 +771,16 @@ static int do_detach(int argc, char **argv)
>  		return err;
>  	}
>  
> +	/* detach tcx prog */
> +	if (is_prefix("tcx", attach_type_strings[attach_type]))
> +		err = do_detach_tcx(ifindex, attach_type);
> +
> +	if (err < 0) {
> +		p_err("interface %s detach failed: %s",
> +		      attach_type_strings[attach_type], strerror(-err));
> +		return err;
> +	}


Same here.


> +
>  	if (json_output)
>  		jsonw_null(json_wtr);
>  
> @@ -928,7 +977,8 @@ static int do_help(int argc, char **argv)
>  		"       %1$s %2$s help\n"
>  		"\n"
>  		"       " HELP_SPEC_PROGRAM "\n"
> -		"       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
> +		"       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload | tcxingress\n"
> +		"			| tcxegress}\n"


Please use spaces only for indent inside of the string, and add a space
before the ending '}'.


>  		"       " HELP_SPEC_OPTIONS " }\n"
>  		"\n"
>  		"Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n"
Tao Chen July 17, 2024, 2:51 p.m. UTC | #2
在 2024/7/17 01:23, Quentin Monnet 写道:
> 2024-07-15 12:37 UTC+0100 ~ Tao Chen <chen.dylane@gmail.com>
>> 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 tcxingress name tc_prog dev lo
>>   # bpftool net
>> 	...
>> 	tc:
>> 	lo(1) tcx/ingress tc_prog prog_id 29
>>
>>   # bpftool net detach tcxingress dev lo
>>   # bpftool net
>> 	...
>> 	tc:
>>   # bpftool net attach tcxingress 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>
>> ---
>>   tools/bpf/bpftool/net.c | 52 ++++++++++++++++++++++++++++++++++++++++-
>>   1 file changed, 51 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
>> index 968714b4c3d4..be7fd76202f1 100644
>> --- a/tools/bpf/bpftool/net.c
>> +++ b/tools/bpf/bpftool/net.c
>> @@ -67,6 +67,8 @@ enum net_attach_type {
>>   	NET_ATTACH_TYPE_XDP_GENERIC,
>>   	NET_ATTACH_TYPE_XDP_DRIVER,
>>   	NET_ATTACH_TYPE_XDP_OFFLOAD,
>> +	NET_ATTACH_TYPE_TCX_INGRESS,
>> +	NET_ATTACH_TYPE_TCX_EGRESS,
>>   };
>>   
>>   static const char * const attach_type_strings[] = {
>> @@ -74,6 +76,8 @@ static const char * const attach_type_strings[] = {
>>   	[NET_ATTACH_TYPE_XDP_GENERIC]	= "xdpgeneric",
>>   	[NET_ATTACH_TYPE_XDP_DRIVER]	= "xdpdrv",
>>   	[NET_ATTACH_TYPE_XDP_OFFLOAD]	= "xdpoffload",
>> +	[NET_ATTACH_TYPE_TCX_INGRESS]	= "tcxingress",
>> +	[NET_ATTACH_TYPE_TCX_EGRESS]	= "tcxegress",
> 
> 
> Hi, thanks for this work!
> 
> I wonder whether "tcx_ingress" and "tcx_egress" might be more readable?
> I know we don't have underscores for XDP types but I'd be tempted to add
> it for the tcx types, what do you think?
> 
> 
Hi,Quentin,thanks for your reply!
You are right, tcx_* looks more readable, i will change it in v2.
>>   };
>>   
>>   static const char * const attach_loc_strings[] = {
>> @@ -647,6 +651,32 @@ static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
>>   	return bpf_xdp_attach(ifindex, progfd, flags, NULL);
>>   }
>>   
>> +static int get_tcx_type(enum net_attach_type attach_type)
>> +{
>> +	int type = 0;
>> +
>> +	if (attach_type == NET_ATTACH_TYPE_TCX_INGRESS)
>> +		type |= BPF_TCX_INGRESS;
>> +	else if (attach_type == NET_ATTACH_TYPE_TCX_EGRESS)
>> +		type |= BPF_TCX_EGRESS;
> 
> 
> Why the logical OR in this function? This seems to be copied from the
> XDP code, where we need to set flags. Here we just need the type, if I
> remember correctly, so we could have:
> 
> 	switch (attach_type) {
> 	case (NET_ATTACH_TYPE_TCX_INGRESS):
> 		return BPF_TXC_INGRESS;
> 	case (NET_ATTACH_TYPE_TCX_EGRESS):
> 		return BPF_TCX_EGRESS;
> 	}
> 
> (or if/else, works as well) which would be easier to understand in my
> opinion.
> 
It seems more reasonable, i will change it in v2.
>> +
>> +	return type;
>> +}
>> +
>> +static int do_attach_tcx(int progfd, enum net_attach_type attach_type, int ifindex)
>> +{
>> +	int type = get_tcx_type(attach_type);
>> +
>> +	return bpf_prog_attach(progfd, ifindex, type, 0);
>> +}
>> +
>> +static int do_detach_tcx(int targetfd, enum net_attach_type attach_type)
>> +{
>> +	int type = get_tcx_type(attach_type);
>> +
>> +	return bpf_prog_detach(targetfd, type);
>> +}
>> +
>>   static int do_attach(int argc, char **argv)
>>   {
>>   	enum net_attach_type attach_type;
>> @@ -694,6 +724,15 @@ static int do_attach(int argc, char **argv)
>>   		goto cleanup;
>>   	}
>>   
>> +	/* attach tcx prog */
>> +	if (is_prefix("tcx", attach_type_strings[attach_type]))
>> +		err = do_attach_tcx(progfd, attach_type, ifindex);
>> +	if (err) {
>> +		p_err("interface %s attach failed: %s",
>> +		      attach_type_strings[attach_type], strerror(-err));
>> +		goto cleanup;
>> +	}
> 
> 
> This introduces a second check on "err" in the function: if we attach an
> XDP program we'll try to attach then check "err" twice. Same for a TCX
> program, we'll check "err" before even trying to attach.
> 
> I understand this replicates what we do for XDP, but I'm not sure the
> sequential calls to 'is_prefix("...")' is the cleanest approach. We
> should probably change the XDP case a bit and integrate with TCX better.
> Expanding the different attach types is more verbose, but remains the
> most straightforward way in my opinion.
> 
> 	switch (attach_type) {
> 	case NET_ATTACH_TYPE_XDP:
> 	case NET_ATTACH_TYPE_XDP_GENERIC:
> 	case NET_ATTACH_TYPE_XDP_DRIVER:
> 	case NET_ATTACH_TYPE_XDP_OFFLOAD:
> 		err = do_attach_xdp(...);
> 		break;
> 	case NET_ATTACH_TYPE_TCX_INGRESS:
> 	case NET_ATTACH_TYPE_TCX_EGRESS:
> 		err = do_attach_tcx(...);
> 		break;
> 	}
> 
> 	// Single check on "err" for both XDP and TCX here;
> 	// Or moving it to the switch statement if checks/error messages
> 	// needed to be different, but that's not the case in your patch
> 	if (err) {
> 		p_err(...);
> 		goto cleanup;
> 	}
> 
> 
My bad, i will add another patch to refactor this as you say.
>> +
>>   	if (json_output)
>>   		jsonw_null(json_wtr);
>>   cleanup:
>> @@ -732,6 +771,16 @@ static int do_detach(int argc, char **argv)
>>   		return err;
>>   	}
>>   
>> +	/* detach tcx prog */
>> +	if (is_prefix("tcx", attach_type_strings[attach_type]))
>> +		err = do_detach_tcx(ifindex, attach_type);
>> +
>> +	if (err < 0) {
>> +		p_err("interface %s detach failed: %s",
>> +		      attach_type_strings[attach_type], strerror(-err));
>> +		return err;
>> +	}
> 
> 
> Same here.
> 
> Got it.
>> +
>>   	if (json_output)
>>   		jsonw_null(json_wtr);
>>   
>> @@ -928,7 +977,8 @@ static int do_help(int argc, char **argv)
>>   		"       %1$s %2$s help\n"
>>   		"\n"
>>   		"       " HELP_SPEC_PROGRAM "\n"
>> -		"       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
>> +		"       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload | tcxingress\n"
>> +		"			| tcxegress}\n"
> 
> 
> Please use spaces only for indent inside of the string, and add a space
> before the ending '}'.
> 
> 
ok, i will fix this.
>>   		"       " HELP_SPEC_OPTIONS " }\n"
>>   		"\n"
>>   		"Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n"
>
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/net.c b/tools/bpf/bpftool/net.c
index 968714b4c3d4..be7fd76202f1 100644
--- a/tools/bpf/bpftool/net.c
+++ b/tools/bpf/bpftool/net.c
@@ -67,6 +67,8 @@  enum net_attach_type {
 	NET_ATTACH_TYPE_XDP_GENERIC,
 	NET_ATTACH_TYPE_XDP_DRIVER,
 	NET_ATTACH_TYPE_XDP_OFFLOAD,
+	NET_ATTACH_TYPE_TCX_INGRESS,
+	NET_ATTACH_TYPE_TCX_EGRESS,
 };
 
 static const char * const attach_type_strings[] = {
@@ -74,6 +76,8 @@  static const char * const attach_type_strings[] = {
 	[NET_ATTACH_TYPE_XDP_GENERIC]	= "xdpgeneric",
 	[NET_ATTACH_TYPE_XDP_DRIVER]	= "xdpdrv",
 	[NET_ATTACH_TYPE_XDP_OFFLOAD]	= "xdpoffload",
+	[NET_ATTACH_TYPE_TCX_INGRESS]	= "tcxingress",
+	[NET_ATTACH_TYPE_TCX_EGRESS]	= "tcxegress",
 };
 
 static const char * const attach_loc_strings[] = {
@@ -647,6 +651,32 @@  static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
 	return bpf_xdp_attach(ifindex, progfd, flags, NULL);
 }
 
+static int get_tcx_type(enum net_attach_type attach_type)
+{
+	int type = 0;
+
+	if (attach_type == NET_ATTACH_TYPE_TCX_INGRESS)
+		type |= BPF_TCX_INGRESS;
+	else if (attach_type == NET_ATTACH_TYPE_TCX_EGRESS)
+		type |= BPF_TCX_EGRESS;
+
+	return type;
+}
+
+static int do_attach_tcx(int progfd, enum net_attach_type attach_type, int ifindex)
+{
+	int type = get_tcx_type(attach_type);
+
+	return bpf_prog_attach(progfd, ifindex, type, 0);
+}
+
+static int do_detach_tcx(int targetfd, enum net_attach_type attach_type)
+{
+	int type = get_tcx_type(attach_type);
+
+	return bpf_prog_detach(targetfd, type);
+}
+
 static int do_attach(int argc, char **argv)
 {
 	enum net_attach_type attach_type;
@@ -694,6 +724,15 @@  static int do_attach(int argc, char **argv)
 		goto cleanup;
 	}
 
+	/* attach tcx prog */
+	if (is_prefix("tcx", attach_type_strings[attach_type]))
+		err = do_attach_tcx(progfd, attach_type, ifindex);
+	if (err) {
+		p_err("interface %s attach failed: %s",
+		      attach_type_strings[attach_type], strerror(-err));
+		goto cleanup;
+	}
+
 	if (json_output)
 		jsonw_null(json_wtr);
 cleanup:
@@ -732,6 +771,16 @@  static int do_detach(int argc, char **argv)
 		return err;
 	}
 
+	/* detach tcx prog */
+	if (is_prefix("tcx", attach_type_strings[attach_type]))
+		err = do_detach_tcx(ifindex, attach_type);
+
+	if (err < 0) {
+		p_err("interface %s detach failed: %s",
+		      attach_type_strings[attach_type], strerror(-err));
+		return err;
+	}
+
 	if (json_output)
 		jsonw_null(json_wtr);
 
@@ -928,7 +977,8 @@  static int do_help(int argc, char **argv)
 		"       %1$s %2$s help\n"
 		"\n"
 		"       " HELP_SPEC_PROGRAM "\n"
-		"       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
+		"       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload | tcxingress\n"
+		"			| tcxegress}\n"
 		"       " HELP_SPEC_OPTIONS " }\n"
 		"\n"
 		"Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n"