-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.p4
300 lines (247 loc) · 6.35 KB
/
project.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
290
291
292
293
294
295
296
297
298
299
300
#include <core.p4>
#include <v1model.p4>
const bit<16> TYPE_MYTTL = 0x1212;
const bit<16> TYPE_ARP = 0x0806;
const bit<16> TYPE_INT = 0x9487 ;
const bit<8> INIT_TTL = (bit<8>) 255;
typedef bit<9> egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<32> switchID_t;
#define NUM_OF_SWITCHES 10
#define MAX_INT_HEADERS 20
/* Header */
header ethernet_t {
macAddr_t dst_addr;
macAddr_t src_addr;
bit<16> ether_type;
}
header myTtl_t {
bit<16> proto_id;
switchID_t src_swid;
bit<8> ttl;
}
header int_count_t {
bit<16> proto_id;
bit<16> num_switches;
}
header int_header_t {
switchID_t switch_id;
}
header arp_t {
bit<16> htype;
bit<16> ptype;
bit<8> hlen;
bit<8> plen;
bit<16> oper;
bit<48> sha;
bit<32> spa;
bit<48> tha;
bit<32> tpa;
}
struct parser_metadata_t {
bit<16> num_headers_remaining;
}
struct metadata {
switchID_t swid;
parser_metadata_t parser_metadata;
}
struct headers {
ethernet_t ethernet;
arp_t arp;
myTtl_t myTtl;
int_count_t int_count;
int_header_t[MAX_INT_HEADERS] int_headers;
}
/* Parser */
parser MyParser(packet_in pkt, out headers hdr, inout metadata meta, inout standard_metadata_t smeta) {
state start {
transition parse_ethernet;
}
state parse_ethernet {
pkt.extract(hdr.ethernet);
transition select(hdr.ethernet.ether_type) {
TYPE_MYTTL: parse_myTtl;
TYPE_ARP: parse_arp;
default: accept;
}
}
state parse_myTtl {
pkt.extract(hdr.myTtl);
transition select(hdr.myTtl.proto_id) {
TYPE_ARP: parse_arp;
default: accept;
}
}
state parse_arp {
pkt.extract(hdr.arp);
transition select(hdr.arp.ptype) {
TYPE_INT: parse_int;
default: accept;
}
}
state parse_int {
pkt.extract(hdr.int_count);
meta.parser_metadata.num_headers_remaining = hdr.int_count.num_switches;
transition select(meta.parser_metadata.num_headers_remaining) {
0: accept;
default: parse_int_headers;
}
}
state parse_int_headers {
pkt.extract(hdr.int_headers.next);
meta.parser_metadata.num_headers_remaining = meta.parser_metadata.num_headers_remaining - 1 ;
transition select(meta.parser_metadata.num_headers_remaining) {
0: accept;
default: parse_int_headers;
}
}
}
/* Checksum Verification */
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
apply { }
}
/* Ingress Processing */
control MyIngress(inout headers hdr, inout metadata meta, inout standard_metadata_t std_meta) {
register<bit<9>> (NUM_OF_SWITCHES) port_reg;
register<bit<8>> (NUM_OF_SWITCHES) ttl_reg;
action drop() {
mark_to_drop(std_meta);
}
action forward(egressSpec_t port_num) {
std_meta.egress_spec = port_num;
}
action get_switch_id(switchID_t swid) {
meta.swid = swid;
}
action add_myTtl_multicast(switchID_t swid) {
// add ttl
hdr.myTtl.setValid();
hdr.myTtl.ttl = INIT_TTL;
hdr.myTtl.src_swid = swid;
hdr.myTtl.proto_id = hdr.ethernet.ether_type;
hdr.ethernet.ether_type = TYPE_MYTTL;
// multicast
std_meta.mcast_grp = 1;
}
table switch_id_table {
key = {
hdr.myTtl.src_swid: exact;
}
actions = {
get_switch_id;
drop;
NoAction;
}
default_action = NoAction;
}
table l2 {
key = {
hdr.ethernet.dst_addr: exact;
}
actions = {
forward;
drop;
add_myTtl_multicast;
NoAction;
}
default_action = NoAction;
}
apply {
if(hdr.myTtl.isValid()) {
hdr.myTtl.ttl = hdr.myTtl.ttl - 1;
bit<9> port;
bit<8> ttl;
switch_id_table.apply(); // grab id info should must match entry
port_reg.read(port, hdr.myTtl.src_swid);
ttl_reg.read(ttl, hdr.myTtl.src_swid);
if(hdr.myTtl.ttl < ttl) {
mark_to_drop(std_meta);
}
else if(hdr.myTtl.ttl == ttl && std_meta.ingress_port != port ) {
mark_to_drop(std_meta);
}
else if(hdr.myTtl.src_swid == meta.swid) {
mark_to_drop(std_meta);
}
else {
port_reg.write(hdr.myTtl.src_swid, std_meta.ingress_port);
ttl_reg.write(hdr.myTtl.src_swid, hdr.myTtl.ttl);
std_meta.mcast_grp = 1;
}
}
else {
l2.apply(); // not contain a ttl hdr -> forwarding table Match
}
}
}
/* Engress Processing */
control MyEgress(inout headers hdr, inout metadata meta, inout standard_metadata_t std_meta) {
action remove_myTtl() {
hdr.ethernet.ether_type = hdr.myTtl.proto_id;
hdr.myTtl.setInvalid();
if(hdr.int_count.isValid())
hdr.arp.ptype = hdr.int_count.proto_id;
}
table host_table {
key = {
std_meta.egress_port: exact;
}
actions = {
remove_myTtl;
NoAction;
}
default_action = NoAction;
}
action add_int() {
hdr.int_count.setValid();
hdr.int_count.num_switches = (bit<16>)1;
hdr.int_count.proto_id = hdr.arp.ptype;
hdr.arp.ptype = TYPE_INT;
// Add int header
hdr.int_headers.push_front(1);
hdr.int_headers[0].setValid();
hdr.int_headers[0].switch_id = hdr.myTtl.src_swid;
}
action add_int_header() {
hdr.int_count.num_switches = hdr.int_count.num_switches + 1;
hdr.int_headers.push_front(1);
hdr.int_headers[0].setValid();
hdr.int_headers[0].switch_id = meta.swid;
}
apply {
// if ingress port == egress port -> drop
if (std_meta.ingress_port == std_meta.egress_port) {
mark_to_drop(std_meta);
}
else {
if(!hdr.int_count.isValid())
add_int();
else
add_int_header();
// if neighbor is a host -> remove myTtl
host_table.apply();
}
}
}
/* Checksum Update */
control MyComputeChecksum(inout headers hdr, inout metadata meta) {
apply { }
}
/* Deparser */
control MyDeparser(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.myTtl);
packet.emit(hdr.arp);
packet.emit(hdr.int_count);
packet.emit(hdr.int_headers);
}
}
V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;