public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: Jon Maloy <jmaloy@redhat.com>
Cc: lvivier@redhat.com, dgibson@redhat.com, passt-dev@passt.top
Subject: Re: tcp.c: leverage MSG_PEEK with offset kernel capability when available
Date: Wed, 6 Dec 2023 18:06:15 +0100	[thread overview]
Message-ID: <20231206180541.3ac15056@elisabeth> (raw)
In-Reply-To: <3d6c3b2b-c16f-c262-36ea-05b28b93aeb8@redhat.com>

On Wed, 6 Dec 2023 11:10:50 -0500
Jon Maloy <jmaloy@redhat.com> wrote:

> On 2023-12-06 09:59, Stefano Brivio wrote:
> > On Tue,  5 Dec 2023 18:36:04 -0500
> > Jon Maloy <jmaloy@redhat.com> wrote:
> >  
> >> The kernel may support recvmsg(MSG_PEEK), starting from a given offset. This
> >> makes it possible to avoid repeated reading of already read initial bytes of
> >> a received message, hence saving us read cycles when forwarding TCP messages
> >> in the host->name space direction.
> >>
> >> When this feature is supported, iov_sock[0].iov_base can be set to NULL. The
> >> kernel code will interpret this as an instruction to skip reading of the first
> >> iov_sock[0].iov_len bytes of the message.
> >>
> >> Since iov_sock[0].iov_base is set to point to tcp_buf_discard, we do this
> >> by simply not allocating that buffer, letting the pointer remain NULL, when
> >> we find that we have this kernel support.
> >>
> >> There is no macro or function indicating kernel support for this feature. We
> >> therefore have to probe for it by reading from an established TCP connection.
> >> The traffic connection cannot be used for this purpose, because it will be
> >> broken if the probe reading fails. We therefore have to create a temporary
> >> local connection for this task. Because of this, we also add a new function,
> >> tcp_probe_msg_peek_offset_cap(), which creates this temporary connection
> >> and performs the probe read on it.
> >>
> >> Signed-off-by: Jon Maloy <jmaloy@redhat.com>
> >> ---
> >>   tcp.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
> >>   1 file changed, 108 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/tcp.c b/tcp.c
> >> index f506cfd..ab5168e 100644
> >> --- a/tcp.c
> >> +++ b/tcp.c
> >> @@ -402,6 +402,8 @@ struct tcp6_l2_head {	/* For MSS6 macro: keep in sync with tcp6_l2_buf_t */
> >>   	 (conn->events & (SOCK_FIN_RCVD | TAP_FIN_RCVD)))
> >>   #define CONN_HAS(conn, set)	((conn->events & (set)) == (set))
> >>   
> >> +static bool tcp_probe_msg_peek_offset_cap();  
> > No need for a forward declaration, I guess.
> >  
> >> +
> >>   static const char *tcp_event_str[] __attribute((__unused__)) = {
> >>   	"SOCK_ACCEPTED", "TAP_SYN_RCVD", "ESTABLISHED", "TAP_SYN_ACK_SENT",
> >>   
> >> @@ -506,7 +508,8 @@ static struct tcp_buf_seq_update tcp6_l2_buf_seq_update[TCP_FRAMES_MEM];
> >>   static unsigned int tcp6_l2_buf_used;
> >>   
> >>   /* recvmsg()/sendmsg() data for tap */
> >> -static char 		tcp_buf_discard		[MAX_WINDOW];
> >> +static char 		*tcp_buf_discard        = NULL;
> >> +
> >>   static struct iovec	iov_sock		[TCP_FRAMES_MEM + 1];
> >>   
> >>   static struct iovec	tcp4_l2_iov		[TCP_FRAMES_MEM];
> >> @@ -573,6 +576,15 @@ static unsigned int tcp6_l2_flags_buf_used;
> >>   
> >>   #define CONN(idx)		(&(FLOW(idx)->tcp))
> >>   
> >> +
> >> +/** msg_peek_offset_cap() - Does the kernel support recvmsg(MSG_PEEK) with offset?
> >> + */
> >> +static inline  bool msg_peek_offset_cap()
> >> +{
> >> +	return !tcp_buf_discard;
> >> +}
> >> +
> >> +
> >>   /** conn_at_idx() - Find a connection by index, if present
> >>    * @idx:	Index of connection to lookup
> >>    *
> >> @@ -2224,7 +2236,9 @@ static int tcp_data_from_sock(struct ctx *c, struct tcp_tap_conn *conn)
> >>   		return 0;
> >>   	}
> >>   
> >> -	sendlen = len - already_sent;
> >> +	sendlen = len;
> >> +	if (!msg_peek_offset_cap())
> >> +		sendlen -= already_sent;
> >>   	if (sendlen <= 0) {
> >>   		conn_flag(c, conn, STALLED);
> >>   		return 0;
> >> @@ -3107,6 +3121,15 @@ int tcp_init(struct ctx *c)
> >>   		NS_CALL(tcp_ns_socks_init, c);
> >>   	}
> >>   
> >> +	/* Only allocate discard buffer if needed */
> >> +	if (!tcp_probe_msg_peek_offset_cap()) {
> >> +		tcp_buf_discard = malloc(MAX_WINDOW);  
> > I would rather not use the heap at all, even though after commit
> > 0515adceaa8f ("passt, pasta: Namespace-based sandboxing, defer seccomp
> > policy application") we don't ask seccomp to terminate the process if we
> > call a sbrk() (or similar) in this phase.
> >
> > The only specific issue I have in mind is rather minor, that is, at the
> > moment we can reliably calculate our memory footprint using nm(1).
> >
> > But in general, having a single set of memory addresses keep things
> > simpler and probably a bit safer. This would be the only non-static
> > memory we use, and I don't see a strong case for it.  
> It is a rather large chunk of memory, but sure, if we could live with it 
> so far, we still can.

...but we wouldn't actually use that anymore -- as long as we never
write to it:

$ cat zero_static.c; gcc -o zero_static zero_static.c; echo; ./zero_static 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

static char zero[4 << 20];

int main()
{
	char cmd[100];

	snprintf(cmd, 100, "grep VmRSS /proc/%i/status", getpid());
	system(cmd);
	memset(zero, 0xff, sizeof(zero));
	system(cmd);
	return 0;
}

VmRSS:	    1388 kB
VmRSS:	    5484 kB

> > I would rather drop this buffer after a few months (in turn, if/after
> > the kernel change is accepted), turning the detection into a build-time
> > step, with passt failing if we find that we don't have this buffer, and
> > we were built for a kernel with support for MSG_PEEK with offset.  
> Sounds a litte risky to me. How do we know how are users are building it?

The idea is that packages are _typically_ built on a system that vaguely
resembles the one where they're going to be installed.

Based on that, we already have some conditionals in the Makefile based
on kernel headers -- those are strictly needed because we use those
headers to access features, so it's not ideal (we have no theoretical
guarantees, and doesn't cover users who don't use packages), but we
can't do much better.

Here, we could bluntly take the kernel version on build, and if it's
recent enough (again, leaving a reasonable timeframe in between), skip
the discard buffer. I don't see it as very risky in the sense that if we
don't have it, and we realise we need it, we can tell the user to
either enable a build conditional, or upgrade the kernel. On the other
hand:

> Maybe a year or two...

...yes, perhaps more reasonable, especially given that we're not using
that memory anymore.

-- 
Stefano


      reply	other threads:[~2023-12-06 17:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-05 23:36 tcp.c: leverage MSG_PEEK with offset kernel capability when available Jon Maloy
2023-12-06 14:59 ` Stefano Brivio
2023-12-06 15:08   ` Stefano Brivio
2023-12-06 16:10   ` Jon Maloy
2023-12-06 17:06     ` Stefano Brivio [this message]

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=20231206180541.3ac15056@elisabeth \
    --to=sbrivio@redhat.com \
    --cc=dgibson@redhat.com \
    --cc=jmaloy@redhat.com \
    --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).