This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ovs.p4
345 lines (308 loc) · 7.32 KB
/
ovs.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
/*
OVS.p4 using tc-ebpf P4 architecture
Requirements:
- This should be an example which uses tc-ebpf P4 model to implement
all the protocols supported by OVS.
Protocol supports:
- L2: VLAN,...
- L3: ...
Tunneling:
- Support Linux's bpf tunnel protocol (ipip, vxlan, gre, geneve, etc)
*/
// TODO: define a new model ovs_ebpf_model.p4
#include <ebpf_model.p4>
#define OUTPUT_OFS (1<<0)
#define PUSHVLAN_OFS (1<<1)
#define POPVLAN_OFS (1<<2)
#define SETMASKED_OFS (1<<3)
#define SETTUNEL_OFS (1<<4)
#define TRUNC_OFS (1<<5)
extern void bpf_skb_clone_redirect(in bit<16> port);
extern void bpf_skb_vlan_push(in bit<16> proto, in bit<16> tci);
extern void bpf_skb_vlan_pop();
struct pkt_metadata_t {
bit<32> recirc_id;
bit<32> dp_hash;
bit<32> skb_priority;
bit<32> pkt_mark;
bit<16> ct_state;
bit<16> ct_zone;
bit<32> ct_mark;
bit<128> ct_label;
bit<32> in_port;
}
struct flow_tnl_t {
bit<32> ip_dst;
bit<64> ipv6_dst;
bit<32> ip_src;
bit<64> ipv6_src;
bit<64> tun_id;
bit<16> flags;
bit<8> ip_tos;
bit<8> ip_ttl;
bit<16> tp_src;
bit<16> tp_dst;
bit<16> gbp_id;
bit<8> gbp_flags;
bit<40> pad1;
}
header arp_rarp_t {
bit<16> hwType;
bit<16> protoType;
bit<8> hwAddrLen;
bit<8> protoAddrLen;
bit<16> opcode;
}
header ethernet_t {
bit<48> dstAddr;
bit<48> srcAddr;
bit<16> etherType;
}
header icmp_t {
bit<16> typeCode;
bit<16> hdrChecksum;
}
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;
bit<32> srcAddr;
bit<32> dstAddr;
}
header ipv6_t {
bit<4> version;
bit<8> trafficClass;
bit<20> flowLabel;
bit<16> payloadLen;
bit<8> nextHdr;
bit<8> hopLimit;
bit<128> srcAddr;
bit<128> dstAddr;
}
header tcp_t {
bit<16> srcPort;
bit<16> dstPort;
bit<32> seqNo;
bit<32> ackNo;
bit<4> dataOffset;
bit<4> res;
bit<8> flags;
bit<16> window;
bit<16> checksum;
bit<16> urgentPtr;
}
header udp_t {
bit<16> srcPort;
bit<16> dstPort;
bit<16> length_;
bit<16> checksum;
}
header vlan_tag_t {
bit<3> pcp;
bit<1> cfi;
bit<12> vid;
bit<16> etherType;
}
struct metadata {
pkt_metadata_t md;
flow_tnl_t tnl;
}
struct ovs_packet {
arp_rarp_t arp;
ethernet_t ethernet;
icmp_t icmp;
ipv4_t ipv4;
ipv6_t ipv6;
tcp_t tcp;
udp_t udp;
vlan_tag_t vlan;
}
// metadata to execute on actions
struct output_md_t {
bit<16> port; //ifindex
}
struct pushvlan_md_t {
bit<16> tci;
bit<16> proto;
}
struct settunnel_md_t {
bit<32> ip_dst;
bit<32> ip_src;
bit<64> tun_id;
bit<16> flags;
}
// it's better if we have union
struct action_md_t {
output_md_t output;
pushvlan_md_t pushvlan;
settunnel_md_t settunnel;
}
/* implement OVS's key_extract() in net/openvswitch/flow.c */
parser TopParser(packet_in packet, /*inout bpf_sk_buff skbU, */ out ovs_packet hdr)
{
state parse_arp {
packet.extract(hdr.arp);
transition accept;
}
state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
16w0x8100: parse_vlan;
16w0x88a8: parse_vlan;
16w0x806: parse_arp;
16w0x800: parse_ipv4;
16w0x86dd: parse_ipv6;
default: accept;
}
}
state parse_icmp {
packet.extract(hdr.icmp);
transition accept;
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition select(hdr.ipv4.protocol) {
8w6: parse_tcp;
8w17: parse_udp;
8w1: parse_icmp;
default: accept;
}
}
state parse_ipv6 {
packet.extract(hdr.ipv6);
transition select(hdr.ipv6.nextHdr) {
8w6: parse_tcp;
8w17: parse_udp;
8w1: parse_icmp;
default: accept;
}
}
state parse_tcp {
packet.extract(hdr.tcp);
transition accept;
}
state parse_udp {
packet.extract(hdr.udp);
transition accept;
}
state parse_vlan {
packet.extract(hdr.vlan);
transition select(hdr.vlan.etherType) {
16w0x806: parse_arp;
16w0x800: parse_ipv4;
16w0x86dd: parse_ipv6;
default: accept;
}
}
state start {
transition parse_ethernet;
}
}
control Ingress(inout ovs_packet hdr,
out bool pass)
{
// TODO: This should become an in parameter of Ingress
metadata md;
// TODO: this should become an out parameter of Ingress
bit<16> outputPort;
bit<32> toRun;
action_md_t action_md;
action Output()
{
outputPort = action_md.output.port;
//bpf_skb_clone_redirect(outputPort);
}
action PushVlan()
{
bit<16> tci = action_md.pushvlan.tci;
bit<16> proto = action_md.pushvlan.proto;
//bpf_skb_vlan_push(proto, tci);
}
action PopVlan()
{
//bpf_skb_vlan_pop();
}
action SetMasked()
{
//bpf_skb_store_byte
}
action SetTunnel()
{
// bpf_set_tunnel_key
}
action Trunc()
{
// bpf_skb_change_tail
}
// a flow miss upcall sends the packet with its md
// to the userspace (ovs-vswitchd), which will look
// up OF tables and install flow entry into map
action Upcall()
{
// bpf_perf_event_output(skb, &perf_events, len, msg, sizeof(msg));
}
// OVS datapath action dispatcher
// Execute a fixed sequence of actions
action OVS(bit<32> bitmap, action_md_t md) {
// the bitmap is set by the control plane and indicates
// which actions should be executed
toRun = bitmap;
// the action_md is the metadata for action to execute
action_md = md;
}
table ovsTable() {
key = { hdr.ipv4.srcAddr : exact; }
actions = {
OVS;
Upcall; // Send to userspce ovs-vswitchd
}
default_action = Upcall; //NoAction;
implementation = hash_table(1024);
}
apply {
pass = true;
toRun = 0;
ovsTable.apply();
// the sequece matters
if ((toRun & OUTPUT_OFS) != 0) {
Output();
}
if ((toRun & PUSHVLAN_OFS) != 0) {
PushVlan();
}
if ((toRun & POPVLAN_OFS) != 0) {
PopVlan();
}
if ((toRun & SETMASKED_OFS) != 0) {
SetMasked();
}
if ((toRun & SETTUNEL_OFS) != 0) {
SetTunnel();
}
if ((toRun & TRUNC_OFS) != 0) {
Trunc();
}
}
}
// TODO: this should be argument to the new model ovs_ebpf_model()
control Deparser(packet_out packet, in ovs_packet hdr) {
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.vlan);
packet.emit(hdr.ipv6);
packet.emit(hdr.ipv4);
packet.emit(hdr.icmp);
packet.emit(hdr.udp);
packet.emit(hdr.tcp);
packet.emit(hdr.arp);
}
}
// TODO: replace this with ovs_ebpf_model
ebpfFilter(TopParser(), Ingress()) main;