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 "../header.p4" |
| 21 | #include "../action.p4" |
| 22 | |
| 23 | control Next ( |
| 24 | inout parsed_headers_t hdr, |
| 25 | inout fabric_metadata_t fabric_metadata, |
| 26 | inout standard_metadata_t standard_metadata) { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 27 | action_selector(HashAlgorithm.crc16, 32w64, 32w16) ecmp_selector; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 28 | direct_counter(CounterType.packets_and_bytes) vlan_meta_counter; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 29 | direct_counter(CounterType.packets_and_bytes) simple_counter; |
| 30 | direct_counter(CounterType.packets_and_bytes) hashed_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 31 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 32 | action output(port_num_t port_num) { |
| 33 | standard_metadata.egress_spec = port_num; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 36 | action set_vlan(vlan_id_t new_vlan_id) { |
| 37 | hdr.vlan_tag.vlan_id = new_vlan_id; |
| 38 | } |
| 39 | |
| 40 | action pop_vlan() { |
| 41 | hdr.ethernet.ether_type = hdr.vlan_tag.ether_type; |
| 42 | hdr.vlan_tag.setInvalid(); |
| 43 | } |
| 44 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 45 | action set_vlan_output(vlan_id_t new_vlan_id, port_num_t port_num){ |
| 46 | hdr.vlan_tag.vlan_id = new_vlan_id; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 47 | output(port_num); |
| 48 | } |
| 49 | |
| 50 | action rewrite_smac(mac_addr_t smac) { |
| 51 | hdr.ethernet.src_addr = smac; |
| 52 | } |
| 53 | |
| 54 | action rewrite_dmac(mac_addr_t dmac) { |
| 55 | hdr.ethernet.dst_addr = dmac; |
| 56 | } |
| 57 | |
| 58 | action l3_routing(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) { |
| 59 | rewrite_smac(smac); |
| 60 | rewrite_dmac(dmac); |
| 61 | output(port_num); |
| 62 | } |
| 63 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 64 | action l3_routing_vlan(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac, vlan_id_t new_vlan_id) { |
| 65 | rewrite_smac(smac); |
| 66 | rewrite_dmac(dmac); |
| 67 | set_vlan_output(new_vlan_id, port_num); |
| 68 | } |
| 69 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 70 | action push_mpls (mpls_label_t label, bit<3> tc) { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 71 | // Suppose that the maximum number of label is one. |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 72 | hdr.mpls.setValid(); |
Yi Tseng | bd46d05 | 2018-01-22 17:18:16 -0800 | [diff] [blame] | 73 | hdr.vlan_tag.ether_type = ETHERTYPE_MPLS; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 74 | hdr.mpls.label = label; |
| 75 | hdr.mpls.tc = tc; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 76 | hdr.mpls.bos = 1w1; // BOS = TRUE |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 77 | hdr.mpls.ttl = DEFAULT_MPLS_TTL; |
| 78 | } |
| 79 | |
| 80 | action mpls_routing_v4 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac, |
| 81 | mpls_label_t label) { |
| 82 | l3_routing(port_num, smac, dmac); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 83 | |
| 84 | // TODO: set tc according to diffserv from ipv4 |
| 85 | push_mpls(label, 3w0); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | action mpls_routing_v6 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac, |
| 89 | mpls_label_t label) { |
| 90 | l3_routing(port_num, smac, dmac); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 91 | |
| 92 | // TODO: set tc according to traffic_class from ipv4 |
| 93 | push_mpls(label, 3w0); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 96 | table vlan_meta { |
| 97 | key = { |
| 98 | fabric_metadata.next_id: exact; |
| 99 | } |
| 100 | |
| 101 | actions = { |
| 102 | set_vlan; |
| 103 | nop; |
| 104 | } |
| 105 | default_action = nop; |
| 106 | counters = vlan_meta_counter; |
| 107 | } |
| 108 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 109 | table simple { |
| 110 | key = { |
| 111 | fabric_metadata.next_id: exact; |
| 112 | } |
| 113 | |
| 114 | actions = { |
| 115 | output; |
| 116 | set_vlan_output; |
| 117 | l3_routing; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 118 | mpls_routing_v4; |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 119 | l3_routing_vlan; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 120 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 121 | counters = simple_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | table hashed { |
| 125 | key = { |
| 126 | fabric_metadata.next_id: exact; |
Yi Tseng | 3d3956d | 2018-01-31 17:28:05 -0800 | [diff] [blame] | 127 | hdr.ipv4.dst_addr: selector; |
| 128 | hdr.ipv4.src_addr: selector; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 129 | fabric_metadata.ip_proto: selector; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 130 | fabric_metadata.l4_src_port: selector; |
| 131 | fabric_metadata.l4_dst_port: selector; |
| 132 | } |
| 133 | |
| 134 | actions = { |
| 135 | l3_routing; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 136 | mpls_routing_v4; |
| 137 | mpls_routing_v6; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | implementation = ecmp_selector; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 141 | counters = hashed_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 144 | #ifdef WITH_MULTICAST |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 145 | /* |
| 146 | * Work in progress |
| 147 | */ |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 148 | action set_mcast_group(group_id_t gid, mac_addr_t smac) { |
| 149 | standard_metadata.mcast_grp = gid; |
| 150 | rewrite_smac(smac); |
| 151 | } |
| 152 | |
| 153 | direct_counter(CounterType.packets_and_bytes) multicast_counter; |
| 154 | |
| 155 | table multicast { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 156 | key = { |
| 157 | fabric_metadata.next_id: exact; |
| 158 | } |
| 159 | actions = { |
| 160 | set_mcast_group; |
| 161 | } |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 162 | counters = multicast_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 163 | } |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 164 | #endif // WITH_MULTICAST |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 165 | |
| 166 | apply { |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 167 | vlan_meta.apply(); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 168 | if (simple.apply().hit) { |
| 169 | if (!hdr.mpls.isValid()) { |
| 170 | if(hdr.ipv4.isValid()) { |
| 171 | hdr.ipv4.ttl = hdr.ipv4.ttl - 1; |
| 172 | } |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 173 | #ifdef WITH_IPV6 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 174 | else if (hdr.ipv6.isValid()) { |
| 175 | hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1; |
| 176 | } |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 177 | #endif // WITH_IPV6 |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
Yi Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame] | 180 | hashed.apply(); |
Carmelo Cascone | a106140 | 2018-02-03 17:39:59 -0800 | [diff] [blame] | 181 | #ifdef WITH_MULTICAST |
| 182 | multicast.apply(); |
| 183 | #endif // WITH_MULTICAST |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | control EgressNextControl ( |
| 188 | inout parsed_headers_t hdr, |
| 189 | inout fabric_metadata_t fabric_metadata, |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 190 | inout standard_metadata_t standard_metadata) { |
| 191 | |
| 192 | action pop_vlan() { |
| 193 | hdr.ethernet.ether_type = hdr.vlan_tag.ether_type; |
| 194 | hdr.vlan_tag.setInvalid(); |
| 195 | } |
| 196 | |
| 197 | table egress_vlan { |
| 198 | key = { |
| 199 | hdr.vlan_tag.vlan_id: exact; |
| 200 | standard_metadata.egress_port: exact; |
| 201 | } |
| 202 | actions = { |
| 203 | pop_vlan; |
| 204 | nop; |
| 205 | } |
| 206 | default_action = nop; |
| 207 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 208 | |
| 209 | apply { |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 210 | egress_vlan.apply(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 211 | } |
| 212 | } |