blob: c76fc65c4530a770f45b59202705df5a68e8e02a [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 Tseng3a5731e2018-01-22 11:38:58 -080028 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;
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
36 action set_vlan_output(vlan_id_t new_vlan_id, port_num_t port_num){
37 hdr.vlan_tag.vlan_id = new_vlan_id;
38
39 // don't remove the vlan from egress since we set the vlan to it.
40 fabric_metadata.pop_vlan_at_egress = false;
41 output(port_num);
42 }
43
44 action rewrite_smac(mac_addr_t smac) {
45 hdr.ethernet.src_addr = smac;
46 }
47
48 action rewrite_dmac(mac_addr_t dmac) {
49 hdr.ethernet.dst_addr = dmac;
50 }
51
52 action l3_routing(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) {
53 rewrite_smac(smac);
54 rewrite_dmac(dmac);
55 output(port_num);
56 }
57
58 action set_mcast_group(group_id_t gid, mac_addr_t smac) {
59 standard_metadata.mcast_grp = gid;
60 rewrite_smac(smac);
61 }
62
Yi Tseng1b154bd2017-11-20 17:48:19 -080063 action push_mpls (mpls_label_t label, bit<3> tc) {
Yi Tseng1d842672017-11-28 16:06:52 -080064 // Suppose that the maximum number of label is one.
Yi Tseng1b154bd2017-11-20 17:48:19 -080065 hdr.mpls.setValid();
Yi Tsengbd46d052018-01-22 17:18:16 -080066 hdr.vlan_tag.ether_type = ETHERTYPE_MPLS;
Yi Tseng1b154bd2017-11-20 17:48:19 -080067 hdr.mpls.label = label;
68 hdr.mpls.tc = tc;
Yi Tseng1d842672017-11-28 16:06:52 -080069 hdr.mpls.bos = 1w1; // BOS = TRUE
Yi Tseng1b154bd2017-11-20 17:48:19 -080070 hdr.mpls.ttl = DEFAULT_MPLS_TTL;
71 }
72
73 action mpls_routing_v4 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
74 mpls_label_t label) {
75 l3_routing(port_num, smac, dmac);
Yi Tseng1d842672017-11-28 16:06:52 -080076
77 // TODO: set tc according to diffserv from ipv4
78 push_mpls(label, 3w0);
Yi Tseng1b154bd2017-11-20 17:48:19 -080079 }
80
81 action mpls_routing_v6 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
82 mpls_label_t label) {
83 l3_routing(port_num, smac, dmac);
Yi Tseng1d842672017-11-28 16:06:52 -080084
85 // TODO: set tc according to traffic_class from ipv4
86 push_mpls(label, 3w0);
Yi Tseng1b154bd2017-11-20 17:48:19 -080087 }
88
Yi Tsengbe342052017-11-03 10:21:23 -070089 table simple {
90 key = {
91 fabric_metadata.next_id: exact;
92 }
93
94 actions = {
95 output;
96 set_vlan_output;
97 l3_routing;
Yi Tseng3a5731e2018-01-22 11:38:58 -080098 mpls_routing_v4;
Yi Tsengbe342052017-11-03 10:21:23 -070099 }
Yi Tseng3a5731e2018-01-22 11:38:58 -0800100 counters = simple_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700101 }
102
103 table hashed {
104 key = {
105 fabric_metadata.next_id: exact;
Yi Tseng3d3956d2018-01-31 17:28:05 -0800106 hdr.ipv4.dst_addr: selector;
107 hdr.ipv4.src_addr: selector;
Yi Tseng1d842672017-11-28 16:06:52 -0800108 fabric_metadata.ip_proto: selector;
Yi Tsengbe342052017-11-03 10:21:23 -0700109 fabric_metadata.l4_src_port: selector;
110 fabric_metadata.l4_dst_port: selector;
111 }
112
113 actions = {
114 l3_routing;
Yi Tseng1b154bd2017-11-20 17:48:19 -0800115 mpls_routing_v4;
116 mpls_routing_v6;
Yi Tsengbe342052017-11-03 10:21:23 -0700117 }
118
119 implementation = ecmp_selector;
Yi Tseng3a5731e2018-01-22 11:38:58 -0800120 counters = hashed_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700121 }
122
123 /*
124 * Work in progress
125 */
126 table broadcast {
127 key = {
128 fabric_metadata.next_id: exact;
129 }
130 actions = {
131 set_mcast_group;
132 }
Yi Tseng3a5731e2018-01-22 11:38:58 -0800133 counters = broadcast_counter;
Yi Tsengbe342052017-11-03 10:21:23 -0700134 }
135
136 apply {
Yi Tseng1d842672017-11-28 16:06:52 -0800137 if (simple.apply().hit) {
138 if (!hdr.mpls.isValid()) {
139 if(hdr.ipv4.isValid()) {
140 hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
141 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800142#ifdef WITH_IPV6
Yi Tseng1d842672017-11-28 16:06:52 -0800143 else if (hdr.ipv6.isValid()) {
144 hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1;
145 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800146#endif // WITH_IPV6
Yi Tseng1d842672017-11-28 16:06:52 -0800147 }
148 }
Yi Tsengf55eaa82017-11-29 15:51:28 -0800149 hashed.apply();
150 broadcast.apply();
Yi Tsengbe342052017-11-03 10:21:23 -0700151 }
152}
153
154control EgressNextControl (
155 inout parsed_headers_t hdr,
156 inout fabric_metadata_t fabric_metadata,
157 inout standard_metadata_t standard_metadata){
158
159 apply {
160 // pop internal vlan if the meta is set
161 if (fabric_metadata.pop_vlan_at_egress) {
Yi Tsengbd46d052018-01-22 17:18:16 -0800162 hdr.ethernet.ether_type = hdr.vlan_tag.ether_type;
Yi Tsengbe342052017-11-03 10:21:23 -0700163 hdr.vlan_tag.setInvalid();
164 }
165 }
166}