* [PATCH v3,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd
@ 2022-05-21 4:35 Zhengchao Shao
2022-05-25 3:27 ` 答复: " shaozhengchao
2022-05-25 11:15 ` Toke Høiland-Jørgensen
0 siblings, 2 replies; 4+ messages in thread
From: Zhengchao Shao @ 2022-05-21 4:35 UTC (permalink / raw)
To: bpf, netdev, linux-kernel, ast, daniel, davem, kuba, hawk,
john.fastabend, andrii, kafai, songliubraving, yhs, kpsingh
Cc: weiyongjun1, shaozhengchao, yuehaibing
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 | 59 ++++++++++++++++++++++++++++++++------
1 file changed, 50 insertions(+), 9 deletions(-)
diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
index 1828487bae9a..03a50f64e99a 100644
--- a/samples/bpf/xdp_fwd_user.c
+++ b/samples/bpf/xdp_fwd_user.c
@@ -47,17 +47,58 @@ 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;
+ }
+
+ if (!curr_prog_id) {
+ printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
+ xdp_flags, ifname);
+ 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) {
+ printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\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 (%s)\n",
+ strerror(errno));
+ return err;
+ }
+ 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;
+ } else {
+ 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);
+ */
+ }
- /* TODO: Remember to cleanup map, when adding use of shared map
- * bpf_map_delete_elem((map_fd, &idx);
- */
return err;
}
@@ -169,7 +210,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 {
--
2.17.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* 答复: [PATCH v3,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd
2022-05-21 4:35 [PATCH v3,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd Zhengchao Shao
@ 2022-05-25 3:27 ` shaozhengchao
2022-05-25 11:16 ` Toke Høiland-Jørgensen
2022-05-25 11:15 ` Toke Høiland-Jørgensen
1 sibling, 1 reply; 4+ messages in thread
From: shaozhengchao @ 2022-05-25 3:27 UTC (permalink / raw)
To: bpf, netdev, linux-kernel, ast, daniel, davem, kuba, hawk,
john.fastabend, andrii, kafai, songliubraving, yhs, kpsingh,
Toke Høiland-Jørgensen
Cc: weiyongjun (A), yuehaibing
-----邮件原件-----
发件人: shaozhengchao
发送时间: 2022年5月21日 12:35
收件人: 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>
主题: [PATCH v3,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd
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 | 59 ++++++++++++++++++++++++++++++++------
1 file changed, 50 insertions(+), 9 deletions(-)
diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c index 1828487bae9a..03a50f64e99a 100644
--- a/samples/bpf/xdp_fwd_user.c
+++ b/samples/bpf/xdp_fwd_user.c
@@ -47,17 +47,58 @@ 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;
+ }
+
+ if (!curr_prog_id) {
+ printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
+ xdp_flags, ifname);
+ 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) {
+ printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\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 (%s)\n",
+ strerror(errno));
+ return err;
+ }
+ 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;
+ } else {
+ 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);
+ */
+ }
- /* TODO: Remember to cleanup map, when adding use of shared map
- * bpf_map_delete_elem((map_fd, &idx);
- */
return err;
}
@@ -169,7 +210,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 {
--
2.17.1
Hi Toke,
Do you have any more feedback? Does it look better to you now?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd
2022-05-21 4:35 [PATCH v3,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd Zhengchao Shao
2022-05-25 3:27 ` 答复: " shaozhengchao
@ 2022-05-25 11:15 ` Toke Høiland-Jørgensen
1 sibling, 0 replies; 4+ messages in thread
From: Toke Høiland-Jørgensen @ 2022-05-25 11:15 UTC (permalink / raw)
To: Zhengchao Shao, bpf, netdev, linux-kernel, ast, daniel, davem,
kuba, hawk, john.fastabend, andrii, kafai, songliubraving, yhs,
kpsingh
Cc: weiyongjun1, shaozhengchao, yuehaibing
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 | 59 ++++++++++++++++++++++++++++++++------
> 1 file changed, 50 insertions(+), 9 deletions(-)
>
> diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
> index 1828487bae9a..03a50f64e99a 100644
> --- a/samples/bpf/xdp_fwd_user.c
> +++ b/samples/bpf/xdp_fwd_user.c
> @@ -47,17 +47,58 @@ 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;
> + }
> +
> + if (!curr_prog_id) {
> + printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
> + xdp_flags, ifname);
> + 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);
This fd is never closed again; you'll need to replace the 'return err'
statement below with a 'goto err' and add a label at the end that closes
the fd before returning - see comments below.
> + if (prog_fd < 0) {
> + printf("ERROR: bpf_prog_get_fd_by_id failed (%s)\n",
> + strerror(errno));
> + return err;
err is not actually set here; you could either 'return prog_fd' or
'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));
> + return err;
make this 'goto err'...
> + }
> + 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;
> + } else {
> + 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);
> + */
> + }
>
> - /* TODO: Remember to cleanup map, when adding use of shared map
> - * bpf_map_delete_elem((map_fd, &idx);
> - */
...and add something like:
err:
close(prog_fd);
> return err;
> }
>
> @@ -169,7 +210,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 {
> --
> 2.17.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: 答复: [PATCH v3,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd
2022-05-25 3:27 ` 答复: " shaozhengchao
@ 2022-05-25 11:16 ` Toke Høiland-Jørgensen
0 siblings, 0 replies; 4+ messages in thread
From: Toke Høiland-Jørgensen @ 2022-05-25 11:16 UTC (permalink / raw)
To: shaozhengchao, bpf, netdev, linux-kernel, ast, daniel, davem,
kuba, hawk, john.fastabend, andrii, kafai, songliubraving, yhs,
kpsingh
Cc: weiyongjun (A), yuehaibing
> Hi Toke,
> Do you have any more feedback? Does it look better to you now?
Ah, sorry, missed your v3; replied now. Please make sure to add me
directly to Cc on any follow-ups to make sure I don't miss them :)
-Toke
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-05-25 11:16 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-21 4:35 [PATCH v3,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd Zhengchao Shao
2022-05-25 3:27 ` 答复: " shaozhengchao
2022-05-25 11:16 ` Toke Høiland-Jørgensen
2022-05-25 11:15 ` Toke Høiland-Jørgensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).