bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Daniel Borkmann <daniel@iogearbox.net>, <ast@kernel.org>
Cc: <bpf@vger.kernel.org>, <netdev@vger.kernel.org>
Subject: Re: [PATCH bpf-next 2/2] bpf: extend bind v4/v6 selftests for mark/prio/bindtoifindex
Date: Mon, 11 Jan 2021 12:15:14 -0800	[thread overview]
Message-ID: <1cf3b794-6b84-e6a4-bed3-6b72c480eafa@fb.com> (raw)
In-Reply-To: <299c73acafd2c20d52624debb8a1e0019d85e6dd.1610381606.git.daniel@iogearbox.net>



On 1/11/21 8:17 AM, Daniel Borkmann wrote:
> Extend existing cgroup bind4/bind6 tests to add coverage for setting and
> retrieving SO_MARK, SO_PRIORITY and SO_BINDTOIFINDEX at the bind hook.
> 
> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>

Ack with a minor comments below.

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   .../testing/selftests/bpf/progs/bind4_prog.c  | 41 +++++++++++++++++--
>   .../testing/selftests/bpf/progs/bind6_prog.c  | 41 +++++++++++++++++--
>   2 files changed, 74 insertions(+), 8 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/progs/bind4_prog.c b/tools/testing/selftests/bpf/progs/bind4_prog.c
> index c6520f21f5f5..4479ac27b1d3 100644
> --- a/tools/testing/selftests/bpf/progs/bind4_prog.c
> +++ b/tools/testing/selftests/bpf/progs/bind4_prog.c
> @@ -29,18 +29,47 @@ static __inline int bind_to_device(struct bpf_sock_addr *ctx)
>   	char veth2[IFNAMSIZ] = "test_sock_addr2";
>   	char missing[IFNAMSIZ] = "nonexistent_dev";
>   	char del_bind[IFNAMSIZ] = "";
> +	int veth1_idx, veth2_idx;
>   
>   	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
> -				&veth1, sizeof(veth1)))
> +			   &veth1, sizeof(veth1)))
> +		return 1;
> +	if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
> +			   &veth1_idx, sizeof(veth1_idx)) || !veth1_idx)
>   		return 1;
>   	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
> -				&veth2, sizeof(veth2)))
> +			   &veth2, sizeof(veth2)))
> +		return 1;
> +	if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
> +			   &veth2_idx, sizeof(veth2_idx)) || !veth2_idx ||
> +	    veth1_idx == veth2_idx)
>   		return 1;
>   	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
> -				&missing, sizeof(missing)) != -ENODEV)
> +			   &missing, sizeof(missing)) != -ENODEV)
> +		return 1;
> +	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
> +			   &veth1_idx, sizeof(veth1_idx)))
>   		return 1;
>   	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
> -				&del_bind, sizeof(del_bind)))
> +			   &del_bind, sizeof(del_bind)))
> +		return 1;
> +
> +	return 0;
> +}
> +
> +static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
> +{
> +	int old, tmp, new = 0xeb9f;
> +
> +	if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)) ||
> +	    old == new)
> +		return 1;

Here, we assume old never equals to new. it would be good to add
a comment to explicitly state this is true. Maybe in the future
somebody will try to add more misc_opts which might have conflict
here.

Alternatively, you could pass in "new" values
from user space with global variables for each option,
but that may be an overkill.

> +	if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &new, sizeof(new)))
> +		return 1;
> +	if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &tmp, sizeof(tmp)) ||
> +	    tmp != new)
> +		return 1;
> +	if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)))
>   		return 1;
>   
>   	return 0;
> @@ -93,6 +122,10 @@ int bind_v4_prog(struct bpf_sock_addr *ctx)
>   	if (bind_to_device(ctx))
>   		return 0;
>   
> +	/* Test for misc socket options. */
> +	if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
> +		return 0;
> +
>   	ctx->user_ip4 = bpf_htonl(SERV4_REWRITE_IP);
>   	ctx->user_port = bpf_htons(SERV4_REWRITE_PORT);
>   
> diff --git a/tools/testing/selftests/bpf/progs/bind6_prog.c b/tools/testing/selftests/bpf/progs/bind6_prog.c
> index 4358e44dcf47..1b4142fcdd4b 100644
> --- a/tools/testing/selftests/bpf/progs/bind6_prog.c
> +++ b/tools/testing/selftests/bpf/progs/bind6_prog.c
> @@ -35,18 +35,47 @@ static __inline int bind_to_device(struct bpf_sock_addr *ctx)
>   	char veth2[IFNAMSIZ] = "test_sock_addr2";
>   	char missing[IFNAMSIZ] = "nonexistent_dev";
>   	char del_bind[IFNAMSIZ] = "";
> +	int veth1_idx, veth2_idx;
>   
>   	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
> -				&veth1, sizeof(veth1)))
> +			   &veth1, sizeof(veth1)))
> +		return 1;
> +	if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
> +			   &veth1_idx, sizeof(veth1_idx)) || !veth1_idx)
>   		return 1;
>   	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
> -				&veth2, sizeof(veth2)))
> +			   &veth2, sizeof(veth2)))
> +		return 1;
> +	if (bpf_getsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
> +			   &veth2_idx, sizeof(veth2_idx)) || !veth2_idx ||
> +	    veth1_idx == veth2_idx)
>   		return 1;
>   	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
> -				&missing, sizeof(missing)) != -ENODEV)
> +			   &missing, sizeof(missing)) != -ENODEV)
> +		return 1;
> +	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTOIFINDEX,
> +			   &veth1_idx, sizeof(veth1_idx)))
>   		return 1;
>   	if (bpf_setsockopt(ctx, SOL_SOCKET, SO_BINDTODEVICE,
> -				&del_bind, sizeof(del_bind)))
> +			   &del_bind, sizeof(del_bind)))
> +		return 1;
> +
> +	return 0;
> +}
> +
> +static __inline int misc_opts(struct bpf_sock_addr *ctx, int opt)
> +{
> +	int old, tmp, new = 0xeb9f;
> +
> +	if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)) ||
> +	    old == new)
> +		return 1;
> +	if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &new, sizeof(new)))
> +		return 1;
> +	if (bpf_getsockopt(ctx, SOL_SOCKET, opt, &tmp, sizeof(tmp)) ||
> +	    tmp != new)
> +		return 1;
> +	if (bpf_setsockopt(ctx, SOL_SOCKET, opt, &old, sizeof(old)))
>   		return 1;
>   
>   	return 0;
> @@ -107,6 +136,10 @@ int bind_v6_prog(struct bpf_sock_addr *ctx)
>   	if (bind_to_device(ctx))
>   		return 0;
>   
> +	/* Test for misc socket options. */
> +	if (misc_opts(ctx, SO_MARK) || misc_opts(ctx, SO_PRIORITY))
> +		return 0;
> +
>   	ctx->user_ip6[0] = bpf_htonl(SERV6_REWRITE_IP_0);
>   	ctx->user_ip6[1] = bpf_htonl(SERV6_REWRITE_IP_1);
>   	ctx->user_ip6[2] = bpf_htonl(SERV6_REWRITE_IP_2);
> 

  reply	other threads:[~2021-01-11 20:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-11 16:17 [PATCH bpf-next 1/2] bpf: allow to retrieve sol_socket opts from sock_addr progs Daniel Borkmann
2021-01-11 16:17 ` [PATCH bpf-next 2/2] bpf: extend bind v4/v6 selftests for mark/prio/bindtoifindex Daniel Borkmann
2021-01-11 20:15   ` Yonghong Song [this message]
2021-01-11 21:01     ` Daniel Borkmann
2021-01-11 20:03 ` [PATCH bpf-next 1/2] bpf: allow to retrieve sol_socket opts from sock_addr progs Yonghong Song

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=1cf3b794-6b84-e6a4-bed3-6b72c480eafa@fb.com \
    --to=yhs@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=netdev@vger.kernel.org \
    /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 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).