blob: 0a77d4ce8c61513f4a8ea47bfc3727997a2dd3fe [file] [log] [blame]
/*
* Copyright 2017-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __PARSER__
#define __PARSER__
#include "define.p4"
parser FabricParser (packet_in packet,
out parsed_headers_t hdr,
inout fabric_metadata_t fabric_metadata,
inout standard_metadata_t standard_metadata) {
bit<6> last_ipv4_dscp = 0;
state start {
transition select(standard_metadata.ingress_port) {
CPU_PORT: check_packet_out;
default: parse_ethernet;
}
}
state check_packet_out {
packet_out_header_t tmp = packet.lookahead<packet_out_header_t>();
transition select(tmp.do_forwarding) {
0: parse_packet_out_and_accept;
default: strip_packet_out;
}
}
state parse_packet_out_and_accept {
// Will transmit over requested egress port as-is. No need to parse further.
packet.extract(hdr.packet_out);
transition accept;
}
state strip_packet_out {
// Remove packet-out header and process as a regular packet.
packet.advance(PACKET_OUT_HDR_SIZE * 8);
transition parse_ethernet;
}
state parse_ethernet {
packet.extract(hdr.ethernet);
fabric_metadata.vlan_id = DEFAULT_VLAN_ID;
transition select(packet.lookahead<bit<16>>()){
ETHERTYPE_QINQ: parse_vlan_tag;
ETHERTYPE_QINQ_NON_STD: parse_vlan_tag;
ETHERTYPE_VLAN: parse_vlan_tag;
default: parse_eth_type;
}
}
state parse_vlan_tag {
packet.extract(hdr.vlan_tag);
#ifdef WITH_BNG
fabric_metadata.bng.s_tag = hdr.vlan_tag.vlan_id;
#endif // WITH_BNG
transition select(packet.lookahead<bit<16>>()){
#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
ETHERTYPE_VLAN: parse_inner_vlan_tag;
#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
default: parse_eth_type;
}
}
#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
state parse_inner_vlan_tag {
packet.extract(hdr.inner_vlan_tag);
#ifdef WITH_BNG
fabric_metadata.bng.c_tag = hdr.inner_vlan_tag.vlan_id;
#endif // WITH_BNG
transition parse_eth_type;
}
#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
state parse_eth_type {
packet.extract(hdr.eth_type);
transition select(hdr.eth_type.value) {
ETHERTYPE_MPLS: parse_mpls;
ETHERTYPE_IPV4: parse_ipv4;
#ifdef WITH_IPV6
ETHERTYPE_IPV6: parse_ipv6;
#endif // WITH_IPV6
#ifdef WITH_BNG
ETHERTYPE_PPPOED: parse_pppoe;
ETHERTYPE_PPPOES: parse_pppoe;
#endif // WITH_BNG
default: accept;
}
}
#ifdef WITH_BNG
state parse_pppoe {
packet.extract(hdr.pppoe);
transition select(hdr.pppoe.protocol) {
PPPOE_PROTOCOL_MPLS: parse_mpls;
PPPOE_PROTOCOL_IP4: parse_ipv4;
#ifdef WITH_IPV6
PPPOE_PROTOCOL_IP6: parse_ipv6;
#endif // WITH_IPV6
default: accept;
}
}
#endif // WITH_BNG
state parse_mpls {
packet.extract(hdr.mpls);
fabric_metadata.mpls_label = hdr.mpls.label;
fabric_metadata.mpls_ttl = hdr.mpls.ttl;
// There is only one MPLS label for this fabric.
// Assume header after MPLS header is IPv4/IPv6
// Lookup first 4 bits for version
transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) {
// The packet should be either IPv4 or IPv6.
// If we have MPLS, go directly to parsing state without
// moving to pre_ states, the packet is considered MPLS
IP_VERSION_4: parse_ipv4;
#ifdef WITH_IPV6
IP_VERSION_6: parse_ipv6;
#endif // WITH_IPV6
default: parse_ethernet;
}
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
fabric_metadata.ip_proto = hdr.ipv4.protocol;
fabric_metadata.ip_eth_type = ETHERTYPE_IPV4;
fabric_metadata.ipv4_src_addr = hdr.ipv4.src_addr;
fabric_metadata.ipv4_dst_addr = hdr.ipv4.dst_addr;
last_ipv4_dscp = hdr.ipv4.dscp;
//Need header verification?
transition select(hdr.ipv4.protocol) {
PROTO_TCP: parse_tcp;
PROTO_UDP: parse_udp;
PROTO_ICMP: parse_icmp;
default: accept;
}
}
#ifdef WITH_IPV6
state parse_ipv6 {
packet.extract(hdr.ipv6);
fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
fabric_metadata.ip_eth_type = ETHERTYPE_IPV6;
transition select(hdr.ipv6.next_hdr) {
PROTO_TCP: parse_tcp;
PROTO_UDP: parse_udp;
PROTO_ICMPV6: parse_icmp;
default: accept;
}
}
#endif // WITH_IPV6
state parse_tcp {
packet.extract(hdr.tcp);
fabric_metadata.l4_sport = hdr.tcp.sport;
fabric_metadata.l4_dport = hdr.tcp.dport;
#ifdef WITH_INT
transition parse_int;
#else
transition accept;
#endif // WITH_INT
}
state parse_udp {
packet.extract(hdr.udp);
fabric_metadata.l4_sport = hdr.udp.sport;
fabric_metadata.l4_dport = hdr.udp.dport;
gtpu_t gtpu = packet.lookahead<gtpu_t>();
transition select(hdr.udp.dport, gtpu.version, gtpu.msgtype) {
// Treat GTP control traffic as payload.
(UDP_PORT_GTPU, GTP_V1, GTP_GPDU): parse_gtpu;
#ifdef WITH_INT
default: parse_int;
#else
default: accept;
#endif // WITH_INT
}
}
state parse_icmp {
packet.extract(hdr.icmp);
transition accept;
}
state parse_gtpu {
packet.extract(hdr.gtpu);
transition select(hdr.gtpu.ex_flag, hdr.gtpu.seq_flag, hdr.gtpu.npdu_flag) {
(0, 0, 0): parse_inner_ipv4;
default: parse_gtpu_options;
}
}
state parse_gtpu_options {
packet.extract(hdr.gtpu_options);
bit<8> gtpu_ext_len = packet.lookahead<bit<8>>();
transition select(hdr.gtpu_options.next_ext, gtpu_ext_len) {
(GTPU_NEXT_EXT_PSC, GTPU_EXT_PSC_LEN): parse_gtpu_ext_psc;
default: accept;
}
}
state parse_gtpu_ext_psc {
packet.extract(hdr.gtpu_ext_psc);
#ifdef WITH_SPGW
fabric_metadata.spgw.qfi = hdr.gtpu_ext_psc.qfi;
#endif // WITH_SPGW
transition select(hdr.gtpu_ext_psc.next_ext) {
GTPU_NEXT_EXT_NONE: parse_inner_ipv4;
default: accept;
}
}
state parse_inner_ipv4 {
packet.extract(hdr.inner_ipv4);
last_ipv4_dscp = hdr.inner_ipv4.dscp;
transition select(hdr.inner_ipv4.protocol) {
PROTO_TCP: parse_inner_tcp;
PROTO_UDP: parse_inner_udp;
PROTO_ICMP: parse_inner_icmp;
default: accept;
}
}
state parse_inner_udp {
packet.extract(hdr.inner_udp);
#ifdef WITH_SPGW
fabric_metadata.inner_l4_sport = hdr.inner_udp.sport;
fabric_metadata.inner_l4_dport = hdr.inner_udp.dport;
#endif // WITH_SPGW
#ifdef WITH_INT
transition parse_int;
#else
transition accept;
#endif // WITH_INT
}
state parse_inner_tcp {
packet.extract(hdr.inner_tcp);
#ifdef WITH_SPGW
fabric_metadata.inner_l4_sport = hdr.inner_tcp.sport;
fabric_metadata.inner_l4_dport = hdr.inner_tcp.dport;
#endif // WITH_SPGW
transition accept;
}
state parse_inner_icmp {
packet.extract(hdr.inner_icmp);
transition accept;
}
#ifdef WITH_INT
state parse_int {
transition select(last_ipv4_dscp) {
INT_DSCP &&& INT_DSCP: parse_intl4_shim;
default: accept;
}
}
state parse_intl4_shim {
packet.extract(hdr.intl4_shim);
transition parse_int_header;
}
state parse_int_header {
packet.extract(hdr.int_header);
// If there is no INT metadata but the INT header (plus shim and tail)
// exists, default value of length field in shim header should be
// INT_HEADER_LEN_WORDS.
transition select (hdr.intl4_shim.len_words) {
INT_HEADER_LEN_WORDS: parse_intl4_tail;
default: parse_int_data;
}
}
state parse_int_data {
#ifdef WITH_INT_SINK
// Parse INT metadata stack, but not tail
packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5);
transition parse_intl4_tail;
#else // not interested in INT data
transition accept;
#endif // WITH_INT_SINK
}
state parse_intl4_tail {
packet.extract(hdr.intl4_tail);
transition accept;
}
#endif // WITH_INT
}
control FabricDeparser(packet_out packet,in parsed_headers_t hdr) {
apply {
packet.emit(hdr.packet_in);
#ifdef WITH_INT_SINK
packet.emit(hdr.report_ethernet);
packet.emit(hdr.report_eth_type);
packet.emit(hdr.report_ipv4);
packet.emit(hdr.report_udp);
packet.emit(hdr.report_fixed_header);
#endif // WITH_INT_SINK
packet.emit(hdr.ethernet);
packet.emit(hdr.vlan_tag);
#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
packet.emit(hdr.inner_vlan_tag);
#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
packet.emit(hdr.eth_type);
#ifdef WITH_BNG
packet.emit(hdr.pppoe);
#endif // WITH_BNG
packet.emit(hdr.mpls);
#ifdef WITH_SPGW
packet.emit(hdr.gtpu_ipv4);
packet.emit(hdr.gtpu_udp);
packet.emit(hdr.outer_gtpu);
packet.emit(hdr.outer_gtpu_options);
packet.emit(hdr.outer_gtpu_ext_psc);
#endif // WITH_SPGW
packet.emit(hdr.ipv4);
#ifdef WITH_IPV6
packet.emit(hdr.ipv6);
#endif // WITH_IPV6
packet.emit(hdr.tcp);
packet.emit(hdr.udp);
packet.emit(hdr.icmp);
// if we parsed a GTPU packet but did not decap it
packet.emit(hdr.gtpu);
packet.emit(hdr.gtpu_options);
packet.emit(hdr.gtpu_ext_psc);
packet.emit(hdr.inner_ipv4);
packet.emit(hdr.inner_tcp);
packet.emit(hdr.inner_udp);
packet.emit(hdr.inner_icmp);
#ifdef WITH_INT
packet.emit(hdr.intl4_shim);
packet.emit(hdr.int_header);
#ifdef WITH_INT_TRANSIT
packet.emit(hdr.int_switch_id);
packet.emit(hdr.int_port_ids);
packet.emit(hdr.int_hop_latency);
packet.emit(hdr.int_q_occupancy);
packet.emit(hdr.int_ingress_tstamp);
packet.emit(hdr.int_egress_tstamp);
packet.emit(hdr.int_q_congestion);
packet.emit(hdr.int_egress_tx_util);
#endif // WITH_INT_TRANSIT
#ifdef WITH_INT_SINK
packet.emit(hdr.int_data);
#endif // WITH_INT_SINK
packet.emit(hdr.intl4_tail);
#endif // WITH_INT
}
}
#endif