Re: [PATCH v4 2/5] i40e: Introduce and use macros for iterating VSIs and VEBs

From: Ivan Vecera
Date: Fri Nov 24 2023 - 09:52:59 EST


On 22. 11. 23 11:15, Ivan Vecera wrote:
Introduce i40e_for_each_vsi() and i40e_for_each_veb() helper
macros and use them to iterate relevant arrays.

Replace pattern:
for (i = 0; i < pf->num_alloc_vsi; i++)
by:
i40e_for_each_vsi(pf, i, vsi)

and pattern:
for (i = 0; i < I40E_MAX_VEB; i++)
by
i40e_for_each_veb(pf, i, veb)

These macros also check if array item pf->vsi[i] or pf->veb[i]
are not NULL and skip such items so we can remove redundant
checks from loop bodies.

Reviewed-by: Wojciech Drewek<wojciech.drewek@xxxxxxxxx>
Signed-off-by: Ivan Vecera<ivecera@xxxxxxxxxx>
---
drivers/net/ethernet/intel/i40e/i40e.h | 56 ++-
drivers/net/ethernet/intel/i40e/i40e_dcb_nl.c | 10 +-
.../net/ethernet/intel/i40e/i40e_debugfs.c | 54 +--
drivers/net/ethernet/intel/i40e/i40e_main.c | 389 ++++++++----------
4 files changed, 264 insertions(+), 245 deletions(-)

diff --git a/drivers/net/ethernet/intel/i40e/i40e.h b/drivers/net/ethernet/intel/i40e/i40e.h
index f1627ab211cd..1e9266de270b 100644
--- a/drivers/net/ethernet/intel/i40e/i40e.h
+++ b/drivers/net/ethernet/intel/i40e/i40e.h
@@ -686,6 +686,54 @@ struct i40e_pf {
struct list_head ddp_old_prof;
};
+/**
+ * __i40e_pf_next_vsi - get next valid VSI
+ * @pf: pointer to the PF struct
+ * @idx: pointer to start position number
+ *
+ * Find and return next non-NULL VSI pointer in pf->vsi array and
+ * updates idx position. Returns NULL if no VSI is found.
+ **/
+static __always_inline struct i40e_vsi *
+__i40e_pf_next_vsi(struct i40e_pf *pf, int *idx)
+{
+ while (*idx < pf->num_alloc_vsi) {
+ if (pf->vsi[*idx])
+ return pf->vsi[(*idx)++];
+ (*idx)++;
+ }
+ return NULL;
+}
+
+#define i40e_pf_for_each_vsi(_pf, _i, _vsi) \
+ for (_i = 0, _vsi = __i40e_pf_next_vsi(_pf, &_i); \
+ _vsi; \
+ _vsi = __i40e_pf_next_vsi(_pf, &_i))
+

It would be safer to update idx to index of returned vsi in __i40e_pf_next_vsi() so

(pf->vsi[i] == vsi) in i40e_pf_for_each_vsi(pf, i, vsi) loop.

Will fix this in v5.

Thanks,
Ivan