public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v23 1/4] flow: Flow table traversing macros
Date: Fri, 14 Feb 2025 20:08:03 +1100	[thread overview]
Message-ID: <20250214090806.2261034-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20250214090806.2261034-1-david@gibson.dropbear.id.au>

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 <david@gibson.dropbear.id.au>
---
 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
  *
-- 
@@ -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


  reply	other threads:[~2025-02-14  9:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-14  9:08 [PATCH v23 0/4] State migration David Gibson
2025-02-14  9:08 ` David Gibson [this message]
2025-02-14  9:08 ` [PATCH v23 2/4] flow, migrate: Flow migration skeleton David Gibson
2025-02-14  9:08 ` [PATCH v23 3/4] migrate: Migrate TCP flows David Gibson
2025-02-14  9:08 ` [PATCH v23 4/4] test: Add migration tests David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250214090806.2261034-2-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).