blob: 68f3876541ae07ce45ab8a64ddd73d9edcc39d62 [file] [log] [blame]
Yi Tseng21629932017-06-06 11:17:43 -07001#ifndef PARSERS
2#define PARSERS
3#include "headers.p4"
4#include "metadata.p4"
5
Carmelo Cascone837e6452017-07-19 20:35:22 -04006parser ParserImpl(packet_in packet, out headers_t hdr, inout metadata_t meta,
7 inout standard_metadata_t standard_metadata) {
Yi Tseng21629932017-06-06 11:17:43 -07008
Carmelo Cascone837e6452017-07-19 20:35:22 -04009 state parse_packet_out {
10 packet.extract(hdr.packet_out);
11 transition parse_ethernet;
12 }
13
Yi Tseng21629932017-06-06 11:17:43 -070014 state parse_ethernet {
15 packet.extract(hdr.ethernet);
16 transition select(hdr.ethernet.etherType) {
17 ETH_TYPE_IPV4: parse_ipv4;
18 default: accept;
19 }
20 }
21
22 state parse_ipv4 {
23 packet.extract(hdr.ipv4);
24 transition select(hdr.ipv4.protocol) {
25 IP_TYPE_TCP: parse_tcp;
26 IP_TYPE_UDP: parse_udp;
27 default: accept;
28 }
29 }
30
31 state parse_tcp {
32 packet.extract(hdr.tcp);
33 transition accept;
34 }
35
36 state parse_udp {
37 packet.extract(hdr.udp);
38 transition accept;
39 }
40
41 state start {
Carmelo Cascone837e6452017-07-19 20:35:22 -040042 transition select(standard_metadata.ingress_port) {
43 CPU_PORT: parse_packet_out;
44 default: parse_ethernet;
45 }
Yi Tseng21629932017-06-06 11:17:43 -070046 }
47}
48
Carmelo Cascone837e6452017-07-19 20:35:22 -040049control DeparserImpl(packet_out packet, in headers_t hdr) {
Yi Tseng21629932017-06-06 11:17:43 -070050 apply {
Carmelo Cascone837e6452017-07-19 20:35:22 -040051 packet.emit(hdr.packet_in);
Yi Tseng21629932017-06-06 11:17:43 -070052 packet.emit(hdr.ethernet);
53 packet.emit(hdr.ipv4);
54 packet.emit(hdr.udp);
55 packet.emit(hdr.tcp);
56 }
57}
58#endif