Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Hyun | 8be0339 | 2017-12-04 15:48:44 -0800 | [diff] [blame] | 18 | #ifndef __INT_PARSER__ |
| 19 | #define __INT_PARSER__ |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 20 | |
| 21 | parser 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 Hyun | 8be0339 | 2017-12-04 15:48:44 -0800 | [diff] [blame] | 57 | local_metadata.l4_src_port = hdr.tcp.src_port; |
| 58 | local_metadata.l4_dst_port = hdr.tcp.dst_port; |
Jonghwan Hyun | c235d46 | 2019-01-30 23:31:48 +0900 | [diff] [blame] | 59 | transition select(hdr.ipv4.dscp) { |
| 60 | DSCP_INT &&& DSCP_MASK: parse_intl4_shim; |
Jonghwan Hyun | 8be0339 | 2017-12-04 15:48:44 -0800 | [diff] [blame] | 61 | default: accept; |
| 62 | } |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 63 | } |
| 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 Hyun | c235d46 | 2019-01-30 23:31:48 +0900 | [diff] [blame] | 69 | transition select(hdr.ipv4.dscp) { |
| 70 | DSCP_INT &&& DSCP_MASK: parse_intl4_shim; |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 71 | default: accept; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | state parse_intl4_shim { |
| 76 | packet.extract(hdr.intl4_shim); |
Jonghwan Hyun | c235d46 | 2019-01-30 23:31:48 +0900 | [diff] [blame] | 77 | local_metadata.int_meta.intl4_shim_len = hdr.intl4_shim.len; |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 78 | transition parse_int_header; |
| 79 | } |
| 80 | |
| 81 | state parse_int_header { |
| 82 | packet.extract(hdr.int_header); |
Jonghwan Hyun | c235d46 | 2019-01-30 23:31:48 +0900 | [diff] [blame] | 83 | transition parse_int_data; |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | state parse_int_data { |
Jonghwan Hyun | c235d46 | 2019-01-30 23:31:48 +0900 | [diff] [blame] | 87 | // Parse INT metadata stack |
| 88 | packet.extract(hdr.int_data, ((bit<32>) (local_metadata.int_meta.intl4_shim_len - INT_HEADER_LEN_WORD)) << 5); |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 89 | transition accept; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | control int_deparser( |
| 94 | packet_out packet, |
| 95 | in headers_t hdr) { |
| 96 | apply { |
| 97 | packet.emit(hdr.packet_in); |
Jonghwan Hyun | 6777d53 | 2018-11-18 20:50:16 +0900 | [diff] [blame] | 98 | packet.emit(hdr.report_ethernet); |
| 99 | packet.emit(hdr.report_ipv4); |
| 100 | packet.emit(hdr.report_udp); |
| 101 | packet.emit(hdr.report_fixed_header); |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 102 | packet.emit(hdr.ethernet); |
| 103 | packet.emit(hdr.ipv4); |
| 104 | packet.emit(hdr.tcp); |
| 105 | packet.emit(hdr.udp); |
| 106 | packet.emit(hdr.intl4_shim); |
| 107 | packet.emit(hdr.int_header); |
| 108 | packet.emit(hdr.int_switch_id); |
Jonghwan Hyun | c235d46 | 2019-01-30 23:31:48 +0900 | [diff] [blame] | 109 | packet.emit(hdr.int_level1_port_ids); |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 110 | packet.emit(hdr.int_hop_latency); |
| 111 | packet.emit(hdr.int_q_occupancy); |
| 112 | packet.emit(hdr.int_ingress_tstamp); |
| 113 | packet.emit(hdr.int_egress_tstamp); |
Jonghwan Hyun | c235d46 | 2019-01-30 23:31:48 +0900 | [diff] [blame] | 114 | packet.emit(hdr.int_level2_port_ids); |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 115 | packet.emit(hdr.int_egress_tx_util); |
| 116 | packet.emit(hdr.int_data); |
Jonghwan Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Jonghwan Hyun | 6777d53 | 2018-11-18 20:50:16 +0900 | [diff] [blame] | 120 | #endif |