blob: 63c29fb7a706eb9eaad471c11b1d03cf160a3d94 [file] [log] [blame]
Jonghwan Hyun4a9a6712017-11-13 14:43:55 -08001/*
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/* -*- P4_16 -*- */
18#ifndef __PARSER__
19#define __PARSER__
20
21parser int_parser (
22 packet_in packet,
23 out headers_t hdr,
24 inout local_metadata_t local_metadata,
25 inout standard_metadata_t standard_metadata) {
26 state start {
27 transition select(standard_metadata.ingress_port) {
28 CPU_PORT: parse_packet_out;
29 default: parse_ethernet;
30 }
31 }
32
33 state parse_packet_out {
34 packet.extract(hdr.packet_out);
35 transition parse_ethernet;
36 }
37
38 state parse_ethernet {
39 packet.extract(hdr.ethernet);
40 transition select(hdr.ethernet.ether_type) {
41 ETH_TYPE_IPV4 : parse_ipv4;
42 default : accept;
43 }
44 }
45
46 state parse_ipv4 {
47 packet.extract(hdr.ipv4);
48 transition select(hdr.ipv4.protocol) {
49 IP_PROTO_TCP : parse_tcp;
50 IP_PROTO_UDP : parse_udp;
51 default: accept;
52 }
53 }
54
55 state parse_tcp {
56 packet.extract(hdr.tcp);
57 transition accept;
58 }
59
60 state parse_udp {
61 packet.extract(hdr.udp);
62 local_metadata.l4_src_port = hdr.udp.src_port;
63 local_metadata.l4_dst_port = hdr.udp.dst_port;
64 transition select(hdr.udp.dst_port) {
65 INT_PORT: parse_intl4_shim;
66 default: accept;
67 }
68 }
69
70 state parse_intl4_shim {
71 packet.extract(hdr.intl4_shim);
72 transition parse_int_header;
73 }
74
75 state parse_int_header {
76 packet.extract(hdr.int_header);
77 // If there is no INT metadata but the INT header (and corresponding shim header
78 // and tail header) exists, default value of length field in shim header
79 // should be INT_HEADER_LEN_WORD.
80 local_metadata.int_meta.metadata_len = hdr.intl4_shim.len - INT_HEADER_LEN_WORD;
81 transition select (local_metadata.int_meta.metadata_len) {
82 0: parse_intl4_tail;
83 default: parse_int_data;
84 }
85 }
86
87 state parse_int_data {
88 // Parse INT metadata, not INT header, INT shim header and INT tail header
89 packet.extract(hdr.int_data, (bit<32>) ((hdr.intl4_shim.len - INT_HEADER_LEN_WORD) << 5));
90 transition parse_intl4_tail;
91 }
92
93 state parse_intl4_tail {
94 packet.extract(hdr.intl4_tail);
95 transition accept;
96 }
97}
98
99control int_deparser(
100 packet_out packet,
101 in headers_t hdr) {
102 apply {
103 packet.emit(hdr.packet_in);
104 packet.emit(hdr.ethernet);
105 packet.emit(hdr.ipv4);
106 packet.emit(hdr.tcp);
107 packet.emit(hdr.udp);
108 packet.emit(hdr.intl4_shim);
109 packet.emit(hdr.int_header);
110 packet.emit(hdr.int_switch_id);
111 packet.emit(hdr.int_port_ids);
112 packet.emit(hdr.int_hop_latency);
113 packet.emit(hdr.int_q_occupancy);
114 packet.emit(hdr.int_ingress_tstamp);
115 packet.emit(hdr.int_egress_tstamp);
116 packet.emit(hdr.int_q_congestion);
117 packet.emit(hdr.int_egress_tx_util);
118 packet.emit(hdr.int_data);
119 packet.emit(hdr.intl4_tail);
120 }
121}
122
123#endif