public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: passt-dev@passt.top
Subject: Re: [PATCH 06/12] udp: Split spliced forwarding path from udp_buf_reply_sock_data()
Date: Mon, 7 Apr 2025 23:49:24 +0200	[thread overview]
Message-ID: <20250407234924.6043b3ee@elisabeth> (raw)
In-Reply-To: <20250404101542.3729316-7-david@gibson.dropbear.id.au>

On Fri,  4 Apr 2025 21:15:36 +1100
David Gibson <david@gibson.dropbear.id.au> wrote:

> udp_buf_reply_sock_data() can handle forwarding data either from socket
> to socket ("splicing") or from socket to tap.  It has a test on each
> datagram for which case we're in, but that will be the same for everything
> in the batch.
> 
> Split out the spliced path into a separate udp_sock_to_sock() function.
> This leaves udp_{buf,vu}_reply_sock_data() handling only forwards from
> socket to tap, so rename and simplify them accordingly.
> 
> This makes the code slightly longer for now, but will allow future cleanups
> to shrink it back down again.
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  udp.c    | 103 ++++++++++++++++++++++++++++++-------------------------
>  udp_vu.c |  12 ++-----
>  udp_vu.h |   3 +-
>  3 files changed, 60 insertions(+), 58 deletions(-)
> 
> diff --git a/udp.c b/udp.c
> index d81f1213..144e625f 100644
> --- a/udp.c
> +++ b/udp.c
> @@ -671,6 +671,49 @@ static int udp_sock_recv(const struct ctx *c, int s, struct mmsghdr *mmh, int n)
>  	return n;
>  }
>  
> +/**
> + * udp_sock_to_sock() - Forward datagrams from socket to socket
> + * @c:		Execution context
> + * @from_s:	Socket to receive datagrams from
> + * @n:		Maximum number of datagrams to forward
> + * @tosidx:	Flow & side to forward datagrams too

"to", fixed on merge.

> + */
> +static void udp_sock_to_sock(const struct ctx *c, int from_s, int n,
> +			     flow_sidx_t tosidx)
> +{
> +	int i;
> +
> +	if ((n = udp_sock_recv(c, from_s, udp_mh_recv, n)) <= 0)
> +		return;
> +
> +	for (i = 0; i < n; i++)
> +		udp_splice_prepare(udp_mh_recv, i);
> +
> +	udp_splice_send(c, 0, n, tosidx);
> +}
> +
> +/**
> + * udp_buf_sock_to_tap() - Forward datagrams from socket to tap
> + * @c:		Execution context
> + * @s:		Socket to read data from
> + * @n:		Maximum number of datagrams to forward
> + * @tosidx:	Flow & side to forward data from @s to
> + */
> +static void udp_buf_sock_to_tap(const struct ctx *c, int s, int n,
> +				flow_sidx_t tosidx)
> +{
> +	const struct flowside *toside = flowside_at_sidx(tosidx);
> +	int i;
> +
> +	if ((n = udp_sock_recv(c, s, udp_mh_recv, n)) <= 0)
> +		return;
> +
> +	for (i = 0; i < n; i++)
> +		udp_tap_prepare(udp_mh_recv, i, toside, false);
> +
> +	tap_send_frames(c, &udp_l2_iov[0][0], UDP_NUM_IOVS, n);
> +}
> +
>  /**
>   * udp_buf_listen_sock_data() - Handle new data from socket
>   * @c:		Execution context
> @@ -738,43 +781,6 @@ void udp_listen_sock_handler(const struct ctx *c,
>  	}
>  }
>  
> -/**
> - * udp_buf_reply_sock_data() - Handle new data from flow specific socket
> - * @c:		Execution context
> - * @s:		Socket to read data from
> - * @n:		Maximum number of datagrams to forward
> - * @tosidx:	Flow & side to forward data from @s to
> - *
> - * Return: true on success, false if can't forward from socket to flow's pif
> - */
> -static bool udp_buf_reply_sock_data(const struct ctx *c, int s, int n,
> -				    flow_sidx_t tosidx)
> -{
> -	const struct flowside *toside = flowside_at_sidx(tosidx);
> -	uint8_t topif = pif_at_sidx(tosidx);
> -	int i;
> -
> -	if ((n = udp_sock_recv(c, s, udp_mh_recv, n)) <= 0)
> -		return true;
> -
> -	for (i = 0; i < n; i++) {
> -		if (pif_is_socket(topif))
> -			udp_splice_prepare(udp_mh_recv, i);
> -		else if (topif == PIF_TAP)
> -			udp_tap_prepare(udp_mh_recv, i, toside, false);
> -	}
> -
> -	if (pif_is_socket(topif)) {
> -		udp_splice_send(c, 0, n, tosidx);
> -	} else if (topif == PIF_TAP) {
> -		tap_send_frames(c, &udp_l2_iov[0][0], UDP_NUM_IOVS, n);
> -	} else {
> -		return false;
> -	}
> -
> -	return true;
> -}
> -
>  /**
>   * udp_sock_handler() - Handle new data from flow specific socket
>   * @c:		Execution context
> @@ -806,21 +812,26 @@ void udp_sock_handler(const struct ctx *c, union epoll_ref ref,
>  		 */
>  		size_t n = (c->mode == MODE_PASTA ? 1 : UDP_MAX_FRAMES);
>  		flow_sidx_t tosidx = flow_sidx_opposite(ref.flowside);
> +		uint8_t topif = pif_at_sidx(tosidx);
>  		int s = ref.fd;
> -		bool ret;
>  
>  		flow_trace(uflow, "Received data on reply socket");
>  		uflow->ts = now->tv_sec;
>  
> -		if (c->mode == MODE_VU) {
> -			ret = udp_vu_reply_sock_data(c, s, UDP_MAX_FRAMES,
> -						     tosidx);
> +		if (pif_is_socket(topif)) {
> +			udp_sock_to_sock(c, ref.fd, n, tosidx);
> +		} else if (topif == PIF_TAP) {
> +			if (c->mode == MODE_VU) {
> +				udp_vu_sock_to_tap(c, s, UDP_MAX_FRAMES,
> +						   tosidx);
> +			} else {
> +				udp_buf_sock_to_tap(c, s, n, tosidx);
> +			}
>  		} else {
> -			ret = udp_buf_reply_sock_data(c, s, n, tosidx);
> -		}
> -
> -		if (!ret) {
> -			flow_err(uflow, "Unable to forward UDP");
> +			flow_err(uflow,
> +				 "No support for forwarding UDP from %s to %s",
> +				 pif_name(pif_at_sidx(ref.flowside)),
> +				 pif_name(topif));
>  			goto fail;
>  		}
>  	}
> diff --git a/udp_vu.c b/udp_vu.c
> index b2618b39..fcccef65 100644
> --- a/udp_vu.c
> +++ b/udp_vu.c
> @@ -254,16 +254,13 @@ void udp_vu_listen_sock_data(const struct ctx *c, union epoll_ref ref,
>  }
>  
>  /**
> - * udp_vu_reply_sock_data() - Handle new data from flow specific socket
> + * udp_vu_sock_to_tap() - Forward datagrames from socket to tap

"datagrams", fixed on merge.

>   * @c:		Execution context
>   * @s:		Socket to read data from
>   * @n:		Maximum number of datagrams to forward
>   * @tosidx:	Flow & side to forward data from @s to
> - *
> - * Return: true on success, false if can't forward from socket to flow's pif
>   */
> -bool udp_vu_reply_sock_data(const struct ctx *c, int s, int n,
> -			    flow_sidx_t tosidx)
> +void udp_vu_sock_to_tap(const struct ctx *c, int s, int n, flow_sidx_t tosidx)
>  {
>  	const struct flowside *toside = flowside_at_sidx(tosidx);
>  	bool v6 = !(inany_v4(&toside->eaddr) && inany_v4(&toside->oaddr));
> @@ -271,9 +268,6 @@ bool udp_vu_reply_sock_data(const struct ctx *c, int s, int n,
>  	struct vu_virtq *vq = &vdev->vq[VHOST_USER_RX_QUEUE];
>  	int i;
>  
> -	if (pif_at_sidx(tosidx) != PIF_TAP)
> -		return false;
> -
>  	for (i = 0; i < n; i++) {
>  		ssize_t dlen;
>  		int iov_used;
> @@ -290,6 +284,4 @@ bool udp_vu_reply_sock_data(const struct ctx *c, int s, int n,
>  		}
>  		vu_flush(vdev, vq, elem, iov_used);
>  	}
> -
> -	return true;
>  }
> diff --git a/udp_vu.h b/udp_vu.h
> index c897c36f..576b0e71 100644
> --- a/udp_vu.h
> +++ b/udp_vu.h
> @@ -8,7 +8,6 @@
>  
>  void udp_vu_listen_sock_data(const struct ctx *c, union epoll_ref ref,
>  			     const struct timespec *now);
> -bool udp_vu_reply_sock_data(const struct ctx *c, int s, int n,
> -			    flow_sidx_t tosidx);
> +void udp_vu_sock_to_tap(const struct ctx *c, int s, int n, flow_sidx_t tosidx);
>  
>  #endif /* UDP_VU_H */

-- 
Stefano


  reply	other threads:[~2025-04-07 21:49 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-04 10:15 [PATCH 00/12] Use connect()ed sockets for both sides of UDP flows David Gibson
2025-04-04 10:15 ` [PATCH 01/12] udp: Use connect()ed sockets for initiating side David Gibson
2025-04-04 10:15 ` [PATCH 02/12] udp: Make udp_sock_recv() take max number of frames as a parameter David Gibson
2025-04-07 21:49   ` Stefano Brivio
2025-04-04 10:15 ` [PATCH 03/12] udp: Polish udp_vu_sock_info() and remove from vu specific code David Gibson
2025-04-07 21:49   ` Stefano Brivio
2025-04-04 10:15 ` [PATCH 04/12] udp: Don't bother to batch datagrams from "listening" socket David Gibson
2025-04-04 10:15 ` [PATCH 05/12] udp: Parameterize number of datagrams handled by udp_*_reply_sock_data() David Gibson
2025-04-04 10:15 ` [PATCH 06/12] udp: Split spliced forwarding path from udp_buf_reply_sock_data() David Gibson
2025-04-07 21:49   ` Stefano Brivio [this message]
2025-04-04 10:15 ` [PATCH 07/12] udp: Merge vhost-user and "buf" listening socket paths David Gibson
2025-04-04 10:15 ` [PATCH 08/12] udp: Move UDP_MAX_FRAMES to udp.c David Gibson
2025-04-04 10:15 ` [PATCH 09/12] udp_flow: Take pif and port as explicit parameters to udp_flow_from_sock() David Gibson
2025-04-07 21:49   ` Stefano Brivio
2025-04-04 10:15 ` [PATCH 10/12] udp: Rework udp_listen_sock_data() into udp_sock_fwd() David Gibson
2025-04-04 10:15 ` [PATCH 11/12] udp: Fold udp_splice_prepare and udp_splice_send into udp_sock_to_sock David Gibson
2025-04-07 21:49   ` Stefano Brivio
2025-04-04 10:15 ` [PATCH 12/12] udp_flow: Don't discard packets that arrive between bind() and connect() David Gibson
2025-04-07 21:49   ` Stefano Brivio
2025-04-07 21:49 ` [PATCH 00/12] Use connect()ed sockets for both sides of UDP flows Stefano Brivio
2025-04-07 23:50   ` 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=20250407234924.6043b3ee@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --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).