aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-02-28 12:52:02 +1100
committerStefano Brivio <sbrivio@redhat.com>2024-02-29 06:24:10 +0100
commit24410b37a44e819877bdb4449a3147567a866ffa (patch)
treed651fed3d65c524f8b1cb0e2acb26d2cdb0c67d3
parent64b63d9e3e1dc2e4c1f32432cf8954f6cc3f788f (diff)
downloadpasst-24410b37a44e819877bdb4449a3147567a866ffa.tar
passt-24410b37a44e819877bdb4449a3147567a866ffa.tar.gz
passt-24410b37a44e819877bdb4449a3147567a866ffa.tar.bz2
passt-24410b37a44e819877bdb4449a3147567a866ffa.tar.lz
passt-24410b37a44e819877bdb4449a3147567a866ffa.tar.xz
passt-24410b37a44e819877bdb4449a3147567a866ffa.tar.zst
passt-24410b37a44e819877bdb4449a3147567a866ffa.zip
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 <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--pcap.c29
1 files 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);
}
/**