public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 1/2] tcp: Generalise probing for tcpi_snd_wnd field
Date: Wed, 23 Oct 2024 11:42:52 +1100	[thread overview]
Message-ID: <20241023004253.1729124-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20241023004253.1729124-1-david@gibson.dropbear.id.au>

In order to use the tcpi_snd_wnd field from the TCPI_INFO getsockopt() we
need both for the field to be present in the headers (HAS_SND_WND) and for
the runtime kernel to actually report it (snd_wnd_cap).

In fact we really need to check the same things for every tcp_info field
we want to use.  Prepare to do that, by generalising the probing for the
tcpi_snd_wnd field which set a single bool to instead record the size of
the returned tcp_info structure.  We can then use that recorded value to
check for the presence of any field we need.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 tcp.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/tcp.c b/tcp.c
index 0d22e07..3cca5c6 100644
--- a/tcp.c
+++ b/tcp.c
@@ -365,11 +365,18 @@ char		tcp_buf_discard		[MAX_WINDOW];
 
 /* Does the kernel support TCP_PEEK_OFF? */
 bool peek_offset_cap;
+/* Size of data returned by TCP_INFO getsockopt() */
+socklen_t tcp_info_size;
+
+#define tcp_info_cap(f_)						\
+	((offsetof(struct tcp_info, tcpi_##f_) +			\
+	  sizeof(((struct tcp_info *)NULL)->tcpi_##f_)) <= tcp_info_size)
+
 #ifdef HAS_SND_WND
 /* Does the kernel report sending window in TCP_INFO (kernel commit
  * 8f7baad7f035)
  */
-bool snd_wnd_cap;
+#define snd_wnd_cap	tcp_info_cap(snd_wnd)
 #else
 #define snd_wnd_cap	(false)
 #endif
@@ -2578,13 +2585,12 @@ static bool tcp_probe_peek_offset_cap(sa_family_t af)
 	return ret;
 }
 
-#ifdef HAS_SND_WND
 /**
- * tcp_probe_snd_wnd_cap() - Check if TCP_INFO reports tcpi_snd_wnd
+ * tcp_probe_tcp_info() - Check what data TCP_INFO reports
  *
- * Return: true if supported, false otherwise
+ * Return: Number of bytes returned by TCP_INFO getsockopt()
  */
-static bool tcp_probe_snd_wnd_cap(void)
+static socklen_t tcp_probe_tcp_info(void)
 {
 	struct tcp_info tinfo;
 	socklen_t sl = sizeof(tinfo);
@@ -2604,13 +2610,8 @@ static bool tcp_probe_snd_wnd_cap(void)
 
 	close(s);
 
-	if (sl < (offsetof(struct tcp_info, tcpi_snd_wnd) +
-		  sizeof(tinfo.tcpi_snd_wnd)))
-		return false;
-
-	return true;
+	return sl;
 }
-#endif /* HAS_SND_WND */
 
 /**
  * tcp_init() - Get initial sequence, hash secret, initialise per-socket data
@@ -2645,11 +2646,14 @@ int tcp_init(struct ctx *c)
 			  (!c->ifi6 || tcp_probe_peek_offset_cap(AF_INET6));
 	debug("SO_PEEK_OFF%ssupported", peek_offset_cap ? " " : " not ");
 
+	tcp_info_size = tcp_probe_tcp_info();
+
+#define dbg_tcpi(f_)	debug("TCP_INFO tcpi_%s field%s supported",	\
+			      STRINGIFY(f_), tcp_info_cap(f_) ? " " : " not ")
 #ifdef HAS_SND_WND
-	snd_wnd_cap = tcp_probe_snd_wnd_cap();
+	dbg_tcpi(snd_wnd);
 #endif
-	debug("TCP_INFO tcpi_snd_wnd field%ssupported",
-	      snd_wnd_cap ? " " : " not ");
+#undef dbg_tcpi
 
 	return 0;
 }
-- 
@@ -365,11 +365,18 @@ char		tcp_buf_discard		[MAX_WINDOW];
 
 /* Does the kernel support TCP_PEEK_OFF? */
 bool peek_offset_cap;
+/* Size of data returned by TCP_INFO getsockopt() */
+socklen_t tcp_info_size;
+
+#define tcp_info_cap(f_)						\
+	((offsetof(struct tcp_info, tcpi_##f_) +			\
+	  sizeof(((struct tcp_info *)NULL)->tcpi_##f_)) <= tcp_info_size)
+
 #ifdef HAS_SND_WND
 /* Does the kernel report sending window in TCP_INFO (kernel commit
  * 8f7baad7f035)
  */
-bool snd_wnd_cap;
+#define snd_wnd_cap	tcp_info_cap(snd_wnd)
 #else
 #define snd_wnd_cap	(false)
 #endif
@@ -2578,13 +2585,12 @@ static bool tcp_probe_peek_offset_cap(sa_family_t af)
 	return ret;
 }
 
-#ifdef HAS_SND_WND
 /**
- * tcp_probe_snd_wnd_cap() - Check if TCP_INFO reports tcpi_snd_wnd
+ * tcp_probe_tcp_info() - Check what data TCP_INFO reports
  *
- * Return: true if supported, false otherwise
+ * Return: Number of bytes returned by TCP_INFO getsockopt()
  */
-static bool tcp_probe_snd_wnd_cap(void)
+static socklen_t tcp_probe_tcp_info(void)
 {
 	struct tcp_info tinfo;
 	socklen_t sl = sizeof(tinfo);
@@ -2604,13 +2610,8 @@ static bool tcp_probe_snd_wnd_cap(void)
 
 	close(s);
 
-	if (sl < (offsetof(struct tcp_info, tcpi_snd_wnd) +
-		  sizeof(tinfo.tcpi_snd_wnd)))
-		return false;
-
-	return true;
+	return sl;
 }
-#endif /* HAS_SND_WND */
 
 /**
  * tcp_init() - Get initial sequence, hash secret, initialise per-socket data
@@ -2645,11 +2646,14 @@ int tcp_init(struct ctx *c)
 			  (!c->ifi6 || tcp_probe_peek_offset_cap(AF_INET6));
 	debug("SO_PEEK_OFF%ssupported", peek_offset_cap ? " " : " not ");
 
+	tcp_info_size = tcp_probe_tcp_info();
+
+#define dbg_tcpi(f_)	debug("TCP_INFO tcpi_%s field%s supported",	\
+			      STRINGIFY(f_), tcp_info_cap(f_) ? " " : " not ")
 #ifdef HAS_SND_WND
-	snd_wnd_cap = tcp_probe_snd_wnd_cap();
+	dbg_tcpi(snd_wnd);
 #endif
-	debug("TCP_INFO tcpi_snd_wnd field%ssupported",
-	      snd_wnd_cap ? " " : " not ");
+#undef dbg_tcpi
 
 	return 0;
 }
-- 
2.47.0


  reply	other threads:[~2024-10-23  0:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-23  0:42 [PATCH 0/2] tcp: Runtime checks for availability of TCP_INFO fields David Gibson
2024-10-23  0:42 ` David Gibson [this message]
2024-10-23  0:42 ` [PATCH 2/2] tcp: Use runtime tests for " David Gibson
2024-10-23  7:41   ` Stefano Brivio
2024-10-24  2:02     ` 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=20241023004253.1729124-2-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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).