diff mbox series

[v5,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd

Message ID 20220602011915.264431-1-shaozhengchao@huawei.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series [v5,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
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: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 73 lines checked
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-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-15
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc

Commit Message

shaozhengchao June 2, 2022, 1:19 a.m. UTC
Before detach the prog, we should check detach prog exist or not.

Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
---
 samples/bpf/xdp_fwd_user.c | 55 +++++++++++++++++++++++++++++++++-----
 1 file changed, 49 insertions(+), 6 deletions(-)

Comments

Toke Høiland-Jørgensen June 2, 2022, 8:55 a.m. UTC | #1
Zhengchao Shao <shaozhengchao@huawei.com> writes:

> Before detach the prog, we should check detach prog exist or not.
>
> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>

You missed one 'return errno', see below:

> ---
>  samples/bpf/xdp_fwd_user.c | 55 +++++++++++++++++++++++++++++++++-----
>  1 file changed, 49 insertions(+), 6 deletions(-)
>
> diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
> index 1828487bae9a..d321e6aa9364 100644
> --- a/samples/bpf/xdp_fwd_user.c
> +++ b/samples/bpf/xdp_fwd_user.c
> @@ -47,17 +47,60 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
>  	return err;
>  }
>  
> -static int do_detach(int idx, const char *name)
> +static int do_detach(int ifindex, const char *ifname, const char *app_name)
>  {
> -	int err;
> +	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
> +	struct bpf_prog_info prog_info = {};
> +	char prog_name[BPF_OBJ_NAME_LEN];
> +	__u32 info_len, curr_prog_id;
> +	int prog_fd;
> +	int err = 1;
> +
> +	if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
> +		printf("ERROR: bpf_xdp_query_id failed (%s)\n",
> +		       strerror(errno));
> +		return err;
> +	}
>  
> -	err = bpf_xdp_detach(idx, xdp_flags, NULL);
> -	if (err < 0)
> -		printf("ERROR: failed to detach program from %s\n", name);
> +	if (!curr_prog_id) {
> +		printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
> +		       xdp_flags, ifname);
> +		return err;
> +	}
>  
> +	info_len = sizeof(prog_info);
> +	prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
> +	if (prog_fd < 0) {
> +		printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
> +		       strerror(errno));
> +		return errno;

This should just be 'return prog_fd' to propagate the error...

-Toke
shaozhengchao June 2, 2022, 9:59 a.m. UTC | #2
-----邮件原件-----
发件人: Toke Høiland-Jørgensen [mailto:toke@kernel.org] 
发送时间: 2022年6月2日 16:55
收件人: shaozhengchao <shaozhengchao@huawei.com>; bpf@vger.kernel.org; netdev@vger.kernel.org; linux-kernel@vger.kernel.org; ast@kernel.org; daniel@iogearbox.net; davem@davemloft.net; kuba@kernel.org; hawk@kernel.org; john.fastabend@gmail.com; andrii@kernel.org; kafai@fb.com; songliubraving@fb.com; yhs@fb.com; kpsingh@kernel.org
抄送: weiyongjun (A) <weiyongjun1@huawei.com>; shaozhengchao <shaozhengchao@huawei.com>; yuehaibing <yuehaibing@huawei.com>
主题: Re: [PATCH v5,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd

Zhengchao Shao <shaozhengchao@huawei.com> writes:

> Before detach the prog, we should check detach prog exist or not.
>
> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>

You missed one 'return errno', see below:

> ---
>  samples/bpf/xdp_fwd_user.c | 55 
> +++++++++++++++++++++++++++++++++-----
>  1 file changed, 49 insertions(+), 6 deletions(-)
>
> diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c 
> index 1828487bae9a..d321e6aa9364 100644
> --- a/samples/bpf/xdp_fwd_user.c
> +++ b/samples/bpf/xdp_fwd_user.c
> @@ -47,17 +47,60 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
>  	return err;
>  }
>  
> -static int do_detach(int idx, const char *name)
> +static int do_detach(int ifindex, const char *ifname, const char 
> +*app_name)
>  {
> -	int err;
> +	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
> +	struct bpf_prog_info prog_info = {};
> +	char prog_name[BPF_OBJ_NAME_LEN];
> +	__u32 info_len, curr_prog_id;
> +	int prog_fd;
> +	int err = 1;
> +
> +	if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
> +		printf("ERROR: bpf_xdp_query_id failed (%s)\n",
> +		       strerror(errno));
> +		return err;
> +	}
>  
> -	err = bpf_xdp_detach(idx, xdp_flags, NULL);
> -	if (err < 0)
> -		printf("ERROR: failed to detach program from %s\n", name);
> +	if (!curr_prog_id) {
> +		printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
> +		       xdp_flags, ifname);
> +		return err;
> +	}
>  
> +	info_len = sizeof(prog_info);
> +	prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
> +	if (prog_fd < 0) {
> +		printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
> +		       strerror(errno));
> +		return errno;

This should just be ' return prog_fd ' to propagate the error...

-Toke


Hi Toke:
	Use 'return prog_fd' instead of 'return errno' first. And Which position missed one 'return errno'?

-Zhengchao Shao
Toke Høiland-Jørgensen June 2, 2022, 10:16 a.m. UTC | #3
shaozhengchao <shaozhengchao@huawei.com> writes:

> -----邮件原件-----
> 发件人: Toke Høiland-Jørgensen [mailto:toke@kernel.org] 
> 发送时间: 2022年6月2日 16:55
> 收件人: shaozhengchao <shaozhengchao@huawei.com>; bpf@vger.kernel.org; netdev@vger.kernel.org; linux-kernel@vger.kernel.org; ast@kernel.org; daniel@iogearbox.net; davem@davemloft.net; kuba@kernel.org; hawk@kernel.org; john.fastabend@gmail.com; andrii@kernel.org; kafai@fb.com; songliubraving@fb.com; yhs@fb.com; kpsingh@kernel.org
> 抄送: weiyongjun (A) <weiyongjun1@huawei.com>; shaozhengchao <shaozhengchao@huawei.com>; yuehaibing <yuehaibing@huawei.com>
> 主题: Re: [PATCH v5,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd
>
> Zhengchao Shao <shaozhengchao@huawei.com> writes:
>
>> Before detach the prog, we should check detach prog exist or not.
>>
>> Signed-off-by: Zhengchao Shao <shaozhengchao@huawei.com>
>
> You missed one 'return errno', see below:
>
>> ---
>>  samples/bpf/xdp_fwd_user.c | 55 
>> +++++++++++++++++++++++++++++++++-----
>>  1 file changed, 49 insertions(+), 6 deletions(-)
>>
>> diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c 
>> index 1828487bae9a..d321e6aa9364 100644
>> --- a/samples/bpf/xdp_fwd_user.c
>> +++ b/samples/bpf/xdp_fwd_user.c
>> @@ -47,17 +47,60 @@ static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
>>  	return err;
>>  }
>>  
>> -static int do_detach(int idx, const char *name)
>> +static int do_detach(int ifindex, const char *ifname, const char 
>> +*app_name)
>>  {
>> -	int err;
>> +	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
>> +	struct bpf_prog_info prog_info = {};
>> +	char prog_name[BPF_OBJ_NAME_LEN];
>> +	__u32 info_len, curr_prog_id;
>> +	int prog_fd;
>> +	int err = 1;
>> +
>> +	if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
>> +		printf("ERROR: bpf_xdp_query_id failed (%s)\n",
>> +		       strerror(errno));
>> +		return err;
>> +	}
>>  
>> -	err = bpf_xdp_detach(idx, xdp_flags, NULL);
>> -	if (err < 0)
>> -		printf("ERROR: failed to detach program from %s\n", name);
>> +	if (!curr_prog_id) {
>> +		printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
>> +		       xdp_flags, ifname);
>> +		return err;
>> +	}
>>  
>> +	info_len = sizeof(prog_info);
>> +	prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
>> +	if (prog_fd < 0) {
>> +		printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
>> +		       strerror(errno));
>> +		return errno;
>
> This should just be ' return prog_fd ' to propagate the error...
>
> -Toke
>
>
> Hi Toke:
> 	Use 'return prog_fd' instead of 'return errno' first. And Which
> position missed one 'return errno'?

Ah, no, that's the one I was talking about; I meant "missed one" as in
"missed removing one of them", not "a return errno was missing but
should be there"; so just changing that one to 'return prog_fd' should
and I think we should be good :)

-Toke
diff mbox series

Patch

diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
index 1828487bae9a..d321e6aa9364 100644
--- a/samples/bpf/xdp_fwd_user.c
+++ b/samples/bpf/xdp_fwd_user.c
@@ -47,17 +47,60 @@  static int do_attach(int idx, int prog_fd, int map_fd, const char *name)
 	return err;
 }
 
-static int do_detach(int idx, const char *name)
+static int do_detach(int ifindex, const char *ifname, const char *app_name)
 {
-	int err;
+	LIBBPF_OPTS(bpf_xdp_attach_opts, opts);
+	struct bpf_prog_info prog_info = {};
+	char prog_name[BPF_OBJ_NAME_LEN];
+	__u32 info_len, curr_prog_id;
+	int prog_fd;
+	int err = 1;
+
+	if (bpf_xdp_query_id(ifindex, xdp_flags, &curr_prog_id)) {
+		printf("ERROR: bpf_xdp_query_id failed (%s)\n",
+		       strerror(errno));
+		return err;
+	}
 
-	err = bpf_xdp_detach(idx, xdp_flags, NULL);
-	if (err < 0)
-		printf("ERROR: failed to detach program from %s\n", name);
+	if (!curr_prog_id) {
+		printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
+		       xdp_flags, ifname);
+		return err;
+	}
 
+	info_len = sizeof(prog_info);
+	prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
+	if (prog_fd < 0) {
+		printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
+		       strerror(errno));
+		return errno;
+	}
+
+	err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
+	if (err) {
+		printf("ERROR: bpf_obj_get_info_by_fd failed (%s)\n",
+		       strerror(errno));
+		goto close_out;
+	}
+	snprintf(prog_name, sizeof(prog_name), "%s_prog", app_name);
+	prog_name[BPF_OBJ_NAME_LEN - 1] = '\0';
+
+	if (strcmp(prog_info.name, prog_name)) {
+		printf("ERROR: %s isn't attached to %s\n", app_name, ifname);
+		err = 1;
+		goto close_out;
+	}
+
+	opts.old_prog_fd = prog_fd;
+	err = bpf_xdp_detach(ifindex, xdp_flags, &opts);
+	if (err < 0)
+		printf("ERROR: failed to detach program from %s (%s)\n",
+		       ifname, strerror(errno));
 	/* TODO: Remember to cleanup map, when adding use of shared map
 	 *  bpf_map_delete_elem((map_fd, &idx);
 	 */
+close_out:
+	close(prog_fd);
 	return err;
 }
 
@@ -169,7 +212,7 @@  int main(int argc, char **argv)
 			return 1;
 		}
 		if (!attach) {
-			err = do_detach(idx, argv[i]);
+			err = do_detach(idx, argv[i], prog_name);
 			if (err)
 				ret = err;
 		} else {