From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id BC86F5A027B for ; Thu, 15 Feb 2024 01:40:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1707957620; bh=RTzr5Gb/si9elb3wEGCMDVB7unGgYRpBI7VWgT9v/1c=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ksOxguJZy8eDJK9lRcRmKIanJ+cr5muykXECUMxK/a1n+++vXyoCcoluFnub2FRnU 1qdRGd58L2WE1S6rLGvKTyivUXU5jLm9ZSqvrhAYvmyAK00lxn+VPkpBZRg6wOwah5 EDS1M7Wf9LTOvhYovuEtDW31rXGaecsi+HnVCLXdl103zllGDv+q4okMlaljejZpVy aGzhDY+m7jfduyCTUHpBGcuhM0R45zOBhIY+APUlCHXYnhlNOcSCl+2kp9hGNX3X9V 0vjdglQ84Btji7gcmBdWpqeIWjCQ5NDJLoPAtp41QCPzvRFajD5IuQzN8GX/LLKVvl NxI0dt5A63IXQ== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TZx6r4yCHz4x1H; Thu, 15 Feb 2024 11:40:20 +1100 (AEDT) Date: Thu, 15 Feb 2024 11:35:56 +1100 From: David Gibson To: Laurent Vivier Subject: Re: [PATCH v2 2/8] pcap: add pcap_iov() Message-ID: References: <20240214085628.210783-1-lvivier@redhat.com> <20240214085628.210783-3-lvivier@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="HTFXJjCWOus+V2hl" Content-Disposition: inline In-Reply-To: <20240214085628.210783-3-lvivier@redhat.com> Message-ID-Hash: HS3SX6WQQRG3OVZTMULXR4M4TVNOECNK X-Message-ID-Hash: HS3SX6WQQRG3OVZTMULXR4M4TVNOECNK 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: --HTFXJjCWOus+V2hl Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Feb 14, 2024 at 09:56:22AM +0100, Laurent Vivier wrote: Some kind of commit message, please, even if it's minimal. > Signed-off-by: Laurent Vivier > --- >=20 > Notes: > 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 | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++------- > pcap.h | 1 + > 2 files changed, 55 insertions(+), 7 deletions(-) >=20 > 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" > =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 the pcap file descriptor = (pcap_fd). > + * > + * @len: Length of the packet data. > + * @tv: Pointer to a timeval struct containing the timestamp for = the packet. Just "timestamp for packet" would suffice. > + * > + * 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 =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,35 @@ 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, unsigned int 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(), n =3D %u\n", n); I'm not convinced the length of the io vector is particularly useful here. strerror(errno) might be more useful, although the existing pcap() helpers also don't print that. > +} > + > /** > * 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 @@ > =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, unsigned int 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 --HTFXJjCWOus+V2hl Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXNXGsACgkQzQJF27ox 2GeB9RAAp0wl1zO/ONKXfOjprOLjO9OwipxbMVVVRdZMUTWXTKM8JOtwGQaNVnxl UP/HdMiyfP+Ag62Hcbtgbuc7YBk2rj1I+vk1QafhzrH5rP0Almrd9u/iHt960DOG To/WgERHragNRZY6eaTzdfnzU48AwUWQVYtfftLbc2DrHJLJe0iKy05yHHdYiEbZ ZwOdpIleSUKz/JkyKXyyrF7c50rRzQgtc5wGHjHsBla32LqG3c4aLA9+ckM5L+eC vnq61Ny/lrcXDSlpwCLiO4GqAoPyONfiDKcrPgpYTlgL7dgAvqI3CYhV8/B1EXUS XwEedyWoaXLN0vjGSoKRSkmEFgkuWRwR49nQ1UnQD6bsecdQVB+kDbZGBSvKZUdX vSSNkGq5+WbFouhRoH68yqYfujE/f3mDrOWGiwOElEciZ1WgIGMBOToGHDH3x/BT x+8PMzlJ5t48fgpKyOfis6a30TeeEoTS7VSBzAjIqQCrrf+T5w3BqJ364QVFXuvf lS93IAFeXtgQxjgTAKplEN3NASylZ8PdeebvURk09/dWxrBD6llMlT6IEj0hn3e7 YzAm+hyp6eyfIvksoMpgV6meIzHnDxwD+aSo910IbYtCIz0lesL4CSEvCZknBJB8 Tf+abSXRtn7FXdL4eCQCnkderyLYrmcqjv/zez/sXkzaG+E7Dfo= =XhkJ -----END PGP SIGNATURE----- --HTFXJjCWOus+V2hl--