blob: 6fd045050487fb8712169895ebc690a05280b1a7 [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;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020022import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000023import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080024import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080025import org.onlab.packet.MacAddress;
Saurav Das822c4e22015-10-23 10:51:11 -070026import org.onlab.packet.VlanId;
27import org.onlab.util.KryoNamespace;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070030import org.onosproject.driver.extensions.Ofdpa3MplsType;
31import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080032import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
33import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070034import org.onosproject.net.DeviceId;
35import org.onosproject.net.Port;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.behaviour.NextGroup;
38import org.onosproject.net.behaviour.Pipeliner;
39import org.onosproject.net.behaviour.PipelinerContext;
40import org.onosproject.net.device.DeviceService;
41import org.onosproject.net.driver.AbstractHandlerBehaviour;
42import org.onosproject.net.flow.DefaultFlowRule;
43import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
45import org.onosproject.net.flow.FlowRule;
46import org.onosproject.net.flow.FlowRuleOperations;
47import org.onosproject.net.flow.FlowRuleOperationsContext;
48import org.onosproject.net.flow.FlowRuleService;
49import org.onosproject.net.flow.TrafficSelector;
50import org.onosproject.net.flow.TrafficTreatment;
51import org.onosproject.net.flow.criteria.Criteria;
52import org.onosproject.net.flow.criteria.Criterion;
53import org.onosproject.net.flow.criteria.EthCriterion;
54import org.onosproject.net.flow.criteria.EthTypeCriterion;
55import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080056import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
57import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080058import org.onosproject.net.flow.criteria.MplsBosCriterion;
59import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070060import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070061import org.onosproject.net.flow.criteria.TcpPortCriterion;
62import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070063import org.onosproject.net.flow.criteria.VlanIdCriterion;
64import org.onosproject.net.flow.instructions.Instruction;
65import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
66import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080067import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chan45b69ab2018-03-02 15:41:41 -080069import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020070import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080071import org.onosproject.net.flow.instructions.L3ModificationInstruction;
72import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070073import org.onosproject.net.flowobjective.FilteringObjective;
74import org.onosproject.net.flowobjective.FlowObjectiveStore;
75import org.onosproject.net.flowobjective.ForwardingObjective;
76import org.onosproject.net.flowobjective.NextObjective;
77import org.onosproject.net.flowobjective.Objective;
78import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070079import org.onosproject.net.group.DefaultGroupKey;
80import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070083import org.onosproject.store.serializers.KryoNamespaces;
84import org.slf4j.Logger;
85
Pier Ventree0ae7a32016-11-23 09:57:42 -080086import java.util.ArrayDeque;
87import java.util.ArrayList;
88import java.util.Collection;
89import java.util.Collections;
90import java.util.Deque;
91import java.util.List;
92import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080093import java.util.concurrent.ScheduledExecutorService;
94import java.util.concurrent.TimeUnit;
95
96import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080097import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -070098import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
99import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800100import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800101import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700102import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700103import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100104import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800105import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700106import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800107
Saurav Das822c4e22015-10-23 10:51:11 -0700108/**
109 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700110 */
Charles Chan361154b2016-03-24 10:23:39 -0700111public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700112 protected static final int PORT_TABLE = 0;
113 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800114 protected static final int VLAN_1_TABLE = 11;
115 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
116 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700117 protected static final int TMAC_TABLE = 20;
118 protected static final int UNICAST_ROUTING_TABLE = 30;
119 protected static final int MULTICAST_ROUTING_TABLE = 40;
120 protected static final int MPLS_TABLE_0 = 23;
121 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700122 protected static final int MPLS_L3_TYPE_TABLE = 27;
123 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int BRIDGING_TABLE = 50;
125 protected static final int ACL_TABLE = 60;
126 protected static final int MAC_LEARNING_TABLE = 254;
127 protected static final long OFPP_MAX = 0xffffff00L;
128
Saurav Das52025962016-01-28 22:30:01 -0800129 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800130 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected static final int LOWEST_PRIORITY = 0x0;
132
Pier Ventre42287df2016-11-09 14:17:26 -0800133 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800134 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
135 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
136
Pier Ventre70d53ba2016-11-17 22:26:29 -0800137 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800138 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
139 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
140
Saurav Das822c4e22015-10-23 10:51:11 -0700141 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700142 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700143 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700144 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800145 protected GroupService groupService;
146 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700147 protected DeviceId deviceId;
148 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700149 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800150 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700151 .register(KryoNamespaces.API)
152 .register(GroupKey.class)
153 .register(DefaultGroupKey.class)
154 .register(OfdpaNextGroup.class)
155 .register(ArrayDeque.class)
156 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700157
Charles Chan425854b2016-04-11 15:32:12 -0700158 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700159
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700160 // flows installations to be retried
Charles Chan50d900c2018-03-02 13:26:22 -0800161 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700162 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800163 private static final int MAX_RETRY_ATTEMPTS = 10;
164 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800165
Saurav Das822c4e22015-10-23 10:51:11 -0700166 @Override
167 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700168 this.deviceId = deviceId;
169
Charles Chan425854b2016-04-11 15:32:12 -0700170 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700171 coreService = serviceDirectory.get(CoreService.class);
172 flowRuleService = serviceDirectory.get(FlowRuleService.class);
173 groupService = serviceDirectory.get(GroupService.class);
174 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700175 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700176
Charles Chan40132b32017-01-22 00:19:37 -0800177 initDriverId();
178 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700179
Saurav Das822c4e22015-10-23 10:51:11 -0700180 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700181 }
182
Charles Chan40132b32017-01-22 00:19:37 -0800183 protected void initDriverId() {
184 driverId = coreService.registerApplication(
185 "org.onosproject.driver.Ofdpa2Pipeline");
186 }
187
188 protected void initGroupHander(PipelinerContext context) {
189 groupHandler = new Ofdpa2GroupHandler();
190 groupHandler.init(deviceId, context);
191 }
192
Saurav Das822c4e22015-10-23 10:51:11 -0700193 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800194 // OF-DPA does not require initializing the pipeline as it puts default
195 // rules automatically in the hardware. However emulation of OFDPA in
196 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700197 }
198
Charles Chand1172632017-03-15 17:33:09 -0700199 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200200 * Determines whether this pipeline requires MPLS POP instruction.
201 *
202 * @return true to use MPLS POP instruction
203 */
204 public boolean requireMplsPop() {
205 return true;
206 }
207
208 /**
209 * Determines whether this pipeline requires MPLS BOS match.
210 *
211 * @return true to use MPLS BOS match
212 */
213 public boolean requireMplsBosMatch() {
214 return true;
215 }
216
217 /**
218 * Determines whether this pipeline requires MPLS TTL decrement and copy.
219 *
220 * @return true to use MPLS TTL decrement and copy
221 */
222 public boolean requireMplsTtlModification() {
223 return true;
224 }
225
226 /**
Charles Chand1172632017-03-15 17:33:09 -0700227 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
228 *
229 * @return true to use the extensions
230 */
231 protected boolean requireVlanExtensions() {
232 return true;
233 }
234
Saurav Das86d13e82017-04-28 17:03:48 -0700235 /**
236 * Determines whether in-port should be matched on in TMAC table rules.
237 *
238 * @return true if match on in-port should be programmed
239 */
240 protected boolean matchInPortTmacTable() {
241 return true;
242 }
243
Charles Chand9e47c62017-10-05 15:17:15 -0700244 /**
245 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
246 *
247 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
248 */
249 protected boolean supportIpv6L4Dst() {
250 return true;
251 }
252
Saurav Dasc568c342018-01-25 09:49:01 -0800253 /**
254 * Determines whether this driver should continue to retry flows that point
255 * to empty groups. See CORD-554.
256 *
257 * @return true if the driver should retry flows
258 */
259 protected boolean shouldRetry() {
260 return true;
261 }
262
Charles Chan45b69ab2018-03-02 15:41:41 -0800263 /**
264 * Determines whether this driver requires unicast flow to be installed before multicast flow
265 * in TMAC table.
266 *
267 * @return true if required
268 */
269 protected boolean requireUnicastBeforeMulticast() {
270 return false;
271 }
272
Saurav Das822c4e22015-10-23 10:51:11 -0700273 //////////////////////////////////////
274 // Flow Objectives
275 //////////////////////////////////////
276
277 @Override
278 public void filter(FilteringObjective filteringObjective) {
279 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
280 processFilter(filteringObjective,
281 filteringObjective.op() == Objective.Operation.ADD,
282 filteringObjective.appId());
283 } else {
284 // Note that packets that don't match the PERMIT filter are
285 // automatically denied. The DENY filter is used to deny packets
286 // that are otherwise permitted by the PERMIT filter.
287 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530288 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700289 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
290 }
291 }
292
293 @Override
294 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700295 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700296 if (rules == null || rules.isEmpty()) {
297 // Assumes fail message has already been generated to the objective
298 // context. Returning here prevents spurious pass message to be
299 // generated by FlowRule service for empty flowOps.
300 return;
301 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700302 sendForward(fwd, rules);
303 }
304
Charles Chan50d900c2018-03-02 13:26:22 -0800305 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700306 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700307 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700308 case ADD:
309 rules.stream()
310 .filter(Objects::nonNull)
311 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800312 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700313 break;
314 case REMOVE:
315 rules.stream()
316 .filter(Objects::nonNull)
317 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800318 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700319 break;
320 default:
321 fail(fwd, ObjectiveError.UNKNOWN);
322 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700323 }
324
Saurav Das822c4e22015-10-23 10:51:11 -0700325 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
326 @Override
327 public void onSuccess(FlowRuleOperations ops) {
328 pass(fwd);
329 }
330
331 @Override
332 public void onError(FlowRuleOperations ops) {
333 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
334 }
335 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700336 }
337
338 @Override
339 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800340 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
341 switch (nextObjective.op()) {
342 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800343 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800344 log.warn("Cannot add next {} that already exists in device {}",
345 nextObjective.id(), deviceId);
346 return;
347 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700348 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800349 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700350 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800351 break;
352 case ADD_TO_EXISTING:
353 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700354 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800355 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700356 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800357 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800358 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800359 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
360 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800361
362 // by design multiple pending bucket is allowed for the group
363 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
364 if (pendBkts == null) {
365 pendBkts = Sets.newHashSet();
366 }
367 pendBkts.add(nextObjective);
368 return pendBkts;
369 });
Saurav Das4f980082015-11-05 13:39:15 -0800370 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800371 break;
372 case REMOVE:
373 if (nextGroup == null) {
374 log.warn("Cannot remove next {} that does not exist in device {}",
375 nextObjective.id(), deviceId);
376 return;
377 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700378 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800379 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700380 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 break;
382 case REMOVE_FROM_EXISTING:
383 if (nextGroup == null) {
384 log.warn("Cannot remove from next {} that does not exist in device {}",
385 nextObjective.id(), deviceId);
386 return;
387 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700388 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800389 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700390 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800391 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900392 case MODIFY:
393 if (nextGroup == null) {
394 log.warn("Cannot modify next {} that does not exist in device {}",
395 nextObjective.id(), deviceId);
396 return;
397 }
398 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
399 nextObjective.id(), deviceId);
400 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
401 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700402 case VERIFY:
403 if (nextGroup == null) {
404 log.warn("Cannot verify next {} that does not exist in device {}",
405 nextObjective.id(), deviceId);
406 return;
407 }
408 log.debug("Processing NextObjective id {} in dev {} - verify",
409 nextObjective.id(), deviceId);
410 groupHandler.verifyGroup(nextObjective, nextGroup);
411 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800412 default:
Saurav Das4f980082015-11-05 13:39:15 -0800413 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700414 }
415 }
416
417 //////////////////////////////////////
418 // Flow handling
419 //////////////////////////////////////
420
421 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700422 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
423 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700424 *
425 * @param filt the filtering objective
426 * @param install indicates whether to add or remove the objective
427 * @param applicationId the application that sent this objective
428 */
Saurav Das52025962016-01-28 22:30:01 -0800429 protected void processFilter(FilteringObjective filt,
430 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700431 // This driver only processes filtering criteria defined with switch
432 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530433 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700434 EthCriterion ethCriterion = null;
435 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700436 if (!filt.key().equals(Criteria.dummy()) &&
437 filt.key().type() == Criterion.Type.IN_PORT) {
438 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700439 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530440 if (portCriterion == null) {
441 log.debug("No IN_PORT defined in filtering objective from app: {}",
442 applicationId);
443 } else {
444 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
445 portCriterion.port());
446 }
Saurav Das822c4e22015-10-23 10:51:11 -0700447 // convert filtering conditions for switch-intfs into flowrules
448 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
449 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700450 switch (criterion.type()) {
451 case ETH_DST:
452 case ETH_DST_MASKED:
453 ethCriterion = (EthCriterion) criterion;
454 break;
455 case VLAN_VID:
456 vidCriterion = (VlanIdCriterion) criterion;
457 break;
458 default:
459 log.warn("Unsupported filter {}", criterion);
460 fail(filt, ObjectiveError.UNSUPPORTED);
461 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700462 }
463 }
464
Saurav Das0e99e2b2015-10-28 12:39:42 -0700465 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800466 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700467 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800468 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800469 assignedVlan = vidCriterion.vlanId();
Piere5bff482018-03-07 11:42:50 +0100470 }
471 // If the meta VLAN is present let's update the assigned vlan
472 if (filt.meta() != null) {
473 VlanId metaVlan = readVlanFromTreatment(filt.meta());
474 if (metaVlan != null) {
475 assignedVlan = metaVlan;
476 }
Charles Chand55e84d2016-03-30 17:54:24 -0700477 }
Charles Chane849c192016-01-11 18:28:54 -0800478
Charles Chand55e84d2016-03-30 17:54:24 -0700479 if (assignedVlan == null) {
480 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800481 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700482 log.debug("VLAN ID in criterion={}, metadata={}",
483 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
484 fail(filt, ObjectiveError.BADPARAMS);
485 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700486 }
487 }
488
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800489 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700490 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700491 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700492 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800493 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800494 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800495 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800496 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800497 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800498 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800499
Charles Chan66291502018-03-02 16:43:28 -0800500 for (FlowRule flowRule : flowRules) {
501 log.trace("{} flow rules in TMAC table: {} for dev: {}",
502 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800503 if (install) {
504 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800505 } else {
Charles Chan66291502018-03-02 16:43:28 -0800506 // NOTE: Only remove TMAC flow when there is no more enabled port within the
507 // same VLAN on this device if TMAC doesn't support matching on in_port.
508 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
509 ops = ops.remove(flowRule);
510 } else {
511 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
512 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800513 }
514 }
Saurav Das822c4e22015-10-23 10:51:11 -0700515 }
516 }
517
Charles Chan985b12e2016-05-11 19:47:22 -0700518 if (vidCriterion == null) {
519 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530520 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700521 } else {
Charles Chan66291502018-03-02 16:43:28 -0800522 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800523 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800524 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800525 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800526 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800527
Charles Chan66291502018-03-02 16:43:28 -0800528 for (FlowRule flowRule : flowRules) {
529 log.trace("{} flow rules in VLAN table: {} for dev: {}",
530 (install) ? "adding" : "removing", flowRule, deviceId);
531 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700532 }
Saurav Das822c4e22015-10-23 10:51:11 -0700533 }
534 }
535
Saurav Das822c4e22015-10-23 10:51:11 -0700536 // apply filtering flow rules
537 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
538 @Override
539 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800540 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700541 ops.stages().get(0).size(), deviceId);
542 pass(filt);
543 }
544
545 @Override
546 public void onError(FlowRuleOperations ops) {
547 log.info("Failed to apply all filtering rules in dev {}", deviceId);
548 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
549 }
550 }));
551
552 }
553
554 /**
Charles Chand1172632017-03-15 17:33:09 -0700555 * Internal implementation of processVlanIdFilter.
556 * <p>
557 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
558 * Since it is non-OF spec, we need an extension treatment for that.
559 * The useVlanExtension must be set to false for OFDPA i12.
560 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800561 * <p>
562 * NOTE: Separate VLAN filtering rules and assignment rules
563 * into different stages in order to guarantee that filtering rules
564 * always go first, as required by OFDPA.
565 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700566 *
Charles Chanf9e98652016-09-07 16:54:23 -0700567 * @param portCriterion port on device for which this filter is programmed
568 * @param vidCriterion vlan assigned to port, or NONE for untagged
569 * @param assignedVlan assigned vlan-id for untagged packets
570 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800571 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700572 */
Charles Chan66291502018-03-02 16:43:28 -0800573 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700574 VlanIdCriterion vidCriterion,
575 VlanId assignedVlan,
576 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800577 List<FlowRule> filteringRules = new ArrayList<>();
578 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700579 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
580 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800581 TrafficSelector.Builder preSelector = null;
582 TrafficTreatment.Builder preTreatment = null;
583
Saurav Das4f980082015-11-05 13:39:15 -0800584 treatment.transition(TMAC_TABLE);
585
Saurav Das822c4e22015-10-23 10:51:11 -0700586 if (vidCriterion.vlanId() == VlanId.NONE) {
587 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700588 preSelector = DefaultTrafficSelector.builder();
589 if (requireVlanExtensions()) {
590 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
591 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700592 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
593 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700594
595 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
596 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700597 } else {
Charles Chand1172632017-03-15 17:33:09 -0700598 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700599 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700600
601 preSelector.matchVlanId(assignedVlan);
602 }
603 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
604 } else {
605 if (requireVlanExtensions()) {
606 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
607 selector.extension(ofdpaMatchVlanVid, deviceId);
608 } else {
609 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700610 }
Charles Chan14967c22015-12-07 11:11:50 -0800611
Charles Chand55e84d2016-03-30 17:54:24 -0700612 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700613 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800614 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
615 treatment.extension(ofdpaSetVlanVid, deviceId);
616 } else {
617 treatment.setVlanId(assignedVlan);
618 }
Charles Chand55e84d2016-03-30 17:54:24 -0700619 }
Saurav Das822c4e22015-10-23 10:51:11 -0700620 }
Saurav Das822c4e22015-10-23 10:51:11 -0700621
622 // ofdpa cannot match on ALL portnumber, so we need to use separate
623 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800624 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530625 if (portCriterion != null) {
626 if (PortNumber.ALL.equals(portCriterion.port())) {
627 for (Port port : deviceService.getPorts(deviceId)) {
628 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
629 portnums.add(port.number());
630 }
Saurav Das822c4e22015-10-23 10:51:11 -0700631 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530632 } else {
633 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700634 }
635 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530636 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800637 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700638 }
Saurav Das4f980082015-11-05 13:39:15 -0800639
Saurav Das822c4e22015-10-23 10:51:11 -0700640 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800641 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700642 selector.matchInPort(pnum);
643 FlowRule rule = DefaultFlowRule.builder()
644 .forDevice(deviceId)
645 .withSelector(selector.build())
646 .withTreatment(treatment.build())
647 .withPriority(DEFAULT_PRIORITY)
648 .fromApp(applicationId)
649 .makePermanent()
650 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800651 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800652
653 if (preSelector != null) {
654 preSelector.matchInPort(pnum);
655 FlowRule preRule = DefaultFlowRule.builder()
656 .forDevice(deviceId)
657 .withSelector(preSelector.build())
658 .withTreatment(preTreatment.build())
659 .withPriority(DEFAULT_PRIORITY)
660 .fromApp(applicationId)
661 .makePermanent()
662 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800663 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800664 }
Saurav Das822c4e22015-10-23 10:51:11 -0700665 }
Charles Chan2686dd72018-03-06 22:10:15 -0800666 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700667 }
668
669 /**
670 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800671 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700672 *
673 * @param portCriterion port on device for which this filter is programmed
674 * @param ethCriterion dstMac of device for which is filter is programmed
675 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700676 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800677 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
678 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
679 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700680 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800681 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700682
683 */
Charles Chan66291502018-03-02 16:43:28 -0800684 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700685 EthCriterion ethCriterion,
686 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700687 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800688 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700689 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800690 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530691 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800692 return processEthDstOnlyFilter(ethCriterion, applicationId);
693 }
694
Charles Chan5b9df8d2016-03-28 22:21:40 -0700695 // Multicast MAC
696 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800697 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700698 }
699
Saurav Das822c4e22015-10-23 10:51:11 -0700700 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530701 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700702 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700703 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530704 List<FlowRule> rules = new ArrayList<>();
705 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
706 if (vidCriterion != null && requireVlanExtensions()) {
707 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
708 }
Saurav Das822c4e22015-10-23 10:51:11 -0700709 // ofdpa cannot match on ALL portnumber, so we need to use separate
710 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700711 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530712 if (portCriterion != null) {
713 if (PortNumber.ALL.equals(portCriterion.port())) {
714 for (Port port : deviceService.getPorts(deviceId)) {
715 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
716 portnums.add(port.number());
717 }
Saurav Das822c4e22015-10-23 10:51:11 -0700718 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530719 } else {
720 portnums.add(portCriterion.port());
721 }
722 for (PortNumber pnum : portnums) {
723 rules.add(buildTmacRuleForIpv4(ethCriterion,
724 vidCriterion,
725 ofdpaMatchVlanVid,
726 applicationId,
727 pnum));
728 rules.add(buildTmacRuleForMpls(ethCriterion,
729 vidCriterion,
730 ofdpaMatchVlanVid,
731 applicationId,
732 pnum));
733 rules.add(buildTmacRuleForIpv6(ethCriterion,
734 vidCriterion,
735 ofdpaMatchVlanVid,
736 applicationId,
737 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700738 }
739 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530740 rules.add(buildTmacRuleForIpv4(ethCriterion,
741 vidCriterion,
742 ofdpaMatchVlanVid,
743 applicationId,
744 null));
745 rules.add(buildTmacRuleForMpls(ethCriterion,
746 vidCriterion,
747 ofdpaMatchVlanVid,
748 applicationId,
749 null));
750 rules.add(buildTmacRuleForIpv6(ethCriterion,
751 vidCriterion,
752 ofdpaMatchVlanVid,
753 applicationId,
754 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700755 }
Charles Chan66291502018-03-02 16:43:28 -0800756 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700757 }
758
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530759 /**
760 * Builds TMAC rules for IPv4 packets.
761 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800762 * @param ethCriterion dst mac matching
763 * @param vidCriterion vlan id assigned to the port
764 * @param ofdpaMatchVlanVid OFDPA vlan id matching
765 * @param applicationId application id
766 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530767 * @return TMAC rule for IPV4 packets
768 */
769 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
770 VlanIdCriterion vidCriterion,
771 OfdpaMatchVlanVid ofdpaMatchVlanVid,
772 ApplicationId applicationId,
773 PortNumber pnum) {
774 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
775 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
776 if (pnum != null) {
777 if (matchInPortTmacTable()) {
778 selector.matchInPort(pnum);
779 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800780 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530781 "ignoring the IN_PORT criteria");
782 }
783 }
784 if (vidCriterion != null) {
785 if (requireVlanExtensions()) {
786 selector.extension(ofdpaMatchVlanVid, deviceId);
787 } else {
788 selector.matchVlanId(vidCriterion.vlanId());
789 }
790 }
791 selector.matchEthType(Ethernet.TYPE_IPV4);
792 selector.matchEthDst(ethCriterion.mac());
793 treatment.transition(UNICAST_ROUTING_TABLE);
794 return DefaultFlowRule.builder()
795 .forDevice(deviceId)
796 .withSelector(selector.build())
797 .withTreatment(treatment.build())
798 .withPriority(DEFAULT_PRIORITY)
799 .fromApp(applicationId)
800 .makePermanent()
801 .forTable(TMAC_TABLE).build();
802 }
803
804 /**
805 * Builds TMAC rule for MPLS packets.
806 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800807 * @param ethCriterion dst mac matching
808 * @param vidCriterion vlan id assigned to the port
809 * @param ofdpaMatchVlanVid OFDPA vlan id matching
810 * @param applicationId application id
811 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530812 * @return TMAC rule for MPLS packets
813 */
814 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
815 VlanIdCriterion vidCriterion,
816 OfdpaMatchVlanVid ofdpaMatchVlanVid,
817 ApplicationId applicationId,
818 PortNumber pnum) {
819 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
820 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
821 if (pnum != null) {
822 if (matchInPortTmacTable()) {
823 selector.matchInPort(pnum);
824 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800825 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530826 "ignoring the IN_PORT criteria");
827 }
828 }
829 if (vidCriterion != null) {
830 if (requireVlanExtensions()) {
831 selector.extension(ofdpaMatchVlanVid, deviceId);
832 } else {
833 selector.matchVlanId(vidCriterion.vlanId());
834 }
835 }
836 selector.matchEthType(Ethernet.MPLS_UNICAST);
837 selector.matchEthDst(ethCriterion.mac());
838 treatment.transition(MPLS_TABLE_0);
839 return DefaultFlowRule.builder()
840 .forDevice(deviceId)
841 .withSelector(selector.build())
842 .withTreatment(treatment.build())
843 .withPriority(DEFAULT_PRIORITY)
844 .fromApp(applicationId)
845 .makePermanent()
846 .forTable(TMAC_TABLE).build();
847 }
848
849 /**
850 * Builds TMAC rules for IPv6 packets.
851 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800852 * @param ethCriterion dst mac matching
853 * @param vidCriterion vlan id assigned to the port
854 * @param ofdpaMatchVlanVid OFDPA vlan id matching
855 * @param applicationId application id
856 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530857 * @return TMAC rule for IPV6 packets
858 */
859 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
860 VlanIdCriterion vidCriterion,
861 OfdpaMatchVlanVid ofdpaMatchVlanVid,
862 ApplicationId applicationId,
863 PortNumber pnum) {
864 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
865 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
866 if (pnum != null) {
867 if (matchInPortTmacTable()) {
868 selector.matchInPort(pnum);
869 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800870 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530871 "ignoring the IN_PORT criteria");
872 }
873 }
874 if (vidCriterion != null) {
875 if (requireVlanExtensions()) {
876 selector.extension(ofdpaMatchVlanVid, deviceId);
877 } else {
878 selector.matchVlanId(vidCriterion.vlanId());
879 }
880 }
881 selector.matchEthType(Ethernet.TYPE_IPV6);
882 selector.matchEthDst(ethCriterion.mac());
883 treatment.transition(UNICAST_ROUTING_TABLE);
884 return DefaultFlowRule.builder()
885 .forDevice(deviceId)
886 .withSelector(selector.build())
887 .withTreatment(treatment.build())
888 .withPriority(DEFAULT_PRIORITY)
889 .fromApp(applicationId)
890 .makePermanent()
891 .forTable(TMAC_TABLE).build();
892 }
893
Charles Chan66291502018-03-02 16:43:28 -0800894 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700895 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800896 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
897
Charles Chan5270ed02016-01-30 23:22:37 -0800898 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
899 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
900 selector.matchEthType(Ethernet.TYPE_IPV4);
901 selector.matchEthDst(ethCriterion.mac());
902 treatment.transition(UNICAST_ROUTING_TABLE);
903 FlowRule rule = DefaultFlowRule.builder()
904 .forDevice(deviceId)
905 .withSelector(selector.build())
906 .withTreatment(treatment.build())
907 .withPriority(DEFAULT_PRIORITY)
908 .fromApp(applicationId)
909 .makePermanent()
910 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800911 builder.add(rule);
912
913 selector = DefaultTrafficSelector.builder();
914 treatment = DefaultTrafficTreatment.builder();
915 selector.matchEthType(Ethernet.TYPE_IPV6);
916 selector.matchEthDst(ethCriterion.mac());
917 treatment.transition(UNICAST_ROUTING_TABLE);
918 rule = DefaultFlowRule.builder()
919 .forDevice(deviceId)
920 .withSelector(selector.build())
921 .withTreatment(treatment.build())
922 .withPriority(DEFAULT_PRIORITY)
923 .fromApp(applicationId)
924 .makePermanent()
925 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800926 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800927 }
928
Charles Chan66291502018-03-02 16:43:28 -0800929 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700930 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800931 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700932 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800933 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
934 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
935 TrafficSelector.Builder selector;
936 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700937 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000938
Charles Chanb4879a52017-10-20 19:09:16 -0700939 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800940 if (requireUnicastBeforeMulticast()) {
941 selector = DefaultTrafficSelector.builder();
942 treatment = DefaultTrafficTreatment.builder();
943 selector.matchEthType(Ethernet.TYPE_IPV4);
944 selector.matchEthDst(unicastMac);
945 selector.matchVlanId(assignedVlan);
946 treatment.transition(UNICAST_ROUTING_TABLE);
947 rule = DefaultFlowRule.builder()
948 .forDevice(deviceId)
949 .withSelector(selector.build())
950 .withTreatment(treatment.build())
951 .withPriority(DEFAULT_PRIORITY)
952 .fromApp(applicationId)
953 .makePermanent()
954 .forTable(TMAC_TABLE).build();
955 unicastFlows.add(rule);
956 }
957
958 selector = DefaultTrafficSelector.builder();
959 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700960 selector.matchEthType(Ethernet.TYPE_IPV4);
961 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
962 selector.matchVlanId(assignedVlan);
963 treatment.transition(MULTICAST_ROUTING_TABLE);
964 rule = DefaultFlowRule.builder()
965 .forDevice(deviceId)
966 .withSelector(selector.build())
967 .withTreatment(treatment.build())
968 .withPriority(DEFAULT_PRIORITY)
969 .fromApp(applicationId)
970 .makePermanent()
971 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -0800972 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700973 }
974
975 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800976 if (requireUnicastBeforeMulticast()) {
977 selector = DefaultTrafficSelector.builder();
978 treatment = DefaultTrafficTreatment.builder();
979 selector.matchEthType(Ethernet.TYPE_IPV6);
980 selector.matchEthDst(unicastMac);
981 selector.matchVlanId(assignedVlan);
982 treatment.transition(UNICAST_ROUTING_TABLE);
983 rule = DefaultFlowRule.builder()
984 .forDevice(deviceId)
985 .withSelector(selector.build())
986 .withTreatment(treatment.build())
987 .withPriority(DEFAULT_PRIORITY)
988 .fromApp(applicationId)
989 .makePermanent()
990 .forTable(TMAC_TABLE).build();
991 unicastFlows.add(rule);
992 }
993
Charles Chanb4879a52017-10-20 19:09:16 -0700994 selector = DefaultTrafficSelector.builder();
995 treatment = DefaultTrafficTreatment.builder();
996 selector.matchEthType(Ethernet.TYPE_IPV6);
997 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
998 selector.matchVlanId(assignedVlan);
999 treatment.transition(MULTICAST_ROUTING_TABLE);
1000 rule = DefaultFlowRule.builder()
1001 .forDevice(deviceId)
1002 .withSelector(selector.build())
1003 .withTreatment(treatment.build())
1004 .withPriority(DEFAULT_PRIORITY)
1005 .fromApp(applicationId)
1006 .makePermanent()
1007 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001008 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001009 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001010 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001011 }
1012
Saurav Das822c4e22015-10-23 10:51:11 -07001013 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1014 switch (fwd.flag()) {
1015 case SPECIFIC:
1016 return processSpecific(fwd);
1017 case VERSATILE:
1018 return processVersatile(fwd);
1019 default:
1020 fail(fwd, ObjectiveError.UNKNOWN);
1021 log.warn("Unknown forwarding flag {}", fwd.flag());
1022 }
1023 return Collections.emptySet();
1024 }
1025
1026 /**
1027 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1028 * ACL table.
1029 * @param fwd the forwarding objective of type 'versatile'
1030 * @return a collection of flow rules to be sent to the switch. An empty
1031 * collection may be returned if there is a problem in processing
1032 * the flow rule
1033 */
Saurav Das52025962016-01-28 22:30:01 -08001034 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001035 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001036 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001037
1038 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -08001039 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -07001040 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001041 log.error("Versatile forwarding objective:{} must include ethType",
1042 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001043 fail(fwd, ObjectiveError.BADPARAMS);
1044 return Collections.emptySet();
1045 }
1046 if (fwd.nextId() == null && fwd.treatment() == null) {
1047 log.error("Forwarding objective {} from {} must contain "
1048 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001049 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001050 return Collections.emptySet();
1051 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001052
Saurav Das77b5e902016-01-27 17:01:59 -08001053 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1054 fwd.selector().criteria().forEach(criterion -> {
1055 if (criterion instanceof VlanIdCriterion) {
1056 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1057 // ensure that match does not include vlan = NONE as OF-DPA does not
1058 // match untagged packets this way in the ACL table.
1059 if (vlanId.equals(VlanId.NONE)) {
1060 return;
1061 }
Charles Chand1172632017-03-15 17:33:09 -07001062 if (requireVlanExtensions()) {
1063 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1064 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1065 } else {
1066 sbuilder.matchVlanId(vlanId);
1067 }
Charles Chan09bf2692018-01-11 11:48:18 -08001068 } else if (criterion instanceof Icmpv6TypeCriterion) {
1069 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1070 sbuilder.matchIcmpv6Type(icmpv6Type);
1071 } else if (criterion instanceof Icmpv6CodeCriterion) {
1072 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1073 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001074 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1075 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1076 // Currently L4 dst port matching is only used by DHCP relay feature
1077 // and therefore is safe to be replaced with L4 src port matching.
1078 // We need to revisit this if L4 dst port is used for other purpose in the future.
1079 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001080 switch (criterion.type()) {
1081 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001082 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001083 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001084 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001085 break;
1086 default:
1087 sbuilder.add(criterion);
1088 }
1089 } else {
1090 sbuilder.add(criterion);
1091 }
Saurav Das77b5e902016-01-27 17:01:59 -08001092 } else {
1093 sbuilder.add(criterion);
1094 }
1095 });
1096
Saurav Das822c4e22015-10-23 10:51:11 -07001097 // XXX driver does not currently do type checking as per Tables 65-67 in
1098 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001099 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1100 if (fwd.treatment() != null) {
1101 for (Instruction ins : fwd.treatment().allInstructions()) {
1102 if (ins instanceof OutputInstruction) {
1103 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001104 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001105 ttBuilder.add(o);
1106 } else {
1107 log.warn("Only allowed treatments in versatile forwarding "
1108 + "objectives are punts to the controller");
1109 }
1110 } else {
1111 log.warn("Cannot process instruction in versatile fwd {}", ins);
1112 }
Saurav Das822c4e22015-10-23 10:51:11 -07001113 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001114 if (fwd.treatment().clearedDeferred()) {
1115 ttBuilder.wipeDeferred();
1116 }
Saurav Das822c4e22015-10-23 10:51:11 -07001117 }
Saurav Das822c4e22015-10-23 10:51:11 -07001118 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001119 // overide case
1120 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301121 if (next == null) {
1122 fail(fwd, ObjectiveError.BADPARAMS);
1123 return Collections.emptySet();
1124 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001125 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1126 // we only need the top level group's key to point the flow to it
1127 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1128 if (group == null) {
1129 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1130 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1131 fail(fwd, ObjectiveError.GROUPMISSING);
1132 return Collections.emptySet();
1133 }
1134 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001135 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001136
1137 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1138 .fromApp(fwd.appId())
1139 .withPriority(fwd.priority())
1140 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001141 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001142 .withTreatment(ttBuilder.build())
1143 .makePermanent()
1144 .forTable(ACL_TABLE);
1145 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001146 }
1147
1148 /**
1149 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001150 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001151 *
1152 * @param fwd the forwarding objective of type 'specific'
1153 * @return a collection of flow rules. Typically there will be only one
1154 * for this type of forwarding objective. An empty set may be
1155 * returned if there is an issue in processing the objective.
1156 */
Charles Chan50d900c2018-03-02 13:26:22 -08001157 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001158 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001159 fwd.id(), deviceId, fwd.nextId());
1160 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1161 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1162
1163 if (isEthTypeObj) {
1164 return processEthTypeSpecific(fwd);
1165 } else if (isEthDstObj) {
1166 return processEthDstSpecific(fwd);
1167 } else {
1168 log.warn("processSpecific: Unsupported forwarding objective "
1169 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001170 fail(fwd, ObjectiveError.UNSUPPORTED);
1171 return Collections.emptySet();
1172 }
Saurav Das4ce45962015-11-24 23:21:05 -08001173 }
1174
Saurav Das4ce45962015-11-24 23:21:05 -08001175 /**
1176 * Handles forwarding rules to the IP and MPLS tables.
1177 *
1178 * @param fwd the forwarding objective
1179 * @return A collection of flow rules, or an empty set
1180 */
1181 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001182 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001183 }
1184
1185 /**
1186 * Internal implementation of processEthTypeSpecific.
1187 * <p>
1188 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1189 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1190 * The allowDefaultRoute must be set to false for OFDPA i12.
1191 * </p>
1192 *
1193 * @param fwd the forwarding objective
1194 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001195 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001196 * @return A collection of flow rules, or an empty set
1197 */
1198 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001199 boolean allowDefaultRoute,
1200 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001201 TrafficSelector selector = fwd.selector();
1202 EthTypeCriterion ethType =
1203 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001204 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001205 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001206 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001207 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001208 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001209
Saurav Das8a0732e2015-11-20 15:27:53 -08001210 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001211 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1212 return Collections.emptyList();
1213 }
1214 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001215 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001216 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001217 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001218 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001219 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001220 }
Charles Chan50d900c2018-03-02 13:26:22 -08001221
Charles Chan236653d2018-03-05 11:28:23 -08001222 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001223 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001224 // if (fwd.treatment() != null) {
1225 // for (Instruction instr : fwd.treatment().allInstructions()) {
1226 // if (instr instanceof L3ModificationInstruction &&
1227 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1228 // tb.deferred().add(instr);
1229 // }
1230 // }
1231 // }
1232
Pier Ventree0ae7a32016-11-23 09:57:42 -08001233 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1234 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1235 return Collections.emptyList();
1236 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001237 //We need to set the proper next table
1238 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1239 if (ipv6Dst.isMulticast()) {
1240 forTableId = MULTICAST_ROUTING_TABLE;
1241 } else {
1242 forTableId = UNICAST_ROUTING_TABLE;
1243 }
1244
Charles Chan236653d2018-03-05 11:28:23 -08001245 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001246 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001247 // if (fwd.treatment() != null) {
1248 // for (Instruction instr : fwd.treatment().allInstructions()) {
1249 // if (instr instanceof L3ModificationInstruction &&
1250 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1251 // tb.deferred().add(instr);
1252 // }
1253 // }
1254 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001255 } else {
1256 filteredSelector
1257 .matchEthType(Ethernet.MPLS_UNICAST)
1258 .matchMplsLabel(((MplsCriterion)
1259 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1260 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001261 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001262 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001263 filteredSelector.matchMplsBos(bos.mplsBos());
1264 }
1265 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001266 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1267 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001268
Charles Chan14967c22015-12-07 11:11:50 -08001269 if (fwd.treatment() != null) {
1270 for (Instruction instr : fwd.treatment().allInstructions()) {
1271 if (instr instanceof L2ModificationInstruction &&
1272 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001273 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001274 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001275 if (requireMplsPop()) {
1276 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1277 tb.immediate().popMpls();
1278 }
1279 } else {
1280 // Skip mpls pop action for mpls_unicast label
1281 if (instr instanceof ModMplsHeaderInstruction &&
1282 !((ModMplsHeaderInstruction) instr).ethernetType()
1283 .equals(EtherType.MPLS_UNICAST.ethType())) {
1284 tb.immediate().add(instr);
1285 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001286 }
Charles Chan14967c22015-12-07 11:11:50 -08001287 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001288
1289 if (requireMplsTtlModification()) {
1290 if (instr instanceof L3ModificationInstruction &&
1291 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1292 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1293 tb.immediate().decMplsTtl();
1294 }
1295 if (instr instanceof L3ModificationInstruction &&
1296 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1297 tb.immediate().add(instr);
1298 }
Charles Chan14967c22015-12-07 11:11:50 -08001299 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001300 }
1301 }
1302 }
Saurav Das822c4e22015-10-23 10:51:11 -07001303
1304 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001305 NextGroup next = getGroupForNextObjective(fwd.nextId());
1306 if (next != null) {
1307 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1308 // we only need the top level group's key to point the flow to it
1309 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1310 if (group == null) {
1311 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1312 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1313 fail(fwd, ObjectiveError.GROUPMISSING);
1314 return Collections.emptySet();
1315 }
Saurav Dasa4020382018-02-14 14:14:54 -08001316 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1317 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1318 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1319 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1320 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1321 return Collections.emptySet();
1322 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001323 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001324 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001325 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001326 if (shouldRetry()) {
1327 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1328 Integer.toHexString(group.id().id()), deviceId,
1329 fwd.id());
1330 emptyGroup = true;
1331 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001332 }
Saurav Das25190812016-05-27 13:54:07 -07001333 } else {
1334 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1335 fwd.nextId(), deviceId, fwd.id());
1336 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1337 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001338 }
Saurav Das822c4e22015-10-23 10:51:11 -07001339 }
Charles Chancad338a2016-09-16 18:03:11 -07001340
1341 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001342 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001343 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001344 // set mpls type as apply_action
1345 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001346 }
1347 tb.transition(mplsNextTable);
1348 } else {
1349 tb.transition(ACL_TABLE);
1350 }
1351
Saurav Das822c4e22015-10-23 10:51:11 -07001352 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1353 .fromApp(fwd.appId())
1354 .withPriority(fwd.priority())
1355 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001356 .withSelector(filteredSelector.build())
1357 .withTreatment(tb.build())
1358 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001359
1360 if (fwd.permanent()) {
1361 ruleBuilder.makePermanent();
1362 } else {
1363 ruleBuilder.makeTemporary(fwd.timeout());
1364 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001365 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1366 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001367 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001368 flowRuleCollection.add(
1369 defaultRoute(fwd, complementarySelector, forTableId, tb)
1370 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001371 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1372 }
Saurav Dasc568c342018-01-25 09:49:01 -08001373
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001374 if (emptyGroup) {
1375 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1376 RETRY_MS, TimeUnit.MILLISECONDS);
1377 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001378 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001379 }
1380
Charles Chan50d900c2018-03-02 13:26:22 -08001381 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001382 TrafficSelector.Builder extBuilder,
1383 ForwardingObjective fwd,
1384 boolean allowDefaultRoute) {
1385 TrafficSelector selector = fwd.selector();
1386
1387 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1388 if (ipv4Dst.isMulticast()) {
1389 if (ipv4Dst.prefixLength() != 32) {
1390 log.warn("Multicast specific forwarding objective can only be /32");
1391 fail(fwd, ObjectiveError.BADPARAMS);
1392 return -1;
1393 }
1394 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1395 if (assignedVlan == null) {
1396 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1397 fail(fwd, ObjectiveError.BADPARAMS);
1398 return -1;
1399 }
Charles Chand1172632017-03-15 17:33:09 -07001400 if (requireVlanExtensions()) {
1401 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1402 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1403 } else {
1404 builderToUpdate.matchVlanId(assignedVlan);
1405 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001406 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1407 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1408 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1409 } else {
1410 if (ipv4Dst.prefixLength() == 0) {
1411 if (allowDefaultRoute) {
1412 // The entire IPV4_DST field is wildcarded intentionally
1413 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1414 } else {
1415 // NOTE: The switch does not support matching 0.0.0.0/0
1416 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1417 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1418 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1419 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1420 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1421 }
1422 } else {
1423 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1424 }
1425 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1426 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1427 }
1428 return 0;
1429 }
1430
1431 /**
1432 * Helper method to build Ipv6 selector using the selector provided by
1433 * a forwarding objective.
1434 *
1435 * @param builderToUpdate the builder to update
1436 * @param fwd the selector to read
1437 * @return 0 if the update ends correctly. -1 if the matches
1438 * are not yet supported
1439 */
Charles Chan50d900c2018-03-02 13:26:22 -08001440 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001441 ForwardingObjective fwd) {
1442
1443 TrafficSelector selector = fwd.selector();
1444
1445 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1446 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001447 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1448 log.warn("Multicast specific forwarding objective can only be /128");
1449 fail(fwd, ObjectiveError.BADPARAMS);
1450 return -1;
1451 }
1452 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1453 if (assignedVlan == null) {
1454 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1455 fail(fwd, ObjectiveError.BADPARAMS);
1456 return -1;
1457 }
1458 if (requireVlanExtensions()) {
1459 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1460 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1461 } else {
1462 builderToUpdate.matchVlanId(assignedVlan);
1463 }
1464 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1465 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1466 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1467 } else {
1468 if (ipv6Dst.prefixLength() != 0) {
1469 builderToUpdate.matchIPv6Dst(ipv6Dst);
1470 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001471 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1472 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1473 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001474 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001475 return 0;
1476 }
1477
Charles Chan50d900c2018-03-02 13:26:22 -08001478 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001479 TrafficSelector.Builder complementarySelector,
1480 int forTableId,
1481 TrafficTreatment.Builder tb) {
1482 FlowRule.Builder rule = DefaultFlowRule.builder()
1483 .fromApp(fwd.appId())
1484 .withPriority(fwd.priority())
1485 .forDevice(deviceId)
1486 .withSelector(complementarySelector.build())
1487 .withTreatment(tb.build())
1488 .forTable(forTableId);
1489 if (fwd.permanent()) {
1490 rule.makePermanent();
1491 } else {
1492 rule.makeTemporary(fwd.timeout());
1493 }
1494 return rule.build();
1495 }
1496
Saurav Das4ce45962015-11-24 23:21:05 -08001497 /**
1498 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1499 * allowed in the bridging table - instead we use L2 Interface group or
1500 * L2 flood group
1501 *
1502 * @param fwd the forwarding objective
1503 * @return A collection of flow rules, or an empty set
1504 */
1505 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1506 List<FlowRule> rules = new ArrayList<>();
1507
1508 // Build filtered selector
1509 TrafficSelector selector = fwd.selector();
1510 EthCriterion ethCriterion = (EthCriterion) selector
1511 .getCriterion(Criterion.Type.ETH_DST);
1512 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1513 .getCriterion(Criterion.Type.VLAN_VID);
1514
1515 if (vlanIdCriterion == null) {
1516 log.warn("Forwarding objective for bridging requires vlan. Not "
1517 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1518 fail(fwd, ObjectiveError.BADPARAMS);
1519 return Collections.emptySet();
1520 }
1521
1522 TrafficSelector.Builder filteredSelectorBuilder =
1523 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001524
1525 if (!ethCriterion.mac().equals(NONE) &&
1526 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001527 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1528 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1529 fwd.id(), fwd.nextId(), deviceId);
1530 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001531 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001532 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1533 + "in dev:{} for vlan:{}",
1534 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1535 }
Charles Chand1172632017-03-15 17:33:09 -07001536 if (requireVlanExtensions()) {
1537 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1538 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1539 } else {
1540 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1541 }
Saurav Das4ce45962015-11-24 23:21:05 -08001542 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1543
1544 if (fwd.treatment() != null) {
1545 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1546 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1547 }
1548
1549 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1550 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001551 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001552 if (next != null) {
1553 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1554 // we only need the top level group's key to point the flow to it
1555 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1556 if (group != null) {
1557 treatmentBuilder.deferred().group(group.id());
1558 } else {
1559 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1560 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1561 fail(fwd, ObjectiveError.GROUPMISSING);
1562 return Collections.emptySet();
1563 }
1564 }
1565 }
1566 treatmentBuilder.immediate().transition(ACL_TABLE);
1567 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1568
1569 // Build bridging table entries
1570 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1571 flowRuleBuilder.fromApp(fwd.appId())
1572 .withPriority(fwd.priority())
1573 .forDevice(deviceId)
1574 .withSelector(filteredSelector)
1575 .withTreatment(filteredTreatment)
1576 .forTable(BRIDGING_TABLE);
1577 if (fwd.permanent()) {
1578 flowRuleBuilder.makePermanent();
1579 } else {
1580 flowRuleBuilder.makeTemporary(fwd.timeout());
1581 }
1582 rules.add(flowRuleBuilder.build());
1583 return rules;
1584 }
1585
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001586 //////////////////////////////////////
1587 // Helper Methods and Classes
1588 //////////////////////////////////////
1589
1590 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1591 TrafficSelector selector = fwd.selector();
1592 EthTypeCriterion ethType = (EthTypeCriterion) selector
1593 .getCriterion(Criterion.Type.ETH_TYPE);
1594 return !((ethType == null) ||
1595 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001596 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1597 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001598 }
1599
1600 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1601 TrafficSelector selector = fwd.selector();
1602 EthCriterion ethDst = (EthCriterion) selector
1603 .getCriterion(Criterion.Type.ETH_DST);
1604 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1605 .getCriterion(Criterion.Type.VLAN_VID);
1606 return !(ethDst == null && vlanId == null);
1607 }
1608
Charles Chan50d900c2018-03-02 13:26:22 -08001609 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001610 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1611 if (next != null) {
1612 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1613 if (gkeys != null && !gkeys.isEmpty()) {
1614 return next;
1615 } else {
1616 log.warn("Empty next group found in FlowObjective store for "
1617 + "next-id:{} in dev:{}", nextId, deviceId);
1618 }
1619 } else {
1620 log.warn("next-id {} not found in Flow objective store for dev:{}",
1621 nextId, deviceId);
1622 }
1623 return null;
1624 }
1625
Charles Chan188ebf52015-12-23 00:15:11 -08001626 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001627 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001628 }
1629
Charles Chan188ebf52015-12-23 00:15:11 -08001630 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001631 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001632 }
Saurav Das24431192016-03-07 19:13:00 -08001633
Saurav Das24431192016-03-07 19:13:00 -08001634 @Override
1635 public List<String> getNextMappings(NextGroup nextGroup) {
1636 List<String> mappings = new ArrayList<>();
1637 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1638 for (Deque<GroupKey> gkd : gkeys) {
1639 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001640 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001641 for (GroupKey gk : gkd) {
1642 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001643 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001644 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001645 continue;
1646 }
1647 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1648 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001649 lastGroup = g;
1650 }
1651 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001652 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001653 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001654 lastGroupIns = lastGroup.buckets().buckets().get(0)
1655 .treatment().allInstructions();
1656 }
1657 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001658 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001659 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001660 }
1661 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001662 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001663 }
1664 return mappings;
1665 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001666
Saurav Dasa4020382018-02-14 14:14:54 -08001667 /**
1668 * Returns true iff the given selector matches on BOS==true, indicating that
1669 * the selector is trying to match on a label that is bottom-of-stack.
1670 *
1671 * @param selector the given match
1672 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1673 * expressed in the given selector
1674 */
Pier Ventre140a8942016-11-02 07:26:38 -07001675 static boolean isMplsBos(TrafficSelector selector) {
1676 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1677 return bosCriterion != null && bosCriterion.mplsBos();
1678 }
1679
Saurav Dasa4020382018-02-14 14:14:54 -08001680 /**
1681 * Returns true iff the given selector matches on BOS==false, indicating
1682 * that the selector is trying to match on a label that is not the
1683 * bottom-of-stack label.
1684 *
1685 * @param selector the given match
1686 * @return true iff BoS==false;
1687 * false if BOS==true, or BOS matching is not expressed in the given selector
1688 */
Pier Ventre140a8942016-11-02 07:26:38 -07001689 static boolean isNotMplsBos(TrafficSelector selector) {
1690 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1691 return bosCriterion != null && !bosCriterion.mplsBos();
1692 }
1693
Saurav Dasa4020382018-02-14 14:14:54 -08001694 /**
1695 * Returns true iff the forwarding objective includes a treatment to pop the
1696 * MPLS label.
1697 *
1698 * @param fwd the given forwarding objective
1699 * @return true iff mpls pop treatment exists
1700 */
1701 static boolean isMplsPop(ForwardingObjective fwd) {
1702 if (fwd.treatment() != null) {
1703 for (Instruction instr : fwd.treatment().allInstructions()) {
1704 if (instr instanceof L2ModificationInstruction
1705 && ((L2ModificationInstruction) instr)
1706 .subtype() == L2SubType.MPLS_POP) {
1707 return true;
1708 }
1709 }
1710 }
1711 return false;
1712 }
1713
Charles Chand9e47c62017-10-05 15:17:15 -07001714 private static boolean isIpv6(TrafficSelector selector) {
1715 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1716 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1717 }
1718
Charles Chan50d900c2018-03-02 13:26:22 -08001719 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001720 if (selector == null) {
1721 return null;
1722 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001723 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1724 return (criterion == null)
1725 ? null : ((VlanIdCriterion) criterion).vlanId();
1726 }
1727
Charles Chand9e47c62017-10-05 15:17:15 -07001728 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001729 if (selector == null) {
1730 return null;
1731 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001732 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1733 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1734 }
Charles Chand55e84d2016-03-30 17:54:24 -07001735
1736 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001737 if (treatment == null) {
1738 return null;
1739 }
Charles Chand55e84d2016-03-30 17:54:24 -07001740 for (Instruction i : treatment.allInstructions()) {
1741 if (i instanceof ModVlanIdInstruction) {
1742 return ((ModVlanIdInstruction) i).vlanId();
1743 }
1744 }
1745 return null;
1746 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001747
Charles Chan45b69ab2018-03-02 15:41:41 -08001748 private static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
1749 if (treatment == null) {
1750 return null;
1751 }
1752 for (Instruction i : treatment.allInstructions()) {
1753 if (i instanceof ModEtherInstruction) {
1754 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1755 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1756 return modEtherInstruction.mac();
1757 }
1758 }
1759 }
1760 return null;
1761 }
1762
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001763 /**
1764 * Utility class that retries sending flows a fixed number of times, even if
1765 * some of the attempts are successful. Used only for forwarding objectives.
1766 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001767 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001768 int attempts = MAX_RETRY_ATTEMPTS;
1769 private Collection<FlowRule> retryFlows;
1770 private ForwardingObjective fwd;
1771
Charles Chan50d900c2018-03-02 13:26:22 -08001772 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001773 this.fwd = fwd;
1774 this.retryFlows = retryFlows;
1775 }
1776
1777 @Override
1778 public void run() {
1779 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1780 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1781 sendForward(fwd, retryFlows);
1782 if (--attempts > 0) {
1783 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1784 }
1785 }
1786 }
1787
Saurav Das822c4e22015-10-23 10:51:11 -07001788}