-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabric.p4
290 lines (242 loc) · 6 KB
/
fabric.p4
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
/* Minimal P4 skeleton code adapted from example in
https://opennetworking.org/news-and-events/blog/getting-started-with-p4/
*/
#include <core.p4>
#include <v1model.p4>
typedef bit<48> EthernetAddress;
typedef bit<32> IPv4Address;
/* HEADER DEFINITIONS */
header ethernet_t {
EthernetAddress dst_addr;
EthernetAddress src_addr;
bit<16> ether_type;
}
header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> total_len;
bit<16> identification;
bit<3> flags;
bit<13> frag_offset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdr_checksum;
IPv4Address src_addr;
IPv4Address dst_addr;
}
header arp_t {
bit<16> hardware_type;
bit<16> protocol_type;
bit<8> hardware_addr_len;
bit<8> protocol_addr_len;
bit<16> op_code;
EthernetAddress sender_hw_addr;
IPv4Address sender_proto_addr;
EthernetAddress dest_hw_addr;
IPv4Address dest_proto_addr;
}
header cust_t {
bit<7> dest_switch;
bit<16> ethertype;
bit<9> tenant;
}
struct headers_t {
ethernet_t ethernet;
cust_t cust;
ipv4_t ipv4;
arp_t arp;
}
struct metadata_t {
IPv4Address destination_addr;
bit<32> marked_color;
}
error {
IPv4IncorrectVersion,
IPv4OptionsNotSupported
}
/* PARSER */
parser my_parser(packet_in packet,
out headers_t hd,
inout metadata_t meta,
inout standard_metadata_t standard_meta)
{
state start {
packet.extract(hd.ethernet);
transition select(hd.ethernet.ether_type) {
0xFFFF: parse_cust;
0x0800: parse_ipv4;
0x0806: parse_arp;
default: accept;
}
}
state parse_cust {
packet.extract(hd.cust);
transition select(hd.cust.ethertype) {
0x0800: parse_ipv4;
0x0806: parse_arp;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hd.ipv4);
verify(hd.ipv4.version == 4w4, error.IPv4IncorrectVersion);
verify(hd.ipv4.ihl == 4w5, error.IPv4OptionsNotSupported);
meta.destination_addr = hd.ipv4.dst_addr;
transition accept;
}
state parse_arp {
packet.extract(hd.arp);
meta.destination_addr = hd.arp.dest_proto_addr;
transition accept;
}
}
/* DEPARSER */
control my_deparser(packet_out packet,
in headers_t hdr)
{
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.cust);
packet.emit(hdr.ipv4);
packet.emit(hdr.arp);
}
}
/* CHECKSUM CALCULATION AND VERIFICATION */
control my_verify_checksum(inout headers_t hdr,
inout metadata_t meta)
{
apply { }
}
control my_compute_checksum(inout headers_t hdr,
inout metadata_t meta)
{
apply { }
}
/* INGRESS PIPELINE */
control my_ingress(inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t standard_metadata)
{
bool dropped = false;
direct_meter<bit<32>>(MeterType.packets) flow_one_meter;
direct_meter<bit<32>>(MeterType.packets) flow_two_meter;
direct_meter<bit<32>>(MeterType.packets) flow_three_meter;
action drop_action() {
mark_to_drop(standard_metadata);
dropped = true;
}
action to_port_action(bit<9> port) {
standard_metadata.egress_spec = port;
}
action flow_one_action() {
flow_one_meter.read(meta.marked_color);
}
action flow_two_action() {
flow_two_meter.read(meta.marked_color);
}
action flow_three_action() {
flow_three_meter.read(meta.marked_color);
}
table ipv4_match {
key = {
meta.destination_addr: lpm;
}
actions = {
drop_action;
to_port_action;
}
size = 1024;
default_action = drop_action;
}
table switch_match {
key = {
hdr.cust.dest_switch: exact;
}
actions = {
drop_action;
to_port_action;
}
size = 1024;
default_action = drop_action;
}
table flow_one {
key = {
hdr.cust.tenant: exact;
}
actions = {
flow_one_action;
NoAction;
}
default_action = NoAction;
meters = flow_one_meter;
size = 1024;
}
table flow_two {
key = {
hdr.cust.tenant: exact;
}
actions = {
flow_two_action;
NoAction;
}
default_action = NoAction;
meters = flow_two_meter;
size = 1024;
}
table flow_three {
key = {
hdr.cust.tenant: exact;
}
actions = {
flow_three_action;
NoAction;
}
default_action = NoAction;
meters = flow_three_meter;
size = 1024;
}
table regulate_packets {
key = {
meta.marked_color: exact;
}
actions = {
drop_action;
NoAction;
}
default_action = NoAction;
size = 32;
}
apply {
if (hdr.cust.isValid()) {
switch_match.apply();
if (hdr.cust.tenant == 1) {
flow_one.apply();
}
if (hdr.cust.tenant == 2) {
flow_two.apply();
}
if (hdr.cust.tenant == 3) {
flow_three.apply();
}
regulate_packets.apply();
} else {
ipv4_match.apply();
}
if (dropped) return;
}
}
/* EGRESS PIPELINE */
control my_egress(inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t standard_metadata)
{
apply { }
}
/* SWITCH PACKAGE DEFINITION */
V1Switch(my_parser(),
my_verify_checksum(),
my_ingress(),
my_egress(),
my_compute_checksum(),
my_deparser()) main;