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 | |
| 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 | |
| 38 | action set_next_id(next_id_t next_id) { |
| 39 | fabric_metadata.next_id = next_id; |
| 40 | } |
| 41 | |
| 42 | action pop_mpls_and_next(next_id_t next_id) { |
| 43 | hdr.mpls.setInvalid(); |
| 44 | if (hdr.ipv4.isValid()) { |
| 45 | hdr.ethernet.ether_type = ETHERTYPE_IPV4; |
| 46 | } else { |
| 47 | hdr.ethernet.ether_type = ETHERTYPE_IPV6; |
| 48 | } |
| 49 | fabric_metadata.next_id = next_id; |
| 50 | } |
| 51 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 52 | action duplicate_to_controller() { |
| 53 | fabric_metadata.next_type = NEXT_TYPE_PUNT; |
| 54 | standard_metadata.egress_spec = CPU_PORT; |
| 55 | } |
| 56 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 57 | table bridging { |
| 58 | key = { |
| 59 | hdr.vlan_tag.vlan_id: exact; |
| 60 | hdr.ethernet.dst_addr: ternary; |
| 61 | } |
| 62 | |
| 63 | actions = { |
| 64 | set_next_id; |
| 65 | } |
| 66 | counters = bridging_counter; |
| 67 | } |
| 68 | |
| 69 | table mpls { |
| 70 | key = { |
| 71 | hdr.mpls.label: exact; |
| 72 | } |
| 73 | |
| 74 | actions = { |
| 75 | pop_mpls_and_next; |
| 76 | } |
| 77 | counters = mpls_counter; |
| 78 | } |
| 79 | |
| 80 | table unicast_v4 { |
| 81 | key = { |
| 82 | hdr.ipv4.dst_addr: lpm; |
| 83 | } |
| 84 | |
| 85 | actions = { |
| 86 | set_next_id; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 87 | } |
| 88 | counters = unicast_v4_counter; |
| 89 | } |
| 90 | |
| 91 | table multicast_v4 { |
| 92 | key = { |
| 93 | hdr.vlan_tag.vlan_id: exact; |
| 94 | hdr.ipv4.dst_addr: lpm; |
| 95 | } |
| 96 | |
| 97 | actions = { |
| 98 | set_next_id; |
| 99 | } |
| 100 | counters = multicast_v4_counter; |
| 101 | } |
| 102 | |
| 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 | } |
| 111 | counters = unicast_v6_counter; |
| 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 | } |
| 123 | counters = multicast_v6_counter; |
| 124 | } |
| 125 | |
| 126 | table acl { |
| 127 | key = { |
| 128 | standard_metadata.ingress_port: ternary; |
| 129 | fabric_metadata.ip_proto: ternary; |
| 130 | hdr.ethernet.dst_addr: ternary; |
| 131 | hdr.ethernet.src_addr: ternary; |
| 132 | hdr.ethernet.ether_type: ternary; |
| 133 | hdr.vlan_tag.vlan_id: ternary; |
| 134 | hdr.vlan_tag.pri: ternary; |
| 135 | hdr.mpls.tc: ternary; |
| 136 | hdr.mpls.bos: ternary; |
| 137 | hdr.mpls.label: ternary; |
| 138 | hdr.ipv4.src_addr: ternary; |
| 139 | hdr.ipv4.dst_addr: ternary; |
| 140 | hdr.ipv4.protocol: ternary; |
| 141 | hdr.ipv6.src_addr: ternary; |
| 142 | hdr.ipv6.dst_addr: ternary; |
| 143 | hdr.ipv6.next_hdr: ternary; |
| 144 | hdr.tcp.src_port: ternary; |
| 145 | hdr.tcp.dst_port: ternary; |
| 146 | hdr.udp.src_port: ternary; |
| 147 | hdr.udp.dst_port: ternary; |
| 148 | hdr.icmp.icmp_type: ternary; |
| 149 | hdr.icmp.icmp_code: ternary; |
| 150 | } |
| 151 | |
| 152 | actions = { |
| 153 | set_next_id; |
| 154 | duplicate_to_controller; |
| 155 | drop; |
| 156 | nop; |
| 157 | } |
| 158 | |
| 159 | const default_action = nop(); |
| 160 | counters = acl_counter; |
| 161 | } |
| 162 | |
| 163 | apply { |
| 164 | if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply(); |
| 165 | else if (fabric_metadata.fwd_type == FWD_MPLS) mpls.apply(); |
| 166 | else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) unicast_v4.apply(); |
| 167 | else if (fabric_metadata.fwd_type == FWD_IPV4_MULTICAST) multicast_v4.apply(); |
| 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(); |
| 170 | acl.apply(); |
| 171 | } |
| 172 | } |