-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedge_switch.p4
385 lines (363 loc) · 10.5 KB
/
edge_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
/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>
//------------------------------------------------------------
// 定义协议号
const bit<16> TYPE_IPV4 = 0x0800;
const bit<16> TYPE_IPV6 = 0x86dd;
const bit<16> TYPE_SINET = 0x8999;
const bit<16> TYPE_ARP = 0x0806;
const bit<16> TYPE_PROBE = 0x0812;
const bit<8> IP_PROTO_TCP = 8w6;
const bit<8> IP_PROTO_UDP = 8w17;
const bit<8> IP_PROTO_ICMP = 8w1;
#define MAX_HOPS 10
#define MAX_PORTS 8
// 定义cpu端口号
#define CPU_PORT 255
//register<bit<32>>(MAX_PORTS) byte_cnt_reg; // 存储接口累积流量,INT协议使用
//register<time_t>(MAX_PORTS) last_time_reg; // 存储上一个INT包到达时间,INT协议使用
//----------------------------- HEADER -------------------------------
// 定义首部
// 物理层首部
header ethernet_h {
bit<48> dst_mac;
bit<48> src_mac;
bit<16> ether_type;
}
//--------------------------
// ARP首部
header arp_h {
bit<16> hardware_type;
bit<16> protocol_type;
bit<8> HLEN;
bit<8> PLEN;
bit<16> OPER;
bit<48> sender_ha;
bit<32> sender_ip;
bit<48> target_ha;
bit<32> target_ip;
}
//--------------------------
// IPv4首部
header ipv4_h {
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;
bit<32> src_addr;
bit<32> dst_addr;
}
//--------------------------
//IPv6首部
header ipv6_h {
bit<4> version;
bit<8> traffic_class;
bit<20> flow_label;
bit<16> payload_len;
bit<8> next_header;
bit<8> hop_limit;
bit<128> src_addr;
bit<128> dst_addr;
}
//--------------------------
//INT首部
header probe_h {
bit<8> hop_cnt; // probe_fwd字段个数
bit<8> data_cnt; // probe_data字段个数
}
//--------------------------
header probe_fwd_h {
bit<8> swid; // 交换机标识
}
//--------------------------
header probe_data_h {
bit<8> swid; // 交换机标识
bit<8> port; // 端口号
bit<32> byte_cnt; // 流量
bit<32> pckcont; // 入口数据包个数
bit<32> enpckcont; // 出口数据包个数
bit<48> last_time; // 上一个INT包到达时间
bit<48> cur_time; // 当前INT包到达时间
bit<32> qdepth; // 队列长度
}
//--------------------------
// ICMP首部
header icmp_h {
bit<8> type;
bit<8> code;
bit<16> hdr_checksum;
}
//--------------------------
//TCP首部
header tcp_h {
bit<16> src_port;
bit<16> dst_port;
bit<32> seq_no;
bit<32> ack_no;
bit<4> data_offset;
bit<4> res;
bit<8> flags;
bit<16> window;
bit<16> checksum;
bit<16> urgent_ptr;
}
//--------------------------
//UDP首部
header udp_h {
bit<16> src_port;
bit<16> dst_port;
bit<16> hdr_length;
bit<16> checksum;
}
//--------------------------
struct metadata {
bit<8> remaining1;
bit<8> remaining2;
bit<8> sswid;
bit<32> pktcont2;
bit<9> ingress_time;
bit<16> srcport;
bit<16> dstport;
}
//--------------------------
//完整首部
struct headers {
ethernet_h ethernet;
arp_h arp;
ipv4_h ipv4;
probe_h probe;
probe_fwd_h[MAX_HOPS] probe_fwd;
probe_data_h[MAX_HOPS] probe_data;
ipv6_h ipv6;
icmp_h icmp;
tcp_h tcp;
udp_h udp;
}
//----------------------------- PARSER -------------------------------
parser c_parser(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.ether_type) {
TYPE_ARP: parse_arp;
TYPE_IPV4: parse_ipv4;
TYPE_IPV6: parse_ipv6;
TYPE_PROBE: parse_probe;
default: accept;
}
}
state parse_arp {
packet.extract(hdr.arp);
transition accept;
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition select(hdr.ipv4.protocol) {
IP_PROTO_TCP: parse_tcp;
IP_PROTO_UDP: parse_udp;
default: accept;
}
}
state parse_tcp {
packet.extract(hdr.tcp);
transition accept;
}
state parse_udp {
packet.extract(hdr.udp);
transition accept;
}
state parse_icmp {
packet.extract(hdr.icmp);
transition accept;
}
state parse_ipv6 {
packet.extract(hdr.ipv6);
transition accept;
}
state parse_probe {
packet.extract(hdr.probe);
meta.remaining1 = hdr.probe.hop_cnt;
meta.remaining2 = hdr.probe.data_cnt;
transition select(hdr.probe.hop_cnt) {
0: parse_probe_data;
default: parse_probe_fwd;
}
}
state parse_probe_fwd {
packet.extract(hdr.probe_fwd.next);
meta.remaining1 = meta.remaining1 - 1;
transition select(meta.remaining1) {
0: parse_probe_data;
default: parse_probe_fwd;
}
}
state parse_probe_data {
packet.extract(hdr.probe_data.next);
meta.remaining2 = meta.remaining2 - 1;
transition select(meta.remaining2) {
0: accept;
default: parse_probe_data;
}
}
}
//------------------------ CONTROL ------------------------------------
control c_verify_checksum(inout headers hdr,
inout metadata meta) {
apply {
}
}
//------------------------------------------------------------
control c_ingress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
action _drop() {
mark_to_drop(standard_metadata);
}
action ipv4_forward(bit<48> dstAddr, bit<9> port) {
hdr.ethernet.src_mac = hdr.ethernet.dst_mac;
hdr.ethernet.dst_mac = dstAddr;
//hdr.ipv4.src_addr = src_addr;
//hdr.ipv4.dst_addr = dst_addr;
standard_metadata.egress_spec = port;
}
table ipv4_lpm {
key = {
hdr.ipv4.dst_addr: lpm;
}
actions = {
ipv4_forward;
_drop;
}
size = 1024;
default_action = _drop();
}
table srcport_match {
key = {
meta.srcport: exact;
}
actions = {
NoAction;
}
}
table dstport_match {
key = {
meta.dstport: exact;
}
actions = {
NoAction;
}
}
// 根据五元组来确定流,从而记录流特征,还得用标号标记每个流对应的寄存器索引
// direct_counter(CounterType.packets_and_bytes) packets_bytes_counter;
counter(4, CounterType.packets_and_bytes) packets_bytes_counter;
action record_flow(bit<32> index) {
// 记录流包数
// 记录流字节数
packets_bytes_counter.count((bit<32>) index);
}
table detect_flow {
key = {
hdr.ipv4.src_addr: exact;
hdr.ipv4.dst_addr: exact;
meta.srcport: exact;
meta.dstport: exact;
hdr.ipv4.protocol: exact;
}
actions = {
record_flow;
}
size = 1024;
// counters = packets_bytes_counter;
}
table check_flow {
key = {
hdr.ipv4.src_addr: exact;
hdr.ipv4.dst_addr: exact;
meta.srcport: exact;
meta.dstport: exact;
hdr.ipv4.protocol: exact;
}
actions = {
NoAction;
}
default_action = NoAction;
}
apply {
if (hdr.ipv4.isValid()) {
// 这里要有一个处理逻辑,根据端口号来排除一些常见的协议
if (hdr.tcp.isValid()) {
// 这里做判断,将一些包直接过滤掉
meta.srcport = hdr.tcp.src_port;
meta.dstport = hdr.tcp.dst_port;
}
else if (hdr.udp.isValid()) {
meta.srcport = hdr.udp.src_port;
meta.dstport = hdr.udp.dst_port;
}
if (hdr.ipv4.protocol == 17 || hdr.ipv4.protocol == 6){
//探测流是否需要记录
if (check_flow.apply().hit) {
//若需要记录,则记录相关的流特征
detect_flow.apply();
}
else {
//若没有,说明该流已上传过控制器或未上传过控制器,则需要判断是否需要clone
if (srcport_match.apply().miss && dstport_match.apply().miss) {
clone(CloneType.I2E, 100);
}
}
}
ipv4_lpm.apply();
}
}
}
//------------------------------------------------------------
control c_egress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
apply {
}
}
//------------------------------------------------------------
control c_compute_checksum(inout headers hdr,
inout metadata meta) {
apply {
}
}
//------------------------------------------------------------
control c_deparser(packet_out packet, in headers hdr) {
apply {
//packet.emit(hdr.packetin);
packet.emit(hdr.ethernet);
packet.emit(hdr.arp);
packet.emit(hdr.ipv4);
packet.emit(hdr.probe);
packet.emit(hdr.probe_fwd);
packet.emit(hdr.probe_data);
packet.emit(hdr.ipv6);
packet.emit(hdr.icmp);
packet.emit(hdr.tcp);
packet.emit(hdr.udp);
}
}
//------------------------------------------------------------
V1Switch(
c_parser(),
c_verify_checksum(),
c_ingress(),
c_egress(),
c_compute_checksum(),
c_deparser()
) main;