net: dsa: tag_dsa: send packets with TX fwd offload from VLAN-unaware bridges using...
[platform/kernel/linux-rpi.git] / net / dsa / tag_dsa.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Regular and Ethertype DSA tagging
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  *
6  * Regular DSA
7  * -----------
8
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.
14  *
15  * Example:
16  *
17  *     .----.----.----.---------
18  * Pu: | DA | SA | ET | Payload ...
19  *     '----'----'----'---------
20  *       6    6    2       N
21  *     .----.----.--------.-----.----.---------
22  * Pt: | DA | SA | 0x8100 | TCI | ET | Payload ...
23  *     '----'----'--------'-----'----'---------
24  *       6    6       2      2    2       N
25  *     .----.----.-----.----.---------
26  * Pd: | DA | SA | DSA | ET | Payload ...
27  *     '----'----'-----'----'---------
28  *       6    6     4    2       N
29  *
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.
35  *
36  * Ethertype DSA
37  * -------------
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:
41  *
42  * .----.----.--------.--------.-----.----.---------
43  * | DA | SA | 0xdada | 0x0000 | DSA | ET | Payload ...
44  * '----'----'--------'--------'-----'----'---------
45  *   6    6       2        2      4    2       N
46  */
47
48 #include <linux/etherdevice.h>
49 #include <linux/list.h>
50 #include <linux/slab.h>
51
52 #include "dsa_priv.h"
53
54 #define DSA_HLEN        4
55
56 /**
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.
81  */
82 enum dsa_cmd {
83         DSA_CMD_TO_CPU     = 0,
84         DSA_CMD_FROM_CPU   = 1,
85         DSA_CMD_TO_SNIFFER = 2,
86         DSA_CMD_FORWARD    = 3
87 };
88
89 /**
90  * enum dsa_code - TO_CPU Code
91  *
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
98  *     snooping.
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
102  *     trap.
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.
105  *
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
109  * or not.
110  *
111  * This is the superset of all possible codes. Any particular device
112  * may only implement a subset.
113  */
114 enum dsa_code {
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
123 };
124
125 static struct sk_buff *dsa_xmit_ll(struct sk_buff *skb, struct net_device *dev,
126                                    u8 extra)
127 {
128         struct dsa_port *dp = dsa_slave_to_port(dev);
129         u8 tag_dev, tag_port;
130         enum dsa_cmd cmd;
131         u8 *dsa_header;
132
133         if (skb->offload_fwd_mark) {
134                 struct dsa_switch_tree *dst = dp->ds->dst;
135
136                 cmd = DSA_CMD_FORWARD;
137
138                 /* When offloading forwarding for a bridge, inject FORWARD
139                  * packets on behalf of a virtual switch device with an index
140                  * past the physical switches.
141                  */
142                 tag_dev = dst->last_switch + 1 + dp->bridge_num;
143                 tag_port = 0;
144         } else {
145                 cmd = DSA_CMD_FROM_CPU;
146                 tag_dev = dp->ds->index;
147                 tag_port = dp->index;
148         }
149
150         if (skb->protocol == htons(ETH_P_8021Q)) {
151                 if (extra) {
152                         skb_push(skb, extra);
153                         dsa_alloc_etype_header(skb, extra);
154                 }
155
156                 /* Construct tagged DSA tag from 802.1Q tag. */
157                 dsa_header = dsa_etype_header_pos_tx(skb) + extra;
158                 dsa_header[0] = (cmd << 6) | 0x20 | tag_dev;
159                 dsa_header[1] = tag_port << 3;
160
161                 /* Move CFI field from byte 2 to byte 1. */
162                 if (dsa_header[2] & 0x10) {
163                         dsa_header[1] |= 0x01;
164                         dsa_header[2] &= ~0x10;
165                 }
166         } else {
167                 skb_push(skb, DSA_HLEN + extra);
168                 dsa_alloc_etype_header(skb, DSA_HLEN + extra);
169
170                 /* Construct untagged DSA tag. */
171                 dsa_header = dsa_etype_header_pos_tx(skb) + extra;
172
173                 dsa_header[0] = (cmd << 6) | tag_dev;
174                 dsa_header[1] = tag_port << 3;
175                 dsa_header[2] = 0;
176                 dsa_header[3] = 0;
177         }
178
179         return skb;
180 }
181
182 static struct sk_buff *dsa_rcv_ll(struct sk_buff *skb, struct net_device *dev,
183                                   u8 extra)
184 {
185         bool trap = false, trunk = false;
186         int source_device, source_port;
187         enum dsa_code code;
188         enum dsa_cmd cmd;
189         u8 *dsa_header;
190
191         /* The ethertype field is part of the DSA header. */
192         dsa_header = dsa_etype_header_pos_rx(skb);
193
194         cmd = dsa_header[0] >> 6;
195         switch (cmd) {
196         case DSA_CMD_FORWARD:
197                 trunk = !!(dsa_header[1] & 4);
198                 break;
199
200         case DSA_CMD_TO_CPU:
201                 code = (dsa_header[1] & 0x6) | ((dsa_header[2] >> 4) & 1);
202
203                 switch (code) {
204                 case DSA_CODE_FRAME2REG:
205                         /* Remote management is not implemented yet,
206                          * drop.
207                          */
208                         return NULL;
209                 case DSA_CODE_ARP_MIRROR:
210                 case DSA_CODE_POLICY_MIRROR:
211                         /* Mark mirrored packets to notify any upper
212                          * device (like a bridge) that forwarding has
213                          * already been done by hardware.
214                          */
215                         break;
216                 case DSA_CODE_MGMT_TRAP:
217                 case DSA_CODE_IGMP_MLD_TRAP:
218                 case DSA_CODE_POLICY_TRAP:
219                         /* Traps have, by definition, not been
220                          * forwarded by hardware, so don't mark them.
221                          */
222                         trap = true;
223                         break;
224                 default:
225                         /* Reserved code, this could be anything. Drop
226                          * seems like the safest option.
227                          */
228                         return NULL;
229                 }
230
231                 break;
232
233         default:
234                 return NULL;
235         }
236
237         source_device = dsa_header[0] & 0x1f;
238         source_port = (dsa_header[1] >> 3) & 0x1f;
239
240         if (trunk) {
241                 struct dsa_port *cpu_dp = dev->dsa_ptr;
242
243                 /* The exact source port is not available in the tag,
244                  * so we inject the frame directly on the upper
245                  * team/bond.
246                  */
247                 skb->dev = dsa_lag_dev(cpu_dp->dst, source_port);
248         } else {
249                 skb->dev = dsa_master_find_slave(dev, source_device,
250                                                  source_port);
251         }
252
253         if (!skb->dev)
254                 return NULL;
255
256         /* When using LAG offload, skb->dev is not a DSA slave interface,
257          * so we cannot call dsa_default_offload_fwd_mark and we need to
258          * special-case it.
259          */
260         if (trunk)
261                 skb->offload_fwd_mark = true;
262         else if (!trap)
263                 dsa_default_offload_fwd_mark(skb);
264
265         /* If the 'tagged' bit is set; convert the DSA tag to a 802.1Q
266          * tag, and delete the ethertype (extra) if applicable. If the
267          * 'tagged' bit is cleared; delete the DSA tag, and ethertype
268          * if applicable.
269          */
270         if (dsa_header[0] & 0x20) {
271                 u8 new_header[4];
272
273                 /* Insert 802.1Q ethertype and copy the VLAN-related
274                  * fields, but clear the bit that will hold CFI (since
275                  * DSA uses that bit location for another purpose).
276                  */
277                 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
278                 new_header[1] = ETH_P_8021Q & 0xff;
279                 new_header[2] = dsa_header[2] & ~0x10;
280                 new_header[3] = dsa_header[3];
281
282                 /* Move CFI bit from its place in the DSA header to
283                  * its 802.1Q-designated place.
284                  */
285                 if (dsa_header[1] & 0x01)
286                         new_header[2] |= 0x10;
287
288                 /* Update packet checksum if skb is CHECKSUM_COMPLETE. */
289                 if (skb->ip_summed == CHECKSUM_COMPLETE) {
290                         __wsum c = skb->csum;
291                         c = csum_add(c, csum_partial(new_header + 2, 2, 0));
292                         c = csum_sub(c, csum_partial(dsa_header + 2, 2, 0));
293                         skb->csum = c;
294                 }
295
296                 memcpy(dsa_header, new_header, DSA_HLEN);
297
298                 if (extra)
299                         dsa_strip_etype_header(skb, extra);
300         } else {
301                 skb_pull_rcsum(skb, DSA_HLEN);
302                 dsa_strip_etype_header(skb, DSA_HLEN + extra);
303         }
304
305         return skb;
306 }
307
308 #if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
309
310 static struct sk_buff *dsa_xmit(struct sk_buff *skb, struct net_device *dev)
311 {
312         return dsa_xmit_ll(skb, dev, 0);
313 }
314
315 static struct sk_buff *dsa_rcv(struct sk_buff *skb, struct net_device *dev)
316 {
317         if (unlikely(!pskb_may_pull(skb, DSA_HLEN)))
318                 return NULL;
319
320         return dsa_rcv_ll(skb, dev, 0);
321 }
322
323 static const struct dsa_device_ops dsa_netdev_ops = {
324         .name     = "dsa",
325         .proto    = DSA_TAG_PROTO_DSA,
326         .xmit     = dsa_xmit,
327         .rcv      = dsa_rcv,
328         .needed_headroom = DSA_HLEN,
329 };
330
331 DSA_TAG_DRIVER(dsa_netdev_ops);
332 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_DSA);
333 #endif  /* CONFIG_NET_DSA_TAG_DSA */
334
335 #if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
336
337 #define EDSA_HLEN 8
338
339 static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
340 {
341         u8 *edsa_header;
342
343         skb = dsa_xmit_ll(skb, dev, EDSA_HLEN - DSA_HLEN);
344         if (!skb)
345                 return NULL;
346
347         edsa_header = dsa_etype_header_pos_tx(skb);
348         edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
349         edsa_header[1] = ETH_P_EDSA & 0xff;
350         edsa_header[2] = 0x00;
351         edsa_header[3] = 0x00;
352         return skb;
353 }
354
355 static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev)
356 {
357         if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
358                 return NULL;
359
360         skb_pull_rcsum(skb, EDSA_HLEN - DSA_HLEN);
361
362         return dsa_rcv_ll(skb, dev, EDSA_HLEN - DSA_HLEN);
363 }
364
365 static const struct dsa_device_ops edsa_netdev_ops = {
366         .name     = "edsa",
367         .proto    = DSA_TAG_PROTO_EDSA,
368         .xmit     = edsa_xmit,
369         .rcv      = edsa_rcv,
370         .needed_headroom = EDSA_HLEN,
371 };
372
373 DSA_TAG_DRIVER(edsa_netdev_ops);
374 MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_EDSA);
375 #endif  /* CONFIG_NET_DSA_TAG_EDSA */
376
377 static struct dsa_tag_driver *dsa_tag_drivers[] = {
378 #if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
379         &DSA_TAG_DRIVER_NAME(dsa_netdev_ops),
380 #endif
381 #if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
382         &DSA_TAG_DRIVER_NAME(edsa_netdev_ops),
383 #endif
384 };
385
386 module_dsa_tag_drivers(dsa_tag_drivers);
387
388 MODULE_LICENSE("GPL");