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 2E1515A0276 for ; Wed, 6 Mar 2024 06:58:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709704722; bh=JltpTFH36i82Wu0cZ0tPZaCbBClXvEmBylgWlRq6Q6A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y4XkrLzdFyzp7YWykXeg4oD2Fp/thALMpwXZiJQpsyMdyflxgz3JWETmgcGYf7MRE 4cfkSCPVk4IEwEaUjJTR56mm1JkxF7e3UEIgUpTdobSKWA140UpoFEXLwxn4Goyh1B UYu0s0fu3Wojzi579iiOFvUjx/ff4gTZqXbYAYsLvi5ozAwDrdYsCGtOBRc4dptIi2 /EDRB9fqmqnkqxONsoU7ckKMz1nhmMEASVqr6SY2NjaLEAdnpGic7FEJMQWpGyB/1D +rqUSQTCAVAB8HdGttRK/HHukODQmnUXZjwSMxiLqANv40/3iJtuPQEQq1CeRvVL0y Ah5YqXBvq77Fg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TqMDy1mzQz4wcC; Wed, 6 Mar 2024 16:58:42 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 1/9] pcap: add pcap_iov() Date: Wed, 6 Mar 2024 16:58:30 +1100 Message-ID: <20240306055838.1180477-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240306055838.1180477-1-david@gibson.dropbear.id.au> References: <20240306055838.1180477-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 7C2EA3LCGGWKERE6IJMQUKE6ZUGRGAWO X-Message-ID-Hash: 7C2EA3LCGGWKERE6IJMQUKE6ZUGRGAWO 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: lvivier@redhat.com, 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: From: Laurent Vivier Introduce a new function pcap_iov() to capture packet desribed by an IO vector. Update pcap_frame() to manage iovcnt > 1. Signed-off-by: Laurent Vivier Reviewed-by: David Gibson Message-ID: <20240303135114.1023026-2-lvivier@redhat.com> [dwg: Fixed trivial cppcheck regressions] Signed-off-by: David Gibson --- iov.c | 1 - pcap.c | 27 +++++++++++++++++++++++---- pcap.h | 1 + 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/iov.c b/iov.c index e3312628..e7b4eb75 100644 --- a/iov.c +++ b/iov.c @@ -146,7 +146,6 @@ size_t iov_to_buf(const struct iovec *iov, size_t iov_cnt, * * Returns: The total size in bytes. */ -/* cppcheck-suppress unusedFunction */ size_t iov_size(const struct iovec *iov, size_t iov_cnt) { unsigned int i; diff --git a/pcap.c b/pcap.c index a4057b5f..a0f01ad3 100644 --- a/pcap.c +++ b/pcap.c @@ -32,6 +32,7 @@ #include "passt.h" #include "log.h" #include "pcap.h" +#include "iov.h" #define PCAP_VERSION_MINOR 4 @@ -78,7 +79,7 @@ struct pcap_pkthdr { static void pcap_frame(const struct iovec *iov, size_t iovcnt, size_t offset, const struct timeval *tv) { - size_t len = iov->iov_len - offset; + size_t len = iov_size(iov, iovcnt) - offset; struct pcap_pkthdr h = { .tv_sec = tv->tv_sec, .tv_usec = tv->tv_usec, @@ -87,10 +88,8 @@ static void pcap_frame(const struct iovec *iov, size_t iovcnt, }; struct iovec hiov = { &h, sizeof(h) }; - (void)iovcnt; - if (write_remainder(pcap_fd, &hiov, 1, 0) < 0 || - write_remainder(pcap_fd, iov, 1, offset) < 0) { + write_remainder(pcap_fd, iov, iovcnt, offset) < 0) { debug("Cannot log packet, length %zu: %s", len, strerror(errno)); } @@ -135,6 +134,26 @@ void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n, pcap_frame(iov + i * frame_parts, frame_parts, offset, &tv); } +/* + * pcap_iov - Write packet data described by an I/O vector + * to a pcap file descriptor. + * + * @iov: Pointer to the array of struct iovec describing the I/O vector + * containing packet data to write, including L2 header + * @iovcnt: Number of buffers (@iov entries) + */ +/* cppcheck-suppress unusedFunction */ +void pcap_iov(const struct iovec *iov, size_t iovcnt) +{ + struct timeval tv; + + if (pcap_fd == -1) + return; + + gettimeofday(&tv, NULL); + pcap_frame(iov, iovcnt, 0, &tv); +} + /** * pcap_init() - Initialise pcap file * @c: Execution context diff --git a/pcap.h b/pcap.h index 85fc58e5..15d46572 100644 --- a/pcap.h +++ b/pcap.h @@ -9,6 +9,7 @@ void pcap(const char *pkt, size_t len); void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned int n, size_t offset); +void pcap_iov(const struct iovec *iov, size_t iovcnt); void pcap_init(struct ctx *c); #endif /* PCAP_H */ -- 2.44.0