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 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 17 | #ifndef PARSERS |
| 18 | #define PARSERS |
| 19 | #include "headers.p4" |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 20 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 21 | parser ParserImpl(packet_in packet, out headers_t hdr, inout metadata_t meta, |
| 22 | inout standard_metadata_t standard_metadata) { |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 23 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 24 | state parse_packet_out { |
| 25 | packet.extract(hdr.packet_out); |
| 26 | transition parse_ethernet; |
| 27 | } |
| 28 | |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 29 | state parse_ethernet { |
| 30 | packet.extract(hdr.ethernet); |
| 31 | transition select(hdr.ethernet.etherType) { |
| 32 | ETH_TYPE_IPV4: parse_ipv4; |
| 33 | default: accept; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | state parse_ipv4 { |
| 38 | packet.extract(hdr.ipv4); |
| 39 | transition select(hdr.ipv4.protocol) { |
| 40 | IP_TYPE_TCP: parse_tcp; |
| 41 | IP_TYPE_UDP: parse_udp; |
| 42 | default: accept; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | state parse_tcp { |
| 47 | packet.extract(hdr.tcp); |
| 48 | transition accept; |
| 49 | } |
| 50 | |
| 51 | state parse_udp { |
| 52 | packet.extract(hdr.udp); |
| 53 | transition accept; |
| 54 | } |
| 55 | |
| 56 | state start { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 57 | transition select(standard_metadata.ingress_port) { |
| 58 | CPU_PORT: parse_packet_out; |
| 59 | default: parse_ethernet; |
| 60 | } |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 64 | control DeparserImpl(packet_out packet, in headers_t hdr) { |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 65 | apply { |
Carmelo Cascone | 837e645 | 2017-07-19 20:35:22 -0400 | [diff] [blame] | 66 | packet.emit(hdr.packet_in); |
Yi Tseng | 2162993 | 2017-06-06 11:17:43 -0700 | [diff] [blame] | 67 | packet.emit(hdr.ethernet); |
| 68 | packet.emit(hdr.ipv4); |
| 69 | packet.emit(hdr.udp); |
| 70 | packet.emit(hdr.tcp); |
| 71 | } |
| 72 | } |
| 73 | #endif |