1 // SPDX-License-Identifier: GPL-2.0+
3 * Regular and Ethertype DSA tagging
4 * Copyright (c) 2008-2009 Marvell Semiconductor
9 * For untagged (in 802.1Q terms) packets, the switch will splice in
10 * the tag between the SA and the ethertype of the original
11 * packet. Tagged frames will instead have their outermost .1Q tag
12 * converted to a DSA tag. It expects the same layout when receiving
13 * packets from the CPU.
17 * .----.----.----.---------
18 * Pu: | DA | SA | ET | Payload ...
19 * '----'----'----'---------
21 * .----.----.--------.-----.----.---------
22 * Pt: | DA | SA | 0x8100 | TCI | ET | Payload ...
23 * '----'----'--------'-----'----'---------
25 * .----.----.-----.----.---------
26 * Pd: | DA | SA | DSA | ET | Payload ...
27 * '----'----'-----'----'---------
30 * No matter if a packet is received untagged (Pu) or tagged (Pt),
31 * they will both have the same layout (Pd) when they are sent to the
32 * CPU. This is done by ignoring 802.3, replacing the ethertype field
33 * with more metadata, among which is a bit to signal if the original
34 * packet was tagged or not.
38 * Uses the exact same tag format as regular DSA, but also includes a
39 * proper ethertype field (which the mv88e6xxx driver sets to
40 * ETH_P_EDSA/0xdada) followed by two zero bytes:
42 * .----.----.--------.--------.-----.----.---------
43 * | DA | SA | 0xdada | 0x0000 | DSA | ET | Payload ...
44 * '----'----'--------'--------'-----'----'---------
48 #include <linux/etherdevice.h>
49 #include <linux/list.h>
50 #include <linux/slab.h>
57 * enum dsa_cmd - DSA Command
58 * @DSA_CMD_TO_CPU: Set on packets that were trapped or mirrored to
59 * the CPU port. This is needed to implement control protocols,
60 * e.g. STP and LLDP, that must not allow those control packets to
61 * be switched according to the normal rules.
62 * @DSA_CMD_FROM_CPU: Used by the CPU to send a packet to a specific
63 * port, ignoring all the barriers that the switch normally
64 * enforces (VLANs, STP port states etc.). No source address
65 * learning takes place. "sudo send packet"
66 * @DSA_CMD_TO_SNIFFER: Set on the copies of packets that matched some
67 * user configured ingress or egress monitor criteria. These are
68 * forwarded by the switch tree to the user configured ingress or
69 * egress monitor port, which can be set to the CPU port or a
70 * regular port. If the destination is a regular port, the tag
71 * will be removed before egressing the port. If the destination
72 * is the CPU port, the tag will not be removed.
73 * @DSA_CMD_FORWARD: This tag is used on all bulk traffic passing
74 * through the switch tree, including the flows that are directed
75 * towards the CPU. Its device/port tuple encodes the original
76 * source port on which the packet ingressed. It can also be used
77 * on transmit by the CPU to defer the forwarding decision to the
78 * hardware, based on the current config of PVT/VTU/ATU
79 * etc. Source address learning takes places if enabled on the
80 * receiving DSA/CPU port.
85 DSA_CMD_TO_SNIFFER
= 2,
90 * enum dsa_code - TO_CPU Code
92 * @DSA_CODE_MGMT_TRAP: DA was classified as a management
93 * address. Typical examples include STP BPDUs and LLDP.
94 * @DSA_CODE_FRAME2REG: Response to a "remote management" request.
95 * @DSA_CODE_IGMP_MLD_TRAP: IGMP/MLD signaling.
96 * @DSA_CODE_POLICY_TRAP: Frame matched some policy configuration on
97 * the device. Typical examples are matching on DA/SA/VID and DHCP
99 * @DSA_CODE_ARP_MIRROR: The name says it all really.
100 * @DSA_CODE_POLICY_MIRROR: Same as @DSA_CODE_POLICY_TRAP, but the
101 * particular policy was set to trigger a mirror instead of a
103 * @DSA_CODE_RESERVED_6: Unused on all devices up to at least 6393X.
104 * @DSA_CODE_RESERVED_7: Unused on all devices up to at least 6393X.
106 * A 3-bit code is used to relay why a particular frame was sent to
107 * the CPU. We only use this to determine if the packet was mirrored
108 * or trapped, i.e. whether the packet has been forwarded by hardware
111 * This is the superset of all possible codes. Any particular device
112 * may only implement a subset.
115 DSA_CODE_MGMT_TRAP
= 0,
116 DSA_CODE_FRAME2REG
= 1,
117 DSA_CODE_IGMP_MLD_TRAP
= 2,
118 DSA_CODE_POLICY_TRAP
= 3,
119 DSA_CODE_ARP_MIRROR
= 4,
120 DSA_CODE_POLICY_MIRROR
= 5,
121 DSA_CODE_RESERVED_6
= 6,
122 DSA_CODE_RESERVED_7
= 7
125 static struct sk_buff
*dsa_xmit_ll(struct sk_buff
*skb
, struct net_device
*dev
,
128 struct dsa_port
*dp
= dsa_slave_to_port(dev
);
131 if (skb
->protocol
== htons(ETH_P_8021Q
)) {
133 skb_push(skb
, extra
);
134 memmove(skb
->data
, skb
->data
+ extra
, 2 * ETH_ALEN
);
137 /* Construct tagged FROM_CPU DSA tag from 802.1Q tag. */
138 dsa_header
= skb
->data
+ 2 * ETH_ALEN
+ extra
;
139 dsa_header
[0] = (DSA_CMD_FROM_CPU
<< 6) | 0x20 | dp
->ds
->index
;
140 dsa_header
[1] = dp
->index
<< 3;
142 /* Move CFI field from byte 2 to byte 1. */
143 if (dsa_header
[2] & 0x10) {
144 dsa_header
[1] |= 0x01;
145 dsa_header
[2] &= ~0x10;
148 skb_push(skb
, DSA_HLEN
+ extra
);
149 memmove(skb
->data
, skb
->data
+ DSA_HLEN
+ extra
, 2 * ETH_ALEN
);
151 /* Construct untagged FROM_CPU DSA tag. */
152 dsa_header
= skb
->data
+ 2 * ETH_ALEN
+ extra
;
153 dsa_header
[0] = (DSA_CMD_FROM_CPU
<< 6) | dp
->ds
->index
;
154 dsa_header
[1] = dp
->index
<< 3;
155 dsa_header
[2] = 0x00;
156 dsa_header
[3] = 0x00;
162 static struct sk_buff
*dsa_rcv_ll(struct sk_buff
*skb
, struct net_device
*dev
,
165 int source_device
, source_port
;
170 /* The ethertype field is part of the DSA header. */
171 dsa_header
= skb
->data
- 2;
173 cmd
= dsa_header
[0] >> 6;
175 case DSA_CMD_FORWARD
:
176 skb
->offload_fwd_mark
= 1;
180 code
= (dsa_header
[1] & 0x6) | ((dsa_header
[2] >> 4) & 1);
183 case DSA_CODE_FRAME2REG
:
184 /* Remote management is not implemented yet,
188 case DSA_CODE_ARP_MIRROR
:
189 case DSA_CODE_POLICY_MIRROR
:
190 /* Mark mirrored packets to notify any upper
191 * device (like a bridge) that forwarding has
192 * already been done by hardware.
194 skb
->offload_fwd_mark
= 1;
196 case DSA_CODE_MGMT_TRAP
:
197 case DSA_CODE_IGMP_MLD_TRAP
:
198 case DSA_CODE_POLICY_TRAP
:
199 /* Traps have, by definition, not been
200 * forwarded by hardware, so don't mark them.
204 /* Reserved code, this could be anything. Drop
205 * seems like the safest option.
216 source_device
= dsa_header
[0] & 0x1f;
217 source_port
= (dsa_header
[1] >> 3) & 0x1f;
219 skb
->dev
= dsa_master_find_slave(dev
, source_device
, source_port
);
223 /* If the 'tagged' bit is set; convert the DSA tag to a 802.1Q
224 * tag, and delete the ethertype (extra) if applicable. If the
225 * 'tagged' bit is cleared; delete the DSA tag, and ethertype
228 if (dsa_header
[0] & 0x20) {
231 /* Insert 802.1Q ethertype and copy the VLAN-related
232 * fields, but clear the bit that will hold CFI (since
233 * DSA uses that bit location for another purpose).
235 new_header
[0] = (ETH_P_8021Q
>> 8) & 0xff;
236 new_header
[1] = ETH_P_8021Q
& 0xff;
237 new_header
[2] = dsa_header
[2] & ~0x10;
238 new_header
[3] = dsa_header
[3];
240 /* Move CFI bit from its place in the DSA header to
241 * its 802.1Q-designated place.
243 if (dsa_header
[1] & 0x01)
244 new_header
[2] |= 0x10;
246 /* Update packet checksum if skb is CHECKSUM_COMPLETE. */
247 if (skb
->ip_summed
== CHECKSUM_COMPLETE
) {
248 __wsum c
= skb
->csum
;
249 c
= csum_add(c
, csum_partial(new_header
+ 2, 2, 0));
250 c
= csum_sub(c
, csum_partial(dsa_header
+ 2, 2, 0));
254 memcpy(dsa_header
, new_header
, DSA_HLEN
);
257 memmove(skb
->data
- ETH_HLEN
,
258 skb
->data
- ETH_HLEN
- extra
,
261 skb_pull_rcsum(skb
, DSA_HLEN
);
262 memmove(skb
->data
- ETH_HLEN
,
263 skb
->data
- ETH_HLEN
- DSA_HLEN
- extra
,
270 #if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
272 static struct sk_buff
*dsa_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
274 return dsa_xmit_ll(skb
, dev
, 0);
277 static struct sk_buff
*dsa_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
278 struct packet_type
*pt
)
280 if (unlikely(!pskb_may_pull(skb
, DSA_HLEN
)))
283 return dsa_rcv_ll(skb
, dev
, 0);
286 static const struct dsa_device_ops dsa_netdev_ops
= {
288 .proto
= DSA_TAG_PROTO_DSA
,
291 .overhead
= DSA_HLEN
,
294 DSA_TAG_DRIVER(dsa_netdev_ops
);
295 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_DSA
);
296 #endif /* CONFIG_NET_DSA_TAG_DSA */
298 #if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
302 static struct sk_buff
*edsa_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
306 skb
= dsa_xmit_ll(skb
, dev
, EDSA_HLEN
- DSA_HLEN
);
310 edsa_header
= skb
->data
+ 2 * ETH_ALEN
;
311 edsa_header
[0] = (ETH_P_EDSA
>> 8) & 0xff;
312 edsa_header
[1] = ETH_P_EDSA
& 0xff;
313 edsa_header
[2] = 0x00;
314 edsa_header
[3] = 0x00;
318 static struct sk_buff
*edsa_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
319 struct packet_type
*pt
)
321 if (unlikely(!pskb_may_pull(skb
, EDSA_HLEN
)))
324 skb_pull_rcsum(skb
, EDSA_HLEN
- DSA_HLEN
);
326 return dsa_rcv_ll(skb
, dev
, EDSA_HLEN
- DSA_HLEN
);
329 static const struct dsa_device_ops edsa_netdev_ops
= {
331 .proto
= DSA_TAG_PROTO_EDSA
,
334 .overhead
= EDSA_HLEN
,
337 DSA_TAG_DRIVER(edsa_netdev_ops
);
338 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_EDSA
);
339 #endif /* CONFIG_NET_DSA_TAG_EDSA */
341 static struct dsa_tag_driver
*dsa_tag_drivers
[] = {
342 #if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
343 &DSA_TAG_DRIVER_NAME(dsa_netdev_ops
),
345 #if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
346 &DSA_TAG_DRIVER_NAME(edsa_netdev_ops
),
350 module_dsa_tag_drivers(dsa_tag_drivers
);
352 MODULE_LICENSE("GPL");