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 | /* |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 77 | * IPv4 Routing Table. |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 78 | * Matches IPv4 prefix and make egress decision. |
| 79 | */ |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 80 | direct_counter(CounterType.packets_and_bytes) routing_v4_counter; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 81 | |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 82 | action set_next_id_routing_v4(next_id_t next_id) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 83 | fabric_metadata.next_id = next_id; |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 84 | routing_v4_counter.count(); |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 85 | } |
| 86 | |
Charles Chan | cd03f07 | 2018-08-31 17:46:37 -0700 | [diff] [blame] | 87 | action nop_routing_v4() { |
| 88 | routing_v4_counter.count(); |
| 89 | } |
| 90 | |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 91 | table routing_v4 { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 92 | key = { |
| 93 | hdr.ipv4.dst_addr: lpm; |
| 94 | } |
| 95 | |
| 96 | actions = { |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 97 | set_next_id_routing_v4; |
Charles Chan | cd03f07 | 2018-08-31 17:46:37 -0700 | [diff] [blame] | 98 | nop_routing_v4; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 99 | } |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 100 | counters = routing_v4_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 103 | /* |
| 104 | * ACL Table. |
| 105 | * Make final egress decision based on general metch fields. |
| 106 | */ |
| 107 | direct_counter(CounterType.packets_and_bytes) acl_counter; |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 108 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 109 | action set_next_id_acl(next_id_t next_id) { |
| 110 | fabric_metadata.next_id = next_id; |
| 111 | acl_counter.count(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 114 | // Send immendiatelly to CPU - skip the rest of pipeline. |
| 115 | action punt_to_cpu() { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 116 | standard_metadata.egress_spec = CPU_PORT; |
| 117 | acl_counter.count(); |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 118 | exit; |
| 119 | } |
| 120 | |
| 121 | action clone_to_cpu() { |
| 122 | // FIXME: works only if pkt will be replicated via PRE multicast group. |
| 123 | fabric_metadata.clone_to_cpu = _TRUE; |
| 124 | acl_counter.count(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 125 | } |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 126 | |
| 127 | action drop() { |
| 128 | mark_to_drop(); |
| 129 | acl_counter.count(); |
| 130 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 131 | |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 132 | action nop_acl() { |
| 133 | acl_counter.count(); |
| 134 | } |
| 135 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 136 | table acl { |
| 137 | key = { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 138 | standard_metadata.ingress_port: ternary; // 9 |
| 139 | fabric_metadata.ip_proto: ternary; // 8 |
| 140 | fabric_metadata.l4_src_port: ternary; // 16 |
| 141 | fabric_metadata.l4_dst_port: ternary; // 16 |
| 142 | |
| 143 | hdr.ethernet.dst_addr: ternary; // 48 |
| 144 | hdr.ethernet.src_addr: ternary; // 48 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 145 | hdr.vlan_tag.vlan_id: ternary; // 12 |
Yi Tseng | 8235a1a | 2018-07-24 20:57:28 +0800 | [diff] [blame] | 146 | hdr.vlan_tag.ether_type: ternary; //16 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 147 | hdr.ipv4.src_addr: ternary; // 32 |
| 148 | hdr.ipv4.dst_addr: ternary; // 32 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 149 | hdr.icmp.icmp_type: ternary; // 8 |
| 150 | hdr.icmp.icmp_code: ternary; // 8 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | actions = { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 154 | set_next_id_acl; |
Carmelo Cascone | 1e8843f | 2018-07-19 19:01:12 +0200 | [diff] [blame] | 155 | punt_to_cpu; |
| 156 | clone_to_cpu; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 157 | drop; |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 158 | nop_acl; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Charles Chan | cf696e5 | 2018-08-16 16:25:13 -0700 | [diff] [blame] | 161 | const default_action = nop_acl(); |
Yi Tseng | 8235a1a | 2018-07-24 20:57:28 +0800 | [diff] [blame] | 162 | size = 128; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 163 | counters = acl_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 166 | #ifdef WITH_IPV6 |
| 167 | /* |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 168 | * IPv6 Routing Table. |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 169 | * Matches IPv6 prefix and make egress decision. |
| 170 | */ |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 171 | direct_counter(CounterType.packets_and_bytes) routing_v6_counter; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 172 | |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 173 | action set_next_id_routing_v6(next_id_t next_id) { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 174 | fabric_metadata.next_id = next_id; |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 175 | routing_v6_counter.count(); |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 176 | } |
| 177 | |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 178 | table routing_v6 { |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 179 | key = { |
| 180 | hdr.ipv6.dst_addr: lpm; |
| 181 | } |
| 182 | |
| 183 | actions = { |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 184 | set_next_id_routing_v6; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 185 | } |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 186 | counters = routing_v6_counter; |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 187 | } |
Yi Tseng | 47eac89 | 2018-07-11 02:17:04 +0800 | [diff] [blame] | 188 | #endif // WITH_IPV6 |
| 189 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 190 | apply { |
| 191 | if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply(); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 192 | else if (fabric_metadata.fwd_type == FWD_MPLS) { |
| 193 | mpls.apply(); |
Yi Tseng | bd46d05 | 2018-01-22 17:18:16 -0800 | [diff] [blame] | 194 | |
| 195 | // TODO: IPv6 |
| 196 | hdr.vlan_tag.ether_type = ETHERTYPE_IPV4; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 197 | } |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 198 | else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) routing_v4.apply(); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 199 | #ifdef WITH_IPV6 |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame] | 200 | else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) routing_v6.apply(); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 201 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 202 | acl.apply(); |
| 203 | } |
| 204 | } |