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 | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 30 | direct_counter(CounterType.packets_and_bytes) bridging_counter; |
| 31 | direct_counter(CounterType.packets_and_bytes) mpls_counter; |
| 32 | direct_counter(CounterType.packets_and_bytes) unicast_v4_counter; |
| 33 | direct_counter(CounterType.packets_and_bytes) multicast_v4_counter; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 34 | direct_counter(CounterType.packets_and_bytes) acl_counter; |
| 35 | |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 36 | action drop() { |
| 37 | mark_to_drop(); |
| 38 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 39 | |
| 40 | action set_next_id(next_id_t next_id) { |
| 41 | fabric_metadata.next_id = next_id; |
| 42 | } |
| 43 | |
| 44 | action pop_mpls_and_next(next_id_t next_id) { |
| 45 | hdr.mpls.setInvalid(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 46 | fabric_metadata.next_id = next_id; |
| 47 | } |
| 48 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 49 | action duplicate_to_controller() { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 50 | standard_metadata.egress_spec = CPU_PORT; |
| 51 | } |
| 52 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 53 | table bridging { |
| 54 | key = { |
| 55 | hdr.vlan_tag.vlan_id: exact; |
| 56 | hdr.ethernet.dst_addr: ternary; |
| 57 | } |
| 58 | |
| 59 | actions = { |
| 60 | set_next_id; |
| 61 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 62 | counters = bridging_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 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 | |
| 76 | table unicast_v4 { |
| 77 | key = { |
| 78 | hdr.ipv4.dst_addr: lpm; |
| 79 | } |
| 80 | |
| 81 | actions = { |
| 82 | set_next_id; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 83 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 84 | counters = unicast_v4_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | table multicast_v4 { |
| 88 | key = { |
| 89 | hdr.vlan_tag.vlan_id: exact; |
| 90 | hdr.ipv4.dst_addr: lpm; |
| 91 | } |
| 92 | |
| 93 | actions = { |
| 94 | set_next_id; |
| 95 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 96 | counters = multicast_v4_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 99 | #ifdef WITH_IPV6 |
| 100 | direct_counter(CounterType.packets_and_bytes) unicast_v6_counter; |
| 101 | direct_counter(CounterType.packets_and_bytes) multicast_v6_counter; |
| 102 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 103 | table unicast_v6 { |
| 104 | key = { |
| 105 | hdr.ipv6.dst_addr: lpm; |
| 106 | } |
| 107 | |
| 108 | actions = { |
| 109 | set_next_id; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 110 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 111 | counters = unicast_v6_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | table multicast_v6 { |
| 115 | key = { |
| 116 | hdr.vlan_tag.vlan_id: exact; |
| 117 | hdr.ipv6.dst_addr: lpm; |
| 118 | } |
| 119 | |
| 120 | actions = { |
| 121 | set_next_id; |
| 122 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 123 | counters = multicast_v6_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 124 | } |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 125 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 126 | |
| 127 | table acl { |
| 128 | key = { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 129 | standard_metadata.ingress_port: ternary; // 9 |
| 130 | fabric_metadata.ip_proto: ternary; // 8 |
| 131 | fabric_metadata.l4_src_port: ternary; // 16 |
| 132 | fabric_metadata.l4_dst_port: ternary; // 16 |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 133 | fabric_metadata.original_ether_type: ternary; //16 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 134 | |
| 135 | hdr.ethernet.dst_addr: ternary; // 48 |
| 136 | hdr.ethernet.src_addr: ternary; // 48 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 137 | hdr.vlan_tag.vlan_id: ternary; // 12 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 138 | hdr.ipv4.src_addr: ternary; // 32 |
| 139 | hdr.ipv4.dst_addr: ternary; // 32 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 140 | hdr.icmp.icmp_type: ternary; // 8 |
| 141 | hdr.icmp.icmp_code: ternary; // 8 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | actions = { |
| 145 | set_next_id; |
| 146 | duplicate_to_controller; |
| 147 | drop; |
| 148 | nop; |
| 149 | } |
| 150 | |
| 151 | const default_action = nop(); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 152 | size = 256; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 153 | counters = acl_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | apply { |
| 157 | if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply(); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 158 | else if (fabric_metadata.fwd_type == FWD_MPLS) { |
| 159 | mpls.apply(); |
Yi Tseng | bd46d05 | 2018-01-22 17:18:16 -0800 | [diff] [blame] | 160 | |
| 161 | // TODO: IPv6 |
| 162 | hdr.vlan_tag.ether_type = ETHERTYPE_IPV4; |
| 163 | fabric_metadata.original_ether_type = ETHERTYPE_IPV4; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 164 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 165 | else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) unicast_v4.apply(); |
| 166 | else if (fabric_metadata.fwd_type == FWD_IPV4_MULTICAST) multicast_v4.apply(); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 167 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 168 | else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) unicast_v6.apply(); |
| 169 | else if (fabric_metadata.fwd_type == FWD_IPV6_MULTICAST) multicast_v6.apply(); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 170 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 171 | acl.apply(); |
| 172 | } |
| 173 | } |