public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 3/6] tap: Restructure in tap_pasta_input()
Date: Tue, 3 Sep 2024 21:25:42 +0200	[thread overview]
Message-ID: <20240903212542.4934f5a6@elisabeth> (raw)
In-Reply-To: <20240903120235.1688429-4-david@gibson.dropbear.id.au>

On Tue,  3 Sep 2024 22:02:32 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> tap_pasta_input() has a rather confusing structure, using two gotos.
> Remove these by restructuring the function to have the main loop condition
> based on filling our buffer space, with errors or running out of data
> treated as the exception, rather than the other way around.  This allows
> us to handle the EINTR which triggered the 'restart' goto with a continue.
> 
> The outer 'redo' was triggered if we completely filled our buffer, to flush
> it and do another pass.  This one is unnecessary since we don't (yet) use
> EPOLLET on the tap device: if there's still more data we'll get another
> event and re-enter the loop.
> 
> Along the way handle a couple of extra edge cases:
>  - Check for EWOULDBLOCK as well as EAGAIN for the benefit of any future
>    ports where those might not have the same value
>  - Detect EOF on the tap device and exit in that case
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  tap.c | 42 +++++++++++++++++-------------------------
>  1 file changed, 17 insertions(+), 25 deletions(-)
> 
> diff --git a/tap.c b/tap.c
> index 9ee59faa..71742748 100644
> --- a/tap.c
> +++ b/tap.c
> @@ -1073,42 +1073,34 @@ void tap_handler_passt(struct ctx *c, uint32_t events,
>  static void tap_pasta_input(struct ctx *c, const struct timespec *now)
>  {
>  	ssize_t n, len;
> -	int ret;
> -
> -redo:
> -	n = 0;
>  
>  	tap_flush_pools();
> -restart:
> -	while ((len = read(c->fd_tap, pkt_buf + n, TAP_BUF_BYTES - n)) > 0) {
>  
> -		if (len < (ssize_t)sizeof(struct ethhdr) ||
> -		    len > (ssize_t)ETH_MAX_MTU) {
> -			n += len;
> -			continue;
> +	for (n = 0; n < (ssize_t)TAP_BUF_BYTES; n += len) {
> +		len = read(c->fd_tap, pkt_buf + n, TAP_BUF_BYTES - n);
> +
> +		if (len <= 0) {
> +			if (errno == EINTR) {

You might be checking errno when read() returns 0, in which case it's
not set. I guess you should zero out errno before read() if you want to
keep this, or, alternatively, directly handle len == 0 here.

> +				len = 0;
> +				continue;
> +			}
> +			break;
>  		}
>  
> +		/* Ignore frames of bad length */
> +		if (len < (ssize_t)sizeof(struct ethhdr) ||
> +		    len > (ssize_t)ETH_MAX_MTU)
> +			continue;
>  
>  		tap_add_packet(c, len, pkt_buf + n);
> -
> -		if ((n += len) == TAP_BUF_BYTES)
> -			break;
>  	}
>  
> -	if (len < 0 && errno == EINTR)
> -		goto restart;
> -
> -	ret = errno;
> +	if (len < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
> +		die("Error on tap device, exiting");
> +	else if (len == 0)
> +		die("EOF on tap device, exiting");
>  
>  	tap_handler(c, now);
> -
> -	if (len > 0 || ret == EAGAIN)
> -		return;
> -
> -	if (n == TAP_BUF_BYTES)
> -		goto redo;
> -
> -	die("Error on tap device, exiting");
>  }
>  
>  /**

-- 
Stefano


  reply	other threads:[~2024-09-03 19:25 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-03 12:02 [PATCH 0/6] RFC: Clean up tap-side event handling David Gibson
2024-09-03 12:02 ` [PATCH 1/6] tap: Split out handling of EPOLLIN events David Gibson
2024-09-03 19:25   ` Stefano Brivio
2024-09-04  1:17     ` David Gibson
2024-09-03 12:02 ` [PATCH 2/6] tap: Improve handling of EINTR in tap_passt_input() David Gibson
2024-09-03 19:25   ` Stefano Brivio
2024-09-04  1:30     ` David Gibson
2024-09-03 12:02 ` [PATCH 3/6] tap: Restructure in tap_pasta_input() David Gibson
2024-09-03 19:25   ` Stefano Brivio [this message]
2024-09-04  1:33     ` David Gibson
2024-09-03 12:02 ` [PATCH 4/6] tap: Don't risk truncating frames on full buffer " David Gibson
2024-09-03 19:25   ` Stefano Brivio
2024-09-04  1:33     ` David Gibson
2024-09-03 12:02 ` [PATCH 5/6] tap: Re-introduce EPOLLET for tap connections David Gibson
2024-09-03 19:25   ` Stefano Brivio
2024-09-04  1:36     ` David Gibson
2024-09-03 12:02 ` [PATCH 6/6] tap: Stub EPOLLOUT handling David Gibson
2024-09-03 19:25 ` [PATCH 0/6] RFC: Clean up tap-side event handling Stefano Brivio
2024-09-04  3:17   ` David Gibson
2024-09-04 17:19     ` Stefano Brivio
2024-09-05  0:35       ` David Gibson
2024-09-05  8:32         ` Stefano Brivio
2024-09-05 11:33           ` Stefano Brivio

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=20240903212542.4934f5a6@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

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 IMAP folder(s).