fa3752ac34bce9774b73fba5f2d09b05b724cb8f
[openwrt/staging/981213.git] /
1 From 7f2973149c22e7a6fee4c0c9fa6b8e4108e9c208 Mon Sep 17 00:00:00 2001
2 From: Vladimir Oltean <vladimir.oltean@nxp.com>
3 Date: Tue, 14 Dec 2021 03:45:36 +0200
4 Subject: net: dsa: make tagging protocols connect to individual switches from
5 a tree
6
7 On the NXP Bluebox 3 board which uses a multi-switch setup with sja1105,
8 the mechanism through which the tagger connects to the switch tree is
9 broken, due to improper DSA code design. At the time when tag_ops->connect()
10 is called in dsa_port_parse_cpu(), DSA hasn't finished "touching" all
11 the ports, so it doesn't know how large the tree is and how many ports
12 it has. It has just seen the first CPU port by this time. As a result,
13 this function will call the tagger's ->connect method too early, and the
14 tagger will connect only to the first switch from the tree.
15
16 This could be perhaps addressed a bit more simply by just moving the
17 tag_ops->connect(dst) call a bit later (for example in dsa_tree_setup),
18 but there is already a design inconsistency at present: on the switch
19 side, the notification is on a per-switch basis, but on the tagger side,
20 it is on a per-tree basis. Furthermore, the persistent storage itself is
21 per switch (ds->tagger_data). And the tagger connect and disconnect
22 procedures (at least the ones that exist currently) could see a fair bit
23 of simplification if they didn't have to iterate through the switches of
24 a tree.
25
26 To fix the issue, this change transforms tag_ops->connect(dst) into
27 tag_ops->connect(ds) and moves it somewhere where we already iterate
28 over all switches of a tree. That is in dsa_switch_setup_tag_protocol(),
29 which is a good placement because we already have there the connection
30 call to the switch side of things.
31
32 As for the dsa_tree_bind_tag_proto() method (called from the code path
33 that changes the tag protocol), things are a bit more complicated
34 because we receive the tree as argument, yet when we unwind on errors,
35 it would be nice to not call tag_ops->disconnect(ds) where we didn't
36 previously call tag_ops->connect(ds). We didn't have this problem before
37 because the tag_ops connection operations passed the entire dst before,
38 and this is more fine grained now. To solve the error rewind case using
39 the new API, we have to create yet one more cross-chip notifier for
40 disconnection, and stay connected with the old tag protocol to all the
41 switches in the tree until we've succeeded to connect with the new one
42 as well. So if something fails half way, the whole tree is still
43 connected to the old tagger. But there may still be leaks if the tagger
44 fails to connect to the 2nd out of 3 switches in a tree: somebody needs
45 to tell the tagger to disconnect from the first switch. Nothing comes
46 for free, and this was previously handled privately by the tagging
47 protocol driver before, but now we need to emit a disconnect cross-chip
48 notifier for that, because DSA has to take care of the unwind path. We
49 assume that the tagging protocol has connected to a switch if it has set
50 ds->tagger_data to something, otherwise we avoid calling its
51 disconnection method in the error rewind path.
52
53 The rest of the changes are in the tagging protocol drivers, and have to
54 do with the replacement of dst with ds. The iteration is removed and the
55 error unwind path is simplified, as mentioned above.
56
57 Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
58 Signed-off-by: David S. Miller <davem@davemloft.net>
59 ---
60 include/net/dsa.h | 5 ++--
61 net/dsa/dsa2.c | 44 +++++++++++++-----------------
62 net/dsa/dsa_priv.h | 1 +
63 net/dsa/switch.c | 52 ++++++++++++++++++++++++++++++++---
64 net/dsa/tag_ocelot_8021q.c | 53 +++++++++++-------------------------
65 net/dsa/tag_sja1105.c | 67 ++++++++++++++++------------------------------
66 6 files changed, 109 insertions(+), 113 deletions(-)
67
68 diff --git a/include/net/dsa.h b/include/net/dsa.h
69 index 64d71968aa91a..f16959444ae12 100644
70 --- a/include/net/dsa.h
71 +++ b/include/net/dsa.h
72 @@ -82,15 +82,14 @@ enum dsa_tag_protocol {
73 };
74
75 struct dsa_switch;
76 -struct dsa_switch_tree;
77
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,
82 int *offset);
83 - int (*connect)(struct dsa_switch_tree *dst);
84 - void (*disconnect)(struct dsa_switch_tree *dst);
85 + int (*connect)(struct dsa_switch *ds);
86 + void (*disconnect)(struct dsa_switch *ds);
87 unsigned int needed_headroom;
88 unsigned int needed_tailroom;
89 const char *name;
90 diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c
91 index cf65661686209..c18b22c0bf55e 100644
92 --- a/net/dsa/dsa2.c
93 +++ b/net/dsa/dsa2.c
94 @@ -248,12 +248,8 @@ static struct dsa_switch_tree *dsa_tree_alloc(int index)
95
96 static void dsa_tree_free(struct dsa_switch_tree *dst)
97 {
98 - if (dst->tag_ops) {
99 - if (dst->tag_ops->disconnect)
100 - dst->tag_ops->disconnect(dst);
101 -
102 + if (dst->tag_ops)
103 dsa_tag_driver_put(dst->tag_ops);
104 - }
105 list_del(&dst->list);
106 kfree(dst);
107 }
108 @@ -841,17 +837,29 @@ static int dsa_switch_setup_tag_protocol(struct dsa_switch *ds)
109 }
110
111 connect:
112 + if (tag_ops->connect) {
113 + err = tag_ops->connect(ds);
114 + if (err)
115 + return err;
116 + }
117 +
118 if (ds->ops->connect_tag_protocol) {
119 err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
120 if (err) {
121 dev_err(ds->dev,
122 "Unable to connect to tag protocol \"%s\": %pe\n",
123 tag_ops->name, ERR_PTR(err));
124 - return err;
125 + goto disconnect;
126 }
127 }
128
129 return 0;
130 +
131 +disconnect:
132 + if (tag_ops->disconnect)
133 + tag_ops->disconnect(ds);
134 +
135 + return err;
136 }
137
138 static int dsa_switch_setup(struct dsa_switch *ds)
139 @@ -1160,13 +1168,6 @@ static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
140
141 dst->tag_ops = tag_ops;
142
143 - /* Notify the new tagger about the connection to this tree */
144 - if (tag_ops->connect) {
145 - err = tag_ops->connect(dst);
146 - if (err)
147 - goto out_revert;
148 - }
149 -
150 /* Notify the switches from this tree about the connection
151 * to the new tagger
152 */
153 @@ -1176,16 +1177,14 @@ static int dsa_tree_bind_tag_proto(struct dsa_switch_tree *dst,
154 goto out_disconnect;
155
156 /* Notify the old tagger about the disconnection from this tree */
157 - if (old_tag_ops->disconnect)
158 - old_tag_ops->disconnect(dst);
159 + info.tag_ops = old_tag_ops;
160 + dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
161
162 return 0;
163
164 out_disconnect:
165 - /* Revert the new tagger's connection to this tree */
166 - if (tag_ops->disconnect)
167 - tag_ops->disconnect(dst);
168 -out_revert:
169 + info.tag_ops = tag_ops;
170 + dsa_tree_notify(dst, DSA_NOTIFIER_TAG_PROTO_DISCONNECT, &info);
171 dst->tag_ops = old_tag_ops;
172
173 return err;
174 @@ -1318,7 +1317,6 @@ static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
175 struct dsa_switch_tree *dst = ds->dst;
176 const struct dsa_device_ops *tag_ops;
177 enum dsa_tag_protocol default_proto;
178 - int err;
179
180 /* Find out which protocol the switch would prefer. */
181 default_proto = dsa_get_tag_protocol(dp, master);
182 @@ -1366,12 +1364,6 @@ static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master,
183 */
184 dsa_tag_driver_put(tag_ops);
185 } else {
186 - if (tag_ops->connect) {
187 - err = tag_ops->connect(dst);
188 - if (err)
189 - return err;
190 - }
191 -
192 dst->tag_ops = tag_ops;
193 }
194
195 diff --git a/net/dsa/dsa_priv.h b/net/dsa/dsa_priv.h
196 index 0db2b26b0c83c..edfaae7b59672 100644
197 --- a/net/dsa/dsa_priv.h
198 +++ b/net/dsa/dsa_priv.h
199 @@ -38,6 +38,7 @@ enum {
200 DSA_NOTIFIER_MTU,
201 DSA_NOTIFIER_TAG_PROTO,
202 DSA_NOTIFIER_TAG_PROTO_CONNECT,
203 + DSA_NOTIFIER_TAG_PROTO_DISCONNECT,
204 DSA_NOTIFIER_MRP_ADD,
205 DSA_NOTIFIER_MRP_DEL,
206 DSA_NOTIFIER_MRP_ADD_RING_ROLE,
207 diff --git a/net/dsa/switch.c b/net/dsa/switch.c
208 index 06948f5368296..393f2d8a860a9 100644
209 --- a/net/dsa/switch.c
210 +++ b/net/dsa/switch.c
211 @@ -647,15 +647,58 @@ static int dsa_switch_change_tag_proto(struct dsa_switch *ds,
212 return 0;
213 }
214
215 -static int dsa_switch_connect_tag_proto(struct dsa_switch *ds,
216 - struct dsa_notifier_tag_proto_info *info)
217 +/* We use the same cross-chip notifiers to inform both the tagger side, as well
218 + * as the switch side, of connection and disconnection events.
219 + * Since ds->tagger_data is owned by the tagger, it isn't a hard error if the
220 + * switch side doesn't support connecting to this tagger, and therefore, the
221 + * fact that we don't disconnect the tagger side doesn't constitute a memory
222 + * leak: the tagger will still operate with persistent per-switch memory, just
223 + * with the switch side unconnected to it. What does constitute a hard error is
224 + * when the switch side supports connecting but fails.
225 + */
226 +static int
227 +dsa_switch_connect_tag_proto(struct dsa_switch *ds,
228 + struct dsa_notifier_tag_proto_info *info)
229 {
230 const struct dsa_device_ops *tag_ops = info->tag_ops;
231 + int err;
232 +
233 + /* Notify the new tagger about the connection to this switch */
234 + if (tag_ops->connect) {
235 + err = tag_ops->connect(ds);
236 + if (err)
237 + return err;
238 + }
239
240 if (!ds->ops->connect_tag_protocol)
241 return -EOPNOTSUPP;
242
243 - return ds->ops->connect_tag_protocol(ds, tag_ops->proto);
244 + /* Notify the switch about the connection to the new tagger */
245 + err = ds->ops->connect_tag_protocol(ds, tag_ops->proto);
246 + if (err) {
247 + /* Revert the new tagger's connection to this tree */
248 + if (tag_ops->disconnect)
249 + tag_ops->disconnect(ds);
250 + return err;
251 + }
252 +
253 + return 0;
254 +}
255 +
256 +static int
257 +dsa_switch_disconnect_tag_proto(struct dsa_switch *ds,
258 + struct dsa_notifier_tag_proto_info *info)
259 +{
260 + const struct dsa_device_ops *tag_ops = info->tag_ops;
261 +
262 + /* Notify the tagger about the disconnection from this switch */
263 + if (tag_ops->disconnect && ds->tagger_data)
264 + tag_ops->disconnect(ds);
265 +
266 + /* No need to notify the switch, since it shouldn't have any
267 + * resources to tear down
268 + */
269 + return 0;
270 }
271
272 static int dsa_switch_mrp_add(struct dsa_switch *ds,
273 @@ -780,6 +823,9 @@ static int dsa_switch_event(struct notifier_block *nb,
274 case DSA_NOTIFIER_TAG_PROTO_CONNECT:
275 err = dsa_switch_connect_tag_proto(ds, info);
276 break;
277 + case DSA_NOTIFIER_TAG_PROTO_DISCONNECT:
278 + err = dsa_switch_disconnect_tag_proto(ds, info);
279 + break;
280 case DSA_NOTIFIER_MRP_ADD:
281 err = dsa_switch_mrp_add(ds, info);
282 break;
283 --
284 cgit 1.2.3-1.el7
285