From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202502 header.b=hH2BvjSl; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id CC01F5A063D for ; Fri, 14 Feb 2025 14:08:52 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202502; t=1739538527; bh=r01rn/lOYDr5tFfV1GQdbSf2DAS57+iTwlNhe+m+Q40=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hH2BvjSlvxxgXYgLSsnYdXAXLwTH57NV9vuuUCq5QsCnr2ZB0lxFuqtmujvGMdoyj Z6r74b92lrlHxMqsE/GDofQbo7xUQM7Ul0Km+T+Ms0HW2Oe4VGZOehYvBb5+yrKiNs Oi5X6xx7YSdvyigAlFhiYLbAqtkL0AnItFWgxkIDvJ27MtE2NxxeVShqCblFwpU+68 vA/gAEoaGN4FpKjibGIWCE0VObM96YpM+2RhDzOjVIBuPrOaX6OTCnFUsK1Utqx3n1 +Tuy8wc0fToRMUhtxpQ6MX1JYcFbgPH+ENjYKtBe+57zS0SA0dFSBWgeHlr8Ymdsca h0WBNC1vAsLBw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4YvXRz6ptDz4x4t; Sat, 15 Feb 2025 00:08:47 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v24 2/5] flow: Flow table traversing macros Date: Sat, 15 Feb 2025 00:08:42 +1100 Message-ID: <20250214130845.3475757-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250214130845.3475757-1-david@gibson.dropbear.id.au> References: <20250214130845.3475757-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: M7SQ6ZANLJMMBJNHS7JETF4LO2NF7QM4 X-Message-ID-Hash: M7SQ6ZANLJMMBJNHS7JETF4LO2NF7QM4 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: David Gibson 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: The migration code adds more places that need to iterate through the flow table. Introduce some macros to make that easier. Signed-off-by: David Gibson --- flow.c | 14 +++++++------- flow_table.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/flow.c b/flow.c index 3ac551bd..d9b888ce 100644 --- a/flow.c +++ b/flow.c @@ -771,7 +771,7 @@ void flow_defer_handler(const struct ctx *c, const struct timespec *now) struct flow_free_cluster *free_head = NULL; unsigned *last_next = &flow_first_free; bool timer = false; - unsigned idx; + union flow *flow; if (timespec_diff_ms(now, &flow_timer_run) >= FLOW_TIMER_INTERVAL) { timer = true; @@ -780,8 +780,7 @@ void flow_defer_handler(const struct ctx *c, const struct timespec *now) ASSERT(!flow_new_entry); /* Incomplete flow at end of cycle */ - for (idx = 0; idx < FLOW_MAX; idx++) { - union flow *flow = &flowtab[idx]; + flow_foreach_slot(flow) { bool closed = false; switch (flow->f.state) { @@ -798,12 +797,12 @@ void flow_defer_handler(const struct ctx *c, const struct timespec *now) } else { /* New free cluster, add to chain */ free_head = &flow->free; - *last_next = idx; + *last_next = FLOW_IDX(flow); last_next = &free_head->next; } /* Skip remaining empty entries */ - idx += skip - 1; + flow += skip - 1; continue; } @@ -856,14 +855,15 @@ void flow_defer_handler(const struct ctx *c, const struct timespec *now) if (free_head) { /* Add slot to current free cluster */ - ASSERT(idx == FLOW_IDX(free_head) + free_head->n); + ASSERT(FLOW_IDX(flow) == + FLOW_IDX(free_head) + free_head->n); free_head->n++; flow->free.n = flow->free.next = 0; } else { /* Create new free cluster */ free_head = &flow->free; free_head->n = 1; - *last_next = idx; + *last_next = FLOW_IDX(flow); last_next = &free_head->next; } } else { diff --git a/flow_table.h b/flow_table.h index 9a2ff24a..6ff6d0f6 100644 --- a/flow_table.h +++ b/flow_table.h @@ -74,6 +74,37 @@ static inline unsigned flow_idx(const struct flow_common *f) */ #define FLOW(idx) (&flowtab[(idx)]) +/** + * flow_foreach_slot() - 'for' type macro to step through every flow slot + * @flow_: Points to each flow entry in order, including free slots + */ +#define flow_foreach_slot(flow_) \ + for ((flow_) = flowtab; FLOW_IDX(flow_) < FLOW_MAX; (flow_)++) + +/** + * flow_foreach() - 'for' type macro to step through every active flow + * @flow_: Points to each active flow in order + */ +#define flow_foreach(flow_) \ + flow_foreach_slot(flow_) \ + if ((flow_)->f.state == FLOW_STATE_FREE) { \ + (flow_) += (flow_)->free.n - 1; \ + } else if ((flow)->f.state != FLOW_STATE_ACTIVE) { \ + flow_err((flow_), "BUG: Traversing non-active flow"); \ + continue; \ + } else + +/** + * flow_foreach_of_type() - step through active flows of one type + * @flow_: Points to each active flow in order + * @type_: Flow type to select + */ +#define flow_foreach_of_type(flow_, type_) \ + flow_foreach(flow_) \ + if ((flow_)->f.type != (type_)) \ + continue; \ + else + /** flow_at_sidx() - Flow entry for a given sidx * @sidx: Flow & side index * -- 2.48.1