blob: a58e88e15687b5790404a24127e875027256ff85 [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;
28
Yi Tsengbe342052017-11-03 10:21:23 -070029 action output(port_num_t port_num) {
30 standard_metadata.egress_spec = port_num;
Yi Tsengbe342052017-11-03 10:21:23 -070031 }
32
33 action set_vlan_output(vlan_id_t new_vlan_id, port_num_t port_num){
34 hdr.vlan_tag.vlan_id = new_vlan_id;
35
36 // don't remove the vlan from egress since we set the vlan to it.
37 fabric_metadata.pop_vlan_at_egress = false;
38 output(port_num);
39 }
40
41 action rewrite_smac(mac_addr_t smac) {
42 hdr.ethernet.src_addr = smac;
43 }
44
45 action rewrite_dmac(mac_addr_t dmac) {
46 hdr.ethernet.dst_addr = dmac;
47 }
48
49 action l3_routing(port_num_t port_num, mac_addr_t smac, mac_addr_t dmac) {
50 rewrite_smac(smac);
51 rewrite_dmac(dmac);
52 output(port_num);
53 }
54
55 action set_mcast_group(group_id_t gid, mac_addr_t smac) {
56 standard_metadata.mcast_grp = gid;
57 rewrite_smac(smac);
58 }
59
Yi Tseng1b154bd2017-11-20 17:48:19 -080060 action push_mpls (mpls_label_t label, bit<3> tc) {
Yi Tseng1d842672017-11-28 16:06:52 -080061 // Suppose that the maximum number of label is one.
Yi Tseng1b154bd2017-11-20 17:48:19 -080062 hdr.mpls.setValid();
63 hdr.ethernet.ether_type = ETHERTYPE_MPLS;
64 hdr.mpls.label = label;
65 hdr.mpls.tc = tc;
Yi Tseng1d842672017-11-28 16:06:52 -080066 hdr.mpls.bos = 1w1; // BOS = TRUE
Yi Tseng1b154bd2017-11-20 17:48:19 -080067 hdr.mpls.ttl = DEFAULT_MPLS_TTL;
68 }
69
70 action mpls_routing_v4 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
71 mpls_label_t label) {
72 l3_routing(port_num, smac, dmac);
Yi Tseng1d842672017-11-28 16:06:52 -080073
74 // TODO: set tc according to diffserv from ipv4
75 push_mpls(label, 3w0);
Yi Tseng1b154bd2017-11-20 17:48:19 -080076 }
77
78 action mpls_routing_v6 (port_num_t port_num, mac_addr_t smac, mac_addr_t dmac,
79 mpls_label_t label) {
80 l3_routing(port_num, smac, dmac);
Yi Tseng1d842672017-11-28 16:06:52 -080081
82 // TODO: set tc according to traffic_class from ipv4
83 push_mpls(label, 3w0);
Yi Tseng1b154bd2017-11-20 17:48:19 -080084 }
85
Yi Tsengbe342052017-11-03 10:21:23 -070086 table simple {
87 key = {
88 fabric_metadata.next_id: exact;
89 }
90
91 actions = {
92 output;
93 set_vlan_output;
94 l3_routing;
95 }
Yi Tsengbe342052017-11-03 10:21:23 -070096 }
97
98 table hashed {
99 key = {
100 fabric_metadata.next_id: exact;
Yi Tseng1d842672017-11-28 16:06:52 -0800101 hdr.ethernet.dst_addr: selector;
102 hdr.ethernet.src_addr: selector;
103 fabric_metadata.ip_proto: selector;
Yi Tsengbe342052017-11-03 10:21:23 -0700104 fabric_metadata.l4_src_port: selector;
105 fabric_metadata.l4_dst_port: selector;
106 }
107
108 actions = {
109 l3_routing;
Yi Tseng1b154bd2017-11-20 17:48:19 -0800110 mpls_routing_v4;
111 mpls_routing_v6;
Yi Tsengbe342052017-11-03 10:21:23 -0700112 }
113
114 implementation = ecmp_selector;
Yi Tsengbe342052017-11-03 10:21:23 -0700115 }
116
117 /*
118 * Work in progress
119 */
120 table broadcast {
121 key = {
122 fabric_metadata.next_id: exact;
123 }
124 actions = {
125 set_mcast_group;
126 }
Yi Tsengbe342052017-11-03 10:21:23 -0700127 }
128
129 apply {
Yi Tseng1d842672017-11-28 16:06:52 -0800130 if (simple.apply().hit) {
131 if (!hdr.mpls.isValid()) {
132 if(hdr.ipv4.isValid()) {
133 hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
134 }
135 else if (hdr.ipv6.isValid()) {
136 hdr.ipv6.hop_limit = hdr.ipv6.hop_limit - 1;
137 }
138 }
139 }
Yi Tsengf55eaa82017-11-29 15:51:28 -0800140 hashed.apply();
141 broadcast.apply();
Yi Tsengbe342052017-11-03 10:21:23 -0700142 }
143}
144
145control EgressNextControl (
146 inout parsed_headers_t hdr,
147 inout fabric_metadata_t fabric_metadata,
148 inout standard_metadata_t standard_metadata){
149
150 apply {
151 // pop internal vlan if the meta is set
152 if (fabric_metadata.pop_vlan_at_egress) {
Yi Tseng1d842672017-11-28 16:06:52 -0800153 if (hdr.mpls.isValid()) {
154 hdr.ethernet.ether_type = ETHERTYPE_MPLS;
155 } else {
156 hdr.ethernet.ether_type = fabric_metadata.original_ether_type;
157 }
Yi Tsengbe342052017-11-03 10:21:23 -0700158 hdr.vlan_tag.setInvalid();
159 }
160 }
161}