blob: f5a785f248b14f27e46fa0a552c1121007271a9c [file] [log] [blame]
Yi Tseng21629932017-06-06 11:17:43 -07001#ifndef PARSERS
2#define PARSERS
3#include "headers.p4"
Yi Tseng21629932017-06-06 11:17:43 -07004
Carmelo Cascone837e6452017-07-19 20:35:22 -04005parser ParserImpl(packet_in packet, out headers_t hdr, inout metadata_t meta,
6 inout standard_metadata_t standard_metadata) {
Yi Tseng21629932017-06-06 11:17:43 -07007
Carmelo Cascone837e6452017-07-19 20:35:22 -04008 state parse_packet_out {
9 packet.extract(hdr.packet_out);
10 transition parse_ethernet;
11 }
12
Yi Tseng21629932017-06-06 11:17:43 -070013 state parse_ethernet {
14 packet.extract(hdr.ethernet);
15 transition select(hdr.ethernet.etherType) {
16 ETH_TYPE_IPV4: parse_ipv4;
17 default: accept;
18 }
19 }
20
21 state parse_ipv4 {
22 packet.extract(hdr.ipv4);
23 transition select(hdr.ipv4.protocol) {
24 IP_TYPE_TCP: parse_tcp;
25 IP_TYPE_UDP: parse_udp;
26 default: accept;
27 }
28 }
29
30 state parse_tcp {
31 packet.extract(hdr.tcp);
32 transition accept;
33 }
34
35 state parse_udp {
36 packet.extract(hdr.udp);
37 transition accept;
38 }
39
40 state start {
Carmelo Cascone837e6452017-07-19 20:35:22 -040041 transition select(standard_metadata.ingress_port) {
42 CPU_PORT: parse_packet_out;
43 default: parse_ethernet;
44 }
Yi Tseng21629932017-06-06 11:17:43 -070045 }
46}
47
Carmelo Cascone837e6452017-07-19 20:35:22 -040048control DeparserImpl(packet_out packet, in headers_t hdr) {
Yi Tseng21629932017-06-06 11:17:43 -070049 apply {
Carmelo Cascone837e6452017-07-19 20:35:22 -040050 packet.emit(hdr.packet_in);
Yi Tseng21629932017-06-06 11:17:43 -070051 packet.emit(hdr.ethernet);
52 packet.emit(hdr.ipv4);
53 packet.emit(hdr.udp);
54 packet.emit(hdr.tcp);
55 }
56}
57#endif