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 "../define.p4" |
| 21 | #include "../header.p4" |
| 22 | #include "../action.p4" |
| 23 | |
| 24 | |
| 25 | control Forwarding ( |
| 26 | inout parsed_headers_t hdr, |
| 27 | inout fabric_metadata_t fabric_metadata, |
| 28 | inout standard_metadata_t standard_metadata) { |
| 29 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 30 | /* |
| 31 | * Bridging Table. |
| 32 | * Matches destination mac address and VLAN Id and make egress decision. |
| 33 | */ |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 34 | direct_counter(CounterType.packets_and_bytes) bridging_counter; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 35 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 36 | action set_next_id_bridging(next_id_t next_id) { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 37 | fabric_metadata.next_id = next_id; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 38 | bridging_counter.count(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 41 | table bridging { |
| 42 | key = { |
| 43 | hdr.vlan_tag.vlan_id: exact; |
| 44 | hdr.ethernet.dst_addr: ternary; |
| 45 | } |
| 46 | |
| 47 | actions = { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 48 | set_next_id_bridging; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 49 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 50 | counters = bridging_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 53 | /* |
| 54 | * MPLS Table. |
| 55 | * Matches MPLS label and make egress decision. |
| 56 | */ |
| 57 | direct_counter(CounterType.packets_and_bytes) mpls_counter; |
| 58 | |
| 59 | action pop_mpls_and_next(next_id_t next_id) { |
| 60 | hdr.mpls.setInvalid(); |
| 61 | fabric_metadata.next_id = next_id; |
| 62 | mpls_counter.count(); |
| 63 | } |
| 64 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 65 | table mpls { |
| 66 | key = { |
| 67 | hdr.mpls.label: exact; |
| 68 | } |
| 69 | |
| 70 | actions = { |
| 71 | pop_mpls_and_next; |
| 72 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 73 | counters = mpls_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 76 | /* |
| 77 | * IPv4 Unicast Table. |
| 78 | * Matches IPv4 prefix and make egress decision. |
| 79 | */ |
| 80 | direct_counter(CounterType.packets_and_bytes) unicast_v4_counter; |
| 81 | |
| 82 | action set_next_id_unicast_v4(next_id_t next_id) { |
| 83 | fabric_metadata.next_id = next_id; |
| 84 | unicast_v4_counter.count(); |
| 85 | } |
| 86 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 87 | table unicast_v4 { |
| 88 | key = { |
| 89 | hdr.ipv4.dst_addr: lpm; |
| 90 | } |
| 91 | |
| 92 | actions = { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 93 | set_next_id_unicast_v4; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 94 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 95 | counters = unicast_v4_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 98 | /* |
| 99 | * ACL Table. |
| 100 | * Make final egress decision based on general metch fields. |
| 101 | */ |
| 102 | direct_counter(CounterType.packets_and_bytes) acl_counter; |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 103 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 104 | action set_next_id_acl(next_id_t next_id) { |
| 105 | fabric_metadata.next_id = next_id; |
| 106 | acl_counter.count(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 109 | action send_to_controller() { |
| 110 | standard_metadata.egress_spec = CPU_PORT; |
| 111 | acl_counter.count(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 112 | } |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 113 | |
| 114 | action drop() { |
| 115 | mark_to_drop(); |
| 116 | acl_counter.count(); |
| 117 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 118 | |
| 119 | table acl { |
| 120 | key = { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 121 | standard_metadata.ingress_port: ternary; // 9 |
| 122 | fabric_metadata.ip_proto: ternary; // 8 |
| 123 | fabric_metadata.l4_src_port: ternary; // 16 |
| 124 | fabric_metadata.l4_dst_port: ternary; // 16 |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 125 | fabric_metadata.original_ether_type: ternary; //16 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 126 | |
| 127 | hdr.ethernet.dst_addr: ternary; // 48 |
| 128 | hdr.ethernet.src_addr: ternary; // 48 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 129 | hdr.vlan_tag.vlan_id: ternary; // 12 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 130 | hdr.ipv4.src_addr: ternary; // 32 |
| 131 | hdr.ipv4.dst_addr: ternary; // 32 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 132 | hdr.icmp.icmp_type: ternary; // 8 |
| 133 | hdr.icmp.icmp_code: ternary; // 8 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | actions = { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 137 | set_next_id_acl; |
| 138 | send_to_controller; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 139 | drop; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 140 | @defaultonly nop; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | const default_action = nop(); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 144 | size = 256; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 145 | counters = acl_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 148 | #ifdef WITH_MULTICAST |
| 149 | /* |
| 150 | * IPv4 Multicast Table. |
| 151 | * Maches multcast IPv4 address and make egress decision. |
| 152 | */ |
| 153 | direct_counter(CounterType.packets_and_bytes) multicast_v4_counter; |
| 154 | action set_next_id_multicast_v4(next_id_t next_id) { |
| 155 | fabric_metadata.next_id = next_id; |
| 156 | multicast_v4_counter.count(); |
| 157 | } |
| 158 | |
| 159 | table multicast_v4 { |
| 160 | key = { |
| 161 | hdr.vlan_tag.vlan_id: exact; |
| 162 | hdr.ipv4.dst_addr: lpm; |
| 163 | } |
| 164 | |
| 165 | actions = { |
| 166 | set_next_id_multicast_v4; |
| 167 | } |
| 168 | counters = multicast_v4_counter; |
| 169 | } |
| 170 | #endif // WITH_MULTICAST |
| 171 | |
| 172 | #ifdef WITH_IPV6 |
| 173 | /* |
| 174 | * IPv6 Unicast Table. |
| 175 | * Matches IPv6 prefix and make egress decision. |
| 176 | */ |
| 177 | direct_counter(CounterType.packets_and_bytes) unicast_v6_counter; |
| 178 | |
| 179 | action set_next_id_unicast_v6(next_id_t next_id) { |
| 180 | fabric_metadata.next_id = next_id; |
| 181 | unicast_v6_counter.count(); |
| 182 | } |
| 183 | |
| 184 | table unicast_v6 { |
| 185 | key = { |
| 186 | hdr.ipv6.dst_addr: lpm; |
| 187 | } |
| 188 | |
| 189 | actions = { |
| 190 | set_next_id_unicast_v6; |
| 191 | } |
| 192 | counters = unicast_v6_counter; |
| 193 | } |
| 194 | |
| 195 | #ifdef WITH_MULTICAST |
| 196 | /* |
| 197 | * IPv6 Multicast Table. |
| 198 | * Maches multcast IPv6 address and make egress decision. |
| 199 | */ |
| 200 | direct_counter(CounterType.packets_and_bytes) multicast_v6_counter; |
| 201 | |
| 202 | action set_next_id_multicast_v6(next_id_t next_id) { |
| 203 | fabric_metadata.next_id = next_id; |
| 204 | multicast_v6_counter.count(); |
| 205 | } |
| 206 | |
| 207 | table multicast_v6 { |
| 208 | key = { |
| 209 | hdr.vlan_tag.vlan_id: exact; |
| 210 | hdr.ipv6.dst_addr: lpm; |
| 211 | } |
| 212 | |
| 213 | actions = { |
| 214 | set_next_id_multicast_v6; |
| 215 | } |
| 216 | counters = multicast_v6_counter; |
| 217 | } |
| 218 | #endif // WITH_MULTICAST |
| 219 | #endif // WITH_IPV6 |
| 220 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 221 | apply { |
| 222 | if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply(); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 223 | else if (fabric_metadata.fwd_type == FWD_MPLS) { |
| 224 | mpls.apply(); |
Yi Tseng | bd46d05 | 2018-01-22 17:18:16 -0800 | [diff] [blame] | 225 | |
| 226 | // TODO: IPv6 |
| 227 | hdr.vlan_tag.ether_type = ETHERTYPE_IPV4; |
| 228 | fabric_metadata.original_ether_type = ETHERTYPE_IPV4; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 229 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 230 | else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) unicast_v4.apply(); |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 231 | #ifdef WITH_MULTICAST |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 232 | else if (fabric_metadata.fwd_type == FWD_IPV4_MULTICAST) multicast_v4.apply(); |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 233 | #endif // WITH_MULTICAST |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 234 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 235 | else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) unicast_v6.apply(); |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 236 | #ifdef WITH_MULTICAST |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 237 | else if (fabric_metadata.fwd_type == FWD_IPV6_MULTICAST) multicast_v6.apply(); |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 238 | #endif // WITH_MULTICAST |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 239 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 240 | acl.apply(); |
| 241 | } |
| 242 | } |