blob: c5653dcafdfa9cc140debe39f5dbd8788152cff8 [file] [log] [blame]
Yi Tsengbe342052017-11-03 10:21:23 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __PARSER__
18#define __PARSER__
19
20#include "define.p4"
21
22parser FabricParser (
23packet_in packet,
24out parsed_headers_t hdr,
25inout fabric_metadata_t fabric_metadata,
26inout standard_metadata_t standard_metadata) {
Yi Tseng1d842672017-11-28 16:06:52 -080027
Yi Tsengbe342052017-11-03 10:21:23 -070028 state start {
29 transition select(standard_metadata.ingress_port) {
30 CPU_PORT: parse_packet_out;
31 default: parse_ethernet;
32 }
33 }
34
35 state parse_packet_out {
36 packet.extract(hdr.packet_out);
37 transition parse_ethernet;
38 }
39
40 state parse_ethernet {
41 packet.extract(hdr.ethernet);
Yi Tseng1d842672017-11-28 16:06:52 -080042 fabric_metadata.original_ether_type = hdr.ethernet.ether_type;
Yi Tsengbe342052017-11-03 10:21:23 -070043 transition select(hdr.ethernet.ether_type){
44 ETHERTYPE_QINQ_NON_STD: parse_vlan_tag;
45 ETHERTYPE_QINQ: parse_vlan_tag;
46 ETHERTYPE_VLAN: parse_vlan_tag;
47 ETHERTYPE_MPLS: parse_mpls;
48 ETHERTYPE_ARP: parse_arp;
49 ETHERTYPE_IPV4: parse_ipv4;
50 ETHERTYPE_IPV6: parse_ipv6;
51 default: accept;
52 }
53 }
54
55 state parse_vlan_tag {
56 packet.extract(hdr.vlan_tag);
57 transition select(hdr.vlan_tag.ether_type){
58 ETHERTYPE_VLAN: parse_inner_vlan_tag;
59 ETHERTYPE_ARP: parse_arp;
60 ETHERTYPE_IPV4: parse_ipv4;
61 ETHERTYPE_IPV6: parse_ipv6;
62 default: accept;
63 }
64 }
65
66 state parse_inner_vlan_tag {
67 packet.extract(hdr.inner_vlan_tag);
68 transition select(hdr.vlan_tag.ether_type){
69 ETHERTYPE_ARP: parse_arp;
70 ETHERTYPE_IPV4: parse_ipv4;
71 ETHERTYPE_IPV6: parse_ipv6;
72 default: accept;
73 }
74 }
75
76 state parse_mpls {
77 packet.extract(hdr.mpls);
Yi Tsengbe342052017-11-03 10:21:23 -070078 //There is only one MPLS label for this fabric.
79 transition select(packet.lookahead<ipv4_t>().version) {
80 //The packet should be either IPv4 or IPv6.
81 IP_VERSION_4: parse_ipv4;
82 IP_VERSION_6: parse_ipv6;
83 default: parse_ethernet;
84 }
85 }
86
87 state parse_ipv4 {
88 packet.extract(hdr.ipv4);
89 fabric_metadata.ip_proto = hdr.ipv4.protocol;
90 //Need header verification?
91 transition select(hdr.ipv4.protocol) {
92 PROTO_TCP: parse_tcp;
93 PROTO_UDP: parse_udp;
94 PROTO_ICMP: parse_icmp;
95 default: accept;
96 }
97 }
98
99 state parse_ipv6 {
100 packet.extract(hdr.ipv6);
101 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
102 transition select(hdr.ipv6.next_hdr) {
103 PROTO_TCP: parse_tcp;
104 PROTO_UDP: parse_udp;
105 PROTO_ICMPV6: parse_icmp;
106 default: accept;
107 }
108 }
109
110 state parse_arp {
111 packet.extract(hdr.arp);
112 transition accept;
113 }
114
115 state parse_tcp {
116 packet.extract(hdr.tcp);
117 fabric_metadata.l4_src_port = hdr.tcp.src_port;
118 fabric_metadata.l4_dst_port = hdr.tcp.dst_port;
119 transition accept;
120 }
121
122 state parse_udp {
123 packet.extract(hdr.udp);
124 fabric_metadata.l4_src_port = hdr.udp.src_port;
125 fabric_metadata.l4_dst_port = hdr.udp.dst_port;
126 transition accept;
127 }
128
129 state parse_icmp {
130 packet.extract(hdr.icmp);
131 transition accept;
132 }
133}
134
135control FabricDeparser(packet_out packet, in parsed_headers_t hdr) {
136 apply{
137 packet.emit(hdr.packet_in);
138 packet.emit(hdr.ethernet);
139 packet.emit(hdr.vlan_tag);
140 packet.emit(hdr.inner_vlan_tag);
141 packet.emit(hdr.mpls);
142 packet.emit(hdr.arp);
143 packet.emit(hdr.ipv4);
144 packet.emit(hdr.ipv6);
145 packet.emit(hdr.tcp);
146 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800147 packet.emit(hdr.icmp);
Yi Tsengbe342052017-11-03 10:21:23 -0700148 }
149}
150
151#endif