-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.p4
169 lines (146 loc) · 3.29 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
#include <core.p4>
#include <v1model.p4>
typedef bit<48> EthernetAddress;
typedef bit<32> IPv4Address;
typedef bit<128> IPv6Address;
header ethernet_t {
EthernetAddress dst_addr;
EthernetAddress src_addr;
bit<16> ether_type;
}
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;
}
header ipv6_t {
bit<4> version;
bit<8> traffic_class;
bit<20> flow_label;
bit<16> payload_length;
bit<8> next_header;
bit<8> hop_limit;
IPv6Address source_address;
IPv6Address dest_address;
}
header packet_data {
varbit<256> data;
}
struct headers_t {
ethernet_t ethernet;
ipv4_t ipv4;
ipv6_t ipv6;
packet_data data;
}
struct metadata_t {
}
error {
IPv4IncorrectVersion,
IPv4OptionsNotSupported
}
parser my_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) {
0x0800: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hd.ipv4);
verify(hd.ipv4.version == 4w4, error.IPv4IncorrectVersion);
verify(hd.ipv4.ihl == 4w5, error.IPv4OptionsNotSupported);
hd.ethernet.ether_type = 0x86DD;
hd.ipv6.setValid();
hd.ipv6.version = 4w6;
hd.ipv6.traffic_class = hd.ipv4.diffserv;
hd.ipv6.flow_label = 0;
hd.ipv6.payload_length = hd.ipv4.total_len - 20;
hd.ipv6.next_header = hd.ipv4.protocol;
hd.ipv6.hop_limit = hd.ipv4.ttl;
hd.ipv6.source_address = (bit<128>) hd.ipv4.src_addr << 96;
hd.ipv6.dest_address = (bit<128>) hd.ipv4.dst_addr << 96;
transition accept;
}
state parse_data {
//Extract packet data
packet.extract(hd.data,(bit<32>) hd.ipv6.payload_length);
transition accept;
}
}
//TODO: Emit data
control my_deparser(packet_out packet,
in headers_t hdr)
{
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv6);
}
}
//TODO: Verify cksum
control my_verify_checksum(inout headers_t hdr,
inout metadata_t meta)
{
apply { }
}
control my_compute_checksum(inout headers_t hdr,
inout metadata_t meta)
{
apply {
update_checksum(true,hdr.ipv4,hdr.ipv4.hdr_checksum,HashAlgorithm.csum16);
}
}
control my_ingress(inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t standard_metadata)
{
bool dropped = false;
action drop_action() {
mark_to_drop(standard_metadata);
dropped = true;
}
action to_port_action(bit<9> port) {
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
standard_metadata.egress_spec = port;
}
table ipv4_match {
key = {
hdr.ipv4.dst_addr: lpm;
}
actions = {
drop_action;
to_port_action;
}
size = 1024;
default_action = drop_action;
}
apply {
ipv4_match.apply();
if (dropped) return;
}
}
control my_egress(inout headers_t hdr,
inout metadata_t meta,
inout standard_metadata_t standard_metadata)
{
apply { }
}
V1Switch(my_parser(),
my_verify_checksum(),
my_ingress(),
my_egress(),
my_compute_checksum(),
my_deparser()) main;