blob: 0b22ca15406a3a917303970be9fc7f92bda20727 [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) {
Yi Tsengbe342052017-11-03 10:21:23 -070027 action_selector(HashAlgorithm.crc16, 32w64, 32w16) ecmp_selector;
Yi Tseng20f9e7b2018-05-24 23:27:39 +080028 direct_counter(CounterType.packets_and_bytes) vlan_meta_counter;
Yi Tseng3a5731e2018-01-22 11:38:58 -080029 direct_counter(CounterType.packets_and_bytes) simple_counter;
30 direct_counter(CounterType.packets_and_bytes) hashed_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070031
Yi Tsengbe342052017-11-03 10:21:23 -070032 action output(port_num_t port_num) {
33 standard_metadata.egress_spec = port_num;
Yi Tsengbe342052017-11-03 10:21:23 -070034 }
35
Yi Tseng20f9e7b2018-05-24 23:27:39 +080036 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 Tsengbe342052017-11-03 10:21:23 -070045 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 Tsengbe342052017-11-03 10:21:23 -070047 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 Tseng20f9e7b2018-05-24 23:27:39 +080064 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 Tseng1b154bd2017-11-20 17:48:19 -080070 action push_mpls (mpls_label_t label, bit<3> tc) {
Yi Tseng1d842672017-11-28 16:06:52 -080071 // Suppose that the maximum number of label is one.
Yi Tseng1b154bd2017-11-20 17:48:19 -080072 hdr.mpls.setValid();
Yi Tsengbd46d052018-01-22 17:18:16 -080073 hdr.vlan_tag.ether_type = ETHERTYPE_MPLS;
Yi Tseng1b154bd2017-11-20 17:48:19 -080074 hdr.mpls.label = label;
75 hdr.mpls.tc = tc;
Yi Tseng1d842672017-11-28 16:06:52 -080076 hdr.mpls.bos = 1w1; // BOS = TRUE
Yi Tseng1b154bd2017-11-20 17:48:19 -080077 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 Tseng1d842672017-11-28 16:06:52 -080083
84 // TODO: set tc according to diffserv from ipv4
85 push_mpls(label, 3w0);
Yi Tseng1b154bd2017-11-20 17:48:19 -080086 }
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 Tseng1d842672017-11-28 16:06:52 -080091
92 // TODO: set tc according to traffic_class from ipv4
93 push_mpls(label, 3w0);
Yi Tseng1b154bd2017-11-20 17:48:19 -080094 }
95
Yi Tseng20f9e7b2018-05-24 23:27:39 +080096 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 Tsengbe342052017-11-03 10:21:23 -0700109 table simple {
110 key = {
111 fabric_metadata.next_id: exact;
112 }
113
114 actions = {
115 output;
116 set_vlan_output;
117 l3_routing;
Yi Tseng3a5731e2018-01-22 11:38:58 -0800118 mpls_routing_v4;
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800119 l3_routing_vlan;
Yi Tsengbe342052017-11-03 10:21:23 -0700120 }
Yi Tseng3a5731e2018-01-22 11:38:58 -0800121 counters = simple_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700122 }
123
124 table hashed {
125 key = {
126 fabric_metadata.next_id: exact;
Yi Tseng3d3956d2018-01-31 17:28:05 -0800127 hdr.ipv4.dst_addr: selector;
128 hdr.ipv4.src_addr: selector;
Yi Tseng1d842672017-11-28 16:06:52 -0800129 fabric_metadata.ip_proto: selector;
Yi Tsengbe342052017-11-03 10:21:23 -0700130 fabric_metadata.l4_src_port: selector;
131 fabric_metadata.l4_dst_port: selector;
132 }
133
134 actions = {
135 l3_routing;
Yi Tseng1b154bd2017-11-20 17:48:19 -0800136 mpls_routing_v4;
137 mpls_routing_v6;
Yi Tsengbe342052017-11-03 10:21:23 -0700138 }
139
140 implementation = ecmp_selector;
Yi Tseng3a5731e2018-01-22 11:38:58 -0800141 counters = hashed_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700142 }
143
144 /*
145 * Work in progress
146 */
Esin Karaman971fb7f2017-12-28 13:44:52 +0000147 action set_mcast_group(group_id_t gid) {
Carmelo Casconea1061402018-02-03 17:39:59 -0800148 standard_metadata.mcast_grp = gid;
Carmelo Casconea1061402018-02-03 17:39:59 -0800149 }
150
151 direct_counter(CounterType.packets_and_bytes) multicast_counter;
152
153 table multicast {
Yi Tsengbe342052017-11-03 10:21:23 -0700154 key = {
155 fabric_metadata.next_id: exact;
156 }
157 actions = {
158 set_mcast_group;
159 }
Carmelo Casconea1061402018-02-03 17:39:59 -0800160 counters = multicast_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700161 }
162
163 apply {
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800164 vlan_meta.apply();
Yi Tseng1d842672017-11-28 16:06:52 -0800165 if (simple.apply().hit) {
166 if (!hdr.mpls.isValid()) {
167 if(hdr.ipv4.isValid()) {
168 hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
169 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800170#ifdef WITH_IPV6
Yi Tseng1d842672017-11-28 16:06:52 -0800171 else if (hdr.ipv6.isValid()) {
172 hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1;
173 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800174#endif // WITH_IPV6
Yi Tseng1d842672017-11-28 16:06:52 -0800175 }
176 }
Yi Tsengf55eaa82017-11-29 15:51:28 -0800177 hashed.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800178 multicast.apply();
Yi Tsengbe342052017-11-03 10:21:23 -0700179 }
180}
181
182control EgressNextControl (
183 inout parsed_headers_t hdr,
184 inout fabric_metadata_t fabric_metadata,
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800185 inout standard_metadata_t standard_metadata) {
186
187 action pop_vlan() {
188 hdr.ethernet.ether_type = hdr.vlan_tag.ether_type;
189 hdr.vlan_tag.setInvalid();
190 }
191
192 table egress_vlan {
193 key = {
194 hdr.vlan_tag.vlan_id: exact;
195 standard_metadata.egress_port: exact;
196 }
197 actions = {
198 pop_vlan;
199 nop;
200 }
201 default_action = nop;
202 }
Yi Tsengbe342052017-11-03 10:21:23 -0700203
204 apply {
Yi Tseng20f9e7b2018-05-24 23:27:39 +0800205 egress_vlan.apply();
Yi Tsengbe342052017-11-03 10:21:23 -0700206 }
207}