blob: 0bfc113ff524d74b6cfa2134b3e24b4ae581ba5c [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 Yashchuka3679532017-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 Chand57552d2018-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;
jayakumarthazhathad3636a2018-10-01 00:51:54 +053055import org.onosproject.net.flow.criteria.ExtensionCriterion;
56import org.onosproject.net.flow.criteria.ExtensionSelector;
Saurav Das822c4e22015-10-23 10:51:11 -070057import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080058import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
59import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080060import org.onosproject.net.flow.criteria.MplsBosCriterion;
61import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070062import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070063import org.onosproject.net.flow.criteria.TcpPortCriterion;
64import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070065import org.onosproject.net.flow.criteria.VlanIdCriterion;
66import org.onosproject.net.flow.instructions.Instruction;
67import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chan3fe71712018-06-15 18:54:18 -070068import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070069import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080070import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chand57552d2018-03-02 15:41:41 -080072import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuka3679532017-12-08 17:40:05 +020073import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080074import org.onosproject.net.flow.instructions.L3ModificationInstruction;
75import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070076import org.onosproject.net.flowobjective.FilteringObjective;
77import org.onosproject.net.flowobjective.FlowObjectiveStore;
78import org.onosproject.net.flowobjective.ForwardingObjective;
79import org.onosproject.net.flowobjective.NextObjective;
80import org.onosproject.net.flowobjective.Objective;
81import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.DefaultGroupKey;
83import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070085import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070086import org.onosproject.store.serializers.KryoNamespaces;
87import org.slf4j.Logger;
88
Pier Ventree0ae7a32016-11-23 09:57:42 -080089import java.util.ArrayDeque;
90import java.util.ArrayList;
91import java.util.Collection;
92import java.util.Collections;
93import java.util.Deque;
94import java.util.List;
95import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080096import java.util.concurrent.ScheduledExecutorService;
97import java.util.concurrent.TimeUnit;
98
99import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800100import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -0700101import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
102import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800103import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800104import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700105import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700106import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi5af88fe2018-02-01 10:23:12 +0100107import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800108import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700109import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800110
Saurav Das822c4e22015-10-23 10:51:11 -0700111/**
112 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700113 */
Charles Chan361154b2016-03-24 10:23:39 -0700114public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700115 protected static final int PORT_TABLE = 0;
116 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800117 protected static final int VLAN_1_TABLE = 11;
118 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
119 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700120 protected static final int TMAC_TABLE = 20;
121 protected static final int UNICAST_ROUTING_TABLE = 30;
122 protected static final int MULTICAST_ROUTING_TABLE = 40;
123 protected static final int MPLS_TABLE_0 = 23;
124 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700125 protected static final int MPLS_L3_TYPE_TABLE = 27;
126 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700127 protected static final int BRIDGING_TABLE = 50;
128 protected static final int ACL_TABLE = 60;
129 protected static final int MAC_LEARNING_TABLE = 254;
130 protected static final long OFPP_MAX = 0xffffff00L;
131
Saurav Das52025962016-01-28 22:30:01 -0800132 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800133 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700134 protected static final int LOWEST_PRIORITY = 0x0;
135
Pier Ventre42287df2016-11-09 14:17:26 -0800136 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800137 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
138 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
139
Pier Ventre70d53ba2016-11-17 22:26:29 -0800140 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800141 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
142 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
143
Saurav Das822c4e22015-10-23 10:51:11 -0700144 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700145 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700146 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700147 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800148 protected GroupService groupService;
149 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700150 protected DeviceId deviceId;
151 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700152 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800153 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700154 .register(KryoNamespaces.API)
155 .register(GroupKey.class)
156 .register(DefaultGroupKey.class)
157 .register(OfdpaNextGroup.class)
158 .register(ArrayDeque.class)
159 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700160
Charles Chan425854b2016-04-11 15:32:12 -0700161 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700162
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700163 // flows installations to be retried
Charles Chan93090352018-03-02 13:26:22 -0800164 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700165 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan93090352018-03-02 13:26:22 -0800166 private static final int MAX_RETRY_ATTEMPTS = 10;
167 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800168
Saurav Das822c4e22015-10-23 10:51:11 -0700169 @Override
170 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700171 this.deviceId = deviceId;
172
Charles Chan425854b2016-04-11 15:32:12 -0700173 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700174 coreService = serviceDirectory.get(CoreService.class);
175 flowRuleService = serviceDirectory.get(FlowRuleService.class);
176 groupService = serviceDirectory.get(GroupService.class);
177 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700178 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700179
Charles Chan40132b32017-01-22 00:19:37 -0800180 initDriverId();
181 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700182
Saurav Das822c4e22015-10-23 10:51:11 -0700183 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700184 }
185
Charles Chan40132b32017-01-22 00:19:37 -0800186 protected void initDriverId() {
187 driverId = coreService.registerApplication(
188 "org.onosproject.driver.Ofdpa2Pipeline");
189 }
190
191 protected void initGroupHander(PipelinerContext context) {
192 groupHandler = new Ofdpa2GroupHandler();
193 groupHandler.init(deviceId, context);
194 }
195
Saurav Das822c4e22015-10-23 10:51:11 -0700196 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800197 // OF-DPA does not require initializing the pipeline as it puts default
198 // rules automatically in the hardware. However emulation of OFDPA in
199 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700200 }
201
Charles Chand1172632017-03-15 17:33:09 -0700202 /**
Alex Yashchuka3679532017-12-08 17:40:05 +0200203 * Determines whether this pipeline requires MPLS POP instruction.
204 *
205 * @return true to use MPLS POP instruction
206 */
207 public boolean requireMplsPop() {
208 return true;
209 }
210
211 /**
212 * Determines whether this pipeline requires MPLS BOS match.
213 *
214 * @return true to use MPLS BOS match
215 */
216 public boolean requireMplsBosMatch() {
217 return true;
218 }
219
220 /**
221 * Determines whether this pipeline requires MPLS TTL decrement and copy.
222 *
223 * @return true to use MPLS TTL decrement and copy
224 */
225 public boolean requireMplsTtlModification() {
226 return true;
227 }
228
229 /**
Charles Chand1172632017-03-15 17:33:09 -0700230 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
231 *
232 * @return true to use the extensions
233 */
234 protected boolean requireVlanExtensions() {
235 return true;
236 }
237
Saurav Das86d13e82017-04-28 17:03:48 -0700238 /**
239 * Determines whether in-port should be matched on in TMAC table rules.
240 *
241 * @return true if match on in-port should be programmed
242 */
243 protected boolean matchInPortTmacTable() {
244 return true;
245 }
246
Charles Chand9e47c62017-10-05 15:17:15 -0700247 /**
248 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
249 *
250 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
251 */
252 protected boolean supportIpv6L4Dst() {
253 return true;
254 }
255
Saurav Dasf3f75942018-01-25 09:49:01 -0800256 /**
257 * Determines whether this driver should continue to retry flows that point
258 * to empty groups. See CORD-554.
259 *
260 * @return true if the driver should retry flows
261 */
262 protected boolean shouldRetry() {
263 return true;
264 }
265
Charles Chand57552d2018-03-02 15:41:41 -0800266 /**
267 * Determines whether this driver requires unicast flow to be installed before multicast flow
268 * in TMAC table.
269 *
270 * @return true if required
271 */
272 protected boolean requireUnicastBeforeMulticast() {
273 return false;
274 }
275
Andrea Campanella84ac4df2018-04-30 11:48:55 +0200276 /**
277 * Determines whether this driver supports installing a clearDeferred action on table 30.
278 *
279 * @return true if required
280 */
281 protected boolean supportsUnicastBlackHole() {
282 return true;
283 }
284
Saurav Das822c4e22015-10-23 10:51:11 -0700285 //////////////////////////////////////
286 // Flow Objectives
287 //////////////////////////////////////
288
289 @Override
290 public void filter(FilteringObjective filteringObjective) {
291 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
292 processFilter(filteringObjective,
293 filteringObjective.op() == Objective.Operation.ADD,
294 filteringObjective.appId());
295 } else {
296 // Note that packets that don't match the PERMIT filter are
297 // automatically denied. The DENY filter is used to deny packets
298 // that are otherwise permitted by the PERMIT filter.
299 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530300 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700301 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
302 }
303 }
304
305 @Override
306 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700307 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700308 if (rules == null || rules.isEmpty()) {
309 // Assumes fail message has already been generated to the objective
310 // context. Returning here prevents spurious pass message to be
311 // generated by FlowRule service for empty flowOps.
312 return;
313 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700314 sendForward(fwd, rules);
315 }
316
Charles Chan93090352018-03-02 13:26:22 -0800317 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700318 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700319 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700320 case ADD:
321 rules.stream()
322 .filter(Objects::nonNull)
323 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800324 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700325 break;
326 case REMOVE:
327 rules.stream()
328 .filter(Objects::nonNull)
329 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800330 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700331 break;
332 default:
333 fail(fwd, ObjectiveError.UNKNOWN);
334 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700335 }
336
Saurav Das822c4e22015-10-23 10:51:11 -0700337 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
338 @Override
339 public void onSuccess(FlowRuleOperations ops) {
340 pass(fwd);
341 }
342
343 @Override
344 public void onError(FlowRuleOperations ops) {
345 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
346 }
347 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700348 }
349
350 @Override
351 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800352 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
353 switch (nextObjective.op()) {
354 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800355 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800356 log.warn("Cannot add next {} that already exists in device {}",
357 nextObjective.id(), deviceId);
358 return;
359 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700360 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800361 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700362 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800363 break;
364 case ADD_TO_EXISTING:
365 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700366 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800367 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700368 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800369 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800370 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800371 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
372 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800373
374 // by design multiple pending bucket is allowed for the group
375 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
376 if (pendBkts == null) {
377 pendBkts = Sets.newHashSet();
378 }
379 pendBkts.add(nextObjective);
380 return pendBkts;
381 });
Saurav Das4f980082015-11-05 13:39:15 -0800382 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800383 break;
384 case REMOVE:
385 if (nextGroup == null) {
386 log.warn("Cannot remove next {} that does not exist in device {}",
387 nextObjective.id(), deviceId);
388 return;
389 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700390 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800391 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700392 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800393 break;
394 case REMOVE_FROM_EXISTING:
395 if (nextGroup == null) {
396 log.warn("Cannot remove from next {} that does not exist in device {}",
397 nextObjective.id(), deviceId);
398 return;
399 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700400 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800401 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700402 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800403 break;
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900404 case MODIFY:
405 if (nextGroup == null) {
406 log.warn("Cannot modify next {} that does not exist in device {}",
407 nextObjective.id(), deviceId);
408 return;
409 }
410 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
411 nextObjective.id(), deviceId);
412 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
413 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700414 case VERIFY:
415 if (nextGroup == null) {
416 log.warn("Cannot verify next {} that does not exist in device {}",
417 nextObjective.id(), deviceId);
418 return;
419 }
420 log.debug("Processing NextObjective id {} in dev {} - verify",
421 nextObjective.id(), deviceId);
422 groupHandler.verifyGroup(nextObjective, nextGroup);
423 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800424 default:
Saurav Das4f980082015-11-05 13:39:15 -0800425 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700426 }
427 }
428
429 //////////////////////////////////////
430 // Flow handling
431 //////////////////////////////////////
432
433 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700434 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
435 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700436 *
437 * @param filt the filtering objective
438 * @param install indicates whether to add or remove the objective
439 * @param applicationId the application that sent this objective
440 */
Saurav Das52025962016-01-28 22:30:01 -0800441 protected void processFilter(FilteringObjective filt,
442 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700443 // This driver only processes filtering criteria defined with switch
444 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530445 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700446 EthCriterion ethCriterion = null;
447 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700448 if (!filt.key().equals(Criteria.dummy()) &&
449 filt.key().type() == Criterion.Type.IN_PORT) {
450 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700451 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530452 if (portCriterion == null) {
453 log.debug("No IN_PORT defined in filtering objective from app: {}",
454 applicationId);
455 } else {
456 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
457 portCriterion.port());
458 }
Saurav Das822c4e22015-10-23 10:51:11 -0700459 // convert filtering conditions for switch-intfs into flowrules
460 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
461 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700462 switch (criterion.type()) {
463 case ETH_DST:
464 case ETH_DST_MASKED:
465 ethCriterion = (EthCriterion) criterion;
466 break;
467 case VLAN_VID:
468 vidCriterion = (VlanIdCriterion) criterion;
469 break;
470 default:
471 log.warn("Unsupported filter {}", criterion);
472 fail(filt, ObjectiveError.UNSUPPORTED);
473 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700474 }
475 }
476
Saurav Das0e99e2b2015-10-28 12:39:42 -0700477 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800478 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700479 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan9174e6b2017-12-19 19:55:57 -0800480 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800481 assignedVlan = vidCriterion.vlanId();
Pier3cc7e662018-03-07 11:42:50 +0100482 }
483 // If the meta VLAN is present let's update the assigned vlan
484 if (filt.meta() != null) {
485 VlanId metaVlan = readVlanFromTreatment(filt.meta());
486 if (metaVlan != null) {
487 assignedVlan = metaVlan;
488 }
Charles Chand55e84d2016-03-30 17:54:24 -0700489 }
Charles Chane849c192016-01-11 18:28:54 -0800490
Charles Chand55e84d2016-03-30 17:54:24 -0700491 if (assignedVlan == null) {
492 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800493 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700494 log.debug("VLAN ID in criterion={}, metadata={}",
495 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
496 fail(filt, ObjectiveError.BADPARAMS);
497 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700498 }
499 }
500
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800501 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700502 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburg16e5ed42018-04-30 19:27:19 -0400503 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700504 } else {
Charles Chand57552d2018-03-02 15:41:41 -0800505 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan206506b2018-03-02 16:43:28 -0800506 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chand57552d2018-03-02 15:41:41 -0800507 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800508 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800509 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800510 ops.newStage();
Charles Chan9174e6b2017-12-19 19:55:57 -0800511
Charles Chan206506b2018-03-02 16:43:28 -0800512 for (FlowRule flowRule : flowRules) {
Charles Chan893a1d72018-05-10 22:19:25 +0000513 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan206506b2018-03-02 16:43:28 -0800514 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan206506b2018-03-02 16:43:28 -0800515 if (install) {
516 ops = ops.add(flowRule);
Charles Chan9174e6b2017-12-19 19:55:57 -0800517 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800518 // NOTE: Only remove TMAC flow when there is no more enabled port within the
519 // same VLAN on this device if TMAC doesn't support matching on in_port.
520 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
521 ops = ops.remove(flowRule);
522 } else {
523 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
524 }
Charles Chan9174e6b2017-12-19 19:55:57 -0800525 }
526 }
Saurav Das822c4e22015-10-23 10:51:11 -0700527 }
528 }
529
Charles Chan985b12e2016-05-11 19:47:22 -0700530 if (vidCriterion == null) {
531 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530532 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700533 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800534 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800535 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800536 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800537 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800538 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800539
Charles Chan206506b2018-03-02 16:43:28 -0800540 for (FlowRule flowRule : flowRules) {
541 log.trace("{} flow rules in VLAN table: {} for dev: {}",
542 (install) ? "adding" : "removing", flowRule, deviceId);
543 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700544 }
Saurav Das822c4e22015-10-23 10:51:11 -0700545 }
546 }
547
Saurav Das822c4e22015-10-23 10:51:11 -0700548 // apply filtering flow rules
549 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
550 @Override
551 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800552 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700553 ops.stages().get(0).size(), deviceId);
554 pass(filt);
555 }
556
557 @Override
558 public void onError(FlowRuleOperations ops) {
559 log.info("Failed to apply all filtering rules in dev {}", deviceId);
560 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
561 }
562 }));
563
564 }
565
566 /**
Charles Chand1172632017-03-15 17:33:09 -0700567 * Internal implementation of processVlanIdFilter.
568 * <p>
569 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
570 * Since it is non-OF spec, we need an extension treatment for that.
571 * The useVlanExtension must be set to false for OFDPA i12.
572 * </p>
Charles Chan206506b2018-03-02 16:43:28 -0800573 * <p>
574 * NOTE: Separate VLAN filtering rules and assignment rules
575 * into different stages in order to guarantee that filtering rules
576 * always go first, as required by OFDPA.
577 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700578 *
Charles Chanf9e98652016-09-07 16:54:23 -0700579 * @param portCriterion port on device for which this filter is programmed
580 * @param vidCriterion vlan assigned to port, or NONE for untagged
581 * @param assignedVlan assigned vlan-id for untagged packets
582 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800583 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700584 */
Charles Chan206506b2018-03-02 16:43:28 -0800585 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700586 VlanIdCriterion vidCriterion,
587 VlanId assignedVlan,
588 ApplicationId applicationId) {
Charles Chan206506b2018-03-02 16:43:28 -0800589 List<FlowRule> filteringRules = new ArrayList<>();
590 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700591 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
592 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800593 TrafficSelector.Builder preSelector = null;
594 TrafficTreatment.Builder preTreatment = null;
595
Saurav Das4f980082015-11-05 13:39:15 -0800596 treatment.transition(TMAC_TABLE);
597
Saurav Das822c4e22015-10-23 10:51:11 -0700598 if (vidCriterion.vlanId() == VlanId.NONE) {
599 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700600 preSelector = DefaultTrafficSelector.builder();
601 if (requireVlanExtensions()) {
602 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
603 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700604 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
605 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700606
607 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
608 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700609 } else {
Charles Chand1172632017-03-15 17:33:09 -0700610 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700611 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700612
613 preSelector.matchVlanId(assignedVlan);
614 }
615 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
616 } else {
617 if (requireVlanExtensions()) {
618 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
619 selector.extension(ofdpaMatchVlanVid, deviceId);
620 } else {
621 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700622 }
Charles Chan14967c22015-12-07 11:11:50 -0800623
Charles Chand55e84d2016-03-30 17:54:24 -0700624 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700625 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800626 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
627 treatment.extension(ofdpaSetVlanVid, deviceId);
628 } else {
629 treatment.setVlanId(assignedVlan);
630 }
Charles Chand55e84d2016-03-30 17:54:24 -0700631 }
Saurav Das822c4e22015-10-23 10:51:11 -0700632 }
Saurav Das822c4e22015-10-23 10:51:11 -0700633
634 // ofdpa cannot match on ALL portnumber, so we need to use separate
635 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800636 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530637 if (portCriterion != null) {
638 if (PortNumber.ALL.equals(portCriterion.port())) {
639 for (Port port : deviceService.getPorts(deviceId)) {
640 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
641 portnums.add(port.number());
642 }
Saurav Das822c4e22015-10-23 10:51:11 -0700643 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530644 } else {
645 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700646 }
647 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530648 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan206506b2018-03-02 16:43:28 -0800649 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700650 }
Saurav Das4f980082015-11-05 13:39:15 -0800651
Saurav Das822c4e22015-10-23 10:51:11 -0700652 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800653 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700654 selector.matchInPort(pnum);
655 FlowRule rule = DefaultFlowRule.builder()
656 .forDevice(deviceId)
657 .withSelector(selector.build())
658 .withTreatment(treatment.build())
659 .withPriority(DEFAULT_PRIORITY)
660 .fromApp(applicationId)
661 .makePermanent()
662 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800663 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800664
665 if (preSelector != null) {
666 preSelector.matchInPort(pnum);
667 FlowRule preRule = DefaultFlowRule.builder()
668 .forDevice(deviceId)
669 .withSelector(preSelector.build())
670 .withTreatment(preTreatment.build())
671 .withPriority(DEFAULT_PRIORITY)
672 .fromApp(applicationId)
673 .makePermanent()
674 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800675 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800676 }
Saurav Das822c4e22015-10-23 10:51:11 -0700677 }
Charles Chan1e58aa62018-03-06 22:10:15 -0800678 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700679 }
680
681 /**
682 * Allows routed packets with correct destination MAC to be directed
Charles Chand57552d2018-03-02 15:41:41 -0800683 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700684 *
685 * @param portCriterion port on device for which this filter is programmed
686 * @param ethCriterion dstMac of device for which is filter is programmed
687 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700688 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chand57552d2018-03-02 15:41:41 -0800689 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
690 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
691 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700692 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800693 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700694
695 */
Charles Chan206506b2018-03-02 16:43:28 -0800696 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700697 EthCriterion ethCriterion,
698 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700699 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800700 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700701 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800702 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530703 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800704 return processEthDstOnlyFilter(ethCriterion, applicationId);
705 }
706
Charles Chan5b9df8d2016-03-28 22:21:40 -0700707 // Multicast MAC
708 if (ethCriterion.mask() != null) {
Charles Chand57552d2018-03-02 15:41:41 -0800709 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700710 }
711
Saurav Das822c4e22015-10-23 10:51:11 -0700712 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530713 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700714 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700715 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530716 List<FlowRule> rules = new ArrayList<>();
717 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
718 if (vidCriterion != null && requireVlanExtensions()) {
719 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
720 }
Saurav Das822c4e22015-10-23 10:51:11 -0700721 // ofdpa cannot match on ALL portnumber, so we need to use separate
722 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700723 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530724 if (portCriterion != null) {
725 if (PortNumber.ALL.equals(portCriterion.port())) {
726 for (Port port : deviceService.getPorts(deviceId)) {
727 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
728 portnums.add(port.number());
729 }
Saurav Das822c4e22015-10-23 10:51:11 -0700730 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530731 } else {
732 portnums.add(portCriterion.port());
733 }
734 for (PortNumber pnum : portnums) {
735 rules.add(buildTmacRuleForIpv4(ethCriterion,
736 vidCriterion,
737 ofdpaMatchVlanVid,
738 applicationId,
739 pnum));
740 rules.add(buildTmacRuleForMpls(ethCriterion,
741 vidCriterion,
742 ofdpaMatchVlanVid,
743 applicationId,
744 pnum));
745 rules.add(buildTmacRuleForIpv6(ethCriterion,
746 vidCriterion,
747 ofdpaMatchVlanVid,
748 applicationId,
749 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700750 }
751 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530752 rules.add(buildTmacRuleForIpv4(ethCriterion,
753 vidCriterion,
754 ofdpaMatchVlanVid,
755 applicationId,
756 null));
757 rules.add(buildTmacRuleForMpls(ethCriterion,
758 vidCriterion,
759 ofdpaMatchVlanVid,
760 applicationId,
761 null));
762 rules.add(buildTmacRuleForIpv6(ethCriterion,
763 vidCriterion,
764 ofdpaMatchVlanVid,
765 applicationId,
766 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700767 }
Charles Chan206506b2018-03-02 16:43:28 -0800768 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700769 }
770
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530771 /**
772 * Builds TMAC rules for IPv4 packets.
773 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800774 * @param ethCriterion dst mac matching
775 * @param vidCriterion vlan id assigned to the port
776 * @param ofdpaMatchVlanVid OFDPA vlan id matching
777 * @param applicationId application id
778 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530779 * @return TMAC rule for IPV4 packets
780 */
781 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
782 VlanIdCriterion vidCriterion,
783 OfdpaMatchVlanVid ofdpaMatchVlanVid,
784 ApplicationId applicationId,
785 PortNumber pnum) {
786 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
787 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
788 if (pnum != null) {
789 if (matchInPortTmacTable()) {
790 selector.matchInPort(pnum);
791 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800792 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530793 "ignoring the IN_PORT criteria");
794 }
795 }
796 if (vidCriterion != null) {
797 if (requireVlanExtensions()) {
798 selector.extension(ofdpaMatchVlanVid, deviceId);
799 } else {
800 selector.matchVlanId(vidCriterion.vlanId());
801 }
802 }
803 selector.matchEthType(Ethernet.TYPE_IPV4);
804 selector.matchEthDst(ethCriterion.mac());
805 treatment.transition(UNICAST_ROUTING_TABLE);
806 return DefaultFlowRule.builder()
807 .forDevice(deviceId)
808 .withSelector(selector.build())
809 .withTreatment(treatment.build())
810 .withPriority(DEFAULT_PRIORITY)
811 .fromApp(applicationId)
812 .makePermanent()
813 .forTable(TMAC_TABLE).build();
814 }
815
816 /**
817 * Builds TMAC rule for MPLS packets.
818 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800819 * @param ethCriterion dst mac matching
820 * @param vidCriterion vlan id assigned to the port
821 * @param ofdpaMatchVlanVid OFDPA vlan id matching
822 * @param applicationId application id
823 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530824 * @return TMAC rule for MPLS packets
825 */
826 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
827 VlanIdCriterion vidCriterion,
828 OfdpaMatchVlanVid ofdpaMatchVlanVid,
829 ApplicationId applicationId,
830 PortNumber pnum) {
831 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
832 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
833 if (pnum != null) {
834 if (matchInPortTmacTable()) {
835 selector.matchInPort(pnum);
836 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800837 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530838 "ignoring the IN_PORT criteria");
839 }
840 }
841 if (vidCriterion != null) {
842 if (requireVlanExtensions()) {
843 selector.extension(ofdpaMatchVlanVid, deviceId);
844 } else {
845 selector.matchVlanId(vidCriterion.vlanId());
846 }
847 }
848 selector.matchEthType(Ethernet.MPLS_UNICAST);
849 selector.matchEthDst(ethCriterion.mac());
850 treatment.transition(MPLS_TABLE_0);
851 return DefaultFlowRule.builder()
852 .forDevice(deviceId)
853 .withSelector(selector.build())
854 .withTreatment(treatment.build())
855 .withPriority(DEFAULT_PRIORITY)
856 .fromApp(applicationId)
857 .makePermanent()
858 .forTable(TMAC_TABLE).build();
859 }
860
861 /**
862 * Builds TMAC rules for IPv6 packets.
863 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800864 * @param ethCriterion dst mac matching
865 * @param vidCriterion vlan id assigned to the port
866 * @param ofdpaMatchVlanVid OFDPA vlan id matching
867 * @param applicationId application id
868 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530869 * @return TMAC rule for IPV6 packets
870 */
871 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
872 VlanIdCriterion vidCriterion,
873 OfdpaMatchVlanVid ofdpaMatchVlanVid,
874 ApplicationId applicationId,
875 PortNumber pnum) {
876 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
877 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
878 if (pnum != null) {
879 if (matchInPortTmacTable()) {
880 selector.matchInPort(pnum);
881 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800882 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530883 "ignoring the IN_PORT criteria");
884 }
885 }
886 if (vidCriterion != null) {
887 if (requireVlanExtensions()) {
888 selector.extension(ofdpaMatchVlanVid, deviceId);
889 } else {
890 selector.matchVlanId(vidCriterion.vlanId());
891 }
892 }
893 selector.matchEthType(Ethernet.TYPE_IPV6);
894 selector.matchEthDst(ethCriterion.mac());
895 treatment.transition(UNICAST_ROUTING_TABLE);
896 return DefaultFlowRule.builder()
897 .forDevice(deviceId)
898 .withSelector(selector.build())
899 .withTreatment(treatment.build())
900 .withPriority(DEFAULT_PRIORITY)
901 .fromApp(applicationId)
902 .makePermanent()
903 .forTable(TMAC_TABLE).build();
904 }
905
Charles Chan206506b2018-03-02 16:43:28 -0800906 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700907 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800908 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
909
Charles Chan5270ed02016-01-30 23:22:37 -0800910 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
911 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
912 selector.matchEthType(Ethernet.TYPE_IPV4);
913 selector.matchEthDst(ethCriterion.mac());
914 treatment.transition(UNICAST_ROUTING_TABLE);
915 FlowRule rule = DefaultFlowRule.builder()
916 .forDevice(deviceId)
917 .withSelector(selector.build())
918 .withTreatment(treatment.build())
919 .withPriority(DEFAULT_PRIORITY)
920 .fromApp(applicationId)
921 .makePermanent()
922 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800923 builder.add(rule);
924
925 selector = DefaultTrafficSelector.builder();
926 treatment = DefaultTrafficTreatment.builder();
927 selector.matchEthType(Ethernet.TYPE_IPV6);
928 selector.matchEthDst(ethCriterion.mac());
929 treatment.transition(UNICAST_ROUTING_TABLE);
930 rule = DefaultFlowRule.builder()
931 .forDevice(deviceId)
932 .withSelector(selector.build())
933 .withTreatment(treatment.build())
934 .withPriority(DEFAULT_PRIORITY)
935 .fromApp(applicationId)
936 .makePermanent()
937 .forTable(TMAC_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800938 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800939 }
940
Charles Chan206506b2018-03-02 16:43:28 -0800941 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700942 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800943 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700944 ApplicationId applicationId) {
Charles Chand57552d2018-03-02 15:41:41 -0800945 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
946 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
947 TrafficSelector.Builder selector;
948 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700949 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000950
Charles Chanb4879a52017-10-20 19:09:16 -0700951 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -0800952 if (requireUnicastBeforeMulticast()) {
953 selector = DefaultTrafficSelector.builder();
954 treatment = DefaultTrafficTreatment.builder();
955 selector.matchEthType(Ethernet.TYPE_IPV4);
956 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +0000957 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -0800958 treatment.transition(UNICAST_ROUTING_TABLE);
959 rule = DefaultFlowRule.builder()
960 .forDevice(deviceId)
961 .withSelector(selector.build())
962 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +0000963 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -0800964 .fromApp(applicationId)
965 .makePermanent()
966 .forTable(TMAC_TABLE).build();
967 unicastFlows.add(rule);
968 }
969
970 selector = DefaultTrafficSelector.builder();
971 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700972 selector.matchEthType(Ethernet.TYPE_IPV4);
973 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +0000974 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -0700975 treatment.transition(MULTICAST_ROUTING_TABLE);
976 rule = DefaultFlowRule.builder()
977 .forDevice(deviceId)
978 .withSelector(selector.build())
979 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +0000980 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -0700981 .fromApp(applicationId)
982 .makePermanent()
983 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -0800984 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700985 }
986
987 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -0800988 if (requireUnicastBeforeMulticast()) {
989 selector = DefaultTrafficSelector.builder();
990 treatment = DefaultTrafficTreatment.builder();
991 selector.matchEthType(Ethernet.TYPE_IPV6);
992 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +0000993 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -0800994 treatment.transition(UNICAST_ROUTING_TABLE);
995 rule = DefaultFlowRule.builder()
996 .forDevice(deviceId)
997 .withSelector(selector.build())
998 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +0000999 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -08001000 .fromApp(applicationId)
1001 .makePermanent()
1002 .forTable(TMAC_TABLE).build();
1003 unicastFlows.add(rule);
1004 }
1005
Charles Chanb4879a52017-10-20 19:09:16 -07001006 selector = DefaultTrafficSelector.builder();
1007 treatment = DefaultTrafficTreatment.builder();
1008 selector.matchEthType(Ethernet.TYPE_IPV6);
1009 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +00001010 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001011 treatment.transition(MULTICAST_ROUTING_TABLE);
1012 rule = DefaultFlowRule.builder()
1013 .forDevice(deviceId)
1014 .withSelector(selector.build())
1015 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001016 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001017 .fromApp(applicationId)
1018 .makePermanent()
1019 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001020 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001021 }
Charles Chand57552d2018-03-02 15:41:41 -08001022 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001023 }
1024
Saurav Das822c4e22015-10-23 10:51:11 -07001025 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1026 switch (fwd.flag()) {
1027 case SPECIFIC:
1028 return processSpecific(fwd);
1029 case VERSATILE:
1030 return processVersatile(fwd);
1031 default:
1032 fail(fwd, ObjectiveError.UNKNOWN);
1033 log.warn("Unknown forwarding flag {}", fwd.flag());
1034 }
1035 return Collections.emptySet();
1036 }
1037
1038 /**
1039 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1040 * ACL table.
1041 * @param fwd the forwarding objective of type 'versatile'
1042 * @return a collection of flow rules to be sent to the switch. An empty
1043 * collection may be returned if there is a problem in processing
1044 * the flow rule
1045 */
Saurav Das52025962016-01-28 22:30:01 -08001046 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001047 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001048 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001049
Saurav Das822c4e22015-10-23 10:51:11 -07001050 if (fwd.nextId() == null && fwd.treatment() == null) {
1051 log.error("Forwarding objective {} from {} must contain "
1052 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001053 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001054 return Collections.emptySet();
1055 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001056
Saurav Das77b5e902016-01-27 17:01:59 -08001057 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1058 fwd.selector().criteria().forEach(criterion -> {
1059 if (criterion instanceof VlanIdCriterion) {
1060 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1061 // ensure that match does not include vlan = NONE as OF-DPA does not
1062 // match untagged packets this way in the ACL table.
1063 if (vlanId.equals(VlanId.NONE)) {
1064 return;
1065 }
Charles Chand1172632017-03-15 17:33:09 -07001066 if (requireVlanExtensions()) {
1067 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1068 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1069 } else {
1070 sbuilder.matchVlanId(vlanId);
1071 }
Charles Chan393339f2018-01-11 11:48:18 -08001072 } else if (criterion instanceof Icmpv6TypeCriterion) {
1073 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1074 sbuilder.matchIcmpv6Type(icmpv6Type);
1075 } else if (criterion instanceof Icmpv6CodeCriterion) {
1076 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1077 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001078 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1079 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1080 // Currently L4 dst port matching is only used by DHCP relay feature
1081 // and therefore is safe to be replaced with L4 src port matching.
1082 // We need to revisit this if L4 dst port is used for other purpose in the future.
1083 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001084 switch (criterion.type()) {
1085 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001086 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001087 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001088 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001089 break;
1090 default:
1091 sbuilder.add(criterion);
1092 }
1093 } else {
1094 sbuilder.add(criterion);
1095 }
Saurav Das77b5e902016-01-27 17:01:59 -08001096 } else {
1097 sbuilder.add(criterion);
1098 }
1099 });
1100
Saurav Das822c4e22015-10-23 10:51:11 -07001101 // XXX driver does not currently do type checking as per Tables 65-67 in
1102 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001103 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1104 if (fwd.treatment() != null) {
1105 for (Instruction ins : fwd.treatment().allInstructions()) {
1106 if (ins instanceof OutputInstruction) {
1107 OutputInstruction o = (OutputInstruction) ins;
Charles Chan93090352018-03-02 13:26:22 -08001108 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001109 ttBuilder.add(o);
1110 } else {
1111 log.warn("Only allowed treatments in versatile forwarding "
1112 + "objectives are punts to the controller");
1113 }
Charles Chan3fe71712018-06-15 18:54:18 -07001114 } else if (ins instanceof NoActionInstruction) {
1115 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001116 } else {
1117 log.warn("Cannot process instruction in versatile fwd {}", ins);
1118 }
Saurav Das822c4e22015-10-23 10:51:11 -07001119 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001120 if (fwd.treatment().clearedDeferred()) {
1121 ttBuilder.wipeDeferred();
1122 }
Saurav Das822c4e22015-10-23 10:51:11 -07001123 }
Saurav Das822c4e22015-10-23 10:51:11 -07001124 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001125 // overide case
1126 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301127 if (next == null) {
1128 fail(fwd, ObjectiveError.BADPARAMS);
1129 return Collections.emptySet();
1130 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001131 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1132 // we only need the top level group's key to point the flow to it
1133 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1134 if (group == null) {
1135 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1136 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1137 fail(fwd, ObjectiveError.GROUPMISSING);
1138 return Collections.emptySet();
1139 }
1140 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001141 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001142
1143 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1144 .fromApp(fwd.appId())
1145 .withPriority(fwd.priority())
1146 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001147 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001148 .withTreatment(ttBuilder.build())
1149 .makePermanent()
1150 .forTable(ACL_TABLE);
1151 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001152 }
1153
1154 /**
1155 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001156 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001157 *
1158 * @param fwd the forwarding objective of type 'specific'
1159 * @return a collection of flow rules. Typically there will be only one
1160 * for this type of forwarding objective. An empty set may be
1161 * returned if there is an issue in processing the objective.
1162 */
Charles Chan93090352018-03-02 13:26:22 -08001163 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001164 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001165 fwd.id(), deviceId, fwd.nextId());
1166 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1167 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1168
1169 if (isEthTypeObj) {
1170 return processEthTypeSpecific(fwd);
1171 } else if (isEthDstObj) {
1172 return processEthDstSpecific(fwd);
1173 } else {
1174 log.warn("processSpecific: Unsupported forwarding objective "
1175 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001176 fail(fwd, ObjectiveError.UNSUPPORTED);
1177 return Collections.emptySet();
1178 }
Saurav Das4ce45962015-11-24 23:21:05 -08001179 }
1180
Saurav Das4ce45962015-11-24 23:21:05 -08001181 /**
1182 * Handles forwarding rules to the IP and MPLS tables.
1183 *
1184 * @param fwd the forwarding objective
1185 * @return A collection of flow rules, or an empty set
1186 */
1187 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001188 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001189 }
1190
1191 /**
1192 * Internal implementation of processEthTypeSpecific.
1193 * <p>
1194 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1195 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1196 * The allowDefaultRoute must be set to false for OFDPA i12.
1197 * </p>
1198 *
1199 * @param fwd the forwarding objective
1200 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001201 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001202 * @return A collection of flow rules, or an empty set
1203 */
1204 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001205 boolean allowDefaultRoute,
1206 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001207 TrafficSelector selector = fwd.selector();
1208 EthTypeCriterion ethType =
1209 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001210 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001211 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001212 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001213 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001214 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001215
Saurav Das8a0732e2015-11-20 15:27:53 -08001216 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001217 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1218 return Collections.emptyList();
1219 }
1220 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001221 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001222 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001223 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001224 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001225 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001226 }
Charles Chan93090352018-03-02 13:26:22 -08001227
Charles Chan7feb6c82018-03-05 11:28:23 -08001228 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001229 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001230 // if (fwd.treatment() != null) {
1231 // for (Instruction instr : fwd.treatment().allInstructions()) {
1232 // if (instr instanceof L3ModificationInstruction &&
1233 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1234 // tb.deferred().add(instr);
1235 // }
1236 // }
1237 // }
1238
Pier Ventree0ae7a32016-11-23 09:57:42 -08001239 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1240 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1241 return Collections.emptyList();
1242 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001243 //We need to set the proper next table
1244 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1245 if (ipv6Dst.isMulticast()) {
1246 forTableId = MULTICAST_ROUTING_TABLE;
1247 } else {
1248 forTableId = UNICAST_ROUTING_TABLE;
1249 }
1250
Charles Chan7feb6c82018-03-05 11:28:23 -08001251 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001252 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001253 // if (fwd.treatment() != null) {
1254 // for (Instruction instr : fwd.treatment().allInstructions()) {
1255 // if (instr instanceof L3ModificationInstruction &&
1256 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1257 // tb.deferred().add(instr);
1258 // }
1259 // }
1260 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001261 } else {
1262 filteredSelector
1263 .matchEthType(Ethernet.MPLS_UNICAST)
1264 .matchMplsLabel(((MplsCriterion)
1265 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1266 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001267 .getCriterion(MPLS_BOS);
Alex Yashchuka3679532017-12-08 17:40:05 +02001268 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001269 filteredSelector.matchMplsBos(bos.mplsBos());
1270 }
1271 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001272 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1273 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001274
Charles Chan14967c22015-12-07 11:11:50 -08001275 if (fwd.treatment() != null) {
1276 for (Instruction instr : fwd.treatment().allInstructions()) {
1277 if (instr instanceof L2ModificationInstruction &&
1278 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001279 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001280 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuka3679532017-12-08 17:40:05 +02001281 if (requireMplsPop()) {
1282 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1283 tb.immediate().popMpls();
1284 }
1285 } else {
1286 // Skip mpls pop action for mpls_unicast label
1287 if (instr instanceof ModMplsHeaderInstruction &&
1288 !((ModMplsHeaderInstruction) instr).ethernetType()
1289 .equals(EtherType.MPLS_UNICAST.ethType())) {
1290 tb.immediate().add(instr);
1291 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001292 }
Charles Chan14967c22015-12-07 11:11:50 -08001293 }
Alex Yashchuka3679532017-12-08 17:40:05 +02001294
1295 if (requireMplsTtlModification()) {
1296 if (instr instanceof L3ModificationInstruction &&
1297 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1298 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1299 tb.immediate().decMplsTtl();
1300 }
1301 if (instr instanceof L3ModificationInstruction &&
1302 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1303 tb.immediate().add(instr);
1304 }
Charles Chan14967c22015-12-07 11:11:50 -08001305 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001306 }
1307 }
1308 }
Saurav Das822c4e22015-10-23 10:51:11 -07001309
1310 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001311 NextGroup next = getGroupForNextObjective(fwd.nextId());
1312 if (next != null) {
1313 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1314 // we only need the top level group's key to point the flow to it
1315 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1316 if (group == null) {
1317 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1318 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1319 fail(fwd, ObjectiveError.GROUPMISSING);
1320 return Collections.emptySet();
1321 }
Saurav Dasa89b95a2018-02-14 14:14:54 -08001322 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1323 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1324 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1325 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1326 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1327 return Collections.emptySet();
1328 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001329 tb.deferred().group(group.id());
Saurav Dasf3f75942018-01-25 09:49:01 -08001330 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001331 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasf3f75942018-01-25 09:49:01 -08001332 if (shouldRetry()) {
1333 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1334 Integer.toHexString(group.id().id()), deviceId,
1335 fwd.id());
1336 emptyGroup = true;
1337 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001338 }
Saurav Das25190812016-05-27 13:54:07 -07001339 } else {
1340 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1341 fwd.nextId(), deviceId, fwd.id());
1342 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1343 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001344 }
Saurav Das822c4e22015-10-23 10:51:11 -07001345 }
Charles Chancad338a2016-09-16 18:03:11 -07001346
1347 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001348 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001349 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001350 // set mpls type as apply_action
1351 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001352 }
1353 tb.transition(mplsNextTable);
1354 } else {
1355 tb.transition(ACL_TABLE);
1356 }
1357
Andrea Campanella84ac4df2018-04-30 11:48:55 +02001358 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1359 if (supportsUnicastBlackHole()) {
1360 tb.wipeDeferred();
1361 } else {
1362 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1363 return Collections.emptySet();
1364 }
1365 }
1366
Saurav Das822c4e22015-10-23 10:51:11 -07001367 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1368 .fromApp(fwd.appId())
1369 .withPriority(fwd.priority())
1370 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001371 .withSelector(filteredSelector.build())
1372 .withTreatment(tb.build())
1373 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001374
1375 if (fwd.permanent()) {
1376 ruleBuilder.makePermanent();
1377 } else {
1378 ruleBuilder.makeTemporary(fwd.timeout());
1379 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001380 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1381 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001382 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001383 flowRuleCollection.add(
1384 defaultRoute(fwd, complementarySelector, forTableId, tb)
1385 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001386 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1387 }
Saurav Dasf3f75942018-01-25 09:49:01 -08001388
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001389 if (emptyGroup) {
1390 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1391 RETRY_MS, TimeUnit.MILLISECONDS);
1392 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001393 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001394 }
1395
Charles Chan93090352018-03-02 13:26:22 -08001396 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001397 TrafficSelector.Builder extBuilder,
1398 ForwardingObjective fwd,
1399 boolean allowDefaultRoute) {
1400 TrafficSelector selector = fwd.selector();
1401
1402 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1403 if (ipv4Dst.isMulticast()) {
1404 if (ipv4Dst.prefixLength() != 32) {
1405 log.warn("Multicast specific forwarding objective can only be /32");
1406 fail(fwd, ObjectiveError.BADPARAMS);
1407 return -1;
1408 }
1409 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1410 if (assignedVlan == null) {
1411 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1412 fail(fwd, ObjectiveError.BADPARAMS);
1413 return -1;
1414 }
Charles Chand1172632017-03-15 17:33:09 -07001415 if (requireVlanExtensions()) {
1416 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1417 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1418 } else {
1419 builderToUpdate.matchVlanId(assignedVlan);
1420 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001421 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1422 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1423 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1424 } else {
1425 if (ipv4Dst.prefixLength() == 0) {
1426 if (allowDefaultRoute) {
1427 // The entire IPV4_DST field is wildcarded intentionally
1428 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1429 } else {
1430 // NOTE: The switch does not support matching 0.0.0.0/0
1431 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1432 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1433 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1434 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1435 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1436 }
1437 } else {
1438 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1439 }
1440 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1441 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1442 }
1443 return 0;
1444 }
1445
1446 /**
1447 * Helper method to build Ipv6 selector using the selector provided by
1448 * a forwarding objective.
1449 *
1450 * @param builderToUpdate the builder to update
1451 * @param fwd the selector to read
1452 * @return 0 if the update ends correctly. -1 if the matches
1453 * are not yet supported
1454 */
Charles Chan93090352018-03-02 13:26:22 -08001455 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001456 ForwardingObjective fwd) {
1457
1458 TrafficSelector selector = fwd.selector();
1459
1460 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1461 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001462 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1463 log.warn("Multicast specific forwarding objective can only be /128");
1464 fail(fwd, ObjectiveError.BADPARAMS);
1465 return -1;
1466 }
1467 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1468 if (assignedVlan == null) {
1469 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1470 fail(fwd, ObjectiveError.BADPARAMS);
1471 return -1;
1472 }
1473 if (requireVlanExtensions()) {
1474 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1475 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1476 } else {
1477 builderToUpdate.matchVlanId(assignedVlan);
1478 }
1479 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1480 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1481 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1482 } else {
1483 if (ipv6Dst.prefixLength() != 0) {
1484 builderToUpdate.matchIPv6Dst(ipv6Dst);
1485 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001486 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1487 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1488 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001489 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001490 return 0;
1491 }
1492
Charles Chan93090352018-03-02 13:26:22 -08001493 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001494 TrafficSelector.Builder complementarySelector,
1495 int forTableId,
1496 TrafficTreatment.Builder tb) {
1497 FlowRule.Builder rule = DefaultFlowRule.builder()
1498 .fromApp(fwd.appId())
1499 .withPriority(fwd.priority())
1500 .forDevice(deviceId)
1501 .withSelector(complementarySelector.build())
1502 .withTreatment(tb.build())
1503 .forTable(forTableId);
1504 if (fwd.permanent()) {
1505 rule.makePermanent();
1506 } else {
1507 rule.makeTemporary(fwd.timeout());
1508 }
1509 return rule.build();
1510 }
1511
Saurav Das4ce45962015-11-24 23:21:05 -08001512 /**
1513 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1514 * allowed in the bridging table - instead we use L2 Interface group or
1515 * L2 flood group
1516 *
1517 * @param fwd the forwarding objective
1518 * @return A collection of flow rules, or an empty set
1519 */
1520 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1521 List<FlowRule> rules = new ArrayList<>();
1522
1523 // Build filtered selector
1524 TrafficSelector selector = fwd.selector();
1525 EthCriterion ethCriterion = (EthCriterion) selector
1526 .getCriterion(Criterion.Type.ETH_DST);
1527 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1528 .getCriterion(Criterion.Type.VLAN_VID);
1529
1530 if (vlanIdCriterion == null) {
1531 log.warn("Forwarding objective for bridging requires vlan. Not "
1532 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1533 fail(fwd, ObjectiveError.BADPARAMS);
1534 return Collections.emptySet();
1535 }
1536
1537 TrafficSelector.Builder filteredSelectorBuilder =
1538 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001539
1540 if (!ethCriterion.mac().equals(NONE) &&
1541 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001542 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1543 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1544 fwd.id(), fwd.nextId(), deviceId);
1545 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001546 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001547 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1548 + "in dev:{} for vlan:{}",
1549 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1550 }
Charles Chand1172632017-03-15 17:33:09 -07001551 if (requireVlanExtensions()) {
1552 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1553 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1554 } else {
1555 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1556 }
Saurav Das4ce45962015-11-24 23:21:05 -08001557 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1558
1559 if (fwd.treatment() != null) {
1560 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1561 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1562 }
1563
1564 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1565 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001566 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001567 if (next != null) {
1568 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1569 // we only need the top level group's key to point the flow to it
1570 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1571 if (group != null) {
1572 treatmentBuilder.deferred().group(group.id());
1573 } else {
1574 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1575 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1576 fail(fwd, ObjectiveError.GROUPMISSING);
1577 return Collections.emptySet();
1578 }
1579 }
1580 }
1581 treatmentBuilder.immediate().transition(ACL_TABLE);
1582 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1583
1584 // Build bridging table entries
1585 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1586 flowRuleBuilder.fromApp(fwd.appId())
1587 .withPriority(fwd.priority())
1588 .forDevice(deviceId)
1589 .withSelector(filteredSelector)
1590 .withTreatment(filteredTreatment)
1591 .forTable(BRIDGING_TABLE);
1592 if (fwd.permanent()) {
1593 flowRuleBuilder.makePermanent();
1594 } else {
1595 flowRuleBuilder.makeTemporary(fwd.timeout());
1596 }
1597 rules.add(flowRuleBuilder.build());
1598 return rules;
1599 }
1600
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001601 //////////////////////////////////////
1602 // Helper Methods and Classes
1603 //////////////////////////////////////
1604
1605 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1606 TrafficSelector selector = fwd.selector();
1607 EthTypeCriterion ethType = (EthTypeCriterion) selector
1608 .getCriterion(Criterion.Type.ETH_TYPE);
1609 return !((ethType == null) ||
1610 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001611 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1612 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001613 }
1614
1615 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1616 TrafficSelector selector = fwd.selector();
1617 EthCriterion ethDst = (EthCriterion) selector
1618 .getCriterion(Criterion.Type.ETH_DST);
1619 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1620 .getCriterion(Criterion.Type.VLAN_VID);
1621 return !(ethDst == null && vlanId == null);
1622 }
1623
Charles Chan93090352018-03-02 13:26:22 -08001624 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001625 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1626 if (next != null) {
1627 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1628 if (gkeys != null && !gkeys.isEmpty()) {
1629 return next;
1630 } else {
1631 log.warn("Empty next group found in FlowObjective store for "
1632 + "next-id:{} in dev:{}", nextId, deviceId);
1633 }
1634 } else {
1635 log.warn("next-id {} not found in Flow objective store for dev:{}",
1636 nextId, deviceId);
1637 }
1638 return null;
1639 }
1640
Charles Chan188ebf52015-12-23 00:15:11 -08001641 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001642 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001643 }
1644
Charles Chan188ebf52015-12-23 00:15:11 -08001645 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001646 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001647 }
Saurav Das24431192016-03-07 19:13:00 -08001648
Saurav Das24431192016-03-07 19:13:00 -08001649 @Override
1650 public List<String> getNextMappings(NextGroup nextGroup) {
1651 List<String> mappings = new ArrayList<>();
1652 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1653 for (Deque<GroupKey> gkd : gkeys) {
1654 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001655 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001656 for (GroupKey gk : gkd) {
1657 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001658 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001659 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001660 continue;
1661 }
1662 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1663 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001664 lastGroup = g;
1665 }
1666 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001667 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001668 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001669 lastGroupIns = lastGroup.buckets().buckets().get(0)
1670 .treatment().allInstructions();
1671 }
1672 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001673 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001674 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001675 }
1676 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001677 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001678 }
1679 return mappings;
1680 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001681
Saurav Dasa89b95a2018-02-14 14:14:54 -08001682 /**
1683 * Returns true iff the given selector matches on BOS==true, indicating that
1684 * the selector is trying to match on a label that is bottom-of-stack.
1685 *
1686 * @param selector the given match
1687 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1688 * expressed in the given selector
1689 */
Pier Ventre140a8942016-11-02 07:26:38 -07001690 static boolean isMplsBos(TrafficSelector selector) {
1691 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1692 return bosCriterion != null && bosCriterion.mplsBos();
1693 }
1694
Saurav Dasa89b95a2018-02-14 14:14:54 -08001695 /**
1696 * Returns true iff the given selector matches on BOS==false, indicating
1697 * that the selector is trying to match on a label that is not the
1698 * bottom-of-stack label.
1699 *
1700 * @param selector the given match
1701 * @return true iff BoS==false;
1702 * false if BOS==true, or BOS matching is not expressed in the given selector
1703 */
Pier Ventre140a8942016-11-02 07:26:38 -07001704 static boolean isNotMplsBos(TrafficSelector selector) {
1705 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1706 return bosCriterion != null && !bosCriterion.mplsBos();
1707 }
1708
Saurav Dasa89b95a2018-02-14 14:14:54 -08001709 /**
1710 * Returns true iff the forwarding objective includes a treatment to pop the
1711 * MPLS label.
1712 *
1713 * @param fwd the given forwarding objective
1714 * @return true iff mpls pop treatment exists
1715 */
1716 static boolean isMplsPop(ForwardingObjective fwd) {
1717 if (fwd.treatment() != null) {
1718 for (Instruction instr : fwd.treatment().allInstructions()) {
1719 if (instr instanceof L2ModificationInstruction
1720 && ((L2ModificationInstruction) instr)
1721 .subtype() == L2SubType.MPLS_POP) {
1722 return true;
1723 }
1724 }
1725 }
1726 return false;
1727 }
1728
Charles Chand9e47c62017-10-05 15:17:15 -07001729 private static boolean isIpv6(TrafficSelector selector) {
1730 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1731 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1732 }
1733
Charles Chan93090352018-03-02 13:26:22 -08001734 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001735 if (selector == null) {
1736 return null;
1737 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001738 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1739 return (criterion == null)
1740 ? null : ((VlanIdCriterion) criterion).vlanId();
1741 }
1742
jayakumarthazhathad3636a2018-10-01 00:51:54 +05301743 static MacAddress readEthDstFromSelector(TrafficSelector selector) {
1744 if (selector == null) {
1745 return null;
1746 }
1747 Criterion criterion = selector.getCriterion(Criterion.Type.ETH_DST);
1748 return (criterion == null)
1749 ? null : ((EthCriterion) criterion).mac();
1750 }
1751
Charles Chand9e47c62017-10-05 15:17:15 -07001752 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001753 if (selector == null) {
1754 return null;
1755 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001756 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1757 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1758 }
Charles Chand55e84d2016-03-30 17:54:24 -07001759
1760 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001761 if (treatment == null) {
1762 return null;
1763 }
Charles Chand55e84d2016-03-30 17:54:24 -07001764 for (Instruction i : treatment.allInstructions()) {
1765 if (i instanceof ModVlanIdInstruction) {
1766 return ((ModVlanIdInstruction) i).vlanId();
1767 }
1768 }
1769 return null;
1770 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001771
Charles Chand57552d2018-03-02 15:41:41 -08001772 private static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
1773 if (treatment == null) {
1774 return null;
1775 }
1776 for (Instruction i : treatment.allInstructions()) {
1777 if (i instanceof ModEtherInstruction) {
1778 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1779 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1780 return modEtherInstruction.mac();
1781 }
1782 }
1783 }
1784 return null;
1785 }
1786
jayakumarthazhathad3636a2018-10-01 00:51:54 +05301787 static ExtensionSelector readExtensionFromSelector(TrafficSelector selector) {
1788 if (selector == null) {
1789 return null;
1790 }
1791 ExtensionCriterion criterion = (ExtensionCriterion) selector.getCriterion(Criterion.Type.EXTENSION);
1792 return (criterion == null) ? null : criterion.extensionSelector();
1793 }
1794
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001795 /**
1796 * Utility class that retries sending flows a fixed number of times, even if
1797 * some of the attempts are successful. Used only for forwarding objectives.
1798 */
Alex Yashchuka3679532017-12-08 17:40:05 +02001799 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001800 int attempts = MAX_RETRY_ATTEMPTS;
1801 private Collection<FlowRule> retryFlows;
1802 private ForwardingObjective fwd;
1803
Charles Chan93090352018-03-02 13:26:22 -08001804 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001805 this.fwd = fwd;
1806 this.retryFlows = retryFlows;
1807 }
1808
1809 @Override
1810 public void run() {
1811 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1812 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1813 sendForward(fwd, retryFlows);
1814 if (--attempts > 0) {
1815 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1816 }
1817 }
1818 }
1819
Saurav Das822c4e22015-10-23 10:51:11 -07001820}