| #ifndef __PARSER_P4__ |
| #define __PARSER_P4__ |
| #include "defines.p4" |
| #include "headers.p4" |
| |
| header packet_in_t packet_in_hdr; |
| header packet_out_t packet_out_hdr; |
| header ethernet_t ethernet; |
| header ipv4_t ipv4; |
| header tcp_t tcp; |
| header udp_t udp; |
| |
| parser start { |
| #ifdef __TOFINO_BUILD__ |
| // Hack to force deparsing of packet_in hdr. |
| // We assume it's impossible to receive a pkt with the first 8 bits of etherType 0. |
| // p4c-tofino complains when switching over 16 bits, i.e. the full etherType |
| // FIXME: This solution appears to be very brittle!!! |
| return select(current(96, 8)) { |
| 0 : parse_pkt_in; |
| #else |
| return select(EGR_PORT_FIELD) { |
| CPU_PORT : parse_pkt_in; |
| #endif |
| default : default_parser; |
| } |
| } |
| |
| parser parse_pkt_in { |
| extract(packet_in_hdr); |
| return parse_ethernet; |
| } |
| |
| parser default_parser { |
| return select(IGR_PORT_FIELD) { |
| CPU_PORT : parse_pkt_out; |
| default : parse_ethernet; |
| } |
| } |
| |
| parser parse_pkt_out { |
| extract(packet_out_hdr); |
| return parse_ethernet; |
| } |
| |
| parser parse_ethernet { |
| extract(ethernet); |
| return select(latest.etherType) { |
| ETHERTYPE_IPV4 : parse_ipv4; |
| default : ingress; |
| } |
| } |
| |
| parser parse_ipv4 { |
| extract(ipv4); |
| return select(latest.fragOffset, latest.protocol) { |
| IP_PROTOCOLS_TCP : parse_tcp; |
| IP_PROTOCOLS_UDP : parse_udp; |
| default: ingress; |
| } |
| } |
| |
| parser parse_tcp { |
| extract(tcp); |
| return ingress; |
| } |
| |
| parser parse_udp { |
| extract(udp); |
| return ingress; |
| } |
| |
| #endif |