blob: c8a01bf04dcfcc23efa846d478223e81499106d9 [file] [log] [blame]
Carmelo Casconec8d34862017-07-30 00:48:23 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Casconec8d34862017-07-30 00:48:23 -04003 *
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 Tseng21629932017-06-06 11:17:43 -070017#ifndef PARSERS
18#define PARSERS
19#include "headers.p4"
Yi Tseng21629932017-06-06 11:17:43 -070020
Carmelo Cascone837e6452017-07-19 20:35:22 -040021parser ParserImpl(packet_in packet, out headers_t hdr, inout metadata_t meta,
22 inout standard_metadata_t standard_metadata) {
Yi Tseng21629932017-06-06 11:17:43 -070023
Carmelo Cascone837e6452017-07-19 20:35:22 -040024 state parse_packet_out {
25 packet.extract(hdr.packet_out);
26 transition parse_ethernet;
27 }
28
Yi Tseng21629932017-06-06 11:17:43 -070029 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 Cascone837e6452017-07-19 20:35:22 -040057 transition select(standard_metadata.ingress_port) {
58 CPU_PORT: parse_packet_out;
59 default: parse_ethernet;
60 }
Yi Tseng21629932017-06-06 11:17:43 -070061 }
62}
63
Carmelo Cascone837e6452017-07-19 20:35:22 -040064control DeparserImpl(packet_out packet, in headers_t hdr) {
Yi Tseng21629932017-06-06 11:17:43 -070065 apply {
Carmelo Cascone837e6452017-07-19 20:35:22 -040066 packet.emit(hdr.packet_in);
Yi Tseng21629932017-06-06 11:17:43 -070067 packet.emit(hdr.ethernet);
68 packet.emit(hdr.ipv4);
69 packet.emit(hdr.udp);
70 packet.emit(hdr.tcp);
71 }
72}
73#endif