pcap: Replace pcapm() with pcap_multiple()

pcapm() captures multiple frames from a msghdr, however the only thing it
cares about in the msghdr is the list of buffers, where it assumes there is
one frame to capture per buffer.  That's what we want for its single caller
but it's not the only obvious choice here (one frame per msghdr would
arguably make more sense in isolation).  In addition pcapm() has logic
that only makes sense in the context of the passt specific path its called
from: it skips the first 4 bytes of each buffer, because those have the
qemu vnet_len rather than the frame proper.

Make this clearer by replacing pcapm() with pcap_multiple() which more
explicitly takes one struct iovec per frame, and parameterizes how much of
each buffer to skip (i.e. the offset of the frame within the buffer).

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2023-01-06 11:43:06 +11:00 committed by Stefano Brivio
parent cb3c1ce307
commit d3089eb0ea
3 changed files with 12 additions and 11 deletions

18
pcap.c
View file

@ -105,10 +105,12 @@ void pcap(const char *pkt, size_t len)
} }
/** /**
* pcapm() - Capture multiple frames from message header to pcap file * pcap_multiple() - Capture multiple frames
* @mh: Pointer to sendmsg() message header buffer * @iov: Array of iovecs, one entry per frame
* @n: Number of frames to capture
* @offset: Offset of the frame within each iovec buffer
*/ */
void pcapm(const struct msghdr *mh) void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset)
{ {
struct timeval tv; struct timeval tv;
unsigned int i; unsigned int i;
@ -118,13 +120,11 @@ void pcapm(const struct msghdr *mh)
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
for (i = 0; i < mh->msg_iovlen; i++) { for (i = 0; i < n; i++) {
const struct iovec *iov = &mh->msg_iov[i]; if (pcap_frame((char *)iov[i].iov_base + offset,
iov[i].iov_len - offset, &tv) != 0) {
if (pcap_frame((char *)iov->iov_base + 4,
iov->iov_len - 4, &tv) != 0) {
debug("Cannot log packet, length %lu", debug("Cannot log packet, length %lu",
iov->iov_len - 4); iov->iov_len - offset);
return; return;
} }
} }

2
pcap.h
View file

@ -7,7 +7,7 @@
#define PCAP_H #define PCAP_H
void pcap(const char *pkt, size_t len); void pcap(const char *pkt, size_t len);
void pcapm(const struct msghdr *mh); void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offset);
void pcapmm(const struct mmsghdr *mmh, unsigned int vlen); void pcapmm(const struct mmsghdr *mmh, unsigned int vlen);
void pcap_init(struct ctx *c); void pcap_init(struct ctx *c);

3
tcp.c
View file

@ -1472,7 +1472,8 @@ static void tcp_l2_buf_flush(struct ctx *c, struct msghdr *mh,
} }
} }
*buf_used = *buf_bytes = 0; *buf_used = *buf_bytes = 0;
pcapm(mh);
pcap_multiple(mh->msg_iov, mh->msg_iovlen, sizeof(uint32_t));
} }
/** /**