Carmelo Cascone | 16de6db | 2017-08-22 00:27:57 +0200 | [diff] [blame] | 1 | #ifndef __PARSER_P4__ |
| 2 | #define __PARSER_P4__ |
| 3 | #include "defines.p4" |
| 4 | #include "headers.p4" |
| 5 | |
| 6 | header packet_in_t packet_in_hdr; |
| 7 | header packet_out_t packet_out_hdr; |
| 8 | header ethernet_t ethernet; |
| 9 | header ipv4_t ipv4; |
| 10 | header tcp_t tcp; |
| 11 | header udp_t udp; |
Carmelo Cascone | 4f01102 | 2017-06-05 01:41:02 -0400 | [diff] [blame] | 12 | |
| 13 | parser start { |
Carmelo Cascone | 465ec2c | 2017-09-12 19:54:43 +0200 | [diff] [blame] | 14 | #ifdef __TOFINO_BUILD__ |
Carmelo Cascone | f2ac720 | 2017-09-08 00:57:44 +0200 | [diff] [blame] | 15 | // Hack to force deparsing of packet_in hdr. |
| 16 | // We assume it's impossible to receive a pkt with the first 8 bits of etherType 0. |
| 17 | // p4c-tofino complains when switching over 16 bits, i.e. the full etherType |
Carmelo Cascone | 465ec2c | 2017-09-12 19:54:43 +0200 | [diff] [blame] | 18 | // FIXME: This solution appears to be very brittle!!! |
Carmelo Cascone | f2ac720 | 2017-09-08 00:57:44 +0200 | [diff] [blame] | 19 | return select(current(96, 8)) { |
| 20 | 0 : parse_pkt_in; |
Carmelo Cascone | 465ec2c | 2017-09-12 19:54:43 +0200 | [diff] [blame] | 21 | #else |
| 22 | return select(EGR_PORT_FIELD) { |
| 23 | CPU_PORT : parse_pkt_in; |
Carmelo Cascone | 6230a61 | 2017-09-13 03:25:41 +0200 | [diff] [blame] | 24 | #endif |
Carmelo Cascone | f2ac720 | 2017-09-08 00:57:44 +0200 | [diff] [blame] | 25 | default : default_parser; |
Carmelo Cascone | 16de6db | 2017-08-22 00:27:57 +0200 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | parser parse_pkt_in { |
| 30 | extract(packet_in_hdr); |
Carmelo Cascone | 4f01102 | 2017-06-05 01:41:02 -0400 | [diff] [blame] | 31 | return parse_ethernet; |
| 32 | } |
| 33 | |
Carmelo Cascone | 16de6db | 2017-08-22 00:27:57 +0200 | [diff] [blame] | 34 | parser default_parser { |
| 35 | return select(IGR_PORT_FIELD) { |
| 36 | CPU_PORT : parse_pkt_out; |
| 37 | default : parse_ethernet; |
| 38 | } |
| 39 | } |
Carmelo Cascone | 4f01102 | 2017-06-05 01:41:02 -0400 | [diff] [blame] | 40 | |
Carmelo Cascone | 16de6db | 2017-08-22 00:27:57 +0200 | [diff] [blame] | 41 | parser parse_pkt_out { |
| 42 | extract(packet_out_hdr); |
| 43 | return parse_ethernet; |
| 44 | } |
Carmelo Cascone | 4f01102 | 2017-06-05 01:41:02 -0400 | [diff] [blame] | 45 | |
| 46 | parser parse_ethernet { |
| 47 | extract(ethernet); |
| 48 | return select(latest.etherType) { |
| 49 | ETHERTYPE_IPV4 : parse_ipv4; |
| 50 | default : ingress; |
| 51 | } |
| 52 | } |
| 53 | |
Carmelo Cascone | 4f01102 | 2017-06-05 01:41:02 -0400 | [diff] [blame] | 54 | parser parse_ipv4 { |
| 55 | extract(ipv4); |
| 56 | return select(latest.fragOffset, latest.protocol) { |
| 57 | IP_PROTOCOLS_TCP : parse_tcp; |
| 58 | IP_PROTOCOLS_UDP : parse_udp; |
| 59 | default: ingress; |
| 60 | } |
| 61 | } |
| 62 | |
Carmelo Cascone | 4f01102 | 2017-06-05 01:41:02 -0400 | [diff] [blame] | 63 | parser parse_tcp { |
| 64 | extract(tcp); |
| 65 | return ingress; |
| 66 | } |
| 67 | |
Carmelo Cascone | 4f01102 | 2017-06-05 01:41:02 -0400 | [diff] [blame] | 68 | parser parse_udp { |
| 69 | extract(udp); |
| 70 | return ingress; |
Carmelo Cascone | 16de6db | 2017-08-22 00:27:57 +0200 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | #endif |