blob: c0c6639ca2d7b74902c2bdaf85d9ceb49ac8f5c0 [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
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 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
Yi Tseng47f82dc2017-03-05 22:48:39 -080019import com.google.common.collect.Sets;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070021import org.onlab.packet.Ethernet;
Julia Ferguson65428c32017-08-10 18:15:24 +000022import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080023import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070024import org.onlab.packet.VlanId;
25import org.onlab.util.KryoNamespace;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070028import org.onosproject.driver.extensions.Ofdpa3MplsType;
29import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080030import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
31import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070032import org.onosproject.net.DeviceId;
33import org.onosproject.net.Port;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.behaviour.NextGroup;
36import org.onosproject.net.behaviour.Pipeliner;
37import org.onosproject.net.behaviour.PipelinerContext;
38import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.driver.AbstractHandlerBehaviour;
40import org.onosproject.net.flow.DefaultFlowRule;
41import org.onosproject.net.flow.DefaultTrafficSelector;
42import org.onosproject.net.flow.DefaultTrafficTreatment;
43import org.onosproject.net.flow.FlowRule;
44import org.onosproject.net.flow.FlowRuleOperations;
45import org.onosproject.net.flow.FlowRuleOperationsContext;
46import org.onosproject.net.flow.FlowRuleService;
47import org.onosproject.net.flow.TrafficSelector;
48import org.onosproject.net.flow.TrafficTreatment;
49import org.onosproject.net.flow.criteria.Criteria;
50import org.onosproject.net.flow.criteria.Criterion;
51import org.onosproject.net.flow.criteria.EthCriterion;
52import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080053import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070054import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080055import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
56import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080057import org.onosproject.net.flow.criteria.MplsBosCriterion;
58import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070059import org.onosproject.net.flow.criteria.PortCriterion;
60import org.onosproject.net.flow.criteria.VlanIdCriterion;
61import org.onosproject.net.flow.instructions.Instruction;
62import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
63import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080064import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070065import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080066import org.onosproject.net.flow.instructions.L3ModificationInstruction;
67import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flowobjective.FilteringObjective;
69import org.onosproject.net.flowobjective.FlowObjectiveStore;
70import org.onosproject.net.flowobjective.ForwardingObjective;
71import org.onosproject.net.flowobjective.NextObjective;
72import org.onosproject.net.flowobjective.Objective;
73import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070074import org.onosproject.net.group.DefaultGroupKey;
75import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.store.serializers.KryoNamespaces;
79import org.slf4j.Logger;
80
Pier Ventree0ae7a32016-11-23 09:57:42 -080081import java.util.ArrayDeque;
82import java.util.ArrayList;
83import java.util.Collection;
84import java.util.Collections;
85import java.util.Deque;
86import java.util.List;
87import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080088import java.util.concurrent.ScheduledExecutorService;
89import java.util.concurrent.TimeUnit;
90
91import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080092import static org.onlab.packet.MacAddress.BROADCAST;
93import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080094import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070095import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Pier Ventree0ae7a32016-11-23 09:57:42 -080096import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -070097import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
98import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED;
Pier Ventree0ae7a32016-11-23 09:57:42 -080099
Saurav Das822c4e22015-10-23 10:51:11 -0700100/**
101 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700102 */
Charles Chan361154b2016-03-24 10:23:39 -0700103public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800104
Saurav Das822c4e22015-10-23 10:51:11 -0700105 protected static final int PORT_TABLE = 0;
106 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800107 protected static final int VLAN_1_TABLE = 11;
108 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
109 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700110 protected static final int TMAC_TABLE = 20;
111 protected static final int UNICAST_ROUTING_TABLE = 30;
112 protected static final int MULTICAST_ROUTING_TABLE = 40;
113 protected static final int MPLS_TABLE_0 = 23;
114 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700115 protected static final int MPLS_L3_TYPE_TABLE = 27;
116 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700117 protected static final int BRIDGING_TABLE = 50;
118 protected static final int ACL_TABLE = 60;
119 protected static final int MAC_LEARNING_TABLE = 254;
120 protected static final long OFPP_MAX = 0xffffff00L;
121
Saurav Das52025962016-01-28 22:30:01 -0800122 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800123 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int LOWEST_PRIORITY = 0x0;
125
Pier Ventre42287df2016-11-09 14:17:26 -0800126 protected static final int MPLS_L2_PORT_PRIORITY = 2;
127
128 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
129 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
130
Pier Ventre70d53ba2016-11-17 22:26:29 -0800131 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
132
133 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
134 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
135
Saurav Das822c4e22015-10-23 10:51:11 -0700136 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700137 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700138 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700139 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800140 protected GroupService groupService;
141 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700142 protected DeviceId deviceId;
143 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700144 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800145 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700146 .register(KryoNamespaces.API)
147 .register(GroupKey.class)
148 .register(DefaultGroupKey.class)
149 .register(OfdpaNextGroup.class)
150 .register(ArrayDeque.class)
151 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700152
Charles Chan425854b2016-04-11 15:32:12 -0700153 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700154
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700155 // flows installations to be retried
156 protected ScheduledExecutorService executorService
157 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
158 protected static final int MAX_RETRY_ATTEMPTS = 10;
159 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800160
Saurav Das822c4e22015-10-23 10:51:11 -0700161 @Override
162 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700163 this.deviceId = deviceId;
164
Charles Chan425854b2016-04-11 15:32:12 -0700165 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700166 coreService = serviceDirectory.get(CoreService.class);
167 flowRuleService = serviceDirectory.get(FlowRuleService.class);
168 groupService = serviceDirectory.get(GroupService.class);
169 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700170 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700171
Charles Chan40132b32017-01-22 00:19:37 -0800172 initDriverId();
173 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700174
Saurav Das822c4e22015-10-23 10:51:11 -0700175 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700176 }
177
Charles Chan40132b32017-01-22 00:19:37 -0800178 protected void initDriverId() {
179 driverId = coreService.registerApplication(
180 "org.onosproject.driver.Ofdpa2Pipeline");
181 }
182
183 protected void initGroupHander(PipelinerContext context) {
184 groupHandler = new Ofdpa2GroupHandler();
185 groupHandler.init(deviceId, context);
186 }
187
Saurav Das822c4e22015-10-23 10:51:11 -0700188 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800189 // OF-DPA does not require initializing the pipeline as it puts default
190 // rules automatically in the hardware. However emulation of OFDPA in
191 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700192 }
193
Charles Chand1172632017-03-15 17:33:09 -0700194 /**
195 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
196 *
197 * @return true to use the extensions
198 */
199 protected boolean requireVlanExtensions() {
200 return true;
201 }
202
Saurav Das86d13e82017-04-28 17:03:48 -0700203 /**
204 * Determines whether in-port should be matched on in TMAC table rules.
205 *
206 * @return true if match on in-port should be programmed
207 */
208 protected boolean matchInPortTmacTable() {
209 return true;
210 }
211
Saurav Das822c4e22015-10-23 10:51:11 -0700212 //////////////////////////////////////
213 // Flow Objectives
214 //////////////////////////////////////
215
216 @Override
217 public void filter(FilteringObjective filteringObjective) {
218 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
219 processFilter(filteringObjective,
220 filteringObjective.op() == Objective.Operation.ADD,
221 filteringObjective.appId());
222 } else {
223 // Note that packets that don't match the PERMIT filter are
224 // automatically denied. The DENY filter is used to deny packets
225 // that are otherwise permitted by the PERMIT filter.
226 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530227 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700228 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
229 }
230 }
231
232 @Override
233 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700234 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700235 if (rules == null || rules.isEmpty()) {
236 // Assumes fail message has already been generated to the objective
237 // context. Returning here prevents spurious pass message to be
238 // generated by FlowRule service for empty flowOps.
239 return;
240 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700241 sendForward(fwd, rules);
242 }
243
244 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
245 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700246 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700247 case ADD:
248 rules.stream()
249 .filter(Objects::nonNull)
250 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800251 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700252 break;
253 case REMOVE:
254 rules.stream()
255 .filter(Objects::nonNull)
256 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800257 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700258 break;
259 default:
260 fail(fwd, ObjectiveError.UNKNOWN);
261 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700262 }
263
Saurav Das822c4e22015-10-23 10:51:11 -0700264 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
265 @Override
266 public void onSuccess(FlowRuleOperations ops) {
267 pass(fwd);
268 }
269
270 @Override
271 public void onError(FlowRuleOperations ops) {
272 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
273 }
274 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700275 }
276
277 @Override
278 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800279 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
280 switch (nextObjective.op()) {
281 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800282 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800283 log.warn("Cannot add next {} that already exists in device {}",
284 nextObjective.id(), deviceId);
285 return;
286 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700287 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800288 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700289 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800290 break;
291 case ADD_TO_EXISTING:
292 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700293 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800294 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700295 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800296 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800297 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800298 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
299 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800300
301 // by design multiple pending bucket is allowed for the group
302 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
303 if (pendBkts == null) {
304 pendBkts = Sets.newHashSet();
305 }
306 pendBkts.add(nextObjective);
307 return pendBkts;
308 });
Saurav Das4f980082015-11-05 13:39:15 -0800309 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800310 break;
311 case REMOVE:
312 if (nextGroup == null) {
313 log.warn("Cannot remove next {} that does not exist in device {}",
314 nextObjective.id(), deviceId);
315 return;
316 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700317 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800318 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700319 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800320 break;
321 case REMOVE_FROM_EXISTING:
322 if (nextGroup == null) {
323 log.warn("Cannot remove from next {} that does not exist in device {}",
324 nextObjective.id(), deviceId);
325 return;
326 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700327 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800328 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700329 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800330 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700331 case VERIFY:
332 if (nextGroup == null) {
333 log.warn("Cannot verify next {} that does not exist in device {}",
334 nextObjective.id(), deviceId);
335 return;
336 }
337 log.debug("Processing NextObjective id {} in dev {} - verify",
338 nextObjective.id(), deviceId);
339 groupHandler.verifyGroup(nextObjective, nextGroup);
340 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800341 default:
Saurav Das4f980082015-11-05 13:39:15 -0800342 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700343 }
344 }
345
346 //////////////////////////////////////
347 // Flow handling
348 //////////////////////////////////////
349
350 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700351 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
352 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700353 *
354 * @param filt the filtering objective
355 * @param install indicates whether to add or remove the objective
356 * @param applicationId the application that sent this objective
357 */
Saurav Das52025962016-01-28 22:30:01 -0800358 protected void processFilter(FilteringObjective filt,
359 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700360 // This driver only processes filtering criteria defined with switch
361 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530362 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700363 EthCriterion ethCriterion = null;
364 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700365 if (!filt.key().equals(Criteria.dummy()) &&
366 filt.key().type() == Criterion.Type.IN_PORT) {
367 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700368 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530369 if (portCriterion == null) {
370 log.debug("No IN_PORT defined in filtering objective from app: {}",
371 applicationId);
372 } else {
373 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
374 portCriterion.port());
375 }
Saurav Das822c4e22015-10-23 10:51:11 -0700376 // convert filtering conditions for switch-intfs into flowrules
377 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
378 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700379 switch (criterion.type()) {
380 case ETH_DST:
381 case ETH_DST_MASKED:
382 ethCriterion = (EthCriterion) criterion;
383 break;
384 case VLAN_VID:
385 vidCriterion = (VlanIdCriterion) criterion;
386 break;
387 default:
388 log.warn("Unsupported filter {}", criterion);
389 fail(filt, ObjectiveError.UNSUPPORTED);
390 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700391 }
392 }
393
Saurav Das0e99e2b2015-10-28 12:39:42 -0700394 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800395 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700396 // Use the VLAN in metadata whenever a metadata is provided
397 if (filt.meta() != null) {
398 assignedVlan = readVlanFromTreatment(filt.meta());
399 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
400 } else if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800401 assignedVlan = vidCriterion.vlanId();
Charles Chand55e84d2016-03-30 17:54:24 -0700402 }
Charles Chane849c192016-01-11 18:28:54 -0800403
Charles Chand55e84d2016-03-30 17:54:24 -0700404 if (assignedVlan == null) {
405 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800406 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700407 log.debug("VLAN ID in criterion={}, metadata={}",
408 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
409 fail(filt, ObjectiveError.BADPARAMS);
410 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700411 }
412 }
413
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800414 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700415 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700416 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700417 } else {
418 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700419 vidCriterion, assignedVlan,
420 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700421 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800422 (install) ? "adding" : "removing", tmacRule, deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700423 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
424 }
425 }
426
Charles Chan985b12e2016-05-11 19:47:22 -0700427 if (vidCriterion == null) {
428 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530429 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700430 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800431 /*
432 * NOTE: Separate vlan filtering rules and assignment rules
433 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800434 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800435 */
436 List<FlowRule> allRules = processVlanIdFilter(
437 portCriterion, vidCriterion, assignedVlan, applicationId);
438 List<FlowRule> filteringRules = new ArrayList<>();
439 List<FlowRule> assignmentRules = new ArrayList<>();
440
441 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700442 VlanId vlanId;
443 if (requireVlanExtensions()) {
444 ExtensionCriterion extCriterion =
445 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
446 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
447 } else {
448 VlanIdCriterion vlanIdCriterion =
449 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
450 vlanId = vlanIdCriterion.vlanId();
451 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800452 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800453 filteringRules.add(flowRule);
454 } else {
455 assignmentRules.add(flowRule);
456 }
457 });
458
459 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700460 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800461 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800462 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
463 }
464
465 ops.newStage();
466
467 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700468 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800469 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800470 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700471 }
472 }
473
Saurav Das822c4e22015-10-23 10:51:11 -0700474 // apply filtering flow rules
475 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
476 @Override
477 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800478 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700479 ops.stages().get(0).size(), deviceId);
480 pass(filt);
481 }
482
483 @Override
484 public void onError(FlowRuleOperations ops) {
485 log.info("Failed to apply all filtering rules in dev {}", deviceId);
486 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
487 }
488 }));
489
490 }
491
492 /**
Charles Chand1172632017-03-15 17:33:09 -0700493 * Internal implementation of processVlanIdFilter.
494 * <p>
495 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
496 * Since it is non-OF spec, we need an extension treatment for that.
497 * The useVlanExtension must be set to false for OFDPA i12.
498 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700499 *
Charles Chanf9e98652016-09-07 16:54:23 -0700500 * @param portCriterion port on device for which this filter is programmed
501 * @param vidCriterion vlan assigned to port, or NONE for untagged
502 * @param assignedVlan assigned vlan-id for untagged packets
503 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700504 * @return list of FlowRule for port-vlan filters
505 */
506 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700507 VlanIdCriterion vidCriterion,
508 VlanId assignedVlan,
509 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800510 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700511 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
512 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800513 TrafficSelector.Builder preSelector = null;
514 TrafficTreatment.Builder preTreatment = null;
515
Saurav Das4f980082015-11-05 13:39:15 -0800516 treatment.transition(TMAC_TABLE);
517
Saurav Das822c4e22015-10-23 10:51:11 -0700518 if (vidCriterion.vlanId() == VlanId.NONE) {
519 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700520 preSelector = DefaultTrafficSelector.builder();
521 if (requireVlanExtensions()) {
522 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
523 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700524 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
525 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700526
527 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
528 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700529 } else {
Charles Chand1172632017-03-15 17:33:09 -0700530 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700531 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700532
533 preSelector.matchVlanId(assignedVlan);
534 }
535 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
536 } else {
537 if (requireVlanExtensions()) {
538 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
539 selector.extension(ofdpaMatchVlanVid, deviceId);
540 } else {
541 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700542 }
Charles Chan14967c22015-12-07 11:11:50 -0800543
Charles Chand55e84d2016-03-30 17:54:24 -0700544 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700545 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800546 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
547 treatment.extension(ofdpaSetVlanVid, deviceId);
548 } else {
549 treatment.setVlanId(assignedVlan);
550 }
Charles Chand55e84d2016-03-30 17:54:24 -0700551 }
Saurav Das822c4e22015-10-23 10:51:11 -0700552 }
Saurav Das822c4e22015-10-23 10:51:11 -0700553
554 // ofdpa cannot match on ALL portnumber, so we need to use separate
555 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800556 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530557 if (portCriterion != null) {
558 if (PortNumber.ALL.equals(portCriterion.port())) {
559 for (Port port : deviceService.getPorts(deviceId)) {
560 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
561 portnums.add(port.number());
562 }
Saurav Das822c4e22015-10-23 10:51:11 -0700563 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530564 } else {
565 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700566 }
567 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530568 log.warn("Filtering Objective missing Port Criterion . " +
569 "VLAN Table cannot be programmed for {}",
570 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700571 }
Saurav Das4f980082015-11-05 13:39:15 -0800572
Saurav Das822c4e22015-10-23 10:51:11 -0700573 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800574 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700575 selector.matchInPort(pnum);
576 FlowRule rule = DefaultFlowRule.builder()
577 .forDevice(deviceId)
578 .withSelector(selector.build())
579 .withTreatment(treatment.build())
580 .withPriority(DEFAULT_PRIORITY)
581 .fromApp(applicationId)
582 .makePermanent()
583 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800584
585 if (preSelector != null) {
586 preSelector.matchInPort(pnum);
587 FlowRule preRule = DefaultFlowRule.builder()
588 .forDevice(deviceId)
589 .withSelector(preSelector.build())
590 .withTreatment(preTreatment.build())
591 .withPriority(DEFAULT_PRIORITY)
592 .fromApp(applicationId)
593 .makePermanent()
594 .forTable(VLAN_TABLE).build();
595 rules.add(preRule);
596 }
597
Saurav Das822c4e22015-10-23 10:51:11 -0700598 rules.add(rule);
599 }
600 return rules;
601 }
602
603 /**
604 * Allows routed packets with correct destination MAC to be directed
605 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700606 *
607 * @param portCriterion port on device for which this filter is programmed
608 * @param ethCriterion dstMac of device for which is filter is programmed
609 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700610 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700611 * @param applicationId for application programming this filter
612 * @return list of FlowRule for port-vlan filters
613
614 */
615 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
616 EthCriterion ethCriterion,
617 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700618 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700619 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800620 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530621 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800622 return processEthDstOnlyFilter(ethCriterion, applicationId);
623 }
624
Charles Chan5b9df8d2016-03-28 22:21:40 -0700625 // Multicast MAC
626 if (ethCriterion.mask() != null) {
627 return processMcastEthDstFilter(ethCriterion, applicationId);
628 }
629
Saurav Das822c4e22015-10-23 10:51:11 -0700630 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530631 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700632 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700633 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530634 List<FlowRule> rules = new ArrayList<>();
635 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
636 if (vidCriterion != null && requireVlanExtensions()) {
637 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
638 }
Saurav Das822c4e22015-10-23 10:51:11 -0700639 // ofdpa cannot match on ALL portnumber, so we need to use separate
640 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700641 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530642 if (portCriterion != null) {
643 if (PortNumber.ALL.equals(portCriterion.port())) {
644 for (Port port : deviceService.getPorts(deviceId)) {
645 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
646 portnums.add(port.number());
647 }
Saurav Das822c4e22015-10-23 10:51:11 -0700648 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530649 } else {
650 portnums.add(portCriterion.port());
651 }
652 for (PortNumber pnum : portnums) {
653 rules.add(buildTmacRuleForIpv4(ethCriterion,
654 vidCriterion,
655 ofdpaMatchVlanVid,
656 applicationId,
657 pnum));
658 rules.add(buildTmacRuleForMpls(ethCriterion,
659 vidCriterion,
660 ofdpaMatchVlanVid,
661 applicationId,
662 pnum));
663 rules.add(buildTmacRuleForIpv6(ethCriterion,
664 vidCriterion,
665 ofdpaMatchVlanVid,
666 applicationId,
667 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700668 }
669 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530670 rules.add(buildTmacRuleForIpv4(ethCriterion,
671 vidCriterion,
672 ofdpaMatchVlanVid,
673 applicationId,
674 null));
675 rules.add(buildTmacRuleForMpls(ethCriterion,
676 vidCriterion,
677 ofdpaMatchVlanVid,
678 applicationId,
679 null));
680 rules.add(buildTmacRuleForIpv6(ethCriterion,
681 vidCriterion,
682 ofdpaMatchVlanVid,
683 applicationId,
684 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700685 }
686 return rules;
687 }
688
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530689 /**
690 * Builds TMAC rules for IPv4 packets.
691 *
692 * @param ethCriterion
693 * @param vidCriterion
694 * @param ofdpaMatchVlanVid
695 * @param applicationId
696 * @param pnum
697 * @return TMAC rule for IPV4 packets
698 */
699 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
700 VlanIdCriterion vidCriterion,
701 OfdpaMatchVlanVid ofdpaMatchVlanVid,
702 ApplicationId applicationId,
703 PortNumber pnum) {
704 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
705 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
706 if (pnum != null) {
707 if (matchInPortTmacTable()) {
708 selector.matchInPort(pnum);
709 } else {
710 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
711 "ignoring the IN_PORT criteria");
712 }
713 }
714 if (vidCriterion != null) {
715 if (requireVlanExtensions()) {
716 selector.extension(ofdpaMatchVlanVid, deviceId);
717 } else {
718 selector.matchVlanId(vidCriterion.vlanId());
719 }
720 }
721 selector.matchEthType(Ethernet.TYPE_IPV4);
722 selector.matchEthDst(ethCriterion.mac());
723 treatment.transition(UNICAST_ROUTING_TABLE);
724 return DefaultFlowRule.builder()
725 .forDevice(deviceId)
726 .withSelector(selector.build())
727 .withTreatment(treatment.build())
728 .withPriority(DEFAULT_PRIORITY)
729 .fromApp(applicationId)
730 .makePermanent()
731 .forTable(TMAC_TABLE).build();
732 }
733
734 /**
735 * Builds TMAC rule for MPLS packets.
736 *
737 * @param ethCriterion
738 * @param vidCriterion
739 * @param ofdpaMatchVlanVid
740 * @param applicationId
741 * @param pnum
742 * @return TMAC rule for MPLS packets
743 */
744 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
745 VlanIdCriterion vidCriterion,
746 OfdpaMatchVlanVid ofdpaMatchVlanVid,
747 ApplicationId applicationId,
748 PortNumber pnum) {
749 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
750 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
751 if (pnum != null) {
752 if (matchInPortTmacTable()) {
753 selector.matchInPort(pnum);
754 } else {
755 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
756 "ignoring the IN_PORT criteria");
757 }
758 }
759 if (vidCriterion != null) {
760 if (requireVlanExtensions()) {
761 selector.extension(ofdpaMatchVlanVid, deviceId);
762 } else {
763 selector.matchVlanId(vidCriterion.vlanId());
764 }
765 }
766 selector.matchEthType(Ethernet.MPLS_UNICAST);
767 selector.matchEthDst(ethCriterion.mac());
768 treatment.transition(MPLS_TABLE_0);
769 return DefaultFlowRule.builder()
770 .forDevice(deviceId)
771 .withSelector(selector.build())
772 .withTreatment(treatment.build())
773 .withPriority(DEFAULT_PRIORITY)
774 .fromApp(applicationId)
775 .makePermanent()
776 .forTable(TMAC_TABLE).build();
777 }
778
779 /**
780 * Builds TMAC rules for IPv6 packets.
781 *
782 * @param ethCriterion
783 * @param vidCriterion
784 * @param ofdpaMatchVlanVid
785 * @param applicationId
786 * @param pnum
787 * @return TMAC rule for IPV6 packets
788 */
789 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
790 VlanIdCriterion vidCriterion,
791 OfdpaMatchVlanVid ofdpaMatchVlanVid,
792 ApplicationId applicationId,
793 PortNumber pnum) {
794 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
795 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
796 if (pnum != null) {
797 if (matchInPortTmacTable()) {
798 selector.matchInPort(pnum);
799 } else {
800 log.info("Pipeline does not support IN_PORT matching in TMAC table, " +
801 "ignoring the IN_PORT criteria");
802 }
803 }
804 if (vidCriterion != null) {
805 if (requireVlanExtensions()) {
806 selector.extension(ofdpaMatchVlanVid, deviceId);
807 } else {
808 selector.matchVlanId(vidCriterion.vlanId());
809 }
810 }
811 selector.matchEthType(Ethernet.TYPE_IPV6);
812 selector.matchEthDst(ethCriterion.mac());
813 treatment.transition(UNICAST_ROUTING_TABLE);
814 return DefaultFlowRule.builder()
815 .forDevice(deviceId)
816 .withSelector(selector.build())
817 .withTreatment(treatment.build())
818 .withPriority(DEFAULT_PRIORITY)
819 .fromApp(applicationId)
820 .makePermanent()
821 .forTable(TMAC_TABLE).build();
822 }
823
Charles Chan5270ed02016-01-30 23:22:37 -0800824 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700825 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800826 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
827
Charles Chan5270ed02016-01-30 23:22:37 -0800828 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
829 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
830 selector.matchEthType(Ethernet.TYPE_IPV4);
831 selector.matchEthDst(ethCriterion.mac());
832 treatment.transition(UNICAST_ROUTING_TABLE);
833 FlowRule rule = DefaultFlowRule.builder()
834 .forDevice(deviceId)
835 .withSelector(selector.build())
836 .withTreatment(treatment.build())
837 .withPriority(DEFAULT_PRIORITY)
838 .fromApp(applicationId)
839 .makePermanent()
840 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800841 builder.add(rule);
842
843 selector = DefaultTrafficSelector.builder();
844 treatment = DefaultTrafficTreatment.builder();
845 selector.matchEthType(Ethernet.TYPE_IPV6);
846 selector.matchEthDst(ethCriterion.mac());
847 treatment.transition(UNICAST_ROUTING_TABLE);
848 rule = DefaultFlowRule.builder()
849 .forDevice(deviceId)
850 .withSelector(selector.build())
851 .withTreatment(treatment.build())
852 .withPriority(DEFAULT_PRIORITY)
853 .fromApp(applicationId)
854 .makePermanent()
855 .forTable(TMAC_TABLE).build();
856 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800857 }
858
Charles Chan5b9df8d2016-03-28 22:21:40 -0700859 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700860 ApplicationId applicationId) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000861 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700862 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
863 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
864 selector.matchEthType(Ethernet.TYPE_IPV4);
865 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
866 treatment.transition(MULTICAST_ROUTING_TABLE);
867 FlowRule rule = DefaultFlowRule.builder()
868 .forDevice(deviceId)
869 .withSelector(selector.build())
870 .withTreatment(treatment.build())
871 .withPriority(DEFAULT_PRIORITY)
872 .fromApp(applicationId)
873 .makePermanent()
874 .forTable(TMAC_TABLE).build();
Julia Ferguson65428c32017-08-10 18:15:24 +0000875 builder.add(rule);
876
877 selector = DefaultTrafficSelector.builder();
878 treatment = DefaultTrafficTreatment.builder();
879 selector.matchEthType(Ethernet.TYPE_IPV6);
880 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
881 treatment.transition(MULTICAST_ROUTING_TABLE);
882 rule = DefaultFlowRule.builder()
883 .forDevice(deviceId)
884 .withSelector(selector.build())
885 .withTreatment(treatment.build())
886 .withPriority(DEFAULT_PRIORITY)
887 .fromApp(applicationId)
888 .makePermanent()
889 .forTable(TMAC_TABLE).build();
890 return builder.add(rule).build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700891 }
892
Saurav Das822c4e22015-10-23 10:51:11 -0700893 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
894 switch (fwd.flag()) {
895 case SPECIFIC:
896 return processSpecific(fwd);
897 case VERSATILE:
898 return processVersatile(fwd);
899 default:
900 fail(fwd, ObjectiveError.UNKNOWN);
901 log.warn("Unknown forwarding flag {}", fwd.flag());
902 }
903 return Collections.emptySet();
904 }
905
906 /**
907 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
908 * ACL table.
909 * @param fwd the forwarding objective of type 'versatile'
910 * @return a collection of flow rules to be sent to the switch. An empty
911 * collection may be returned if there is a problem in processing
912 * the flow rule
913 */
Saurav Das52025962016-01-28 22:30:01 -0800914 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800915 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800916 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700917
918 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800919 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700920 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800921 log.error("Versatile forwarding objective:{} must include ethType",
922 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700923 fail(fwd, ObjectiveError.BADPARAMS);
924 return Collections.emptySet();
925 }
926 if (fwd.nextId() == null && fwd.treatment() == null) {
927 log.error("Forwarding objective {} from {} must contain "
928 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800929 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700930 return Collections.emptySet();
931 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800932
Saurav Das77b5e902016-01-27 17:01:59 -0800933 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
934 fwd.selector().criteria().forEach(criterion -> {
935 if (criterion instanceof VlanIdCriterion) {
936 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
937 // ensure that match does not include vlan = NONE as OF-DPA does not
938 // match untagged packets this way in the ACL table.
939 if (vlanId.equals(VlanId.NONE)) {
940 return;
941 }
Charles Chand1172632017-03-15 17:33:09 -0700942 if (requireVlanExtensions()) {
943 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
944 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
945 } else {
946 sbuilder.matchVlanId(vlanId);
947 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800948 } else if (criterion instanceof Icmpv6TypeCriterion ||
949 criterion instanceof Icmpv6CodeCriterion) {
950 /*
951 * We silenty discard these criterions, our current
952 * OFDPA platform does not support these matches on
953 * the ACL table.
954 */
955 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das77b5e902016-01-27 17:01:59 -0800956 } else {
957 sbuilder.add(criterion);
958 }
959 });
960
Saurav Das822c4e22015-10-23 10:51:11 -0700961 // XXX driver does not currently do type checking as per Tables 65-67 in
962 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -0800963 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
964 if (fwd.treatment() != null) {
965 for (Instruction ins : fwd.treatment().allInstructions()) {
966 if (ins instanceof OutputInstruction) {
967 OutputInstruction o = (OutputInstruction) ins;
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530968 if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800969 ttBuilder.add(o);
970 } else {
971 log.warn("Only allowed treatments in versatile forwarding "
972 + "objectives are punts to the controller");
973 }
974 } else {
975 log.warn("Cannot process instruction in versatile fwd {}", ins);
976 }
Saurav Das822c4e22015-10-23 10:51:11 -0700977 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800978 if (fwd.treatment().clearedDeferred()) {
979 ttBuilder.wipeDeferred();
980 }
Saurav Das822c4e22015-10-23 10:51:11 -0700981 }
Saurav Das822c4e22015-10-23 10:51:11 -0700982 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800983 // overide case
984 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530985 if (next == null) {
986 fail(fwd, ObjectiveError.BADPARAMS);
987 return Collections.emptySet();
988 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800989 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
990 // we only need the top level group's key to point the flow to it
991 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
992 if (group == null) {
993 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
994 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
995 fail(fwd, ObjectiveError.GROUPMISSING);
996 return Collections.emptySet();
997 }
998 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700999 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001000
1001 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1002 .fromApp(fwd.appId())
1003 .withPriority(fwd.priority())
1004 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001005 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001006 .withTreatment(ttBuilder.build())
1007 .makePermanent()
1008 .forTable(ACL_TABLE);
1009 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001010 }
1011
1012 /**
1013 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001014 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001015 *
1016 * @param fwd the forwarding objective of type 'specific'
1017 * @return a collection of flow rules. Typically there will be only one
1018 * for this type of forwarding objective. An empty set may be
1019 * returned if there is an issue in processing the objective.
1020 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001021 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001022 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001023 fwd.id(), deviceId, fwd.nextId());
1024 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1025 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1026
1027 if (isEthTypeObj) {
1028 return processEthTypeSpecific(fwd);
1029 } else if (isEthDstObj) {
1030 return processEthDstSpecific(fwd);
1031 } else {
1032 log.warn("processSpecific: Unsupported forwarding objective "
1033 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001034 fail(fwd, ObjectiveError.UNSUPPORTED);
1035 return Collections.emptySet();
1036 }
Saurav Das4ce45962015-11-24 23:21:05 -08001037 }
1038
Saurav Das4ce45962015-11-24 23:21:05 -08001039 /**
1040 * Handles forwarding rules to the IP and MPLS tables.
1041 *
1042 * @param fwd the forwarding objective
1043 * @return A collection of flow rules, or an empty set
1044 */
1045 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001046 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001047 }
1048
1049 /**
1050 * Internal implementation of processEthTypeSpecific.
1051 * <p>
1052 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1053 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1054 * The allowDefaultRoute must be set to false for OFDPA i12.
1055 * </p>
1056 *
1057 * @param fwd the forwarding objective
1058 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001059 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001060 * @return A collection of flow rules, or an empty set
1061 */
1062 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001063 boolean allowDefaultRoute,
1064 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001065 TrafficSelector selector = fwd.selector();
1066 EthTypeCriterion ethType =
1067 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001068 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001069 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001070 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001071 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001072 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001073 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001074
Saurav Das8a0732e2015-11-20 15:27:53 -08001075 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001076 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1077 return Collections.emptyList();
1078 }
1079 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001080 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001081 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001082 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001083 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001084 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001085 }
Charles Chan14967c22015-12-07 11:11:50 -08001086 if (fwd.treatment() != null) {
1087 for (Instruction instr : fwd.treatment().allInstructions()) {
1088 if (instr instanceof L3ModificationInstruction &&
1089 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001090 // XXX decrementing IP ttl is done automatically for routing, this
1091 // action is ignored or rejected in ofdpa as it is not fully implemented
1092 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001093 }
1094 }
1095 }
1096
Pier Ventree0ae7a32016-11-23 09:57:42 -08001097 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1098 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1099 return Collections.emptyList();
1100 }
1101 forTableId = UNICAST_ROUTING_TABLE;
1102 if (fwd.treatment() != null) {
1103 for (Instruction instr : fwd.treatment().allInstructions()) {
1104 if (instr instanceof L3ModificationInstruction &&
1105 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1106 // XXX decrementing IP ttl is done automatically for routing, this
1107 // action is ignored or rejected in ofdpa as it is not fully implemented
1108 //tb.deferred().add(instr);
1109 }
1110 }
1111 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001112 } else {
1113 filteredSelector
1114 .matchEthType(Ethernet.MPLS_UNICAST)
1115 .matchMplsLabel(((MplsCriterion)
1116 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1117 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001118 .getCriterion(MPLS_BOS);
Saurav Das8a0732e2015-11-20 15:27:53 -08001119 if (bos != null) {
1120 filteredSelector.matchMplsBos(bos.mplsBos());
1121 }
1122 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001123 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1124 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001125
Charles Chan14967c22015-12-07 11:11:50 -08001126 if (fwd.treatment() != null) {
1127 for (Instruction instr : fwd.treatment().allInstructions()) {
1128 if (instr instanceof L2ModificationInstruction &&
1129 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001130 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001131 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001132 // setting the MPLS_TYPE so pop can happen down the pipeline
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001133 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1134 tb.immediate().popMpls();
1135 }
Charles Chan14967c22015-12-07 11:11:50 -08001136 }
1137 if (instr instanceof L3ModificationInstruction &&
1138 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1139 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1140 tb.immediate().decMplsTtl();
1141 }
1142 if (instr instanceof L3ModificationInstruction &&
1143 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1144 tb.immediate().add(instr);
1145 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001146 }
1147 }
1148 }
Saurav Das822c4e22015-10-23 10:51:11 -07001149
1150 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001151 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1152 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001153 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1154 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001155 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1156 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1157 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001158 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001159 return Collections.emptySet();
1160 }
1161
Saurav Das423fe2b2015-12-04 10:52:59 -08001162 NextGroup next = getGroupForNextObjective(fwd.nextId());
1163 if (next != null) {
1164 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1165 // we only need the top level group's key to point the flow to it
1166 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Ventre140a8942016-11-02 07:26:38 -07001167 if (isNotMplsBos(selector) && group.type().equals(HASHED)) {
1168 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1169 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1170 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1171 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1172 return Collections.emptySet();
1173 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001174 if (group == null) {
1175 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1176 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1177 fail(fwd, ObjectiveError.GROUPMISSING);
1178 return Collections.emptySet();
1179 }
1180 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001181 // check if group is empty
1182 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1183 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1184 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1185 emptyGroup = true;
1186 }
Saurav Das25190812016-05-27 13:54:07 -07001187 } else {
1188 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1189 fwd.nextId(), deviceId, fwd.id());
1190 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1191 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001192 }
Saurav Das822c4e22015-10-23 10:51:11 -07001193 }
Charles Chancad338a2016-09-16 18:03:11 -07001194
1195 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001196 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001197 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001198 // set mpls type as apply_action
1199 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001200 }
1201 tb.transition(mplsNextTable);
1202 } else {
1203 tb.transition(ACL_TABLE);
1204 }
1205
Saurav Das822c4e22015-10-23 10:51:11 -07001206 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1207 .fromApp(fwd.appId())
1208 .withPriority(fwd.priority())
1209 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001210 .withSelector(filteredSelector.build())
1211 .withTreatment(tb.build())
1212 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001213
1214 if (fwd.permanent()) {
1215 ruleBuilder.makePermanent();
1216 } else {
1217 ruleBuilder.makeTemporary(fwd.timeout());
1218 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001219 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1220 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001221 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001222 flowRuleCollection.add(
1223 defaultRoute(fwd, complementarySelector, forTableId, tb)
1224 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001225 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1226 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001227 // XXX retrying flows may be necessary due to bug CORD-554
1228 if (emptyGroup) {
1229 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1230 RETRY_MS, TimeUnit.MILLISECONDS);
1231 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001232 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001233 }
1234
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001235 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1236 TrafficSelector.Builder extBuilder,
1237 ForwardingObjective fwd,
1238 boolean allowDefaultRoute) {
1239 TrafficSelector selector = fwd.selector();
1240
1241 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1242 if (ipv4Dst.isMulticast()) {
1243 if (ipv4Dst.prefixLength() != 32) {
1244 log.warn("Multicast specific forwarding objective can only be /32");
1245 fail(fwd, ObjectiveError.BADPARAMS);
1246 return -1;
1247 }
1248 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1249 if (assignedVlan == null) {
1250 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1251 fail(fwd, ObjectiveError.BADPARAMS);
1252 return -1;
1253 }
Charles Chand1172632017-03-15 17:33:09 -07001254 if (requireVlanExtensions()) {
1255 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1256 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1257 } else {
1258 builderToUpdate.matchVlanId(assignedVlan);
1259 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001260 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1261 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1262 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1263 } else {
1264 if (ipv4Dst.prefixLength() == 0) {
1265 if (allowDefaultRoute) {
1266 // The entire IPV4_DST field is wildcarded intentionally
1267 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1268 } else {
1269 // NOTE: The switch does not support matching 0.0.0.0/0
1270 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1271 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1272 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1273 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1274 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1275 }
1276 } else {
1277 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1278 }
1279 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1280 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1281 }
1282 return 0;
1283 }
1284
1285 /**
1286 * Helper method to build Ipv6 selector using the selector provided by
1287 * a forwarding objective.
1288 *
1289 * @param builderToUpdate the builder to update
1290 * @param fwd the selector to read
1291 * @return 0 if the update ends correctly. -1 if the matches
1292 * are not yet supported
1293 */
1294 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1295 ForwardingObjective fwd) {
1296
1297 TrafficSelector selector = fwd.selector();
1298
1299 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1300 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001301 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1302 log.warn("Multicast specific forwarding objective can only be /128");
1303 fail(fwd, ObjectiveError.BADPARAMS);
1304 return -1;
1305 }
1306 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1307 if (assignedVlan == null) {
1308 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1309 fail(fwd, ObjectiveError.BADPARAMS);
1310 return -1;
1311 }
1312 if (requireVlanExtensions()) {
1313 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1314 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1315 } else {
1316 builderToUpdate.matchVlanId(assignedVlan);
1317 }
1318 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1319 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1320 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1321 } else {
1322 if (ipv6Dst.prefixLength() != 0) {
1323 builderToUpdate.matchIPv6Dst(ipv6Dst);
1324 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001325 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1326 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1327 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001328 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001329 return 0;
1330 }
1331
Pier Ventree0ae7a32016-11-23 09:57:42 -08001332 protected FlowRule defaultRoute(ForwardingObjective fwd,
1333 TrafficSelector.Builder complementarySelector,
1334 int forTableId,
1335 TrafficTreatment.Builder tb) {
1336 FlowRule.Builder rule = DefaultFlowRule.builder()
1337 .fromApp(fwd.appId())
1338 .withPriority(fwd.priority())
1339 .forDevice(deviceId)
1340 .withSelector(complementarySelector.build())
1341 .withTreatment(tb.build())
1342 .forTable(forTableId);
1343 if (fwd.permanent()) {
1344 rule.makePermanent();
1345 } else {
1346 rule.makeTemporary(fwd.timeout());
1347 }
1348 return rule.build();
1349 }
1350
Saurav Das4ce45962015-11-24 23:21:05 -08001351 /**
1352 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1353 * allowed in the bridging table - instead we use L2 Interface group or
1354 * L2 flood group
1355 *
1356 * @param fwd the forwarding objective
1357 * @return A collection of flow rules, or an empty set
1358 */
1359 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1360 List<FlowRule> rules = new ArrayList<>();
1361
1362 // Build filtered selector
1363 TrafficSelector selector = fwd.selector();
1364 EthCriterion ethCriterion = (EthCriterion) selector
1365 .getCriterion(Criterion.Type.ETH_DST);
1366 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1367 .getCriterion(Criterion.Type.VLAN_VID);
1368
1369 if (vlanIdCriterion == null) {
1370 log.warn("Forwarding objective for bridging requires vlan. Not "
1371 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1372 fail(fwd, ObjectiveError.BADPARAMS);
1373 return Collections.emptySet();
1374 }
1375
1376 TrafficSelector.Builder filteredSelectorBuilder =
1377 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001378
1379 if (!ethCriterion.mac().equals(NONE) &&
1380 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001381 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1382 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1383 fwd.id(), fwd.nextId(), deviceId);
1384 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001385 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001386 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1387 + "in dev:{} for vlan:{}",
1388 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1389 }
Charles Chand1172632017-03-15 17:33:09 -07001390 if (requireVlanExtensions()) {
1391 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1392 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1393 } else {
1394 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1395 }
Saurav Das4ce45962015-11-24 23:21:05 -08001396 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1397
1398 if (fwd.treatment() != null) {
1399 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1400 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1401 }
1402
1403 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1404 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001405 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001406 if (next != null) {
1407 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1408 // we only need the top level group's key to point the flow to it
1409 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1410 if (group != null) {
1411 treatmentBuilder.deferred().group(group.id());
1412 } else {
1413 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1414 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1415 fail(fwd, ObjectiveError.GROUPMISSING);
1416 return Collections.emptySet();
1417 }
1418 }
1419 }
1420 treatmentBuilder.immediate().transition(ACL_TABLE);
1421 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1422
1423 // Build bridging table entries
1424 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1425 flowRuleBuilder.fromApp(fwd.appId())
1426 .withPriority(fwd.priority())
1427 .forDevice(deviceId)
1428 .withSelector(filteredSelector)
1429 .withTreatment(filteredTreatment)
1430 .forTable(BRIDGING_TABLE);
1431 if (fwd.permanent()) {
1432 flowRuleBuilder.makePermanent();
1433 } else {
1434 flowRuleBuilder.makeTemporary(fwd.timeout());
1435 }
1436 rules.add(flowRuleBuilder.build());
1437 return rules;
1438 }
1439
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001440 //////////////////////////////////////
1441 // Helper Methods and Classes
1442 //////////////////////////////////////
1443
1444 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1445 TrafficSelector selector = fwd.selector();
1446 EthTypeCriterion ethType = (EthTypeCriterion) selector
1447 .getCriterion(Criterion.Type.ETH_TYPE);
1448 return !((ethType == null) ||
1449 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001450 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1451 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001452 }
1453
1454 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1455 TrafficSelector selector = fwd.selector();
1456 EthCriterion ethDst = (EthCriterion) selector
1457 .getCriterion(Criterion.Type.ETH_DST);
1458 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1459 .getCriterion(Criterion.Type.VLAN_VID);
1460 return !(ethDst == null && vlanId == null);
1461 }
1462
Saurav Das423fe2b2015-12-04 10:52:59 -08001463 protected NextGroup getGroupForNextObjective(Integer nextId) {
1464 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1465 if (next != null) {
1466 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1467 if (gkeys != null && !gkeys.isEmpty()) {
1468 return next;
1469 } else {
1470 log.warn("Empty next group found in FlowObjective store for "
1471 + "next-id:{} in dev:{}", nextId, deviceId);
1472 }
1473 } else {
1474 log.warn("next-id {} not found in Flow objective store for dev:{}",
1475 nextId, deviceId);
1476 }
1477 return null;
1478 }
1479
Charles Chan188ebf52015-12-23 00:15:11 -08001480 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001481 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001482 }
1483
Charles Chan188ebf52015-12-23 00:15:11 -08001484 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001485 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001486 }
Saurav Das24431192016-03-07 19:13:00 -08001487
Saurav Das24431192016-03-07 19:13:00 -08001488 @Override
1489 public List<String> getNextMappings(NextGroup nextGroup) {
1490 List<String> mappings = new ArrayList<>();
1491 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1492 for (Deque<GroupKey> gkd : gkeys) {
1493 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001494 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001495 for (GroupKey gk : gkd) {
1496 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001497 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001498 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001499 continue;
1500 }
1501 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1502 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001503 lastGroup = g;
1504 }
1505 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001506 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001507 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001508 lastGroupIns = lastGroup.buckets().buckets().get(0)
1509 .treatment().allInstructions();
1510 }
1511 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001512 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001513 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001514 }
1515 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001516 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001517 }
1518 return mappings;
1519 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001520
Pier Ventre140a8942016-11-02 07:26:38 -07001521 static boolean isMplsBos(TrafficSelector selector) {
1522 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1523 return bosCriterion != null && bosCriterion.mplsBos();
1524 }
1525
1526 static boolean isNotMplsBos(TrafficSelector selector) {
1527 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1528 return bosCriterion != null && !bosCriterion.mplsBos();
1529 }
1530
Charles Chan5b9df8d2016-03-28 22:21:40 -07001531 protected static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001532 if (selector == null) {
1533 return null;
1534 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001535 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1536 return (criterion == null)
1537 ? null : ((VlanIdCriterion) criterion).vlanId();
1538 }
1539
1540 protected static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001541 if (selector == null) {
1542 return null;
1543 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001544 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1545 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1546 }
Charles Chand55e84d2016-03-30 17:54:24 -07001547
1548 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001549 if (treatment == null) {
1550 return null;
1551 }
Charles Chand55e84d2016-03-30 17:54:24 -07001552 for (Instruction i : treatment.allInstructions()) {
1553 if (i instanceof ModVlanIdInstruction) {
1554 return ((ModVlanIdInstruction) i).vlanId();
1555 }
1556 }
1557 return null;
1558 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001559
1560 /**
1561 * Utility class that retries sending flows a fixed number of times, even if
1562 * some of the attempts are successful. Used only for forwarding objectives.
1563 */
1564 protected final class RetryFlows implements Runnable {
1565 int attempts = MAX_RETRY_ATTEMPTS;
1566 private Collection<FlowRule> retryFlows;
1567 private ForwardingObjective fwd;
1568
1569 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
1570 this.fwd = fwd;
1571 this.retryFlows = retryFlows;
1572 }
1573
1574 @Override
1575 public void run() {
1576 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1577 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1578 sendForward(fwd, retryFlows);
1579 if (--attempts > 0) {
1580 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1581 }
1582 }
1583 }
1584
Saurav Das822c4e22015-10-23 10:51:11 -07001585}