public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Laurent Vivier <lvivier@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH v2 2/8] pcap: add pcap_iov()
Date: Thu, 15 Feb 2024 11:35:56 +1100	[thread overview]
Message-ID: <Zc1cbGj90EIxUBGz@zatzit> (raw)
In-Reply-To: <20240214085628.210783-3-lvivier@redhat.com>

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

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 <lvivier@redhat.com>
> ---
> 
> Notes:
>     v2:
>       - introduce pcap_header(), a common helper to write
>         packet header
>       - use writev() rather than write() in a loop
>       - add functions comment
> 
>  pcap.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
>  pcap.h |  1 +
>  2 files changed, 55 insertions(+), 7 deletions(-)
> 
> diff --git a/pcap.c b/pcap.c
> index 501d52d4992b..3869a403dd0f 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 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 = 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,35 @@ 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, unsigned int 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(), n = %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 @@
>  
>  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, unsigned int 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 --]

  reply	other threads:[~2024-02-15  0:40 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-14  8:56 [PATCH v2 0/8] Add vhost-user support to passt (part 1) Laurent Vivier
2024-02-14  8:56 ` [PATCH v2 1/8] iov: add some functions to manage iovec Laurent Vivier
2024-02-15  0:24   ` David Gibson
2024-02-15  0:32     ` David Gibson
2024-02-16  5:29     ` Stefano Brivio
2024-02-14  8:56 ` [PATCH v2 2/8] pcap: add pcap_iov() Laurent Vivier
2024-02-15  0:35   ` David Gibson [this message]
2024-02-16  5:30   ` Stefano Brivio
2024-02-14  8:56 ` [PATCH v2 3/8] checksum: align buffers Laurent Vivier
2024-02-15  0:40   ` David Gibson
2024-02-14  8:56 ` [PATCH v2 4/8] checksum: add csum_iov() Laurent Vivier
2024-02-15  0:44   ` David Gibson
2024-02-14  8:56 ` [PATCH v2 5/8] util: move IP stuff from util.[ch] to ip.[ch] Laurent Vivier
2024-02-15  2:29   ` David Gibson
2024-02-14  8:56 ` [PATCH v2 6/8] checksum: use csum_ip4_header() in udp.c and tcp.c Laurent Vivier
2024-02-15  2:51   ` David Gibson
2024-02-16  9:08   ` Stefano Brivio
2024-02-16 14:17     ` Laurent Vivier
2024-02-16 14:54       ` Stefano Brivio
2024-02-16 18:05         ` Laurent Vivier
2024-02-16 18:24           ` Stefano Brivio
2024-02-17 14:22             ` Laurent Vivier
2024-02-17 14:37               ` Stefano Brivio
2024-02-19  2:06               ` David Gibson
2024-02-14  8:56 ` [PATCH v2 7/8] checksum: introduce functions to compute the header part checksum for TCP/UDP Laurent Vivier
2024-02-15  3:12   ` David Gibson
2024-02-14  8:56 ` [PATCH v2 8/8] tap: make tap_update_mac() generic Laurent Vivier
2024-02-15  3:13   ` David Gibson

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=Zc1cbGj90EIxUBGz@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).