tap: Restructure in tap_pasta_input()

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>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2024-09-06 21:49:38 +10:00 committed by Stefano Brivio
parent 11e29054fe
commit d2a1dc744b

49
tap.c
View file

@ -1073,44 +1073,37 @@ void tap_handler_passt(struct ctx *c, uint32_t events,
static void tap_pasta_input(struct ctx *c, const struct timespec *now) static void tap_pasta_input(struct ctx *c, const struct timespec *now)
{ {
ssize_t n, len; ssize_t n, len;
int ret;
redo:
n = 0;
tap_flush_pools(); 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) || for (n = 0; n < (ssize_t)TAP_BUF_BYTES; n += len) {
len > (ssize_t)ETH_MAX_MTU) { len = read(c->fd_tap, pkt_buf + n, TAP_BUF_BYTES - n);
n += len;
if (len == 0) {
die("EOF on tap device, exiting");
} else if (len < 0) {
if (errno == EINTR) {
len = 0;
continue; continue;
} }
if (errno == EAGAIN && errno == EWOULDBLOCK)
tap_add_packet(c, len, pkt_buf + n); break; /* all done for now */
if ((n += len) == TAP_BUF_BYTES)
break;
}
if (len < 0 && errno == EINTR)
goto restart;
ret = errno;
tap_handler(c, now);
if (len > 0 || ret == EAGAIN)
return;
if (n == TAP_BUF_BYTES)
goto redo;
die("Error on tap device, exiting"); die("Error on tap device, exiting");
} }
/* 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);
}
tap_handler(c, now);
}
/** /**
* tap_handler_pasta() - Packet handler for /dev/net/tun file descriptor * tap_handler_pasta() - Packet handler for /dev/net/tun file descriptor
* @c: Execution context * @c: Execution context