From mboxrd@z Thu Jan  1 00:00:00 1970
Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124])
	by passt.top (Postfix) with ESMTP id 633E15A0268
	for <passt-dev@passt.top>; Thu, 17 Nov 2022 00:53:50 +0100 (CET)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
	s=mimecast20190719; t=1668642829;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:content-type:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=yQXq43reRqKnJ1JSpUj0Lvvz6IKGUYKmwe43ohUKrGM=;
	b=eKG4SqBR5mJRPvYdwK+mSD+Huqa8kRSZZjtfuhbmX8cpH2OROOLYu+WazI/X/aS+s5GSyy
	K56d/8L/J2233cuvkiC0XcdeRfhREX+RayZoU5KEFciZPwFcryWLR2V7x8fl515Bw6ifeR
	+FZuHS/LGvR348vf3YnTaLxZKBtrDuQ=
Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com
 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 us-mta-413-t-yAjEMuPFWpvtiJBXFL4Q-1; Wed, 16 Nov 2022 18:53:46 -0500
X-MC-Unique: t-yAjEMuPFWpvtiJBXFL4Q-1
Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7])
	(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CC00E29AB453;
	Wed, 16 Nov 2022 23:53:45 +0000 (UTC)
Received: from maya.cloud.tilaa.com (ovpn-208-8.brq.redhat.com [10.40.208.8])
	by smtp.corp.redhat.com (Postfix) with ESMTPS id 97E39140EBF3;
	Wed, 16 Nov 2022 23:53:45 +0000 (UTC)
Date: Thu, 17 Nov 2022 00:11:30 +0100
From: Stefano Brivio <sbrivio@redhat.com>
To: David Gibson <david@gibson.dropbear.id.au>
Subject: Re: [PATCH 05/32] tcp: Better helpers for converting between
 connection pointer and index
Message-ID: <20221117001130.247743ea@elisabeth>
In-Reply-To: <20221116044212.3876516-6-david@gibson.dropbear.id.au>
References: <20221116044212.3876516-1-david@gibson.dropbear.id.au>
	<20221116044212.3876516-6-david@gibson.dropbear.id.au>
Organization: Red Hat
MIME-Version: 1.0
X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7
X-Mimecast-Spam-Score: 0
X-Mimecast-Originator: redhat.com
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Message-ID-Hash: MAVRCOOR5DV6ZBLQXNY2SEOEVKE3ZLD7
X-Message-ID-Hash: MAVRCOOR5DV6ZBLQXNY2SEOEVKE3ZLD7
X-MailFrom: sbrivio@redhat.com
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 <passt-dev.passt.top>
Archived-At: <https://archives.passt.top/passt-dev/20221117001130.247743ea@elisabeth/>
Archived-At: <https://passt.top/hyperkitty/list/passt-dev@passt.top/message/MAVRCOOR5DV6ZBLQXNY2SEOEVKE3ZLD7/>
List-Archive: <https://archives.passt.top/passt-dev/>
List-Archive: <https://passt.top/hyperkitty/list/passt-dev@passt.top/>
List-Help: <mailto:passt-dev-request@passt.top?subject=help>
List-Owner: <mailto:passt-dev-owner@passt.top>
List-Post: <mailto:passt-dev@passt.top>
List-Subscribe: <mailto:passt-dev-join@passt.top>
List-Unsubscribe: <mailto:passt-dev-leave@passt.top>

On Wed, 16 Nov 2022 15:41:45 +1100
David Gibson <david@gibson.dropbear.id.au> wrote:

> 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
> 
> 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.

Ah, yes, much better, agreed. Just two things here:

> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  tcp.c | 83 ++++++++++++++++++++++++++++++++---------------------------
>  1 file changed, 45 insertions(+), 38 deletions(-)
> 
> 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)) == (set))
>  
> -#define CONN(index)		(tc + (index))
> -
> -/* We probably don't want to use gcc statement expressions (for portability), so
> - * use this only after well-defined sequence points (no pre-/post-increments).
> - */
> -#define CONN_OR_NULL(index)						\
> -	(((int)(index) >= 0 && (index) < TCP_MAX_CONNS) ? (tc + (index)) : NULL)
> -
>  static const char *tcp_event_str[] __attribute((__unused__)) = {
>  	"SOCK_ACCEPTED", "TAP_SYN_RCVD", "ESTABLISHED", "TAP_SYN_ACK_SENT",
>  
> @@ -705,6 +697,21 @@ static size_t tcp6_l2_flags_buf_bytes;
>  /* TCP connections */
>  static struct tcp_conn tc[TCP_MAX_CONNS];
>  
> +#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

Return: pointer [...]

> + */
> +static inline struct tcp_conn *conn_at_idx(int index)

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.

-- 
Stefano