generic: copy backport, hack, pending patch and config from 5.15 to 6.1
[openwrt/openwrt.git] / target / linux / generic / backport-6.1 / 703-10-v5.16-net-dsa-introduce-helpers-for-iterating-through-port.patch
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
5 dp
6
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.
10
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
16 tree.
17
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.
22
23 These functions can be used both by the core and by drivers.
24
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>
28 ---
29 include/net/dsa.h | 28 ++++++++++++++++++++++++++++
30 1 file changed, 28 insertions(+)
31
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;
36 }
37
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)))
41 +
42 +#define dsa_switch_for_each_port(_dp, _ds) \
43 + list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
44 + if ((_dp)->ds == (_ds))
45 +
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))
49 +
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))
53 +
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)))
57 +
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)))
61 +
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)))
65 +
66 static inline u32 dsa_user_ports(struct dsa_switch *ds)
67 {
68 u32 mask = 0;