blob: 9f23cc9ee413fa3f1c848d2d42267faba30bcfe8 [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();
Charles Chanc550f2e2017-12-19 19:55:57 -0800470 } else if (filt.meta() != null) {
471 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700472 }
Charles Chane849c192016-01-11 18:28:54 -0800473
Charles Chand55e84d2016-03-30 17:54:24 -0700474 if (assignedVlan == null) {
475 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800476 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700477 log.debug("VLAN ID in criterion={}, metadata={}",
478 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
479 fail(filt, ObjectiveError.BADPARAMS);
480 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700481 }
482 }
483
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800484 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700485 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700486 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700487 } else {
Charles Chan45b69ab2018-03-02 15:41:41 -0800488 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan66291502018-03-02 16:43:28 -0800489 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chan45b69ab2018-03-02 15:41:41 -0800490 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800491 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800492 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan66291502018-03-02 16:43:28 -0800493 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800494
Charles Chan66291502018-03-02 16:43:28 -0800495 for (FlowRule flowRule : flowRules) {
496 log.trace("{} flow rules in TMAC table: {} for dev: {}",
497 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan66291502018-03-02 16:43:28 -0800498 if (install) {
499 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800500 } else {
Charles Chan66291502018-03-02 16:43:28 -0800501 // NOTE: Only remove TMAC flow when there is no more enabled port within the
502 // same VLAN on this device if TMAC doesn't support matching on in_port.
503 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
504 ops = ops.remove(flowRule);
505 } else {
506 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
507 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800508 }
509 }
Saurav Das822c4e22015-10-23 10:51:11 -0700510 }
511 }
512
Charles Chan985b12e2016-05-11 19:47:22 -0700513 if (vidCriterion == null) {
514 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530515 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700516 } else {
Charles Chan66291502018-03-02 16:43:28 -0800517 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800518 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800519 for (List<FlowRule> flowRules : allStages) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800520 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan66291502018-03-02 16:43:28 -0800521 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800522
Charles Chan66291502018-03-02 16:43:28 -0800523 for (FlowRule flowRule : flowRules) {
524 log.trace("{} flow rules in VLAN table: {} for dev: {}",
525 (install) ? "adding" : "removing", flowRule, deviceId);
526 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700527 }
Saurav Das822c4e22015-10-23 10:51:11 -0700528 }
529 }
530
Saurav Das822c4e22015-10-23 10:51:11 -0700531 // apply filtering flow rules
532 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
533 @Override
534 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800535 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700536 ops.stages().get(0).size(), deviceId);
537 pass(filt);
538 }
539
540 @Override
541 public void onError(FlowRuleOperations ops) {
542 log.info("Failed to apply all filtering rules in dev {}", deviceId);
543 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
544 }
545 }));
546
547 }
548
549 /**
Charles Chand1172632017-03-15 17:33:09 -0700550 * Internal implementation of processVlanIdFilter.
551 * <p>
552 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
553 * Since it is non-OF spec, we need an extension treatment for that.
554 * The useVlanExtension must be set to false for OFDPA i12.
555 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800556 * <p>
557 * NOTE: Separate VLAN filtering rules and assignment rules
558 * into different stages in order to guarantee that filtering rules
559 * always go first, as required by OFDPA.
560 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700561 *
Charles Chanf9e98652016-09-07 16:54:23 -0700562 * @param portCriterion port on device for which this filter is programmed
563 * @param vidCriterion vlan assigned to port, or NONE for untagged
564 * @param assignedVlan assigned vlan-id for untagged packets
565 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800566 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700567 */
Charles Chan66291502018-03-02 16:43:28 -0800568 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700569 VlanIdCriterion vidCriterion,
570 VlanId assignedVlan,
571 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800572 List<FlowRule> filteringRules = new ArrayList<>();
573 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700574 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
575 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800576 TrafficSelector.Builder preSelector = null;
577 TrafficTreatment.Builder preTreatment = null;
578
Saurav Das4f980082015-11-05 13:39:15 -0800579 treatment.transition(TMAC_TABLE);
580
Saurav Das822c4e22015-10-23 10:51:11 -0700581 if (vidCriterion.vlanId() == VlanId.NONE) {
582 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700583 preSelector = DefaultTrafficSelector.builder();
584 if (requireVlanExtensions()) {
585 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
586 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700587 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
588 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700589
590 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
591 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700592 } else {
Charles Chand1172632017-03-15 17:33:09 -0700593 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700594 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700595
596 preSelector.matchVlanId(assignedVlan);
597 }
598 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
599 } else {
600 if (requireVlanExtensions()) {
601 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
602 selector.extension(ofdpaMatchVlanVid, deviceId);
603 } else {
604 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700605 }
Charles Chan14967c22015-12-07 11:11:50 -0800606
Charles Chand55e84d2016-03-30 17:54:24 -0700607 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700608 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800609 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
610 treatment.extension(ofdpaSetVlanVid, deviceId);
611 } else {
612 treatment.setVlanId(assignedVlan);
613 }
Charles Chand55e84d2016-03-30 17:54:24 -0700614 }
Saurav Das822c4e22015-10-23 10:51:11 -0700615 }
Saurav Das822c4e22015-10-23 10:51:11 -0700616
617 // ofdpa cannot match on ALL portnumber, so we need to use separate
618 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800619 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530620 if (portCriterion != null) {
621 if (PortNumber.ALL.equals(portCriterion.port())) {
622 for (Port port : deviceService.getPorts(deviceId)) {
623 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
624 portnums.add(port.number());
625 }
Saurav Das822c4e22015-10-23 10:51:11 -0700626 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530627 } else {
628 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700629 }
630 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530631 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800632 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700633 }
Saurav Das4f980082015-11-05 13:39:15 -0800634
Saurav Das822c4e22015-10-23 10:51:11 -0700635 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800636 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700637 selector.matchInPort(pnum);
638 FlowRule rule = DefaultFlowRule.builder()
639 .forDevice(deviceId)
640 .withSelector(selector.build())
641 .withTreatment(treatment.build())
642 .withPriority(DEFAULT_PRIORITY)
643 .fromApp(applicationId)
644 .makePermanent()
645 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800646 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800647
648 if (preSelector != null) {
649 preSelector.matchInPort(pnum);
650 FlowRule preRule = DefaultFlowRule.builder()
651 .forDevice(deviceId)
652 .withSelector(preSelector.build())
653 .withTreatment(preTreatment.build())
654 .withPriority(DEFAULT_PRIORITY)
655 .fromApp(applicationId)
656 .makePermanent()
657 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800658 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800659 }
Saurav Das822c4e22015-10-23 10:51:11 -0700660 }
Charles Chan2686dd72018-03-06 22:10:15 -0800661 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700662 }
663
664 /**
665 * Allows routed packets with correct destination MAC to be directed
Charles Chan45b69ab2018-03-02 15:41:41 -0800666 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700667 *
668 * @param portCriterion port on device for which this filter is programmed
669 * @param ethCriterion dstMac of device for which is filter is programmed
670 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700671 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chan45b69ab2018-03-02 15:41:41 -0800672 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
673 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
674 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700675 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800676 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700677
678 */
Charles Chan66291502018-03-02 16:43:28 -0800679 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700680 EthCriterion ethCriterion,
681 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700682 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800683 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700684 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800685 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530686 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800687 return processEthDstOnlyFilter(ethCriterion, applicationId);
688 }
689
Charles Chan5b9df8d2016-03-28 22:21:40 -0700690 // Multicast MAC
691 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800692 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700693 }
694
Saurav Das822c4e22015-10-23 10:51:11 -0700695 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530696 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700697 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700698 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530699 List<FlowRule> rules = new ArrayList<>();
700 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
701 if (vidCriterion != null && requireVlanExtensions()) {
702 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
703 }
Saurav Das822c4e22015-10-23 10:51:11 -0700704 // ofdpa cannot match on ALL portnumber, so we need to use separate
705 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700706 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530707 if (portCriterion != null) {
708 if (PortNumber.ALL.equals(portCriterion.port())) {
709 for (Port port : deviceService.getPorts(deviceId)) {
710 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
711 portnums.add(port.number());
712 }
Saurav Das822c4e22015-10-23 10:51:11 -0700713 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530714 } else {
715 portnums.add(portCriterion.port());
716 }
717 for (PortNumber pnum : portnums) {
718 rules.add(buildTmacRuleForIpv4(ethCriterion,
719 vidCriterion,
720 ofdpaMatchVlanVid,
721 applicationId,
722 pnum));
723 rules.add(buildTmacRuleForMpls(ethCriterion,
724 vidCriterion,
725 ofdpaMatchVlanVid,
726 applicationId,
727 pnum));
728 rules.add(buildTmacRuleForIpv6(ethCriterion,
729 vidCriterion,
730 ofdpaMatchVlanVid,
731 applicationId,
732 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700733 }
734 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530735 rules.add(buildTmacRuleForIpv4(ethCriterion,
736 vidCriterion,
737 ofdpaMatchVlanVid,
738 applicationId,
739 null));
740 rules.add(buildTmacRuleForMpls(ethCriterion,
741 vidCriterion,
742 ofdpaMatchVlanVid,
743 applicationId,
744 null));
745 rules.add(buildTmacRuleForIpv6(ethCriterion,
746 vidCriterion,
747 ofdpaMatchVlanVid,
748 applicationId,
749 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700750 }
Charles Chan66291502018-03-02 16:43:28 -0800751 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700752 }
753
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530754 /**
755 * Builds TMAC rules for IPv4 packets.
756 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800757 * @param ethCriterion dst mac matching
758 * @param vidCriterion vlan id assigned to the port
759 * @param ofdpaMatchVlanVid OFDPA vlan id matching
760 * @param applicationId application id
761 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530762 * @return TMAC rule for IPV4 packets
763 */
764 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
765 VlanIdCriterion vidCriterion,
766 OfdpaMatchVlanVid ofdpaMatchVlanVid,
767 ApplicationId applicationId,
768 PortNumber pnum) {
769 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
770 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
771 if (pnum != null) {
772 if (matchInPortTmacTable()) {
773 selector.matchInPort(pnum);
774 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800775 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530776 "ignoring the IN_PORT criteria");
777 }
778 }
779 if (vidCriterion != null) {
780 if (requireVlanExtensions()) {
781 selector.extension(ofdpaMatchVlanVid, deviceId);
782 } else {
783 selector.matchVlanId(vidCriterion.vlanId());
784 }
785 }
786 selector.matchEthType(Ethernet.TYPE_IPV4);
787 selector.matchEthDst(ethCriterion.mac());
788 treatment.transition(UNICAST_ROUTING_TABLE);
789 return DefaultFlowRule.builder()
790 .forDevice(deviceId)
791 .withSelector(selector.build())
792 .withTreatment(treatment.build())
793 .withPriority(DEFAULT_PRIORITY)
794 .fromApp(applicationId)
795 .makePermanent()
796 .forTable(TMAC_TABLE).build();
797 }
798
799 /**
800 * Builds TMAC rule for MPLS packets.
801 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800802 * @param ethCriterion dst mac matching
803 * @param vidCriterion vlan id assigned to the port
804 * @param ofdpaMatchVlanVid OFDPA vlan id matching
805 * @param applicationId application id
806 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530807 * @return TMAC rule for MPLS packets
808 */
809 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
810 VlanIdCriterion vidCriterion,
811 OfdpaMatchVlanVid ofdpaMatchVlanVid,
812 ApplicationId applicationId,
813 PortNumber pnum) {
814 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
815 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
816 if (pnum != null) {
817 if (matchInPortTmacTable()) {
818 selector.matchInPort(pnum);
819 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800820 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530821 "ignoring the IN_PORT criteria");
822 }
823 }
824 if (vidCriterion != null) {
825 if (requireVlanExtensions()) {
826 selector.extension(ofdpaMatchVlanVid, deviceId);
827 } else {
828 selector.matchVlanId(vidCriterion.vlanId());
829 }
830 }
831 selector.matchEthType(Ethernet.MPLS_UNICAST);
832 selector.matchEthDst(ethCriterion.mac());
833 treatment.transition(MPLS_TABLE_0);
834 return DefaultFlowRule.builder()
835 .forDevice(deviceId)
836 .withSelector(selector.build())
837 .withTreatment(treatment.build())
838 .withPriority(DEFAULT_PRIORITY)
839 .fromApp(applicationId)
840 .makePermanent()
841 .forTable(TMAC_TABLE).build();
842 }
843
844 /**
845 * Builds TMAC rules for IPv6 packets.
846 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800847 * @param ethCriterion dst mac matching
848 * @param vidCriterion vlan id assigned to the port
849 * @param ofdpaMatchVlanVid OFDPA vlan id matching
850 * @param applicationId application id
851 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530852 * @return TMAC rule for IPV6 packets
853 */
854 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
855 VlanIdCriterion vidCriterion,
856 OfdpaMatchVlanVid ofdpaMatchVlanVid,
857 ApplicationId applicationId,
858 PortNumber pnum) {
859 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
860 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
861 if (pnum != null) {
862 if (matchInPortTmacTable()) {
863 selector.matchInPort(pnum);
864 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800865 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530866 "ignoring the IN_PORT criteria");
867 }
868 }
869 if (vidCriterion != null) {
870 if (requireVlanExtensions()) {
871 selector.extension(ofdpaMatchVlanVid, deviceId);
872 } else {
873 selector.matchVlanId(vidCriterion.vlanId());
874 }
875 }
876 selector.matchEthType(Ethernet.TYPE_IPV6);
877 selector.matchEthDst(ethCriterion.mac());
878 treatment.transition(UNICAST_ROUTING_TABLE);
879 return DefaultFlowRule.builder()
880 .forDevice(deviceId)
881 .withSelector(selector.build())
882 .withTreatment(treatment.build())
883 .withPriority(DEFAULT_PRIORITY)
884 .fromApp(applicationId)
885 .makePermanent()
886 .forTable(TMAC_TABLE).build();
887 }
888
Charles Chan66291502018-03-02 16:43:28 -0800889 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700890 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800891 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
892
Charles Chan5270ed02016-01-30 23:22:37 -0800893 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
894 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
895 selector.matchEthType(Ethernet.TYPE_IPV4);
896 selector.matchEthDst(ethCriterion.mac());
897 treatment.transition(UNICAST_ROUTING_TABLE);
898 FlowRule rule = DefaultFlowRule.builder()
899 .forDevice(deviceId)
900 .withSelector(selector.build())
901 .withTreatment(treatment.build())
902 .withPriority(DEFAULT_PRIORITY)
903 .fromApp(applicationId)
904 .makePermanent()
905 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800906 builder.add(rule);
907
908 selector = DefaultTrafficSelector.builder();
909 treatment = DefaultTrafficTreatment.builder();
910 selector.matchEthType(Ethernet.TYPE_IPV6);
911 selector.matchEthDst(ethCriterion.mac());
912 treatment.transition(UNICAST_ROUTING_TABLE);
913 rule = DefaultFlowRule.builder()
914 .forDevice(deviceId)
915 .withSelector(selector.build())
916 .withTreatment(treatment.build())
917 .withPriority(DEFAULT_PRIORITY)
918 .fromApp(applicationId)
919 .makePermanent()
920 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800921 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800922 }
923
Charles Chan66291502018-03-02 16:43:28 -0800924 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700925 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800926 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700927 ApplicationId applicationId) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800928 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
929 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
930 TrafficSelector.Builder selector;
931 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700932 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000933
Charles Chanb4879a52017-10-20 19:09:16 -0700934 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800935 if (requireUnicastBeforeMulticast()) {
936 selector = DefaultTrafficSelector.builder();
937 treatment = DefaultTrafficTreatment.builder();
938 selector.matchEthType(Ethernet.TYPE_IPV4);
939 selector.matchEthDst(unicastMac);
940 selector.matchVlanId(assignedVlan);
941 treatment.transition(UNICAST_ROUTING_TABLE);
942 rule = DefaultFlowRule.builder()
943 .forDevice(deviceId)
944 .withSelector(selector.build())
945 .withTreatment(treatment.build())
946 .withPriority(DEFAULT_PRIORITY)
947 .fromApp(applicationId)
948 .makePermanent()
949 .forTable(TMAC_TABLE).build();
950 unicastFlows.add(rule);
951 }
952
953 selector = DefaultTrafficSelector.builder();
954 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700955 selector.matchEthType(Ethernet.TYPE_IPV4);
956 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
957 selector.matchVlanId(assignedVlan);
958 treatment.transition(MULTICAST_ROUTING_TABLE);
959 rule = DefaultFlowRule.builder()
960 .forDevice(deviceId)
961 .withSelector(selector.build())
962 .withTreatment(treatment.build())
963 .withPriority(DEFAULT_PRIORITY)
964 .fromApp(applicationId)
965 .makePermanent()
966 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -0800967 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700968 }
969
970 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800971 if (requireUnicastBeforeMulticast()) {
972 selector = DefaultTrafficSelector.builder();
973 treatment = DefaultTrafficTreatment.builder();
974 selector.matchEthType(Ethernet.TYPE_IPV6);
975 selector.matchEthDst(unicastMac);
976 selector.matchVlanId(assignedVlan);
977 treatment.transition(UNICAST_ROUTING_TABLE);
978 rule = DefaultFlowRule.builder()
979 .forDevice(deviceId)
980 .withSelector(selector.build())
981 .withTreatment(treatment.build())
982 .withPriority(DEFAULT_PRIORITY)
983 .fromApp(applicationId)
984 .makePermanent()
985 .forTable(TMAC_TABLE).build();
986 unicastFlows.add(rule);
987 }
988
Charles Chanb4879a52017-10-20 19:09:16 -0700989 selector = DefaultTrafficSelector.builder();
990 treatment = DefaultTrafficTreatment.builder();
991 selector.matchEthType(Ethernet.TYPE_IPV6);
992 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
993 selector.matchVlanId(assignedVlan);
994 treatment.transition(MULTICAST_ROUTING_TABLE);
995 rule = DefaultFlowRule.builder()
996 .forDevice(deviceId)
997 .withSelector(selector.build())
998 .withTreatment(treatment.build())
999 .withPriority(DEFAULT_PRIORITY)
1000 .fromApp(applicationId)
1001 .makePermanent()
1002 .forTable(TMAC_TABLE).build();
Charles Chan45b69ab2018-03-02 15:41:41 -08001003 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001004 }
Charles Chan45b69ab2018-03-02 15:41:41 -08001005 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001006 }
1007
Saurav Das822c4e22015-10-23 10:51:11 -07001008 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1009 switch (fwd.flag()) {
1010 case SPECIFIC:
1011 return processSpecific(fwd);
1012 case VERSATILE:
1013 return processVersatile(fwd);
1014 default:
1015 fail(fwd, ObjectiveError.UNKNOWN);
1016 log.warn("Unknown forwarding flag {}", fwd.flag());
1017 }
1018 return Collections.emptySet();
1019 }
1020
1021 /**
1022 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1023 * ACL table.
1024 * @param fwd the forwarding objective of type 'versatile'
1025 * @return a collection of flow rules to be sent to the switch. An empty
1026 * collection may be returned if there is a problem in processing
1027 * the flow rule
1028 */
Saurav Das52025962016-01-28 22:30:01 -08001029 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001030 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001031 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001032
1033 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -08001034 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -07001035 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001036 log.error("Versatile forwarding objective:{} must include ethType",
1037 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001038 fail(fwd, ObjectiveError.BADPARAMS);
1039 return Collections.emptySet();
1040 }
1041 if (fwd.nextId() == null && fwd.treatment() == null) {
1042 log.error("Forwarding objective {} from {} must contain "
1043 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001044 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001045 return Collections.emptySet();
1046 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001047
Saurav Das77b5e902016-01-27 17:01:59 -08001048 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1049 fwd.selector().criteria().forEach(criterion -> {
1050 if (criterion instanceof VlanIdCriterion) {
1051 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1052 // ensure that match does not include vlan = NONE as OF-DPA does not
1053 // match untagged packets this way in the ACL table.
1054 if (vlanId.equals(VlanId.NONE)) {
1055 return;
1056 }
Charles Chand1172632017-03-15 17:33:09 -07001057 if (requireVlanExtensions()) {
1058 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1059 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1060 } else {
1061 sbuilder.matchVlanId(vlanId);
1062 }
Charles Chan09bf2692018-01-11 11:48:18 -08001063 } else if (criterion instanceof Icmpv6TypeCriterion) {
1064 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1065 sbuilder.matchIcmpv6Type(icmpv6Type);
1066 } else if (criterion instanceof Icmpv6CodeCriterion) {
1067 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1068 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001069 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1070 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1071 // Currently L4 dst port matching is only used by DHCP relay feature
1072 // and therefore is safe to be replaced with L4 src port matching.
1073 // We need to revisit this if L4 dst port is used for other purpose in the future.
1074 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001075 switch (criterion.type()) {
1076 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001077 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001078 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001079 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001080 break;
1081 default:
1082 sbuilder.add(criterion);
1083 }
1084 } else {
1085 sbuilder.add(criterion);
1086 }
Saurav Das77b5e902016-01-27 17:01:59 -08001087 } else {
1088 sbuilder.add(criterion);
1089 }
1090 });
1091
Saurav Das822c4e22015-10-23 10:51:11 -07001092 // XXX driver does not currently do type checking as per Tables 65-67 in
1093 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001094 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1095 if (fwd.treatment() != null) {
1096 for (Instruction ins : fwd.treatment().allInstructions()) {
1097 if (ins instanceof OutputInstruction) {
1098 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001099 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001100 ttBuilder.add(o);
1101 } else {
1102 log.warn("Only allowed treatments in versatile forwarding "
1103 + "objectives are punts to the controller");
1104 }
1105 } else {
1106 log.warn("Cannot process instruction in versatile fwd {}", ins);
1107 }
Saurav Das822c4e22015-10-23 10:51:11 -07001108 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001109 if (fwd.treatment().clearedDeferred()) {
1110 ttBuilder.wipeDeferred();
1111 }
Saurav Das822c4e22015-10-23 10:51:11 -07001112 }
Saurav Das822c4e22015-10-23 10:51:11 -07001113 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001114 // overide case
1115 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301116 if (next == null) {
1117 fail(fwd, ObjectiveError.BADPARAMS);
1118 return Collections.emptySet();
1119 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001120 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1121 // we only need the top level group's key to point the flow to it
1122 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1123 if (group == null) {
1124 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1125 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1126 fail(fwd, ObjectiveError.GROUPMISSING);
1127 return Collections.emptySet();
1128 }
1129 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001130 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001131
1132 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1133 .fromApp(fwd.appId())
1134 .withPriority(fwd.priority())
1135 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001136 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001137 .withTreatment(ttBuilder.build())
1138 .makePermanent()
1139 .forTable(ACL_TABLE);
1140 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001141 }
1142
1143 /**
1144 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001145 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001146 *
1147 * @param fwd the forwarding objective of type 'specific'
1148 * @return a collection of flow rules. Typically there will be only one
1149 * for this type of forwarding objective. An empty set may be
1150 * returned if there is an issue in processing the objective.
1151 */
Charles Chan50d900c2018-03-02 13:26:22 -08001152 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001153 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001154 fwd.id(), deviceId, fwd.nextId());
1155 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1156 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1157
1158 if (isEthTypeObj) {
1159 return processEthTypeSpecific(fwd);
1160 } else if (isEthDstObj) {
1161 return processEthDstSpecific(fwd);
1162 } else {
1163 log.warn("processSpecific: Unsupported forwarding objective "
1164 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001165 fail(fwd, ObjectiveError.UNSUPPORTED);
1166 return Collections.emptySet();
1167 }
Saurav Das4ce45962015-11-24 23:21:05 -08001168 }
1169
Saurav Das4ce45962015-11-24 23:21:05 -08001170 /**
1171 * Handles forwarding rules to the IP and MPLS tables.
1172 *
1173 * @param fwd the forwarding objective
1174 * @return A collection of flow rules, or an empty set
1175 */
1176 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001177 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001178 }
1179
1180 /**
1181 * Internal implementation of processEthTypeSpecific.
1182 * <p>
1183 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1184 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1185 * The allowDefaultRoute must be set to false for OFDPA i12.
1186 * </p>
1187 *
1188 * @param fwd the forwarding objective
1189 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001190 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001191 * @return A collection of flow rules, or an empty set
1192 */
1193 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001194 boolean allowDefaultRoute,
1195 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001196 TrafficSelector selector = fwd.selector();
1197 EthTypeCriterion ethType =
1198 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001199 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001200 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001201 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001202 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001203 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001204
Saurav Das8a0732e2015-11-20 15:27:53 -08001205 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001206 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1207 return Collections.emptyList();
1208 }
1209 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001210 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001211 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001212 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001213 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001214 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001215 }
Charles Chan50d900c2018-03-02 13:26:22 -08001216
Charles Chan236653d2018-03-05 11:28:23 -08001217 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001218 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001219 // if (fwd.treatment() != null) {
1220 // for (Instruction instr : fwd.treatment().allInstructions()) {
1221 // if (instr instanceof L3ModificationInstruction &&
1222 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1223 // tb.deferred().add(instr);
1224 // }
1225 // }
1226 // }
1227
Pier Ventree0ae7a32016-11-23 09:57:42 -08001228 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1229 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1230 return Collections.emptyList();
1231 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001232 //We need to set the proper next table
1233 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1234 if (ipv6Dst.isMulticast()) {
1235 forTableId = MULTICAST_ROUTING_TABLE;
1236 } else {
1237 forTableId = UNICAST_ROUTING_TABLE;
1238 }
1239
Charles Chan236653d2018-03-05 11:28:23 -08001240 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan50d900c2018-03-02 13:26:22 -08001241 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan236653d2018-03-05 11:28:23 -08001242 // if (fwd.treatment() != null) {
1243 // for (Instruction instr : fwd.treatment().allInstructions()) {
1244 // if (instr instanceof L3ModificationInstruction &&
1245 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1246 // tb.deferred().add(instr);
1247 // }
1248 // }
1249 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001250 } else {
1251 filteredSelector
1252 .matchEthType(Ethernet.MPLS_UNICAST)
1253 .matchMplsLabel(((MplsCriterion)
1254 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1255 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001256 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001257 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001258 filteredSelector.matchMplsBos(bos.mplsBos());
1259 }
1260 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001261 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1262 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001263
Charles Chan14967c22015-12-07 11:11:50 -08001264 if (fwd.treatment() != null) {
1265 for (Instruction instr : fwd.treatment().allInstructions()) {
1266 if (instr instanceof L2ModificationInstruction &&
1267 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001268 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001269 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001270 if (requireMplsPop()) {
1271 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1272 tb.immediate().popMpls();
1273 }
1274 } else {
1275 // Skip mpls pop action for mpls_unicast label
1276 if (instr instanceof ModMplsHeaderInstruction &&
1277 !((ModMplsHeaderInstruction) instr).ethernetType()
1278 .equals(EtherType.MPLS_UNICAST.ethType())) {
1279 tb.immediate().add(instr);
1280 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001281 }
Charles Chan14967c22015-12-07 11:11:50 -08001282 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001283
1284 if (requireMplsTtlModification()) {
1285 if (instr instanceof L3ModificationInstruction &&
1286 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1287 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1288 tb.immediate().decMplsTtl();
1289 }
1290 if (instr instanceof L3ModificationInstruction &&
1291 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1292 tb.immediate().add(instr);
1293 }
Charles Chan14967c22015-12-07 11:11:50 -08001294 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001295 }
1296 }
1297 }
Saurav Das822c4e22015-10-23 10:51:11 -07001298
1299 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001300 NextGroup next = getGroupForNextObjective(fwd.nextId());
1301 if (next != null) {
1302 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1303 // we only need the top level group's key to point the flow to it
1304 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1305 if (group == null) {
1306 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1307 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1308 fail(fwd, ObjectiveError.GROUPMISSING);
1309 return Collections.emptySet();
1310 }
Saurav Dasa4020382018-02-14 14:14:54 -08001311 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1312 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1313 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1314 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1315 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1316 return Collections.emptySet();
1317 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001318 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001319 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001320 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001321 if (shouldRetry()) {
1322 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1323 Integer.toHexString(group.id().id()), deviceId,
1324 fwd.id());
1325 emptyGroup = true;
1326 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001327 }
Saurav Das25190812016-05-27 13:54:07 -07001328 } else {
1329 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1330 fwd.nextId(), deviceId, fwd.id());
1331 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1332 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001333 }
Saurav Das822c4e22015-10-23 10:51:11 -07001334 }
Charles Chancad338a2016-09-16 18:03:11 -07001335
1336 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001337 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001338 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001339 // set mpls type as apply_action
1340 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001341 }
1342 tb.transition(mplsNextTable);
1343 } else {
1344 tb.transition(ACL_TABLE);
1345 }
1346
Saurav Das822c4e22015-10-23 10:51:11 -07001347 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1348 .fromApp(fwd.appId())
1349 .withPriority(fwd.priority())
1350 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001351 .withSelector(filteredSelector.build())
1352 .withTreatment(tb.build())
1353 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001354
1355 if (fwd.permanent()) {
1356 ruleBuilder.makePermanent();
1357 } else {
1358 ruleBuilder.makeTemporary(fwd.timeout());
1359 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001360 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1361 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001362 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001363 flowRuleCollection.add(
1364 defaultRoute(fwd, complementarySelector, forTableId, tb)
1365 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001366 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1367 }
Saurav Dasc568c342018-01-25 09:49:01 -08001368
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001369 if (emptyGroup) {
1370 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1371 RETRY_MS, TimeUnit.MILLISECONDS);
1372 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001373 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001374 }
1375
Charles Chan50d900c2018-03-02 13:26:22 -08001376 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001377 TrafficSelector.Builder extBuilder,
1378 ForwardingObjective fwd,
1379 boolean allowDefaultRoute) {
1380 TrafficSelector selector = fwd.selector();
1381
1382 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1383 if (ipv4Dst.isMulticast()) {
1384 if (ipv4Dst.prefixLength() != 32) {
1385 log.warn("Multicast specific forwarding objective can only be /32");
1386 fail(fwd, ObjectiveError.BADPARAMS);
1387 return -1;
1388 }
1389 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1390 if (assignedVlan == null) {
1391 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1392 fail(fwd, ObjectiveError.BADPARAMS);
1393 return -1;
1394 }
Charles Chand1172632017-03-15 17:33:09 -07001395 if (requireVlanExtensions()) {
1396 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1397 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1398 } else {
1399 builderToUpdate.matchVlanId(assignedVlan);
1400 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001401 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1402 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1403 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1404 } else {
1405 if (ipv4Dst.prefixLength() == 0) {
1406 if (allowDefaultRoute) {
1407 // The entire IPV4_DST field is wildcarded intentionally
1408 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1409 } else {
1410 // NOTE: The switch does not support matching 0.0.0.0/0
1411 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1412 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1413 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1414 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1415 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1416 }
1417 } else {
1418 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1419 }
1420 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1421 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1422 }
1423 return 0;
1424 }
1425
1426 /**
1427 * Helper method to build Ipv6 selector using the selector provided by
1428 * a forwarding objective.
1429 *
1430 * @param builderToUpdate the builder to update
1431 * @param fwd the selector to read
1432 * @return 0 if the update ends correctly. -1 if the matches
1433 * are not yet supported
1434 */
Charles Chan50d900c2018-03-02 13:26:22 -08001435 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001436 ForwardingObjective fwd) {
1437
1438 TrafficSelector selector = fwd.selector();
1439
1440 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1441 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001442 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1443 log.warn("Multicast specific forwarding objective can only be /128");
1444 fail(fwd, ObjectiveError.BADPARAMS);
1445 return -1;
1446 }
1447 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1448 if (assignedVlan == null) {
1449 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1450 fail(fwd, ObjectiveError.BADPARAMS);
1451 return -1;
1452 }
1453 if (requireVlanExtensions()) {
1454 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1455 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1456 } else {
1457 builderToUpdate.matchVlanId(assignedVlan);
1458 }
1459 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1460 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1461 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1462 } else {
1463 if (ipv6Dst.prefixLength() != 0) {
1464 builderToUpdate.matchIPv6Dst(ipv6Dst);
1465 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001466 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1467 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1468 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001469 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001470 return 0;
1471 }
1472
Charles Chan50d900c2018-03-02 13:26:22 -08001473 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001474 TrafficSelector.Builder complementarySelector,
1475 int forTableId,
1476 TrafficTreatment.Builder tb) {
1477 FlowRule.Builder rule = DefaultFlowRule.builder()
1478 .fromApp(fwd.appId())
1479 .withPriority(fwd.priority())
1480 .forDevice(deviceId)
1481 .withSelector(complementarySelector.build())
1482 .withTreatment(tb.build())
1483 .forTable(forTableId);
1484 if (fwd.permanent()) {
1485 rule.makePermanent();
1486 } else {
1487 rule.makeTemporary(fwd.timeout());
1488 }
1489 return rule.build();
1490 }
1491
Saurav Das4ce45962015-11-24 23:21:05 -08001492 /**
1493 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1494 * allowed in the bridging table - instead we use L2 Interface group or
1495 * L2 flood group
1496 *
1497 * @param fwd the forwarding objective
1498 * @return A collection of flow rules, or an empty set
1499 */
1500 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1501 List<FlowRule> rules = new ArrayList<>();
1502
1503 // Build filtered selector
1504 TrafficSelector selector = fwd.selector();
1505 EthCriterion ethCriterion = (EthCriterion) selector
1506 .getCriterion(Criterion.Type.ETH_DST);
1507 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1508 .getCriterion(Criterion.Type.VLAN_VID);
1509
1510 if (vlanIdCriterion == null) {
1511 log.warn("Forwarding objective for bridging requires vlan. Not "
1512 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1513 fail(fwd, ObjectiveError.BADPARAMS);
1514 return Collections.emptySet();
1515 }
1516
1517 TrafficSelector.Builder filteredSelectorBuilder =
1518 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001519
1520 if (!ethCriterion.mac().equals(NONE) &&
1521 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001522 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1523 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1524 fwd.id(), fwd.nextId(), deviceId);
1525 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001526 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001527 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1528 + "in dev:{} for vlan:{}",
1529 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1530 }
Charles Chand1172632017-03-15 17:33:09 -07001531 if (requireVlanExtensions()) {
1532 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1533 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1534 } else {
1535 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1536 }
Saurav Das4ce45962015-11-24 23:21:05 -08001537 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1538
1539 if (fwd.treatment() != null) {
1540 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1541 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1542 }
1543
1544 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1545 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001546 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001547 if (next != null) {
1548 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1549 // we only need the top level group's key to point the flow to it
1550 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1551 if (group != null) {
1552 treatmentBuilder.deferred().group(group.id());
1553 } else {
1554 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1555 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1556 fail(fwd, ObjectiveError.GROUPMISSING);
1557 return Collections.emptySet();
1558 }
1559 }
1560 }
1561 treatmentBuilder.immediate().transition(ACL_TABLE);
1562 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1563
1564 // Build bridging table entries
1565 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1566 flowRuleBuilder.fromApp(fwd.appId())
1567 .withPriority(fwd.priority())
1568 .forDevice(deviceId)
1569 .withSelector(filteredSelector)
1570 .withTreatment(filteredTreatment)
1571 .forTable(BRIDGING_TABLE);
1572 if (fwd.permanent()) {
1573 flowRuleBuilder.makePermanent();
1574 } else {
1575 flowRuleBuilder.makeTemporary(fwd.timeout());
1576 }
1577 rules.add(flowRuleBuilder.build());
1578 return rules;
1579 }
1580
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001581 //////////////////////////////////////
1582 // Helper Methods and Classes
1583 //////////////////////////////////////
1584
1585 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1586 TrafficSelector selector = fwd.selector();
1587 EthTypeCriterion ethType = (EthTypeCriterion) selector
1588 .getCriterion(Criterion.Type.ETH_TYPE);
1589 return !((ethType == null) ||
1590 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001591 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1592 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001593 }
1594
1595 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1596 TrafficSelector selector = fwd.selector();
1597 EthCriterion ethDst = (EthCriterion) selector
1598 .getCriterion(Criterion.Type.ETH_DST);
1599 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1600 .getCriterion(Criterion.Type.VLAN_VID);
1601 return !(ethDst == null && vlanId == null);
1602 }
1603
Charles Chan50d900c2018-03-02 13:26:22 -08001604 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001605 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1606 if (next != null) {
1607 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1608 if (gkeys != null && !gkeys.isEmpty()) {
1609 return next;
1610 } else {
1611 log.warn("Empty next group found in FlowObjective store for "
1612 + "next-id:{} in dev:{}", nextId, deviceId);
1613 }
1614 } else {
1615 log.warn("next-id {} not found in Flow objective store for dev:{}",
1616 nextId, deviceId);
1617 }
1618 return null;
1619 }
1620
Charles Chan188ebf52015-12-23 00:15:11 -08001621 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001622 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001623 }
1624
Charles Chan188ebf52015-12-23 00:15:11 -08001625 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001626 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001627 }
Saurav Das24431192016-03-07 19:13:00 -08001628
Saurav Das24431192016-03-07 19:13:00 -08001629 @Override
1630 public List<String> getNextMappings(NextGroup nextGroup) {
1631 List<String> mappings = new ArrayList<>();
1632 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1633 for (Deque<GroupKey> gkd : gkeys) {
1634 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001635 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001636 for (GroupKey gk : gkd) {
1637 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001638 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001639 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001640 continue;
1641 }
1642 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1643 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001644 lastGroup = g;
1645 }
1646 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001647 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001648 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001649 lastGroupIns = lastGroup.buckets().buckets().get(0)
1650 .treatment().allInstructions();
1651 }
1652 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001653 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001654 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001655 }
1656 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001657 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001658 }
1659 return mappings;
1660 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001661
Saurav Dasa4020382018-02-14 14:14:54 -08001662 /**
1663 * Returns true iff the given selector matches on BOS==true, indicating that
1664 * the selector is trying to match on a label that is bottom-of-stack.
1665 *
1666 * @param selector the given match
1667 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1668 * expressed in the given selector
1669 */
Pier Ventre140a8942016-11-02 07:26:38 -07001670 static boolean isMplsBos(TrafficSelector selector) {
1671 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1672 return bosCriterion != null && bosCriterion.mplsBos();
1673 }
1674
Saurav Dasa4020382018-02-14 14:14:54 -08001675 /**
1676 * Returns true iff the given selector matches on BOS==false, indicating
1677 * that the selector is trying to match on a label that is not the
1678 * bottom-of-stack label.
1679 *
1680 * @param selector the given match
1681 * @return true iff BoS==false;
1682 * false if BOS==true, or BOS matching is not expressed in the given selector
1683 */
Pier Ventre140a8942016-11-02 07:26:38 -07001684 static boolean isNotMplsBos(TrafficSelector selector) {
1685 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1686 return bosCriterion != null && !bosCriterion.mplsBos();
1687 }
1688
Saurav Dasa4020382018-02-14 14:14:54 -08001689 /**
1690 * Returns true iff the forwarding objective includes a treatment to pop the
1691 * MPLS label.
1692 *
1693 * @param fwd the given forwarding objective
1694 * @return true iff mpls pop treatment exists
1695 */
1696 static boolean isMplsPop(ForwardingObjective fwd) {
1697 if (fwd.treatment() != null) {
1698 for (Instruction instr : fwd.treatment().allInstructions()) {
1699 if (instr instanceof L2ModificationInstruction
1700 && ((L2ModificationInstruction) instr)
1701 .subtype() == L2SubType.MPLS_POP) {
1702 return true;
1703 }
1704 }
1705 }
1706 return false;
1707 }
1708
Charles Chand9e47c62017-10-05 15:17:15 -07001709 private static boolean isIpv6(TrafficSelector selector) {
1710 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1711 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1712 }
1713
Charles Chan50d900c2018-03-02 13:26:22 -08001714 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001715 if (selector == null) {
1716 return null;
1717 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001718 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1719 return (criterion == null)
1720 ? null : ((VlanIdCriterion) criterion).vlanId();
1721 }
1722
Charles Chand9e47c62017-10-05 15:17:15 -07001723 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001724 if (selector == null) {
1725 return null;
1726 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001727 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1728 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1729 }
Charles Chand55e84d2016-03-30 17:54:24 -07001730
1731 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001732 if (treatment == null) {
1733 return null;
1734 }
Charles Chand55e84d2016-03-30 17:54:24 -07001735 for (Instruction i : treatment.allInstructions()) {
1736 if (i instanceof ModVlanIdInstruction) {
1737 return ((ModVlanIdInstruction) i).vlanId();
1738 }
1739 }
1740 return null;
1741 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001742
Charles Chan45b69ab2018-03-02 15:41:41 -08001743 private static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
1744 if (treatment == null) {
1745 return null;
1746 }
1747 for (Instruction i : treatment.allInstructions()) {
1748 if (i instanceof ModEtherInstruction) {
1749 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1750 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1751 return modEtherInstruction.mac();
1752 }
1753 }
1754 }
1755 return null;
1756 }
1757
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001758 /**
1759 * Utility class that retries sending flows a fixed number of times, even if
1760 * some of the attempts are successful. Used only for forwarding objectives.
1761 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001762 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001763 int attempts = MAX_RETRY_ATTEMPTS;
1764 private Collection<FlowRule> retryFlows;
1765 private ForwardingObjective fwd;
1766
Charles Chan50d900c2018-03-02 13:26:22 -08001767 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001768 this.fwd = fwd;
1769 this.retryFlows = retryFlows;
1770 }
1771
1772 @Override
1773 public void run() {
1774 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1775 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1776 sendForward(fwd, retryFlows);
1777 if (--attempts > 0) {
1778 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1779 }
1780 }
1781 }
1782
Saurav Das822c4e22015-10-23 10:51:11 -07001783}