Signed-off-by: Laurent Vivier
---
Notes:
v2:
- introduce pcap_header(), a common helper to write
packet header
- use writev() rather than write() in a loop
- add functions comment
pcap.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
pcap.h | 1 +
2 files changed, 55 insertions(+), 7 deletions(-)
diff --git a/pcap.c b/pcap.c
index 501d52d4992b..3869a403dd0f 100644
--- a/pcap.c
+++ b/pcap.c
@@ -20,6 +20,7 @@
#include
#include
#include
+#include
#include
#include
#include
@@ -31,6 +32,7 @@
#include "util.h"
#include "passt.h"
#include "log.h"
+#include "iov.h"
#define PCAP_VERSION_MINOR 4
@@ -65,6 +67,28 @@ struct pcap_pkthdr {
uint32_t len;
};
+/*
+ * pcap_header - Write a pcap packet header to the pcap file descriptor (pcap_fd).
+ *
+ * @len: Length of the packet data.
+ * @tv: Pointer to a timeval struct containing the timestamp for the packet.
+ *
+ * Returns; -1 in case of error, otherwise, 0 to indicate success.
+ */
+static int pcap_header(size_t len, const struct timeval *tv)
+{
+ 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)
+ return -1;
+
+ return 0;
+}
+
/**
* pcap_frame() - Capture a single frame to pcap file with given timestamp
* @pkt: Pointer to data buffer, including L2 headers
@@ -75,13 +99,7 @@ struct pcap_pkthdr {
*/
static int pcap_frame(const char *pkt, size_t len, const struct timeval *tv)
{
- 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)
+ if (pcap_header(len, tv) < 0 || write(pcap_fd, pkt, len) < 0)
return -errno;
return 0;
@@ -130,6 +148,35 @@ void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset)
}
}
+/*
+ * pcap_iov - Write packet data described by a scatter/gather I/O vector (iov)
+ * to a pcap file descriptor (pcap_fd).
+ *
+ * @iov: Pointer to the array of struct iovec describing the scatter/gather
+ * I/O vector containing packet data to write, including L2 header
+ * @n: Number of elements in the iov array.
+ */
+void pcap_iov(const struct iovec *iov, unsigned int n)
+{
+ struct timeval tv;
+ size_t len;
+
+ if (pcap_fd == -1)
+ return;
+
+ gettimeofday(&tv, NULL);
+
+ len = iov_size(iov, n);
+
+ if (pcap_header(len, &tv) < 0) {
+ debug("Cannot write pcap header");
+ return;
+ }
+
+ if (writev(pcap_fd, iov, n) < 0)
+ debug("Cannot log packet using writev(), n = %u\n", n);
+}
+
/**
* 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 */
--
2.42.0