forked from rachitnigam/Hula-hoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch.p4
394 lines (325 loc) · 11 KB
/
switch.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
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
389
390
391
392
393
394
/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>
typedef bit<9> egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<32> ip4Addr_t;
typedef bit<9> port_id_t;
typedef bit<8> util_t;
typedef bit<24> tor_id_t;
typedef bit<48> time_t;
/* Constants about the topology and switches. */
const port_id_t NUM_PORTS = 255;
const tor_id_t NUM_TORS = 512;
const bit<32> EGDE_HOSTS = 4;
/* Declaration for the various packet types. */
const bit<16> TYPE_IPV4 = 0x800;
const bit<8> PROTO_HULA = 0x42;
const bit<8> PROTO_TCP = 0x06;
/* Tracking things for flowlets */
const time_t FLOWLET_TOUT = 48w1 << 3;
const util_t PROBE_FREQ_FACTOR = 6;
const time_t KEEP_ALIVE_THRESH = 48w1 << PROBE_FREQ_FACTOR;
const time_t PROBE_FREQ = 48w1 << PROBE_FREQ_FACTOR; // Here for documentation. Unused.
/*************************************************************************
*********************** H E A D E R S ***********************************
*************************************************************************/
header hula_t {
bit<24> dst_tor;
bit<8> path_util;
}
header ethernet_t {
macAddr_t dstAddr;
macAddr_t srcAddr;
bit<16> etherType;
}
header ipv4_t {
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;
ip4Addr_t srcAddr;
ip4Addr_t dstAddr;
}
header tcp_t {
bit<16> srcPort;
bit<16> dstPort;
bit<32> seq;
bit<32> ack;
bit<4> dataofs;
bit<3> reserved;
bit<9> flags;
bit<32> window;
bit<16> chksum;
bit<16> urgptr;
}
struct metadata {
bit<9> nxt_hop;
bit<32> self_id;
bit<32> dst_tor;
}
struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
tcp_t tcp;
hula_t hula;
}
/*************************************************************************
*********************** P A R S E R ***********************************
*************************************************************************/
parser MyParser(packet_in packet,
out headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
state start {
transition parse_ethernet;
}
state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
TYPE_IPV4: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition select(hdr.ipv4.protocol) {
PROTO_HULA: parse_hula;
PROTO_TCP: parse_tcp;
default: accept;
}
}
state parse_hula {
packet.extract(hdr.hula);
transition accept;
}
state parse_tcp {
packet.extract(hdr.tcp);
transition accept;
}
}
/*************************************************************************
************ C H E C K S U M V E R I F I C A T I O N *************
*************************************************************************/
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
apply { }
}
/*************************************************************************
************** I N G R E S S P R O C E S S I N G *******************
*************************************************************************/
control MyIngress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
/****** Registers to keep track of utilization. *******/
// Keep track of the port utilization
register<util_t>((bit<32>) NUM_PORTS) port_util;
// Last time port_util was updated for a port.
register<time_t>((bit<32>) NUM_PORTS) port_util_last_updated;
// Keep track of the last time a probe from dst_tor came.
register<time_t>((bit<32>) NUM_TORS) update_time;
// Best hop for for each tor
register<port_id_t>((bit<32>) NUM_TORS) best_hop;
// Last time a packet from a flowlet was observed.
register<time_t>((bit<32>) 1024) flowlet_time;
// The next hop a flow should take.
register<port_id_t>((bit<32>) 1024) flowlet_hop;
// Keep track of the minimum utilized path
register<util_t>((bit<32>) NUM_TORS) min_path_util;
action drop() {
mark_to_drop();
}
/******************************************************/
/**** Core HULA logic *****/
action hula_handle_probe() {
time_t curr_time = standard_metadata.ingress_global_timestamp;
bit<32> dst_tor = (bit<32>) hdr.hula.dst_tor;
util_t tx_util;
util_t mpu;
time_t up_time;
port_util.read(tx_util, (bit<32>) standard_metadata.ingress_port);
min_path_util.read(mpu, dst_tor);
update_time.read(up_time, dst_tor);
// If the current link util is higher, then that is the path util.
if(hdr.hula.path_util < tx_util) {
hdr.hula.path_util = tx_util;
}
// If the path util from probe is lower than minimum path util,
// update best hop.
bool cond = (hdr.hula.path_util < mpu || curr_time - up_time > KEEP_ALIVE_THRESH);
mpu = cond ? hdr.hula.path_util : mpu;
min_path_util.write(dst_tor, mpu);
up_time = cond ? curr_time : up_time;
update_time.write(dst_tor, up_time);
port_id_t bh_temp;
best_hop.read(bh_temp, dst_tor);
bh_temp = cond ? standard_metadata.ingress_port : bh_temp;
best_hop.write(dst_tor, bh_temp);
min_path_util.read(mpu, dst_tor);
hdr.hula.path_util = mpu;
}
action hula_handle_data_packet() {
time_t curr_time = standard_metadata.ingress_global_timestamp;
bit<32> dst_tor = (bit<32>) hdr.hula.dst_tor;
util_t tx_util;
port_util.read(tx_util, (bit<32>) standard_metadata.ingress_port);
bit<32> flow_hash;
time_t flow_t;
port_id_t flow_h;
port_id_t best_h;
hash(flow_hash, HashAlgorithm.csum16, 32w0, {
hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
hdr.ipv4.protocol,
hdr.tcp.srcPort,
hdr.tcp.dstPort
}, 32w1 << 10 - 1);
flowlet_time.read(flow_t, flow_hash);
/*if (curr_time - flow_t > FLOWLET_TOUT) {*/
best_hop.read(best_h, meta.dst_tor);
port_id_t tmp;
flowlet_hop.read(tmp, flow_hash);
tmp = (curr_time - flow_t > FLOWLET_TOUT) ? best_h : tmp;
flowlet_hop.write(flow_hash, tmp);
/*}*/
flowlet_hop.read(flow_h, flow_hash);
standard_metadata.egress_spec = flow_h;
flowlet_time.write(flow_hash, curr_time);
}
table hula_logic {
key = {
hdr.ipv4.protocol: exact;
}
actions = {
hula_handle_probe;
hula_handle_data_packet;
drop;
}
size = 4;
default_action = drop();
}
/***********************************************/
/***** Implement mapping from dstAddr to dst_tor ********/
// Uses the destination address to compute the destination tor and the id of
// current switch. The table is configured by the control plane.
action set_dst_tor(tor_id_t dst_tor, tor_id_t self_id) {
meta.dst_tor = (bit<32>) dst_tor;
meta.self_id = (bit<32>) self_id;
}
// Used when matching a probe packet.
action dummy_dst_tor() {
meta.dst_tor = 0;
meta.self_id = 1;
}
table get_dst_tor {
key= {
hdr.ipv4.dstAddr: exact;
}
actions = {
set_dst_tor;
dummy_dst_tor;
}
default_action = dummy_dst_tor;
}
/***********************/
/********* Implement forwarding for edge nodes. ********/
action simple_forward(egressSpec_t port) {
standard_metadata.egress_spec = port;
}
table edge_forward {
key = {
hdr.ipv4.dstAddr: exact;
}
actions = {
simple_forward;
drop;
}
size = EGDE_HOSTS;
default_action = drop();
}
/******************************************************/
action update_ingress_statistics() {
util_t util;
time_t last_update;
time_t curr_time = standard_metadata.ingress_global_timestamp;
bit<32> port= (bit<32>) standard_metadata.ingress_port;
port_util.read(util, port);
port_util_last_updated.read(last_update, port);
bit<8> delta_t = (bit<8>) (curr_time - last_update);
util = (((bit<8>) standard_metadata.packet_length + util) << PROBE_FREQ_FACTOR) - delta_t;
util = util >> PROBE_FREQ_FACTOR;
port_util.write(port, util);
port_util_last_updated.write(port, curr_time);
}
apply {
drop();
get_dst_tor.apply();
update_ingress_statistics();
if (hdr.ipv4.isValid()) {
hula_logic.apply();
if (hdr.hula.isValid()) {
standard_metadata.mcast_grp = (bit<16>)standard_metadata.ingress_port;
}
if (meta.dst_tor == meta.self_id) {
edge_forward.apply();
}
}
}
}
/*************************************************************************
**************** E G R E S S P R O C E S S I N G *******************
*************************************************************************/
control MyEgress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
apply { }
}
/*************************************************************************
************* C H E C K S U M C O M P U T A T I O N **************
*************************************************************************/
control MyComputeChecksum(inout headers hdr, inout metadata meta) {
apply {
update_checksum(
hdr.ipv4.isValid(),
{ hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.totalLen,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.fragOffset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr },
hdr.ipv4.hdrChecksum,
HashAlgorithm.csum16);
}
}
/*************************************************************************
*********************** D E P A R S E R *******************************
*************************************************************************/
control MyDeparser(packet_out packet, in headers hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv4);
packet.emit(hdr.tcp);
packet.emit(hdr.hula);
}
}
/*************************************************************************
*********************** S W I T C H *******************************
*************************************************************************/
V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;