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) { |
| 27 | direct_counter(CounterType.packets_and_bytes) next_id_mapping_counter; |
| 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; |
| 31 | action_selector(HashAlgorithm.crc16, 32w64, 32w16) ecmp_selector; |
| 32 | |
| 33 | action set_next_type(next_type_t next_type) { |
| 34 | fabric_metadata.next_type = next_type; |
| 35 | } |
| 36 | |
| 37 | action output(port_num_t port_num) { |
| 38 | standard_metadata.egress_spec = port_num; |
| 39 | if(!hdr.mpls.isValid()) { |
| 40 | if(hdr.ipv4.isValid()) { |
| 41 | hdr.ipv4.ttl = hdr.ipv4.ttl - 1; |
| 42 | } |
| 43 | else if (hdr.ipv6.isValid()) { |
| 44 | hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | action set_vlan_output(vlan_id_t new_vlan_id, port_num_t port_num){ |
| 50 | hdr.vlan_tag.vlan_id = new_vlan_id; |
| 51 | |
| 52 | // don't remove the vlan from egress since we set the vlan to it. |
| 53 | fabric_metadata.pop_vlan_at_egress = false; |
| 54 | output(port_num); |
| 55 | } |
| 56 | |
| 57 | action rewrite_smac(mac_addr_t smac) { |
| 58 | hdr.ethernet.src_addr = smac; |
| 59 | } |
| 60 | |
| 61 | action rewrite_dmac(mac_addr_t dmac) { |
| 62 | hdr.ethernet.dst_addr = dmac; |
| 63 | } |
| 64 | |
| 65 | action l3_routing(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) { |
| 66 | rewrite_smac(smac); |
| 67 | rewrite_dmac(dmac); |
| 68 | output(port_num); |
| 69 | } |
| 70 | |
| 71 | action set_mcast_group(group_id_t gid, mac_addr_t smac) { |
| 72 | standard_metadata.mcast_grp = gid; |
| 73 | rewrite_smac(smac); |
| 74 | } |
| 75 | |
| 76 | table next_id_mapping { |
| 77 | key = { |
| 78 | fabric_metadata.next_id: exact; |
| 79 | } |
| 80 | |
| 81 | actions = { |
| 82 | set_next_type; |
| 83 | } |
| 84 | counters = next_id_mapping_counter; |
| 85 | } |
| 86 | |
| 87 | table simple { |
| 88 | key = { |
| 89 | fabric_metadata.next_id: exact; |
| 90 | } |
| 91 | |
| 92 | actions = { |
| 93 | output; |
| 94 | set_vlan_output; |
| 95 | l3_routing; |
| 96 | } |
| 97 | counters = simple_counter; |
| 98 | } |
| 99 | |
| 100 | table hashed { |
| 101 | key = { |
| 102 | fabric_metadata.next_id: exact; |
| 103 | hdr.ipv4.src_addr: selector; |
| 104 | hdr.ipv4.dst_addr: selector; |
| 105 | hdr.ipv4.protocol: selector; |
| 106 | hdr.ipv6.src_addr: selector; |
| 107 | hdr.ipv6.dst_addr: selector; |
| 108 | hdr.ipv6.next_hdr: selector; |
| 109 | fabric_metadata.l4_src_port: selector; |
| 110 | fabric_metadata.l4_dst_port: selector; |
| 111 | } |
| 112 | |
| 113 | actions = { |
| 114 | l3_routing; |
| 115 | } |
| 116 | |
| 117 | implementation = ecmp_selector; |
| 118 | counters = hashed_counter; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Work in progress |
| 123 | */ |
| 124 | table broadcast { |
| 125 | key = { |
| 126 | fabric_metadata.next_id: exact; |
| 127 | } |
| 128 | actions = { |
| 129 | set_mcast_group; |
| 130 | } |
| 131 | counters = broadcast_counter; |
| 132 | } |
| 133 | |
| 134 | apply { |
| 135 | next_id_mapping.apply(); |
| 136 | if (fabric_metadata.next_type == NEXT_TYPE_SIMPLE) simple.apply(); |
| 137 | else if (fabric_metadata.next_type == NEXT_TYPE_HASHED) hashed.apply(); |
| 138 | else if (fabric_metadata.next_type == NEXT_TYPE_BROADCAST) broadcast.apply(); |
| 139 | // next_type == PUNT, leave it to packet-io egress |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | control EgressNextControl ( |
| 144 | inout parsed_headers_t hdr, |
| 145 | inout fabric_metadata_t fabric_metadata, |
| 146 | inout standard_metadata_t standard_metadata){ |
| 147 | |
| 148 | apply { |
| 149 | // pop internal vlan if the meta is set |
| 150 | if (fabric_metadata.pop_vlan_at_egress) { |
| 151 | hdr.vlan_tag.setInvalid(); |
| 152 | } |
| 153 | } |
| 154 | } |