Linux Audio

Check our new training course

Loading...
v6.13.7
  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.
 
 
  6 */
 
  7#include <linux/if_vlan.h>
  8#include <linux/dsa/8021q.h>
  9
 10#include "port.h"
 11#include "switch.h"
 12#include "tag.h"
 13#include "tag_8021q.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 * |    RSV    | VBID|    SWITCH_ID    |   VBID    |          PORT         |
 21 * +-----------+-----+-----------------+-----------+-----------------------+
 22 *
 23 * RSV - VID[11:10]:
 24 *	Reserved. Must be set to 3 (0b11).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 25 *
 26 * SWITCH_ID - VID[8:6]:
 27 *	Index of switch within DSA tree. Must be between 0 and 7.
 28 *
 29 * VBID - { VID[9], VID[5:4] }:
 30 *	Virtual bridge ID. If between 1 and 7, packet targets the broadcast
 31 *	domain of a bridge. If transmitted as zero, packet targets a single
 32 *	port.
 33 *
 34 * PORT - VID[3:0]:
 35 *	Index of switch port. Must be between 0 and 15.
 36 */
 37
 38#define DSA_8021Q_RSV_VAL		3
 39#define DSA_8021Q_RSV_SHIFT		10
 40#define DSA_8021Q_RSV_MASK		GENMASK(11, 10)
 41#define DSA_8021Q_RSV			((DSA_8021Q_RSV_VAL << DSA_8021Q_RSV_SHIFT) & \
 42							       DSA_8021Q_RSV_MASK)
 
 43
 44#define DSA_8021Q_SWITCH_ID_SHIFT	6
 45#define DSA_8021Q_SWITCH_ID_MASK	GENMASK(8, 6)
 46#define DSA_8021Q_SWITCH_ID(x)		(((x) << DSA_8021Q_SWITCH_ID_SHIFT) & \
 47						 DSA_8021Q_SWITCH_ID_MASK)
 48
 49#define DSA_8021Q_VBID_HI_SHIFT		9
 50#define DSA_8021Q_VBID_HI_MASK		GENMASK(9, 9)
 51#define DSA_8021Q_VBID_LO_SHIFT		4
 52#define DSA_8021Q_VBID_LO_MASK		GENMASK(5, 4)
 53#define DSA_8021Q_VBID_HI(x)		(((x) & GENMASK(2, 2)) >> 2)
 54#define DSA_8021Q_VBID_LO(x)		((x) & GENMASK(1, 0))
 55#define DSA_8021Q_VBID(x)		\
 56		(((DSA_8021Q_VBID_LO(x) << DSA_8021Q_VBID_LO_SHIFT) & \
 57		  DSA_8021Q_VBID_LO_MASK) | \
 58		 ((DSA_8021Q_VBID_HI(x) << DSA_8021Q_VBID_HI_SHIFT) & \
 59		  DSA_8021Q_VBID_HI_MASK))
 60
 61#define DSA_8021Q_PORT_SHIFT		0
 62#define DSA_8021Q_PORT_MASK		GENMASK(3, 0)
 63#define DSA_8021Q_PORT(x)		(((x) << DSA_8021Q_PORT_SHIFT) & \
 64						 DSA_8021Q_PORT_MASK)
 65
 66struct dsa_tag_8021q_vlan {
 67	struct list_head list;
 68	int port;
 69	u16 vid;
 70	refcount_t refcount;
 71};
 72
 73struct dsa_8021q_context {
 74	struct dsa_switch *ds;
 75	struct list_head vlans;
 76	/* EtherType of RX VID, used for filtering on conduit interface */
 77	__be16 proto;
 78};
 79
 80u16 dsa_tag_8021q_bridge_vid(unsigned int bridge_num)
 81{
 82	/* The VBID value of 0 is reserved for precise TX, but it is also
 83	 * reserved/invalid for the bridge_num, so all is well.
 84	 */
 85	return DSA_8021Q_RSV | DSA_8021Q_VBID(bridge_num);
 86}
 87EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_vid);
 88
 89/* Returns the VID that will be installed as pvid for this switch port, sent as
 90 * tagged egress towards the CPU port and decoded by the rcv function.
 91 */
 92u16 dsa_tag_8021q_standalone_vid(const struct dsa_port *dp)
 93{
 94	return DSA_8021Q_RSV | DSA_8021Q_SWITCH_ID(dp->ds->index) |
 95	       DSA_8021Q_PORT(dp->index);
 96}
 97EXPORT_SYMBOL_GPL(dsa_tag_8021q_standalone_vid);
 
 
 
 
 
 
 
 98
 99/* Returns the decoded switch ID from the RX VID. */
100int dsa_8021q_rx_switch_id(u16 vid)
101{
102	return (vid & DSA_8021Q_SWITCH_ID_MASK) >> DSA_8021Q_SWITCH_ID_SHIFT;
103}
104EXPORT_SYMBOL_GPL(dsa_8021q_rx_switch_id);
105
106/* Returns the decoded port ID from the RX VID. */
107int dsa_8021q_rx_source_port(u16 vid)
108{
109	return (vid & DSA_8021Q_PORT_MASK) >> DSA_8021Q_PORT_SHIFT;
110}
111EXPORT_SYMBOL_GPL(dsa_8021q_rx_source_port);
112
113/* Returns the decoded VBID from the RX VID. */
114static int dsa_tag_8021q_rx_vbid(u16 vid)
115{
116	u16 vbid_hi = (vid & DSA_8021Q_VBID_HI_MASK) >> DSA_8021Q_VBID_HI_SHIFT;
117	u16 vbid_lo = (vid & DSA_8021Q_VBID_LO_MASK) >> DSA_8021Q_VBID_LO_SHIFT;
118
119	return (vbid_hi << 2) | vbid_lo;
120}
121
122bool vid_is_dsa_8021q(u16 vid)
123{
124	u16 rsv = (vid & DSA_8021Q_RSV_MASK) >> DSA_8021Q_RSV_SHIFT;
 
125
126	return rsv == DSA_8021Q_RSV_VAL;
127}
128EXPORT_SYMBOL_GPL(vid_is_dsa_8021q);
129
130static struct dsa_tag_8021q_vlan *
131dsa_tag_8021q_vlan_find(struct dsa_8021q_context *ctx, int port, u16 vid)
132{
133	struct dsa_tag_8021q_vlan *v;
134
135	list_for_each_entry(v, &ctx->vlans, list)
136		if (v->vid == vid && v->port == port)
137			return v;
138
139	return NULL;
140}
 
141
142static int dsa_port_do_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid,
143					  u16 flags)
144{
145	struct dsa_8021q_context *ctx = dp->ds->tag_8021q_ctx;
146	struct dsa_switch *ds = dp->ds;
147	struct dsa_tag_8021q_vlan *v;
148	int port = dp->index;
149	int err;
150
151	/* No need to bother with refcounting for user ports */
152	if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
153		return ds->ops->tag_8021q_vlan_add(ds, port, vid, flags);
154
155	v = dsa_tag_8021q_vlan_find(ctx, port, vid);
156	if (v) {
157		refcount_inc(&v->refcount);
158		return 0;
159	}
160
161	v = kzalloc(sizeof(*v), GFP_KERNEL);
162	if (!v)
163		return -ENOMEM;
164
165	err = ds->ops->tag_8021q_vlan_add(ds, port, vid, flags);
166	if (err) {
167		kfree(v);
168		return err;
169	}
170
171	v->vid = vid;
172	v->port = port;
173	refcount_set(&v->refcount, 1);
174	list_add_tail(&v->list, &ctx->vlans);
175
176	return 0;
177}
 
178
179static int dsa_port_do_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid)
180{
181	struct dsa_8021q_context *ctx = dp->ds->tag_8021q_ctx;
182	struct dsa_switch *ds = dp->ds;
183	struct dsa_tag_8021q_vlan *v;
184	int port = dp->index;
185	int err;
186
187	/* No need to bother with refcounting for user ports */
188	if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
189		return ds->ops->tag_8021q_vlan_del(ds, port, vid);
190
191	v = dsa_tag_8021q_vlan_find(ctx, port, vid);
192	if (!v)
193		return -ENOENT;
194
195	if (!refcount_dec_and_test(&v->refcount))
196		return 0;
197
198	err = ds->ops->tag_8021q_vlan_del(ds, port, vid);
199	if (err) {
200		refcount_inc(&v->refcount);
201		return err;
202	}
203
204	list_del(&v->list);
205	kfree(v);
206
207	return 0;
208}
209
210static bool
211dsa_port_tag_8021q_vlan_match(struct dsa_port *dp,
212			      struct dsa_notifier_tag_8021q_vlan_info *info)
213{
214	return dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp) || dp == info->dp;
215}
 
216
217int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
218				  struct dsa_notifier_tag_8021q_vlan_info *info)
 
 
 
 
 
 
219{
220	struct dsa_port *dp;
221	int err;
222
223	/* Since we use dsa_broadcast(), there might be other switches in other
224	 * trees which don't support tag_8021q, so don't return an error.
225	 * Or they might even support tag_8021q but have not registered yet to
226	 * use it (maybe they use another tagger currently).
227	 */
228	if (!ds->ops->tag_8021q_vlan_add || !ds->tag_8021q_ctx)
229		return 0;
230
231	dsa_switch_for_each_port(dp, ds) {
232		if (dsa_port_tag_8021q_vlan_match(dp, info)) {
233			u16 flags = 0;
234
235			if (dsa_port_is_user(dp))
236				flags |= BRIDGE_VLAN_INFO_UNTAGGED |
237					 BRIDGE_VLAN_INFO_PVID;
238
239			err = dsa_port_do_tag_8021q_vlan_add(dp, info->vid,
240							     flags);
241			if (err)
242				return err;
243		}
244	}
245
246	return 0;
247}
248
249int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
250				  struct dsa_notifier_tag_8021q_vlan_info *info)
251{
252	struct dsa_port *dp;
253	int err;
254
255	if (!ds->ops->tag_8021q_vlan_del || !ds->tag_8021q_ctx)
256		return 0;
257
258	dsa_switch_for_each_port(dp, ds) {
259		if (dsa_port_tag_8021q_vlan_match(dp, info)) {
260			err = dsa_port_do_tag_8021q_vlan_del(dp, info->vid);
261			if (err)
262				return err;
263		}
264	}
265
266	return 0;
267}
268
269/* There are 2 ways of offloading tag_8021q VLANs.
270 *
271 * One is to use a hardware TCAM to push the port's standalone VLAN into the
272 * frame when forwarding it to the CPU, as an egress modification rule on the
273 * CPU port. This is preferable because it has no side effects for the
274 * autonomous forwarding path, and accomplishes tag_8021q's primary goal of
275 * identifying the source port of each packet based on VLAN ID.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276 *
277 * The other is to commit the tag_8021q VLAN as a PVID to the VLAN table, and
278 * to configure the port as VLAN-unaware. This is less preferable because
279 * unique source port identification can only be done for standalone ports;
280 * under a VLAN-unaware bridge, all ports share the same tag_8021q VLAN as
281 * PVID, and under a VLAN-aware bridge, packets received by software will not
282 * have tag_8021q VLANs appended, just bridge VLANs.
283 *
284 * For tag_8021q implementations of the second type, this method is used to
285 * replace the standalone tag_8021q VLAN of a port with the tag_8021q VLAN to
286 * be used for VLAN-unaware bridging.
 
 
 
 
 
 
 
 
 
 
 
 
287 */
288int dsa_tag_8021q_bridge_join(struct dsa_switch *ds, int port,
289			      struct dsa_bridge bridge, bool *tx_fwd_offload,
290			      struct netlink_ext_ack *extack)
291{
292	struct dsa_port *dp = dsa_to_port(ds, port);
293	u16 standalone_vid, bridge_vid;
294	int err;
 
295
296	/* Delete the standalone VLAN of the port and replace it with a
297	 * bridging VLAN
298	 */
299	standalone_vid = dsa_tag_8021q_standalone_vid(dp);
300	bridge_vid = dsa_tag_8021q_bridge_vid(bridge.num);
301
302	err = dsa_port_tag_8021q_vlan_add(dp, bridge_vid, true);
303	if (err)
304		return err;
305
306	dsa_port_tag_8021q_vlan_del(dp, standalone_vid, false);
307
308	*tx_fwd_offload = true;
309
310	return 0;
311}
312EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_join);
 
 
 
 
313
314void dsa_tag_8021q_bridge_leave(struct dsa_switch *ds, int port,
315				struct dsa_bridge bridge)
316{
317	struct dsa_port *dp = dsa_to_port(ds, port);
318	u16 standalone_vid, bridge_vid;
319	int err;
 
 
 
 
 
 
 
 
 
 
 
 
320
321	/* Delete the bridging VLAN of the port and replace it with a
322	 * standalone VLAN
323	 */
324	standalone_vid = dsa_tag_8021q_standalone_vid(dp);
325	bridge_vid = dsa_tag_8021q_bridge_vid(bridge.num);
326
327	err = dsa_port_tag_8021q_vlan_add(dp, standalone_vid, false);
328	if (err) {
329		dev_err(ds->dev,
330			"Failed to delete tag_8021q standalone VLAN %d from port %d: %pe\n",
331			standalone_vid, port, ERR_PTR(err));
 
332	}
333
334	dsa_port_tag_8021q_vlan_del(dp, bridge_vid, true);
335}
336EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_leave);
337
338/* Set up a port's standalone tag_8021q VLAN */
339static int dsa_tag_8021q_port_setup(struct dsa_switch *ds, int port)
340{
341	struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
342	struct dsa_port *dp = dsa_to_port(ds, port);
343	u16 vid = dsa_tag_8021q_standalone_vid(dp);
344	struct net_device *conduit;
345	int err;
346
347	/* The CPU port is implicitly configured by
348	 * configuring the front-panel ports
349	 */
350	if (!dsa_port_is_user(dp))
351		return 0;
352
353	conduit = dsa_port_to_conduit(dp);
 
 
 
 
354
355	err = dsa_port_tag_8021q_vlan_add(dp, vid, false);
 
 
356	if (err) {
357		dev_err(ds->dev,
358			"Failed to apply standalone VID %d to port %d: %pe\n",
359			vid, port, ERR_PTR(err));
 
 
 
 
 
 
 
360		return err;
361	}
362
363	/* Add the VLAN to the conduit's RX filter. */
364	vlan_vid_add(conduit, ctx->proto, vid);
365
366	return err;
367}
368
369static void dsa_tag_8021q_port_teardown(struct dsa_switch *ds, int port)
370{
371	struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
372	struct dsa_port *dp = dsa_to_port(ds, port);
373	u16 vid = dsa_tag_8021q_standalone_vid(dp);
374	struct net_device *conduit;
375
376	/* The CPU port is implicitly configured by
377	 * configuring the front-panel ports
378	 */
379	if (!dsa_port_is_user(dp))
380		return;
381
382	conduit = dsa_port_to_conduit(dp);
383
384	dsa_port_tag_8021q_vlan_del(dp, vid, false);
385
386	vlan_vid_del(conduit, ctx->proto, vid);
387}
388
389static int dsa_tag_8021q_setup(struct dsa_switch *ds)
390{
391	int err, port;
392
393	ASSERT_RTNL();
394
395	for (port = 0; port < ds->num_ports; port++) {
396		err = dsa_tag_8021q_port_setup(ds, port);
397		if (err < 0) {
398			dev_err(ds->dev,
399				"Failed to setup VLAN tagging for port %d: %pe\n",
400				port, ERR_PTR(err));
401			return err;
402		}
403	}
404
405	return 0;
406}
 
407
408static void dsa_tag_8021q_teardown(struct dsa_switch *ds)
 
 
 
409{
410	int port;
411
412	ASSERT_RTNL();
413
414	for (port = 0; port < ds->num_ports; port++)
415		dsa_tag_8021q_port_teardown(ds, port);
 
 
 
416}
417
418int dsa_tag_8021q_register(struct dsa_switch *ds, __be16 proto)
 
 
419{
420	struct dsa_8021q_context *ctx;
421	int err;
422
423	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
424	if (!ctx)
425		return -ENOMEM;
 
 
 
 
426
427	ctx->proto = proto;
428	ctx->ds = ds;
 
429
430	INIT_LIST_HEAD(&ctx->vlans);
 
 
431
432	ds->tag_8021q_ctx = ctx;
 
 
 
433
434	err = dsa_tag_8021q_setup(ds);
435	if (err)
436		goto err_free;
437
438	return 0;
439
440err_free:
441	kfree(ctx);
442	return err;
443}
444EXPORT_SYMBOL_GPL(dsa_tag_8021q_register);
445
446void dsa_tag_8021q_unregister(struct dsa_switch *ds)
 
 
447{
448	struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
449	struct dsa_tag_8021q_vlan *v, *n;
450
451	dsa_tag_8021q_teardown(ds);
 
452
453	list_for_each_entry_safe(v, n, &ctx->vlans, list) {
454		list_del(&v->list);
455		kfree(v);
456	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457
458	ds->tag_8021q_ctx = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
459
460	kfree(ctx);
461}
462EXPORT_SYMBOL_GPL(dsa_tag_8021q_unregister);
463
464struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev,
465			       u16 tpid, u16 tci)
466{
467	/* skb->data points at the MAC header, which is fine
468	 * for vlan_insert_tag().
469	 */
470	return vlan_insert_tag(skb, htons(tpid), tci);
471}
472EXPORT_SYMBOL_GPL(dsa_8021q_xmit);
473
474static struct net_device *
475dsa_tag_8021q_find_port_by_vbid(struct net_device *conduit, int vbid)
476{
477	struct dsa_port *cpu_dp = conduit->dsa_ptr;
478	struct dsa_switch_tree *dst = cpu_dp->dst;
479	struct dsa_port *dp;
480
481	if (WARN_ON(!vbid))
482		return NULL;
483
484	dsa_tree_for_each_user_port(dp, dst) {
485		if (!dp->bridge)
486			continue;
487
488		if (dp->stp_state != BR_STATE_LEARNING &&
489		    dp->stp_state != BR_STATE_FORWARDING)
490			continue;
491
492		if (dp->cpu_dp != cpu_dp)
493			continue;
494
495		if (dsa_port_bridge_num_get(dp) == vbid)
496			return dp->user;
497	}
498
499	return NULL;
500}
501
502struct net_device *dsa_tag_8021q_find_user(struct net_device *conduit,
503					   int source_port, int switch_id,
504					   int vid, int vbid)
505{
506	/* Always prefer precise source port information, if available */
507	if (source_port != -1 && switch_id != -1)
508		return dsa_conduit_find_user(conduit, switch_id, source_port);
509	else if (vbid >= 1)
510		return dsa_tag_8021q_find_port_by_vbid(conduit, vbid);
511
512	return dsa_find_designated_bridge_port_by_vid(conduit, vid);
513}
514EXPORT_SYMBOL_GPL(dsa_tag_8021q_find_user);
515
516/**
517 * dsa_8021q_rcv - Decode source information from tag_8021q header
518 * @skb: RX socket buffer
519 * @source_port: pointer to storage for precise source port information.
520 *	If this is known already from outside tag_8021q, the pre-initialized
521 *	value is preserved. If not known, pass -1.
522 * @switch_id: similar to source_port.
523 * @vbid: pointer to storage for imprecise bridge ID. Must be pre-initialized
524 *	with -1. If a positive value is returned, the source_port and switch_id
525 *	are invalid.
526 * @vid: pointer to storage for original VID, in case tag_8021q decoding failed.
527 *
528 * If the packet has a tag_8021q header, decode it and set @source_port,
529 * @switch_id and @vbid, and strip the header. Otherwise set @vid and keep the
530 * header in the hwaccel area of the packet.
531 */
532void dsa_8021q_rcv(struct sk_buff *skb, int *source_port, int *switch_id,
533		   int *vbid, int *vid)
534{
535	int tmp_source_port, tmp_switch_id, tmp_vbid;
536	__be16 vlan_proto;
537	u16 tmp_vid, tci;
538
 
539	if (skb_vlan_tag_present(skb)) {
540		vlan_proto = skb->vlan_proto;
541		tci = skb_vlan_tag_get(skb);
542		__vlan_hwaccel_clear_tag(skb);
543	} else {
544		struct vlan_ethhdr *hdr = vlan_eth_hdr(skb);
545
546		vlan_proto = hdr->h_vlan_proto;
547		skb_push_rcsum(skb, ETH_HLEN);
548		__skb_vlan_pop(skb, &tci);
549		skb_pull_rcsum(skb, ETH_HLEN);
550	}
551
552	tmp_vid = tci & VLAN_VID_MASK;
553	if (!vid_is_dsa_8021q(tmp_vid)) {
554		/* Not a tag_8021q frame, so return the VID to the
555		 * caller for further processing, and put the tag back
556		 */
557		if (vid)
558			*vid = tmp_vid;
559
560		__vlan_hwaccel_put_tag(skb, vlan_proto, tci);
561
562		return;
563	}
 
564
565	tmp_source_port = dsa_8021q_rx_source_port(tmp_vid);
566	tmp_switch_id = dsa_8021q_rx_switch_id(tmp_vid);
567	tmp_vbid = dsa_tag_8021q_rx_vbid(tmp_vid);
568
569	/* Precise source port information is unknown when receiving from a
570	 * VLAN-unaware bridging domain, and tmp_source_port and tmp_switch_id
571	 * are zeroes in this case.
572	 *
573	 * Preserve the source information from hardware-specific mechanisms,
574	 * if available. This allows us to not overwrite a valid source port
575	 * and switch ID with less precise values.
576	 */
577	if (tmp_vbid == 0 && *source_port == -1)
578		*source_port = tmp_source_port;
579	if (tmp_vbid == 0 && *switch_id == -1)
580		*switch_id = tmp_switch_id;
581
582	if (vbid)
583		*vbid = tmp_vbid;
584
 
 
 
585	skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
586	return;
587}
588EXPORT_SYMBOL_GPL(dsa_8021q_rcv);
v5.14.15
  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(5, 4)
 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_rxvlan(u16 vid)
 
137{
138	return (vid & DSA_8021Q_DIR_MASK) == DSA_8021Q_DIR_RX;
 
 
 
 
 
 
139}
140EXPORT_SYMBOL_GPL(vid_is_dsa_8021q_rxvlan);
141
142bool vid_is_dsa_8021q_txvlan(u16 vid)
 
143{
144	return (vid & DSA_8021Q_DIR_MASK) == DSA_8021Q_DIR_TX;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145}
146EXPORT_SYMBOL_GPL(vid_is_dsa_8021q_txvlan);
147
148bool vid_is_dsa_8021q(u16 vid)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149{
150	return vid_is_dsa_8021q_rxvlan(vid) || vid_is_dsa_8021q_txvlan(vid);
151}
152EXPORT_SYMBOL_GPL(vid_is_dsa_8021q);
153
154/* If @enabled is true, installs @vid with @flags into the switch port's HW
155 * filter.
156 * If @enabled is false, deletes @vid (ignores @flags) from the port. Had the
157 * user explicitly configured this @vid through the bridge core, then the @vid
158 * is installed again, but this time with the flags from the bridge layer.
159 */
160static int dsa_8021q_vid_apply(struct dsa_8021q_context *ctx, int port, u16 vid,
161			       u16 flags, bool enabled)
162{
163	struct dsa_port *dp = dsa_to_port(ctx->ds, port);
 
 
 
 
 
 
 
 
 
164
165	if (enabled)
166		return ctx->ops->vlan_add(ctx->ds, dp->index, vid, flags);
 
 
 
 
 
 
 
 
 
 
 
 
167
168	return ctx->ops->vlan_del(ctx->ds, dp->index, vid);
169}
170
171/* RX VLAN tagging (left) and TX VLAN tagging (right) setup shown for a single
172 * front-panel switch port (here swp0).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173 *
174 * Port identification through VLAN (802.1Q) tags has different requirements
175 * for it to work effectively:
176 *  - On RX (ingress from network): each front-panel port must have a pvid
177 *    that uniquely identifies it, and the egress of this pvid must be tagged
178 *    towards the CPU port, so that software can recover the source port based
179 *    on the VID in the frame. But this would only work for standalone ports;
180 *    if bridged, this VLAN setup would break autonomous forwarding and would
181 *    force all switched traffic to pass through the CPU. So we must also make
182 *    the other front-panel ports members of this VID we're adding, albeit
183 *    we're not making it their PVID (they'll still have their own).
184 *    By the way - just because we're installing the same VID in multiple
185 *    switch ports doesn't mean that they'll start to talk to one another, even
186 *    while not bridged: the final forwarding decision is still an AND between
187 *    the L2 forwarding information (which is limiting forwarding in this case)
188 *    and the VLAN-based restrictions (of which there are none in this case,
189 *    since all ports are members).
190 *  - On TX (ingress from CPU and towards network) we are faced with a problem.
191 *    If we were to tag traffic (from within DSA) with the port's pvid, all
192 *    would be well, assuming the switch ports were standalone. Frames would
193 *    have no choice but to be directed towards the correct front-panel port.
194 *    But because we also want the RX VLAN to not break bridging, then
195 *    inevitably that means that we have to give them a choice (of what
196 *    front-panel port to go out on), and therefore we cannot steer traffic
197 *    based on the RX VID. So what we do is simply install one more VID on the
198 *    front-panel and CPU ports, and profit off of the fact that steering will
199 *    work just by virtue of the fact that there is only one other port that's
200 *    a member of the VID we're tagging the traffic with - the desired one.
201 *
202 * So at the end, each front-panel port will have one RX VID (also the PVID),
203 * the RX VID of all other front-panel ports, and one TX VID. Whereas the CPU
204 * port will have the RX and TX VIDs of all front-panel ports, and on top of
205 * that, is also tagged-input and tagged-output (VLAN trunk).
 
 
206 *
207 *               CPU port                               CPU port
208 * +-------------+-----+-------------+    +-------------+-----+-------------+
209 * |  RX VID     |     |             |    |  TX VID     |     |             |
210 * |  of swp0    |     |             |    |  of swp0    |     |             |
211 * |             +-----+             |    |             +-----+             |
212 * |                ^ T              |    |                | Tagged         |
213 * |                |                |    |                | ingress        |
214 * |    +-------+---+---+-------+    |    |    +-----------+                |
215 * |    |       |       |       |    |    |    | Untagged                   |
216 * |    |     U v     U v     U v    |    |    v egress                     |
217 * | +-----+ +-----+ +-----+ +-----+ |    | +-----+ +-----+ +-----+ +-----+ |
218 * | |     | |     | |     | |     | |    | |     | |     | |     | |     | |
219 * | |PVID | |     | |     | |     | |    | |     | |     | |     | |     | |
220 * +-+-----+-+-----+-+-----+-+-----+-+    +-+-----+-+-----+-+-----+-+-----+-+
221 *   swp0    swp1    swp2    swp3           swp0    swp1    swp2    swp3
222 */
223static int dsa_8021q_setup_port(struct dsa_8021q_context *ctx, int port,
224				bool enabled)
225{
226	int upstream = dsa_upstream_port(ctx->ds, port);
227	u16 rx_vid = dsa_8021q_rx_vid(ctx->ds, port);
228	u16 tx_vid = dsa_8021q_tx_vid(ctx->ds, port);
229	struct net_device *master;
230	int i, err, subvlan;
231
232	/* The CPU port is implicitly configured by
233	 * configuring the front-panel ports
234	 */
235	if (!dsa_is_user_port(ctx->ds, port))
236		return 0;
 
 
 
 
 
 
237
238	master = dsa_to_port(ctx->ds, port)->cpu_dp->master;
239
240	/* Add this user port's RX VID to the membership list of all others
241	 * (including itself). This is so that bridging will not be hindered.
242	 * L2 forwarding rules still take precedence when there are no VLAN
243	 * restrictions, so there are no concerns about leaking traffic.
244	 */
245	for (i = 0; i < ctx->ds->num_ports; i++) {
246		u16 flags;
247
248		if (i == upstream)
249			continue;
250		else if (i == port)
251			/* The RX VID is pvid on this port */
252			flags = BRIDGE_VLAN_INFO_UNTAGGED |
253				BRIDGE_VLAN_INFO_PVID;
254		else
255			/* The RX VID is a regular VLAN on all others */
256			flags = BRIDGE_VLAN_INFO_UNTAGGED;
257
258		err = dsa_8021q_vid_apply(ctx, i, rx_vid, flags, enabled);
259		if (err) {
260			dev_err(ctx->ds->dev,
261				"Failed to apply RX VID %d to port %d: %d\n",
262				rx_vid, port, err);
263			return err;
264		}
265	}
266
267	/* CPU port needs to see this port's RX VID
268	 * as tagged egress.
269	 */
270	err = dsa_8021q_vid_apply(ctx, upstream, rx_vid, 0, enabled);
 
 
 
271	if (err) {
272		dev_err(ctx->ds->dev,
273			"Failed to apply RX VID %d to port %d: %d\n",
274			rx_vid, port, err);
275		return err;
276	}
277
278	/* Add to the master's RX filter not only @rx_vid, but in fact
279	 * the entire subvlan range, just in case this DSA switch might
280	 * want to use sub-VLANs.
 
 
 
 
 
 
 
 
 
 
 
 
281	 */
282	for (subvlan = 0; subvlan < DSA_8021Q_N_SUBVLAN; subvlan++) {
283		u16 vid = dsa_8021q_rx_vid_subvlan(ctx->ds, port, subvlan);
284
285		if (enabled)
286			vlan_vid_add(master, ctx->proto, vid);
287		else
288			vlan_vid_del(master, ctx->proto, vid);
289	}
290
291	/* Finally apply the TX VID on this port and on the CPU port */
292	err = dsa_8021q_vid_apply(ctx, port, tx_vid, BRIDGE_VLAN_INFO_UNTAGGED,
293				  enabled);
294	if (err) {
295		dev_err(ctx->ds->dev,
296			"Failed to apply TX VID %d on port %d: %d\n",
297			tx_vid, port, err);
298		return err;
299	}
300	err = dsa_8021q_vid_apply(ctx, upstream, tx_vid, 0, enabled);
301	if (err) {
302		dev_err(ctx->ds->dev,
303			"Failed to apply TX VID %d on port %d: %d\n",
304			tx_vid, upstream, err);
305		return err;
306	}
 
 
 
307
308	return err;
309}
310
311int dsa_8021q_setup(struct dsa_8021q_context *ctx, bool enabled)
312{
313	int rc, port;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
314
315	ASSERT_RTNL();
316
317	for (port = 0; port < ctx->ds->num_ports; port++) {
318		rc = dsa_8021q_setup_port(ctx, port, enabled);
319		if (rc < 0) {
320			dev_err(ctx->ds->dev,
321				"Failed to setup VLAN tagging for port %d: %d\n",
322				port, rc);
323			return rc;
324		}
325	}
326
327	return 0;
328}
329EXPORT_SYMBOL_GPL(dsa_8021q_setup);
330
331static int dsa_8021q_crosschip_link_apply(struct dsa_8021q_context *ctx,
332					  int port,
333					  struct dsa_8021q_context *other_ctx,
334					  int other_port, bool enabled)
335{
336	u16 rx_vid = dsa_8021q_rx_vid(ctx->ds, port);
 
 
337
338	/* @rx_vid of local @ds port @port goes to @other_port of
339	 * @other_ds
340	 */
341	return dsa_8021q_vid_apply(other_ctx, other_port, rx_vid,
342				   BRIDGE_VLAN_INFO_UNTAGGED, enabled);
343}
344
345static int dsa_8021q_crosschip_link_add(struct dsa_8021q_context *ctx, int port,
346					struct dsa_8021q_context *other_ctx,
347					int other_port)
348{
349	struct dsa_8021q_crosschip_link *c;
 
350
351	list_for_each_entry(c, &ctx->crosschip_links, list) {
352		if (c->port == port && c->other_ctx == other_ctx &&
353		    c->other_port == other_port) {
354			refcount_inc(&c->refcount);
355			return 0;
356		}
357	}
358
359	dev_dbg(ctx->ds->dev,
360		"adding crosschip link from port %d to %s port %d\n",
361		port, dev_name(other_ctx->ds->dev), other_port);
362
363	c = kzalloc(sizeof(*c), GFP_KERNEL);
364	if (!c)
365		return -ENOMEM;
366
367	c->port = port;
368	c->other_ctx = other_ctx;
369	c->other_port = other_port;
370	refcount_set(&c->refcount, 1);
371
372	list_add(&c->list, &ctx->crosschip_links);
 
 
373
374	return 0;
 
 
 
 
375}
 
376
377static void dsa_8021q_crosschip_link_del(struct dsa_8021q_context *ctx,
378					 struct dsa_8021q_crosschip_link *c,
379					 bool *keep)
380{
381	*keep = !refcount_dec_and_test(&c->refcount);
 
382
383	if (*keep)
384		return;
385
386	dev_dbg(ctx->ds->dev,
387		"deleting crosschip link from port %d to %s port %d\n",
388		c->port, dev_name(c->other_ctx->ds->dev), c->other_port);
389
390	list_del(&c->list);
391	kfree(c);
392}
393
394/* Make traffic from local port @port be received by remote port @other_port.
395 * This means that our @rx_vid needs to be installed on @other_ds's upstream
396 * and user ports. The user ports should be egress-untagged so that they can
397 * pop the dsa_8021q VLAN. But the @other_upstream can be either egress-tagged
398 * or untagged: it doesn't matter, since it should never egress a frame having
399 * our @rx_vid.
400 */
401int dsa_8021q_crosschip_bridge_join(struct dsa_8021q_context *ctx, int port,
402				    struct dsa_8021q_context *other_ctx,
403				    int other_port)
404{
405	/* @other_upstream is how @other_ds reaches us. If we are part
406	 * of disjoint trees, then we are probably connected through
407	 * our CPU ports. If we're part of the same tree though, we should
408	 * probably use dsa_towards_port.
409	 */
410	int other_upstream = dsa_upstream_port(other_ctx->ds, other_port);
411	int rc;
412
413	rc = dsa_8021q_crosschip_link_add(ctx, port, other_ctx, other_port);
414	if (rc)
415		return rc;
416
417	rc = dsa_8021q_crosschip_link_apply(ctx, port, other_ctx,
418					    other_port, true);
419	if (rc)
420		return rc;
421
422	rc = dsa_8021q_crosschip_link_add(ctx, port, other_ctx, other_upstream);
423	if (rc)
424		return rc;
425
426	return dsa_8021q_crosschip_link_apply(ctx, port, other_ctx,
427					      other_upstream, true);
428}
429EXPORT_SYMBOL_GPL(dsa_8021q_crosschip_bridge_join);
430
431int dsa_8021q_crosschip_bridge_leave(struct dsa_8021q_context *ctx, int port,
432				     struct dsa_8021q_context *other_ctx,
433				     int other_port)
434{
435	int other_upstream = dsa_upstream_port(other_ctx->ds, other_port);
436	struct dsa_8021q_crosschip_link *c, *n;
437
438	list_for_each_entry_safe(c, n, &ctx->crosschip_links, list) {
439		if (c->port == port && c->other_ctx == other_ctx &&
440		    (c->other_port == other_port ||
441		     c->other_port == other_upstream)) {
442			struct dsa_8021q_context *other_ctx = c->other_ctx;
443			int other_port = c->other_port;
444			bool keep;
445			int rc;
446
447			dsa_8021q_crosschip_link_del(ctx, c, &keep);
448			if (keep)
449				continue;
450
451			rc = dsa_8021q_crosschip_link_apply(ctx, port,
452							    other_ctx,
453							    other_port,
454							    false);
455			if (rc)
456				return rc;
457		}
458	}
459
460	return 0;
461}
462EXPORT_SYMBOL_GPL(dsa_8021q_crosschip_bridge_leave);
463
464struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev,
465			       u16 tpid, u16 tci)
466{
467	/* skb->data points at skb_mac_header, which
468	 * is fine for vlan_insert_tag.
469	 */
470	return vlan_insert_tag(skb, htons(tpid), tci);
471}
472EXPORT_SYMBOL_GPL(dsa_8021q_xmit);
473
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
474void dsa_8021q_rcv(struct sk_buff *skb, int *source_port, int *switch_id,
475		   int *subvlan)
476{
477	u16 vid, tci;
 
 
478
479	skb_push_rcsum(skb, ETH_HLEN);
480	if (skb_vlan_tag_present(skb)) {
 
481		tci = skb_vlan_tag_get(skb);
482		__vlan_hwaccel_clear_tag(skb);
483	} else {
 
 
 
 
484		__skb_vlan_pop(skb, &tci);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
485	}
486	skb_pull_rcsum(skb, ETH_HLEN);
487
488	vid = tci & VLAN_VID_MASK;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
490	*source_port = dsa_8021q_rx_source_port(vid);
491	*switch_id = dsa_8021q_rx_switch_id(vid);
492	*subvlan = dsa_8021q_rx_subvlan(vid);
493	skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
 
494}
495EXPORT_SYMBOL_GPL(dsa_8021q_rcv);