This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
xdp1.p4
74 lines (62 loc) · 1.72 KB
/
xdp1.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
/*
Copyright 2017 VMWare, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "xdp_model.p4"
header Ethernet {
bit<48> destination;
bit<48> source;
bit<16> protocol;
}
header IPv4 {
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;
}
struct Headers {
Ethernet ethernet;
IPv4 ipv4;
}
parser Parser(packet_in packet, out Headers hd) {
state start {
packet.extract(hd.ethernet);
transition select(hd.ethernet.protocol) {
16w0x800: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
packet.extract(hd.ipv4);
transition accept;
}
}
control Ingress(inout Headers hdr, in xdp_input xin, out xdp_output xout) {
apply {
xout.output_port = 0;
xout.output_action = (hdr.ethernet.protocol != 0x800) ? xdp_action.XDP_DROP : xdp_action.XDP_PASS;
}
}
control Deparser(in Headers hdrs, packet_out packet) {
apply {
packet.emit(hdrs.ethernet);
packet.emit(hdrs.ipv4);
}
}
xdp(Parser(), Ingress(), Deparser()) main;