From mboxrd@z Thu Jan 1 00:00:00 1970 From: Stefano Brivio 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 Message-ID: <20220325225300.2803584-11-sbrivio@redhat.com> In-Reply-To: <20220325225300.2803584-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============5113004523312576152==" --===============5113004523312576152== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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 --- 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 bi= nd - * @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) }; =20 /** @@ -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 =3D=3D IN_LOOPBACKNET || src =3D=3D INADDR_ANY || src =3D=3D ntohl(c->addr4_seen)) { b->iph.saddr =3D c->gw4; - udp_tap_map[V4][src_port].ts_local =3D now->tv_sec; + udp_tap_map[V4][src_port].ts =3D now->tv_sec; + udp_tap_map[V4][src_port].flags |=3D PORT_LOCAL; =20 if (b->s_in.sin_addr.s_addr =3D=3D c->addr4_seen) - udp_tap_map[V4][src_port].loopback =3D 0; + udp_tap_map[V4][src_port].flags &=3D ~PORT_LOOPBACK; else - udp_tap_map[V4][src_port].loopback =3D 1; + udp_tap_map[V4][src_port].flags |=3D PORT_LOOPBACK; =20 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 =3D c->addr6_ll; =20 - udp_tap_map[V6][src_port].ts_local =3D now->tv_sec; + udp_tap_map[V6][src_port].ts =3D now->tv_sec; + udp_tap_map[V6][src_port].flags |=3D PORT_LOCAL; =20 if (IN6_IS_ADDR_LOOPBACK(src)) - udp_tap_map[V6][src_port].loopback =3D 1; + udp_tap_map[V6][src_port].flags |=3D PORT_LOOPBACK; else - udp_tap_map[V6][src_port].loopback =3D 0; + udp_tap_map[V6][src_port].flags &=3D ~PORT_LOOPBACK; =20 if (IN6_ARE_ADDR_EQUAL(src, &c->addr6)) - udp_tap_map[V6][src_port].gua =3D 1; + udp_tap_map[V6][src_port].flags |=3D PORT_GUA; else - udp_tap_map[V6][src_port].gua =3D 0; + udp_tap_map[V6][src_port].flags &=3D ~PORT_GUA; =20 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 =3D now->tv_sec; =20 if (s_in.sin_addr.s_addr =3D=3D 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 =3D htonl(INADDR_LOOPBACK); else s_in.sin_addr.s_addr =3D c->addr4_seen; @@ -1020,10 +1022,10 @@ int udp_tap_handler(struct ctx *c, int af, void *addr, sl =3D sizeof(s_in6); =20 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 =3D in6addr_loopback; - else if (udp_tap_map[V6][dst].gua) + else if (udp_tap_map[V6][dst].flags & PORT_GUA) s_in6.sin6_addr =3D c->addr6; else s_in6.sin6_addr =3D 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 =3D &udp_tap_map[v6 ? V6 : V4][port]; =20 - if (ts->tv_sec - tp->ts > UDP_CONN_TIMEOUT) + if (ts->tv_sec - tp->ts > UDP_CONN_TIMEOUT) { s =3D tp->sock; - - if (ts->tv_sec - tp->ts_local > UDP_CONN_TIMEOUT) { - tp->ts_local =3D 0; - tp->loopback =3D 0; - tp->gua =3D 0; + tp->flags =3D 0; } =20 break; --=20 2.35.1 --===============5113004523312576152==--