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" |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 21 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 22 | control Filtering (inout parsed_headers_t hdr, |
| 23 | inout fabric_metadata_t fabric_metadata, |
| 24 | inout standard_metadata_t standard_metadata) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Ingress Port VLAN Table. |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 28 | * |
| 29 | * Filter packets based on ingress port and VLAN tag. |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 30 | */ |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 31 | direct_counter(CounterType.packets_and_bytes) ingress_port_vlan_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 32 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 33 | action deny() { |
| 34 | // Packet from unconfigured port. Skip forwarding and next block. |
| 35 | // Do ACL table in case we want to punt to cpu. |
| 36 | fabric_metadata.skip_forwarding = _TRUE; |
| 37 | fabric_metadata.skip_next = _TRUE; |
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 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 41 | action permit() { |
| 42 | // Allow packet as is. |
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 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 46 | action permit_with_internal_vlan(vlan_id_t vlan_id) { |
| 47 | fabric_metadata.vlan_id = vlan_id; |
Carmelo Cascone | 8a715f8 | 2018-08-20 23:16:27 -0700 | [diff] [blame] | 48 | ingress_port_vlan_counter.count(); |
| 49 | } |
| 50 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 51 | table ingress_port_vlan { |
| 52 | key = { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 53 | standard_metadata.ingress_port: exact @name("ig_port"); |
| 54 | hdr.vlan_tag.isValid(): exact @name("vlan_is_valid"); |
| 55 | hdr.vlan_tag.vlan_id: ternary @name("vlan_id"); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 56 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 57 | actions = { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 58 | deny(); |
| 59 | permit(); |
| 60 | permit_with_internal_vlan(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 61 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 62 | const default_action = deny(); |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 63 | counters = ingress_port_vlan_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 66 | /* |
| 67 | * Forwarding Classifier. |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 68 | * |
| 69 | * Set which type of forwarding behavior to execute in the next control block. |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 70 | * There are six types of tables in Forwarding control block: |
| 71 | * - Bridging: default forwarding type |
| 72 | * - MPLS: destination mac address is the router mac and ethernet type is |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 73 | * MPLS(0x8847) |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 74 | * - IP Multicast: destination mac address is multicast address and ethernet |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 75 | * type is IP(0x0800 or 0x86dd) |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 76 | * - IP Unicast: destination mac address is router mac and ethernet type is |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 77 | * IP(0x0800 or 0x86dd) |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 78 | */ |
| 79 | direct_counter(CounterType.packets_and_bytes) fwd_classifier_counter; |
| 80 | |
| 81 | action set_forwarding_type(fwd_type_t fwd_type) { |
| 82 | fabric_metadata.fwd_type = fwd_type; |
| 83 | fwd_classifier_counter.count(); |
| 84 | } |
| 85 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 86 | table fwd_classifier { |
| 87 | key = { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 88 | standard_metadata.ingress_port: exact @name("ig_port"); |
| 89 | hdr.ethernet.dst_addr: ternary @name("eth_dst"); |
| 90 | fabric_metadata.eth_type: exact @name("eth_type"); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 91 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 92 | actions = { |
| 93 | set_forwarding_type; |
| 94 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 95 | const default_action = set_forwarding_type(FWD_BRIDGING); |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 96 | counters = fwd_classifier_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | apply { |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 100 | // Initialize lookup metadata. Packets without a VLAN header will be |
| 101 | // treated as belonging to a default VLAN ID (see parser). |
| 102 | if (hdr.vlan_tag.isValid()) { |
| 103 | fabric_metadata.eth_type = hdr.vlan_tag.eth_type; |
| 104 | fabric_metadata.vlan_id = hdr.vlan_tag.vlan_id; |
| 105 | fabric_metadata.vlan_pri = hdr.vlan_tag.pri; |
| 106 | fabric_metadata.vlan_cfi = hdr.vlan_tag.cfi; |
Carmelo Cascone | 8a715f8 | 2018-08-20 23:16:27 -0700 | [diff] [blame] | 107 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame^] | 108 | if (!hdr.mpls.isValid()) { |
| 109 | // Packets with a valid MPLS header will have |
| 110 | // fabric_metadata.mpls_ttl set to the packet's MPLS ttl value (see |
| 111 | // parser). In any case, if we are forwarding via MPLS, ttl will be |
| 112 | // decremented in egress. |
| 113 | fabric_metadata.mpls_ttl = DEFAULT_MPLS_TTL + 1; |
| 114 | } |
| 115 | |
| 116 | ingress_port_vlan.apply(); |
| 117 | fwd_classifier.apply(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 118 | } |
| 119 | } |