blob: e29a9f2f1db527429124b4c287545310e9e88a92 [file] [log] [blame]
Carmelo Cascone16de6db2017-08-22 00:27:57 +02001#ifndef __PARSER_P4__
2#define __PARSER_P4__
3#include "defines.p4"
4#include "headers.p4"
5
6header packet_in_t packet_in_hdr;
7header packet_out_t packet_out_hdr;
8header ethernet_t ethernet;
9header ipv4_t ipv4;
10header tcp_t tcp;
11header udp_t udp;
Carmelo Cascone4f011022017-06-05 01:41:02 -040012
13parser start {
Carmelo Cascone465ec2c2017-09-12 19:54:43 +020014 #ifdef __TOFINO_BUILD__
Carmelo Casconef2ac7202017-09-08 00:57:44 +020015 // 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 Cascone465ec2c2017-09-12 19:54:43 +020018 // FIXME: This solution appears to be very brittle!!!
Carmelo Casconef2ac7202017-09-08 00:57:44 +020019 return select(current(96, 8)) {
20 0 : parse_pkt_in;
Carmelo Cascone465ec2c2017-09-12 19:54:43 +020021 #else
22 return select(EGR_PORT_FIELD) {
23 CPU_PORT : parse_pkt_in;
Carmelo Cascone6230a612017-09-13 03:25:41 +020024 #endif
Carmelo Casconef2ac7202017-09-08 00:57:44 +020025 default : default_parser;
Carmelo Cascone16de6db2017-08-22 00:27:57 +020026 }
27}
28
29parser parse_pkt_in {
30 extract(packet_in_hdr);
Carmelo Cascone4f011022017-06-05 01:41:02 -040031 return parse_ethernet;
32}
33
Carmelo Cascone16de6db2017-08-22 00:27:57 +020034parser default_parser {
35 return select(IGR_PORT_FIELD) {
36 CPU_PORT : parse_pkt_out;
37 default : parse_ethernet;
38 }
39}
Carmelo Cascone4f011022017-06-05 01:41:02 -040040
Carmelo Cascone16de6db2017-08-22 00:27:57 +020041parser parse_pkt_out {
42 extract(packet_out_hdr);
43 return parse_ethernet;
44}
Carmelo Cascone4f011022017-06-05 01:41:02 -040045
46parser parse_ethernet {
47 extract(ethernet);
48 return select(latest.etherType) {
49 ETHERTYPE_IPV4 : parse_ipv4;
50 default : ingress;
51 }
52}
53
Carmelo Cascone4f011022017-06-05 01:41:02 -040054parser 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 Cascone4f011022017-06-05 01:41:02 -040063parser parse_tcp {
64 extract(tcp);
65 return ingress;
66}
67
Carmelo Cascone4f011022017-06-05 01:41:02 -040068parser parse_udp {
69 extract(udp);
70 return ingress;
Carmelo Cascone16de6db2017-08-22 00:27:57 +020071}
72
73#endif