diff options
author | Laurent Vivier <lvivier@redhat.com> | 2024-03-06 16:58:30 +1100 |
---|---|---|
committer | Stefano Brivio <sbrivio@redhat.com> | 2024-03-06 08:03:30 +0100 |
commit | 94502fa15e0b7386318e4a7dad78ea05462010d0 (patch) | |
tree | a62bc9725f1cd13f73ca198c10c9947f8969a2c0 | |
parent | 3b9098aa49bd083a7900dc6e0219bf76e389afd4 (diff) | |
download | passt-94502fa15e0b7386318e4a7dad78ea05462010d0.tar passt-94502fa15e0b7386318e4a7dad78ea05462010d0.tar.gz passt-94502fa15e0b7386318e4a7dad78ea05462010d0.tar.bz2 passt-94502fa15e0b7386318e4a7dad78ea05462010d0.tar.lz passt-94502fa15e0b7386318e4a7dad78ea05462010d0.tar.xz passt-94502fa15e0b7386318e4a7dad78ea05462010d0.tar.zst passt-94502fa15e0b7386318e4a7dad78ea05462010d0.zip |
pcap: add pcap_iov()
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 <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Message-ID: <20240303135114.1023026-2-lvivier@redhat.com>
[dwg: Fixed trivial cppcheck regressions]
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r-- | iov.c | 1 | ||||
-rw-r--r-- | pcap.c | 27 | ||||
-rw-r--r-- | pcap.h | 1 |
3 files changed, 24 insertions, 5 deletions
@@ -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; @@ -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 @@ -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 */ |