Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 1 | #ifndef PARSERS |
| 2 | #define PARSERS |
| 3 | #include "headers.p4" |
| 4 | #include "metadata.p4" |
| 5 | |
| 6 | #define ETH_TYPE_IPV4 16w0x0800 |
| 7 | #define IP_TYPE_TCP 8w6 |
| 8 | #define IP_TYPE_UDP 8w17 |
| 9 | |
| 10 | parser ParserImpl(packet_in packet, out headers hdr, inout metadata meta, inout standard_metadata_t standard_metadata) { |
| 11 | state parse_ethernet { |
| 12 | packet.extract(hdr.ethernet); |
| 13 | transition select(hdr.ethernet.etherType) { |
| 14 | ETH_TYPE_IPV4: parse_ipv4; |
| 15 | default: accept; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | state parse_ipv4 { |
| 20 | packet.extract(hdr.ipv4); |
| 21 | transition select(hdr.ipv4.protocol) { |
| 22 | IP_TYPE_TCP: parse_tcp; |
| 23 | IP_TYPE_UDP: parse_udp; |
| 24 | default: accept; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | state parse_tcp { |
| 29 | packet.extract(hdr.tcp); |
| 30 | transition accept; |
| 31 | } |
| 32 | |
| 33 | state parse_udp { |
| 34 | packet.extract(hdr.udp); |
| 35 | transition accept; |
| 36 | } |
| 37 | |
| 38 | state start { |
| 39 | transition parse_ethernet; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | control DeparserImpl(packet_out packet, in headers hdr) { |
| 44 | apply { |
| 45 | packet.emit(hdr.ethernet); |
| 46 | packet.emit(hdr.ipv4); |
| 47 | packet.emit(hdr.udp); |
| 48 | packet.emit(hdr.tcp); |
| 49 | } |
| 50 | } |
| 51 | #endif |