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