blob: dcabea730907438de30bd4a18e56844a978bdf43 [file] [log] [blame]
Yi Tsengbe342052017-11-03 10:21:23 -07001/*
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#include <core.p4>
18#include <v1model.p4>
19
20#include "../header.p4"
21#include "../action.p4"
22
23control Filtering (
24 inout parsed_headers_t hdr,
25 inout fabric_metadata_t fabric_metadata,
26 inout standard_metadata_t standard_metadata) {
Yi Tseng47eac892018-07-11 02:17:04 +080027
28 /*
29 * Ingress Port VLAN Table.
30 * Process packets for different interfaces (Port number + VLAN).
31 * For example, an untagged packet will be tagged when it entered to an
32 * interface with untagged VLAN configuration.
33 */
Yi Tseng3a5731e2018-01-22 11:38:58 -080034 direct_counter(CounterType.packets_and_bytes) ingress_port_vlan_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070035
Yi Tseng1d842672017-11-28 16:06:52 -080036 action drop() {
37 mark_to_drop();
Yi Tseng47eac892018-07-11 02:17:04 +080038 ingress_port_vlan_counter.count();
Yi Tseng1d842672017-11-28 16:06:52 -080039 }
Yi Tsengbe342052017-11-03 10:21:23 -070040
41 action set_vlan(vlan_id_t new_vlan_id) {
42 hdr.vlan_tag.vlan_id = new_vlan_id;
Yi Tseng47eac892018-07-11 02:17:04 +080043 ingress_port_vlan_counter.count();
Yi Tsengbe342052017-11-03 10:21:23 -070044 }
45
46 action push_internal_vlan(vlan_id_t new_vlan_id) {
47 // Add internal VLAN header, will be removed before packet emission.
48 // cfi and pri values are dummy.
49 hdr.vlan_tag.setValid();
50 hdr.vlan_tag.cfi = 0;
51 hdr.vlan_tag.pri = 0;
Yi Tseng1d842672017-11-28 16:06:52 -080052 hdr.vlan_tag.ether_type = hdr.ethernet.ether_type;
53 hdr.ethernet.ether_type = ETHERTYPE_VLAN;
Yi Tseng47eac892018-07-11 02:17:04 +080054 hdr.vlan_tag.vlan_id = new_vlan_id;
Yi Tsengbe342052017-11-03 10:21:23 -070055
Yi Tseng20f9e7b2018-05-24 23:27:39 +080056 // pop internal vlan before packet in
Carmelo Cascone35d9b332018-06-15 16:27:22 +020057 fabric_metadata.pop_vlan_when_packet_in = _TRUE;
Yi Tseng47eac892018-07-11 02:17:04 +080058 ingress_port_vlan_counter.count();
Yi Tsengbe342052017-11-03 10:21:23 -070059 }
60
Carmelo Cascone8a715f82018-08-20 23:16:27 -070061 action nop_ingress_port_vlan() {
62 nop();
63 ingress_port_vlan_counter.count();
64 }
65
Yi Tsengbe342052017-11-03 10:21:23 -070066 table ingress_port_vlan {
67 key = {
68 standard_metadata.ingress_port: exact;
69 hdr.vlan_tag.isValid(): exact @name("hdr.vlan_tag.is_valid");
70 hdr.vlan_tag.vlan_id: ternary;
71 }
72
73 actions = {
74 push_internal_vlan;
75 set_vlan;
Yi Tsengbe342052017-11-03 10:21:23 -070076 drop;
Carmelo Cascone8a715f82018-08-20 23:16:27 -070077 nop_ingress_port_vlan();
Yi Tsengbe342052017-11-03 10:21:23 -070078 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080079
Carmelo Cascone8a715f82018-08-20 23:16:27 -070080 const default_action = push_internal_vlan(DEFAULT_VLAN_ID);
Yi Tseng3a5731e2018-01-22 11:38:58 -080081 counters = ingress_port_vlan_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070082 }
83
Yi Tseng47eac892018-07-11 02:17:04 +080084 /*
85 * Forwarding Classifier.
86 * Setup Forwarding Type metadata for Forwarding control block.
87 * There are six types of tables in Forwarding control block:
88 * - Bridging: default forwarding type
89 * - MPLS: destination mac address is the router mac and ethernet type is
90 * MPLS(0x8847)
91 * - IP Multicast: destination mac address is multicast address and ethernet
92 * type is IP(0x0800 or 0x86dd)
93 * - IP Unicast: destination mac address is router mac and ethernet type is
94 * IP(0x0800 or 0x86dd)
95 */
96 direct_counter(CounterType.packets_and_bytes) fwd_classifier_counter;
97
98 action set_forwarding_type(fwd_type_t fwd_type) {
99 fabric_metadata.fwd_type = fwd_type;
100 fwd_classifier_counter.count();
101 }
102
Yi Tsengbe342052017-11-03 10:21:23 -0700103 table fwd_classifier {
104 key = {
105 standard_metadata.ingress_port: exact;
106 hdr.ethernet.dst_addr: exact;
Yi Tseng8235a1a2018-07-24 20:57:28 +0800107 hdr.vlan_tag.ether_type: exact;
Yi Tsengbe342052017-11-03 10:21:23 -0700108 }
109
110 actions = {
111 set_forwarding_type;
112 }
113
114 const default_action = set_forwarding_type(FWD_BRIDGING);
Yi Tseng3a5731e2018-01-22 11:38:58 -0800115 counters = fwd_classifier_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700116 }
117
118 apply {
Carmelo Cascone8a715f82018-08-20 23:16:27 -0700119 if (ingress_port_vlan.apply().hit) {
120 fwd_classifier.apply();
121 } else {
122 // Packet from unconfigured port. Skip forwarding processing,
123 // except for ACL table in case we want to punt to cpu.
124 fabric_metadata.fwd_type = FWD_UNKNOWN;
125 }
Yi Tsengbe342052017-11-03 10:21:23 -0700126 }
127}