1 From dc452a471dbae8aca8257c565174212620880093 Mon Sep 17 00:00:00 2001
2 From: Vladimir Oltean <vladimir.oltean@nxp.com>
3 Date: Fri, 10 Dec 2021 01:34:37 +0200
4 Subject: net: dsa: introduce tagger-owned storage for private and shared data
6 Ansuel is working on register access over Ethernet for the qca8k switch
7 family. This requires the qca8k tagging protocol driver to receive
8 frames which aren't intended for the network stack, but instead for the
9 qca8k switch driver itself.
11 The dp->priv is currently the prevailing method for passing data back
12 and forth between the tagging protocol driver and the switch driver.
13 However, this method is riddled with caveats.
15 The DSA design allows in principle for any switch driver to return any
16 protocol it desires in ->get_tag_protocol(). The dsa_loop driver can be
17 modified to do just that. But in the current design, the memory behind
18 dp->priv has to be allocated by the switch driver, so if the tagging
19 protocol is paired to an unexpected switch driver, we may end up in NULL
20 pointer dereferences inside the kernel, or worse (a switch driver may
21 allocate dp->priv according to the expectations of a different tagger).
23 The latter possibility is even more plausible considering that DSA
24 switches can dynamically change tagging protocols in certain cases
25 (dsa <-> edsa, ocelot <-> ocelot-8021q), and the current design lends
26 itself to mistakes that are all too easy to make.
28 This patch proposes that the tagging protocol driver should manage its
29 own memory, instead of relying on the switch driver to do so.
30 After analyzing the different in-tree needs, it can be observed that the
31 required tagger storage is per switch, therefore a ds->tagger_data
32 pointer is introduced. In principle, per-port storage could also be
33 introduced, although there is no need for it at the moment. Future
34 changes will replace the current usage of dp->priv with ds->tagger_data.
36 We define a "binding" event between the DSA switch tree and the tagging
37 protocol. During this binding event, the tagging protocol's ->connect()
38 method is called first, and this may allocate some memory for each
39 switch of the tree. Then a cross-chip notifier is emitted for the
40 switches within that tree, and they are given the opportunity to fix up
41 the tagger's memory (for example, they might set up some function
42 pointers that represent virtual methods for consuming packets).
43 Because the memory is owned by the tagger, there exists a ->disconnect()
44 method for the tagger (which is the place to free the resources), but
45 there doesn't exist a ->disconnect() method for the switch driver.
46 This is part of the design. The switch driver should make minimal use of
47 the public part of the tagger data, and only after type-checking it
48 using the supplied "proto" argument.
50 In the code there are in fact two binding events, one is the initial
51 event in dsa_switch_setup_tag_protocol(). At this stage, the cross chip
52 notifier chains aren't initialized, so we call each switch's connect()
53 method by hand. Then there is dsa_tree_bind_tag_proto() during
54 dsa_tree_change_tag_proto(), and here we have an old protocol and a new
55 one. We first connect to the new one before disconnecting from the old
56 one, to simplify error handling a bit and to ensure we remain in a valid
59 Co-developed-by: Ansuel Smith <ansuelsmth@gmail.com>
60 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
61 Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
62 Signed-off-by: David S. Miller <davem@davemloft.net>
64 include/net/dsa.h | 12 +++++++++
65 net/dsa/dsa2.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++---
66 net/dsa/dsa_priv.h | 1 +
67 net/dsa/switch.c | 14 +++++++++++
68 4 files changed, 96 insertions(+), 4 deletions(-)
70 --- a/include/net/dsa.h
71 +++ b/include/net/dsa.h
72 @@ -80,12 +80,15 @@ enum dsa_tag_protocol {
76 +struct dsa_switch_tree;
78 struct dsa_device_ops {
79 struct sk_buff *(*xmit)(struct sk_buff *skb, struct net_device *dev);
80 struct sk_buff *(*rcv)(struct sk_buff *skb, struct net_device *dev);
81 void (*flow_dissect)(const struct sk_buff *skb, __be16 *proto,
83 + int (*connect)(struct dsa_switch_tree *dst);
84 + void (*disconnect)(struct dsa_switch_tree *dst);
85 unsigned int needed_headroom;
86 unsigned int needed_tailroom;
88 @@ -329,6 +332,8 @@ struct dsa_switch {
95 * Configuration data for this switch.
97 @@ -612,6 +617,13 @@ struct dsa_switch_ops {
98 enum dsa_tag_protocol mprot);
99 int (*change_tag_protocol)(struct dsa_switch *ds, int port,
100 enum dsa_tag_protocol proto);
102 + * Method for switch drivers to connect to the tagging protocol driver
103 + * in current use. The switch driver can provide handlers for certain
104 + * types of packets for switch management.
106 + int (*connect_tag_protocol)(struct dsa_switch *ds,
107 + enum dsa_tag_protocol proto);
109 /* Optional switch-wide initialization and destruction methods */
110 int (*setup)(struct dsa_switch *ds);
113 @@ -230,8 +230,12 @@ static struct dsa_switch_tree *dsa_tree_
115 static void dsa_tree_free(struct dsa_switch_tree *dst)
118 + if (dst->tag_ops) {
119 + if (dst->tag_ops->disconnect)
120 + dst->tag_ops->disconnect(dst);
122 dsa_tag_driver_put(dst->tag_ops);
124 list_del(&dst->list);
127 @@ -805,7 +809,7 @@ static int dsa_switch_setup_tag_protocol
130 if (tag_ops->proto == dst->default_proto)
134 for (port = 0; port < ds->num_ports; port++) {
135 if (!dsa_is_cpu_port(ds, port))
136 @@ -821,6 +825,17 @@ static int dsa_switch_setup_tag_protocol
141 + if (ds->ops->connect_tag_protocol) {
142 + err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
145 + "Unable to connect to tag protocol \"%s\": %pe\n",
146 + tag_ops->name, ERR_PTR(err));
154 @@ -1132,6 +1147,46 @@ static void dsa_tree_teardown(struct dsa
158 +static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
159 + const struct dsa_device_ops *tag_ops)
161 + const struct dsa_device_ops *old_tag_ops = dst->tag_ops;
162 + struct dsa_notifier_tag_proto_info info;
165 + dst->tag_ops = tag_ops;
167 + /* Notify the new tagger about the connection to this tree */
168 + if (tag_ops->connect) {
169 + err = tag_ops->connect(dst);
174 + /* Notify the switches from this tree about the connection
175 + * to the new tagger
177 + info.tag_ops = tag_ops;
178 + err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_CONNECT, &info);
179 + if (err && err != -EOPNOTSUPP)
180 + goto out_disconnect;
182 + /* Notify the old tagger about the disconnection from this tree */
183 + if (old_tag_ops->disconnect)
184 + old_tag_ops->disconnect(dst);
189 + /* Revert the new tagger's connection to this tree */
190 + if (tag_ops->disconnect)
191 + tag_ops->disconnect(dst);
193 + dst->tag_ops = old_tag_ops;
198 /* Since the dsa/tagging sysfs device attribute is per master, the assumption
199 * is that all DSA switches within a tree share the same tagger, otherwise
200 * they would have formed disjoint trees (different "dsa,member" values).
201 @@ -1164,12 +1219,15 @@ int dsa_tree_change_tag_proto(struct dsa
205 + /* Notify the tag protocol change */
206 info.tag_ops = tag_ops;
207 err = dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO, &info);
209 - goto out_unwind_tagger;
212 - dst->tag_ops = tag_ops;
213 + err = dsa_tree_bind_tag_proto(dst, tag_ops);
215 + goto out_unwind_tagger;
219 @@ -1257,6 +1315,7 @@ static int dsa_port_parse_cpu(struct dsa
220 struct dsa_switch *ds = dp->ds;
221 struct dsa_switch_tree *dst = ds->dst;
222 enum dsa_tag_protocol default_proto;
225 /* Find out which protocol the switch would prefer. */
226 default_proto = dsa_get_tag_protocol(dp, master);
227 @@ -1311,6 +1370,12 @@ static int dsa_port_parse_cpu(struct dsa
229 dsa_tag_driver_put(tag_ops);
231 + if (tag_ops->connect) {
232 + err = tag_ops->connect(dst);
237 dst->tag_ops = tag_ops;
240 --- a/net/dsa/dsa_priv.h
241 +++ b/net/dsa/dsa_priv.h
242 @@ -37,6 +37,7 @@ enum {
243 DSA_NOTIFIER_VLAN_DEL,
245 DSA_NOTIFIER_TAG_PROTO,
246 + DSA_NOTIFIER_TAG_PROTO_CONNECT,
247 DSA_NOTIFIER_MRP_ADD,
248 DSA_NOTIFIER_MRP_DEL,
249 DSA_NOTIFIER_MRP_ADD_RING_ROLE,
250 --- a/net/dsa/switch.c
251 +++ b/net/dsa/switch.c
252 @@ -616,6 +616,17 @@ static int dsa_switch_change_tag_proto(s
256 +static int dsa_switch_connect_tag_proto(struct dsa_switch *ds,
257 + struct dsa_notifier_tag_proto_info *info)
259 + const struct dsa_device_ops *tag_ops = info->tag_ops;
261 + if (!ds->ops->connect_tag_protocol)
262 + return -EOPNOTSUPP;
264 + return ds->ops->connect_tag_protocol(ds, tag_ops->proto);
267 static int dsa_switch_mrp_add(struct dsa_switch *ds,
268 struct dsa_notifier_mrp_info *info)
270 @@ -735,6 +746,9 @@ static int dsa_switch_event(struct notif
271 case DSA_NOTIFIER_TAG_PROTO:
272 err = dsa_switch_change_tag_proto(ds, info);
274 + case DSA_NOTIFIER_TAG_PROTO_CONNECT:
275 + err = dsa_switch_connect_tag_proto(ds, info);
277 case DSA_NOTIFIER_MRP_ADD:
278 err = dsa_switch_mrp_add(ds, info);