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 | #include <core.p4> |
| 18 | #include <v1model.p4> |
| 19 | |
| 20 | #include "../header.p4" |
| 21 | #include "../action.p4" |
| 22 | |
| 23 | control Filtering ( |
| 24 | inout parsed_headers_t hdr, |
| 25 | inout fabric_metadata_t fabric_metadata, |
| 26 | inout standard_metadata_t standard_metadata) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 27 | |
| 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 Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 34 | direct_counter(CounterType.packets_and_bytes) ingress_port_vlan_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 35 | |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 36 | action drop() { |
| 37 | mark_to_drop(); |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 38 | ingress_port_vlan_counter.count(); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 39 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 40 | |
| 41 | action set_vlan(vlan_id_t new_vlan_id) { |
| 42 | hdr.vlan_tag.vlan_id = new_vlan_id; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 43 | ingress_port_vlan_counter.count(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 44 | } |
| 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 Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 52 | hdr.vlan_tag.ether_type = hdr.ethernet.ether_type; |
| 53 | hdr.ethernet.ether_type = ETHERTYPE_VLAN; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 54 | hdr.vlan_tag.vlan_id = new_vlan_id; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 55 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 56 | // pop internal vlan before packet in |
Carmelo Cascone | 35d9b33 | 2018-06-15 16:27:22 +0200 | [diff] [blame] | 57 | fabric_metadata.pop_vlan_when_packet_in = _TRUE; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 58 | ingress_port_vlan_counter.count(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 61 | table ingress_port_vlan { |
| 62 | key = { |
| 63 | standard_metadata.ingress_port: exact; |
| 64 | hdr.vlan_tag.isValid(): exact @name("hdr.vlan_tag.is_valid"); |
| 65 | hdr.vlan_tag.vlan_id: ternary; |
| 66 | } |
| 67 | |
| 68 | actions = { |
| 69 | push_internal_vlan; |
| 70 | set_vlan; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 71 | @defaultonly nop; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 72 | drop; |
| 73 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 74 | |
| 75 | const default_action = nop(); |
| 76 | counters = ingress_port_vlan_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 79 | /* |
| 80 | * Forwarding Classifier. |
| 81 | * Setup Forwarding Type metadata for Forwarding control block. |
| 82 | * There are six types of tables in Forwarding control block: |
| 83 | * - Bridging: default forwarding type |
| 84 | * - MPLS: destination mac address is the router mac and ethernet type is |
| 85 | * MPLS(0x8847) |
| 86 | * - IP Multicast: destination mac address is multicast address and ethernet |
| 87 | * type is IP(0x0800 or 0x86dd) |
| 88 | * - IP Unicast: destination mac address is router mac and ethernet type is |
| 89 | * IP(0x0800 or 0x86dd) |
| 90 | */ |
| 91 | direct_counter(CounterType.packets_and_bytes) fwd_classifier_counter; |
| 92 | |
| 93 | action set_forwarding_type(fwd_type_t fwd_type) { |
| 94 | fabric_metadata.fwd_type = fwd_type; |
| 95 | fwd_classifier_counter.count(); |
| 96 | } |
| 97 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 98 | table fwd_classifier { |
| 99 | key = { |
| 100 | standard_metadata.ingress_port: exact; |
| 101 | hdr.ethernet.dst_addr: exact; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 102 | fabric_metadata.original_ether_type: exact; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | actions = { |
| 106 | set_forwarding_type; |
| 107 | } |
| 108 | |
| 109 | const default_action = set_forwarding_type(FWD_BRIDGING); |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 110 | counters = fwd_classifier_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | apply { |
| 114 | ingress_port_vlan.apply(); |
| 115 | fwd_classifier.apply(); |
| 116 | } |
| 117 | } |