Skip to contentSkip to conversation

net/failsafe: safer subdev iterator

PATCH Superseded Gaetan Rivet 3 participants 5 messages Aug 17, 2017 – Aug 28, 2017 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/failsafe: safer subdev iterator

The sub_device iterator macro should follow the general gist of the tailq API for an easier understanding and safer use.

Once the loop has finished, the iterator should be set to NULL. If no sub_device was iterated upon, the iterator should still be NULL.

---
 drivers/net/failsafe/failsafe_private.h | 1 +
 1 file changed, 1 insertion(+)
drivers/net/failsafe/failsafe_private.h +1 -0
index 0361cf4..6fa8de5 100644
@@ -222,6 +222,7 @@ extern int mac_from_arg;
  */
 #define FOREACH_SUBDEV_STATE(s, i, dev, state)				\
 	for (i = fs_find_next((dev), 0, state);				\
+	     ((s = NULL) == NULL) &&					\
 	     i < PRIV(dev)->subs_tail && (s = &PRIV(dev)->subs[i]);	\
 	     i = fs_find_next((dev), i + 1, state))
 
Signature · 1 line
2.1.4
#1 lore raw
Quoted 16 lines · On Thu, 17 Aug 2017 17:52:09 +0200 Gaetan Rivet <[email protected]> wrote:

The sub_device iterator macro should follow the general gist of the tailq API for an easier understanding and safer use.

Once the loop has finished, the iterator should be set to NULL. If no sub_device was iterated upon, the iterator should still be NULL.

---
 drivers/net/failsafe/failsafe_private.h | 1 +
 1 file changed, 1 insertion(+)
drivers/net/failsafe/failsafe_private.h +0 -0
index 0361cf4..6fa8de5 100644

drivers/net/failsafe/failsafe_private.h extern int mac_from_arg; · line 225

Quoted 4 lines
drivers/net/failsafe/failsafe_private.h +1 -0
@@ -222,6 +222,7 @@ extern int mac_from_arg;
  */
 #define FOREACH_SUBDEV_STATE(s, i, dev, state)				\
 	for (i = fs_find_next((dev), 0, state);				\
+	     ((s = NULL) == NULL) &&					\

I assume you are trying to do assignment inside an expression and just expect that to always to be true and go onto next part of assignment. This is a convulted way to put code in the loop iterator.

But this macro is way too complex. Please break it up into inline functions or figure out how to simplify the logic better.

#2 lore raw
Quoted 27 lines · Stephen Hemminger
Quoted 16 lines · On Thu, 17 Aug 2017 17:52:09 +0200 Gaetan Rivet <[email protected]> wrote:

The sub_device iterator macro should follow the general gist of the tailq API for an easier understanding and safer use.

Once the loop has finished, the iterator should be set to NULL. If no sub_device was iterated upon, the iterator should still be NULL.

---
 drivers/net/failsafe/failsafe_private.h | 1 +
 1 file changed, 1 insertion(+)
drivers/net/failsafe/failsafe_private.h +0 -0
index 0361cf4..6fa8de5 100644

drivers/net/failsafe/failsafe_private.h extern int mac_from_arg; · line 225

Quoted 4 lines
drivers/net/failsafe/failsafe_private.h +1 -0
@@ -222,6 +222,7 @@ extern int mac_from_arg;
  */
 #define FOREACH_SUBDEV_STATE(s, i, dev, state)				\
 	for (i = fs_find_next((dev), 0, state);				\
+	     ((s = NULL) == NULL) &&					\

I assume you are trying to do assignment inside an expression and just expect that to always to be true and go onto next part of assignment. This is a convulted way to put code in the loop iterator.

Quoted 2 lines

But this macro is way too complex. Please break it up into inline functions or figure out how to simplify the logic better.

I think you are right, thanks for the wake-up call.

Signature · 2 lines
Gaëtan Rivet
6WIND
#3 lore raw

PATCH v2 1/1 net/failsafe: safer subdev iterator

In reply to Gaetan Rivet

The sub_device iterator macro should follow the general gist of the tailq API for an easier understanding and safer use.

Once the loop has finished, the iterator should be set to NULL. If no sub_device was iterated upon, the iterator should still be NULL.

---

v2:

- Rewrite fs_find_next to simplify the macro using it.

 drivers/net/failsafe/failsafe_private.h | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
drivers/net/failsafe/failsafe_private.h +20 -12
index 0361cf4..19d36ab 100644
@@ -220,10 +220,10 @@ extern int mac_from_arg;
  * dev:   (struct rte_eth_dev *), fail-safe ethdev
  * state: (enum dev_state), minimum acceptable device state
  */
-#define FOREACH_SUBDEV_STATE(s, i, dev, state)				\
-	for (i = fs_find_next((dev), 0, state);				\
-	     i < PRIV(dev)->subs_tail && (s = &PRIV(dev)->subs[i]);	\
-	     i = fs_find_next((dev), i + 1, state))
+#define FOREACH_SUBDEV_STATE(s, i, dev, state)		\
+	for (s = fs_find_next((dev), 0, state, &i);	\
+	     s != NULL;					\
+	     s = fs_find_next((dev), i + 1, state, &i))
 
 /**
  * Iterator construct over fail-safe sub-devices:
@@ -294,18 +294,26 @@ extern int mac_from_arg;
 
 /* inlined functions */
 
-static inline uint8_t
-fs_find_next(struct rte_eth_dev *dev, uint8_t sid,
-		enum dev_state min_state)
+static inline struct sub_device *
+fs_find_next(struct rte_eth_dev *dev,
+	     uint8_t sid,
+	     enum dev_state min_state,
+	     uint8_t *sid_out)
 {
-	while (sid < PRIV(dev)->subs_tail) {
-		if (PRIV(dev)->subs[sid].state >= min_state)
+	struct sub_device *subs;
+	uint8_t tail;
+
+	subs = PRIV(dev)->subs;
+	tail = PRIV(dev)->subs_tail;
+	while (sid < tail) {
+		if (subs[sid].state >= min_state)
 			break;
 		sid++;
 	}
-	if (sid >= PRIV(dev)->subs_tail)
-		return PRIV(dev)->subs_tail;
-	return sid;
+	*sid_out = sid;
+	if (sid >= tail)
+		return NULL;
+	return &subs[sid];
 }
 
 /*
Signature · 1 line
2.1.4
Applied
#4 lore raw
Quoted 7 lines · Gaetan Rivet

The sub_device iterator macro should follow the general gist of the tailq API for an easier understanding and safer use.

Once the loop has finished, the iterator should be set to NULL. If no sub_device was iterated upon, the iterator should still be NULL.

Applied to dpdk-next-net/master, thanks.