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 8/9] port_fwd: Move port scanning /proc fds into struct port_fwd
Date: Thu,  5 Oct 2023 14:44:44 +1100	[thread overview]
Message-ID: <20231005034445.2015303-9-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20231005034445.2015303-1-david@gibson.dropbear.id.au>

Currently we store /proc/net fds used to implement automatic port
forwarding in the proc_net_{tcp,udp} fields of the main context structure.
However, in fact each of those is associated with a particular direction
of forwarding, and we already have struct port_fwd which collects all
other information related to a particular direction of port forwarding.

We can simplify things a bit by moving the /proc fds into struct port_fwd.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 passt.h    |  5 -----
 port_fwd.c | 62 ++++++++++++++++++++++++++++--------------------------
 port_fwd.h |  4 ++++
 3 files changed, 36 insertions(+), 35 deletions(-)

diff --git a/passt.h b/passt.h
index 282bd1a..53defa4 100644
--- a/passt.h
+++ b/passt.h
@@ -203,8 +203,6 @@ struct ip6_ctx {
  * @no_netns_quit:	In pasta mode, don't exit if fs-bound namespace is gone
  * @netns_base:		Base name for fs-bound namespace, if any, in pasta mode
  * @netns_dir:		Directory of fs-bound namespace, if any, in pasta mode
- * @proc_net_tcp:	Stored handles for /proc/net/tcp{,6} in init and ns
- * @proc_net_udp:	Stored handles for /proc/net/udp{,6} in init and ns
  * @epollfd:		File descriptor for epoll instance
  * @fd_tap_listen:	File descriptor for listening AF_UNIX socket, if any
  * @fd_tap:		AF_UNIX socket, tuntap device, or pre-opened socket
@@ -258,9 +256,6 @@ struct ctx {
 	char netns_base[PATH_MAX];
 	char netns_dir[PATH_MAX];
 
-	int proc_net_tcp[IP_VERSIONS][2];
-	int proc_net_udp[IP_VERSIONS][2];
-
 	int epollfd;
 	int fd_tap_listen;
 	int fd_tap;
diff --git a/port_fwd.c b/port_fwd.c
index e6b3b14..5308620 100644
--- a/port_fwd.c
+++ b/port_fwd.c
@@ -74,19 +74,19 @@ static void procfs_scan_listen(int fd, unsigned int lstate,
  */
 void get_bound_ports_tcp(struct ctx *c, int ns)
 {
-	uint8_t *map, *excl;
+	struct port_fwd *fwd, *rev;
 
 	if (ns) {
-		map = c->tcp.fwd_in.map;
-		excl = c->tcp.fwd_out.map;
+		fwd = &c->tcp.fwd_in;
+		rev = &c->tcp.fwd_out;
 	} else {
-		map = c->tcp.fwd_out.map;
-		excl = c->tcp.fwd_in.map;
+		fwd = &c->tcp.fwd_out;
+		rev = &c->tcp.fwd_in;
 	}
 
-	memset(map, 0, PORT_BITMAP_SIZE);
-	procfs_scan_listen(c->proc_net_tcp[V4][ns], TCP_LISTEN, map, excl);
-	procfs_scan_listen(c->proc_net_tcp[V6][ns], TCP_LISTEN, map, excl);
+	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);
 }
 
 /**
@@ -96,27 +96,29 @@ void get_bound_ports_tcp(struct ctx *c, int ns)
  */
 void get_bound_ports_udp(struct ctx *c, int ns)
 {
-	uint8_t *map, *excl;
+	struct port_fwd *fwd, *rev, *tcp;
 
 	if (ns) {
-		map = c->udp.fwd_in.f.map;
-		excl = c->udp.fwd_out.f.map;
+		fwd = &c->udp.fwd_in.f;
+		rev = &c->udp.fwd_out.f;
+		tcp = &c->tcp.fwd_in;
 	} else {
-		map = c->udp.fwd_out.f.map;
-		excl = c->udp.fwd_in.f.map;
+		fwd = &c->udp.fwd_out.f;
+		rev = &c->udp.fwd_in.f;
+		tcp = &c->tcp.fwd_out;
 	}
 
-	memset(map, 0, PORT_BITMAP_SIZE);
-	procfs_scan_listen(c->proc_net_udp[V4][ns], UDP_LISTEN, map, excl);
-	procfs_scan_listen(c->proc_net_udp[V6][ns], UDP_LISTEN, map, excl);
+	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);
 
 	/* Also forward UDP ports with the same numbers as bound TCP ports.
 	 * This is useful for a handful of protocols (e.g. iperf3) where a TCP
 	 * control port is used to set up transfers on a corresponding UDP
 	 * port.
 	 */
-	procfs_scan_listen(c->proc_net_tcp[V4][ns], TCP_LISTEN, map, excl);
-	procfs_scan_listen(c->proc_net_tcp[V6][ns], TCP_LISTEN, map, excl);
+	procfs_scan_listen(tcp->scan4, TCP_LISTEN, fwd->map, rev->map);
+	procfs_scan_listen(tcp->scan6, TCP_LISTEN, fwd->map, rev->map);
 }
 
 /**
@@ -127,29 +129,29 @@ void port_fwd_init(struct ctx *c)
 {
 	const int flags = O_RDONLY | O_CLOEXEC;
 
-	c->proc_net_tcp[V4][0] = c->proc_net_tcp[V4][1] = -1;
-	c->proc_net_tcp[V6][0] = c->proc_net_tcp[V6][1] = -1;
-	c->proc_net_udp[V4][0] = c->proc_net_udp[V4][1] = -1;
-	c->proc_net_udp[V6][0] = c->proc_net_udp[V6][1] = -1;
+	c->tcp.fwd_in.scan4 = c->tcp.fwd_in.scan6 = -1;
+	c->tcp.fwd_out.scan4 = c->tcp.fwd_out.scan6 = -1;
+	c->udp.fwd_in.f.scan4 = c->udp.fwd_in.f.scan6 = -1;
+	c->udp.fwd_out.f.scan4 = c->udp.fwd_out.f.scan6 = -1;
 
 	if (c->tcp.fwd_in.mode == FWD_AUTO) {
-		c->proc_net_tcp[V4][1] = open_in_ns(c, "/proc/net/tcp", flags);
-		c->proc_net_tcp[V6][1] = open_in_ns(c, "/proc/net/tcp6", flags);
+		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);
 	}
 	if (c->udp.fwd_in.f.mode == FWD_AUTO) {
-		c->proc_net_udp[V4][1] = open_in_ns(c, "/proc/net/udp", flags);
-		c->proc_net_udp[V6][1] = open_in_ns(c, "/proc/net/udp6", flags);
+		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);
 	}
 	if (c->tcp.fwd_out.mode == FWD_AUTO) {
-		c->proc_net_tcp[V4][0] = open("/proc/net/tcp", flags);
-		c->proc_net_tcp[V6][0] = open("/proc/net/tcp6", flags);
+		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);
 	}
 	if (c->udp.fwd_out.f.mode == FWD_AUTO) {
-		c->proc_net_udp[V4][0] = open("/proc/net/udp", flags);
-		c->proc_net_udp[V6][0] = open("/proc/net/udp6", flags);
+		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);
 	}
 }
diff --git a/port_fwd.h b/port_fwd.h
index 2f8f526..8ab6b48 100644
--- a/port_fwd.h
+++ b/port_fwd.h
@@ -22,11 +22,15 @@ enum port_fwd_mode {
 /**
  * port_fwd - Describes port forwarding for one protocol and direction
  * @mode:	Overall forwarding mode (all, none, auto, specific ports)
+ * @scan4:	/proc/net fd to scan for IPv4 ports when in AUTO mode
+ * @scan6:	/proc/net fd to scan for IPv6 ports when in AUTO mode
  * @map:	Bitmap describing which ports are forwarded
  * @delta:	Offset between the original destination and mapped port number
  */
 struct port_fwd {
 	enum port_fwd_mode mode;
+	int scan4;
+	int scan6;
 	uint8_t map[PORT_BITMAP_SIZE];
 	in_port_t delta[NUM_PORTS];
 };
-- 
@@ -22,11 +22,15 @@ enum port_fwd_mode {
 /**
  * port_fwd - Describes port forwarding for one protocol and direction
  * @mode:	Overall forwarding mode (all, none, auto, specific ports)
+ * @scan4:	/proc/net fd to scan for IPv4 ports when in AUTO mode
+ * @scan6:	/proc/net fd to scan for IPv6 ports when in AUTO mode
  * @map:	Bitmap describing which ports are forwarded
  * @delta:	Offset between the original destination and mapped port number
  */
 struct port_fwd {
 	enum port_fwd_mode mode;
+	int scan4;
+	int scan6;
 	uint8_t map[PORT_BITMAP_SIZE];
 	in_port_t delta[NUM_PORTS];
 };
-- 
2.41.0


  parent reply	other threads:[~2023-10-05  3:45 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 ` David Gibson [this message]
2023-10-05  3:44 ` [PATCH 9/9] port_fwd: Simplify get_bound_ports_*() to port_fwd_scan_*() 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=20231005034445.2015303-9-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).