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 2E0155A026E for ; Thu, 17 Nov 2022 03:08:55 +0100 (CET) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4NCNcx2C8mz4xZZ; 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=KQIJBmbKaQB2/mlAH1hV9KhS4hdLeT/S8jcKKjsWXXk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=hBfvl+N3rfJ1D9qXhl76Gj9slctHcQPe8DlgsDmQ3UubxX4eq2YlPDuPjuHWw+cYM nNEwJHorM01ISN/wYw5w8B7C3nDzjeMhYRC3b1hPrK+6OwtxPwqDn54A2pJCgJxBc3 cQnep6+BnXGh4Jeh2zhGdcFt42mxI3GAxmkd7WhU= Date: Thu, 17 Nov 2022 12:24:59 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 05/32] tcp: Better helpers for converting between connection pointer and index Message-ID: References: <20221116044212.3876516-1-david@gibson.dropbear.id.au> <20221116044212.3876516-6-david@gibson.dropbear.id.au> <20221117001130.247743ea@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="DQyrt/uOf0yxdCnn" Content-Disposition: inline In-Reply-To: <20221117001130.247743ea@elisabeth> Message-ID-Hash: 72TONJIOFOH2YSVI4MXOZF7Y7ZTHRIO4 X-Message-ID-Hash: 72TONJIOFOH2YSVI4MXOZF7Y7ZTHRIO4 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: --DQyrt/uOf0yxdCnn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Nov 17, 2022 at 12:11:30AM +0100, Stefano Brivio wrote: > On Wed, 16 Nov 2022 15:41:45 +1100 > David Gibson wrote: >=20 > > The macro CONN_OR_NULL() is used to look up connections by index with > > bounds checking. Replace it with an inline function, which means: > > - Better type checking > > - No danger of multiple evaluation of an @index with side effects > >=20 > > Also add a helper to perform the reverse translation: from connection > > pointer to index. Introduce a macro for this which will make later > > cleanups easier and safer. >=20 > Ah, yes, much better, agreed. Just two things here: >=20 > > Signed-off-by: David Gibson > > --- > > tcp.c | 83 ++++++++++++++++++++++++++++++++--------------------------- > > 1 file changed, 45 insertions(+), 38 deletions(-) > >=20 > > diff --git a/tcp.c b/tcp.c > > index d043123..4e56a6c 100644 > > --- a/tcp.c > > +++ b/tcp.c > > @@ -518,14 +518,6 @@ struct tcp_conn { > > (conn->events & (SOCK_FIN_RCVD | TAP_FIN_RCVD))) > > #define CONN_HAS(conn, set) ((conn->events & (set)) =3D=3D (set)) > > =20 > > -#define CONN(index) (tc + (index)) > > - > > -/* We probably don't want to use gcc statement expressions (for portab= ility), so > > - * use this only after well-defined sequence points (no pre-/post-incr= ements). > > - */ > > -#define CONN_OR_NULL(index) \ > > - (((int)(index) >=3D 0 && (index) < TCP_MAX_CONNS) ? (tc + (index)) : = NULL) > > - > > static const char *tcp_event_str[] __attribute((__unused__)) =3D { > > "SOCK_ACCEPTED", "TAP_SYN_RCVD", "ESTABLISHED", "TAP_SYN_ACK_SENT", > > =20 > > @@ -705,6 +697,21 @@ static size_t tcp6_l2_flags_buf_bytes; > > /* TCP connections */ > > static struct tcp_conn tc[TCP_MAX_CONNS]; > > =20 > > +#define CONN(index) (tc + (index)) > > +#define CONN_IDX(conn) ((conn) - tc) > > + > > +/** conn_at_idx() - Find a connection by index, if present > > + * @index: Index of connection to lookup > > + * > > + * Return: Pointer to connection, or NULL if @index is out of bounds >=20 > Return: pointer [...] Fixed. > > + */ > > +static inline struct tcp_conn *conn_at_idx(int index) >=20 > The CONN_OR_NULL name made it very explicit that the pointer obtained > there could be NULL. On the other hand I find conn_at_idx() more > descriptive. But maybe conn_or_null() would be "safer". I don't really > have a preference. I see your point, but on balance I think I marginally prefer conn_at_idx(), so I've left it. --=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 --DQyrt/uOf0yxdCnn Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoULxWu4/Ws0dB+XtgypY4gEwYSIFAmN1jWQACgkQgypY4gEw YSLpUA/+JmfM4oyDeplgJOofyQImUVjUbB85dD0SJuXyP99vdjir+C4tH6QfTNKi gxHH7PYJ3VV9XXN3B3U3qxaAZnkp7cRQzkdR4fpZcH2zJ5Akpls62xDFuA6uaaz4 2sZ7m4bwsY43yKcHXzAKsjchNC+GkGTn0FsM3zZHCtRuR2eFxsoiNF/qEDBrX30s udUEIVuKzjnrrGCnUAOngKz20LnYkSV+4V6qBU8xXv7hCp/G4GGgjtrxNIRHUWLI K8pfqD9lY9T0GklKfPxtAxn94s6eATxtducE6k5xSXT3gMBp3sHf+s2vUWjgbyG/ urfwl2Y3H8oOZVwKYADusbpxvy+jZdvEG22hlkKnI4xgcIofrbYi22k7cnHZ5Vd4 RNlFPTqjK6HGCGiw2/hsqPzKfglw0xfLtYCRUXoi5karlpkBT0Fg8mH8pXwvZnQ8 HDQuHPcfpRLQSgObeIhtnOQOppZ3fgLJDOVOMadjY8Lc/nyO1R5/HQVXE3/gG4H8 5IpCwMxY9F13q0Jb254UgXs0NJDAstMvK8N1F7jOdW//riZqPamodQBWki3j9K+W CzrVy0QE5s5oBZ7+XSi9EXrfAPBsQMrsdcyrouuTVjPipdLceWViix1DadoU/7Uo hLriBKrZvzQuDQlLmePx+t4yBk3pv5cQn4zLUE65YbtWCnV0KQc= =xl2B -----END PGP SIGNATURE----- --DQyrt/uOf0yxdCnn--