public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] pcap: Use clock_gettime() instead of gettimeofday()
@ 2024-03-12 19:01 Stefano Brivio
  2024-03-14  2:11 ` David Gibson
  0 siblings, 1 reply; 2+ messages in thread
From: Stefano Brivio @ 2024-03-12 19:01 UTC (permalink / raw)
  To: passt-dev; +Cc: Jon Maloy

POSIX.1-2008 declared gettimeofday() as obsolete, but I'm a dinosaur.

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.

Use clock_gettime() instead.

Reported-by: Jon Maloy <jmaloy@redhat.com>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
 pcap.c | 26 +++++++++++++-------------
 util.h |  1 +
 2 files changed, 14 insertions(+), 13 deletions(-)

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 = iov_size(iov, iovcnt) - offset;
 	struct pcap_pkthdr h = {
-		.tv_sec = tv->tv_sec,
-		.tv_usec = tv->tv_usec,
+		.tv_sec = now->tv_sec,
+		.tv_usec = DIV_ROUND_CLOSEST(now->tv_nsec, 1000),
 		.caplen = len,
 		.len = len
 	};
@@ -103,13 +103,13 @@ static void pcap_frame(const struct iovec *iov, size_t iovcnt,
 void pcap(const char *pkt, size_t len)
 {
 	struct iovec iov = { (char *)pkt, len };
-	struct timeval tv;
+	struct timespec now;
 
 	if (pcap_fd == -1)
 		return;
 
-	gettimeofday(&tv, NULL);
-	pcap_frame(&iov, 1, 0, &tv);
+	clock_gettime(CLOCK_REALTIME, &now);
+	pcap_frame(&iov, 1, 0, &now);
 }
 
 /**
@@ -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;
 
 	if (pcap_fd == -1)
 		return;
 
-	gettimeofday(&tv, NULL);
+	clock_gettime(CLOCK_REALTIME, &now);
 
 	for (i = 0; i < n; i++)
-		pcap_frame(iov + i * frame_parts, frame_parts, offset, &tv);
+		pcap_frame(iov + i * frame_parts, frame_parts, offset, &now);
 }
 
 /*
@@ -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;
 
 	if (pcap_fd == -1)
 		return;
 
-	gettimeofday(&tv, NULL);
-	pcap_frame(iov, iovcnt, 0, &tv);
+	clock_gettime(CLOCK_REALTIME, &now);
+	pcap_frame(iov, iovcnt, 0, &now);
 }
 
 /**
diff --git a/util.h b/util.h
index 25e54a7..48f3560 100644
--- a/util.h
+++ b/util.h
@@ -40,6 +40,7 @@
 #endif
 
 #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))
 
-- 
@@ -40,6 +40,7 @@
 #endif
 
 #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))
 
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] pcap: Use clock_gettime() instead of gettimeofday()
  2024-03-12 19:01 [PATCH] pcap: Use clock_gettime() instead of gettimeofday() Stefano Brivio
@ 2024-03-14  2:11 ` David Gibson
  0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2024-03-14  2:11 UTC (permalink / raw)
  To: Stefano Brivio; +Cc: passt-dev, Jon Maloy

[-- Attachment #1: Type: text/plain, Size: 3680 bytes --]

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.
> 
> 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.
> 
> Use clock_gettime() instead.
> 
> Reported-by: Jon Maloy <jmaloy@redhat.com>
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  pcap.c | 26 +++++++++++++-------------
>  util.h |  1 +
>  2 files changed, 14 insertions(+), 13 deletions(-)
> 
> 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 = iov_size(iov, iovcnt) - offset;
>  	struct pcap_pkthdr h = {
> -		.tv_sec = tv->tv_sec,
> -		.tv_usec = tv->tv_usec,
> +		.tv_sec = now->tv_sec,
> +		.tv_usec = DIV_ROUND_CLOSEST(now->tv_nsec, 1000),
>  		.caplen = len,
>  		.len = len
>  	};
> @@ -103,13 +103,13 @@ static void pcap_frame(const struct iovec *iov, size_t iovcnt,
>  void pcap(const char *pkt, size_t len)
>  {
>  	struct iovec iov = { (char *)pkt, len };
> -	struct timeval tv;
> +	struct timespec now;
>  
>  	if (pcap_fd == -1)
>  		return;
>  
> -	gettimeofday(&tv, NULL);
> -	pcap_frame(&iov, 1, 0, &tv);
> +	clock_gettime(CLOCK_REALTIME, &now);
> +	pcap_frame(&iov, 1, 0, &now);
>  }
>  
>  /**
> @@ -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;
>  
>  	if (pcap_fd == -1)
>  		return;
>  
> -	gettimeofday(&tv, NULL);
> +	clock_gettime(CLOCK_REALTIME, &now);
>  
>  	for (i = 0; i < n; i++)
> -		pcap_frame(iov + i * frame_parts, frame_parts, offset, &tv);
> +		pcap_frame(iov + i * frame_parts, frame_parts, offset, &now);
>  }
>  
>  /*
> @@ -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;
>  
>  	if (pcap_fd == -1)
>  		return;
>  
> -	gettimeofday(&tv, NULL);
> -	pcap_frame(iov, iovcnt, 0, &tv);
> +	clock_gettime(CLOCK_REALTIME, &now);
> +	pcap_frame(iov, iovcnt, 0, &now);
>  }
>  
>  /**
> diff --git a/util.h b/util.h
> index 25e54a7..48f3560 100644
> --- a/util.h
> +++ b/util.h
> @@ -40,6 +40,7 @@
>  #endif
>  
>  #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))
>  

-- 
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 --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-14  2:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-12 19:01 [PATCH] pcap: Use clock_gettime() instead of gettimeofday() Stefano Brivio
2024-03-14  2:11 ` David Gibson

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).