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 0015B5A005E for ; Fri, 9 Dec 2022 06:42:34 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NT0KM2bynz4xRB; 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=ZNBmiBXVyAuQ5kyo5hTE27QgQuWvtS0oT3S261frFM0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eu/w9AQVXJ32xjpkc5z2IOE6pu8hlrBn3LmhDVM5Kx6QHH3SlsRIjTMsbfEwCPlIm 8IccjJF9OHvlSmQPpnA6bn5eBATy5fQNGLZfKWZiGcCPuo21+nZpYbfqgh3p0uQjzf 7G/qtlSeyRUaMAvUsvtDe7StCpO64DaVCKILRJsE= From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 02/18] pcap: Replace pcapm() with pcap_multiple() Date: Fri, 9 Dec 2022 16:42:12 +1100 Message-Id: <20221209054228.4085990-3-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: WMZ2OGSPOJJT7YRP5WKOXOG7QRRFTRB4 X-Message-ID-Hash: WMZ2OGSPOJJT7YRP5WKOXOG7QRRFTRB4 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: pcapm() captures multiple frames from a msghdr, however the only thing it cares about in the msghdr is the list of buffers, where it assumes there is one frame to capture per buffer. That's what we want for its single caller but it's not the only obvious choice here (one frame per msghdr would arguably make more sense in isolation). In addition pcapm() has logic that only makes sense in the context of the passt specific path its called from: it skips the first 4 bytes of each buffer, because those have the qemu vnet_len rather than the frame proper. Make this clearer by replacing pcapm() with pcap_multiple() which more explicitly takes one struct iovec per frame, and parameterizes how much of each buffer to skip (i.e. the offset of the frame within the buffer). Signed-off-by: David Gibson --- pcap.c | 16 ++++++++-------- pcap.h | 2 +- tcp.c | 3 ++- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/pcap.c b/pcap.c index c9ac5cf..8af9021 100644 --- a/pcap.c +++ b/pcap.c @@ -100,10 +100,12 @@ void pcap(const char *pkt, size_t len) } /** - * pcapm() - Capture multiple frames from message header to pcap file - * @mh: Pointer to sendmsg() message header buffer + * pcap_multiple() - Capture multiple frames + * @iov: Array of iovecs, one entry per frame + * @n: Number of frames to capture + * @offset: Offset of the frame within each iovec buffer */ -void pcapm(const struct msghdr *mh) +void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset) { struct timeval tv; unsigned int i; @@ -113,11 +115,9 @@ void pcapm(const struct msghdr *mh) gettimeofday(&tv, NULL); - for (i = 0; i < mh->msg_iovlen; i++) { - const struct iovec *iov = &mh->msg_iov[i]; - - pcap_frame((char *)iov->iov_base + 4, iov->iov_len - 4, &tv); - } + for (i = 0; i < n; i++) + pcap_frame((char *)iov[i].iov_base + offset, + iov[i].iov_len - offset, &tv); } /** diff --git a/pcap.h b/pcap.h index 9e1736c..eafc89b 100644 --- a/pcap.h +++ b/pcap.h @@ -7,7 +7,7 @@ #define PCAP_H void pcap(const char *pkt, size_t len); -void pcapm(const struct msghdr *mh); +void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset); void pcapmm(const struct mmsghdr *mmh, unsigned int vlen); void pcap_init(struct ctx *c); diff --git a/tcp.c b/tcp.c index cfdae06..ed65a9e 100644 --- a/tcp.c +++ b/tcp.c @@ -1468,7 +1468,8 @@ static void tcp_l2_buf_flush(struct ctx *c, struct msghdr *mh, } } *buf_used = *buf_bytes = 0; - pcapm(mh); + + pcap_multiple(mh->msg_iov, mh->msg_iovlen, sizeof(uint32_t)); } /** -- 2.38.1