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 D1A8C5A026D for ; Thu, 7 Dec 2023 07:20:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701930025; 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=eI3TZ4/Pwhw8WWivyc+C3Q+AijiNzX9jvi/LtS7DA74=; b=hvfvTuVL+0Q3oaAZSxZkWkB26H85BRP/Ea4usqA/gJfuXgLH0Y3ynIvVApo+AHeCqFxtuk DLk8/+gMNIYVvzQRN9pQaqhYjxGKbhz+sZukReLxauPIW/TJqWISsN1yGEBTllbusZutVv nw1WoZE1bNOW6mXvKu6EHIkf+F5+wfw= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-515-ql6Q1lmBNKKwiTbuavR9Zg-1; Thu, 07 Dec 2023 01:20:22 -0500 X-MC-Unique: ql6Q1lmBNKKwiTbuavR9Zg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 108E729ABA1F; Thu, 7 Dec 2023 06:20:22 +0000 (UTC) Received: from elisabeth (unknown [10.39.208.4]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 42C89492BC6; Thu, 7 Dec 2023 06:20:21 +0000 (UTC) Date: Thu, 7 Dec 2023 07:20:10 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 2/3] tcp: Implement hash table with indices rather than pointers Message-ID: <20231207072010.6b653b17@elisabeth> In-Reply-To: References: <20231204031611.3566791-1-david@gibson.dropbear.id.au> <20231204031611.3566791-3-david@gibson.dropbear.id.au> <20231206203727.31014fd6@elisabeth> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: MENEZ36FLHJMA5PB3SFUFMKOLDFA4EOH X-Message-ID-Hash: MENEZ36FLHJMA5PB3SFUFMKOLDFA4EOH 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.8 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: On Thu, 7 Dec 2023 12:04:18 +1100 David Gibson wrote: > On Wed, Dec 06, 2023 at 08:37:27PM +0100, Stefano Brivio wrote: > > On Mon, 4 Dec 2023 14:16:10 +1100 > > David Gibson wrote: > > > > > We implement our hash table with pointers to the entry for each bucket (or > > > NULL). However, the entries are always allocated within the flow table, > > > meaning that a flow index will suffice, halving the size of the hash table. > > > > > > For TCP, just a flow index would be enough, but future uses will want to > > > expand the hash table to cover indexing either side of a flow, so use a > > > flow_sidx_t as the type for each hash bucket. > > > > > > Signed-off-by: David Gibson > > > --- > > > flow.h | 11 +++++++++++ > > > tcp.c | 34 +++++++++++++++++++++++----------- > > > 2 files changed, 34 insertions(+), 11 deletions(-) > > > > > > diff --git a/flow.h b/flow.h > > > index c2a5190..959b461 100644 > > > --- a/flow.h > > > +++ b/flow.h > > > @@ -53,6 +53,17 @@ static_assert(sizeof(flow_sidx_t) <= sizeof(uint32_t), > > > > > > #define FLOW_SIDX_NONE ((flow_sidx_t){ .flow = FLOW_MAX }) > > > > In hindsight, while reviewing the functions below: FLOW_MAX should > > probably be MAX_FROM_BITS(FLOW_INDEX_BITS) - 1 instead (and those >= > > comparisons would happily become >), so that we don't need to have a > > "maximum" value that's also not allowed (then, strictly speaking, it's > > more than the maximum). > > Right, either that or name the variable MAX_NUM_FLOWS or something. > Eh, whatever. > > > > +/** > > > + * flow_sidx_eq() - Test if two sidx values are equal > > > + * @a, @b: sidx values > > > + * > > > + * Return: true iff @a and @b refer to the same side of the same flow > > > + */ > > > +static inline bool flow_sidx_eq(flow_sidx_t a, flow_sidx_t b) > > > +{ > > > + return (a.flow == b.flow) && (a.side == b.side); > > > +} > > > + > > > union flow; > > > > > > void flow_table_compact(struct ctx *c, union flow *hole); > > > diff --git a/tcp.c b/tcp.c > > > index 09acf7f..7e438b7 100644 > > > --- a/tcp.c > > > +++ b/tcp.c > > > @@ -574,7 +574,7 @@ static unsigned int tcp6_l2_flags_buf_used; > > > #define CONN(idx) (&(FLOW(idx)->tcp)) > > > > > > /* Table for lookup from remote address, local port, remote port */ > > > -static struct tcp_tap_conn *tc_hash[TCP_HASH_TABLE_SIZE]; > > > +static flow_sidx_t tc_hash[TCP_HASH_TABLE_SIZE]; > > > > > > static_assert(ARRAY_SIZE(tc_hash) >= FLOW_MAX, > > > "Safe linear probing requires hash table larger than connection table"); > > > @@ -1197,10 +1197,13 @@ static unsigned int tcp_conn_hash(const struct ctx *c, > > > static inline unsigned tcp_hash_probe(const struct ctx *c, > > > const struct tcp_tap_conn *conn) > > > { > > > + flow_sidx_t sidx = FLOW_SIDX(conn, TAPSIDE); > > > unsigned b; > > > > > > /* Linear probing */ > > > - for (b = tcp_conn_hash(c, conn); tc_hash[b] && tc_hash[b] != conn; > > > + for (b = tcp_conn_hash(c, conn); > > > + !flow_sidx_eq(tc_hash[b], FLOW_SIDX_NONE) && > > > > Do we actually need to check for FLOW_SIDX_NONE explicitly? That is, > > sidx we get as input here should never be FLOW_SIDX_NONE. > > Yes: we need to stop when we reach something matching @sidx *or* we > hit an empty entry. Otherwise we'll never terminate if the entry > isn't in there. Ah, right, sorry, for a moment I read this as: !flow_sidx_eq(tc_hash[b], FLOW_SIDX_NONE) && flow_sidx_eq(tc_hash[b], sidx); where sidx != FLOW_SIDX_NONE would have the first comparison redundant. But it's not the case, of course. -- Stefano