blob: 2b3019cf540479f4395e6d6ba6c0d132656aa7c6 [file] [log] [blame]
Yi Tsengbe342052017-11-03 10:21:23 -07001/*
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
23control 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
Yi Tseng1b154bd2017-11-20 17:48:19 -080076 action push_mpls (mpls_label_t label, bit<3> tc) {
77 //Suppose that the maximum number of label is one.
78 hdr.mpls.setValid();
79 hdr.ethernet.ether_type = ETHERTYPE_MPLS;
80 hdr.mpls.label = label;
81 hdr.mpls.tc = tc;
82 hdr.mpls.bos = 1; // BOS = TRUE
83 hdr.mpls.ttl = DEFAULT_MPLS_TTL;
84 }
85
86 action mpls_routing_v4 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
87 mpls_label_t label) {
88 l3_routing(port_num, smac, dmac);
89 push_mpls(label, hdr.ipv4.diffserv[7:5]);
90 }
91
92 action mpls_routing_v6 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
93 mpls_label_t label) {
94 l3_routing(port_num, smac, dmac);
95 push_mpls(label, hdr.ipv6.traffic_class[7:5]);
96 }
97
Yi Tsengbe342052017-11-03 10:21:23 -070098 table next_id_mapping {
99 key = {
100 fabric_metadata.next_id: exact;
101 }
102
103 actions = {
104 set_next_type;
105 }
106 counters = next_id_mapping_counter;
107 }
108
109 table simple {
110 key = {
111 fabric_metadata.next_id: exact;
112 }
113
114 actions = {
115 output;
116 set_vlan_output;
117 l3_routing;
118 }
119 counters = simple_counter;
120 }
121
122 table hashed {
123 key = {
124 fabric_metadata.next_id: exact;
125 hdr.ipv4.src_addr: selector;
126 hdr.ipv4.dst_addr: selector;
127 hdr.ipv4.protocol: selector;
128 hdr.ipv6.src_addr: selector;
129 hdr.ipv6.dst_addr: selector;
130 hdr.ipv6.next_hdr: selector;
131 fabric_metadata.l4_src_port: selector;
132 fabric_metadata.l4_dst_port: selector;
133 }
134
135 actions = {
136 l3_routing;
Yi Tseng1b154bd2017-11-20 17:48:19 -0800137 mpls_routing_v4;
138 mpls_routing_v6;
Yi Tsengbe342052017-11-03 10:21:23 -0700139 }
140
141 implementation = ecmp_selector;
142 counters = hashed_counter;
143 }
144
145 /*
146 * Work in progress
147 */
148 table broadcast {
149 key = {
150 fabric_metadata.next_id: exact;
151 }
152 actions = {
153 set_mcast_group;
154 }
155 counters = broadcast_counter;
156 }
157
158 apply {
159 next_id_mapping.apply();
160 if (fabric_metadata.next_type == NEXT_TYPE_SIMPLE) simple.apply();
161 else if (fabric_metadata.next_type == NEXT_TYPE_HASHED) hashed.apply();
162 else if (fabric_metadata.next_type == NEXT_TYPE_BROADCAST) broadcast.apply();
163 // next_type == PUNT, leave it to packet-io egress
164 }
165}
166
167control EgressNextControl (
168 inout parsed_headers_t hdr,
169 inout fabric_metadata_t fabric_metadata,
170 inout standard_metadata_t standard_metadata){
171
172 apply {
173 // pop internal vlan if the meta is set
174 if (fabric_metadata.pop_vlan_at_egress) {
175 hdr.vlan_tag.setInvalid();
176 }
177 }
178}