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 9/9] port_fwd: Simplify get_bound_ports_*() to port_fwd_scan_*()
Date: Thu,  5 Oct 2023 14:44:45 +1100	[thread overview]
Message-ID: <20231005034445.2015303-10-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20231005034445.2015303-1-david@gibson.dropbear.id.au>

get_bound_ports_*() now only use their context and ns parameters to
determine which forwarding maps they're operating on.  Each function needs
the map they're actually updating, as well as the map for the other
direction, to avoid creating forwarding loops.  The UDP function also
requires the corresponding TCP map, to implement the behaviour where we
forward UDP ports of the same number as bound TCP ports for tools like
iperf3.

Passing those maps directly as parameters simplifies the code without
making the callers life harder, because those already know the relevant
maps.  IMO, invoking these functions in terms of where they're looking for
updated forwarding also makes more logical sense than in terms of where
they're looking for bound ports.  Given that new way of looking at the
functions, also rename them to port_fwd_scan_*().

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 port_fwd.c | 50 ++++++++++++++++----------------------------------
 port_fwd.h |  5 +++--
 tcp.c      |  4 ++--
 3 files changed, 21 insertions(+), 38 deletions(-)

diff --git a/port_fwd.c b/port_fwd.c
index 5308620..b6e256a 100644
--- a/port_fwd.c
+++ b/port_fwd.c
@@ -68,46 +68,26 @@ static void procfs_scan_listen(int fd, unsigned int lstate,
 }
 
 /**
- * get_bound_ports_tcp() - Get maps of TCP ports with bound sockets
- * @c:		Execution context
- * @ns:		If set, set bitmaps for ports to tap/ns -- to init otherwise
+ * port_fwd_scan_tcp() - Scan /proc to update TCP forwarding map
+ * @fwd:	Forwarding information to update
+ * @rev:	Forwarding information for the reverse direction
  */
-void get_bound_ports_tcp(struct ctx *c, int ns)
+void port_fwd_scan_tcp(struct port_fwd *fwd, const struct port_fwd *rev)
 {
-	struct port_fwd *fwd, *rev;
-
-	if (ns) {
-		fwd = &c->tcp.fwd_in;
-		rev = &c->tcp.fwd_out;
-	} else {
-		fwd = &c->tcp.fwd_out;
-		rev = &c->tcp.fwd_in;
-	}
-
 	memset(fwd->map, 0, PORT_BITMAP_SIZE);
 	procfs_scan_listen(fwd->scan4, TCP_LISTEN, fwd->map, rev->map);
 	procfs_scan_listen(fwd->scan6, TCP_LISTEN, fwd->map, rev->map);
 }
 
 /**
- * get_bound_ports_udp() - Get maps of UDP ports with bound sockets
- * @c:		Execution context
- * @ns:		If set, set bitmaps for ports to tap/ns -- to init otherwise
+ * port_fwd_scan_tcp() - Scan /proc to update TCP forwarding map
+ * @fwd:	Forwarding information to update
+ * @rev:	Forwarding information for the reverse direction
+ * @tcp:	Corresponding TCP forwarding information
  */
-void get_bound_ports_udp(struct ctx *c, int ns)
+void port_fwd_scan_udp(struct port_fwd *fwd, const struct port_fwd *rev,
+		       const struct port_fwd *tcp)
 {
-	struct port_fwd *fwd, *rev, *tcp;
-
-	if (ns) {
-		fwd = &c->udp.fwd_in.f;
-		rev = &c->udp.fwd_out.f;
-		tcp = &c->tcp.fwd_in;
-	} else {
-		fwd = &c->udp.fwd_out.f;
-		rev = &c->udp.fwd_in.f;
-		tcp = &c->tcp.fwd_out;
-	}
-
 	memset(fwd->map, 0, PORT_BITMAP_SIZE);
 	procfs_scan_listen(fwd->scan4, UDP_LISTEN, fwd->map, rev->map);
 	procfs_scan_listen(fwd->scan6, UDP_LISTEN, fwd->map, rev->map);
@@ -137,21 +117,23 @@ void port_fwd_init(struct ctx *c)
 	if (c->tcp.fwd_in.mode == FWD_AUTO) {
 		c->tcp.fwd_in.scan4 = open_in_ns(c, "/proc/net/tcp", flags);
 		c->tcp.fwd_in.scan6 = open_in_ns(c, "/proc/net/tcp6", flags);
-		get_bound_ports_tcp(c, 1);
+		port_fwd_scan_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
 	}
 	if (c->udp.fwd_in.f.mode == FWD_AUTO) {
 		c->udp.fwd_in.f.scan4 = open_in_ns(c, "/proc/net/udp", flags);
 		c->udp.fwd_in.f.scan6 = open_in_ns(c, "/proc/net/udp6", flags);
-		get_bound_ports_udp(c, 1);
+		port_fwd_scan_udp(&c->udp.fwd_in.f, &c->udp.fwd_out.f,
+				  &c->tcp.fwd_in);
 	}
 	if (c->tcp.fwd_out.mode == FWD_AUTO) {
 		c->tcp.fwd_out.scan4 = open("/proc/net/tcp", flags);
 		c->tcp.fwd_out.scan6 = open("/proc/net/tcp6", flags);
-		get_bound_ports_tcp(c, 0);
+		port_fwd_scan_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
 	}
 	if (c->udp.fwd_out.f.mode == FWD_AUTO) {
 		c->udp.fwd_out.f.scan4 = open("/proc/net/udp", flags);
 		c->udp.fwd_out.f.scan6 = open("/proc/net/udp6", flags);
-		get_bound_ports_udp(c, 0);
+		port_fwd_scan_udp(&c->udp.fwd_out.f, &c->udp.fwd_in.f,
+				  &c->tcp.fwd_out);
 	}
 }
diff --git a/port_fwd.h b/port_fwd.h
index 8ab6b48..8a823d8 100644
--- a/port_fwd.h
+++ b/port_fwd.h
@@ -35,8 +35,9 @@ struct port_fwd {
 	in_port_t delta[NUM_PORTS];
 };
 
-void get_bound_ports_tcp(struct ctx *c, int ns);
-void get_bound_ports_udp(struct ctx *c, int ns);
+void port_fwd_scan_tcp(struct port_fwd *fwd, const struct port_fwd *rev);
+void port_fwd_scan_udp(struct port_fwd *fwd, const struct port_fwd *rev,
+		       const struct port_fwd *tcp);
 void port_fwd_init(struct ctx *c);
 
 #endif /* PORT_FWD_H */
diff --git a/tcp.c b/tcp.c
index 921bd2a..1c3af76 100644
--- a/tcp.c
+++ b/tcp.c
@@ -3240,13 +3240,13 @@ void tcp_timer(struct ctx *c, const struct timespec *ts)
 		struct tcp_port_rebind_arg rebind_arg = { c, 0 };
 
 		if (c->tcp.fwd_out.mode == FWD_AUTO) {
-			get_bound_ports_tcp(c, 0);
+			port_fwd_scan_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
 			rebind_arg.bind_in_ns = 1;
 			NS_CALL(tcp_port_rebind, &rebind_arg);
 		}
 
 		if (c->tcp.fwd_in.mode == FWD_AUTO) {
-			get_bound_ports_tcp(c, 1);
+			port_fwd_scan_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
 			rebind_arg.bind_in_ns = 0;
 			tcp_port_rebind(&rebind_arg);
 		}
-- 
@@ -3240,13 +3240,13 @@ void tcp_timer(struct ctx *c, const struct timespec *ts)
 		struct tcp_port_rebind_arg rebind_arg = { c, 0 };
 
 		if (c->tcp.fwd_out.mode == FWD_AUTO) {
-			get_bound_ports_tcp(c, 0);
+			port_fwd_scan_tcp(&c->tcp.fwd_out, &c->tcp.fwd_in);
 			rebind_arg.bind_in_ns = 1;
 			NS_CALL(tcp_port_rebind, &rebind_arg);
 		}
 
 		if (c->tcp.fwd_in.mode == FWD_AUTO) {
-			get_bound_ports_tcp(c, 1);
+			port_fwd_scan_tcp(&c->tcp.fwd_in, &c->tcp.fwd_out);
 			rebind_arg.bind_in_ns = 0;
 			tcp_port_rebind(&rebind_arg);
 		}
-- 
2.41.0


      parent reply	other threads:[~2023-10-05  3:44 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-05  3:44 [PATCH 0/9] Clean ups to automatic port forwarding David Gibson
2023-10-05  3:44 ` [PATCH 1/9] conf: Cleaner initialisation of default forwarding modes David Gibson
2023-10-05  3:44 ` [PATCH 2/9] port_fwd: Move automatic port forwarding code to port_fwd.[ch] David Gibson
2023-10-05  3:44 ` [PATCH 3/9] port_fwd: Better parameterise procfs_scan_listen() David Gibson
2023-11-02 18:07   ` Stefano Brivio
2023-11-03  0:16     ` David Gibson
2023-10-05  3:44 ` [PATCH 4/9] util: Add open_in_ns() helper David Gibson
2023-11-02 18:07   ` Stefano Brivio
2023-11-03  0:20     ` David Gibson
2023-10-05  3:44 ` [PATCH 5/9] port_fwd: Pre-open /proc/net/* files rather than on-demand David Gibson
2023-10-05  3:44 ` [PATCH 6/9] port_fwd: Don't NS_CALL get_bound_ports() David Gibson
2023-10-05  3:44 ` [PATCH 7/9] port_fwd: Split TCP and UDP cases for get_bound_ports() David Gibson
2023-10-05  3:44 ` [PATCH 8/9] port_fwd: Move port scanning /proc fds into struct port_fwd David Gibson
2023-10-05  3:44 ` David Gibson [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=20231005034445.2015303-10-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).