public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Andrey Ignatov <rdna@fb.com>
To: Yonghong Song <yhs@fb.com>
Cc: <bpf@vger.kernel.org>, <ast@kernel.org>, <daniel@iogearbox.net>,
	<kernel-team@fb.com>
Subject: Re: [PATCH v2 bpf-next 4/5] selftests/bpf: Add connect_fd_to_fd, connect_wait net helpers
Date: Thu, 14 May 2020 10:42:38 -0700	[thread overview]
Message-ID: <20200514174238.GB22366@rdna-mbp> (raw)
In-Reply-To: <3f5d4625-32ee-7739-ccfb-53c19e38778b@fb.com>

Yonghong Song <yhs@fb.com> [Thu, 2020-05-14 08:56 -0700]:
> On 5/13/20 2:38 PM, Andrey Ignatov wrote:

> > @@ -77,8 +81,6 @@ static const size_t timeo_optlen = sizeof(timeo_sec);
> >   int connect_to_fd(int family, int type, int server_fd)
> >   {
> > -	struct sockaddr_storage addr;
> > -	socklen_t len = sizeof(addr);
> >   	int fd;
> >   	fd = socket(family, type, 0);
> > @@ -87,24 +89,64 @@ int connect_to_fd(int family, int type, int server_fd)
> >   		return -1;
> >   	}
> > -	if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec, timeo_optlen)) {
> > +	if (connect_fd_to_fd(fd, server_fd) < 0 && errno != EINPROGRESS) {
> > +		close(fd);
> 
> Remote possibility. close(fd) may change error code?

It can in some cases that are rather theoritical in this case (e.g.
buggy multi-threaded program closes fd from another thread right before
this close()).

But I can save/restore it before/after close just in case.

> In my opinion, maybe convert the original syscall failure errno to return
> value and carrying on might be a simpler choice?

I wanted to preserve semantics of connect(2) here: return -1 on error,
or fd >= 0 on success.

I guess if I save/restore errno it should be fine.

> > +		return -1;
> > +	}
> > +
> > +	return fd;
> > +}
> > +
> > +int connect_fd_to_fd(int client_fd, int server_fd)
> > +{
> > +	struct sockaddr_storage addr;
> > +	socklen_t len = sizeof(addr);
> > +
> > +	if (setsockopt(client_fd, SOL_SOCKET, SO_RCVTIMEO, &timeo_sec,
> > +		       timeo_optlen)) {
> >   		log_err("Failed to set SO_RCVTIMEO");
> > -		goto out;
> > +		return -1;
> >   	}
> >   	if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
> >   		log_err("Failed to get server addr");
> > -		goto out;
> > +		return -1;
> >   	}
> > -	if (connect(fd, (const struct sockaddr *)&addr, len) < 0) {
> > -		log_err("Fail to connect to server with family %d", family);
> > -		goto out;
> > +	if (connect(client_fd, (const struct sockaddr *)&addr, len) < 0) {
> > +		if (errno != EINPROGRESS)
> > +			log_err("Failed to connect to server");
> 
> Not saying it is possible, but any remote possibility log_err()
> may change error code to EINPROGRESS?

To my best knowledge, neither fprintf(3) nor strerror(3) use
EINPROGRESS, but since in this case having a reliable way to communicate
EINPROGRESS from connect is rather required, I'll save/restore errno for
caling log_err.

-- 
Andrey Ignatov

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

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-13 21:38 [PATCH v2 bpf-next 0/5] bpf: sk lookup, cgroup id helpers in cgroup skb Andrey Ignatov
2020-05-13 21:38 ` [PATCH v2 bpf-next 1/5] bpf: Allow sk lookup " Andrey Ignatov
2020-05-14 15:06   ` Yonghong Song
2020-05-13 21:38 ` [PATCH v2 bpf-next 2/5] bpf: Allow skb_ancestor_cgroup_id helper " Andrey Ignatov
2020-05-14 15:06   ` Yonghong Song
2020-05-13 21:38 ` [PATCH v2 bpf-next 3/5] bpf: Introduce bpf_sk_{,ancestor_}cgroup_id helpers Andrey Ignatov
2020-05-14 15:16   ` Yonghong Song
2020-05-14 16:55     ` Andrey Ignatov
2020-05-14 17:24       ` Yonghong Song
2020-05-14 18:01         ` Andrey Ignatov
2020-05-14 18:15           ` Yonghong Song
2020-05-13 21:38 ` [PATCH v2 bpf-next 4/5] selftests/bpf: Add connect_fd_to_fd, connect_wait net helpers Andrey Ignatov
2020-05-14 15:56   ` Yonghong Song
2020-05-14 17:42     ` Andrey Ignatov [this message]
2020-05-13 21:38 ` [PATCH v2 bpf-next 5/5] selftests/bpf: Test for sk helpers in cgroup skb Andrey Ignatov
2020-05-14 16:07   ` Yonghong Song
2020-05-14 17:46     ` 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=20200514174238.GB22366@rdna-mbp \
    --to=rdna@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=yhs@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