1 From 82b318983c515f29b8b3a0dad9f6a5fe8a68a7f4 Mon Sep 17 00:00:00 2001
2 From: Vladimir Oltean <vladimir.oltean@nxp.com>
3 Date: Wed, 20 Oct 2021 20:49:49 +0300
4 Subject: [PATCH] net: dsa: introduce helpers for iterating through ports using
7 Since the DSA conversion from the ds->ports array into the dst->ports
8 list, the DSA API has encouraged driver writers, as well as the core
9 itself, to write inefficient code.
11 Currently, code that wants to filter by a specific type of port when
12 iterating, like {!unused, user, cpu, dsa}, uses the dsa_is_*_port helper.
13 Under the hood, this uses dsa_to_port which iterates again through
14 dst->ports. But the driver iterates through the port list already, so
15 the complexity is quadratic for the typical case of a single-switch
18 This patch introduces some iteration helpers where the iterator is
19 already a struct dsa_port *dp, so that the other variant of the
20 filtering functions, dsa_port_is_{unused,user,cpu_dsa}, can be used
21 directly on the iterator. This eliminates the second lookup.
23 These functions can be used both by the core and by drivers.
25 Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
26 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
27 Signed-off-by: David S. Miller <davem@davemloft.net>
29 include/net/dsa.h | 28 ++++++++++++++++++++++++++++
30 1 file changed, 28 insertions(+)
32 --- a/include/net/dsa.h
33 +++ b/include/net/dsa.h
34 @@ -476,6 +476,34 @@ static inline bool dsa_is_user_port(stru
35 return dsa_to_port(ds, p)->type == DSA_PORT_TYPE_USER;
38 +#define dsa_tree_for_each_user_port(_dp, _dst) \
39 + list_for_each_entry((_dp), &(_dst)->ports, list) \
40 + if (dsa_port_is_user((_dp)))
42 +#define dsa_switch_for_each_port(_dp, _ds) \
43 + list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
44 + if ((_dp)->ds == (_ds))
46 +#define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
47 + list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
48 + if ((_dp)->ds == (_ds))
50 +#define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
51 + list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
52 + if ((_dp)->ds == (_ds))
54 +#define dsa_switch_for_each_available_port(_dp, _ds) \
55 + dsa_switch_for_each_port((_dp), (_ds)) \
56 + if (!dsa_port_is_unused((_dp)))
58 +#define dsa_switch_for_each_user_port(_dp, _ds) \
59 + dsa_switch_for_each_port((_dp), (_ds)) \
60 + if (dsa_port_is_user((_dp)))
62 +#define dsa_switch_for_each_cpu_port(_dp, _ds) \
63 + dsa_switch_for_each_port((_dp), (_ds)) \
64 + if (dsa_port_is_cpu((_dp)))
66 static inline u32 dsa_user_ports(struct dsa_switch *ds)