blob: f0bfd5c3e0ced304045ebb5714a96c822a76ee7d [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 Tseng3a5731e2018-01-22 11:38:58 -080030 direct_counter(CounterType.packets_and_bytes) bridging_counter;
31 direct_counter(CounterType.packets_and_bytes) mpls_counter;
32 direct_counter(CounterType.packets_and_bytes) unicast_v4_counter;
Yi Tseng3a5731e2018-01-22 11:38:58 -080033 direct_counter(CounterType.packets_and_bytes) acl_counter;
34
Yi Tseng1d842672017-11-28 16:06:52 -080035 action drop() {
36 mark_to_drop();
37 }
Yi Tsengbe342052017-11-03 10:21:23 -070038
39 action set_next_id(next_id_t next_id) {
40 fabric_metadata.next_id = next_id;
41 }
42
43 action pop_mpls_and_next(next_id_t next_id) {
44 hdr.mpls.setInvalid();
Yi Tsengbe342052017-11-03 10:21:23 -070045 fabric_metadata.next_id = next_id;
46 }
47
Yi Tsengbe342052017-11-03 10:21:23 -070048 action duplicate_to_controller() {
Yi Tsengbe342052017-11-03 10:21:23 -070049 standard_metadata.egress_spec = CPU_PORT;
50 }
51
Yi Tsengbe342052017-11-03 10:21:23 -070052 table bridging {
53 key = {
54 hdr.vlan_tag.vlan_id: exact;
55 hdr.ethernet.dst_addr: ternary;
56 }
57
58 actions = {
59 set_next_id;
60 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080061 counters = bridging_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070062 }
63
64 table mpls {
65 key = {
66 hdr.mpls.label: exact;
67 }
68
69 actions = {
70 pop_mpls_and_next;
71 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080072 counters = mpls_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070073 }
74
75 table unicast_v4 {
76 key = {
77 hdr.ipv4.dst_addr: lpm;
78 }
79
80 actions = {
81 set_next_id;
Yi Tsengbe342052017-11-03 10:21:23 -070082 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080083 counters = unicast_v4_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070084 }
85
Carmelo Casconea1061402018-02-03 17:39:59 -080086#ifdef WITH_MULTICAST
87 direct_counter(CounterType.packets_and_bytes) multicast_v4_counter;
88
Yi Tsengbe342052017-11-03 10:21:23 -070089 table multicast_v4 {
90 key = {
91 hdr.vlan_tag.vlan_id: exact;
92 hdr.ipv4.dst_addr: lpm;
93 }
94
95 actions = {
96 set_next_id;
97 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080098 counters = multicast_v4_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070099 }
Carmelo Casconea1061402018-02-03 17:39:59 -0800100#endif // WITH_MULTICAST
Yi Tsengbe342052017-11-03 10:21:23 -0700101
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800102#ifdef WITH_IPV6
103 direct_counter(CounterType.packets_and_bytes) unicast_v6_counter;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800104
Yi Tsengbe342052017-11-03 10:21:23 -0700105 table unicast_v6 {
106 key = {
107 hdr.ipv6.dst_addr: lpm;
108 }
109
110 actions = {
111 set_next_id;
Yi Tsengbe342052017-11-03 10:21:23 -0700112 }
Yi Tseng3a5731e2018-01-22 11:38:58 -0800113 counters = unicast_v6_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700114 }
115
Carmelo Casconea1061402018-02-03 17:39:59 -0800116#ifdef WITH_MULTICAST
117 direct_counter(CounterType.packets_and_bytes) multicast_v6_counter;
118
Yi Tsengbe342052017-11-03 10:21:23 -0700119 table multicast_v6 {
120 key = {
121 hdr.vlan_tag.vlan_id: exact;
122 hdr.ipv6.dst_addr: lpm;
123 }
124
125 actions = {
126 set_next_id;
127 }
Yi Tseng3a5731e2018-01-22 11:38:58 -0800128 counters = multicast_v6_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700129 }
Carmelo Casconea1061402018-02-03 17:39:59 -0800130#endif // WITH_MULTICAST
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800131#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700132
133 table acl {
134 key = {
Yi Tseng1d842672017-11-28 16:06:52 -0800135 standard_metadata.ingress_port: ternary; // 9
136 fabric_metadata.ip_proto: ternary; // 8
137 fabric_metadata.l4_src_port: ternary; // 16
138 fabric_metadata.l4_dst_port: ternary; // 16
Yi Tsengc6844f52017-12-19 11:58:25 -0800139 fabric_metadata.original_ether_type: ternary; //16
Yi Tseng1d842672017-11-28 16:06:52 -0800140
141 hdr.ethernet.dst_addr: ternary; // 48
142 hdr.ethernet.src_addr: ternary; // 48
Yi Tseng1d842672017-11-28 16:06:52 -0800143 hdr.vlan_tag.vlan_id: ternary; // 12
Yi Tseng1d842672017-11-28 16:06:52 -0800144 hdr.ipv4.src_addr: ternary; // 32
145 hdr.ipv4.dst_addr: ternary; // 32
Yi Tseng1d842672017-11-28 16:06:52 -0800146 hdr.icmp.icmp_type: ternary; // 8
147 hdr.icmp.icmp_code: ternary; // 8
Yi Tsengbe342052017-11-03 10:21:23 -0700148 }
149
150 actions = {
151 set_next_id;
152 duplicate_to_controller;
153 drop;
154 nop;
155 }
156
157 const default_action = nop();
Yi Tseng1d842672017-11-28 16:06:52 -0800158 size = 256;
Yi Tseng3a5731e2018-01-22 11:38:58 -0800159 counters = acl_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700160 }
161
162 apply {
163 if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply();
Yi Tseng1d842672017-11-28 16:06:52 -0800164 else if (fabric_metadata.fwd_type == FWD_MPLS) {
165 mpls.apply();
Yi Tsengbd46d052018-01-22 17:18:16 -0800166
167 // TODO: IPv6
168 hdr.vlan_tag.ether_type = ETHERTYPE_IPV4;
169 fabric_metadata.original_ether_type = ETHERTYPE_IPV4;
Yi Tseng1d842672017-11-28 16:06:52 -0800170 }
Yi Tsengbe342052017-11-03 10:21:23 -0700171 else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) unicast_v4.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800172#ifdef WITH_MULTICAST
Yi Tsengbe342052017-11-03 10:21:23 -0700173 else if (fabric_metadata.fwd_type == FWD_IPV4_MULTICAST) multicast_v4.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800174#endif // WITH_MULTICAST
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800175#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700176 else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) unicast_v6.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800177#ifdef WITH_MULTICAST
Yi Tsengbe342052017-11-03 10:21:23 -0700178 else if (fabric_metadata.fwd_type == FWD_IPV6_MULTICAST) multicast_v6.apply();
Carmelo Casconea1061402018-02-03 17:39:59 -0800179#endif // WITH_MULTICAST
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800180#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700181 acl.apply();
182 }
183}