-----邮件原件----- 发件人: Toke Høiland-Jørgensen [mailto:toke@xxxxxxxxxx] 发送时间: 2022年6月2日 16:55 收件人: shaozhengchao <shaozhengchao@xxxxxxxxxx>; bpf@xxxxxxxxxxxxxxx; netdev@xxxxxxxxxxxxxxx; linux-kernel@xxxxxxxxxxxxxxx; ast@xxxxxxxxxx; daniel@xxxxxxxxxxxxx; davem@xxxxxxxxxxxxx; kuba@xxxxxxxxxx; hawk@xxxxxxxxxx; john.fastabend@xxxxxxxxx; andrii@xxxxxxxxxx; kafai@xxxxxx; songliubraving@xxxxxx; yhs@xxxxxx; kpsingh@xxxxxxxxxx 抄送: weiyongjun (A) <weiyongjun1@xxxxxxxxxx>; shaozhengchao <shaozhengchao@xxxxxxxxxx>; yuehaibing <yuehaibing@xxxxxxxxxx> 主题: Re: [PATCH v5,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd Zhengchao Shao <shaozhengchao@xxxxxxxxxx> writes: > Before detach the prog, we should check detach prog exist or not. > > Signed-off-by: Zhengchao Shao <shaozhengchao@xxxxxxxxxx> 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