From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 137865A026A for ; Thu, 17 Nov 2022 03:08:59 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NCNcx2dXbz4xZp; Thu, 17 Nov 2022 13:08:49 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1668650929; bh=W0E3LSG6IrJY70ff4nZR2sH4YeN97OPmKTP06iotRds=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=UWzQuiNEqrQke1Oi/9bxxCuw/A+Cg/qGiSFo5xcCOqeevuZdwF5xT6Rb+kB0qxEYd LPomnFhYvJFxRwSteKERbZacaO/6Hr7SzV0gLEIzcKsVSFE0ES24egicsWGhvRo2av TmUl8+wLpu0EfErjXy5Q1xvztYSJK2K1kun97x4A= Date: Thu, 17 Nov 2022 12:48:30 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 19/32] inany: Helper functions for handling addresses which could be IPv4 or IPv6 Message-ID: References: <20221116044212.3876516-1-david@gibson.dropbear.id.au> <20221116044212.3876516-20-david@gibson.dropbear.id.au> <20221117005408.0f99d1e3@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="QqzD1g2A67Xq/89H" Content-Disposition: inline In-Reply-To: <20221117005408.0f99d1e3@elisabeth> Message-ID-Hash: LMLLQTDUC7ROL2LPNAA6F2EHZRE2HT4L X-Message-ID-Hash: LMLLQTDUC7ROL2LPNAA6F2EHZRE2HT4L X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.3 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: --QqzD1g2A67Xq/89H Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Nov 17, 2022 at 12:54:08AM +0100, Stefano Brivio wrote: > [Reviewed until 25/32 so far] >=20 > On Wed, 16 Nov 2022 15:41:59 +1100 > David Gibson wrote: >=20 > > struct tcp_conn stores an address which could be IPv6 or IPv4 using a > > union. We can do this without an additional tag by encoding IPv4 addre= sses > > as IPv4-mapped IPv6 addresses. > >=20 > > This approach is useful wider than the specific place in tcp_conn, so > > expose a new 'union inany_addr' like this from a new inany.h. Along wi= th > > that create a number of helper functions to make working with these "in= any" > > addresses easier. > >=20 > > Signed-off-by: David Gibson > > --- > > Makefile | 6 ++-- > > inany.h | 68 ++++++++++++++++++++++++++++++++++++++++ > > tcp.c | 88 +++++++++++++++++++++++++--------------------------- > > tcp_conn.h | 15 ++------- > > tcp_splice.c | 1 + > > 5 files changed, 117 insertions(+), 61 deletions(-) > > create mode 100644 inany.h > >=20 > > diff --git a/Makefile b/Makefile > > index 9046b0b..ca453aa 100644 > > --- a/Makefile > > +++ b/Makefile > > @@ -44,9 +44,9 @@ SRCS =3D $(PASST_SRCS) $(QRAP_SRCS) > > MANPAGES =3D passt.1 pasta.1 qrap.1 > > =20 > > PASST_HEADERS =3D arch.h arp.h checksum.h conf.h dhcp.h dhcpv6.h icmp.= h \ > > - isolation.h lineread.h log.h ndp.h netlink.h packet.h passt.h pasta.h= \ > > - pcap.h port_fwd.h siphash.h tap.h tcp.h tcp_conn.h tcp_splice.h udp.h= \ > > - util.h > > + inany.h isolation.h lineread.h log.h ndp.h netlink.h packet.h passt.h= \ > > + pasta.h pcap.h port_fwd.h siphash.h tap.h tcp.h tcp_conn.h \ > > + tcp_splice.h udp.h util.h > > HEADERS =3D $(PASST_HEADERS) seccomp.h > > =20 > > # On gcc 11 and 12, with -O2 and -flto, tcp_hash() and siphash_20b(), = if > > diff --git a/inany.h b/inany.h > > new file mode 100644 > > index 0000000..4e53da9 > > --- /dev/null > > +++ b/inany.h > > @@ -0,0 +1,68 @@ > > +/* SPDX-License-Identifier: AGPL-3.0-or-later > > + * Copyright Red Hat > > + * Author: David Gibson > > + * > > + * inany.h - Types and helpers for handling addresses which could be > > + * IPv6 or IPv4 (encoded as IPv4-mapped IPv6 addresses) > > + */ > > + > > +#include > > + > > +/** union inany_addr - Represents either an IPv4 or IPv6 address > > + * @a6: Address as an IPv6 address, may be IPv4-mapped > > + * @_v4._zero: All zero-bits for an IPv4 address > > + * @_v4._one: All one-bits for an IPv4 address > > + * @_v4.a4: If @a6 is an IPv4 mapped address, this is the raw IPv4 add= ress > > + * > > + * Fields starting with _ shouldn't be accessed except via helpers. > > + */ > > +union inany_addr { > > + struct in6_addr a6; > > + struct { > > + uint8_t _zero[10]; > > + uint8_t _one[2]; > > + struct in_addr a4; > > + } _v4mapped; >=20 > I'm not sure the extra _ are really worth it. I mean, that's not really > enforceable, so saying that v4mapped should only be accessed by helpers > should be equivalent. Fair call. Adjusted. >=20 > > +}; > > + > > +/** inany_v4 - Extract IPv4 address, if present, from IPv[46] address > > + * @addr: IPv4 or IPv6 address > > + * > > + * Return: IPv4 address if @addr is IPv4, NULL otherwise > > + */ > > +static inline const struct in_addr *inany_v4(const union inany_addr *a= ddr) > > +{ > > + if (!IN6_IS_ADDR_V4MAPPED(&addr->a6)) > > + return NULL; > > + return &addr->_v4mapped.a4; > > +} > > + > > +/** inany_equals - Compare two IPv[46] addresses > > + * @a, @b: IPv[46] addresses > > + * > > + * Return: true if @a and @b are the same address > > + */ > > +static inline bool inany_equals(const union inany_addr *a, > > + const union inany_addr *b) > > +{ > > + return IN6_ARE_ADDR_EQUAL(&a->a6, &b->a6); > > +} > > + > > +/** inany_from_af - Set IPv[46] address from IPv4 or IPv6 address > > + * @aa: Pointer to store IPv[46] address > > + * @af: Address family of @addr > > + * @addr: struct in_addr (IPv4) or struct in6_addr (IPv6) > > + */ > > +static inline void inany_from_af(union inany_addr *aa, int af, const v= oid *addr) > > +{ > > + if (af =3D=3D AF_INET6) { > > + aa->a6 =3D *((struct in6_addr *)addr); > > + } else if (af =3D=3D AF_INET) { > > + memset(&aa->_v4mapped._zero, 0, sizeof(aa->_v4mapped._zero)); > > + memset(&aa->_v4mapped._one, 0xff, sizeof(aa->_v4mapped._one)); > > + aa->_v4mapped.a4 =3D *((struct in_addr *)addr); > > + } else { > > + /* Not valid to call with other address families */ > > + assert(0); > > + } > > +} > > diff --git a/tcp.c b/tcp.c > > index 7686766..4040198 100644 > > --- a/tcp.c > > +++ b/tcp.c > > @@ -301,6 +301,7 @@ > > #include "conf.h" > > #include "tcp_splice.h" > > #include "log.h" > > +#include "inany.h" > > =20 > > #include "tcp_conn.h" > > =20 > > @@ -404,7 +405,7 @@ struct tcp6_l2_head { /* For MSS6 macro: keep in sy= nc with tcp6_l2_buf_t */ > > #define OPT_SACK 5 > > #define OPT_TS 8 > > =20 > > -#define CONN_V4(conn) IN6_IS_ADDR_V4MAPPED(&conn->a.a6) > > +#define CONN_V4(conn) (!!inany_v4(&(conn)->addr)) > > #define CONN_V6(conn) (!CONN_V4(conn)) > > #define CONN_IS_CLOSING(conn) \ > > ((conn->events & ESTABLISHED) && \ > > @@ -438,7 +439,7 @@ static int tcp_sock_init_ext [NUM_PORTS][IP_VERSION= S]; > > static int tcp_sock_ns [NUM_PORTS][IP_VERSIONS]; > > =20 > > /* Table of destinations with very low RTT (assumed to be local), LRU = */ > > -static struct in6_addr low_rtt_dst[LOW_RTT_TABLE_SIZE]; > > +static union inany_addr low_rtt_dst[LOW_RTT_TABLE_SIZE]; > > =20 > > /* Static buffers */ > > =20 > > @@ -861,7 +862,7 @@ static int tcp_rtt_dst_low(const struct tcp_tap_con= n *conn) > > int i; > > =20 > > for (i =3D 0; i < LOW_RTT_TABLE_SIZE; i++) > > - if (IN6_ARE_ADDR_EQUAL(&conn->a.a6, low_rtt_dst + i)) > > + if (inany_equals(&conn->addr, low_rtt_dst + i)) > > return 1; > > =20 > > return 0; > > @@ -883,7 +884,7 @@ static void tcp_rtt_dst_check(const struct tcp_tap_= conn *conn, > > return; > > =20 > > for (i =3D 0; i < LOW_RTT_TABLE_SIZE; i++) { > > - if (IN6_ARE_ADDR_EQUAL(&conn->a.a6, low_rtt_dst + i)) > > + if (inany_equals(&conn->addr, low_rtt_dst + i)) > > return; > > if (hole =3D=3D -1 && IN6_IS_ADDR_UNSPECIFIED(low_rtt_dst + i)) > > hole =3D i; > > @@ -895,10 +896,10 @@ static void tcp_rtt_dst_check(const struct tcp_ta= p_conn *conn, > > if (hole =3D=3D -1) > > return; > > =20 > > - memcpy(low_rtt_dst + hole++, &conn->a.a6, sizeof(conn->a.a6)); > > + low_rtt_dst[hole++] =3D conn->addr; > > if (hole =3D=3D LOW_RTT_TABLE_SIZE) > > hole =3D 0; > > - memcpy(low_rtt_dst + hole, &in6addr_any, sizeof(conn->a.a6)); > > + inany_from_af(low_rtt_dst + hole, AF_INET6, &in6addr_any); > > #else > > (void)conn; > > (void)tinfo; > > @@ -1187,13 +1188,14 @@ static int tcp_hash_match(const struct tcp_tap_= conn *conn, > > int af, const void *addr, > > in_port_t tap_port, in_port_t sock_port) > > { > > - if (af =3D=3D AF_INET && CONN_V4(conn) && > > - !memcmp(&conn->a.a4.a, addr, sizeof(conn->a.a4.a)) && > > + const struct in_addr *a4 =3D inany_v4(&conn->addr); > > + > > + if (af =3D=3D AF_INET && a4 && !memcmp(a4, addr, sizeof(*a4)) && > > conn->tap_port =3D=3D tap_port && conn->sock_port =3D=3D sock_por= t) > > return 1; > > =20 > > if (af =3D=3D AF_INET6 && > > - IN6_ARE_ADDR_EQUAL(&conn->a.a6, addr) && > > + IN6_ARE_ADDR_EQUAL(&conn->addr.a6, addr) && > > conn->tap_port =3D=3D tap_port && conn->sock_port =3D=3D sock_por= t) > > return 1; >=20 > Note to self or other reviewers: switch to inany_equals() in 22/32. >=20 --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --QqzD1g2A67Xq/89H Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoULxWu4/Ws0dB+XtgypY4gEwYSIFAmN1kucACgkQgypY4gEw YSJZZg/8DU/8YzNGy9XyCapbRXNT91T20VUFjbVLHxm3NQQAOwlVHiYLuZ4D1LTy 11tQfyxdCuzv/9Oeqw2KSIiGoZvNDBkOz5gq10Gdp3MOi+dgG3b0HmSFwCNjYj0U jOlxbug7CvAU4YgcO+7Fvgzu+uVzD668GRnBQkO0N3o9aG3uwiJATfQaZJcXCD9D E69nNvDlVSazP6CIkwDMt6EA9KCQ+g5k/DVv9pd3g6SCCmo9Jc6W9EDXvR8wVN+k OOZ7DjQLiuJ7+2BQZLQCwobAVd4kmh7Hxj9n0uSrvqxtic1Uu06UtCJK2R2sklNE dQHF+BGysDs/1euV3KQuXeddyApfwAsTY5AfCYLonC78zNZfGPrcGU9Yx3FnEg88 wRi/cJnscqbPy4z+TZUVj+waA/ko7HeEEGo5CleSzqiVmaHGK8SW4RJ5p4URb8qT C183PjMQ2MiRlzR0OXVs+SvskXG4ZlUBfTsvmCrtOlba9oRSdONcuaWZFNDp/AM5 Gw5ERS3DZbPCNaM/y5fq6tk77F6cUzZ349fRb1OBeHpiBRUzCTZRtxPxV8YUvm18 5HbbQcIDiPrUnuWObBC9Mu3ar7j860tENEnzNM6phjfidJxEzCFonyL4hqt+J8+T K2DzIvDsR0jVbjPyuR0bskHqQEN5HUo3FAKU4Eyn3fJrdETELyo= =sU7C -----END PGP SIGNATURE----- --QqzD1g2A67Xq/89H--