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 | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 28 | direct_counter(CounterType.packets_and_bytes) simple_counter; |
| 29 | direct_counter(CounterType.packets_and_bytes) hashed_counter; |
| 30 | direct_counter(CounterType.packets_and_bytes) broadcast_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 | |
| 36 | action set_vlan_output(vlan_id_t new_vlan_id, port_num_t port_num){ |
| 37 | hdr.vlan_tag.vlan_id = new_vlan_id; |
| 38 | |
| 39 | // don't remove the vlan from egress since we set the vlan to it. |
| 40 | fabric_metadata.pop_vlan_at_egress = false; |
| 41 | output(port_num); |
| 42 | } |
| 43 | |
| 44 | action rewrite_smac(mac_addr_t smac) { |
| 45 | hdr.ethernet.src_addr = smac; |
| 46 | } |
| 47 | |
| 48 | action rewrite_dmac(mac_addr_t dmac) { |
| 49 | hdr.ethernet.dst_addr = dmac; |
| 50 | } |
| 51 | |
| 52 | action l3_routing(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) { |
| 53 | rewrite_smac(smac); |
| 54 | rewrite_dmac(dmac); |
| 55 | output(port_num); |
| 56 | } |
| 57 | |
| 58 | action set_mcast_group(group_id_t gid, mac_addr_t smac) { |
| 59 | standard_metadata.mcast_grp = gid; |
| 60 | rewrite_smac(smac); |
| 61 | } |
| 62 | |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 63 | action push_mpls (mpls_label_t label, bit<3> tc) { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 64 | // Suppose that the maximum number of label is one. |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 65 | hdr.mpls.setValid(); |
| 66 | hdr.ethernet.ether_type = ETHERTYPE_MPLS; |
| 67 | hdr.mpls.label = label; |
| 68 | hdr.mpls.tc = tc; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 69 | hdr.mpls.bos = 1w1; // BOS = TRUE |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 70 | hdr.mpls.ttl = DEFAULT_MPLS_TTL; |
| 71 | } |
| 72 | |
| 73 | action mpls_routing_v4 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac, |
| 74 | mpls_label_t label) { |
| 75 | l3_routing(port_num, smac, dmac); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 76 | |
| 77 | // TODO: set tc according to diffserv from ipv4 |
| 78 | push_mpls(label, 3w0); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | action mpls_routing_v6 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac, |
| 82 | mpls_label_t label) { |
| 83 | l3_routing(port_num, smac, dmac); |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 84 | |
| 85 | // TODO: set tc according to traffic_class from ipv4 |
| 86 | push_mpls(label, 3w0); |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 89 | table simple { |
| 90 | key = { |
| 91 | fabric_metadata.next_id: exact; |
| 92 | } |
| 93 | |
| 94 | actions = { |
| 95 | output; |
| 96 | set_vlan_output; |
| 97 | l3_routing; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 98 | mpls_routing_v4; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 99 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 100 | counters = simple_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | table hashed { |
| 104 | key = { |
| 105 | fabric_metadata.next_id: exact; |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 106 | hdr.ethernet.dst_addr: selector; |
| 107 | hdr.ethernet.src_addr: selector; |
| 108 | fabric_metadata.ip_proto: selector; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 109 | fabric_metadata.l4_src_port: selector; |
| 110 | fabric_metadata.l4_dst_port: selector; |
| 111 | } |
| 112 | |
| 113 | actions = { |
| 114 | l3_routing; |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 115 | mpls_routing_v4; |
| 116 | mpls_routing_v6; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | implementation = ecmp_selector; |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 120 | counters = hashed_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Work in progress |
| 125 | */ |
| 126 | table broadcast { |
| 127 | key = { |
| 128 | fabric_metadata.next_id: exact; |
| 129 | } |
| 130 | actions = { |
| 131 | set_mcast_group; |
| 132 | } |
Yi Tseng | 3a5731e | 2018-01-22 11:38:58 -0800 | [diff] [blame] | 133 | counters = broadcast_counter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | apply { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 137 | if (simple.apply().hit) { |
| 138 | if (!hdr.mpls.isValid()) { |
| 139 | if(hdr.ipv4.isValid()) { |
| 140 | hdr.ipv4.ttl = hdr.ipv4.ttl - 1; |
| 141 | } |
| 142 | else if (hdr.ipv6.isValid()) { |
| 143 | hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1; |
| 144 | } |
| 145 | } |
| 146 | } |
Yi Tseng | f55eaa8 | 2017-11-29 15:51:28 -0800 | [diff] [blame] | 147 | hashed.apply(); |
| 148 | broadcast.apply(); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | control EgressNextControl ( |
| 153 | inout parsed_headers_t hdr, |
| 154 | inout fabric_metadata_t fabric_metadata, |
| 155 | inout standard_metadata_t standard_metadata){ |
| 156 | |
| 157 | apply { |
| 158 | // pop internal vlan if the meta is set |
| 159 | if (fabric_metadata.pop_vlan_at_egress) { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 160 | if (hdr.mpls.isValid()) { |
| 161 | hdr.ethernet.ether_type = ETHERTYPE_MPLS; |
| 162 | } else { |
| 163 | hdr.ethernet.ether_type = fabric_metadata.original_ether_type; |
| 164 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 165 | hdr.vlan_tag.setInvalid(); |
| 166 | } |
| 167 | } |
| 168 | } |