From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 532315A0275 for ; Wed, 28 Feb 2024 02:52:11 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709085128; bh=jlqSng5bHKTJ9FNIiSNjSTR+1NDFfqQlphDOunXh180=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qtEKfzIQ4SIT2QQ0ShVsCkjNLShhSx5Ff9RQFsVtZ6jdykHZcHX9w3hj2Kvrock3o 8HGtTA7UNFtnKCtk/YAzUHxuy1zbcKpwNbAUHBxVej8Au4GK9nlfv/nTP3a+xV8nNb d2ei6bk+W3v89ayND+DYKrhlGovjWb309vM5FKcRprLcMpn/nQlmC72qUINyvvXxdc VcVwgZ0U9hvchuZVBybRt76wzvZ5zBKrwA+LetETCFlN82EqOqpB3CrrKcj5yoBojL iizyiiMjxb4qcodP9fUW1O0nMoPvGHIcKDirCpMBkTv2dJPq1h+z5tWHNakE8Gkbw8 epS0RYEVWYg4Q== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Tky5h6Hc4z4wxt; Wed, 28 Feb 2024 12:52:08 +1100 (AEDT) From: David Gibson To: Laurent Vivier , passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 3/7] pcap: Update pcap_frame() to take an iovec and offset Date: Wed, 28 Feb 2024 12:52:02 +1100 Message-ID: <20240228015206.1214242-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240228015206.1214242-1-david@gibson.dropbear.id.au> References: <20240228015206.1214242-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: WA7FGG4MVWPQQN4RC7M7QFYKB2UQW6EX X-Message-ID-Hash: WA7FGG4MVWPQQN4RC7M7QFYKB2UQW6EX 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: Update the low-level helper pcap_frame() to take a struct iovec and offset within it, rather than an explicit pointer and length for the frame. This moves the handling of an offset (to skip vnet_len) from pcap_multiple() to pcap_frame(). This doesn't accomplish a great deal immediately, but will make subsequent changes easier. Signed-off-by: David Gibson --- pcap.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/pcap.c b/pcap.c index 501d52d4..5cd7a8cc 100644 --- a/pcap.c +++ b/pcap.c @@ -67,24 +67,25 @@ struct pcap_pkthdr { /** * pcap_frame() - Capture a single frame to pcap file with given timestamp - * @pkt: Pointer to data buffer, including L2 headers - * @len: L2 packet length + * @iov: IO vector referencing buffer containing frame (with L2 headers) + * @offset: Offset of the frame from @iov->iov_base * @tv: Timestamp * * Returns: 0 on success, -errno on error writing to the file */ -static int pcap_frame(const char *pkt, size_t len, const struct timeval *tv) +static void pcap_frame(const struct iovec *iov, size_t offset, + const struct timeval *tv) { + size_t len = iov->iov_len - offset; struct pcap_pkthdr h; h.tv_sec = tv->tv_sec; h.tv_usec = tv->tv_usec; h.caplen = h.len = len; - if (write(pcap_fd, &h, sizeof(h)) < 0 || write(pcap_fd, pkt, len) < 0) - return -errno; - - return 0; + if (write(pcap_fd, &h, sizeof(h)) < 0 || + write(pcap_fd, (char *)iov->iov_base + offset, len) < 0) + debug("Cannot log packet, length %zu", len); } /** @@ -94,14 +95,14 @@ static int pcap_frame(const char *pkt, size_t len, const struct timeval *tv) */ void pcap(const char *pkt, size_t len) { + struct iovec iov = { (char *)pkt, len }; struct timeval tv; if (pcap_fd == -1) return; gettimeofday(&tv, NULL); - if (pcap_frame(pkt, len, &tv) != 0) - debug("Cannot log packet, length %zu", len); + pcap_frame(&iov, 0, &tv); } /** @@ -120,14 +121,8 @@ void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset) gettimeofday(&tv, NULL); - for (i = 0; i < n; i++) { - if (pcap_frame((char *)iov[i].iov_base + offset, - iov[i].iov_len - offset, &tv) != 0) { - debug("Cannot log packet, length %zu", - iov->iov_len - offset); - return; - } - } + for (i = 0; i < n; i++) + pcap_frame(iov + i, offset, &tv); } /** -- 2.43.2