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 | |
| 52 | action push_mpls (mpls_label_t label, bit<3> tc) { |
| 53 | //Suppose that the maximum number of label is one. |
| 54 | hdr.mpls.setValid(); |
| 55 | hdr.ethernet.ether_type = ETHERTYPE_MPLS; |
| 56 | hdr.mpls.label = label; |
| 57 | hdr.mpls.tc = tc; |
| 58 | hdr.mpls.bos = 1; |
| 59 | hdr.mpls.ttl = 64; |
| 60 | } |
| 61 | |
| 62 | action push_mpls_and_next_v4 (mpls_label_t label, |
| 63 | next_id_t next_id) { |
| 64 | set_next_id(next_id); |
| 65 | push_mpls(label, hdr.ipv4.diffserv[7:5]); |
| 66 | } |
| 67 | |
| 68 | action push_mpls_and_next_v6 (mpls_label_t label, next_id_t next_id) { |
| 69 | set_next_id(next_id); |
| 70 | push_mpls(label, hdr.ipv6.traffic_class[7:5]); |
| 71 | } |
| 72 | |
| 73 | action duplicate_to_controller() { |
| 74 | fabric_metadata.next_type = NEXT_TYPE_PUNT; |
| 75 | standard_metadata.egress_spec = CPU_PORT; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | table bridging { |
| 81 | key = { |
| 82 | hdr.vlan_tag.vlan_id: exact; |
| 83 | hdr.ethernet.dst_addr: ternary; |
| 84 | } |
| 85 | |
| 86 | actions = { |
| 87 | set_next_id; |
| 88 | } |
| 89 | counters = bridging_counter; |
| 90 | } |
| 91 | |
| 92 | table mpls { |
| 93 | key = { |
| 94 | hdr.mpls.label: exact; |
| 95 | } |
| 96 | |
| 97 | actions = { |
| 98 | pop_mpls_and_next; |
| 99 | } |
| 100 | counters = mpls_counter; |
| 101 | } |
| 102 | |
| 103 | table unicast_v4 { |
| 104 | key = { |
| 105 | hdr.ipv4.dst_addr: lpm; |
| 106 | } |
| 107 | |
| 108 | actions = { |
| 109 | set_next_id; |
| 110 | push_mpls_and_next_v4; |
| 111 | } |
| 112 | counters = unicast_v4_counter; |
| 113 | } |
| 114 | |
| 115 | table multicast_v4 { |
| 116 | key = { |
| 117 | hdr.vlan_tag.vlan_id: exact; |
| 118 | hdr.ipv4.dst_addr: lpm; |
| 119 | } |
| 120 | |
| 121 | actions = { |
| 122 | set_next_id; |
| 123 | } |
| 124 | counters = multicast_v4_counter; |
| 125 | } |
| 126 | |
| 127 | table unicast_v6 { |
| 128 | key = { |
| 129 | hdr.ipv6.dst_addr: lpm; |
| 130 | } |
| 131 | |
| 132 | actions = { |
| 133 | set_next_id; |
| 134 | push_mpls_and_next_v6; |
| 135 | } |
| 136 | counters = unicast_v6_counter; |
| 137 | } |
| 138 | |
| 139 | table multicast_v6 { |
| 140 | key = { |
| 141 | hdr.vlan_tag.vlan_id: exact; |
| 142 | hdr.ipv6.dst_addr: lpm; |
| 143 | } |
| 144 | |
| 145 | actions = { |
| 146 | set_next_id; |
| 147 | } |
| 148 | counters = multicast_v6_counter; |
| 149 | } |
| 150 | |
| 151 | table acl { |
| 152 | key = { |
| 153 | standard_metadata.ingress_port: ternary; |
| 154 | fabric_metadata.ip_proto: ternary; |
| 155 | hdr.ethernet.dst_addr: ternary; |
| 156 | hdr.ethernet.src_addr: ternary; |
| 157 | hdr.ethernet.ether_type: ternary; |
| 158 | hdr.vlan_tag.vlan_id: ternary; |
| 159 | hdr.vlan_tag.pri: ternary; |
| 160 | hdr.mpls.tc: ternary; |
| 161 | hdr.mpls.bos: ternary; |
| 162 | hdr.mpls.label: ternary; |
| 163 | hdr.ipv4.src_addr: ternary; |
| 164 | hdr.ipv4.dst_addr: ternary; |
| 165 | hdr.ipv4.protocol: ternary; |
| 166 | hdr.ipv6.src_addr: ternary; |
| 167 | hdr.ipv6.dst_addr: ternary; |
| 168 | hdr.ipv6.next_hdr: ternary; |
| 169 | hdr.tcp.src_port: ternary; |
| 170 | hdr.tcp.dst_port: ternary; |
| 171 | hdr.udp.src_port: ternary; |
| 172 | hdr.udp.dst_port: ternary; |
| 173 | hdr.icmp.icmp_type: ternary; |
| 174 | hdr.icmp.icmp_code: ternary; |
| 175 | } |
| 176 | |
| 177 | actions = { |
| 178 | set_next_id; |
| 179 | duplicate_to_controller; |
| 180 | drop; |
| 181 | nop; |
| 182 | } |
| 183 | |
| 184 | const default_action = nop(); |
| 185 | counters = acl_counter; |
| 186 | } |
| 187 | |
| 188 | apply { |
| 189 | if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply(); |
| 190 | else if (fabric_metadata.fwd_type == FWD_MPLS) mpls.apply(); |
| 191 | else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) unicast_v4.apply(); |
| 192 | else if (fabric_metadata.fwd_type == FWD_IPV4_MULTICAST) multicast_v4.apply(); |
| 193 | else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) unicast_v6.apply(); |
| 194 | else if (fabric_metadata.fwd_type == FWD_IPV6_MULTICAST) multicast_v6.apply(); |
| 195 | acl.apply(); |
| 196 | } |
| 197 | } |