blob: a07655c7fa6137d5f1cc30023798a25954e55a52 [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;
Saurav Das822c4e22015-10-23 10:51:11 -070025import org.onlab.packet.VlanId;
26import org.onlab.util.KryoNamespace;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070029import org.onosproject.driver.extensions.Ofdpa3MplsType;
30import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080031import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
32import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070033import org.onosproject.net.DeviceId;
34import org.onosproject.net.Port;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.behaviour.NextGroup;
37import org.onosproject.net.behaviour.Pipeliner;
38import org.onosproject.net.behaviour.PipelinerContext;
39import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.driver.AbstractHandlerBehaviour;
41import org.onosproject.net.flow.DefaultFlowRule;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.FlowRuleOperations;
46import org.onosproject.net.flow.FlowRuleOperationsContext;
47import org.onosproject.net.flow.FlowRuleService;
48import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.flow.criteria.Criteria;
51import org.onosproject.net.flow.criteria.Criterion;
52import org.onosproject.net.flow.criteria.EthCriterion;
53import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080054import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070055import 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;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020069import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080070import org.onosproject.net.flow.instructions.L3ModificationInstruction;
71import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070072import org.onosproject.net.flowobjective.FilteringObjective;
73import org.onosproject.net.flowobjective.FlowObjectiveStore;
74import org.onosproject.net.flowobjective.ForwardingObjective;
75import org.onosproject.net.flowobjective.NextObjective;
76import org.onosproject.net.flowobjective.Objective;
77import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.net.group.DefaultGroupKey;
79import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.store.serializers.KryoNamespaces;
83import org.slf4j.Logger;
84
Pier Ventree0ae7a32016-11-23 09:57:42 -080085import java.util.ArrayDeque;
86import java.util.ArrayList;
87import java.util.Collection;
88import java.util.Collections;
89import java.util.Deque;
90import java.util.List;
91import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080092import java.util.concurrent.ScheduledExecutorService;
93import java.util.concurrent.TimeUnit;
94
95import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080096import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -070097import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
98import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080099import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800100import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700101import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700102import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100103import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800104import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700105import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800106
Saurav Das822c4e22015-10-23 10:51:11 -0700107/**
108 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700109 */
Charles Chan361154b2016-03-24 10:23:39 -0700110public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700111 protected static final int PORT_TABLE = 0;
112 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800113 protected static final int VLAN_1_TABLE = 11;
114 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
115 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700116 protected static final int TMAC_TABLE = 20;
117 protected static final int UNICAST_ROUTING_TABLE = 30;
118 protected static final int MULTICAST_ROUTING_TABLE = 40;
119 protected static final int MPLS_TABLE_0 = 23;
120 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700121 protected static final int MPLS_L3_TYPE_TABLE = 27;
122 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700123 protected static final int BRIDGING_TABLE = 50;
124 protected static final int ACL_TABLE = 60;
125 protected static final int MAC_LEARNING_TABLE = 254;
126 protected static final long OFPP_MAX = 0xffffff00L;
127
Saurav Das52025962016-01-28 22:30:01 -0800128 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800129 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700130 protected static final int LOWEST_PRIORITY = 0x0;
131
Pier Ventre42287df2016-11-09 14:17:26 -0800132 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800133 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
134 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
135
Pier Ventre70d53ba2016-11-17 22:26:29 -0800136 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800137 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
138 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
139
Saurav Das822c4e22015-10-23 10:51:11 -0700140 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700141 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700142 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700143 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800144 protected GroupService groupService;
145 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700146 protected DeviceId deviceId;
147 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700148 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800149 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700150 .register(KryoNamespaces.API)
151 .register(GroupKey.class)
152 .register(DefaultGroupKey.class)
153 .register(OfdpaNextGroup.class)
154 .register(ArrayDeque.class)
155 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700156
Charles Chan425854b2016-04-11 15:32:12 -0700157 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700158
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700159 // flows installations to be retried
Charles Chan50d900c2018-03-02 13:26:22 -0800160 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700161 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800162 private static final int MAX_RETRY_ATTEMPTS = 10;
163 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800164
Saurav Das822c4e22015-10-23 10:51:11 -0700165 @Override
166 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700167 this.deviceId = deviceId;
168
Charles Chan425854b2016-04-11 15:32:12 -0700169 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700170 coreService = serviceDirectory.get(CoreService.class);
171 flowRuleService = serviceDirectory.get(FlowRuleService.class);
172 groupService = serviceDirectory.get(GroupService.class);
173 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700174 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700175
Charles Chan40132b32017-01-22 00:19:37 -0800176 initDriverId();
177 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700178
Saurav Das822c4e22015-10-23 10:51:11 -0700179 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700180 }
181
Charles Chan40132b32017-01-22 00:19:37 -0800182 protected void initDriverId() {
183 driverId = coreService.registerApplication(
184 "org.onosproject.driver.Ofdpa2Pipeline");
185 }
186
187 protected void initGroupHander(PipelinerContext context) {
188 groupHandler = new Ofdpa2GroupHandler();
189 groupHandler.init(deviceId, context);
190 }
191
Saurav Das822c4e22015-10-23 10:51:11 -0700192 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800193 // OF-DPA does not require initializing the pipeline as it puts default
194 // rules automatically in the hardware. However emulation of OFDPA in
195 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700196 }
197
Charles Chand1172632017-03-15 17:33:09 -0700198 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200199 * Determines whether this pipeline requires MPLS POP instruction.
200 *
201 * @return true to use MPLS POP instruction
202 */
203 public boolean requireMplsPop() {
204 return true;
205 }
206
207 /**
208 * Determines whether this pipeline requires MPLS BOS match.
209 *
210 * @return true to use MPLS BOS match
211 */
212 public boolean requireMplsBosMatch() {
213 return true;
214 }
215
216 /**
217 * Determines whether this pipeline requires MPLS TTL decrement and copy.
218 *
219 * @return true to use MPLS TTL decrement and copy
220 */
221 public boolean requireMplsTtlModification() {
222 return true;
223 }
224
225 /**
Charles Chand1172632017-03-15 17:33:09 -0700226 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
227 *
228 * @return true to use the extensions
229 */
230 protected boolean requireVlanExtensions() {
231 return true;
232 }
233
Saurav Das86d13e82017-04-28 17:03:48 -0700234 /**
235 * Determines whether in-port should be matched on in TMAC table rules.
236 *
237 * @return true if match on in-port should be programmed
238 */
239 protected boolean matchInPortTmacTable() {
240 return true;
241 }
242
Charles Chand9e47c62017-10-05 15:17:15 -0700243 /**
244 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
245 *
246 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
247 */
248 protected boolean supportIpv6L4Dst() {
249 return true;
250 }
251
Saurav Dasc568c342018-01-25 09:49:01 -0800252 /**
253 * Determines whether this driver should continue to retry flows that point
254 * to empty groups. See CORD-554.
255 *
256 * @return true if the driver should retry flows
257 */
258 protected boolean shouldRetry() {
259 return true;
260 }
261
Saurav Das822c4e22015-10-23 10:51:11 -0700262 //////////////////////////////////////
263 // Flow Objectives
264 //////////////////////////////////////
265
266 @Override
267 public void filter(FilteringObjective filteringObjective) {
268 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
269 processFilter(filteringObjective,
270 filteringObjective.op() == Objective.Operation.ADD,
271 filteringObjective.appId());
272 } else {
273 // Note that packets that don't match the PERMIT filter are
274 // automatically denied. The DENY filter is used to deny packets
275 // that are otherwise permitted by the PERMIT filter.
276 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530277 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700278 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
279 }
280 }
281
282 @Override
283 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700284 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700285 if (rules == null || rules.isEmpty()) {
286 // Assumes fail message has already been generated to the objective
287 // context. Returning here prevents spurious pass message to be
288 // generated by FlowRule service for empty flowOps.
289 return;
290 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700291 sendForward(fwd, rules);
292 }
293
Charles Chan50d900c2018-03-02 13:26:22 -0800294 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700295 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700296 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700297 case ADD:
298 rules.stream()
299 .filter(Objects::nonNull)
300 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800301 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700302 break;
303 case REMOVE:
304 rules.stream()
305 .filter(Objects::nonNull)
306 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800307 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700308 break;
309 default:
310 fail(fwd, ObjectiveError.UNKNOWN);
311 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700312 }
313
Saurav Das822c4e22015-10-23 10:51:11 -0700314 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
315 @Override
316 public void onSuccess(FlowRuleOperations ops) {
317 pass(fwd);
318 }
319
320 @Override
321 public void onError(FlowRuleOperations ops) {
322 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
323 }
324 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700325 }
326
327 @Override
328 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800329 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
330 switch (nextObjective.op()) {
331 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800332 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800333 log.warn("Cannot add next {} that already exists in device {}",
334 nextObjective.id(), deviceId);
335 return;
336 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700337 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800338 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700339 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800340 break;
341 case ADD_TO_EXISTING:
342 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700343 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800344 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700345 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800346 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800347 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800348 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
349 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800350
351 // by design multiple pending bucket is allowed for the group
352 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
353 if (pendBkts == null) {
354 pendBkts = Sets.newHashSet();
355 }
356 pendBkts.add(nextObjective);
357 return pendBkts;
358 });
Saurav Das4f980082015-11-05 13:39:15 -0800359 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800360 break;
361 case REMOVE:
362 if (nextGroup == null) {
363 log.warn("Cannot remove next {} that does not exist in device {}",
364 nextObjective.id(), deviceId);
365 return;
366 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700367 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800368 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700369 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800370 break;
371 case REMOVE_FROM_EXISTING:
372 if (nextGroup == null) {
373 log.warn("Cannot remove from next {} that does not exist in device {}",
374 nextObjective.id(), deviceId);
375 return;
376 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700377 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800378 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700379 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800380 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900381 case MODIFY:
382 if (nextGroup == null) {
383 log.warn("Cannot modify next {} that does not exist in device {}",
384 nextObjective.id(), deviceId);
385 return;
386 }
387 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
388 nextObjective.id(), deviceId);
389 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
390 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700391 case VERIFY:
392 if (nextGroup == null) {
393 log.warn("Cannot verify next {} that does not exist in device {}",
394 nextObjective.id(), deviceId);
395 return;
396 }
397 log.debug("Processing NextObjective id {} in dev {} - verify",
398 nextObjective.id(), deviceId);
399 groupHandler.verifyGroup(nextObjective, nextGroup);
400 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800401 default:
Saurav Das4f980082015-11-05 13:39:15 -0800402 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700403 }
404 }
405
406 //////////////////////////////////////
407 // Flow handling
408 //////////////////////////////////////
409
410 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700411 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
412 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700413 *
414 * @param filt the filtering objective
415 * @param install indicates whether to add or remove the objective
416 * @param applicationId the application that sent this objective
417 */
Saurav Das52025962016-01-28 22:30:01 -0800418 protected void processFilter(FilteringObjective filt,
419 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700420 // This driver only processes filtering criteria defined with switch
421 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530422 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700423 EthCriterion ethCriterion = null;
424 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700425 if (!filt.key().equals(Criteria.dummy()) &&
426 filt.key().type() == Criterion.Type.IN_PORT) {
427 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700428 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530429 if (portCriterion == null) {
430 log.debug("No IN_PORT defined in filtering objective from app: {}",
431 applicationId);
432 } else {
433 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
434 portCriterion.port());
435 }
Saurav Das822c4e22015-10-23 10:51:11 -0700436 // convert filtering conditions for switch-intfs into flowrules
437 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
438 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700439 switch (criterion.type()) {
440 case ETH_DST:
441 case ETH_DST_MASKED:
442 ethCriterion = (EthCriterion) criterion;
443 break;
444 case VLAN_VID:
445 vidCriterion = (VlanIdCriterion) criterion;
446 break;
447 default:
448 log.warn("Unsupported filter {}", criterion);
449 fail(filt, ObjectiveError.UNSUPPORTED);
450 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700451 }
452 }
453
Saurav Das0e99e2b2015-10-28 12:39:42 -0700454 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800455 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700456 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800457 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800458 assignedVlan = vidCriterion.vlanId();
Charles Chanc550f2e2017-12-19 19:55:57 -0800459 } else if (filt.meta() != null) {
460 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700461 }
Charles Chane849c192016-01-11 18:28:54 -0800462
Charles Chand55e84d2016-03-30 17:54:24 -0700463 if (assignedVlan == null) {
464 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800465 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700466 log.debug("VLAN ID in criterion={}, metadata={}",
467 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
468 fail(filt, ObjectiveError.BADPARAMS);
469 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700470 }
471 }
472
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800473 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700474 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700475 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700476 } else {
477 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700478 vidCriterion, assignedVlan,
479 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700480 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800481 (install) ? "adding" : "removing", tmacRule, deviceId);
Charles Chanc550f2e2017-12-19 19:55:57 -0800482
483 if (install) {
484 ops = ops.add(tmacRule);
485 } else {
486 // NOTE: Only remove TMAC flow when there is no more enabled port within the
487 // same VLAN on this device if TMAC doesn't support matching on in_port.
488 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
489 ops = ops.remove(tmacRule);
490 } else {
491 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
492 }
493 }
Saurav Das822c4e22015-10-23 10:51:11 -0700494 }
495 }
496
Charles Chan985b12e2016-05-11 19:47:22 -0700497 if (vidCriterion == null) {
498 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530499 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700500 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800501 /*
502 * NOTE: Separate vlan filtering rules and assignment rules
503 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800504 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800505 */
506 List<FlowRule> allRules = processVlanIdFilter(
507 portCriterion, vidCriterion, assignedVlan, applicationId);
508 List<FlowRule> filteringRules = new ArrayList<>();
509 List<FlowRule> assignmentRules = new ArrayList<>();
510
511 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700512 VlanId vlanId;
513 if (requireVlanExtensions()) {
514 ExtensionCriterion extCriterion =
515 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
516 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
517 } else {
518 VlanIdCriterion vlanIdCriterion =
519 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
520 vlanId = vlanIdCriterion.vlanId();
521 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800522 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800523 filteringRules.add(flowRule);
524 } else {
525 assignmentRules.add(flowRule);
526 }
527 });
528
529 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700530 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800531 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800532 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
533 }
534
535 ops.newStage();
536
537 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700538 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800539 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800540 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700541 }
542 }
543
Saurav Das822c4e22015-10-23 10:51:11 -0700544 // apply filtering flow rules
545 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
546 @Override
547 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800548 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700549 ops.stages().get(0).size(), deviceId);
550 pass(filt);
551 }
552
553 @Override
554 public void onError(FlowRuleOperations ops) {
555 log.info("Failed to apply all filtering rules in dev {}", deviceId);
556 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
557 }
558 }));
559
560 }
561
562 /**
Charles Chand1172632017-03-15 17:33:09 -0700563 * Internal implementation of processVlanIdFilter.
564 * <p>
565 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
566 * Since it is non-OF spec, we need an extension treatment for that.
567 * The useVlanExtension must be set to false for OFDPA i12.
568 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700569 *
Charles Chanf9e98652016-09-07 16:54:23 -0700570 * @param portCriterion port on device for which this filter is programmed
571 * @param vidCriterion vlan assigned to port, or NONE for untagged
572 * @param assignedVlan assigned vlan-id for untagged packets
573 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700574 * @return list of FlowRule for port-vlan filters
575 */
576 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700577 VlanIdCriterion vidCriterion,
578 VlanId assignedVlan,
579 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800580 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700581 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
582 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800583 TrafficSelector.Builder preSelector = null;
584 TrafficTreatment.Builder preTreatment = null;
585
Saurav Das4f980082015-11-05 13:39:15 -0800586 treatment.transition(TMAC_TABLE);
587
Saurav Das822c4e22015-10-23 10:51:11 -0700588 if (vidCriterion.vlanId() == VlanId.NONE) {
589 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700590 preSelector = DefaultTrafficSelector.builder();
591 if (requireVlanExtensions()) {
592 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
593 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700594 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
595 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700596
597 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
598 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700599 } else {
Charles Chand1172632017-03-15 17:33:09 -0700600 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700601 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700602
603 preSelector.matchVlanId(assignedVlan);
604 }
605 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
606 } else {
607 if (requireVlanExtensions()) {
608 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
609 selector.extension(ofdpaMatchVlanVid, deviceId);
610 } else {
611 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700612 }
Charles Chan14967c22015-12-07 11:11:50 -0800613
Charles Chand55e84d2016-03-30 17:54:24 -0700614 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700615 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800616 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
617 treatment.extension(ofdpaSetVlanVid, deviceId);
618 } else {
619 treatment.setVlanId(assignedVlan);
620 }
Charles Chand55e84d2016-03-30 17:54:24 -0700621 }
Saurav Das822c4e22015-10-23 10:51:11 -0700622 }
Saurav Das822c4e22015-10-23 10:51:11 -0700623
624 // ofdpa cannot match on ALL portnumber, so we need to use separate
625 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800626 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530627 if (portCriterion != null) {
628 if (PortNumber.ALL.equals(portCriterion.port())) {
629 for (Port port : deviceService.getPorts(deviceId)) {
630 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
631 portnums.add(port.number());
632 }
Saurav Das822c4e22015-10-23 10:51:11 -0700633 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530634 } else {
635 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700636 }
637 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530638 log.warn("Filtering Objective missing Port Criterion . " +
639 "VLAN Table cannot be programmed for {}",
640 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700641 }
Saurav Das4f980082015-11-05 13:39:15 -0800642
Saurav Das822c4e22015-10-23 10:51:11 -0700643 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800644 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700645 selector.matchInPort(pnum);
646 FlowRule rule = DefaultFlowRule.builder()
647 .forDevice(deviceId)
648 .withSelector(selector.build())
649 .withTreatment(treatment.build())
650 .withPriority(DEFAULT_PRIORITY)
651 .fromApp(applicationId)
652 .makePermanent()
653 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800654
655 if (preSelector != null) {
656 preSelector.matchInPort(pnum);
657 FlowRule preRule = DefaultFlowRule.builder()
658 .forDevice(deviceId)
659 .withSelector(preSelector.build())
660 .withTreatment(preTreatment.build())
661 .withPriority(DEFAULT_PRIORITY)
662 .fromApp(applicationId)
663 .makePermanent()
664 .forTable(VLAN_TABLE).build();
665 rules.add(preRule);
666 }
667
Saurav Das822c4e22015-10-23 10:51:11 -0700668 rules.add(rule);
669 }
670 return rules;
671 }
672
673 /**
674 * Allows routed packets with correct destination MAC to be directed
675 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700676 *
677 * @param portCriterion port on device for which this filter is programmed
678 * @param ethCriterion dstMac of device for which is filter is programmed
679 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700680 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700681 * @param applicationId for application programming this filter
682 * @return list of FlowRule for port-vlan filters
683
684 */
685 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
686 EthCriterion ethCriterion,
687 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700688 VlanId assignedVlan,
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 Chanb4879a52017-10-20 19:09:16 -0700697 return processMcastEthDstFilter(ethCriterion, assignedVlan, 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 }
756 return rules;
757 }
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 Chan5270ed02016-01-30 23:22:37 -0800894 protected 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();
926 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800927 }
928
Charles Chan50d900c2018-03-02 13:26:22 -0800929 List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700930 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700931 ApplicationId applicationId) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000932 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700933 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
934 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700935 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000936
Charles Chanb4879a52017-10-20 19:09:16 -0700937 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
938 selector.matchEthType(Ethernet.TYPE_IPV4);
939 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
940 selector.matchVlanId(assignedVlan);
941 treatment.transition(MULTICAST_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 builder.add(rule);
951 }
952
953 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
954 selector = DefaultTrafficSelector.builder();
955 treatment = DefaultTrafficTreatment.builder();
956 selector.matchEthType(Ethernet.TYPE_IPV6);
957 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
958 selector.matchVlanId(assignedVlan);
959 treatment.transition(MULTICAST_ROUTING_TABLE);
960 rule = DefaultFlowRule.builder()
961 .forDevice(deviceId)
962 .withSelector(selector.build())
963 .withTreatment(treatment.build())
964 .withPriority(DEFAULT_PRIORITY)
965 .fromApp(applicationId)
966 .makePermanent()
967 .forTable(TMAC_TABLE).build();
968 builder.add(rule);
969 }
970 return builder.build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700971 }
972
Saurav Das822c4e22015-10-23 10:51:11 -0700973 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
974 switch (fwd.flag()) {
975 case SPECIFIC:
976 return processSpecific(fwd);
977 case VERSATILE:
978 return processVersatile(fwd);
979 default:
980 fail(fwd, ObjectiveError.UNKNOWN);
981 log.warn("Unknown forwarding flag {}", fwd.flag());
982 }
983 return Collections.emptySet();
984 }
985
986 /**
987 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
988 * ACL table.
989 * @param fwd the forwarding objective of type 'versatile'
990 * @return a collection of flow rules to be sent to the switch. An empty
991 * collection may be returned if there is a problem in processing
992 * the flow rule
993 */
Saurav Das52025962016-01-28 22:30:01 -0800994 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800995 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800996 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700997
998 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800999 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -07001000 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001001 log.error("Versatile forwarding objective:{} must include ethType",
1002 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001003 fail(fwd, ObjectiveError.BADPARAMS);
1004 return Collections.emptySet();
1005 }
1006 if (fwd.nextId() == null && fwd.treatment() == null) {
1007 log.error("Forwarding objective {} from {} must contain "
1008 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001009 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001010 return Collections.emptySet();
1011 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001012
Saurav Das77b5e902016-01-27 17:01:59 -08001013 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1014 fwd.selector().criteria().forEach(criterion -> {
1015 if (criterion instanceof VlanIdCriterion) {
1016 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1017 // ensure that match does not include vlan = NONE as OF-DPA does not
1018 // match untagged packets this way in the ACL table.
1019 if (vlanId.equals(VlanId.NONE)) {
1020 return;
1021 }
Charles Chand1172632017-03-15 17:33:09 -07001022 if (requireVlanExtensions()) {
1023 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1024 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1025 } else {
1026 sbuilder.matchVlanId(vlanId);
1027 }
Charles Chan09bf2692018-01-11 11:48:18 -08001028 } else if (criterion instanceof Icmpv6TypeCriterion) {
1029 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1030 sbuilder.matchIcmpv6Type(icmpv6Type);
1031 } else if (criterion instanceof Icmpv6CodeCriterion) {
1032 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1033 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001034 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1035 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1036 // Currently L4 dst port matching is only used by DHCP relay feature
1037 // and therefore is safe to be replaced with L4 src port matching.
1038 // We need to revisit this if L4 dst port is used for other purpose in the future.
1039 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001040 switch (criterion.type()) {
1041 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001042 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001043 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001044 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001045 break;
1046 default:
1047 sbuilder.add(criterion);
1048 }
1049 } else {
1050 sbuilder.add(criterion);
1051 }
Saurav Das77b5e902016-01-27 17:01:59 -08001052 } else {
1053 sbuilder.add(criterion);
1054 }
1055 });
1056
Saurav Das822c4e22015-10-23 10:51:11 -07001057 // XXX driver does not currently do type checking as per Tables 65-67 in
1058 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001059 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1060 if (fwd.treatment() != null) {
1061 for (Instruction ins : fwd.treatment().allInstructions()) {
1062 if (ins instanceof OutputInstruction) {
1063 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001064 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001065 ttBuilder.add(o);
1066 } else {
1067 log.warn("Only allowed treatments in versatile forwarding "
1068 + "objectives are punts to the controller");
1069 }
1070 } else {
1071 log.warn("Cannot process instruction in versatile fwd {}", ins);
1072 }
Saurav Das822c4e22015-10-23 10:51:11 -07001073 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001074 if (fwd.treatment().clearedDeferred()) {
1075 ttBuilder.wipeDeferred();
1076 }
Saurav Das822c4e22015-10-23 10:51:11 -07001077 }
Saurav Das822c4e22015-10-23 10:51:11 -07001078 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001079 // overide case
1080 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301081 if (next == null) {
1082 fail(fwd, ObjectiveError.BADPARAMS);
1083 return Collections.emptySet();
1084 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001085 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1086 // we only need the top level group's key to point the flow to it
1087 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1088 if (group == null) {
1089 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1090 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1091 fail(fwd, ObjectiveError.GROUPMISSING);
1092 return Collections.emptySet();
1093 }
1094 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001095 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001096
1097 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1098 .fromApp(fwd.appId())
1099 .withPriority(fwd.priority())
1100 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001101 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001102 .withTreatment(ttBuilder.build())
1103 .makePermanent()
1104 .forTable(ACL_TABLE);
1105 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001106 }
1107
1108 /**
1109 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001110 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001111 *
1112 * @param fwd the forwarding objective of type 'specific'
1113 * @return a collection of flow rules. Typically there will be only one
1114 * for this type of forwarding objective. An empty set may be
1115 * returned if there is an issue in processing the objective.
1116 */
Charles Chan50d900c2018-03-02 13:26:22 -08001117 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001118 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001119 fwd.id(), deviceId, fwd.nextId());
1120 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1121 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1122
1123 if (isEthTypeObj) {
1124 return processEthTypeSpecific(fwd);
1125 } else if (isEthDstObj) {
1126 return processEthDstSpecific(fwd);
1127 } else {
1128 log.warn("processSpecific: Unsupported forwarding objective "
1129 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001130 fail(fwd, ObjectiveError.UNSUPPORTED);
1131 return Collections.emptySet();
1132 }
Saurav Das4ce45962015-11-24 23:21:05 -08001133 }
1134
Saurav Das4ce45962015-11-24 23:21:05 -08001135 /**
1136 * Handles forwarding rules to the IP and MPLS tables.
1137 *
1138 * @param fwd the forwarding objective
1139 * @return A collection of flow rules, or an empty set
1140 */
1141 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001142 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001143 }
1144
1145 /**
1146 * Internal implementation of processEthTypeSpecific.
1147 * <p>
1148 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1149 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1150 * The allowDefaultRoute must be set to false for OFDPA i12.
1151 * </p>
1152 *
1153 * @param fwd the forwarding objective
1154 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001155 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001156 * @return A collection of flow rules, or an empty set
1157 */
1158 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001159 boolean allowDefaultRoute,
1160 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001161 TrafficSelector selector = fwd.selector();
1162 EthTypeCriterion ethType =
1163 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001164 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001165 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001166 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001167 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001168 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001169
Saurav Das8a0732e2015-11-20 15:27:53 -08001170 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001171 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1172 return Collections.emptyList();
1173 }
1174 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001175 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001176 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001177 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001178 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001179 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001180 }
Charles Chan50d900c2018-03-02 13:26:22 -08001181
1182 /*
1183 // XXX decrementing IP ttl is done automatically for routing, this
1184 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan14967c22015-12-07 11:11:50 -08001185 if (fwd.treatment() != null) {
1186 for (Instruction instr : fwd.treatment().allInstructions()) {
1187 if (instr instanceof L3ModificationInstruction &&
1188 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Charles Chan50d900c2018-03-02 13:26:22 -08001189 tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001190 }
1191 }
1192 }
Charles Chan50d900c2018-03-02 13:26:22 -08001193 */
Pier Ventree0ae7a32016-11-23 09:57:42 -08001194 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1195 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1196 return Collections.emptyList();
1197 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001198 //We need to set the proper next table
1199 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1200 if (ipv6Dst.isMulticast()) {
1201 forTableId = MULTICAST_ROUTING_TABLE;
1202 } else {
1203 forTableId = UNICAST_ROUTING_TABLE;
1204 }
1205
Charles Chan50d900c2018-03-02 13:26:22 -08001206 // XXX decrementing IP ttl is done automatically for routing, this
1207 // action is ignored or rejected in ofdpa as it is not fully implemented
1208 /*
Pier Ventree0ae7a32016-11-23 09:57:42 -08001209 if (fwd.treatment() != null) {
1210 for (Instruction instr : fwd.treatment().allInstructions()) {
1211 if (instr instanceof L3ModificationInstruction &&
1212 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Charles Chan50d900c2018-03-02 13:26:22 -08001213 tb.deferred().add(instr);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001214 }
1215 }
1216 }
Charles Chan50d900c2018-03-02 13:26:22 -08001217 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001218 } else {
1219 filteredSelector
1220 .matchEthType(Ethernet.MPLS_UNICAST)
1221 .matchMplsLabel(((MplsCriterion)
1222 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1223 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001224 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001225 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001226 filteredSelector.matchMplsBos(bos.mplsBos());
1227 }
1228 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001229 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1230 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001231
Charles Chan14967c22015-12-07 11:11:50 -08001232 if (fwd.treatment() != null) {
1233 for (Instruction instr : fwd.treatment().allInstructions()) {
1234 if (instr instanceof L2ModificationInstruction &&
1235 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001236 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001237 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001238 if (requireMplsPop()) {
1239 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1240 tb.immediate().popMpls();
1241 }
1242 } else {
1243 // Skip mpls pop action for mpls_unicast label
1244 if (instr instanceof ModMplsHeaderInstruction &&
1245 !((ModMplsHeaderInstruction) instr).ethernetType()
1246 .equals(EtherType.MPLS_UNICAST.ethType())) {
1247 tb.immediate().add(instr);
1248 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001249 }
Charles Chan14967c22015-12-07 11:11:50 -08001250 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001251
1252 if (requireMplsTtlModification()) {
1253 if (instr instanceof L3ModificationInstruction &&
1254 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1255 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1256 tb.immediate().decMplsTtl();
1257 }
1258 if (instr instanceof L3ModificationInstruction &&
1259 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1260 tb.immediate().add(instr);
1261 }
Charles Chan14967c22015-12-07 11:11:50 -08001262 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001263 }
1264 }
1265 }
Saurav Das822c4e22015-10-23 10:51:11 -07001266
1267 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001268 NextGroup next = getGroupForNextObjective(fwd.nextId());
1269 if (next != null) {
1270 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1271 // we only need the top level group's key to point the flow to it
1272 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1273 if (group == null) {
1274 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1275 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1276 fail(fwd, ObjectiveError.GROUPMISSING);
1277 return Collections.emptySet();
1278 }
Saurav Dasa4020382018-02-14 14:14:54 -08001279 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1280 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1281 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1282 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1283 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1284 return Collections.emptySet();
1285 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001286 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001287 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001288 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001289 if (shouldRetry()) {
1290 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1291 Integer.toHexString(group.id().id()), deviceId,
1292 fwd.id());
1293 emptyGroup = true;
1294 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001295 }
Saurav Das25190812016-05-27 13:54:07 -07001296 } else {
1297 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1298 fwd.nextId(), deviceId, fwd.id());
1299 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1300 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001301 }
Saurav Das822c4e22015-10-23 10:51:11 -07001302 }
Charles Chancad338a2016-09-16 18:03:11 -07001303
1304 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001305 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001306 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001307 // set mpls type as apply_action
1308 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001309 }
1310 tb.transition(mplsNextTable);
1311 } else {
1312 tb.transition(ACL_TABLE);
1313 }
1314
Saurav Das822c4e22015-10-23 10:51:11 -07001315 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1316 .fromApp(fwd.appId())
1317 .withPriority(fwd.priority())
1318 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001319 .withSelector(filteredSelector.build())
1320 .withTreatment(tb.build())
1321 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001322
1323 if (fwd.permanent()) {
1324 ruleBuilder.makePermanent();
1325 } else {
1326 ruleBuilder.makeTemporary(fwd.timeout());
1327 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001328 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1329 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001330 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001331 flowRuleCollection.add(
1332 defaultRoute(fwd, complementarySelector, forTableId, tb)
1333 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001334 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1335 }
Saurav Dasc568c342018-01-25 09:49:01 -08001336
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001337 if (emptyGroup) {
1338 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1339 RETRY_MS, TimeUnit.MILLISECONDS);
1340 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001341 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001342 }
1343
Charles Chan50d900c2018-03-02 13:26:22 -08001344 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001345 TrafficSelector.Builder extBuilder,
1346 ForwardingObjective fwd,
1347 boolean allowDefaultRoute) {
1348 TrafficSelector selector = fwd.selector();
1349
1350 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1351 if (ipv4Dst.isMulticast()) {
1352 if (ipv4Dst.prefixLength() != 32) {
1353 log.warn("Multicast specific forwarding objective can only be /32");
1354 fail(fwd, ObjectiveError.BADPARAMS);
1355 return -1;
1356 }
1357 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1358 if (assignedVlan == null) {
1359 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1360 fail(fwd, ObjectiveError.BADPARAMS);
1361 return -1;
1362 }
Charles Chand1172632017-03-15 17:33:09 -07001363 if (requireVlanExtensions()) {
1364 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1365 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1366 } else {
1367 builderToUpdate.matchVlanId(assignedVlan);
1368 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001369 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1370 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1371 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1372 } else {
1373 if (ipv4Dst.prefixLength() == 0) {
1374 if (allowDefaultRoute) {
1375 // The entire IPV4_DST field is wildcarded intentionally
1376 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1377 } else {
1378 // NOTE: The switch does not support matching 0.0.0.0/0
1379 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1380 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1381 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1382 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1383 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1384 }
1385 } else {
1386 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1387 }
1388 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1389 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1390 }
1391 return 0;
1392 }
1393
1394 /**
1395 * Helper method to build Ipv6 selector using the selector provided by
1396 * a forwarding objective.
1397 *
1398 * @param builderToUpdate the builder to update
1399 * @param fwd the selector to read
1400 * @return 0 if the update ends correctly. -1 if the matches
1401 * are not yet supported
1402 */
Charles Chan50d900c2018-03-02 13:26:22 -08001403 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001404 ForwardingObjective fwd) {
1405
1406 TrafficSelector selector = fwd.selector();
1407
1408 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1409 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001410 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1411 log.warn("Multicast specific forwarding objective can only be /128");
1412 fail(fwd, ObjectiveError.BADPARAMS);
1413 return -1;
1414 }
1415 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1416 if (assignedVlan == null) {
1417 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1418 fail(fwd, ObjectiveError.BADPARAMS);
1419 return -1;
1420 }
1421 if (requireVlanExtensions()) {
1422 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1423 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1424 } else {
1425 builderToUpdate.matchVlanId(assignedVlan);
1426 }
1427 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1428 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1429 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1430 } else {
1431 if (ipv6Dst.prefixLength() != 0) {
1432 builderToUpdate.matchIPv6Dst(ipv6Dst);
1433 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001434 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1435 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1436 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001437 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001438 return 0;
1439 }
1440
Charles Chan50d900c2018-03-02 13:26:22 -08001441 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001442 TrafficSelector.Builder complementarySelector,
1443 int forTableId,
1444 TrafficTreatment.Builder tb) {
1445 FlowRule.Builder rule = DefaultFlowRule.builder()
1446 .fromApp(fwd.appId())
1447 .withPriority(fwd.priority())
1448 .forDevice(deviceId)
1449 .withSelector(complementarySelector.build())
1450 .withTreatment(tb.build())
1451 .forTable(forTableId);
1452 if (fwd.permanent()) {
1453 rule.makePermanent();
1454 } else {
1455 rule.makeTemporary(fwd.timeout());
1456 }
1457 return rule.build();
1458 }
1459
Saurav Das4ce45962015-11-24 23:21:05 -08001460 /**
1461 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1462 * allowed in the bridging table - instead we use L2 Interface group or
1463 * L2 flood group
1464 *
1465 * @param fwd the forwarding objective
1466 * @return A collection of flow rules, or an empty set
1467 */
1468 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1469 List<FlowRule> rules = new ArrayList<>();
1470
1471 // Build filtered selector
1472 TrafficSelector selector = fwd.selector();
1473 EthCriterion ethCriterion = (EthCriterion) selector
1474 .getCriterion(Criterion.Type.ETH_DST);
1475 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1476 .getCriterion(Criterion.Type.VLAN_VID);
1477
1478 if (vlanIdCriterion == null) {
1479 log.warn("Forwarding objective for bridging requires vlan. Not "
1480 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1481 fail(fwd, ObjectiveError.BADPARAMS);
1482 return Collections.emptySet();
1483 }
1484
1485 TrafficSelector.Builder filteredSelectorBuilder =
1486 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001487
1488 if (!ethCriterion.mac().equals(NONE) &&
1489 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001490 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1491 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1492 fwd.id(), fwd.nextId(), deviceId);
1493 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001494 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001495 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1496 + "in dev:{} for vlan:{}",
1497 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1498 }
Charles Chand1172632017-03-15 17:33:09 -07001499 if (requireVlanExtensions()) {
1500 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1501 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1502 } else {
1503 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1504 }
Saurav Das4ce45962015-11-24 23:21:05 -08001505 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1506
1507 if (fwd.treatment() != null) {
1508 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1509 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1510 }
1511
1512 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1513 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001514 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001515 if (next != null) {
1516 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1517 // we only need the top level group's key to point the flow to it
1518 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1519 if (group != null) {
1520 treatmentBuilder.deferred().group(group.id());
1521 } else {
1522 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1523 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1524 fail(fwd, ObjectiveError.GROUPMISSING);
1525 return Collections.emptySet();
1526 }
1527 }
1528 }
1529 treatmentBuilder.immediate().transition(ACL_TABLE);
1530 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1531
1532 // Build bridging table entries
1533 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1534 flowRuleBuilder.fromApp(fwd.appId())
1535 .withPriority(fwd.priority())
1536 .forDevice(deviceId)
1537 .withSelector(filteredSelector)
1538 .withTreatment(filteredTreatment)
1539 .forTable(BRIDGING_TABLE);
1540 if (fwd.permanent()) {
1541 flowRuleBuilder.makePermanent();
1542 } else {
1543 flowRuleBuilder.makeTemporary(fwd.timeout());
1544 }
1545 rules.add(flowRuleBuilder.build());
1546 return rules;
1547 }
1548
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001549 //////////////////////////////////////
1550 // Helper Methods and Classes
1551 //////////////////////////////////////
1552
1553 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1554 TrafficSelector selector = fwd.selector();
1555 EthTypeCriterion ethType = (EthTypeCriterion) selector
1556 .getCriterion(Criterion.Type.ETH_TYPE);
1557 return !((ethType == null) ||
1558 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001559 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1560 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001561 }
1562
1563 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1564 TrafficSelector selector = fwd.selector();
1565 EthCriterion ethDst = (EthCriterion) selector
1566 .getCriterion(Criterion.Type.ETH_DST);
1567 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1568 .getCriterion(Criterion.Type.VLAN_VID);
1569 return !(ethDst == null && vlanId == null);
1570 }
1571
Charles Chan50d900c2018-03-02 13:26:22 -08001572 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001573 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1574 if (next != null) {
1575 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1576 if (gkeys != null && !gkeys.isEmpty()) {
1577 return next;
1578 } else {
1579 log.warn("Empty next group found in FlowObjective store for "
1580 + "next-id:{} in dev:{}", nextId, deviceId);
1581 }
1582 } else {
1583 log.warn("next-id {} not found in Flow objective store for dev:{}",
1584 nextId, deviceId);
1585 }
1586 return null;
1587 }
1588
Charles Chan188ebf52015-12-23 00:15:11 -08001589 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001590 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001591 }
1592
Charles Chan188ebf52015-12-23 00:15:11 -08001593 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001594 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001595 }
Saurav Das24431192016-03-07 19:13:00 -08001596
Saurav Das24431192016-03-07 19:13:00 -08001597 @Override
1598 public List<String> getNextMappings(NextGroup nextGroup) {
1599 List<String> mappings = new ArrayList<>();
1600 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1601 for (Deque<GroupKey> gkd : gkeys) {
1602 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001603 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001604 for (GroupKey gk : gkd) {
1605 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001606 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001607 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001608 continue;
1609 }
1610 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1611 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001612 lastGroup = g;
1613 }
1614 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001615 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001616 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001617 lastGroupIns = lastGroup.buckets().buckets().get(0)
1618 .treatment().allInstructions();
1619 }
1620 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001621 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001622 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001623 }
1624 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001625 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001626 }
1627 return mappings;
1628 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001629
Saurav Dasa4020382018-02-14 14:14:54 -08001630 /**
1631 * Returns true iff the given selector matches on BOS==true, indicating that
1632 * the selector is trying to match on a label that is bottom-of-stack.
1633 *
1634 * @param selector the given match
1635 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1636 * expressed in the given selector
1637 */
Pier Ventre140a8942016-11-02 07:26:38 -07001638 static boolean isMplsBos(TrafficSelector selector) {
1639 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1640 return bosCriterion != null && bosCriterion.mplsBos();
1641 }
1642
Saurav Dasa4020382018-02-14 14:14:54 -08001643 /**
1644 * Returns true iff the given selector matches on BOS==false, indicating
1645 * that the selector is trying to match on a label that is not the
1646 * bottom-of-stack label.
1647 *
1648 * @param selector the given match
1649 * @return true iff BoS==false;
1650 * false if BOS==true, or BOS matching is not expressed in the given selector
1651 */
Pier Ventre140a8942016-11-02 07:26:38 -07001652 static boolean isNotMplsBos(TrafficSelector selector) {
1653 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1654 return bosCriterion != null && !bosCriterion.mplsBos();
1655 }
1656
Saurav Dasa4020382018-02-14 14:14:54 -08001657 /**
1658 * Returns true iff the forwarding objective includes a treatment to pop the
1659 * MPLS label.
1660 *
1661 * @param fwd the given forwarding objective
1662 * @return true iff mpls pop treatment exists
1663 */
1664 static boolean isMplsPop(ForwardingObjective fwd) {
1665 if (fwd.treatment() != null) {
1666 for (Instruction instr : fwd.treatment().allInstructions()) {
1667 if (instr instanceof L2ModificationInstruction
1668 && ((L2ModificationInstruction) instr)
1669 .subtype() == L2SubType.MPLS_POP) {
1670 return true;
1671 }
1672 }
1673 }
1674 return false;
1675 }
1676
Charles Chand9e47c62017-10-05 15:17:15 -07001677 private static boolean isIpv6(TrafficSelector selector) {
1678 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1679 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1680 }
1681
Charles Chan50d900c2018-03-02 13:26:22 -08001682 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001683 if (selector == null) {
1684 return null;
1685 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001686 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1687 return (criterion == null)
1688 ? null : ((VlanIdCriterion) criterion).vlanId();
1689 }
1690
Charles Chand9e47c62017-10-05 15:17:15 -07001691 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001692 if (selector == null) {
1693 return null;
1694 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001695 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1696 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1697 }
Charles Chand55e84d2016-03-30 17:54:24 -07001698
1699 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001700 if (treatment == null) {
1701 return null;
1702 }
Charles Chand55e84d2016-03-30 17:54:24 -07001703 for (Instruction i : treatment.allInstructions()) {
1704 if (i instanceof ModVlanIdInstruction) {
1705 return ((ModVlanIdInstruction) i).vlanId();
1706 }
1707 }
1708 return null;
1709 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001710
1711 /**
1712 * Utility class that retries sending flows a fixed number of times, even if
1713 * some of the attempts are successful. Used only for forwarding objectives.
1714 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001715 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001716 int attempts = MAX_RETRY_ATTEMPTS;
1717 private Collection<FlowRule> retryFlows;
1718 private ForwardingObjective fwd;
1719
Charles Chan50d900c2018-03-02 13:26:22 -08001720 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001721 this.fwd = fwd;
1722 this.retryFlows = retryFlows;
1723 }
1724
1725 @Override
1726 public void run() {
1727 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1728 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1729 sendForward(fwd, retryFlows);
1730 if (--attempts > 0) {
1731 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1732 }
1733 }
1734 }
1735
Saurav Das822c4e22015-10-23 10:51:11 -07001736}