lkml.org 
[lkml]   [2022]   [Jun]   [1]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject答复: [PATCH v4,bpf-next] samples/bpf: chec k detach prog exist or not in xdp fwd
Date

-----邮件原件-----
发件人: Toke Høiland-Jørgensen [mailto:toke@kernel.org]
发送时间: 2022年6月1日 17:25
收件人: 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 v4,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>

The logic looks good now, just spotted one issue with the use of 'return errno' (which I just realised I suggested on the last version, sorry about that).

> samples/bpf/xdp_fwd_user.c | 62
> ++++++++++++++++++++++++++++++++------
> 1 file changed, 53 insertions(+), 9 deletions(-)
>
> diff --git a/samples/bpf/xdp_fwd_user.c b/samples/bpf/xdp_fwd_user.c
> index 1828487bae9a..a4ba53891653 100644
> --- a/samples/bpf/xdp_fwd_user.c
> +++ b/samples/bpf/xdp_fwd_user.c
> @@ -47,17 +47,61 @@ 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 -errno;

The printf above may override errno, so this could return 0; the actual return value is not really used by the caller, so you could just always 'return -1' or 'return err'.

> + }
> +
> + if (!curr_prog_id) {
> + printf("ERROR: flags(0x%x) xdp prog is not attached to %s\n",
> + xdp_flags, ifname);
> + goto err_out;

Doesn't really need a label, just do a direct return here as well.

> + }
>
> - 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 -errno;

Same issue as above with errno being overwritten.

> + }
> +
> + 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;
> + } else {

You can save a level of indentation by adding a 'goto close_out' after 'err=1' instead of using an 'else' block.

> + 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);
> - */
> +close_out:
> + close(prog_fd);
> +err_out:

don't need the err_out label, see above.

-Toke

Hi Toke:
Thank you for your reply. I will fix it in next patch.

Zhengchao Shao

\
 
 \ /
  Last update: 2022-06-01 12:35    [W:0.143 / U:0.040 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site