From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 1AB175A0279 for ; Mon, 19 Feb 2024 03:52:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1708311146; bh=m/ZaUkyTQqIC5AcY85qSXBKn1l9F9ew1r0aRQdL/EZ0=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=U21zWYBS0+gmdd2ArwzhBFXQFPI1ZYV8L3iuJ1iZXskN+3V48Jpnn32DyxPX0B3nw R7VV4miEMve0ft/Wsf7OIBg8pPH+YpRMRL0TzIDT11rqYlKMgfPpcdqFPRAwVEFOa2 Kxqn/oZvqCjX6R34IygK65RlRZpJofmu8BKDXszHgeIYtLakjzxjCZlUlVWN8yd6iz f2iawF56cJQCxFGB3/Ahy5QWs1g7A79Jz/m4/0UISOxNorpU7XuSwKC0jthcmgDGQd 3ocU8GqN95E+DF79z6Y+zvO6bSpfWQFCw+CW9moTJJSyOBjVn+ooyuwtHDlBrjBSvb fBMwV8lD2qZ/w== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TdRsQ0kP4z4wcJ; Mon, 19 Feb 2024 13:52:26 +1100 (AEDT) Date: Mon, 19 Feb 2024 13:50:49 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v3 2/9] pcap: add pcap_iov() Message-ID: References: <20240217150725.661467-1-lvivier@redhat.com> <20240217150725.661467-3-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ncoHirlWaIUdw89S" Content-Disposition: inline In-Reply-To: <20240217150725.661467-3-lvivier@redhat.com> Message-ID-Hash: QB7YJQSLNGILGOHKUA52JAMAFTVQKTVG X-Message-ID-Hash: QB7YJQSLNGILGOHKUA52JAMAFTVQKTVG X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --ncoHirlWaIUdw89S Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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. >=20 > Move packet header writing to to pcap_header() and use it in > pcap_frame() and pcap_iov(). >=20 > Signed-off-by: Laurent Vivier Reviewed-by: David Gibson > --- >=20 > Notes: > v3: > - update rational > - update comment > - use strerror(errno) > - use size_t for io vector length > =20 > v2: > - introduce pcap_header(), a common helper to write > packet header > - use writev() rather than write() in a loop > - add functions comment >=20 > pcap.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++------- > pcap.h | 1 + > 2 files changed, 56 insertions(+), 7 deletions(-) >=20 > diff --git a/pcap.c b/pcap.c > index 501d52d4992b..4e213eea8113 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" > =20 > #define PCAP_VERSION_MINOR 4 > =20 > @@ -65,6 +67,28 @@ struct pcap_pkthdr { > uint32_t len; > }; > =20 > +/* > + * 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 =3D tv->tv_sec; > + h.tv_usec =3D tv->tv_usec; > + h.caplen =3D h.len =3D len; > + > + if (write(pcap_fd, &h, sizeof(h)) < 0) > + return -1; > + > + return 0; > +} > + > /** > * pcap_frame() - Capture a single frame to pcap file with given timesta= mp > * @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 =3D tv->tv_sec; > - h.tv_usec =3D tv->tv_usec; > - h.caplen =3D h.len =3D 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; > =20 > return 0; > @@ -130,6 +148,36 @@ void pcap_multiple(const struct iovec *iov, unsigned= int n, size_t offset) > } > } > =20 > +/* > + * 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 scatt= er/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 =3D=3D -1) > + return; > + > + gettimeofday(&tv, NULL); > + > + len =3D 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 @@ > =20 > void pcap(const char *pkt, size_t len); > void pcap_multiple(const struct iovec *iov, unsigned int n, size_t offse= t); > +void pcap_iov(const struct iovec *iov, size_t n); > void pcap_init(struct ctx *c); > =20 > #endif /* PCAP_H */ --=20 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 --ncoHirlWaIUdw89S Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXSwgkACgkQzQJF27ox 2GeXJw/6AvuhgzfipooKG6kDflPmlCgpdWgdv6K6crK+jEiiqkguRhDFjLUKxFf2 8EfU5i1xXQKMk1GLwKbBopCGjTzQ8UXMShIe0Ml5Sy+G5j3znjmLFqvz4UYJLWGt 8k0k9N0lcR+upQjZQfrvbV3GxhdA4pyjinepqN66SEUayH+GWrDGcQOXC338immV kz1gyhvd695+b8HT0Xv+DONLM/WaiJoDbS9gqiYZq0cu2w46EM+gk+4nnwVew/zt JkM4cYOkXBUOvh/+oGuSx46Mi+qLcjhiJSxG9+AiOwXV3I34jE0nzPzZYxfosgw4 6bblLTALzajQFqXZHU/XqfXi3gpbDSND9/oE0PsoWTUhDzohsKJBTI9bsQ51YERV gGaqdbpofShfSKAMKU3Ac7J0wMUIEGNZK/98j9/Tyqwus6/Oq/bQxo7dTPH9cavj LXz3+u4sRSg3HKJw00D2ZhELn/5Dw9iw9jfsFefgMxxHr2DmrIA1Ak4zuJXriEgw U+gTJDr42tSIn7665tTl6MNiVl5uG5BtCw9fX6uhMwSupxpFjU+FGGG7XPjd5lsv B+Y8V489noxWjZBzfzC9hMzm4WTXKDyJTRD9zSSlF1yEnkpODcxrCmpqBjDiwGnv e74GXmYOvlenFZvDdZu1lthtOD/Vf1Xtvh4JJkk5rwnUTapKku0= =9CDd -----END PGP SIGNATURE----- --ncoHirlWaIUdw89S--