-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathdemo3.p4_14.p4
264 lines (227 loc) · 5.68 KB
/
demo3.p4_14.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
/* -*- mode: P4_14 -*- */
/*
Copyright 2017 Cisco Systems, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
header_type ethernet_t {
fields {
dstAddr : 48;
srcAddr : 48;
etherType : 16;
}
}
header_type ipv4_t {
fields {
version : 4;
ihl : 4;
diffserv : 8;
totalLen : 16;
identification : 16;
flags : 3;
fragOffset : 13;
ttl : 8;
protocol : 8;
hdrChecksum : 16;
srcAddr : 32;
dstAddr: 32;
}
}
#define NEXTHOP_TYPE_L2PTR 0
#define NEXTHOP_TYPE_ECMP_GROUP_IDX 1
header_type fwd_metadata_t {
fields {
hash1 : 16;
nexthop_type : 1;
ecmp_group_idx : 10;
ecmp_path_selector : 8; // 8 bits allows up to 256-way ECMP
l2ptr : 32;
out_bd : 24;
}
}
header ethernet_t ethernet;
header ipv4_t ipv4;
metadata fwd_metadata_t fwd_metadata;
field_list ipv4_checksum_list {
ipv4.version;
ipv4.ihl;
ipv4.diffserv;
ipv4.totalLen;
ipv4.identification;
ipv4.flags;
ipv4.fragOffset;
ipv4.ttl;
ipv4.protocol;
ipv4.srcAddr;
ipv4.dstAddr;
}
field_list_calculation ipv4_checksum {
input {
ipv4_checksum_list;
}
algorithm : csum16;
output_width : 16;
}
calculated_field ipv4.hdrChecksum {
verify ipv4_checksum if (ipv4.ihl == 5);
update ipv4_checksum if (ipv4.ihl == 5);
}
parser start {
return parse_ethernet;
}
#define ETHERTYPE_IPV4 0x0800
parser parse_ethernet {
extract(ethernet);
return select(latest.etherType) {
ETHERTYPE_IPV4 : parse_ipv4;
default: ingress;
}
}
parser parse_ipv4 {
extract(ipv4);
return ingress;
}
action my_drop() {
drop();
}
// lkp_ipv4_hash1_fields, lkp_ipv4_hash1, compute_lkp_ipv4_hash,
// compute_ipv4_hashes are copied from a recent version of switch.p4
// from the p4lang/switch Github repository, and then modified only
// slightly.
field_list lkp_ipv4_hash1_fields {
ipv4.srcAddr;
ipv4.dstAddr;
ipv4.protocol;
// Normally we would also have L4 ports extracted from the packet
// here, too, but they are left that out of this program to avoid
// the need to parse TCP/UDP headers.
}
field_list_calculation lkp_ipv4_hash1 {
input {
lkp_ipv4_hash1_fields;
}
algorithm : crc16;
output_width : 16;
}
action compute_lkp_ipv4_hash() {
modify_field_with_hash_based_offset(fwd_metadata.hash1, 0,
lkp_ipv4_hash1, 65536);
}
table compute_ipv4_hashes {
actions {
compute_lkp_ipv4_hash;
}
}
action set_l2ptr(l2ptr) {
modify_field(fwd_metadata.nexthop_type, NEXTHOP_TYPE_L2PTR);
modify_field(fwd_metadata.l2ptr, l2ptr);
}
action set_ecmp_group_idx(ecmp_group_idx) {
modify_field(fwd_metadata.nexthop_type, NEXTHOP_TYPE_ECMP_GROUP_IDX);
modify_field(fwd_metadata.ecmp_group_idx, ecmp_group_idx);
}
counter ipv4_da_lpm_stats {
type : packets;
direct : ipv4_da_lpm;
}
table ipv4_da_lpm {
reads {
ipv4.dstAddr : lpm;
}
actions {
set_l2ptr;
set_ecmp_group_idx;
my_drop;
}
}
field_list l3_hash_fields {
fwd_metadata.hash1;
}
field_list_calculation ecmp_hash {
input {
l3_hash_fields;
}
algorithm : identity;
output_width : 16;
}
action set_ecmp_path_idx(num_paths) {
// The following statement, combined with the definition of
// ecmp_hash and l3_hash_fields above, are equivalent to the
// following C assignment statement:
//
// fwd_metadata.ecmp_path_selector = 0 + (fwd_metadata.hash1 % num_paths)
//
// TBD: How does the compiler know how many bits are in num_paths?
// Does it need to be assigned to a metadata field with a defined
// width, before executing the following line?
modify_field_with_hash_based_offset(fwd_metadata.ecmp_path_selector,
0, ecmp_hash, num_paths);
}
table ecmp_group {
reads {
fwd_metadata.ecmp_group_idx : exact;
}
actions {
set_ecmp_path_idx;
set_l2ptr;
}
size : 32768;
}
table ecmp_path {
reads {
fwd_metadata.ecmp_group_idx : exact;
fwd_metadata.ecmp_path_selector : exact;
}
actions {
set_l2ptr;
}
size : 32768;
}
action set_bd_dmac_intf (bd, dmac, intf) {
modify_field(fwd_metadata.out_bd, bd);
modify_field(ethernet.dstAddr, dmac);
modify_field(standard_metadata.egress_spec, intf);
add_to_field(ipv4.ttl, -1);
}
table mac_da {
reads {
fwd_metadata.l2ptr : exact;
}
actions {
set_bd_dmac_intf;
my_drop;
}
}
action rewrite_mac(smac) {
modify_field(ethernet.srcAddr, smac);
}
table send_frame {
reads {
fwd_metadata.out_bd : exact;
}
actions {
rewrite_mac;
my_drop;
}
}
control ingress {
apply(compute_ipv4_hashes);
apply(ipv4_da_lpm);
if (fwd_metadata.nexthop_type != NEXTHOP_TYPE_L2PTR) {
apply(ecmp_group);
if (fwd_metadata.nexthop_type != NEXTHOP_TYPE_L2PTR) {
apply(ecmp_path);
}
}
apply(mac_da);
}
control egress {
apply(send_frame);
}