1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Regular and Ethertype DSA tagging
* Copyright (c) 2008-2009 Marvell Semiconductor
*
* Regular DSA
* -----------
* For untagged (in 802.1Q terms) packets, the switch will splice in
* the tag between the SA and the ethertype of the original
* packet. Tagged frames will instead have their outermost .1Q tag
* converted to a DSA tag. It expects the same layout when receiving
* packets from the CPU.
*
* Example:
*
* .----.----.----.---------
* Pu: | DA | SA | ET | Payload ...
* '----'----'----'---------
* 6 6 2 N
* .----.----.--------.-----.----.---------
* Pt: | DA | SA | 0x8100 | TCI | ET | Payload ...
* '----'----'--------'-----'----'---------
* 6 6 2 2 2 N
* .----.----.-----.----.---------
* Pd: | DA | SA | DSA | ET | Payload ...
* '----'----'-----'----'---------
* 6 6 4 2 N
*
* No matter if a packet is received untagged (Pu) or tagged (Pt),
* they will both have the same layout (Pd) when they are sent to the
* CPU. This is done by ignoring 802.3, replacing the ethertype field
* with more metadata, among which is a bit to signal if the original
* packet was tagged or not.
*
* Ethertype DSA
* -------------
* Uses the exact same tag format as regular DSA, but also includes a
* proper ethertype field (which the mv88e6xxx driver sets to
* ETH_P_EDSA/0xdada) followed by two zero bytes:
*
* .----.----.--------.--------.-----.----.---------
* | DA | SA | 0xdada | 0x0000 | DSA | ET | Payload ...
* '----'----'--------'--------'-----'----'---------
* 6 6 2 2 4 2 N
*/
#include <linux/etherdevice.h>
#include <linux/list.h>
#include <linux/slab.h>
#include "dsa_priv.h"
#define DSA_HLEN 4
/**
* enum dsa_cmd - DSA Command
* @DSA_CMD_TO_CPU: Set on packets that were trapped or mirrored to
* the CPU port. This is needed to implement control protocols,
* e.g. STP and LLDP, that must not allow those control packets to
* be switched according to the normal rules.
* @DSA_CMD_FROM_CPU: Used by the CPU to send a packet to a specific
* port, ignoring all the barriers that the switch normally
* enforces (VLANs, STP port states etc.). No source address
* learning takes place. "sudo send packet"
* @DSA_CMD_TO_SNIFFER: Set on the copies of packets that matched some
* user configured ingress or egress monitor criteria. These are
* forwarded by the switch tree to the user configured ingress or
* egress monitor port, which can be set to the CPU port or a
* regular port. If the destination is a regular port, the tag
* will be removed before egressing the port. If the destination
* is the CPU port, the tag will not be removed.
* @DSA_CMD_FORWARD: This tag is used on all bulk traffic passing
* through the switch tree, including the flows that are directed
* towards the CPU. Its device/port tuple encodes the original
* source port on which the packet ingressed. It can also be used
* on transmit by the CPU to defer the forwarding decision to the
* hardware, based on the current config of PVT/VTU/ATU
* etc. Source address learning takes places if enabled on the
* receiving DSA/CPU port.
*/
enum dsa_cmd {
DSA_CMD_TO_CPU = 0,
DSA_CMD_FROM_CPU = 1,
DSA_CMD_TO_SNIFFER = 2,
DSA_CMD_FORWARD = 3
};
/**
* enum dsa_code - TO_CPU Code
*
* @DSA_CODE_MGMT_TRAP: DA was classified as a management
* address. Typical examples include STP BPDUs and LLDP.
* @DSA_CODE_FRAME2REG: Response to a "remote management" request.
* @DSA_CODE_IGMP_MLD_TRAP: IGMP/MLD signaling.
* @DSA_CODE_POLICY_TRAP: Frame matched some policy configuration on
* the device. Typical examples are matching on DA/SA/VID and DHCP
* snooping.
* @DSA_CODE_ARP_MIRROR: The name says it all really.
* @DSA_CODE_POLICY_MIRROR: Same as @DSA_CODE_POLICY_TRAP, but the
* particular policy was set to trigger a mirror instead of a
* trap.
* @DSA_CODE_RESERVED_6: Unused on all devices up to at least 6393X.
* @DSA_CODE_RESERVED_7: Unused on all devices up to at least 6393X.
*
* A 3-bit code is used to relay why a particular frame was sent to
* the CPU. We only use this to determine if the packet was mirrored
* or trapped, i.e. whether the packet has been forwarded by hardware
* or not.
*
* This is the superset of all possible codes. Any particular device
* may only implement a subset.
*/
enum dsa_code {
DSA_CODE_MGMT_TRAP = 0,
DSA_CODE_FRAME2REG = 1,
DSA_CODE_IGMP_MLD_TRAP = 2,
DSA_CODE_POLICY_TRAP = 3,
DSA_CODE_ARP_MIRROR = 4,
DSA_CODE_POLICY_MIRROR = 5,
DSA_CODE_RESERVED_6 = 6,
DSA_CODE_RESERVED_7 = 7
};
static struct sk_buff *dsa_xmit_ll(struct sk_buff *skb, struct net_device *dev,
u8 extra)
{
struct dsa_port *dp = dsa_slave_to_port(dev);
u8 tag_dev, tag_port;
enum dsa_cmd cmd;
u8 *dsa_header;
if (skb->offload_fwd_mark) {
struct dsa_switch_tree *dst = dp->ds->dst;
cmd = DSA_CMD_FORWARD;
/* When offloading forwarding for a bridge, inject FORWARD
* packets on behalf of a virtual switch device with an index
* past the physical switches.
*/
tag_dev = dst->last_switch + 1 + dp->bridge_num;
tag_port = 0;
} else {
cmd = DSA_CMD_FROM_CPU;
tag_dev = dp->ds->index;
tag_port = dp->index;
}
if (skb->protocol == htons(ETH_P_8021Q)) {
if (extra) {
skb_push(skb, extra);
dsa_alloc_etype_header(skb, extra);
}
/* Construct tagged DSA tag from 802.1Q tag. */
dsa_header = dsa_etype_header_pos_tx(skb) + extra;
dsa_header[0] = (cmd << 6) | 0x20 | tag_dev;
dsa_header[1] = tag_port << 3;
/* Move CFI field from byte 2 to byte 1. */
if (dsa_header[2] & 0x10) {
dsa_header[1] |= 0x01;
dsa_header[2] &= ~0x10;
}
} else {
skb_push(skb, DSA_HLEN + extra);
dsa_alloc_etype_header(skb, DSA_HLEN + extra);
/* Construct untagged DSA tag. */
dsa_header = dsa_etype_header_pos_tx(skb) + extra;
dsa_header[0] = (cmd << 6) | tag_dev;
dsa_header[1] = tag_port << 3;
dsa_header[2] = 0;
dsa_header[3] = 0;
}
return skb;
}
static struct sk_buff *dsa_rcv_ll(struct sk_buff *skb, struct net_device *dev,
u8 extra)
{
bool trap = false, trunk = false;
int source_device, source_port;
enum dsa_code code;
enum dsa_cmd cmd;
u8 *dsa_header;
/* The ethertype field is part of the DSA header. */
dsa_header = dsa_etype_header_pos_rx(skb);
cmd = dsa_header[0] >> 6;
switch (cmd) {
case DSA_CMD_FORWARD:
trunk = !!(dsa_header[1] & 4);
break;
case DSA_CMD_TO_CPU:
code = (dsa_header[1] & 0x6) | ((dsa_header[2] >> 4) & 1);
switch (code) {
case DSA_CODE_FRAME2REG:
/* Remote management is not implemented yet,
* drop.
*/
return NULL;
case DSA_CODE_ARP_MIRROR:
case DSA_CODE_POLICY_MIRROR:
/* Mark mirrored packets to notify any upper
* device (like a bridge) that forwarding has
* already been done by hardware.
*/
break;
case DSA_CODE_MGMT_TRAP:
case DSA_CODE_IGMP_MLD_TRAP:
case DSA_CODE_POLICY_TRAP:
/* Traps have, by definition, not been
* forwarded by hardware, so don't mark them.
*/
trap = true;
break;
default:
/* Reserved code, this could be anything. Drop
* seems like the safest option.
*/
return NULL;
}
break;
default:
return NULL;
}
source_device = dsa_header[0] & 0x1f;
source_port = (dsa_header[1] >> 3) & 0x1f;
if (trunk) {
struct dsa_port *cpu_dp = dev->dsa_ptr;
/* The exact source port is not available in the tag,
* so we inject the frame directly on the upper
* team/bond.
*/
skb->dev = dsa_lag_dev(cpu_dp->dst, source_port);
} else {
skb->dev = dsa_master_find_slave(dev, source_device,
source_port);
}
if (!skb->dev)
return NULL;
/* When using LAG offload, skb->dev is not a DSA slave interface,
* so we cannot call dsa_default_offload_fwd_mark and we need to
* special-case it.
*/
if (trunk)
skb->offload_fwd_mark = true;
else if (!trap)
dsa_default_offload_fwd_mark(skb);
/* If the 'tagged' bit is set; convert the DSA tag to a 802.1Q
* tag, and delete the ethertype (extra) if applicable. If the
* 'tagged' bit is cleared; delete the DSA tag, and ethertype
* if applicable.
*/
if (dsa_header[0] & 0x20) {
u8 new_header[4];
/* Insert 802.1Q ethertype and copy the VLAN-related
* fields, but clear the bit that will hold CFI (since
* DSA uses that bit location for another purpose).
*/
new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
new_header[1] = ETH_P_8021Q & 0xff;
new_header[2] = dsa_header[2] & ~0x10;
new_header[3] = dsa_header[3];
/* Move CFI bit from its place in the DSA header to
* its 802.1Q-designated place.
*/
if (dsa_header[1] & 0x01)
new_header[2] |= 0x10;
/* Update packet checksum if skb is CHECKSUM_COMPLETE. */
if (skb->ip_summed == CHECKSUM_COMPLETE) {
__wsum c = skb->csum;
c = csum_add(c, csum_partial(new_header + 2, 2, 0));
c = csum_sub(c, csum_partial(dsa_header + 2, 2, 0));
skb->csum = c;
}
memcpy(dsa_header, new_header, DSA_HLEN);
if (extra)
dsa_strip_etype_header(skb, extra);
} else {
skb_pull_rcsum(skb, DSA_HLEN);
dsa_strip_etype_header(skb, DSA_HLEN + extra);
}
return skb;
}
#if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
static struct sk_buff *dsa_xmit(struct sk_buff *skb, struct net_device *dev)
{
return dsa_xmit_ll(skb, dev, 0);
}
static struct sk_buff *dsa_rcv(struct sk_buff *skb, struct net_device *dev)
{
if (unlikely(!pskb_may_pull(skb, DSA_HLEN)))
return NULL;
return dsa_rcv_ll(skb, dev, 0);
}
static const struct dsa_device_ops dsa_netdev_ops = {
.name = "dsa",
.proto = DSA_TAG_PROTO_DSA,
.xmit = dsa_xmit,
.rcv = dsa_rcv,
.needed_headroom = DSA_HLEN,
};
DSA_TAG_DRIVER(dsa_netdev_ops);
MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_DSA);
#endif /* CONFIG_NET_DSA_TAG_DSA */
#if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
#define EDSA_HLEN 8
static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
{
u8 *edsa_header;
skb = dsa_xmit_ll(skb, dev, EDSA_HLEN - DSA_HLEN);
if (!skb)
return NULL;
edsa_header = dsa_etype_header_pos_tx(skb);
edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
edsa_header[1] = ETH_P_EDSA & 0xff;
edsa_header[2] = 0x00;
edsa_header[3] = 0x00;
return skb;
}
static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev)
{
if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
return NULL;
skb_pull_rcsum(skb, EDSA_HLEN - DSA_HLEN);
return dsa_rcv_ll(skb, dev, EDSA_HLEN - DSA_HLEN);
}
static const struct dsa_device_ops edsa_netdev_ops = {
.name = "edsa",
.proto = DSA_TAG_PROTO_EDSA,
.xmit = edsa_xmit,
.rcv = edsa_rcv,
.needed_headroom = EDSA_HLEN,
};
DSA_TAG_DRIVER(edsa_netdev_ops);
MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_EDSA);
#endif /* CONFIG_NET_DSA_TAG_EDSA */
static struct dsa_tag_driver *dsa_tag_drivers[] = {
#if IS_ENABLED(CONFIG_NET_DSA_TAG_DSA)
&DSA_TAG_DRIVER_NAME(dsa_netdev_ops),
#endif
#if IS_ENABLED(CONFIG_NET_DSA_TAG_EDSA)
&DSA_TAG_DRIVER_NAME(edsa_netdev_ops),
#endif
};
module_dsa_tag_drivers(dsa_tag_drivers);
MODULE_LICENSE("GPL");
|