blob: 601de35b26aac9347842f80dc1fcf5344477df7c [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
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070017#ifndef __PARSERS__
18#define __PARSERS__
Yi Tseng21629932017-06-06 11:17:43 -070019
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070020#include "headers.p4"
21#include "defines.p4"
22
23parser 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 Tseng21629932017-06-06 11:17:43 -070034
Carmelo Cascone837e6452017-07-19 20:35:22 -040035 state parse_packet_out {
36 packet.extract(hdr.packet_out);
37 transition parse_ethernet;
38 }
39
Yi Tseng21629932017-06-06 11:17:43 -070040 state parse_ethernet {
41 packet.extract(hdr.ethernet);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070042 transition select(hdr.ethernet.ether_type) {
Yi Tseng21629932017-06-06 11:17:43 -070043 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 Casconeca94bcf2017-10-27 14:16:59 -070051 IP_PROTO_TCP: parse_tcp;
52 IP_PROTO_UDP: parse_udp;
Yi Tseng21629932017-06-06 11:17:43 -070053 default: accept;
54 }
55 }
56
57 state parse_tcp {
58 packet.extract(hdr.tcp);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070059 local_metadata.l4_src_port = hdr.tcp.src_port;
60 local_metadata.l4_dst_port = hdr.tcp.dst_port;
Yi Tseng21629932017-06-06 11:17:43 -070061 transition accept;
62 }
63
64 state parse_udp {
65 packet.extract(hdr.udp);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070066 local_metadata.l4_src_port = hdr.udp.src_port;
67 local_metadata.l4_dst_port = hdr.udp.dst_port;
Yi Tseng21629932017-06-06 11:17:43 -070068 transition accept;
69 }
Yi Tseng21629932017-06-06 11:17:43 -070070}
71
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070072control deparser(packet_out packet, in headers_t hdr) {
Yi Tseng21629932017-06-06 11:17:43 -070073 apply {
Carmelo Cascone837e6452017-07-19 20:35:22 -040074 packet.emit(hdr.packet_in);
Yi Tseng21629932017-06-06 11:17:43 -070075 packet.emit(hdr.ethernet);
76 packet.emit(hdr.ipv4);
Yi Tseng21629932017-06-06 11:17:43 -070077 packet.emit(hdr.tcp);
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070078 packet.emit(hdr.udp);
Yi Tseng21629932017-06-06 11:17:43 -070079 }
80}
Carmelo Casconeca94bcf2017-10-27 14:16:59 -070081
Yi Tseng21629932017-06-06 11:17:43 -070082#endif