netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: sdf@google.com
To: Andrey Ignatov <rdna@fb.com>
Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, davem@davemloft.net,
	ast@kernel.org, daniel@iogearbox.net
Subject: Re: [PATCH bpf-next 4/4] bpf: allow any port in bpf_bind helper
Date: Tue, 5 May 2020 10:43:27 -0700	[thread overview]
Message-ID: <20200505174327.GF241848@google.com> (raw)
In-Reply-To: <20200505173338.GA55644@rdna-mbp>

On 05/05, Andrey Ignatov wrote:
> sdf@google.com <sdf@google.com> [Tue, 2020-05-05 10:09 -0700]:
> > On 05/05, Stanislav Fomichev wrote:
> > > On 05/04, Andrey Ignatov wrote:
> > > > Stanislav Fomichev <sdf@google.com> [Mon, 2020-05-04 10:34 -0700]:
> > > > > [...]
> > > > > diff --git a/net/core/filter.c b/net/core/filter.c
> > > > > index fa9ddab5dd1f..fc5161b9ff6a 100644
> > > > > --- a/net/core/filter.c
> > > > > +++ b/net/core/filter.c
> > > > > @@ -4527,29 +4527,24 @@ BPF_CALL_3(bpf_bind, struct
> > > bpf_sock_addr_kern *, ctx, struct sockaddr *, addr,
> > > > >  	struct sock *sk = ctx->sk;
> > > > >  	int err;
> > > > >
> > > > > -	/* Binding to port can be expensive so it's prohibited in the
> > > helper.
> > > > > -	 * Only binding to IP is supported.
> > > > > -	 */
> > > > >  	err = -EINVAL;
> > > > >  	if (addr_len < offsetofend(struct sockaddr, sa_family))
> > > > >  		return err;
> > > > >  	if (addr->sa_family == AF_INET) {
> > > > >  		if (addr_len < sizeof(struct sockaddr_in))
> > > > >  			return err;
> > > > > -		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
> > > > > -			return err;
> > > > >  		return __inet_bind(sk, addr, addr_len,
> > > > > +				   BIND_FROM_BPF |
> > > > >  				   BIND_FORCE_ADDRESS_NO_PORT);
> > > >
> > > > Should BIND_FORCE_ADDRESS_NO_PORT be passed only if port is zero?
> > > > Passing non zero port and BIND_FORCE_ADDRESS_NO_PORT at the same  
> time
> > > > looks confusing (even though it works).
> > > Makes sense, will remove it here, thx.
> > Looking at it some more, I think we need to always have that
> > BIND_FORCE_ADDRESS_NO_PORT. Otherwise, it might regress your
> > usecase with zero port:
> >
> >   if (snum || !(inet->bind_address_no_port ||
> >                (flags & BIND_FORCE_ADDRESS_NO_PORT)))
> >
> > If snum == 0 we want to have either the flag on or
> > IP_BIND_ADDRESS_NO_PORT being set on the socket to prevent the port
> > allocation a bind time.

> Yes, if snum == 0 then flag is needed, that's why my previous comment
> has "only if port is zero" part.

> > If snum != 0, BIND_FORCE_ADDRESS_NO_PORT doesn't matter and the port
> > is passed as an argument. We don't need to search for a free one, just
> > to confirm it's not used.

> Yes, if snum != 0 then flag doesn't matter. So both cases are covered by
> your current code and that's what I meant by "(even though it works)".

> My point is in the "snum != 0" case it would look better not to pass the
> flag since:

> 1) as we see the flag doesn't matter on one hand;

> 2) but passing both port number and flag that says "bind only to address,
>     but not to port" can look confusing and raises a question "which
>     options wins? the one that sets the port or the one that asks to
>     ignore the port" and that question can be answered only by looking at
>     __inet_bind implementation.

> so basically what I mean is:

> 		flags = BIND_FROM_BPF;
> 		if (((struct sockaddr_in *)addr)->sin_port == htons(0))
> 			flags &= BIND_FORCE_ADDRESS_NO_PORT;

> That won't change anything for "snum == 0" case, but it would make the
> "snum != 0" case more readable IMO.

> Does it clarify?
Yes, it does, thanks! I somehow missed your 'only if port is zero' part.

  reply	other threads:[~2020-05-05 17:43 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-04 17:34 [PATCH bpf-next 0/4] bpf: allow any port in bpf_bind helper Stanislav Fomichev
2020-05-04 17:34 ` [PATCH bpf-next 1/4] selftests/bpf: generalize helpers to control backround listener Stanislav Fomichev
2020-05-05  6:23   ` Andrii Nakryiko
2020-05-05 16:08     ` sdf
2020-05-05 18:50       ` Andrii Nakryiko
2020-05-04 17:34 ` [PATCH bpf-next 2/4] selftests/bpf: adopt accept_timeout from sockmap_listen Stanislav Fomichev
2020-05-04 17:34 ` [PATCH bpf-next 3/4] net: refactor arguments of inet{,6}_bind Stanislav Fomichev
2020-05-05 18:16   ` Martin KaFai Lau
2020-05-05 18:19     ` Stanislav Fomichev
2020-05-04 17:34 ` [PATCH bpf-next 4/4] bpf: allow any port in bpf_bind helper Stanislav Fomichev
2020-05-04 23:22   ` Andrey Ignatov
2020-05-05 16:02     ` sdf
2020-05-05 17:09       ` sdf
2020-05-05 17:33         ` Andrey Ignatov
2020-05-05 17:43           ` sdf [this message]
2020-05-05 18:20       ` Andrey Ignatov

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=20200505174327.GF241848@google.com \
    --to=sdf@google.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    --cc=rdna@fb.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 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).