On Fri, Feb 02, 2024 at 03:11:29PM +0100, Laurent Vivier wrote:Signed-off-by: Laurent Vivier <lvivier(a)redhat.com> --- pcap.c | 32 ++++++++++++++++++++++++++++++++ pcap.h | 1 + 2 files changed, 33 insertions(+) diff --git a/pcap.c b/pcap.c index 501d52d4992b..b002bb01314c 100644 --- a/pcap.c +++ b/pcap.c @@ -31,6 +31,7 @@ #include "util.h" #include "passt.h" #include "log.h" +#include "iov.h" #define PCAP_VERSION_MINOR 4 @@ -130,6 +131,37 @@ void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset) } }Function comment.+void pcap_iov(const struct iovec *iov, unsigned int n) +{ + struct timeval tv; + struct pcap_pkthdr h; + size_t len; + unsigned int i; + + if (pcap_fd == -1) + return; + + gettimeofday(&tv, NULL); + + len = iov_size(iov, n); + + 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) { + debug("Cannot write pcap header"); + return; + }It would be nice to have a common helper for writing the header used by pcap_iov() and pcap_frame().+ for (i = 0; i < n; i++) { + if (write(pcap_fd, iov[i].iov_base, iov[i].iov_len) < 0) {You should be able to avoid the loop by using writev() no? Although, both here and the existing code we should, technically, be looping on short write()s to make sure we've written everything. That's more awkward with an iov.+ debug("Cannot log packet, iov %d length %lu\n", + i, iov[i].iov_len); + } + } +} + /** * pcap_init() - Initialise pcap file * @c: Execution context diff --git a/pcap.h b/pcap.h index da5a7e846b72..732a0ddf14cc 100644 --- a/pcap.h +++ b/pcap.h @@ -8,6 +8,7 @@ void pcap(const char *pkt, size_t len); void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset); +void pcap_iov(const struct iovec *iov, unsigned int n); void pcap_init(struct ctx *c); #endif /* PCAP_H */-- David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson