blob: 6098ed392272a08cd4441fd2cac10e20f2441fa5 [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 "../define.p4"
21#include "../header.p4"
22#include "../action.p4"
23
24
25control Forwarding (
26 inout parsed_headers_t hdr,
27 inout fabric_metadata_t fabric_metadata,
28 inout standard_metadata_t standard_metadata) {
29
Yi Tseng47eac892018-07-11 02:17:04 +080030 /*
31 * Bridging Table.
32 * Matches destination mac address and VLAN Id and make egress decision.
33 */
Yi Tseng3a5731e2018-01-22 11:38:58 -080034 direct_counter(CounterType.packets_and_bytes) bridging_counter;
Yi Tseng3a5731e2018-01-22 11:38:58 -080035
Yi Tseng47eac892018-07-11 02:17:04 +080036 action set_next_id_bridging(next_id_t next_id) {
Yi Tsengbe342052017-11-03 10:21:23 -070037 fabric_metadata.next_id = next_id;
Yi Tseng47eac892018-07-11 02:17:04 +080038 bridging_counter.count();
Yi Tsengbe342052017-11-03 10:21:23 -070039 }
40
Yi Tsengbe342052017-11-03 10:21:23 -070041 table bridging {
42 key = {
43 hdr.vlan_tag.vlan_id: exact;
44 hdr.ethernet.dst_addr: ternary;
45 }
46
47 actions = {
Yi Tseng47eac892018-07-11 02:17:04 +080048 set_next_id_bridging;
Yi Tsengbe342052017-11-03 10:21:23 -070049 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080050 counters = bridging_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070051 }
52
Yi Tseng47eac892018-07-11 02:17:04 +080053 /*
54 * MPLS Table.
55 * Matches MPLS label and make egress decision.
56 */
57 direct_counter(CounterType.packets_and_bytes) mpls_counter;
58
59 action pop_mpls_and_next(next_id_t next_id) {
60 hdr.mpls.setInvalid();
61 fabric_metadata.next_id = next_id;
62 mpls_counter.count();
63 }
64
Yi Tsengbe342052017-11-03 10:21:23 -070065 table mpls {
66 key = {
67 hdr.mpls.label: exact;
68 }
69
70 actions = {
71 pop_mpls_and_next;
72 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080073 counters = mpls_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070074 }
75
Yi Tseng47eac892018-07-11 02:17:04 +080076 /*
77 * IPv4 Unicast Table.
78 * Matches IPv4 prefix and make egress decision.
79 */
80 direct_counter(CounterType.packets_and_bytes) unicast_v4_counter;
81
82 action set_next_id_unicast_v4(next_id_t next_id) {
83 fabric_metadata.next_id = next_id;
84 unicast_v4_counter.count();
85 }
86
Yi Tsengbe342052017-11-03 10:21:23 -070087 table unicast_v4 {
88 key = {
89 hdr.ipv4.dst_addr: lpm;
90 }
91
92 actions = {
Yi Tseng47eac892018-07-11 02:17:04 +080093 set_next_id_unicast_v4;
Yi Tsengbe342052017-11-03 10:21:23 -070094 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080095 counters = unicast_v4_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070096 }
97
Yi Tseng47eac892018-07-11 02:17:04 +080098 /*
99 * ACL Table.
100 * Make final egress decision based on general metch fields.
101 */
102 direct_counter(CounterType.packets_and_bytes) acl_counter;
Carmelo Casconea1061402018-02-03 17:39:59 -0800103
Yi Tseng47eac892018-07-11 02:17:04 +0800104 action set_next_id_acl(next_id_t next_id) {
105 fabric_metadata.next_id = next_id;
106 acl_counter.count();
Yi Tsengbe342052017-11-03 10:21:23 -0700107 }
108
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200109 // Send immendiatelly to CPU - skip the rest of pipeline.
110 action punt_to_cpu() {
Yi Tseng47eac892018-07-11 02:17:04 +0800111 standard_metadata.egress_spec = CPU_PORT;
112 acl_counter.count();
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200113 exit;
114 }
115
116 action clone_to_cpu() {
117 // FIXME: works only if pkt will be replicated via PRE multicast group.
118 fabric_metadata.clone_to_cpu = _TRUE;
119 acl_counter.count();
Yi Tsengbe342052017-11-03 10:21:23 -0700120 }
Yi Tseng47eac892018-07-11 02:17:04 +0800121
122 action drop() {
123 mark_to_drop();
124 acl_counter.count();
125 }
Yi Tsengbe342052017-11-03 10:21:23 -0700126
127 table acl {
128 key = {
Yi Tseng1d842672017-11-28 16:06:52 -0800129 standard_metadata.ingress_port: ternary; // 9
130 fabric_metadata.ip_proto: ternary; // 8
131 fabric_metadata.l4_src_port: ternary; // 16
132 fabric_metadata.l4_dst_port: ternary; // 16
133
134 hdr.ethernet.dst_addr: ternary; // 48
135 hdr.ethernet.src_addr: ternary; // 48
Yi Tseng1d842672017-11-28 16:06:52 -0800136 hdr.vlan_tag.vlan_id: ternary; // 12
Yi Tseng8235a1a2018-07-24 20:57:28 +0800137 hdr.vlan_tag.ether_type: ternary; //16
Yi Tseng1d842672017-11-28 16:06:52 -0800138 hdr.ipv4.src_addr: ternary; // 32
139 hdr.ipv4.dst_addr: ternary; // 32
Yi Tseng1d842672017-11-28 16:06:52 -0800140 hdr.icmp.icmp_type: ternary; // 8
141 hdr.icmp.icmp_code: ternary; // 8
Yi Tsengbe342052017-11-03 10:21:23 -0700142 }
143
144 actions = {
Yi Tseng47eac892018-07-11 02:17:04 +0800145 set_next_id_acl;
Carmelo Cascone1e8843f2018-07-19 19:01:12 +0200146 punt_to_cpu;
147 clone_to_cpu;
Yi Tsengbe342052017-11-03 10:21:23 -0700148 drop;
Yi Tseng47eac892018-07-11 02:17:04 +0800149 @defaultonly nop;
Yi Tsengbe342052017-11-03 10:21:23 -0700150 }
151
152 const default_action = nop();
Yi Tseng8235a1a2018-07-24 20:57:28 +0800153 size = 128;
Yi Tseng3a5731e2018-01-22 11:38:58 -0800154 counters = acl_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700155 }
156
Yi Tseng47eac892018-07-11 02:17:04 +0800157#ifdef WITH_MULTICAST
158 /*
159 * IPv4 Multicast Table.
160 * Maches multcast IPv4 address and make egress decision.
161 */
162 direct_counter(CounterType.packets_and_bytes) multicast_v4_counter;
163 action set_next_id_multicast_v4(next_id_t next_id) {
164 fabric_metadata.next_id = next_id;
165 multicast_v4_counter.count();
166 }
167
168 table multicast_v4 {
169 key = {
170 hdr.vlan_tag.vlan_id: exact;
171 hdr.ipv4.dst_addr: lpm;
172 }
173
174 actions = {
175 set_next_id_multicast_v4;
176 }
177 counters = multicast_v4_counter;
178 }
179#endif // WITH_MULTICAST
180
181#ifdef WITH_IPV6
182 /*
183 * IPv6 Unicast Table.
184 * Matches IPv6 prefix and make egress decision.
185 */
186 direct_counter(CounterType.packets_and_bytes) unicast_v6_counter;
187
188 action set_next_id_unicast_v6(next_id_t next_id) {
189 fabric_metadata.next_id = next_id;
190 unicast_v6_counter.count();
191 }
192
193 table unicast_v6 {
194 key = {
195 hdr.ipv6.dst_addr: lpm;
196 }
197
198 actions = {
199 set_next_id_unicast_v6;
200 }
201 counters = unicast_v6_counter;
202 }
203
204#ifdef WITH_MULTICAST
205 /*
206 * IPv6 Multicast Table.
207 * Maches multcast IPv6 address and make egress decision.
208 */
209 direct_counter(CounterType.packets_and_bytes) multicast_v6_counter;
210
211 action set_next_id_multicast_v6(next_id_t next_id) {
212 fabric_metadata.next_id = next_id;
213 multicast_v6_counter.count();
214 }
215
216 table multicast_v6 {
217 key = {
218 hdr.vlan_tag.vlan_id: exact;
219 hdr.ipv6.dst_addr: lpm;
220 }
221
222 actions = {
223 set_next_id_multicast_v6;
224 }
225 counters = multicast_v6_counter;
226 }
227#endif // WITH_MULTICAST
228#endif // WITH_IPV6
229
Yi Tsengbe342052017-11-03 10:21:23 -0700230 apply {
231 if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply();
Yi Tseng1d842672017-11-28 16:06:52 -0800232 else if (fabric_metadata.fwd_type == FWD_MPLS) {
233 mpls.apply();
Yi Tsengbd46d052018-01-22 17:18:16 -0800234
235 // TODO: IPv6
236 hdr.vlan_tag.ether_type = ETHERTYPE_IPV4;
Yi Tseng1d842672017-11-28 16:06:52 -0800237 }
Yi Tsengbe342052017-11-03 10:21:23 -0700238 else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) unicast_v4.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800239#ifdef WITH_MULTICAST
Yi Tsengbe342052017-11-03 10:21:23 -0700240 else if (fabric_metadata.fwd_type == FWD_IPV4_MULTICAST) multicast_v4.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800241#endif // WITH_MULTICAST
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800242#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700243 else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) unicast_v6.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800244#ifdef WITH_MULTICAST
Yi Tsengbe342052017-11-03 10:21:23 -0700245 else if (fabric_metadata.fwd_type == FWD_IPV6_MULTICAST) multicast_v6.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800246#endif // WITH_MULTICAST
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800247#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700248 acl.apply();
249 }
250}