From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id A3EC15A005E for ; Fri, 9 Dec 2022 06:42:38 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NT0KM33XRz4xvL; Fri, 9 Dec 2022 16:42:31 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1670564551; bh=5/jSRDV2CbzchoTAEQW/wQQ6gYZIzu8XwdaTNhl/Ygw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h8q1oSG/wmXy9REKMtqxfr1HupI/AhMIPeDYLQ4ny3I2NI+1Hack8EQZCbEMCPA/K El2afIdPY6u+37SV5ZeL9IDrS9Bne8hq6W3OEOjArNqbtA+EihRuWV08htyTVg0Yzf /x3nPAr3IvIHlCLDdK0i/RyMARn3Kq0wpNcSH6BI= From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 07/18] tap, tcp: Move tap send path to tap.c Date: Fri, 9 Dec 2022 16:42:17 +1100 Message-Id: <20221209054228.4085990-8-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221209054228.4085990-1-david@gibson.dropbear.id.au> References: <20221209054228.4085990-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: FHQPFJRSB7SW3Q66AVNUF53LRSPMO76S X-Message-ID-Hash: FHQPFJRSB7SW3Q66AVNUF53LRSPMO76S 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.3 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: The functions which do the final steps of sending TCP packets on through the tap interface - tcp_l2_buf_flush*() - no longer have anything that's actually specific to TCP in them, other than comments and names. Move them all to tap.c. Signed-off-by: David Gibson --- tap.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tap.h | 1 + tcp.c | 85 +++-------------------------------------------------------- 3 files changed, 89 insertions(+), 81 deletions(-) diff --git a/tap.c b/tap.c index 2e603ed..558a734 100644 --- a/tap.c +++ b/tap.c @@ -53,6 +53,7 @@ #include "netlink.h" #include "pasta.h" #include "packet.h" +#include "tap.h" #include "log.h" /* IPv4 (plus ARP) and IPv6 message batches from tap/guest to IP handlers */ @@ -302,6 +303,89 @@ void tap_icmp6_send(const struct ctx *c, debug("tap: failed to send %lu bytes (IPv6)", len); } +/** + * tap_send_frames_pasta() - Send multiple frames to the pasta tap + * @c: Execution context + * @iov: Array of buffers, each containing one frame + * @n: Number of buffers/frames in @iov + * + * #syscalls:pasta write + */ +static void tap_send_frames_pasta(struct ctx *c, + const struct iovec *iov, size_t n) +{ + size_t i; + + for (i = 0; i < n; i++) { + if (write(c->fd_tap, (char *)iov->iov_base + 4, + iov->iov_len - 4) < 0) { + debug("tap write: %s", strerror(errno)); + if (errno != EAGAIN && errno != EWOULDBLOCK) + tap_handler(c, c->fd_tap, EPOLLERR, NULL); + i--; + } + } +} + +/** + * tap_send_frames_passt() - Send multiple frames to the passt tap + * @c: Execution context + * @iov: Array of buffers, each containing one frame + * @n: Number of buffers/frames in @iov + * + * #syscalls:passt sendmsg send + */ +static void tap_send_frames_passt(const struct ctx *c, + const struct iovec *iov, size_t n) +{ + struct msghdr mh = { + .msg_iov = (void *)iov, + .msg_iovlen = n, + }; + size_t end = 0, missing; + unsigned int i; + ssize_t sent; + char *p; + + sent = sendmsg(c->fd_tap, &mh, MSG_NOSIGNAL | MSG_DONTWAIT); + if (sent < 0) + return; + + /* Ensure a complete last message on partial sendmsg() */ + for (i = 0; i < n; i++, iov++) { + end += iov->iov_len; + if (end >= (size_t)sent) + break; + } + + missing = end - sent; + if (!missing) + return; + + p = (char *)iov->iov_base + iov->iov_len - missing; + if (send(c->fd_tap, p, missing, MSG_NOSIGNAL)) + debug("tap: failed to flush %lu missing bytes to tap", missing); +} + +/** + * tap_send_frames() - Send out multiple prepared frames + * @c: Execution context + * @iov: Array of buffers, each containing one frame (with L2 headers) + * @n: Number of buffers/frames in @iov + */ +void tap_send_frames(struct ctx *c, const struct iovec *iov, size_t n) +{ + if (!n) + return; + + if (c->mode == MODE_PASST) + tap_send_frames_passt(c, iov, n); + else + tap_send_frames_pasta(c, iov, n); + + pcap_multiple(iov, n, sizeof(uint32_t)); +} + PACKET_POOL_DECL(pool_l4, UIO_MAXIOV, pkt_buf); /** diff --git a/tap.h b/tap.h index 674ab5c..ceac890 100644 --- a/tap.h +++ b/tap.h @@ -22,6 +22,7 @@ void tap_icmp6_send(const struct ctx *c, const struct in6_addr *src, const struct in6_addr *dst, void *in, size_t len); int tap_send(const struct ctx *c, const void *data, size_t len); +void tap_send_frames(struct ctx *c, const struct iovec *iov, size_t n); void tap_handler(struct ctx *c, int fd, uint32_t events, const struct timespec *now); void tap_sock_init(struct ctx *c); diff --git a/tcp.c b/tcp.c index 9960a35..0c46a7e 100644 --- a/tcp.c +++ b/tcp.c @@ -1390,93 +1390,16 @@ static void tcp_rst_do(struct ctx *c, struct tcp_tap_conn *conn); tcp_rst_do(c, conn); \ } while (0) -/** - * tcp_l2_buf_flush_pasta() - Send frames on the pasta tap interface - * @c: Execution context - * @iov: Pointer to array of buffers, one per frame - * @n: Number of buffers/frames to flush - */ -static void tcp_l2_buf_flush_pasta(struct ctx *c, - const struct iovec *iov, size_t n) -{ - size_t i; - - for (i = 0; i < n; i++) { - if (write(c->fd_tap, (char *)iov->iov_base + 4, - iov->iov_len - 4) < 0) { - debug("tap write: %s", strerror(errno)); - if (errno != EAGAIN && errno != EWOULDBLOCK) - tap_handler(c, c->fd_tap, EPOLLERR, NULL); - i--; - } - } -} - -/** - * tcp_l2_buf_flush_passt() - Send a message on the passt tap interface - * @c: Execution context - * @iov: Pointer to array of buffers, one per frame - * @n: Number of buffers/frames to flush - */ -static void tcp_l2_buf_flush_passt(const struct ctx *c, - const struct iovec *iov, size_t n) -{ - struct msghdr mh = { - .msg_iov = (void *)iov, - .msg_iovlen = n, - }; - size_t end = 0, missing; - unsigned int i; - ssize_t sent; - char *p; - - sent = sendmsg(c->fd_tap, &mh, MSG_NOSIGNAL | MSG_DONTWAIT); - if (sent < 0) - return; - - /* Ensure a complete last message on partial sendmsg() */ - for (i = 0; i < n; i++, iov++) { - end += iov->iov_len; - if (end >= (size_t)sent) - break; - } - - missing = end - sent; - if (!missing) - return; - - p = (char *)iov->iov_base + iov->iov_len - missing; - if (send(c->fd_tap, p, missing, MSG_NOSIGNAL)) - debug("TCP: failed to flush %lu missing bytes to tap", missing); -} - -/** - * tcp_l2_flags_buf_flush() - Send out buffers for segments with or without data - * @c: Execution context - */ -static void tcp_l2_buf_flush(struct ctx *c, const struct iovec *iov, size_t n) -{ - if (!n) - return; - - if (c->mode == MODE_PASST) - tcp_l2_buf_flush_passt(c, iov, n); - else - tcp_l2_buf_flush_pasta(c, iov, n); - - pcap_multiple(iov, n, sizeof(uint32_t)); -} - /** * tcp_l2_flags_buf_flush() - Send out buffers for segments with no data (flags) * @c: Execution context */ static void tcp_l2_flags_buf_flush(struct ctx *c) { - tcp_l2_buf_flush(c, tcp6_l2_flags_iov, tcp6_l2_flags_buf_used); + tap_send_frames(c, tcp6_l2_flags_iov, tcp6_l2_flags_buf_used); tcp6_l2_flags_buf_used = 0; - tcp_l2_buf_flush(c, tcp4_l2_flags_iov, tcp4_l2_flags_buf_used); + tap_send_frames(c, tcp4_l2_flags_iov, tcp4_l2_flags_buf_used); tcp4_l2_flags_buf_used = 0; } @@ -1486,10 +1409,10 @@ static void tcp_l2_flags_buf_flush(struct ctx *c) */ static void tcp_l2_data_buf_flush(struct ctx *c) { - tcp_l2_buf_flush(c, tcp6_l2_iov, tcp6_l2_buf_used); + tap_send_frames(c, tcp6_l2_iov, tcp6_l2_buf_used); tcp6_l2_buf_used = 0; - tcp_l2_buf_flush(c, tcp4_l2_iov, tcp4_l2_buf_used); + tap_send_frames(c, tcp4_l2_iov, tcp4_l2_buf_used); tcp4_l2_buf_used = 0; } -- 2.38.1