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