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 DB0455A026F for ; Thu, 14 Mar 2024 03:11:42 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1710382298; bh=texazGAOCPp7zutyEwB/U79QRpXGykQ3mA5sJUdadCo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=JEgjngx7vR3iJD0csWDxjgAS9amn1nrAL6a3Vy5B1m5ZDZHDTK65BmvOP0xY1dAOJ 6+TpPT1BmrvN+5XHqHwGMnSdZRKPai1AKgUJkrOfGg/wuYBYVlTmDiLSwm/7AxRJ5y 38ALxyT5pa/dWmV+iwPYOYjwN+1utdObkBFNALE3QhiDP1K6bjq0rQMYIDcoSWK9t0 KCBcocLbJEG/GeHHZEiFB7xMttNr193FZyefjRBdBLs6iK4gz5YWfUWa7zkFVKkxEE Wp8gznaQoRQDI4CFmofbtS7Oe6EzMoZnnd6ac/wcH++L/Szi5gy82IA5wzSyBi4QK+ YOZ0QnIIdETPw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Tw9qG6BGvz4wc8; Thu, 14 Mar 2024 13:11:38 +1100 (AEDT) Date: Thu, 14 Mar 2024 13:11:27 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH] pcap: Use clock_gettime() instead of gettimeofday() Message-ID: References: <20240312190120.532298-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="zAHeeLzYRqPGnCGL" Content-Disposition: inline In-Reply-To: <20240312190120.532298-1-sbrivio@redhat.com> Message-ID-Hash: FKF77R5JHVNN2VIM2EQWLSRHIRJMXAZA X-Message-ID-Hash: FKF77R5JHVNN2VIM2EQWLSRHIRJMXAZA 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, Jon Maloy 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: --zAHeeLzYRqPGnCGL Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Mar 12, 2024 at 08:01:20PM +0100, Stefano Brivio wrote: > POSIX.1-2008 declared gettimeofday() as obsolete, but I'm a dinosaur. >=20 > Usually, C libraries translate that to the clock_gettime() system > call anyway, but this doesn't happen in Jon's environment, and, > there, seccomp happily kills pasta(1) when started with --pcap, > because we didn't add gettimeofday() to our seccomp profiles. >=20 > Use clock_gettime() instead. >=20 > Reported-by: Jon Maloy > Signed-off-by: Stefano Brivio Reviewed-by: David Gibson > --- > pcap.c | 26 +++++++++++++------------- > util.h | 1 + > 2 files changed, 14 insertions(+), 13 deletions(-) >=20 > diff --git a/pcap.c b/pcap.c > index a0f01ad..45bbfcd 100644 > --- a/pcap.c > +++ b/pcap.c > @@ -72,17 +72,17 @@ struct pcap_pkthdr { > * @iov: IO vector containing frame (with L2 headers and tap headers) > * @iovcnt: Number of buffers (@iov entries) in frame > * @offset: Byte offset of the L2 headers within @iov > - * @tv: Timestamp > + * @now: Timestamp > * > * Returns: 0 on success, -errno on error writing to the file > */ > static void pcap_frame(const struct iovec *iov, size_t iovcnt, > - size_t offset, const struct timeval *tv) > + size_t offset, const struct timespec *now) > { > size_t len =3D iov_size(iov, iovcnt) - offset; > struct pcap_pkthdr h =3D { > - .tv_sec =3D tv->tv_sec, > - .tv_usec =3D tv->tv_usec, > + .tv_sec =3D now->tv_sec, > + .tv_usec =3D DIV_ROUND_CLOSEST(now->tv_nsec, 1000), > .caplen =3D len, > .len =3D len > }; > @@ -103,13 +103,13 @@ static void pcap_frame(const struct iovec *iov, siz= e_t iovcnt, > void pcap(const char *pkt, size_t len) > { > struct iovec iov =3D { (char *)pkt, len }; > - struct timeval tv; > + struct timespec now; > =20 > if (pcap_fd =3D=3D -1) > return; > =20 > - gettimeofday(&tv, NULL); > - pcap_frame(&iov, 1, 0, &tv); > + clock_gettime(CLOCK_REALTIME, &now); > + pcap_frame(&iov, 1, 0, &now); > } > =20 > /** > @@ -122,16 +122,16 @@ void pcap(const char *pkt, size_t len) > void pcap_multiple(const struct iovec *iov, size_t frame_parts, unsigned= int n, > size_t offset) > { > - struct timeval tv; > + struct timespec now; > unsigned int i; > =20 > if (pcap_fd =3D=3D -1) > return; > =20 > - gettimeofday(&tv, NULL); > + clock_gettime(CLOCK_REALTIME, &now); > =20 > for (i =3D 0; i < n; i++) > - pcap_frame(iov + i * frame_parts, frame_parts, offset, &tv); > + pcap_frame(iov + i * frame_parts, frame_parts, offset, &now); > } > =20 > /* > @@ -145,13 +145,13 @@ void pcap_multiple(const struct iovec *iov, size_t = frame_parts, unsigned int n, > /* cppcheck-suppress unusedFunction */ > void pcap_iov(const struct iovec *iov, size_t iovcnt) > { > - struct timeval tv; > + struct timespec now; > =20 > if (pcap_fd =3D=3D -1) > return; > =20 > - gettimeofday(&tv, NULL); > - pcap_frame(iov, iovcnt, 0, &tv); > + clock_gettime(CLOCK_REALTIME, &now); > + pcap_frame(iov, iovcnt, 0, &now); > } > =20 > /** > diff --git a/util.h b/util.h > index 25e54a7..48f3560 100644 > --- a/util.h > +++ b/util.h > @@ -40,6 +40,7 @@ > #endif > =20 > #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) > +#define DIV_ROUND_CLOSEST(n, d) (((n) + (d) / 2) / (d)) > #define ROUND_DOWN(x, y) ((x) & ~((y) - 1)) > #define ROUND_UP(x, y) (((x) + (y) - 1) & ~((y) - 1)) > =20 --=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 --zAHeeLzYRqPGnCGL Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmXyXK8ACgkQzQJF27ox 2Gf/SA//dW2ShVDMAKAvGxgMFj8egP2+7vX9gSom6gL4AQvzPfqH4Pz84wHneBKv vrg1a9f2dhG0TsBuAOCtFlWDwXXFTj9qu3AoKifewm6Rzwz9Uv3nvXLIcAUXZR/f 3k86q9p0yTL1G8TPyhpk0oueEX9aCWBFHaxmsnZxV1UmF2TechTWra47Lx0WBbMZ mBMBy7tb93pxCpxwHMMqisLMQ9SD8KsVxi3wQt6OAe51hOC9IqAaEdqj7TXuNUwW rsE3INKois6FazUJJ9m2xDn09OCe5h3+z42mo3y4Dqgeswz/kxch3vUUorahV7gT GXdZCj3RMqT6annLmJoAWM6TwpDHtbrClwxjVHxIVOKkMJjS85idDdf7iUUepgzS tidBr1yw5QUtV4k0bNwVYQToJaFPdam9MwjcNJ7Y1oyPqE0iNUgOIqq3IfXW0Zm9 52/lWpe83s9w677czM8HjvZYXu/R/mx/9EqqpY3HVfjRX9+tPSBUOzVdERILjeoT lt55mN6RI0Ddw0YV+3mqwMrX4yJnHITNexNUJBwj8jbKvRPIivwQdREyEK32xeRF ArVLfpX6/CNOyl4exROYD60oe+nNlTlX93SFtk1pXR2Eyl/1mwVa1eSwu7m/B+n5 G7SFYv+kXHTjTsFUHlOZrRlm8JtIx/71Xq654fjfNqtdoGeAd+I= =DB0H -----END PGP SIGNATURE----- --zAHeeLzYRqPGnCGL--