blob: 2d03c207d7061adb283dac818018e50fc2ca1cdd [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 -*- */
Jonghwan Hyun8be03392017-12-04 15:48:44 -080018#ifndef __INT_PARSER__
19#define __INT_PARSER__
Jonghwan Hyun4a9a6712017-11-13 14:43:55 -080020
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);
Jonghwan Hyun8be03392017-12-04 15:48:44 -080057 local_metadata.l4_src_port = hdr.tcp.src_port;
58 local_metadata.l4_dst_port = hdr.tcp.dst_port;
59 transition select((hdr.ipv4.dscp & INT_DSCP) == INT_DSCP) {
60 true: parse_intl4_shim;
61 default: accept;
62 }
Jonghwan Hyun4a9a6712017-11-13 14:43:55 -080063 }
64
65 state parse_udp {
66 packet.extract(hdr.udp);
67 local_metadata.l4_src_port = hdr.udp.src_port;
68 local_metadata.l4_dst_port = hdr.udp.dst_port;
Jonghwan Hyun8be03392017-12-04 15:48:44 -080069 transition select((hdr.ipv4.dscp & INT_DSCP) == INT_DSCP) {
70 true: parse_intl4_shim;
Jonghwan Hyun4a9a6712017-11-13 14:43:55 -080071 default: accept;
72 }
73 }
74
75 state parse_intl4_shim {
76 packet.extract(hdr.intl4_shim);
77 transition parse_int_header;
78 }
79
80 state parse_int_header {
81 packet.extract(hdr.int_header);
82 // If there is no INT metadata but the INT header (and corresponding shim header
83 // and tail header) exists, default value of length field in shim header
84 // should be INT_HEADER_LEN_WORD.
85 local_metadata.int_meta.metadata_len = hdr.intl4_shim.len - INT_HEADER_LEN_WORD;
86 transition select (local_metadata.int_meta.metadata_len) {
87 0: parse_intl4_tail;
88 default: parse_int_data;
89 }
90 }
91
92 state parse_int_data {
93 // Parse INT metadata, not INT header, INT shim header and INT tail header
94 packet.extract(hdr.int_data, (bit<32>) ((hdr.intl4_shim.len - INT_HEADER_LEN_WORD) << 5));
95 transition parse_intl4_tail;
96 }
97
98 state parse_intl4_tail {
99 packet.extract(hdr.intl4_tail);
100 transition accept;
101 }
102}
103
104control int_deparser(
105 packet_out packet,
106 in headers_t hdr) {
107 apply {
108 packet.emit(hdr.packet_in);
Jonghwan Hyun6777d532018-11-18 20:50:16 +0900109 packet.emit(hdr.report_ethernet);
110 packet.emit(hdr.report_ipv4);
111 packet.emit(hdr.report_udp);
112 packet.emit(hdr.report_fixed_header);
Jonghwan Hyun4a9a6712017-11-13 14:43:55 -0800113 packet.emit(hdr.ethernet);
114 packet.emit(hdr.ipv4);
115 packet.emit(hdr.tcp);
116 packet.emit(hdr.udp);
117 packet.emit(hdr.intl4_shim);
118 packet.emit(hdr.int_header);
119 packet.emit(hdr.int_switch_id);
120 packet.emit(hdr.int_port_ids);
121 packet.emit(hdr.int_hop_latency);
122 packet.emit(hdr.int_q_occupancy);
123 packet.emit(hdr.int_ingress_tstamp);
124 packet.emit(hdr.int_egress_tstamp);
125 packet.emit(hdr.int_q_congestion);
126 packet.emit(hdr.int_egress_tx_util);
127 packet.emit(hdr.int_data);
128 packet.emit(hdr.intl4_tail);
129 }
130}
131
Jonghwan Hyun6777d532018-11-18 20:50:16 +0900132#endif