Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [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 | #ifndef __PARSER__ |
| 18 | #define __PARSER__ |
| 19 | |
| 20 | #include "define.p4" |
| 21 | |
| 22 | parser FabricParser ( |
| 23 | packet_in packet, |
| 24 | out parsed_headers_t hdr, |
| 25 | inout fabric_metadata_t fabric_metadata, |
| 26 | inout standard_metadata_t standard_metadata) { |
| 27 | state start { |
| 28 | transition select(standard_metadata.ingress_port) { |
| 29 | CPU_PORT: parse_packet_out; |
| 30 | default: parse_ethernet; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | state parse_packet_out { |
| 35 | packet.extract(hdr.packet_out); |
| 36 | transition parse_ethernet; |
| 37 | } |
| 38 | |
| 39 | state parse_ethernet { |
| 40 | packet.extract(hdr.ethernet); |
| 41 | transition select(hdr.ethernet.ether_type){ |
| 42 | ETHERTYPE_QINQ_NON_STD: parse_vlan_tag; |
| 43 | ETHERTYPE_QINQ: parse_vlan_tag; |
| 44 | ETHERTYPE_VLAN: parse_vlan_tag; |
| 45 | ETHERTYPE_MPLS: parse_mpls; |
| 46 | ETHERTYPE_ARP: parse_arp; |
| 47 | ETHERTYPE_IPV4: parse_ipv4; |
| 48 | ETHERTYPE_IPV6: parse_ipv6; |
| 49 | default: accept; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | state parse_vlan_tag { |
| 54 | packet.extract(hdr.vlan_tag); |
| 55 | transition select(hdr.vlan_tag.ether_type){ |
| 56 | ETHERTYPE_VLAN: parse_inner_vlan_tag; |
| 57 | ETHERTYPE_ARP: parse_arp; |
| 58 | ETHERTYPE_IPV4: parse_ipv4; |
| 59 | ETHERTYPE_IPV6: parse_ipv6; |
| 60 | default: accept; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | state parse_inner_vlan_tag { |
| 65 | packet.extract(hdr.inner_vlan_tag); |
| 66 | transition select(hdr.vlan_tag.ether_type){ |
| 67 | ETHERTYPE_ARP: parse_arp; |
| 68 | ETHERTYPE_IPV4: parse_ipv4; |
| 69 | ETHERTYPE_IPV6: parse_ipv6; |
| 70 | default: accept; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | state parse_mpls { |
| 75 | packet.extract(hdr.mpls); |
| 76 | |
| 77 | //There is only one MPLS label for this fabric. |
| 78 | transition select(packet.lookahead<ipv4_t>().version) { |
| 79 | //The packet should be either IPv4 or IPv6. |
| 80 | IP_VERSION_4: parse_ipv4; |
| 81 | IP_VERSION_6: parse_ipv6; |
| 82 | default: parse_ethernet; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | state parse_ipv4 { |
| 87 | packet.extract(hdr.ipv4); |
| 88 | fabric_metadata.ip_proto = hdr.ipv4.protocol; |
| 89 | //Need header verification? |
| 90 | transition select(hdr.ipv4.protocol) { |
| 91 | PROTO_TCP: parse_tcp; |
| 92 | PROTO_UDP: parse_udp; |
| 93 | PROTO_ICMP: parse_icmp; |
| 94 | default: accept; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | state parse_ipv6 { |
| 99 | packet.extract(hdr.ipv6); |
| 100 | fabric_metadata.ip_proto = hdr.ipv6.next_hdr; |
| 101 | transition select(hdr.ipv6.next_hdr) { |
| 102 | PROTO_TCP: parse_tcp; |
| 103 | PROTO_UDP: parse_udp; |
| 104 | PROTO_ICMPV6: parse_icmp; |
| 105 | default: accept; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | state parse_arp { |
| 110 | packet.extract(hdr.arp); |
| 111 | transition accept; |
| 112 | } |
| 113 | |
| 114 | state parse_tcp { |
| 115 | packet.extract(hdr.tcp); |
| 116 | fabric_metadata.l4_src_port = hdr.tcp.src_port; |
| 117 | fabric_metadata.l4_dst_port = hdr.tcp.dst_port; |
| 118 | transition accept; |
| 119 | } |
| 120 | |
| 121 | state parse_udp { |
| 122 | packet.extract(hdr.udp); |
| 123 | fabric_metadata.l4_src_port = hdr.udp.src_port; |
| 124 | fabric_metadata.l4_dst_port = hdr.udp.dst_port; |
| 125 | transition accept; |
| 126 | } |
| 127 | |
| 128 | state parse_icmp { |
| 129 | packet.extract(hdr.icmp); |
| 130 | transition accept; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | control FabricDeparser(packet_out packet, in parsed_headers_t hdr) { |
| 135 | apply{ |
| 136 | packet.emit(hdr.packet_in); |
| 137 | packet.emit(hdr.ethernet); |
| 138 | packet.emit(hdr.vlan_tag); |
| 139 | packet.emit(hdr.inner_vlan_tag); |
| 140 | packet.emit(hdr.mpls); |
| 141 | packet.emit(hdr.arp); |
| 142 | packet.emit(hdr.ipv4); |
| 143 | packet.emit(hdr.ipv6); |
| 144 | packet.emit(hdr.tcp); |
| 145 | packet.emit(hdr.udp); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | #endif |