blob: de46115780de772fd8c7dd8e6d1ada9cda21623f [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;
33 direct_counter(CounterType.packets_and_bytes) multicast_v4_counter;
Yi Tseng3a5731e2018-01-22 11:38:58 -080034 direct_counter(CounterType.packets_and_bytes) acl_counter;
35
Yi Tseng1d842672017-11-28 16:06:52 -080036 action drop() {
37 mark_to_drop();
38 }
Yi Tsengbe342052017-11-03 10:21:23 -070039
40 action set_next_id(next_id_t next_id) {
41 fabric_metadata.next_id = next_id;
42 }
43
44 action pop_mpls_and_next(next_id_t next_id) {
45 hdr.mpls.setInvalid();
Yi Tsengbe342052017-11-03 10:21:23 -070046 fabric_metadata.next_id = next_id;
47 }
48
Yi Tsengbe342052017-11-03 10:21:23 -070049 action duplicate_to_controller() {
Yi Tsengbe342052017-11-03 10:21:23 -070050 standard_metadata.egress_spec = CPU_PORT;
51 }
52
Yi Tsengbe342052017-11-03 10:21:23 -070053 table bridging {
54 key = {
55 hdr.vlan_tag.vlan_id: exact;
56 hdr.ethernet.dst_addr: ternary;
57 }
58
59 actions = {
60 set_next_id;
61 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080062 counters = bridging_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070063 }
64
65 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
76 table unicast_v4 {
77 key = {
78 hdr.ipv4.dst_addr: lpm;
79 }
80
81 actions = {
82 set_next_id;
Yi Tsengbe342052017-11-03 10:21:23 -070083 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080084 counters = unicast_v4_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070085 }
86
87 table multicast_v4 {
88 key = {
89 hdr.vlan_tag.vlan_id: exact;
90 hdr.ipv4.dst_addr: lpm;
91 }
92
93 actions = {
94 set_next_id;
95 }
Yi Tseng3a5731e2018-01-22 11:38:58 -080096 counters = multicast_v4_counter;
Yi Tsengbe342052017-11-03 10:21:23 -070097 }
98
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080099#ifdef WITH_IPV6
100 direct_counter(CounterType.packets_and_bytes) unicast_v6_counter;
101 direct_counter(CounterType.packets_and_bytes) multicast_v6_counter;
102
Yi Tsengbe342052017-11-03 10:21:23 -0700103 table unicast_v6 {
104 key = {
105 hdr.ipv6.dst_addr: lpm;
106 }
107
108 actions = {
109 set_next_id;
Yi Tsengbe342052017-11-03 10:21:23 -0700110 }
Yi Tseng3a5731e2018-01-22 11:38:58 -0800111 counters = unicast_v6_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700112 }
113
114 table multicast_v6 {
115 key = {
116 hdr.vlan_tag.vlan_id: exact;
117 hdr.ipv6.dst_addr: lpm;
118 }
119
120 actions = {
121 set_next_id;
122 }
Yi Tseng3a5731e2018-01-22 11:38:58 -0800123 counters = multicast_v6_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700124 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800125#endif // WITH_IPV6
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
Yi Tsengc6844f52017-12-19 11:58:25 -0800133 fabric_metadata.original_ether_type: ternary; //16
Yi Tseng1d842672017-11-28 16:06:52 -0800134
135 hdr.ethernet.dst_addr: ternary; // 48
136 hdr.ethernet.src_addr: ternary; // 48
Yi Tseng1d842672017-11-28 16:06:52 -0800137 hdr.vlan_tag.vlan_id: ternary; // 12
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 = {
145 set_next_id;
146 duplicate_to_controller;
147 drop;
148 nop;
149 }
150
151 const default_action = nop();
Yi Tseng1d842672017-11-28 16:06:52 -0800152 size = 256;
Yi Tseng3a5731e2018-01-22 11:38:58 -0800153 counters = acl_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700154 }
155
156 apply {
157 if(fabric_metadata.fwd_type == FWD_BRIDGING) bridging.apply();
Yi Tseng1d842672017-11-28 16:06:52 -0800158 else if (fabric_metadata.fwd_type == FWD_MPLS) {
159 mpls.apply();
Yi Tsengbd46d052018-01-22 17:18:16 -0800160
161 // TODO: IPv6
162 hdr.vlan_tag.ether_type = ETHERTYPE_IPV4;
163 fabric_metadata.original_ether_type = ETHERTYPE_IPV4;
Yi Tseng1d842672017-11-28 16:06:52 -0800164 }
Yi Tsengbe342052017-11-03 10:21:23 -0700165 else if (fabric_metadata.fwd_type == FWD_IPV4_UNICAST) unicast_v4.apply();
166 else if (fabric_metadata.fwd_type == FWD_IPV4_MULTICAST) multicast_v4.apply();
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800167#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700168 else if (fabric_metadata.fwd_type == FWD_IPV6_UNICAST) unicast_v6.apply();
169 else if (fabric_metadata.fwd_type == FWD_IPV6_MULTICAST) multicast_v6.apply();
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800170#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700171 acl.apply();
172 }
173}