public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Subject: [PATCH 10/24] udp: Use flags for local, loopback, and configured unicast binds
Date: Fri, 25 Mar 2022 23:52:46 +0100	[thread overview]
Message-ID: <20220325225300.2803584-11-sbrivio@redhat.com> (raw)
In-Reply-To: <20220325225300.2803584-1-sbrivio@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 4485 bytes --]

There's no value in keeping a separate timestamp for activity and for
aging of local binds, given that they have the same timeout. Reduce
that to a single timestamp, with a flag indicating the local bind.

Also use flags instead of separate int fields for loopback and
configured unicast address usage as source address.

Signed-off-by: Stefano Brivio <sbrivio(a)redhat.com>
---
 udp.c | 48 +++++++++++++++++++++++-------------------------
 1 file changed, 23 insertions(+), 25 deletions(-)

diff --git a/udp.c b/udp.c
index ebbcda1..ad8a775 100644
--- a/udp.c
+++ b/udp.c
@@ -125,16 +125,16 @@
  * struct udp_tap_port - Port tracking based on tap-facing source port
  * @sock:	Socket bound to source port used as index
  * @ts:		Activity timestamp from tap, used for socket aging
- * @ts_local:	Timestamp of tap packet to gateway address, aging for local bind
- * @loopback:	Whether local bind maps to loopback address as source
- * @gua:	Whether local bind maps to configured unicast address as source
+ * @flags:	Flags for local bind, loopback address/unicast address as source
  */
 struct udp_tap_port {
 	int sock;
 	time_t ts;
-	time_t ts_local;
-	int loopback;
-	int gua;
+
+	uint8_t flags;
+#define PORT_LOCAL	BIT(0)
+#define PORT_LOOPBACK	BIT(1)
+#define PORT_GUA	BIT(2)
 };
 
 /**
@@ -684,12 +684,13 @@ static void udp_sock_fill_data_v4(struct ctx *c, int n, union epoll_ref ref,
 	if (src >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET ||
 	    src == INADDR_ANY || src == ntohl(c->addr4_seen)) {
 		b->iph.saddr = c->gw4;
-		udp_tap_map[V4][src_port].ts_local = now->tv_sec;
+		udp_tap_map[V4][src_port].ts = now->tv_sec;
+		udp_tap_map[V4][src_port].flags |= PORT_LOCAL;
 
 		if (b->s_in.sin_addr.s_addr == c->addr4_seen)
-			udp_tap_map[V4][src_port].loopback = 0;
+			udp_tap_map[V4][src_port].flags &= ~PORT_LOOPBACK;
 		else
-			udp_tap_map[V4][src_port].loopback = 1;
+			udp_tap_map[V4][src_port].flags |= PORT_LOOPBACK;
 
 		bitmap_set(udp_act[V4][UDP_ACT_TAP], src_port);
 	} else if (c->dns4_fwd &&
@@ -768,17 +769,18 @@ static void udp_sock_fill_data_v6(struct ctx *c, int n, union epoll_ref ref,
 		else
 			b->ip6h.saddr = c->addr6_ll;
 
-		udp_tap_map[V6][src_port].ts_local = now->tv_sec;
+		udp_tap_map[V6][src_port].ts = now->tv_sec;
+		udp_tap_map[V6][src_port].flags |= PORT_LOCAL;
 
 		if (IN6_IS_ADDR_LOOPBACK(src))
-			udp_tap_map[V6][src_port].loopback = 1;
+			udp_tap_map[V6][src_port].flags |= PORT_LOOPBACK;
 		else
-			udp_tap_map[V6][src_port].loopback = 0;
+			udp_tap_map[V6][src_port].flags &= ~PORT_LOOPBACK;
 
 		if (IN6_ARE_ADDR_EQUAL(src, &c->addr6))
-			udp_tap_map[V6][src_port].gua = 1;
+			udp_tap_map[V6][src_port].flags |= PORT_GUA;
 		else
-			udp_tap_map[V6][src_port].gua = 0;
+			udp_tap_map[V6][src_port].flags &= ~PORT_GUA;
 
 		bitmap_set(udp_act[V6][UDP_ACT_TAP], src_port);
 	} else if (!IN6_IS_ADDR_UNSPECIFIED(&c->dns6_fwd) &&
@@ -999,8 +1001,8 @@ int udp_tap_handler(struct ctx *c, int af, void *addr,
 		udp_tap_map[V4][src].ts = now->tv_sec;
 
 		if (s_in.sin_addr.s_addr == c->gw4 && !c->no_map_gw) {
-			if (!udp_tap_map[V4][dst].ts_local ||
-			    udp_tap_map[V4][dst].loopback)
+			if (!(udp_tap_map[V4][dst].flags & PORT_LOCAL) ||
+			    (udp_tap_map[V4][dst].flags & PORT_LOOPBACK))
 				s_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 			else
 				s_in.sin_addr.s_addr = c->addr4_seen;
@@ -1020,10 +1022,10 @@ int udp_tap_handler(struct ctx *c, int af, void *addr,
 		sl = sizeof(s_in6);
 
 		if (IN6_ARE_ADDR_EQUAL(addr, &c->gw6) && !c->no_map_gw) {
-			if (!udp_tap_map[V6][dst].ts_local ||
-			    udp_tap_map[V6][dst].loopback)
+			if (!(udp_tap_map[V6][dst].flags & PORT_LOCAL) ||
+			    (udp_tap_map[V6][dst].flags & PORT_LOOPBACK))
 				s_in6.sin6_addr = in6addr_loopback;
-			else if (udp_tap_map[V6][dst].gua)
+			else if (udp_tap_map[V6][dst].flags & PORT_GUA)
 				s_in6.sin6_addr = c->addr6;
 			else
 				s_in6.sin6_addr = c->addr6_seen;
@@ -1241,13 +1243,9 @@ static void udp_timer_one(struct ctx *c, int v6, enum udp_act_type type,
 	case UDP_ACT_TAP:
 		tp = &udp_tap_map[v6 ? V6 : V4][port];
 
-		if (ts->tv_sec - tp->ts > UDP_CONN_TIMEOUT)
+		if (ts->tv_sec - tp->ts > UDP_CONN_TIMEOUT) {
 			s = tp->sock;
-
-		if (ts->tv_sec - tp->ts_local > UDP_CONN_TIMEOUT) {
-			tp->ts_local = 0;
-			tp->loopback = 0;
-			tp->gua = 0;
+			tp->flags = 0;
 		}
 
 		break;
-- 
@@ -125,16 +125,16 @@
  * struct udp_tap_port - Port tracking based on tap-facing source port
  * @sock:	Socket bound to source port used as index
  * @ts:		Activity timestamp from tap, used for socket aging
- * @ts_local:	Timestamp of tap packet to gateway address, aging for local bind
- * @loopback:	Whether local bind maps to loopback address as source
- * @gua:	Whether local bind maps to configured unicast address as source
+ * @flags:	Flags for local bind, loopback address/unicast address as source
  */
 struct udp_tap_port {
 	int sock;
 	time_t ts;
-	time_t ts_local;
-	int loopback;
-	int gua;
+
+	uint8_t flags;
+#define PORT_LOCAL	BIT(0)
+#define PORT_LOOPBACK	BIT(1)
+#define PORT_GUA	BIT(2)
 };
 
 /**
@@ -684,12 +684,13 @@ static void udp_sock_fill_data_v4(struct ctx *c, int n, union epoll_ref ref,
 	if (src >> IN_CLASSA_NSHIFT == IN_LOOPBACKNET ||
 	    src == INADDR_ANY || src == ntohl(c->addr4_seen)) {
 		b->iph.saddr = c->gw4;
-		udp_tap_map[V4][src_port].ts_local = now->tv_sec;
+		udp_tap_map[V4][src_port].ts = now->tv_sec;
+		udp_tap_map[V4][src_port].flags |= PORT_LOCAL;
 
 		if (b->s_in.sin_addr.s_addr == c->addr4_seen)
-			udp_tap_map[V4][src_port].loopback = 0;
+			udp_tap_map[V4][src_port].flags &= ~PORT_LOOPBACK;
 		else
-			udp_tap_map[V4][src_port].loopback = 1;
+			udp_tap_map[V4][src_port].flags |= PORT_LOOPBACK;
 
 		bitmap_set(udp_act[V4][UDP_ACT_TAP], src_port);
 	} else if (c->dns4_fwd &&
@@ -768,17 +769,18 @@ static void udp_sock_fill_data_v6(struct ctx *c, int n, union epoll_ref ref,
 		else
 			b->ip6h.saddr = c->addr6_ll;
 
-		udp_tap_map[V6][src_port].ts_local = now->tv_sec;
+		udp_tap_map[V6][src_port].ts = now->tv_sec;
+		udp_tap_map[V6][src_port].flags |= PORT_LOCAL;
 
 		if (IN6_IS_ADDR_LOOPBACK(src))
-			udp_tap_map[V6][src_port].loopback = 1;
+			udp_tap_map[V6][src_port].flags |= PORT_LOOPBACK;
 		else
-			udp_tap_map[V6][src_port].loopback = 0;
+			udp_tap_map[V6][src_port].flags &= ~PORT_LOOPBACK;
 
 		if (IN6_ARE_ADDR_EQUAL(src, &c->addr6))
-			udp_tap_map[V6][src_port].gua = 1;
+			udp_tap_map[V6][src_port].flags |= PORT_GUA;
 		else
-			udp_tap_map[V6][src_port].gua = 0;
+			udp_tap_map[V6][src_port].flags &= ~PORT_GUA;
 
 		bitmap_set(udp_act[V6][UDP_ACT_TAP], src_port);
 	} else if (!IN6_IS_ADDR_UNSPECIFIED(&c->dns6_fwd) &&
@@ -999,8 +1001,8 @@ int udp_tap_handler(struct ctx *c, int af, void *addr,
 		udp_tap_map[V4][src].ts = now->tv_sec;
 
 		if (s_in.sin_addr.s_addr == c->gw4 && !c->no_map_gw) {
-			if (!udp_tap_map[V4][dst].ts_local ||
-			    udp_tap_map[V4][dst].loopback)
+			if (!(udp_tap_map[V4][dst].flags & PORT_LOCAL) ||
+			    (udp_tap_map[V4][dst].flags & PORT_LOOPBACK))
 				s_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 			else
 				s_in.sin_addr.s_addr = c->addr4_seen;
@@ -1020,10 +1022,10 @@ int udp_tap_handler(struct ctx *c, int af, void *addr,
 		sl = sizeof(s_in6);
 
 		if (IN6_ARE_ADDR_EQUAL(addr, &c->gw6) && !c->no_map_gw) {
-			if (!udp_tap_map[V6][dst].ts_local ||
-			    udp_tap_map[V6][dst].loopback)
+			if (!(udp_tap_map[V6][dst].flags & PORT_LOCAL) ||
+			    (udp_tap_map[V6][dst].flags & PORT_LOOPBACK))
 				s_in6.sin6_addr = in6addr_loopback;
-			else if (udp_tap_map[V6][dst].gua)
+			else if (udp_tap_map[V6][dst].flags & PORT_GUA)
 				s_in6.sin6_addr = c->addr6;
 			else
 				s_in6.sin6_addr = c->addr6_seen;
@@ -1241,13 +1243,9 @@ static void udp_timer_one(struct ctx *c, int v6, enum udp_act_type type,
 	case UDP_ACT_TAP:
 		tp = &udp_tap_map[v6 ? V6 : V4][port];
 
-		if (ts->tv_sec - tp->ts > UDP_CONN_TIMEOUT)
+		if (ts->tv_sec - tp->ts > UDP_CONN_TIMEOUT) {
 			s = tp->sock;
-
-		if (ts->tv_sec - tp->ts_local > UDP_CONN_TIMEOUT) {
-			tp->ts_local = 0;
-			tp->loopback = 0;
-			tp->gua = 0;
+			tp->flags = 0;
 		}
 
 		break;
-- 
2.35.1


  parent reply	other threads:[~2022-03-25 22:52 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-25 22:52 [PATCH 00/24] Boundary-checked "packets", TCP timerfd timeouts, assorted fixes Stefano Brivio
2022-03-25 22:52 ` [PATCH 01/24] conf, util, tap: Implement --trace option for extra verbose logging Stefano Brivio
2022-03-25 22:52 ` [PATCH 02/24] pcap: Fix mistake in printed string Stefano Brivio
2022-03-25 22:52 ` [PATCH 03/24] util: Drop CHECK_SET_MIN_MAX{,_PROTO_FD} macros Stefano Brivio
2022-03-25 22:52 ` [PATCH 04/24] util: Use standard int types Stefano Brivio
2022-03-25 22:52 ` [PATCH 05/24] tcp: Refactor to use events instead of states, split out spliced implementation Stefano Brivio
2022-03-25 22:52 ` [PATCH 06/24] test/lib/video: Fill in href attributes of video shortcuts Stefano Brivio
2022-03-25 22:52 ` [PATCH 07/24] udp: Drop _splice from recv, send, sendto static buffer names Stefano Brivio
2022-03-25 22:52 ` [PATCH 08/24] udp: Split buffer queueing/writing parts of udp_sock_handler() Stefano Brivio
2022-03-25 22:52 ` [PATCH 09/24] dhcpv6, tap, tcp: Use IN6_ARE_ADDR_EQUAL instead of open-coded memcmp() Stefano Brivio
2022-03-25 22:52 ` Stefano Brivio [this message]
2022-03-25 22:52 ` [PATCH 11/24] Makefile: Enable a few hardening flags Stefano Brivio
2022-03-25 22:52 ` [PATCH 12/24] test: Add asciinema(1) as requirement for CI in README Stefano Brivio
2022-03-25 22:52 ` [PATCH 13/24] test, seccomp, Makefile: Switch to valgrind runs for passt functional tests Stefano Brivio
2022-03-25 22:52 ` [PATCH 14/24] tcp, udp, util: Enforce 24-bit limit on socket numbers Stefano Brivio
2022-03-25 22:52 ` [PATCH 15/24] tcp: Rework timers to use timerfd instead of periodic bitmap scan Stefano Brivio
2022-03-25 22:52 ` [PATCH 16/24] tcp_splice: Close sockets right away on high number of open files Stefano Brivio
2022-03-25 22:52 ` [PATCH 17/24] test/perf: Work-around for virtio_net hang before long streams from guest Stefano Brivio
2022-03-25 22:52 ` [PATCH 18/24] README: Avoid "here" links Stefano Brivio
2022-03-25 22:52 ` [PATCH 19/24] README: Update Interfaces and Availability sections Stefano Brivio
2022-03-25 22:52 ` [PATCH 20/24] tcp: Fit struct tcp_conn into a single 64-byte cacheline Stefano Brivio
2022-03-25 22:52 ` [PATCH 21/24] dhcp: Minimum option length implied by RFC 951 is 60 bytes, not 62 Stefano Brivio
2022-03-25 22:52 ` [PATCH 22/24] tcp, tcp_splice: Use less awkward syntax to swap in/out sockets from pools Stefano Brivio
2022-03-25 22:52 ` [PATCH 23/24] util: Fix function declaration style of write_pidfile() Stefano Brivio
2022-03-25 22:53 ` [PATCH 24/24] treewide: Packet abstraction with mandatory boundary checks Stefano Brivio

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=20220325225300.2803584-11-sbrivio@redhat.com \
    --to=sbrivio@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).