From: David Gibson <david@gibson.dropbear.id.au>
To: Laurent Vivier <lvivier@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v3 2/9] pcap: add pcap_iov()
Date: Mon, 19 Feb 2024 13:50:49 +1100 [thread overview]
Message-ID: <ZdLCCaE9Nt1UYMlL@zatzit> (raw)
In-Reply-To: <20240217150725.661467-3-lvivier@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 4139 bytes --]
On Sat, Feb 17, 2024 at 04:07:18PM +0100, Laurent Vivier wrote:
> Introduce a new function pcap_iov() to capture packet desribed by an IO
> vector.
>
> Move packet header writing to to pcap_header() and use it in
> pcap_frame() and pcap_iov().
>
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>
> Notes:
> v3:
> - update rational
> - update comment
> - use strerror(errno)
> - use size_t for io vector length
>
> v2:
> - introduce pcap_header(), a common helper to write
> packet header
> - use writev() rather than write() in a loop
> - add functions comment
>
> pcap.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
> pcap.h | 1 +
> 2 files changed, 56 insertions(+), 7 deletions(-)
>
> diff --git a/pcap.c b/pcap.c
> index 501d52d4992b..4e213eea8113 100644
> --- a/pcap.c
> +++ b/pcap.c
> @@ -20,6 +20,7 @@
> #include <sys/time.h>
> #include <sys/types.h>
> #include <sys/stat.h>
> +#include <sys/uio.h>
> #include <fcntl.h>
> #include <time.h>
> #include <errno.h>
> @@ -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 pcap file
> + *
> + * @len: Length of the packet data.
> + * @tv: Timestamp for the packet.
> + *
> + * Return: -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,36 @@ 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, size_t 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(): %s\n",
> + strerror(errno));
> +}
> +
> /**
> * pcap_init() - Initialise pcap file
> * @c: Execution context
> diff --git a/pcap.h b/pcap.h
> index da5a7e846b72..4950f617f4c8 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, size_t 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
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2024-02-19 2:52 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-17 15:07 [PATCH v3 0/9] Add vhost-user support to passt (part 1) Laurent Vivier
2024-02-17 15:07 ` [PATCH v3 1/9] iov: add some functions to manage iovec Laurent Vivier
2024-02-19 2:45 ` David Gibson
2024-02-17 15:07 ` [PATCH v3 2/9] pcap: add pcap_iov() Laurent Vivier
2024-02-19 2:50 ` David Gibson [this message]
2024-02-17 15:07 ` [PATCH v3 3/9] checksum: align buffers Laurent Vivier
2024-02-17 15:07 ` [PATCH v3 4/9] checksum: add csum_iov() Laurent Vivier
2024-02-19 2:52 ` David Gibson
2024-02-17 15:07 ` [PATCH v3 5/9] util: move IP stuff from util.[ch] to ip.[ch] Laurent Vivier
2024-02-19 2:59 ` David Gibson
2024-02-17 15:07 ` [PATCH v3 6/9] checksum: use csum_ip4_header() in udp.c and tcp.c Laurent Vivier
2024-02-19 3:01 ` David Gibson
2024-02-29 16:24 ` Stefano Brivio
2024-02-29 23:10 ` David Gibson
2024-03-01 7:58 ` Stefano Brivio
2024-03-01 12:23 ` David Gibson
2024-03-04 17:04 ` Stefano Brivio
2024-02-17 15:07 ` [PATCH v3 7/9] checksum: introduce functions to compute the header part checksum for TCP/UDP Laurent Vivier
2024-02-19 3:08 ` David Gibson
2024-02-28 13:26 ` Laurent Vivier
2024-02-29 0:38 ` David Gibson
2024-02-29 7:05 ` Stefano Brivio
2024-02-29 8:49 ` David Gibson
2024-02-29 8:56 ` Stefano Brivio
2024-02-29 14:15 ` Stefano Brivio
2024-02-29 23:09 ` David Gibson
2024-03-01 6:56 ` Stefano Brivio
2024-03-04 1:54 ` David Gibson
2024-03-04 11:00 ` Stefano Brivio
2024-03-04 22:47 ` Stefano Brivio
2024-03-06 5:09 ` David Gibson
2024-03-08 0:08 ` Stefano Brivio
2024-03-08 1:20 ` David Gibson
2024-02-17 15:07 ` [PATCH v3 8/9] tap: make tap_update_mac() generic Laurent Vivier
2024-02-17 15:07 ` [PATCH v3 9/9] tcp: Introduce ipv4_fill_headers()/ipv6_fill_headers() Laurent Vivier
2024-02-19 3:14 ` David Gibson
2024-02-29 16:29 ` Stefano Brivio
2024-02-29 16:31 ` [PATCH v3 0/9] Add vhost-user support to passt (part 1) Stefano Brivio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZdLCCaE9Nt1UYMlL@zatzit \
--to=david@gibson.dropbear.id.au \
--cc=lvivier@redhat.com \
--cc=passt-dev@passt.top \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).