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; |
| 59 | transition select((hdr.ipv4.dscp & INT_DSCP) == INT_DSCP) { |
| 60 | true: parse_intl4_shim; |
| 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 | 8be0339 | 2017-12-04 15:48:44 -0800 | [diff] [blame] | 69 | transition select((hdr.ipv4.dscp & INT_DSCP) == INT_DSCP) { |
| 70 | true: 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); |
| 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 | |
| 104 | control int_deparser( |
| 105 | packet_out packet, |
| 106 | in headers_t hdr) { |
| 107 | apply { |
| 108 | packet.emit(hdr.packet_in); |
Jonghwan Hyun | 6777d53 | 2018-11-18 20:50:16 +0900 | [diff] [blame^] | 109 | 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 Hyun | 4a9a671 | 2017-11-13 14:43:55 -0800 | [diff] [blame] | 113 | 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 Hyun | 6777d53 | 2018-11-18 20:50:16 +0900 | [diff] [blame^] | 132 | #endif |