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() { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 53 | standard_metadata.egress_spec = CPU_PORT; |
| 54 | } |
| 55 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 56 | table bridging { |
| 57 | key = { |
| 58 | hdr.vlan_tag.vlan_id: exact; |
| 59 | hdr.ethernet.dst_addr: ternary; |
| 60 | } |
| 61 | |
| 62 | actions = { |
| 63 | set_next_id; |
| 64 | } |
| 65 | counters = bridging_counter; |
| 66 | } |
| 67 | |
| 68 | table mpls { |
| 69 | key = { |
| 70 | hdr.mpls.label: exact; |
| 71 | } |
| 72 | |
| 73 | actions = { |
| 74 | pop_mpls_and_next; |
| 75 | } |
| 76 | counters = mpls_counter; |
| 77 | } |
| 78 | |
| 79 | table unicast_v4 { |
| 80 | key = { |
| 81 | hdr.ipv4.dst_addr: lpm; |
| 82 | } |
| 83 | |
| 84 | actions = { |
| 85 | set_next_id; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 86 | } |
| 87 | counters = unicast_v4_counter; |
| 88 | } |
| 89 | |
| 90 | table multicast_v4 { |
| 91 | key = { |
| 92 | hdr.vlan_tag.vlan_id: exact; |
| 93 | hdr.ipv4.dst_addr: lpm; |
| 94 | } |
| 95 | |
| 96 | actions = { |
| 97 | set_next_id; |
| 98 | } |
| 99 | counters = multicast_v4_counter; |
| 100 | } |
| 101 | |
| 102 | table unicast_v6 { |
| 103 | key = { |
| 104 | hdr.ipv6.dst_addr: lpm; |
| 105 | } |
| 106 | |
| 107 | actions = { |
| 108 | set_next_id; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 109 | } |
| 110 | counters = unicast_v6_counter; |
| 111 | } |
| 112 | |
| 113 | table multicast_v6 { |
| 114 | key = { |
| 115 | hdr.vlan_tag.vlan_id: exact; |
| 116 | hdr.ipv6.dst_addr: lpm; |
| 117 | } |
| 118 | |
| 119 | actions = { |
| 120 | set_next_id; |
| 121 | } |
| 122 | counters = multicast_v6_counter; |
| 123 | } |
| 124 | |
| 125 | table acl { |
| 126 | key = { |
| 127 | standard_metadata.ingress_port: ternary; |
| 128 | fabric_metadata.ip_proto: ternary; |
| 129 | hdr.ethernet.dst_addr: ternary; |
| 130 | hdr.ethernet.src_addr: ternary; |
| 131 | hdr.ethernet.ether_type: ternary; |
| 132 | hdr.vlan_tag.vlan_id: ternary; |
| 133 | hdr.vlan_tag.pri: ternary; |
| 134 | hdr.mpls.tc: ternary; |
| 135 | hdr.mpls.bos: ternary; |
| 136 | hdr.mpls.label: ternary; |
| 137 | hdr.ipv4.src_addr: ternary; |
| 138 | hdr.ipv4.dst_addr: ternary; |
| 139 | hdr.ipv4.protocol: ternary; |
| 140 | hdr.ipv6.src_addr: ternary; |
| 141 | hdr.ipv6.dst_addr: ternary; |
| 142 | hdr.ipv6.next_hdr: ternary; |
| 143 | hdr.tcp.src_port: ternary; |
| 144 | hdr.tcp.dst_port: ternary; |
| 145 | hdr.udp.src_port: ternary; |
| 146 | hdr.udp.dst_port: ternary; |
| 147 | hdr.icmp.icmp_type: ternary; |
| 148 | hdr.icmp.icmp_code: ternary; |
| 149 | } |
| 150 | |
| 151 | actions = { |
| 152 | set_next_id; |
| 153 | duplicate_to_controller; |
| 154 | drop; |
| 155 | nop; |
| 156 | } |
| 157 | |
| 158 | const default_action = nop(); |
| 159 | counters = acl_counter; |
| 160 | } |
| 161 | |
| 162 | apply { |
| 163 | if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply(); |
| 164 | else if (fabric_metadata.fwd_type == FWD_MPLS) mpls.apply(); |
| 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 | } |