From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id E76815A031A for ; Fri, 05 Jul 2024 12:44:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1720176252; bh=Px2fq4JZIGde3uZpfOl7crP/YDwwTY4g9wFAKkMNg8Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FBAwK7EUa2UZzNDDWt9YWzghjXITXydwUV3ZBF5p3plLWJS4gOuteTzhUnTH7xQ/K 1BSPG8IQ2pRe9YSJvRD892NQokEuRPGUcQ1/GYpG0rB16mrv0c97MUH7vLQtbaVRMo r89ZuYX/IZli2fNmOKG9c0HjMmehQ9DNYHid5F+A7EoXFbTdEr6+CYH0QTgbsGNXlf WNtErL3PJjoDY4gt3IVU46k3ON/Mz9+tKjq5j8jsN2Z+WvyNBdc4F7dQVn+XIHNXLl SGdJh5hU7cbpcVno/JgYpe+rtZ8v1q5YperfsZkw5JBlJcwkzqJcBC5Pu1n3cP9c87 /I69N/tr5dy4g== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4WFqrX0Ny5z4x0t; Fri, 5 Jul 2024 20:44:12 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 08/11] udp: Move some more of sock_handler tasks into sub-functions Date: Fri, 5 Jul 2024 20:44:06 +1000 Message-ID: <20240705104409.3847002-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240705104409.3847002-1-david@gibson.dropbear.id.au> References: <20240705104409.3847002-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 5TRTWE3NDG4KRUVR4GI7SHRZPJLGSFVT X-Message-ID-Hash: 5TRTWE3NDG4KRUVR4GI7SHRZPJLGSFVT X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: udp_buf_sock_handler(), udp_splice_send() and udp_tap_send loosely, do four things between them: 1. Receive some datagrams from a socket 2. Split those datagrams into batches depending on how they need to be sent (via tap or via a specific splice socket) 3. Prepare buffers for each datagram to send it onwards 4. Actually send it onwards Split (1) and (3) into specific helper functions. This isn't immediately useful (udp_splice_prepare(), in particular, is trivial), but it will make further reworks clearer. Signed-off-by: David Gibson --- udp.c | 130 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 46 deletions(-) diff --git a/udp.c b/udp.c index 2d403378..af5f23f0 100644 --- a/udp.c +++ b/udp.c @@ -490,6 +490,16 @@ static int udp_mmh_splice_port(union epoll_ref ref, const struct mmsghdr *mmh) return -1; } +/** + * udp_splice_prepare() - Prepare one datagram for splicing + * @mmh: Receiving mmsghdr array + * @idx: Index of the datagram to prepare + */ +static void udp_splice_prepare(struct mmsghdr *mmh, unsigned idx) +{ + udp_mh_splice[idx].msg_hdr.msg_iov->iov_len = mmh[idx].msg_len; +} + /** * udp_splice_send() - Send datagrams from socket to socket * @c: Execution context @@ -535,7 +545,7 @@ static unsigned udp_splice_send(const struct ctx *c, size_t start, size_t n, } do { - udp_mh_splice[i].msg_hdr.msg_iov->iov_len = mmh_recv[i].msg_len; + udp_splice_prepare(mmh_recv, i); if (++i >= n) break; @@ -706,6 +716,42 @@ static size_t udp_update_hdr6(const struct ctx *c, return l4len; } +/** + * udp_tap_prepare() - Convert one datagram into a tap frame + * @c: Execution context + * @mmh: Receiving mmsghdr array + * @idx: Index of the datagram to prepare + * @dstport: Destination port + * @v6: Prepare for IPv6? + * @now: Current timestamp + */ +static void udp_tap_prepare(const struct ctx *c, struct mmsghdr *mmh, + unsigned idx, in_port_t dstport, bool v6, + const struct timespec *now) +{ + struct iovec (*tap_iov)[UDP_NUM_IOVS] = &udp_l2_iov[idx]; + struct udp_payload_t *bp = &udp_payload[idx]; + struct udp_meta_t *bm = &udp_meta[idx]; + size_t l4len; + + if (v6) { + l4len = udp_update_hdr6(c, &bm->ip6h, &bm->s_in.sa6, bp, + dstport, mmh[idx].msg_len, now); + tap_hdr_update(&bm->taph, l4len + sizeof(bm->ip6h) + + sizeof(udp6_eth_hdr)); + (*tap_iov)[UDP_IOV_ETH] = IOV_OF_LVALUE(udp6_eth_hdr); + (*tap_iov)[UDP_IOV_IP] = IOV_OF_LVALUE(bm->ip6h); + } else { + l4len = udp_update_hdr4(c, &bm->ip4h, &bm->s_in.sa4, bp, + dstport, mmh[idx].msg_len, now); + tap_hdr_update(&bm->taph, l4len + sizeof(bm->ip4h) + + sizeof(udp4_eth_hdr)); + (*tap_iov)[UDP_IOV_ETH] = IOV_OF_LVALUE(udp4_eth_hdr); + (*tap_iov)[UDP_IOV_IP] = IOV_OF_LVALUE(bm->ip4h); + } + (*tap_iov)[UDP_IOV_PAYLOAD].iov_len = l4len; +} + /** * udp_tap_send() - Prepare UDP datagrams and send to tap interface * @c: Execution context @@ -737,29 +783,7 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n, mmh_recv = udp4_mh_recv; do { - struct iovec (*tap_iov)[UDP_NUM_IOVS] = &udp_l2_iov[i]; - struct udp_payload_t *bp = &udp_payload[i]; - struct udp_meta_t *bm = &udp_meta[i]; - size_t l4len; - - if (ref.udp.v6) { - l4len = udp_update_hdr6(c, &bm->ip6h, - &bm->s_in.sa6, bp, dstport, - udp6_mh_recv[i].msg_len, now); - tap_hdr_update(&bm->taph, l4len + sizeof(bm->ip6h) + - sizeof(udp6_eth_hdr)); - (*tap_iov)[UDP_IOV_ETH] = IOV_OF_LVALUE(udp6_eth_hdr); - (*tap_iov)[UDP_IOV_IP] = IOV_OF_LVALUE(bm->ip6h); - } else { - l4len = udp_update_hdr4(c, &bm->ip4h, - &bm->s_in.sa4, bp, dstport, - udp4_mh_recv[i].msg_len, now); - tap_hdr_update(&bm->taph, l4len + sizeof(bm->ip4h) + - sizeof(udp4_eth_hdr)); - (*tap_iov)[UDP_IOV_ETH] = IOV_OF_LVALUE(udp4_eth_hdr); - (*tap_iov)[UDP_IOV_IP] = IOV_OF_LVALUE(bm->ip4h); - } - (*tap_iov)[UDP_IOV_PAYLOAD].iov_len = l4len; + udp_tap_prepare(c, mmh_recv, i, dstport, ref.udp.v6, now); if (++i >= n) break; @@ -771,6 +795,39 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n, return i - start; } +/** + * udp_sock_recv() - Receive datagrams from a socket + * @c: Execution context + * @s: Socket to receive from + * @events: epoll events bitmap + * @mmh mmsghdr array to receive into + * + * #syscalls recvmmsg + */ +int udp_sock_recv(const struct ctx *c, int s, uint32_t events, + struct mmsghdr *mmh) +{ + /* For not entirely clear reasons (data locality?) pasta gets better + * throughput if we receive tap datagrams one at a atime. For small + * splice datagrams throughput is slightly better if we do batch, but + * it's slightly worse for large splice datagrams. Since we don't know + * before we receive whether we'll use tap or splice, always go one at a + * time for pasta mode. + */ + int n = (c->mode == MODE_PASTA ? 1 : UDP_MAX_FRAMES); + + if (c->no_udp || !(events & EPOLLIN)) + return 0; + + n = recvmmsg(s, mmh, n, 0, NULL); + if (n < 0) { + err_perror("Error receiving datagrams"); + return 0; + } + + return n; +} + /** * udp_buf_sock_handler() - Handle new data from socket * @c: Execution context @@ -783,21 +840,11 @@ static unsigned udp_tap_send(const struct ctx *c, size_t start, size_t n, void udp_buf_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t events, const struct timespec *now) { - /* For not entirely clear reasons (data locality?) pasta gets - * better throughput if we receive tap datagrams one at a - * atime. For small splice datagrams throughput is slightly - * better if we do batch, but it's slightly worse for large - * splice datagrams. Since we don't know before we receive - * whether we'll use tap or splice, always go one at a time - * for pasta mode. - */ - ssize_t n = (c->mode == MODE_PASTA ? 1 : UDP_MAX_FRAMES); + struct mmsghdr *mmh_recv = ref.udp.v6 ? udp6_mh_recv : udp4_mh_recv; in_port_t dstport = ref.udp.port; - bool v6 = ref.udp.v6; - struct mmsghdr *mmh_recv; - int i, m; + int n, m, i; - if (c->no_udp || !(events & EPOLLIN)) + if ((n = udp_sock_recv(c, ref.fd, events, mmh_recv)) <= 0) return; if (ref.udp.pif == PIF_SPLICE) @@ -805,15 +852,6 @@ void udp_buf_sock_handler(const struct ctx *c, union epoll_ref ref, uint32_t eve else if (ref.udp.pif == PIF_HOST) dstport += c->udp.fwd_in.f.delta[dstport]; - if (v6) - mmh_recv = udp6_mh_recv; - else - mmh_recv = udp4_mh_recv; - - n = recvmmsg(ref.fd, mmh_recv, n, 0, NULL); - if (n <= 0) - return; - /* We divide things into batches based on how we need to send them, * determined by udp_meta[i].splicesrc. To avoid either two passes * through the array, or recalculating splicesrc for a single entry, we -- 2.45.2