-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pipeline.p4
182 lines (153 loc) · 3.6 KB
/
test_pipeline.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
#include <core.p4>
#include <v1model.p4>
#define IPV4_ETHTYPE 0x0800
typedef bit<48> EthernetAddress;
typedef bit<32> IPv4Address;
typedef bit<128> IPv6Address;
typedef bit<128> IPv4ORv6Address;
header ethernet_t {
EthernetAddress dst_addr;
EthernetAddress src_addr;
bit<16> ether_type;
}
const bit<16> ETHER_HDR_SIZE=112/8;
header ipv4_t {
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;
IPv4Address src_addr;
IPv4Address dst_addr;
}
struct headers_t {
ethernet_t ethernet;
ipv4_t ipv4;
}
enum bit<16> direction_t {
INVALID = 0,
OUTBOUND = 1,
INBOUND = 2
}
struct metadata_t {
bool dropped;
direction_t direction;
IPv4Address dst_ip_addr;
IPv4Address src_ip_addr;
EthernetAddress ethernet_addr;
}
parser test_parser(packet_in packet,
out headers_t hd,
inout metadata_t meta,
inout standard_metadata_t standard_meta)
{
state start {
packet.extract(hd.ethernet);
transition select(hd.ethernet.ether_type) {
IPV4_ETHTYPE: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hd.ipv4);
transition accept;
}
}
control test_deparser(packet_out packet,
in headers_t hdr)
{
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv4);
}
}
control test_verify_checksum(inout headers_t hdr,
inout metadata_t meta)
{
apply { }
}
control test_compute_checksum(inout headers_t hdr,
inout metadata_t meta)
{
apply { }
}
control test_ingress(inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t standard_metadata)
{
action drop_action() {
mark_to_drop(standard_metadata);
}
action action3() {
meta.dropped = true;
}
action action2() {
meta.dst_ip_addr = hdr.ipv4.dst_addr;
}
action action1() {
meta.src_ip_addr = hdr.ipv4.dst_addr;
}
table pre_tbl1 {
key = {
hdr.ipv4.dst_addr : exact;
}
actions = {
action1;
action2;
action3;
}
}
action action4() {
meta.direction = direction_t.OUTBOUND;
}
table in_tbl2 {
key = {
hdr.ipv4.protocol : exact;
}
actions = {
action4;
}
}
action action5(EthernetAddress neighbor_mac) {
meta.ethernet_addr = neighbor_mac;
}
table post_tbl3 {
key = {
hdr.ipv4.src_addr : exact;
}
actions = {
action5;
}
}
apply {
/* Send packet on same port it arrived (echo) by default */
standard_metadata.egress_spec = standard_metadata.ingress_port;
switch (pre_tbl1.apply().action_run) {
action1: // Fall-through
action2: {
in_tbl2.apply();
}
}
post_tbl3.apply();
if (meta.dropped) {
drop_action();
}
}
}
control test_egress(inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t standard_metadata)
{
apply { }
}
V1Switch(test_parser(),
test_verify_checksum(),
test_ingress(),
test_egress(),
test_compute_checksum(),
test_deparser()) main;