-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetecting_high_load.p4
452 lines (392 loc) · 11.4 KB
/
detecting_high_load.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#include <core.p4>
#include <v1model.p4>
typedef bit<48> EthernetAddress;
typedef bit<32> IPv4Address;
typedef bit <16> PortIdToController_t;
header ethernet_t {
bit<48> dstAddr;
bit<48> srcAddr;
bit<16> etherType;
}
// IPv4 header _with_ options
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;
IPv4Address srcAddr;
IPv4Address dstAddr;
varbit<320> options;
}
header tcp_t {
bit<16> srcPort;
bit<16> dstPort;
bit<32> seqNo;
bit<32> ackNo;
bit<4> dataOffset;
bit<3> res;
bit<3> ecn;
bit<1> urg;
bit<1> ack;
bit<1> psh;
bit<1> rst;
bit<1> syn;
bit<1> fin;
bit<16> window;
bit<16> checksum; // Includes Pseudo Hdr + TCP segment (hdr + payload)
bit<16> urgentPtr;
varbit<320> options;
}
header udp_t {
bit<16> srcPort;
bit<16> dstPort;
bit<16> length_;
bit<16> checksum;
}
#define CPU_PORT 510
#define max_long_connection 10
#define max_time_to_complete 10000000
header packet_in_t
{
PortIdToController_t input_port;
bit<32> operand0;
}
header IPv4_up_to_ihl_only_h {
bit<4> version;
bit<4> ihl;
}
header tcp_upto_data_offset_only_h {
bit<16> srcPort;
bit<16> dstPort;
bit<32> seqNo;
bit<32> ackNo;
// dataOffset in TCP hdr uses 4 bits but needs padding.
// If 4 bits are used for it, p4c-bm2-ss complains the header
// is not a multiple of 8 bits.
bit<4> dataOffset;
bit<4> dontCare;
}
struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
tcp_t tcp;
udp_t udp;
packet_in_t packet_in;
}
struct mystruct1_t {
bit<4> a;
bit<4> b;
}
struct metadata {
mystruct1_t mystruct1;
bit<16> l4Len; // includes TCP hdr len + TCP payload len in bytes.
}
typedef tuple<
bit<4>,
bit<4>,
bit<8>,
varbit<56>
> myTuple1;
// Declare user-defined errors that may be signaled during parsing
error {
IPv4HeaderTooShort,
TCPHeaderTooShort,
IPv4IncorrectVersion,
IPv4ChecksumError
}
parser parserI(packet_in pkt,
out headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta)
{
state start {
pkt.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
0x0800: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
// The 4-bit IHL field of the IPv4 base header is the number
// of 32-bit words in the entire IPv4 header. It is an error
// for it to be less than 5. There are only IPv4 options
// present if the value is at least 6. The length of the IPv4
// options alone, without the 20-byte base header, is thus ((4
// * ihl) - 20) bytes, or 8 times that many bits.
pkt.extract(hdr.ipv4,
(bit<32>)
(8 *
(4 * (bit<9>) (pkt.lookahead<IPv4_up_to_ihl_only_h>().ihl)
- 20)));
verify(hdr.ipv4.version == 4w4, error.IPv4IncorrectVersion);
verify(hdr.ipv4.ihl >= 4w5, error.IPv4HeaderTooShort);
meta.l4Len = hdr.ipv4.totalLen - (bit<16>)(hdr.ipv4.ihl)*4;
transition select (hdr.ipv4.protocol) {
6: parse_tcp;
17: parse_udp;
default: accept;
}
}
state parse_tcp {
// The 4-bit dataOffset field of the TCP base header is the number
// of 32-bit words in the entire TCP header. It is an error
// for it to be less than 5. There are only TCP options
// present if the value is at least 6. The length of the TCP
// options alone, without the 20-byte base header, is thus ((4
// * dataOffset) - 20) bytes, or 8 times that many bits.
pkt.extract(hdr.tcp,
(bit<32>)
(8 *
(4 * (bit<9>) (pkt.lookahead<tcp_upto_data_offset_only_h>().dataOffset)
- 20)));
verify(hdr.tcp.dataOffset >= 4w5, error.TCPHeaderTooShort);
transition accept;
}
state parse_udp {
pkt.extract(hdr.udp);
transition accept;
}
}
control cIngress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta)
{
@name("counting_asset") register<bit<48>>(100) counting_asset;
bit<48> x;
@name("complete_time") register<bit<48>>(10) complete_time;
@name ("packet_syn_time") register <bit<48>> (10) packet_syn_time;
@name ("number_of_access") register <bit<48>> (10) number_of_access;
@name ("long_connections") register <bit<48>> (10) long_connections;
action count_access (bit<48> src_mac,bit<48> dst_mac,bit<32> src_ip, bit<32> dst_ip, bit<9> port){
@atomic{
number_of_access.read(x,(bit<32>)hdr.tcp.dstPort);
x = x + 1;
number_of_access.write( (bit<32>)hdr.tcp.dstPort , x);
hdr.ethernet.srcAddr=src_mac;
hdr.ethernet.dstAddr=dst_mac;
hdr.ipv4.dstAddr=dst_ip;
hdr.ipv4.srcAddr=src_ip;
stdmeta.egress_spec = port;
}
}
action send_to_client(bit<48> src_mac,bit<48> dst_mac,bit<32> src_ip, bit<32> dst_ip, bit<9> port){
hdr.ethernet.srcAddr=src_mac;
hdr.ethernet.dstAddr=dst_mac;
hdr.ipv4.dstAddr=dst_ip;
hdr.ipv4.srcAddr=src_ip;
stdmeta.egress_spec = port;
}
action send_to_controller_packet_in ()
{
stdmeta.egress_spec = CPU_PORT;
hdr.packet_in.setValid();
}
table count_number_of_flows{
key = {
hdr.ipv4.dstAddr : lpm;
}
actions = {count_access;NoAction;}
default_action = NoAction;
}
table ipv4_match {
key = {
hdr.ipv4.dstAddr : lpm;
}
actions = {send_to_nat;NoAction; }
default_action = NoAction;
}
table decrease_number_of_flows {
key = {
hdr.ipv4.dstAddr : lpm;
}
actions = {decrease_access;NoAction;}
default_action = NoAction;
}
table tcp_port_match {
key = {
hdr.tcp.dstPort : exact;
}
actions = {count_access;NoAction; }
default_action = NoAction;
}
table tcp_srcport_match {
key = {
hdr.tcp.srcPort :exact;
}
actions = {send_to_client; NoAction; }
default_action = NoAction;
}
apply {
@atomic{
if (hdr.tcp.syn == 1)
{
packet_syn_time.write ((bit<32>) hdr.tcp.dstPort, stdmeta.ingress_global_timestamp);
}
if (hdr.tcp.fin == 1)
{
bit <48> time_to_complete ;
bit <48> t_syn ;
bit <48> connections = 0;
packet_syn_time.read ( t_syn, (bit<32>)hdr.tcp.dstPort);
time_to_complete = stdmeta.ingress_global_timestamp - t_syn;
if (time_to_complete > max_time_to_complete )
{
long_connections.read(connections, (bit<32>)hdr.tcp.dstPort);
connections = connections + 1;
long_connections.write((bit<32>) hdr.tcp.dstPort, connections);
}
if (connections > max_long_connection )
{
stdmeta.egress_spec = CPU_PORT;
hdr.packet_in.setValid();
}
}
tcp_port_match.apply();
tcp_srcport_match.apply();
}
}
}
control cEgress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta)
{
apply {
}
}
control vc(inout headers hdr,
inout metadata meta)
{
apply {
verify_checksum(true,
{ 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.options
},
hdr.ipv4.hdrChecksum, HashAlgorithm.csum16);
verify_checksum_with_payload(hdr.tcp.isValid(),
{ hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
8w0,
hdr.ipv4.protocol,
meta.l4Len,
hdr.tcp.srcPort,
hdr.tcp.dstPort,
hdr.tcp.seqNo,
hdr.tcp.ackNo,
hdr.tcp.dataOffset,
hdr.tcp.res,
hdr.tcp.ecn,
hdr.tcp.urg,
hdr.tcp.ack,
hdr.tcp.psh,
hdr.tcp.rst,
hdr.tcp.syn,
hdr.tcp.fin,
hdr.tcp.window,
16w0,
hdr.tcp.urgentPtr,
hdr.tcp.options
},
hdr.tcp.checksum, HashAlgorithm.csum16);
verify_checksum_with_payload(hdr.udp.isValid(),
{ hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
8w0,
hdr.ipv4.protocol,
meta.l4Len,
hdr.udp.srcPort,
hdr.udp.dstPort,
hdr.udp.length_
},
hdr.udp.checksum, HashAlgorithm.csum16);
}
}
control uc(inout headers hdr,
inout metadata meta)
{
apply {
update_checksum(true,
{ 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.options
},
hdr.ipv4.hdrChecksum, HashAlgorithm.csum16);
update_checksum_with_payload(hdr.tcp.isValid(),
{ hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
8w0,
hdr.ipv4.protocol,
meta.l4Len,
hdr.tcp.srcPort,
hdr.tcp.dstPort,
hdr.tcp.seqNo,
hdr.tcp.ackNo,
hdr.tcp.dataOffset,
hdr.tcp.res,
hdr.tcp.ecn,
hdr.tcp.urg,
hdr.tcp.ack,
hdr.tcp.psh,
hdr.tcp.rst,
hdr.tcp.syn,
hdr.tcp.fin,
hdr.tcp.window,
16w0,
hdr.tcp.urgentPtr,
hdr.tcp.options
},
hdr.tcp.checksum, HashAlgorithm.csum16);
update_checksum_with_payload(hdr.udp.isValid(),
{ hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
8w0,
hdr.ipv4.protocol,
meta.l4Len,
hdr.udp.srcPort,
hdr.udp.dstPort,
hdr.udp.length_
},
hdr.udp.checksum, HashAlgorithm.csum16);
}
}
control DeparserI(packet_out packet,
in headers hdr)
{
apply {
packet.emit(hdr.packet_in);
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv4);
packet.emit(hdr.tcp);
packet.emit(hdr.udp);
}
}
V1Switch<headers, metadata>(parserI(),
vc(),
cIngress(),
cEgress(),
uc(),
DeparserI()) main;