This repository was archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsnvs.p4
More file actions
328 lines (288 loc) · 9.46 KB
/
snvs.p4
File metadata and controls
328 lines (288 loc) · 9.46 KB
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
/*
Copyright (c) 2021 VMware, Inc.
SPDX-License-Identifier: MIT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* Simple Nerpa Virtual Switch pipeline. */
#include <core.p4>
#include <v1model.p4>
const bit<16> ETH_VLAN = 0x8100;
typedef bit<48> EthernetAddress;
typedef bit<32> IPv4Address;
typedef bit<12> VlanID;
typedef bit<9> PortID;
typedef bit<3> PCP;
// simple_switch.md suggests defining these constants for the values
// of standard_metadata.instance_type.
const bit<32> PKT_INSTANCE_TYPE_NORMAL = 0;
const bit<32> PKT_INSTANCE_TYPE_INGRESS_CLONE = 1;
const bit<32> PKT_INSTANCE_TYPE_EGRESS_CLONE = 2;
const bit<32> PKT_INSTANCE_TYPE_COALESCED = 3;
const bit<32> PKT_INSTANCE_TYPE_INGRESS_RECIRC = 4;
const bit<32> PKT_INSTANCE_TYPE_REPLICATION = 5;
const bit<32> PKT_INSTANCE_TYPE_RESUBMIT = 6;
bool eth_addr_is_multicast(in EthernetAddress a) {
return (a & (1 << 40)) != 0;
}
const PortID DROP_PORT = 511; // This is meaningful to simple_switch.
const PortID FLOOD_PORT = 510; // Just an internal constant.
header Ethernet_h {
EthernetAddress dst;
EthernetAddress src;
bit<16> type;
}
header Vlan_h {
PCP pcp;
bit<1> dei;
VlanID vid;
bit<16> type;
}
header Ipv4_h {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
IPv4Address srcAddr;
IPv4Address dstAddr;
}
struct metadata {
// The packet's conceptual VLAN, which might not be in a VLAN header.
VlanID vlan;
// Whether to flood this packet.
bool flood;
}
struct headers {
Ethernet_h eth;
Vlan_h vlan;
}
parser SnvsParser(packet_in packet,
out headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
state start {
packet.extract(hdr.eth);
transition select(hdr.eth.type) {
ETH_VLAN: parse_vlan;
}
}
state parse_vlan {
packet.extract(hdr.vlan);
transition accept;
}
}
control SnvsVerifyChecksum(inout headers hdr, inout metadata meta) {
apply {}
}
const bit<32> MAC_LEARN_RCVR = 1;
struct LearnDigest {
PortID port;
VlanID vlan;
EthernetAddress mac;
bit<48> timestamp;
}
control SnvsIngress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
action Drop() {
mark_to_drop(standard_metadata);
exit;
}
// Drop packets received on mirror destination port.
table MirrorDstDrop {
key = { standard_metadata.ingress_port: exact @name("port"); }
actions = { Drop; NoAction; }
const default_action = NoAction;
}
// Drop packets to reserved Ethernet multicast address.
@nerpa_singleton
table ReservedMcastDstDrop {
key = { hdr.eth.dst: exact @name("dst"); }
actions = { Drop; NoAction; }
const default_action = NoAction;
}
// Input VLAN processing.
action SetVlan(VlanID vid) {
meta.vlan = vid;
}
action UseTaggedVlan() {
meta.vlan = hdr.vlan.vid;
}
table InputVlan {
key = {
standard_metadata.ingress_port: exact @name("port");
hdr.vlan.isValid(): exact @name("has_vlan") @nerpa_bool;
hdr.vlan.vid: optional @name("vid");
}
actions = { Drop; SetVlan; UseTaggedVlan; }
default_action = Drop;
}
// Mirroring packet selection.
table MirrorSelectProduct {
key = {
standard_metadata.ingress_port: optional @name("port");
meta.vlan: optional @name("vlan");
}
actions = { NoAction; }
const default_action = NoAction;
}
// Tracks VLANs in which all packets are flooded.
action set_flood() {
meta.flood = true;
}
table FloodVlan {
key = { meta.vlan: exact @name("vlan"); }
actions = { set_flood; NoAction; }
const default_action = NoAction;
}
// Known VLAN+MAC -> port mappings.
//
// We should only need one table for this, with one lookup for the source
// MAC and one for the destination MAC per packet, but hardware and BMv2
// don't support that. So we need two different tables.
table LearnedSrc {
key = {
meta.vlan: exact @name("vlan");
hdr.eth.src: exact @name("mac");
standard_metadata.ingress_port: exact @name("port");
}
actions = { NoAction; }
const default_action = NoAction;
}
PortID output;
action KnownDst(PortID port) {
output = port;
}
table LearnedDst {
key = {
meta.vlan: exact @name("vlan");
hdr.eth.dst: exact @name("mac");
}
actions = { KnownDst; NoAction; }
const default_action = NoAction;
}
apply {
// Drop packets received on mirror destination port.
MirrorDstDrop.apply();
// Drop packets to reserved Ethernet multicast address.
ReservedMcastDstDrop.apply();
// Input VLAN processing.
InputVlan.apply();
// Mirroring packet selection.
if (MirrorSelectProduct.apply().hit) {
clone(CloneType.I2E, 1);
}
// Is this a flood VLAN?
meta.flood = false;
FloodVlan.apply();
// If the source MAC isn't known, send it to the control plane to
// be learned.
if (!meta.flood && !eth_addr_is_multicast(hdr.eth.src)
&& !LearnedSrc.apply().hit) {
LearnDigest d;
d.port = standard_metadata.ingress_port;
d.vlan = meta.vlan;
d.mac = hdr.eth.src;
d.timestamp = standard_metadata.ingress_global_timestamp;
digest<LearnDigest>(MAC_LEARN_RCVR, d);
}
// Look up destination MAC.
output = FLOOD_PORT;
if (!meta.flood && !eth_addr_is_multicast(hdr.eth.dst)) {
LearnedDst.apply();
}
// If we're flooding, then use the VLAN as the multicast group
// (we assume that the control plane has configured one multicast
// group per VLAN, with the VLAN number as the multicast group ID).
//
// If we have a destination port, then it becomes the output port.
//
// We don't bother to try to drop output to the input port here
// because it happens in the egress pipeline.
if (output == FLOOD_PORT) {
standard_metadata.mcast_grp = (bit<16>) meta.vlan;
} else {
standard_metadata.egress_spec = output;
}
}
}
control SnvsEgress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
// Output VLAN processing.
table OutputVlan {
key = {
standard_metadata.egress_port: exact @name("port");
meta.vlan: optional @name("vlan");
}
actions = { NoAction; }
const default_action = NoAction;
}
// Priority tagging mode.
table PriorityTagging {
key = {
standard_metadata.egress_port: exact @name("port");
hdr.vlan.isValid() && hdr.vlan.pcp != 0: exact @name("nonzero_pcp") @nerpa_bool;
}
actions = { NoAction; }
const default_action = NoAction;
}
apply {
// If this is a clone for the purpose of port mirroring, we're all
// done.
if (standard_metadata.instance_type == PKT_INSTANCE_TYPE_INGRESS_CLONE) {
exit;
}
// Drop loopback.
if (standard_metadata.egress_port == standard_metadata.ingress_port) {
mark_to_drop(standard_metadata);
exit;
}
// Output VLAN processing, including priority tagging.
bool tag_vlan = OutputVlan.apply().hit;
VlanID vid = tag_vlan ? meta.vlan : 0;
bool include_vlan_header = tag_vlan || PriorityTagging.apply().hit;
if (include_vlan_header && !hdr.vlan.isValid()) {
hdr.vlan = { 0, 0, vid, hdr.eth.type };
hdr.eth.type = ETH_VLAN;
} else if (!include_vlan_header && hdr.vlan.isValid()) {
hdr.eth.type = hdr.vlan.type;
hdr.vlan.setInvalid();
}
}
}
control SnvsComputeChecksum(inout headers hdr, inout metadata meta) {
apply {}
}
control SnvsDeparser(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr);
}
}
V1Switch (
SnvsParser(),
SnvsVerifyChecksum(),
SnvsIngress(),
SnvsEgress(),
SnvsComputeChecksum(),
SnvsDeparser()
) main;