Loading...
Note: File does not exist in v3.1.
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
3 *
4 * This module is not a complete tagger implementation. It only provides
5 * primitives for taggers that rely on 802.1Q VLAN tags to use. The
6 * dsa_8021q_netdev_ops is registered for API compliance and not used
7 * directly by callers.
8 */
9#include <linux/if_bridge.h>
10#include <linux/if_vlan.h>
11#include <linux/dsa/8021q.h>
12
13#include "dsa_priv.h"
14
15/* Binary structure of the fake 12-bit VID field (when the TPID is
16 * ETH_P_DSA_8021Q):
17 *
18 * | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
19 * +-----------+-----+-----------------+-----------+-----------------------+
20 * | DIR | SVL | SWITCH_ID | SUBVLAN | PORT |
21 * +-----------+-----+-----------------+-----------+-----------------------+
22 *
23 * DIR - VID[11:10]:
24 * Direction flags.
25 * * 1 (0b01) for RX VLAN,
26 * * 2 (0b10) for TX VLAN.
27 * These values make the special VIDs of 0, 1 and 4095 to be left
28 * unused by this coding scheme.
29 *
30 * SVL/SUBVLAN - { VID[9], VID[5:4] }:
31 * Sub-VLAN encoding. Valid only when DIR indicates an RX VLAN.
32 * * 0 (0b000): Field does not encode a sub-VLAN, either because
33 * received traffic is untagged, PVID-tagged or because a second
34 * VLAN tag is present after this tag and not inside of it.
35 * * 1 (0b001): Received traffic is tagged with a VID value private
36 * to the host. This field encodes the index in the host's lookup
37 * table through which the value of the ingress VLAN ID can be
38 * recovered.
39 * * 2 (0b010): Field encodes a sub-VLAN.
40 * ...
41 * * 7 (0b111): Field encodes a sub-VLAN.
42 * When DIR indicates a TX VLAN, SUBVLAN must be transmitted as zero
43 * (by the host) and ignored on receive (by the switch).
44 *
45 * SWITCH_ID - VID[8:6]:
46 * Index of switch within DSA tree. Must be between 0 and 7.
47 *
48 * PORT - VID[3:0]:
49 * Index of switch port. Must be between 0 and 15.
50 */
51
52#define DSA_8021Q_DIR_SHIFT 10
53#define DSA_8021Q_DIR_MASK GENMASK(11, 10)
54#define DSA_8021Q_DIR(x) (((x) << DSA_8021Q_DIR_SHIFT) & \
55 DSA_8021Q_DIR_MASK)
56#define DSA_8021Q_DIR_RX DSA_8021Q_DIR(1)
57#define DSA_8021Q_DIR_TX DSA_8021Q_DIR(2)
58
59#define DSA_8021Q_SWITCH_ID_SHIFT 6
60#define DSA_8021Q_SWITCH_ID_MASK GENMASK(8, 6)
61#define DSA_8021Q_SWITCH_ID(x) (((x) << DSA_8021Q_SWITCH_ID_SHIFT) & \
62 DSA_8021Q_SWITCH_ID_MASK)
63
64#define DSA_8021Q_SUBVLAN_HI_SHIFT 9
65#define DSA_8021Q_SUBVLAN_HI_MASK GENMASK(9, 9)
66#define DSA_8021Q_SUBVLAN_LO_SHIFT 4
67#define DSA_8021Q_SUBVLAN_LO_MASK GENMASK(4, 3)
68#define DSA_8021Q_SUBVLAN_HI(x) (((x) & GENMASK(2, 2)) >> 2)
69#define DSA_8021Q_SUBVLAN_LO(x) ((x) & GENMASK(1, 0))
70#define DSA_8021Q_SUBVLAN(x) \
71 (((DSA_8021Q_SUBVLAN_LO(x) << DSA_8021Q_SUBVLAN_LO_SHIFT) & \
72 DSA_8021Q_SUBVLAN_LO_MASK) | \
73 ((DSA_8021Q_SUBVLAN_HI(x) << DSA_8021Q_SUBVLAN_HI_SHIFT) & \
74 DSA_8021Q_SUBVLAN_HI_MASK))
75
76#define DSA_8021Q_PORT_SHIFT 0
77#define DSA_8021Q_PORT_MASK GENMASK(3, 0)
78#define DSA_8021Q_PORT(x) (((x) << DSA_8021Q_PORT_SHIFT) & \
79 DSA_8021Q_PORT_MASK)
80
81/* Returns the VID to be inserted into the frame from xmit for switch steering
82 * instructions on egress. Encodes switch ID and port ID.
83 */
84u16 dsa_8021q_tx_vid(struct dsa_switch *ds, int port)
85{
86 return DSA_8021Q_DIR_TX | DSA_8021Q_SWITCH_ID(ds->index) |
87 DSA_8021Q_PORT(port);
88}
89EXPORT_SYMBOL_GPL(dsa_8021q_tx_vid);
90
91/* Returns the VID that will be installed as pvid for this switch port, sent as
92 * tagged egress towards the CPU port and decoded by the rcv function.
93 */
94u16 dsa_8021q_rx_vid(struct dsa_switch *ds, int port)
95{
96 return DSA_8021Q_DIR_RX | DSA_8021Q_SWITCH_ID(ds->index) |
97 DSA_8021Q_PORT(port);
98}
99EXPORT_SYMBOL_GPL(dsa_8021q_rx_vid);
100
101u16 dsa_8021q_rx_vid_subvlan(struct dsa_switch *ds, int port, u16 subvlan)
102{
103 return DSA_8021Q_DIR_RX | DSA_8021Q_SWITCH_ID(ds->index) |
104 DSA_8021Q_PORT(port) | DSA_8021Q_SUBVLAN(subvlan);
105}
106EXPORT_SYMBOL_GPL(dsa_8021q_rx_vid_subvlan);
107
108/* Returns the decoded switch ID from the RX VID. */
109int dsa_8021q_rx_switch_id(u16 vid)
110{
111 return (vid & DSA_8021Q_SWITCH_ID_MASK) >> DSA_8021Q_SWITCH_ID_SHIFT;
112}
113EXPORT_SYMBOL_GPL(dsa_8021q_rx_switch_id);
114
115/* Returns the decoded port ID from the RX VID. */
116int dsa_8021q_rx_source_port(u16 vid)
117{
118 return (vid & DSA_8021Q_PORT_MASK) >> DSA_8021Q_PORT_SHIFT;
119}
120EXPORT_SYMBOL_GPL(dsa_8021q_rx_source_port);
121
122/* Returns the decoded subvlan from the RX VID. */
123u16 dsa_8021q_rx_subvlan(u16 vid)
124{
125 u16 svl_hi, svl_lo;
126
127 svl_hi = (vid & DSA_8021Q_SUBVLAN_HI_MASK) >>
128 DSA_8021Q_SUBVLAN_HI_SHIFT;
129 svl_lo = (vid & DSA_8021Q_SUBVLAN_LO_MASK) >>
130 DSA_8021Q_SUBVLAN_LO_SHIFT;
131
132 return (svl_hi << 2) | svl_lo;
133}
134EXPORT_SYMBOL_GPL(dsa_8021q_rx_subvlan);
135
136bool vid_is_dsa_8021q(u16 vid)
137{
138 return ((vid & DSA_8021Q_DIR_MASK) == DSA_8021Q_DIR_RX ||
139 (vid & DSA_8021Q_DIR_MASK) == DSA_8021Q_DIR_TX);
140}
141EXPORT_SYMBOL_GPL(vid_is_dsa_8021q);
142
143/* If @enabled is true, installs @vid with @flags into the switch port's HW
144 * filter.
145 * If @enabled is false, deletes @vid (ignores @flags) from the port. Had the
146 * user explicitly configured this @vid through the bridge core, then the @vid
147 * is installed again, but this time with the flags from the bridge layer.
148 */
149static int dsa_8021q_vid_apply(struct dsa_switch *ds, int port, u16 vid,
150 u16 flags, bool enabled)
151{
152 struct dsa_port *dp = dsa_to_port(ds, port);
153
154 if (enabled)
155 return dsa_port_vid_add(dp, vid, flags);
156
157 return dsa_port_vid_del(dp, vid);
158}
159
160/* RX VLAN tagging (left) and TX VLAN tagging (right) setup shown for a single
161 * front-panel switch port (here swp0).
162 *
163 * Port identification through VLAN (802.1Q) tags has different requirements
164 * for it to work effectively:
165 * - On RX (ingress from network): each front-panel port must have a pvid
166 * that uniquely identifies it, and the egress of this pvid must be tagged
167 * towards the CPU port, so that software can recover the source port based
168 * on the VID in the frame. But this would only work for standalone ports;
169 * if bridged, this VLAN setup would break autonomous forwarding and would
170 * force all switched traffic to pass through the CPU. So we must also make
171 * the other front-panel ports members of this VID we're adding, albeit
172 * we're not making it their PVID (they'll still have their own).
173 * By the way - just because we're installing the same VID in multiple
174 * switch ports doesn't mean that they'll start to talk to one another, even
175 * while not bridged: the final forwarding decision is still an AND between
176 * the L2 forwarding information (which is limiting forwarding in this case)
177 * and the VLAN-based restrictions (of which there are none in this case,
178 * since all ports are members).
179 * - On TX (ingress from CPU and towards network) we are faced with a problem.
180 * If we were to tag traffic (from within DSA) with the port's pvid, all
181 * would be well, assuming the switch ports were standalone. Frames would
182 * have no choice but to be directed towards the correct front-panel port.
183 * But because we also want the RX VLAN to not break bridging, then
184 * inevitably that means that we have to give them a choice (of what
185 * front-panel port to go out on), and therefore we cannot steer traffic
186 * based on the RX VID. So what we do is simply install one more VID on the
187 * front-panel and CPU ports, and profit off of the fact that steering will
188 * work just by virtue of the fact that there is only one other port that's
189 * a member of the VID we're tagging the traffic with - the desired one.
190 *
191 * So at the end, each front-panel port will have one RX VID (also the PVID),
192 * the RX VID of all other front-panel ports, and one TX VID. Whereas the CPU
193 * port will have the RX and TX VIDs of all front-panel ports, and on top of
194 * that, is also tagged-input and tagged-output (VLAN trunk).
195 *
196 * CPU port CPU port
197 * +-------------+-----+-------------+ +-------------+-----+-------------+
198 * | RX VID | | | | TX VID | | |
199 * | of swp0 | | | | of swp0 | | |
200 * | +-----+ | | +-----+ |
201 * | ^ T | | | Tagged |
202 * | | | | | ingress |
203 * | +-------+---+---+-------+ | | +-----------+ |
204 * | | | | | | | | Untagged |
205 * | | U v U v U v | | v egress |
206 * | +-----+ +-----+ +-----+ +-----+ | | +-----+ +-----+ +-----+ +-----+ |
207 * | | | | | | | | | | | | | | | | | | | |
208 * | |PVID | | | | | | | | | | | | | | | | | |
209 * +-+-----+-+-----+-+-----+-+-----+-+ +-+-----+-+-----+-+-----+-+-----+-+
210 * swp0 swp1 swp2 swp3 swp0 swp1 swp2 swp3
211 */
212int dsa_port_setup_8021q_tagging(struct dsa_switch *ds, int port, bool enabled)
213{
214 int upstream = dsa_upstream_port(ds, port);
215 u16 rx_vid = dsa_8021q_rx_vid(ds, port);
216 u16 tx_vid = dsa_8021q_tx_vid(ds, port);
217 int i, err;
218
219 /* The CPU port is implicitly configured by
220 * configuring the front-panel ports
221 */
222 if (!dsa_is_user_port(ds, port))
223 return 0;
224
225 /* Add this user port's RX VID to the membership list of all others
226 * (including itself). This is so that bridging will not be hindered.
227 * L2 forwarding rules still take precedence when there are no VLAN
228 * restrictions, so there are no concerns about leaking traffic.
229 */
230 for (i = 0; i < ds->num_ports; i++) {
231 u16 flags;
232
233 if (i == upstream)
234 continue;
235 else if (i == port)
236 /* The RX VID is pvid on this port */
237 flags = BRIDGE_VLAN_INFO_UNTAGGED |
238 BRIDGE_VLAN_INFO_PVID;
239 else
240 /* The RX VID is a regular VLAN on all others */
241 flags = BRIDGE_VLAN_INFO_UNTAGGED;
242
243 err = dsa_8021q_vid_apply(ds, i, rx_vid, flags, enabled);
244 if (err) {
245 dev_err(ds->dev, "Failed to apply RX VID %d to port %d: %d\n",
246 rx_vid, port, err);
247 return err;
248 }
249 }
250
251 /* CPU port needs to see this port's RX VID
252 * as tagged egress.
253 */
254 err = dsa_8021q_vid_apply(ds, upstream, rx_vid, 0, enabled);
255 if (err) {
256 dev_err(ds->dev, "Failed to apply RX VID %d to port %d: %d\n",
257 rx_vid, port, err);
258 return err;
259 }
260
261 /* Finally apply the TX VID on this port and on the CPU port */
262 err = dsa_8021q_vid_apply(ds, port, tx_vid, BRIDGE_VLAN_INFO_UNTAGGED,
263 enabled);
264 if (err) {
265 dev_err(ds->dev, "Failed to apply TX VID %d on port %d: %d\n",
266 tx_vid, port, err);
267 return err;
268 }
269 err = dsa_8021q_vid_apply(ds, upstream, tx_vid, 0, enabled);
270 if (err) {
271 dev_err(ds->dev, "Failed to apply TX VID %d on port %d: %d\n",
272 tx_vid, upstream, err);
273 return err;
274 }
275
276 return err;
277}
278EXPORT_SYMBOL_GPL(dsa_port_setup_8021q_tagging);
279
280static int dsa_8021q_crosschip_link_apply(struct dsa_switch *ds, int port,
281 struct dsa_switch *other_ds,
282 int other_port, bool enabled)
283{
284 u16 rx_vid = dsa_8021q_rx_vid(ds, port);
285
286 /* @rx_vid of local @ds port @port goes to @other_port of
287 * @other_ds
288 */
289 return dsa_8021q_vid_apply(other_ds, other_port, rx_vid,
290 BRIDGE_VLAN_INFO_UNTAGGED, enabled);
291}
292
293static int dsa_8021q_crosschip_link_add(struct dsa_switch *ds, int port,
294 struct dsa_switch *other_ds,
295 int other_port,
296 struct list_head *crosschip_links)
297{
298 struct dsa_8021q_crosschip_link *c;
299
300 list_for_each_entry(c, crosschip_links, list) {
301 if (c->port == port && c->other_ds == other_ds &&
302 c->other_port == other_port) {
303 refcount_inc(&c->refcount);
304 return 0;
305 }
306 }
307
308 dev_dbg(ds->dev, "adding crosschip link from port %d to %s port %d\n",
309 port, dev_name(other_ds->dev), other_port);
310
311 c = kzalloc(sizeof(*c), GFP_KERNEL);
312 if (!c)
313 return -ENOMEM;
314
315 c->port = port;
316 c->other_ds = other_ds;
317 c->other_port = other_port;
318 refcount_set(&c->refcount, 1);
319
320 list_add(&c->list, crosschip_links);
321
322 return 0;
323}
324
325static void dsa_8021q_crosschip_link_del(struct dsa_switch *ds,
326 struct dsa_8021q_crosschip_link *c,
327 struct list_head *crosschip_links,
328 bool *keep)
329{
330 *keep = !refcount_dec_and_test(&c->refcount);
331
332 if (*keep)
333 return;
334
335 dev_dbg(ds->dev,
336 "deleting crosschip link from port %d to %s port %d\n",
337 c->port, dev_name(c->other_ds->dev), c->other_port);
338
339 list_del(&c->list);
340 kfree(c);
341}
342
343/* Make traffic from local port @port be received by remote port @other_port.
344 * This means that our @rx_vid needs to be installed on @other_ds's upstream
345 * and user ports. The user ports should be egress-untagged so that they can
346 * pop the dsa_8021q VLAN. But the @other_upstream can be either egress-tagged
347 * or untagged: it doesn't matter, since it should never egress a frame having
348 * our @rx_vid.
349 */
350int dsa_8021q_crosschip_bridge_join(struct dsa_switch *ds, int port,
351 struct dsa_switch *other_ds,
352 int other_port,
353 struct list_head *crosschip_links)
354{
355 /* @other_upstream is how @other_ds reaches us. If we are part
356 * of disjoint trees, then we are probably connected through
357 * our CPU ports. If we're part of the same tree though, we should
358 * probably use dsa_towards_port.
359 */
360 int other_upstream = dsa_upstream_port(other_ds, other_port);
361 int rc;
362
363 rc = dsa_8021q_crosschip_link_add(ds, port, other_ds,
364 other_port, crosschip_links);
365 if (rc)
366 return rc;
367
368 rc = dsa_8021q_crosschip_link_apply(ds, port, other_ds,
369 other_port, true);
370 if (rc)
371 return rc;
372
373 rc = dsa_8021q_crosschip_link_add(ds, port, other_ds,
374 other_upstream,
375 crosschip_links);
376 if (rc)
377 return rc;
378
379 return dsa_8021q_crosschip_link_apply(ds, port, other_ds,
380 other_upstream, true);
381}
382EXPORT_SYMBOL_GPL(dsa_8021q_crosschip_bridge_join);
383
384int dsa_8021q_crosschip_bridge_leave(struct dsa_switch *ds, int port,
385 struct dsa_switch *other_ds,
386 int other_port,
387 struct list_head *crosschip_links)
388{
389 int other_upstream = dsa_upstream_port(other_ds, other_port);
390 struct dsa_8021q_crosschip_link *c, *n;
391
392 list_for_each_entry_safe(c, n, crosschip_links, list) {
393 if (c->port == port && c->other_ds == other_ds &&
394 (c->other_port == other_port ||
395 c->other_port == other_upstream)) {
396 struct dsa_switch *other_ds = c->other_ds;
397 int other_port = c->other_port;
398 bool keep;
399 int rc;
400
401 dsa_8021q_crosschip_link_del(ds, c, crosschip_links,
402 &keep);
403 if (keep)
404 continue;
405
406 rc = dsa_8021q_crosschip_link_apply(ds, port,
407 other_ds,
408 other_port,
409 false);
410 if (rc)
411 return rc;
412 }
413 }
414
415 return 0;
416}
417EXPORT_SYMBOL_GPL(dsa_8021q_crosschip_bridge_leave);
418
419struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev,
420 u16 tpid, u16 tci)
421{
422 /* skb->data points at skb_mac_header, which
423 * is fine for vlan_insert_tag.
424 */
425 return vlan_insert_tag(skb, htons(tpid), tci);
426}
427EXPORT_SYMBOL_GPL(dsa_8021q_xmit);
428
429MODULE_LICENSE("GPL v2");