diff options
author | David Gibson <david@gibson.dropbear.id.au> | 2024-03-08 17:53:24 +1100 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2024-03-14 16:57:37 +0100 |
commit | 4db947d17c8c7dac3b344c8ce0a266f7be159200 (patch) | |
tree | 5db310fa193a86befdf4df2ff940aa6aeed59f82 /tap.c | |
parent | 1ebe787fe460eb83d67792b6af1b02f4f555dc86 (diff) | |
download | passt-4db947d17c8c7dac3b344c8ce0a266f7be159200.tar passt-4db947d17c8c7dac3b344c8ce0a266f7be159200.tar.gz passt-4db947d17c8c7dac3b344c8ce0a266f7be159200.tar.bz2 passt-4db947d17c8c7dac3b344c8ce0a266f7be159200.tar.lz passt-4db947d17c8c7dac3b344c8ce0a266f7be159200.tar.xz passt-4db947d17c8c7dac3b344c8ce0a266f7be159200.tar.zst passt-4db947d17c8c7dac3b344c8ce0a266f7be159200.zip |
tap: Implement tap_send() "slow path" in terms of fast path
Most times we send frames to the guest it goes via tap_send_frames().
However "slow path" protocols - ARP, ICMP, ICMPv6, DHCP and DHCPv6 - go
via tap_send().
As well as being a semantic duplication, tap_send() contains at least one
serious problem: it doesn't properly handle short sends, which can be fatal
on the qemu socket connection, since frame boundaries will get out of sync.
Rewrite tap_send() to call tap_send_frames(). While we're there, rename it
tap_send_single() for clarity.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'tap.c')
-rw-r--r-- | tap.c | 38 |
1 files changed, 17 insertions, 21 deletions
@@ -67,28 +67,28 @@ static PACKET_POOL_NOINIT(pool_tap6, TAP_MSGS, pkt_buf); #define FRAGMENT_MSG_RATE 10 /* # seconds between fragment warnings */ /** - * tap_send() - Send frame, with qemu socket header if needed + * tap_send_single() - Send a single frame * @c: Execution context * @data: Packet buffer * @len: Total L2 packet length - * - * Return: return code from send() or write() */ -int tap_send(const struct ctx *c, const void *data, size_t len) +void tap_send_single(const struct ctx *c, const void *data, size_t len) { - pcap(data, len); + uint32_t vnet_len = htonl(len); + struct iovec iov[2]; + size_t iovcnt = 0; if (c->mode == MODE_PASST) { - int flags = MSG_NOSIGNAL | MSG_DONTWAIT; - uint32_t vnet_len = htonl(len); - - if (send(c->fd_tap, &vnet_len, 4, flags) < 0) - return -1; - - return send(c->fd_tap, data, len, flags); + iov[iovcnt].iov_base = &vnet_len; + iov[iovcnt].iov_len = sizeof(vnet_len); + iovcnt++; } - return write(c->fd_tap, (char *)data, len); + iov[iovcnt].iov_base = (void *)data; + iov[iovcnt].iov_len = len; + iovcnt++; + + tap_send_frames(c, iov, iovcnt, 1); } /** @@ -189,8 +189,7 @@ void tap_udp4_send(const struct ctx *c, struct in_addr src, in_port_t sport, csum_udp4(uh, src, dst, in, len); memcpy(data, in, len); - if (tap_send(c, buf, len + (data - buf)) < 0) - debug("tap: failed to send %zu bytes (IPv4)", len); + tap_send_single(c, buf, len + (data - buf)); } /** @@ -212,8 +211,7 @@ void tap_icmp4_send(const struct ctx *c, struct in_addr src, struct in_addr dst, memcpy(icmp4h, in, len); csum_icmp4(icmp4h, icmp4h + 1, len - sizeof(*icmp4h)); - if (tap_send(c, buf, len + ((char *)icmp4h - buf)) < 0) - debug("tap: failed to send %zu bytes (IPv4)", len); + tap_send_single(c, buf, len + ((char *)icmp4h - buf)); } /** @@ -274,8 +272,7 @@ void tap_udp6_send(const struct ctx *c, csum_udp6(uh, src, dst, in, len); memcpy(data, in, len); - if (tap_send(c, buf, len + (data - buf)) < 1) - debug("tap: failed to send %zu bytes (IPv6)", len); + tap_send_single(c, buf, len + (data - buf)); } /** @@ -298,8 +295,7 @@ void tap_icmp6_send(const struct ctx *c, memcpy(icmp6h, in, len); csum_icmp6(icmp6h, src, dst, icmp6h + 1, len - sizeof(*icmp6h)); - if (tap_send(c, buf, len + ((char *)icmp6h - buf)) < 1) - debug("tap: failed to send %zu bytes (IPv6)", len); + tap_send_single(c, buf, len + ((char *)icmp6h - buf)); } /** |