All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Toke Høiland-Jørgensen" <toke@kernel.org>
To: Zhengchao Shao <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
Cc: weiyongjun1@huawei.com, shaozhengchao@huawei.com, yuehaibing@huawei.com
Subject: Re: [PATCH v2,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd
Date: Tue, 17 May 2022 14:57:11 +0200	[thread overview]
Message-ID: <87tu9ob9lk.fsf@toke.dk> (raw)
In-Reply-To: <20220517112748.358295-1-shaozhengchao@huawei.com>

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

      reply	other threads:[~2022-05-17 12:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-17 11:27 [PATCH v2,bpf-next] samples/bpf: check detach prog exist or not in xdp_fwd Zhengchao Shao
2022-05-17 12:57 ` Toke Høiland-Jørgensen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tu9ob9lk.fsf@toke.dk \
    --to=toke@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=shaozhengchao@huawei.com \
    --cc=songliubraving@fb.com \
    --cc=weiyongjun1@huawei.com \
    --cc=yhs@fb.com \
    --cc=yuehaibing@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.