Carmelo Cascone | c8d3486 | 2017-07-30 00:48:23 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | c8d3486 | 2017-07-30 00:48:23 -0400 | [diff] [blame] | 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 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 17 | #ifndef __PARSERS__ |
| 18 | #define __PARSERS__ |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 19 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 20 | #include "headers.p4" |
| 21 | #include "defines.p4" |
| 22 | |
| 23 | parser parser_impl(packet_in packet, |
| 24 | out headers_t hdr, |
| 25 | inout local_metadata_t local_metadata, |
| 26 | inout standard_metadata_t standard_metadata) { |
| 27 | |
| 28 | state start { |
| 29 | transition select(standard_metadata.ingress_port) { |
| 30 | CPU_PORT: parse_packet_out; |
| 31 | default: parse_ethernet; |
| 32 | } |
| 33 | } |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 34 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 35 | state parse_packet_out { |
| 36 | packet.extract(hdr.packet_out); |
| 37 | transition parse_ethernet; |
| 38 | } |
| 39 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 40 | state parse_ethernet { |
| 41 | packet.extract(hdr.ethernet); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 42 | transition select(hdr.ethernet.ether_type) { |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 43 | ETH_TYPE_IPV4: parse_ipv4; |
| 44 | default: accept; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | state parse_ipv4 { |
| 49 | packet.extract(hdr.ipv4); |
| 50 | transition select(hdr.ipv4.protocol) { |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 51 | IP_PROTO_TCP: parse_tcp; |
| 52 | IP_PROTO_UDP: parse_udp; |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 53 | default: accept; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | state parse_tcp { |
| 58 | packet.extract(hdr.tcp); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 59 | local_metadata.l4_src_port = hdr.tcp.src_port; |
| 60 | local_metadata.l4_dst_port = hdr.tcp.dst_port; |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 61 | transition accept; |
| 62 | } |
| 63 | |
| 64 | state parse_udp { |
| 65 | packet.extract(hdr.udp); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 66 | local_metadata.l4_src_port = hdr.udp.src_port; |
| 67 | local_metadata.l4_dst_port = hdr.udp.dst_port; |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 68 | transition accept; |
| 69 | } |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 72 | control deparser(packet_out packet, in headers_t hdr) { |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 73 | apply { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 74 | packet.emit(hdr.packet_in); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 75 | packet.emit(hdr.ethernet); |
| 76 | packet.emit(hdr.ipv4); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 77 | packet.emit(hdr.tcp); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 78 | packet.emit(hdr.udp); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 81 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 82 | #endif |