diff mbox series

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

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

Checks

Context Check Description
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/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning + printf("ERROR: failed to detach program from %s\n", + printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n", CHECK: Alignment should match open parenthesis
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-3 success Logs for Kernel LATEST on z15 with gcc
bpf/vmtest-bpf-next-PR success PR summary
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

Commit Message

shaozhengchao May 17, 2022, 11:27 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 | 52 +++++++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 9 deletions(-)

Comments

Toke Høiland-Jørgensen May 17, 2022, 12:57 p.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>
> ---
>  samples/bpf/xdp_fwd_user.c | 52 +++++++++++++++++++++++++++++++-------
>  1 file changed, 43 insertions(+), 9 deletions(-)
>
> diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
> index 1828487bae9a..2294486ef10a 100644
> --- a/samples/bpf/xdp_fwd_user.c
> +++ b/samples/bpf/xdp_fwd_user.c
> @@ -47,17 +47,51 @@ 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 idx, const char *name, const char *prog_name)

two 'name' arguments is a bit confusing; could we rename the parameters
to 'ifindex', 'ifname' and 'app_name', then use 'prog_name' for the
stack variable below instead of 'namepad'?

>  {
> -	int err;
> +	int err = 1;
> +	__u32 info_len, curr_prog_id;
> +	struct bpf_prog_info prog_info = {};
> +	int prog_fd;
> +	char namepad[BPF_OBJ_NAME_LEN];

Reverse x-mas tree, please.

> +
> +	if (bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id)) {
> +		printf("ERROR: bpf_xdp_query_id failed\n");

strerror(errno) might be nice to add to the error message, so users have
an inkling as to why the call is failing (same below).

> +		return err;
> +	}
> +
> +	if (!curr_prog_id) {
> +		printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
> +			xdp_flags, name);
> +		return err;
> +	}
>  
> -	err = bpf_xdp_detach(idx, xdp_flags, NULL);
> -	if (err < 0)
> -		printf("ERROR: failed to detach program from %s\n", name);
> +	info_len = sizeof(prog_info);
> +	prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
> +	if (prog_fd < 0 && errno == ENOENT) {

Why the ENOENT check? This should error out regardless of the errno, no?

> +		printf("ERROR: bpf_prog_get_fd_by_id failed\n");

strerror(errno)

> +		return err;
> +	}
> +
> +	err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
> +	if (err) {
> +		printf("ERROR: bpf_obj_get_info_by_fd failed\n");

strerror(errno)

> +		return err;
> +	}
> +	snprintf(namepad, sizeof(namepad), "%s_prog", prog_name);

If the binary somehow gets renamed, this may overflow and you'll end up
without a NULL byte terminating the string. So either check the input
length first, or make sure to set the last byte to '\0' after this
call...

> +
> +	if (strcmp(prog_info.name, namepad)) {
> +		printf("ERROR: %s isn't attached to %s\n", prog_name, name);
> +	} else {
> +		err = bpf_xdp_detach(idx, xdp_flags, NULL);

This call should be including an opts struct with the fd obtained above
as the old_prog_fd (so make sure it wasn't swapped out since the check).

> +		if (err < 0)
> +			printf("ERROR: failed to detach program from %s\n",
> +				name);

strerror(errno)

-Toke
diff mbox series

Patch

diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
index 1828487bae9a..2294486ef10a 100644
--- a/samples/bpf/xdp_fwd_user.c
+++ b/samples/bpf/xdp_fwd_user.c
@@ -47,17 +47,51 @@  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 idx, const char *name, const char *prog_name)
 {
-	int err;
+	int err = 1;
+	__u32 info_len, curr_prog_id;
+	struct bpf_prog_info prog_info = {};
+	int prog_fd;
+	char namepad[BPF_OBJ_NAME_LEN];
+
+	if (bpf_xdp_query_id(idx, xdp_flags, &curr_prog_id)) {
+		printf("ERROR: bpf_xdp_query_id failed\n");
+		return err;
+	}
+
+	if (!curr_prog_id) {
+		printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
+			xdp_flags, name);
+		return err;
+	}
 
-	err = bpf_xdp_detach(idx, xdp_flags, NULL);
-	if (err < 0)
-		printf("ERROR: failed to detach program from %s\n", name);
+	info_len = sizeof(prog_info);
+	prog_fd = bpf_prog_get_fd_by_id(curr_prog_id);
+	if (prog_fd < 0 && errno == ENOENT) {
+		printf("ERROR: bpf_prog_get_fd_by_id failed\n");
+		return err;
+	}
+
+	err = bpf_obj_get_info_by_fd(prog_fd, &prog_info, &info_len);
+	if (err) {
+		printf("ERROR: bpf_obj_get_info_by_fd failed\n");
+		return err;
+	}
+	snprintf(namepad, sizeof(namepad), "%s_prog", prog_name);
+
+	if (strcmp(prog_info.name, namepad)) {
+		printf("ERROR: %s isn't attached to %s\n", prog_name, name);
+	} else {
+		err = bpf_xdp_detach(idx, xdp_flags, NULL);
+		if (err < 0)
+			printf("ERROR: failed to detach program from %s\n",
+				name);
+		/* TODO: Remember to cleanup map, when adding use of shared map
+		 *  bpf_map_delete_elem((map_fd, &idx);
+		 */
+	}
 
-	/* TODO: Remember to cleanup map, when adding use of shared map
-	 *  bpf_map_delete_elem((map_fd, &idx);
-	 */
 	return err;
 }
 
@@ -169,7 +203,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 {