From 24410b37a44e819877bdb4449a3147567a866ffa Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 28 Feb 2024 12:52:02 +1100 Subject: pcap: Update pcap_frame() to take an iovec and offset 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 Signed-off-by: Stefano Brivio --- pcap.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/pcap.c b/pcap.c index 501d52d..5cd7a8c 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); } /** -- cgit v1.2.3