blob: e63cda03611effc703675933f3a7deec545f779e [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;
55import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080056import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
57import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080058import org.onosproject.net.flow.criteria.MplsBosCriterion;
59import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070060import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070061import org.onosproject.net.flow.criteria.TcpPortCriterion;
62import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070063import org.onosproject.net.flow.criteria.VlanIdCriterion;
64import org.onosproject.net.flow.instructions.Instruction;
65import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chan3fe71712018-06-15 18:54:18 -070066import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080068import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070069import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Charles Chand57552d2018-03-02 15:41:41 -080070import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Alex Yashchuka3679532017-12-08 17:40:05 +020071import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080072import org.onosproject.net.flow.instructions.L3ModificationInstruction;
73import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070074import org.onosproject.net.flowobjective.FilteringObjective;
75import org.onosproject.net.flowobjective.FlowObjectiveStore;
76import org.onosproject.net.flowobjective.ForwardingObjective;
77import org.onosproject.net.flowobjective.NextObjective;
78import org.onosproject.net.flowobjective.Objective;
79import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.group.DefaultGroupKey;
81import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070083import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070084import org.onosproject.store.serializers.KryoNamespaces;
85import org.slf4j.Logger;
86
Pier Ventree0ae7a32016-11-23 09:57:42 -080087import java.util.ArrayDeque;
88import java.util.ArrayList;
89import java.util.Collection;
90import java.util.Collections;
91import java.util.Deque;
92import java.util.List;
93import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080094import java.util.concurrent.ScheduledExecutorService;
95import java.util.concurrent.TimeUnit;
96
97import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080098import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -070099import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
100import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800101import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800102import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700103import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700104import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi5af88fe2018-02-01 10:23:12 +0100105import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800106import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700107import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800108
Saurav Das822c4e22015-10-23 10:51:11 -0700109/**
110 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700111 */
Charles Chan361154b2016-03-24 10:23:39 -0700112public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700113 protected static final int PORT_TABLE = 0;
114 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800115 protected static final int VLAN_1_TABLE = 11;
116 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
117 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700118 protected static final int TMAC_TABLE = 20;
119 protected static final int UNICAST_ROUTING_TABLE = 30;
120 protected static final int MULTICAST_ROUTING_TABLE = 40;
121 protected static final int MPLS_TABLE_0 = 23;
122 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700123 protected static final int MPLS_L3_TYPE_TABLE = 27;
124 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700125 protected static final int BRIDGING_TABLE = 50;
126 protected static final int ACL_TABLE = 60;
127 protected static final int MAC_LEARNING_TABLE = 254;
128 protected static final long OFPP_MAX = 0xffffff00L;
129
Saurav Das52025962016-01-28 22:30:01 -0800130 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800131 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700132 protected static final int LOWEST_PRIORITY = 0x0;
133
Pier Ventre42287df2016-11-09 14:17:26 -0800134 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800135 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
136 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
137
Pier Ventre70d53ba2016-11-17 22:26:29 -0800138 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800139 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
140 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
141
Saurav Das822c4e22015-10-23 10:51:11 -0700142 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700143 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700144 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700145 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800146 protected GroupService groupService;
147 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700148 protected DeviceId deviceId;
149 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700150 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800151 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700152 .register(KryoNamespaces.API)
153 .register(GroupKey.class)
154 .register(DefaultGroupKey.class)
155 .register(OfdpaNextGroup.class)
156 .register(ArrayDeque.class)
157 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700158
Charles Chan425854b2016-04-11 15:32:12 -0700159 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700160
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700161 // flows installations to be retried
Charles Chan93090352018-03-02 13:26:22 -0800162 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700163 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan93090352018-03-02 13:26:22 -0800164 private static final int MAX_RETRY_ATTEMPTS = 10;
165 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800166
Saurav Das822c4e22015-10-23 10:51:11 -0700167 @Override
168 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700169 this.deviceId = deviceId;
170
Charles Chan425854b2016-04-11 15:32:12 -0700171 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700172 coreService = serviceDirectory.get(CoreService.class);
173 flowRuleService = serviceDirectory.get(FlowRuleService.class);
174 groupService = serviceDirectory.get(GroupService.class);
175 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700176 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700177
Charles Chan40132b32017-01-22 00:19:37 -0800178 initDriverId();
179 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700180
Saurav Das822c4e22015-10-23 10:51:11 -0700181 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700182 }
183
Charles Chan40132b32017-01-22 00:19:37 -0800184 protected void initDriverId() {
185 driverId = coreService.registerApplication(
186 "org.onosproject.driver.Ofdpa2Pipeline");
187 }
188
189 protected void initGroupHander(PipelinerContext context) {
190 groupHandler = new Ofdpa2GroupHandler();
191 groupHandler.init(deviceId, context);
192 }
193
Saurav Das822c4e22015-10-23 10:51:11 -0700194 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800195 // OF-DPA does not require initializing the pipeline as it puts default
196 // rules automatically in the hardware. However emulation of OFDPA in
197 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700198 }
199
Charles Chand1172632017-03-15 17:33:09 -0700200 /**
Alex Yashchuka3679532017-12-08 17:40:05 +0200201 * Determines whether this pipeline requires MPLS POP instruction.
202 *
203 * @return true to use MPLS POP instruction
204 */
205 public boolean requireMplsPop() {
206 return true;
207 }
208
209 /**
210 * Determines whether this pipeline requires MPLS BOS match.
211 *
212 * @return true to use MPLS BOS match
213 */
214 public boolean requireMplsBosMatch() {
215 return true;
216 }
217
218 /**
219 * Determines whether this pipeline requires MPLS TTL decrement and copy.
220 *
221 * @return true to use MPLS TTL decrement and copy
222 */
223 public boolean requireMplsTtlModification() {
224 return true;
225 }
226
227 /**
Charles Chand1172632017-03-15 17:33:09 -0700228 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
229 *
230 * @return true to use the extensions
231 */
232 protected boolean requireVlanExtensions() {
233 return true;
234 }
235
Saurav Das86d13e82017-04-28 17:03:48 -0700236 /**
237 * Determines whether in-port should be matched on in TMAC table rules.
238 *
239 * @return true if match on in-port should be programmed
240 */
241 protected boolean matchInPortTmacTable() {
242 return true;
243 }
244
Charles Chand9e47c62017-10-05 15:17:15 -0700245 /**
246 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
247 *
248 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
249 */
250 protected boolean supportIpv6L4Dst() {
251 return true;
252 }
253
Saurav Dasf3f75942018-01-25 09:49:01 -0800254 /**
255 * Determines whether this driver should continue to retry flows that point
256 * to empty groups. See CORD-554.
257 *
258 * @return true if the driver should retry flows
259 */
260 protected boolean shouldRetry() {
261 return true;
262 }
263
Charles Chand57552d2018-03-02 15:41:41 -0800264 /**
265 * Determines whether this driver requires unicast flow to be installed before multicast flow
266 * in TMAC table.
267 *
268 * @return true if required
269 */
270 protected boolean requireUnicastBeforeMulticast() {
271 return false;
272 }
273
Andrea Campanella84ac4df2018-04-30 11:48:55 +0200274 /**
275 * Determines whether this driver supports installing a clearDeferred action on table 30.
276 *
277 * @return true if required
278 */
279 protected boolean supportsUnicastBlackHole() {
280 return true;
281 }
282
Saurav Das822c4e22015-10-23 10:51:11 -0700283 //////////////////////////////////////
284 // Flow Objectives
285 //////////////////////////////////////
286
287 @Override
288 public void filter(FilteringObjective filteringObjective) {
289 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
290 processFilter(filteringObjective,
291 filteringObjective.op() == Objective.Operation.ADD,
292 filteringObjective.appId());
293 } else {
294 // Note that packets that don't match the PERMIT filter are
295 // automatically denied. The DENY filter is used to deny packets
296 // that are otherwise permitted by the PERMIT filter.
297 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530298 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700299 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
300 }
301 }
302
303 @Override
304 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700305 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700306 if (rules == null || rules.isEmpty()) {
307 // Assumes fail message has already been generated to the objective
308 // context. Returning here prevents spurious pass message to be
309 // generated by FlowRule service for empty flowOps.
310 return;
311 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700312 sendForward(fwd, rules);
313 }
314
Charles Chan93090352018-03-02 13:26:22 -0800315 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700316 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700317 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700318 case ADD:
319 rules.stream()
320 .filter(Objects::nonNull)
321 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800322 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700323 break;
324 case REMOVE:
325 rules.stream()
326 .filter(Objects::nonNull)
327 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800328 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700329 break;
330 default:
331 fail(fwd, ObjectiveError.UNKNOWN);
332 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700333 }
334
Saurav Das822c4e22015-10-23 10:51:11 -0700335 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
336 @Override
337 public void onSuccess(FlowRuleOperations ops) {
338 pass(fwd);
339 }
340
341 @Override
342 public void onError(FlowRuleOperations ops) {
343 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
344 }
345 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700346 }
347
348 @Override
349 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800350 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
351 switch (nextObjective.op()) {
352 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800353 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800354 log.warn("Cannot add next {} that already exists in device {}",
355 nextObjective.id(), deviceId);
356 return;
357 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700358 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800359 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700360 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800361 break;
362 case ADD_TO_EXISTING:
363 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700364 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800365 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700366 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800367 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800368 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800369 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
370 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800371
372 // by design multiple pending bucket is allowed for the group
373 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
374 if (pendBkts == null) {
375 pendBkts = Sets.newHashSet();
376 }
377 pendBkts.add(nextObjective);
378 return pendBkts;
379 });
Saurav Das4f980082015-11-05 13:39:15 -0800380 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800381 break;
382 case REMOVE:
383 if (nextGroup == null) {
384 log.warn("Cannot remove next {} that does not exist in device {}",
385 nextObjective.id(), deviceId);
386 return;
387 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700388 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800389 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700390 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800391 break;
392 case REMOVE_FROM_EXISTING:
393 if (nextGroup == null) {
394 log.warn("Cannot remove from next {} that does not exist in device {}",
395 nextObjective.id(), deviceId);
396 return;
397 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700398 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800399 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700400 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800401 break;
Jonghwan Hyun25199f22018-02-12 16:43:45 +0900402 case MODIFY:
403 if (nextGroup == null) {
404 log.warn("Cannot modify next {} that does not exist in device {}",
405 nextObjective.id(), deviceId);
406 return;
407 }
408 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
409 nextObjective.id(), deviceId);
410 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
411 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700412 case VERIFY:
413 if (nextGroup == null) {
414 log.warn("Cannot verify next {} that does not exist in device {}",
415 nextObjective.id(), deviceId);
416 return;
417 }
418 log.debug("Processing NextObjective id {} in dev {} - verify",
419 nextObjective.id(), deviceId);
420 groupHandler.verifyGroup(nextObjective, nextGroup);
421 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800422 default:
Saurav Das4f980082015-11-05 13:39:15 -0800423 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700424 }
425 }
426
427 //////////////////////////////////////
428 // Flow handling
429 //////////////////////////////////////
430
431 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700432 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
433 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700434 *
435 * @param filt the filtering objective
436 * @param install indicates whether to add or remove the objective
437 * @param applicationId the application that sent this objective
438 */
Saurav Das52025962016-01-28 22:30:01 -0800439 protected void processFilter(FilteringObjective filt,
440 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700441 // This driver only processes filtering criteria defined with switch
442 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530443 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700444 EthCriterion ethCriterion = null;
445 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700446 if (!filt.key().equals(Criteria.dummy()) &&
447 filt.key().type() == Criterion.Type.IN_PORT) {
448 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700449 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530450 if (portCriterion == null) {
451 log.debug("No IN_PORT defined in filtering objective from app: {}",
452 applicationId);
453 } else {
454 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
455 portCriterion.port());
456 }
Saurav Das822c4e22015-10-23 10:51:11 -0700457 // convert filtering conditions for switch-intfs into flowrules
458 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
459 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700460 switch (criterion.type()) {
461 case ETH_DST:
462 case ETH_DST_MASKED:
463 ethCriterion = (EthCriterion) criterion;
464 break;
465 case VLAN_VID:
466 vidCriterion = (VlanIdCriterion) criterion;
467 break;
468 default:
469 log.warn("Unsupported filter {}", criterion);
470 fail(filt, ObjectiveError.UNSUPPORTED);
471 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700472 }
473 }
474
Saurav Das0e99e2b2015-10-28 12:39:42 -0700475 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800476 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700477 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chan9174e6b2017-12-19 19:55:57 -0800478 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800479 assignedVlan = vidCriterion.vlanId();
Pier3cc7e662018-03-07 11:42:50 +0100480 }
481 // If the meta VLAN is present let's update the assigned vlan
482 if (filt.meta() != null) {
483 VlanId metaVlan = readVlanFromTreatment(filt.meta());
484 if (metaVlan != null) {
485 assignedVlan = metaVlan;
486 }
Charles Chand55e84d2016-03-30 17:54:24 -0700487 }
Charles Chane849c192016-01-11 18:28:54 -0800488
Charles Chand55e84d2016-03-30 17:54:24 -0700489 if (assignedVlan == null) {
490 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800491 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700492 log.debug("VLAN ID in criterion={}, metadata={}",
493 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
494 fail(filt, ObjectiveError.BADPARAMS);
495 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700496 }
497 }
498
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800499 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700500 // NOTE: it is possible that a filtering objective only has vidCriterion
Daniel Ginsburg16e5ed42018-04-30 19:27:19 -0400501 log.debug("filtering objective missing dstMac, won't program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700502 } else {
Charles Chand57552d2018-03-02 15:41:41 -0800503 MacAddress unicastMac = readEthDstFromTreatment(filt.meta());
Charles Chan206506b2018-03-02 16:43:28 -0800504 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
Charles Chand57552d2018-03-02 15:41:41 -0800505 vidCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800506 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800507 log.trace("Starting a new flow rule stage for TMAC table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800508 ops.newStage();
Charles Chan9174e6b2017-12-19 19:55:57 -0800509
Charles Chan206506b2018-03-02 16:43:28 -0800510 for (FlowRule flowRule : flowRules) {
Charles Chan893a1d72018-05-10 22:19:25 +0000511 log.trace("{} flow rules in TMAC table: {} for dev: {}",
Charles Chan206506b2018-03-02 16:43:28 -0800512 (install) ? "adding" : "removing", flowRules, deviceId);
Charles Chan206506b2018-03-02 16:43:28 -0800513 if (install) {
514 ops = ops.add(flowRule);
Charles Chan9174e6b2017-12-19 19:55:57 -0800515 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800516 // NOTE: Only remove TMAC flow when there is no more enabled port within the
517 // same VLAN on this device if TMAC doesn't support matching on in_port.
518 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
519 ops = ops.remove(flowRule);
520 } else {
521 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
522 }
Charles Chan9174e6b2017-12-19 19:55:57 -0800523 }
524 }
Saurav Das822c4e22015-10-23 10:51:11 -0700525 }
526 }
527
Charles Chan985b12e2016-05-11 19:47:22 -0700528 if (vidCriterion == null) {
529 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530530 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700531 } else {
Charles Chan206506b2018-03-02 16:43:28 -0800532 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800533 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan206506b2018-03-02 16:43:28 -0800534 for (List<FlowRule> flowRules : allStages) {
Charles Chand57552d2018-03-02 15:41:41 -0800535 log.trace("Starting a new flow rule stage for VLAN table flow");
Charles Chan206506b2018-03-02 16:43:28 -0800536 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800537
Charles Chan206506b2018-03-02 16:43:28 -0800538 for (FlowRule flowRule : flowRules) {
539 log.trace("{} flow rules in VLAN table: {} for dev: {}",
540 (install) ? "adding" : "removing", flowRule, deviceId);
541 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700542 }
Saurav Das822c4e22015-10-23 10:51:11 -0700543 }
544 }
545
Saurav Das822c4e22015-10-23 10:51:11 -0700546 // apply filtering flow rules
547 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
548 @Override
549 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800550 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700551 ops.stages().get(0).size(), deviceId);
552 pass(filt);
553 }
554
555 @Override
556 public void onError(FlowRuleOperations ops) {
557 log.info("Failed to apply all filtering rules in dev {}", deviceId);
558 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
559 }
560 }));
561
562 }
563
564 /**
Charles Chand1172632017-03-15 17:33:09 -0700565 * Internal implementation of processVlanIdFilter.
566 * <p>
567 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
568 * Since it is non-OF spec, we need an extension treatment for that.
569 * The useVlanExtension must be set to false for OFDPA i12.
570 * </p>
Charles Chan206506b2018-03-02 16:43:28 -0800571 * <p>
572 * NOTE: Separate VLAN filtering rules and assignment rules
573 * into different stages in order to guarantee that filtering rules
574 * always go first, as required by OFDPA.
575 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700576 *
Charles Chanf9e98652016-09-07 16:54:23 -0700577 * @param portCriterion port on device for which this filter is programmed
578 * @param vidCriterion vlan assigned to port, or NONE for untagged
579 * @param assignedVlan assigned vlan-id for untagged packets
580 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800581 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700582 */
Charles Chan206506b2018-03-02 16:43:28 -0800583 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700584 VlanIdCriterion vidCriterion,
585 VlanId assignedVlan,
586 ApplicationId applicationId) {
Charles Chan206506b2018-03-02 16:43:28 -0800587 List<FlowRule> filteringRules = new ArrayList<>();
588 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700589 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
590 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800591 TrafficSelector.Builder preSelector = null;
592 TrafficTreatment.Builder preTreatment = null;
593
Saurav Das4f980082015-11-05 13:39:15 -0800594 treatment.transition(TMAC_TABLE);
595
Saurav Das822c4e22015-10-23 10:51:11 -0700596 if (vidCriterion.vlanId() == VlanId.NONE) {
597 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700598 preSelector = DefaultTrafficSelector.builder();
599 if (requireVlanExtensions()) {
600 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
601 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700602 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
603 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700604
605 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
606 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700607 } else {
Charles Chand1172632017-03-15 17:33:09 -0700608 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700609 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700610
611 preSelector.matchVlanId(assignedVlan);
612 }
613 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
614 } else {
615 if (requireVlanExtensions()) {
616 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
617 selector.extension(ofdpaMatchVlanVid, deviceId);
618 } else {
619 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700620 }
Charles Chan14967c22015-12-07 11:11:50 -0800621
Charles Chand55e84d2016-03-30 17:54:24 -0700622 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700623 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800624 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
625 treatment.extension(ofdpaSetVlanVid, deviceId);
626 } else {
627 treatment.setVlanId(assignedVlan);
628 }
Charles Chand55e84d2016-03-30 17:54:24 -0700629 }
Saurav Das822c4e22015-10-23 10:51:11 -0700630 }
Saurav Das822c4e22015-10-23 10:51:11 -0700631
632 // ofdpa cannot match on ALL portnumber, so we need to use separate
633 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800634 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530635 if (portCriterion != null) {
636 if (PortNumber.ALL.equals(portCriterion.port())) {
637 for (Port port : deviceService.getPorts(deviceId)) {
638 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
639 portnums.add(port.number());
640 }
Saurav Das822c4e22015-10-23 10:51:11 -0700641 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530642 } else {
643 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700644 }
645 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530646 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan206506b2018-03-02 16:43:28 -0800647 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700648 }
Saurav Das4f980082015-11-05 13:39:15 -0800649
Saurav Das822c4e22015-10-23 10:51:11 -0700650 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800651 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700652 selector.matchInPort(pnum);
653 FlowRule rule = DefaultFlowRule.builder()
654 .forDevice(deviceId)
655 .withSelector(selector.build())
656 .withTreatment(treatment.build())
657 .withPriority(DEFAULT_PRIORITY)
658 .fromApp(applicationId)
659 .makePermanent()
660 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800661 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800662
663 if (preSelector != null) {
664 preSelector.matchInPort(pnum);
665 FlowRule preRule = DefaultFlowRule.builder()
666 .forDevice(deviceId)
667 .withSelector(preSelector.build())
668 .withTreatment(preTreatment.build())
669 .withPriority(DEFAULT_PRIORITY)
670 .fromApp(applicationId)
671 .makePermanent()
672 .forTable(VLAN_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800673 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800674 }
Saurav Das822c4e22015-10-23 10:51:11 -0700675 }
Charles Chan1e58aa62018-03-06 22:10:15 -0800676 return ImmutableList.of(filteringRules, assignmentRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700677 }
678
679 /**
680 * Allows routed packets with correct destination MAC to be directed
Charles Chand57552d2018-03-02 15:41:41 -0800681 * to unicast routing table, multicast routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700682 *
683 * @param portCriterion port on device for which this filter is programmed
684 * @param ethCriterion dstMac of device for which is filter is programmed
685 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700686 * @param assignedVlan assigned vlan-id for untagged packets
Charles Chand57552d2018-03-02 15:41:41 -0800687 * @param unicastMac some switches require a unicast TMAC flow to be programmed before multicast
688 * TMAC flow. This MAC address will be used for the unicast TMAC flow.
689 * This is unused if the filtering objective is a unicast.
Saurav Das822c4e22015-10-23 10:51:11 -0700690 * @param applicationId for application programming this filter
Charles Chan206506b2018-03-02 16:43:28 -0800691 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700692
693 */
Charles Chan206506b2018-03-02 16:43:28 -0800694 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700695 EthCriterion ethCriterion,
696 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700697 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800698 MacAddress unicastMac,
Saurav Das822c4e22015-10-23 10:51:11 -0700699 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800700 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530701 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800702 return processEthDstOnlyFilter(ethCriterion, applicationId);
703 }
704
Charles Chan5b9df8d2016-03-28 22:21:40 -0700705 // Multicast MAC
706 if (ethCriterion.mask() != null) {
Charles Chand57552d2018-03-02 15:41:41 -0800707 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700708 }
709
Saurav Das822c4e22015-10-23 10:51:11 -0700710 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530711 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700712 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700713 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530714 List<FlowRule> rules = new ArrayList<>();
715 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
716 if (vidCriterion != null && requireVlanExtensions()) {
717 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
718 }
Saurav Das822c4e22015-10-23 10:51:11 -0700719 // ofdpa cannot match on ALL portnumber, so we need to use separate
720 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700721 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530722 if (portCriterion != null) {
723 if (PortNumber.ALL.equals(portCriterion.port())) {
724 for (Port port : deviceService.getPorts(deviceId)) {
725 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
726 portnums.add(port.number());
727 }
Saurav Das822c4e22015-10-23 10:51:11 -0700728 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530729 } else {
730 portnums.add(portCriterion.port());
731 }
732 for (PortNumber pnum : portnums) {
733 rules.add(buildTmacRuleForIpv4(ethCriterion,
734 vidCriterion,
735 ofdpaMatchVlanVid,
736 applicationId,
737 pnum));
738 rules.add(buildTmacRuleForMpls(ethCriterion,
739 vidCriterion,
740 ofdpaMatchVlanVid,
741 applicationId,
742 pnum));
743 rules.add(buildTmacRuleForIpv6(ethCriterion,
744 vidCriterion,
745 ofdpaMatchVlanVid,
746 applicationId,
747 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700748 }
749 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530750 rules.add(buildTmacRuleForIpv4(ethCriterion,
751 vidCriterion,
752 ofdpaMatchVlanVid,
753 applicationId,
754 null));
755 rules.add(buildTmacRuleForMpls(ethCriterion,
756 vidCriterion,
757 ofdpaMatchVlanVid,
758 applicationId,
759 null));
760 rules.add(buildTmacRuleForIpv6(ethCriterion,
761 vidCriterion,
762 ofdpaMatchVlanVid,
763 applicationId,
764 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700765 }
Charles Chan206506b2018-03-02 16:43:28 -0800766 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700767 }
768
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530769 /**
770 * Builds TMAC rules for IPv4 packets.
771 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800772 * @param ethCriterion dst mac matching
773 * @param vidCriterion vlan id assigned to the port
774 * @param ofdpaMatchVlanVid OFDPA vlan id matching
775 * @param applicationId application id
776 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530777 * @return TMAC rule for IPV4 packets
778 */
779 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
780 VlanIdCriterion vidCriterion,
781 OfdpaMatchVlanVid ofdpaMatchVlanVid,
782 ApplicationId applicationId,
783 PortNumber pnum) {
784 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
785 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
786 if (pnum != null) {
787 if (matchInPortTmacTable()) {
788 selector.matchInPort(pnum);
789 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800790 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530791 "ignoring the IN_PORT criteria");
792 }
793 }
794 if (vidCriterion != null) {
795 if (requireVlanExtensions()) {
796 selector.extension(ofdpaMatchVlanVid, deviceId);
797 } else {
798 selector.matchVlanId(vidCriterion.vlanId());
799 }
800 }
801 selector.matchEthType(Ethernet.TYPE_IPV4);
802 selector.matchEthDst(ethCriterion.mac());
803 treatment.transition(UNICAST_ROUTING_TABLE);
804 return DefaultFlowRule.builder()
805 .forDevice(deviceId)
806 .withSelector(selector.build())
807 .withTreatment(treatment.build())
808 .withPriority(DEFAULT_PRIORITY)
809 .fromApp(applicationId)
810 .makePermanent()
811 .forTable(TMAC_TABLE).build();
812 }
813
814 /**
815 * Builds TMAC rule for MPLS packets.
816 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800817 * @param ethCriterion dst mac matching
818 * @param vidCriterion vlan id assigned to the port
819 * @param ofdpaMatchVlanVid OFDPA vlan id matching
820 * @param applicationId application id
821 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530822 * @return TMAC rule for MPLS packets
823 */
824 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
825 VlanIdCriterion vidCriterion,
826 OfdpaMatchVlanVid ofdpaMatchVlanVid,
827 ApplicationId applicationId,
828 PortNumber pnum) {
829 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
830 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
831 if (pnum != null) {
832 if (matchInPortTmacTable()) {
833 selector.matchInPort(pnum);
834 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800835 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530836 "ignoring the IN_PORT criteria");
837 }
838 }
839 if (vidCriterion != null) {
840 if (requireVlanExtensions()) {
841 selector.extension(ofdpaMatchVlanVid, deviceId);
842 } else {
843 selector.matchVlanId(vidCriterion.vlanId());
844 }
845 }
846 selector.matchEthType(Ethernet.MPLS_UNICAST);
847 selector.matchEthDst(ethCriterion.mac());
848 treatment.transition(MPLS_TABLE_0);
849 return DefaultFlowRule.builder()
850 .forDevice(deviceId)
851 .withSelector(selector.build())
852 .withTreatment(treatment.build())
853 .withPriority(DEFAULT_PRIORITY)
854 .fromApp(applicationId)
855 .makePermanent()
856 .forTable(TMAC_TABLE).build();
857 }
858
859 /**
860 * Builds TMAC rules for IPv6 packets.
861 *
Charles Chan9174e6b2017-12-19 19:55:57 -0800862 * @param ethCriterion dst mac matching
863 * @param vidCriterion vlan id assigned to the port
864 * @param ofdpaMatchVlanVid OFDPA vlan id matching
865 * @param applicationId application id
866 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530867 * @return TMAC rule for IPV6 packets
868 */
869 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
870 VlanIdCriterion vidCriterion,
871 OfdpaMatchVlanVid ofdpaMatchVlanVid,
872 ApplicationId applicationId,
873 PortNumber pnum) {
874 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
875 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
876 if (pnum != null) {
877 if (matchInPortTmacTable()) {
878 selector.matchInPort(pnum);
879 } else {
Saurav Das73a6cc12017-12-05 15:00:23 -0800880 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530881 "ignoring the IN_PORT criteria");
882 }
883 }
884 if (vidCriterion != null) {
885 if (requireVlanExtensions()) {
886 selector.extension(ofdpaMatchVlanVid, deviceId);
887 } else {
888 selector.matchVlanId(vidCriterion.vlanId());
889 }
890 }
891 selector.matchEthType(Ethernet.TYPE_IPV6);
892 selector.matchEthDst(ethCriterion.mac());
893 treatment.transition(UNICAST_ROUTING_TABLE);
894 return DefaultFlowRule.builder()
895 .forDevice(deviceId)
896 .withSelector(selector.build())
897 .withTreatment(treatment.build())
898 .withPriority(DEFAULT_PRIORITY)
899 .fromApp(applicationId)
900 .makePermanent()
901 .forTable(TMAC_TABLE).build();
902 }
903
Charles Chan206506b2018-03-02 16:43:28 -0800904 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700905 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800906 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
907
Charles Chan5270ed02016-01-30 23:22:37 -0800908 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
909 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
910 selector.matchEthType(Ethernet.TYPE_IPV4);
911 selector.matchEthDst(ethCriterion.mac());
912 treatment.transition(UNICAST_ROUTING_TABLE);
913 FlowRule rule = DefaultFlowRule.builder()
914 .forDevice(deviceId)
915 .withSelector(selector.build())
916 .withTreatment(treatment.build())
917 .withPriority(DEFAULT_PRIORITY)
918 .fromApp(applicationId)
919 .makePermanent()
920 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800921 builder.add(rule);
922
923 selector = DefaultTrafficSelector.builder();
924 treatment = DefaultTrafficTreatment.builder();
925 selector.matchEthType(Ethernet.TYPE_IPV6);
926 selector.matchEthDst(ethCriterion.mac());
927 treatment.transition(UNICAST_ROUTING_TABLE);
928 rule = DefaultFlowRule.builder()
929 .forDevice(deviceId)
930 .withSelector(selector.build())
931 .withTreatment(treatment.build())
932 .withPriority(DEFAULT_PRIORITY)
933 .fromApp(applicationId)
934 .makePermanent()
935 .forTable(TMAC_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800936 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800937 }
938
Charles Chan206506b2018-03-02 16:43:28 -0800939 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700940 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800941 MacAddress unicastMac,
Yi Tsengef19de12017-04-24 11:33:05 -0700942 ApplicationId applicationId) {
Charles Chand57552d2018-03-02 15:41:41 -0800943 ImmutableList.Builder<FlowRule> unicastFlows = ImmutableList.builder();
944 ImmutableList.Builder<FlowRule> multicastFlows = ImmutableList.builder();
945 TrafficSelector.Builder selector;
946 TrafficTreatment.Builder treatment;
Charles Chanb4879a52017-10-20 19:09:16 -0700947 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000948
Charles Chanb4879a52017-10-20 19:09:16 -0700949 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -0800950 if (requireUnicastBeforeMulticast()) {
951 selector = DefaultTrafficSelector.builder();
952 treatment = DefaultTrafficTreatment.builder();
953 selector.matchEthType(Ethernet.TYPE_IPV4);
954 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +0000955 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -0800956 treatment.transition(UNICAST_ROUTING_TABLE);
957 rule = DefaultFlowRule.builder()
958 .forDevice(deviceId)
959 .withSelector(selector.build())
960 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +0000961 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -0800962 .fromApp(applicationId)
963 .makePermanent()
964 .forTable(TMAC_TABLE).build();
965 unicastFlows.add(rule);
966 }
967
968 selector = DefaultTrafficSelector.builder();
969 treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700970 selector.matchEthType(Ethernet.TYPE_IPV4);
971 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +0000972 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -0700973 treatment.transition(MULTICAST_ROUTING_TABLE);
974 rule = DefaultFlowRule.builder()
975 .forDevice(deviceId)
976 .withSelector(selector.build())
977 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +0000978 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -0700979 .fromApp(applicationId)
980 .makePermanent()
981 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -0800982 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -0700983 }
984
985 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
Charles Chand57552d2018-03-02 15:41:41 -0800986 if (requireUnicastBeforeMulticast()) {
987 selector = DefaultTrafficSelector.builder();
988 treatment = DefaultTrafficTreatment.builder();
989 selector.matchEthType(Ethernet.TYPE_IPV6);
990 selector.matchEthDst(unicastMac);
Charles Chan893a1d72018-05-10 22:19:25 +0000991 selector.matchVlanId(assignedVlan);
Charles Chand57552d2018-03-02 15:41:41 -0800992 treatment.transition(UNICAST_ROUTING_TABLE);
993 rule = DefaultFlowRule.builder()
994 .forDevice(deviceId)
995 .withSelector(selector.build())
996 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +0000997 .withPriority(DEFAULT_PRIORITY)
Charles Chand57552d2018-03-02 15:41:41 -0800998 .fromApp(applicationId)
999 .makePermanent()
1000 .forTable(TMAC_TABLE).build();
1001 unicastFlows.add(rule);
1002 }
1003
Charles Chanb4879a52017-10-20 19:09:16 -07001004 selector = DefaultTrafficSelector.builder();
1005 treatment = DefaultTrafficTreatment.builder();
1006 selector.matchEthType(Ethernet.TYPE_IPV6);
1007 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
Charles Chan893a1d72018-05-10 22:19:25 +00001008 selector.matchVlanId(assignedVlan);
Charles Chanb4879a52017-10-20 19:09:16 -07001009 treatment.transition(MULTICAST_ROUTING_TABLE);
1010 rule = DefaultFlowRule.builder()
1011 .forDevice(deviceId)
1012 .withSelector(selector.build())
1013 .withTreatment(treatment.build())
Charles Chan893a1d72018-05-10 22:19:25 +00001014 .withPriority(DEFAULT_PRIORITY)
Charles Chanb4879a52017-10-20 19:09:16 -07001015 .fromApp(applicationId)
1016 .makePermanent()
1017 .forTable(TMAC_TABLE).build();
Charles Chand57552d2018-03-02 15:41:41 -08001018 multicastFlows.add(rule);
Charles Chanb4879a52017-10-20 19:09:16 -07001019 }
Charles Chand57552d2018-03-02 15:41:41 -08001020 return ImmutableList.of(unicastFlows.build(), multicastFlows.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -07001021 }
1022
Saurav Das822c4e22015-10-23 10:51:11 -07001023 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
1024 switch (fwd.flag()) {
1025 case SPECIFIC:
1026 return processSpecific(fwd);
1027 case VERSATILE:
1028 return processVersatile(fwd);
1029 default:
1030 fail(fwd, ObjectiveError.UNKNOWN);
1031 log.warn("Unknown forwarding flag {}", fwd.flag());
1032 }
1033 return Collections.emptySet();
1034 }
1035
1036 /**
1037 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
1038 * ACL table.
1039 * @param fwd the forwarding objective of type 'versatile'
1040 * @return a collection of flow rules to be sent to the switch. An empty
1041 * collection may be returned if there is a problem in processing
1042 * the flow rule
1043 */
Saurav Das52025962016-01-28 22:30:01 -08001044 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -08001045 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001046 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001047
Saurav Das822c4e22015-10-23 10:51:11 -07001048 if (fwd.nextId() == null && fwd.treatment() == null) {
1049 log.error("Forwarding objective {} from {} must contain "
1050 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -08001051 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -07001052 return Collections.emptySet();
1053 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001054
Saurav Das77b5e902016-01-27 17:01:59 -08001055 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
1056 fwd.selector().criteria().forEach(criterion -> {
1057 if (criterion instanceof VlanIdCriterion) {
1058 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1059 // ensure that match does not include vlan = NONE as OF-DPA does not
1060 // match untagged packets this way in the ACL table.
1061 if (vlanId.equals(VlanId.NONE)) {
1062 return;
1063 }
Charles Chand1172632017-03-15 17:33:09 -07001064 if (requireVlanExtensions()) {
1065 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1066 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1067 } else {
1068 sbuilder.matchVlanId(vlanId);
1069 }
Charles Chan393339f2018-01-11 11:48:18 -08001070 } else if (criterion instanceof Icmpv6TypeCriterion) {
1071 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1072 sbuilder.matchIcmpv6Type(icmpv6Type);
1073 } else if (criterion instanceof Icmpv6CodeCriterion) {
1074 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1075 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001076 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1077 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1078 // Currently L4 dst port matching is only used by DHCP relay feature
1079 // and therefore is safe to be replaced with L4 src port matching.
1080 // We need to revisit this if L4 dst port is used for other purpose in the future.
1081 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001082 switch (criterion.type()) {
1083 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001084 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001085 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001086 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001087 break;
1088 default:
1089 sbuilder.add(criterion);
1090 }
1091 } else {
1092 sbuilder.add(criterion);
1093 }
Saurav Das77b5e902016-01-27 17:01:59 -08001094 } else {
1095 sbuilder.add(criterion);
1096 }
1097 });
1098
Saurav Das822c4e22015-10-23 10:51:11 -07001099 // XXX driver does not currently do type checking as per Tables 65-67 in
1100 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001101 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1102 if (fwd.treatment() != null) {
1103 for (Instruction ins : fwd.treatment().allInstructions()) {
1104 if (ins instanceof OutputInstruction) {
1105 OutputInstruction o = (OutputInstruction) ins;
Charles Chan93090352018-03-02 13:26:22 -08001106 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001107 ttBuilder.add(o);
1108 } else {
1109 log.warn("Only allowed treatments in versatile forwarding "
1110 + "objectives are punts to the controller");
1111 }
Charles Chan3fe71712018-06-15 18:54:18 -07001112 } else if (ins instanceof NoActionInstruction) {
1113 // No action is allowed and nothing needs to be done
Saurav Das49cb5a12016-01-16 22:54:07 -08001114 } else {
1115 log.warn("Cannot process instruction in versatile fwd {}", ins);
1116 }
Saurav Das822c4e22015-10-23 10:51:11 -07001117 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001118 if (fwd.treatment().clearedDeferred()) {
1119 ttBuilder.wipeDeferred();
1120 }
Saurav Das822c4e22015-10-23 10:51:11 -07001121 }
Saurav Das822c4e22015-10-23 10:51:11 -07001122 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001123 // overide case
1124 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301125 if (next == null) {
1126 fail(fwd, ObjectiveError.BADPARAMS);
1127 return Collections.emptySet();
1128 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001129 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1130 // we only need the top level group's key to point the flow to it
1131 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1132 if (group == null) {
1133 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1134 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1135 fail(fwd, ObjectiveError.GROUPMISSING);
1136 return Collections.emptySet();
1137 }
1138 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001139 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001140
1141 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1142 .fromApp(fwd.appId())
1143 .withPriority(fwd.priority())
1144 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001145 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001146 .withTreatment(ttBuilder.build())
1147 .makePermanent()
1148 .forTable(ACL_TABLE);
1149 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001150 }
1151
1152 /**
1153 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001154 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001155 *
1156 * @param fwd the forwarding objective of type 'specific'
1157 * @return a collection of flow rules. Typically there will be only one
1158 * for this type of forwarding objective. An empty set may be
1159 * returned if there is an issue in processing the objective.
1160 */
Charles Chan93090352018-03-02 13:26:22 -08001161 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001162 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001163 fwd.id(), deviceId, fwd.nextId());
1164 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1165 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1166
1167 if (isEthTypeObj) {
1168 return processEthTypeSpecific(fwd);
1169 } else if (isEthDstObj) {
1170 return processEthDstSpecific(fwd);
1171 } else {
1172 log.warn("processSpecific: Unsupported forwarding objective "
1173 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001174 fail(fwd, ObjectiveError.UNSUPPORTED);
1175 return Collections.emptySet();
1176 }
Saurav Das4ce45962015-11-24 23:21:05 -08001177 }
1178
Saurav Das4ce45962015-11-24 23:21:05 -08001179 /**
1180 * Handles forwarding rules to the IP and MPLS tables.
1181 *
1182 * @param fwd the forwarding objective
1183 * @return A collection of flow rules, or an empty set
1184 */
1185 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001186 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001187 }
1188
1189 /**
1190 * Internal implementation of processEthTypeSpecific.
1191 * <p>
1192 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1193 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1194 * The allowDefaultRoute must be set to false for OFDPA i12.
1195 * </p>
1196 *
1197 * @param fwd the forwarding objective
1198 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001199 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001200 * @return A collection of flow rules, or an empty set
1201 */
1202 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001203 boolean allowDefaultRoute,
1204 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001205 TrafficSelector selector = fwd.selector();
1206 EthTypeCriterion ethType =
1207 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001208 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001209 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001210 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001211 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001212 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001213
Saurav Das8a0732e2015-11-20 15:27:53 -08001214 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001215 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1216 return Collections.emptyList();
1217 }
1218 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001219 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001220 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001221 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001222 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001223 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001224 }
Charles Chan93090352018-03-02 13:26:22 -08001225
Charles Chan7feb6c82018-03-05 11:28:23 -08001226 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001227 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001228 // if (fwd.treatment() != null) {
1229 // for (Instruction instr : fwd.treatment().allInstructions()) {
1230 // if (instr instanceof L3ModificationInstruction &&
1231 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1232 // tb.deferred().add(instr);
1233 // }
1234 // }
1235 // }
1236
Pier Ventree0ae7a32016-11-23 09:57:42 -08001237 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1238 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1239 return Collections.emptyList();
1240 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001241 //We need to set the proper next table
1242 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1243 if (ipv6Dst.isMulticast()) {
1244 forTableId = MULTICAST_ROUTING_TABLE;
1245 } else {
1246 forTableId = UNICAST_ROUTING_TABLE;
1247 }
1248
Charles Chan7feb6c82018-03-05 11:28:23 -08001249 // TODO decrementing IP ttl is done automatically for routing, this
Charles Chan93090352018-03-02 13:26:22 -08001250 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan7feb6c82018-03-05 11:28:23 -08001251 // if (fwd.treatment() != null) {
1252 // for (Instruction instr : fwd.treatment().allInstructions()) {
1253 // if (instr instanceof L3ModificationInstruction &&
1254 // ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1255 // tb.deferred().add(instr);
1256 // }
1257 // }
1258 // }
Saurav Das8a0732e2015-11-20 15:27:53 -08001259 } else {
1260 filteredSelector
1261 .matchEthType(Ethernet.MPLS_UNICAST)
1262 .matchMplsLabel(((MplsCriterion)
1263 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1264 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001265 .getCriterion(MPLS_BOS);
Alex Yashchuka3679532017-12-08 17:40:05 +02001266 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001267 filteredSelector.matchMplsBos(bos.mplsBos());
1268 }
1269 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001270 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1271 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001272
Charles Chan14967c22015-12-07 11:11:50 -08001273 if (fwd.treatment() != null) {
1274 for (Instruction instr : fwd.treatment().allInstructions()) {
1275 if (instr instanceof L2ModificationInstruction &&
1276 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001277 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001278 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuka3679532017-12-08 17:40:05 +02001279 if (requireMplsPop()) {
1280 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1281 tb.immediate().popMpls();
1282 }
1283 } else {
1284 // Skip mpls pop action for mpls_unicast label
1285 if (instr instanceof ModMplsHeaderInstruction &&
1286 !((ModMplsHeaderInstruction) instr).ethernetType()
1287 .equals(EtherType.MPLS_UNICAST.ethType())) {
1288 tb.immediate().add(instr);
1289 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001290 }
Charles Chan14967c22015-12-07 11:11:50 -08001291 }
Alex Yashchuka3679532017-12-08 17:40:05 +02001292
1293 if (requireMplsTtlModification()) {
1294 if (instr instanceof L3ModificationInstruction &&
1295 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1296 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1297 tb.immediate().decMplsTtl();
1298 }
1299 if (instr instanceof L3ModificationInstruction &&
1300 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1301 tb.immediate().add(instr);
1302 }
Charles Chan14967c22015-12-07 11:11:50 -08001303 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001304 }
1305 }
1306 }
Saurav Das822c4e22015-10-23 10:51:11 -07001307
1308 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001309 NextGroup next = getGroupForNextObjective(fwd.nextId());
1310 if (next != null) {
1311 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1312 // we only need the top level group's key to point the flow to it
1313 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1314 if (group == null) {
1315 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1316 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1317 fail(fwd, ObjectiveError.GROUPMISSING);
1318 return Collections.emptySet();
1319 }
Saurav Dasa89b95a2018-02-14 14:14:54 -08001320 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1321 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1322 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1323 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1324 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1325 return Collections.emptySet();
1326 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001327 tb.deferred().group(group.id());
Saurav Dasf3f75942018-01-25 09:49:01 -08001328 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001329 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasf3f75942018-01-25 09:49:01 -08001330 if (shouldRetry()) {
1331 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1332 Integer.toHexString(group.id().id()), deviceId,
1333 fwd.id());
1334 emptyGroup = true;
1335 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001336 }
Saurav Das25190812016-05-27 13:54:07 -07001337 } else {
1338 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1339 fwd.nextId(), deviceId, fwd.id());
1340 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1341 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001342 }
Saurav Das822c4e22015-10-23 10:51:11 -07001343 }
Charles Chancad338a2016-09-16 18:03:11 -07001344
1345 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001346 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001347 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001348 // set mpls type as apply_action
1349 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001350 }
1351 tb.transition(mplsNextTable);
1352 } else {
1353 tb.transition(ACL_TABLE);
1354 }
1355
Andrea Campanella84ac4df2018-04-30 11:48:55 +02001356 if (fwd.treatment() != null && fwd.treatment().clearedDeferred()) {
1357 if (supportsUnicastBlackHole()) {
1358 tb.wipeDeferred();
1359 } else {
1360 log.warn("Clear Deferred is not supported Unicast Routing Table on device {}", deviceId);
1361 return Collections.emptySet();
1362 }
1363 }
1364
Saurav Das822c4e22015-10-23 10:51:11 -07001365 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1366 .fromApp(fwd.appId())
1367 .withPriority(fwd.priority())
1368 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001369 .withSelector(filteredSelector.build())
1370 .withTreatment(tb.build())
1371 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001372
1373 if (fwd.permanent()) {
1374 ruleBuilder.makePermanent();
1375 } else {
1376 ruleBuilder.makeTemporary(fwd.timeout());
1377 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001378 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1379 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001380 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001381 flowRuleCollection.add(
1382 defaultRoute(fwd, complementarySelector, forTableId, tb)
1383 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001384 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1385 }
Saurav Dasf3f75942018-01-25 09:49:01 -08001386
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001387 if (emptyGroup) {
1388 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1389 RETRY_MS, TimeUnit.MILLISECONDS);
1390 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001391 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001392 }
1393
Charles Chan93090352018-03-02 13:26:22 -08001394 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001395 TrafficSelector.Builder extBuilder,
1396 ForwardingObjective fwd,
1397 boolean allowDefaultRoute) {
1398 TrafficSelector selector = fwd.selector();
1399
1400 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1401 if (ipv4Dst.isMulticast()) {
1402 if (ipv4Dst.prefixLength() != 32) {
1403 log.warn("Multicast specific forwarding objective can only be /32");
1404 fail(fwd, ObjectiveError.BADPARAMS);
1405 return -1;
1406 }
1407 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1408 if (assignedVlan == null) {
1409 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1410 fail(fwd, ObjectiveError.BADPARAMS);
1411 return -1;
1412 }
Charles Chand1172632017-03-15 17:33:09 -07001413 if (requireVlanExtensions()) {
1414 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1415 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1416 } else {
1417 builderToUpdate.matchVlanId(assignedVlan);
1418 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001419 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1420 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1421 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1422 } else {
1423 if (ipv4Dst.prefixLength() == 0) {
1424 if (allowDefaultRoute) {
1425 // The entire IPV4_DST field is wildcarded intentionally
1426 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1427 } else {
1428 // NOTE: The switch does not support matching 0.0.0.0/0
1429 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1430 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1431 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1432 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1433 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1434 }
1435 } else {
1436 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1437 }
1438 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1439 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1440 }
1441 return 0;
1442 }
1443
1444 /**
1445 * Helper method to build Ipv6 selector using the selector provided by
1446 * a forwarding objective.
1447 *
1448 * @param builderToUpdate the builder to update
1449 * @param fwd the selector to read
1450 * @return 0 if the update ends correctly. -1 if the matches
1451 * are not yet supported
1452 */
Charles Chan93090352018-03-02 13:26:22 -08001453 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001454 ForwardingObjective fwd) {
1455
1456 TrafficSelector selector = fwd.selector();
1457
1458 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1459 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001460 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1461 log.warn("Multicast specific forwarding objective can only be /128");
1462 fail(fwd, ObjectiveError.BADPARAMS);
1463 return -1;
1464 }
1465 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1466 if (assignedVlan == null) {
1467 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1468 fail(fwd, ObjectiveError.BADPARAMS);
1469 return -1;
1470 }
1471 if (requireVlanExtensions()) {
1472 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1473 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1474 } else {
1475 builderToUpdate.matchVlanId(assignedVlan);
1476 }
1477 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1478 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1479 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1480 } else {
1481 if (ipv6Dst.prefixLength() != 0) {
1482 builderToUpdate.matchIPv6Dst(ipv6Dst);
1483 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001484 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1485 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1486 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001487 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001488 return 0;
1489 }
1490
Charles Chan93090352018-03-02 13:26:22 -08001491 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001492 TrafficSelector.Builder complementarySelector,
1493 int forTableId,
1494 TrafficTreatment.Builder tb) {
1495 FlowRule.Builder rule = DefaultFlowRule.builder()
1496 .fromApp(fwd.appId())
1497 .withPriority(fwd.priority())
1498 .forDevice(deviceId)
1499 .withSelector(complementarySelector.build())
1500 .withTreatment(tb.build())
1501 .forTable(forTableId);
1502 if (fwd.permanent()) {
1503 rule.makePermanent();
1504 } else {
1505 rule.makeTemporary(fwd.timeout());
1506 }
1507 return rule.build();
1508 }
1509
Saurav Das4ce45962015-11-24 23:21:05 -08001510 /**
1511 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1512 * allowed in the bridging table - instead we use L2 Interface group or
1513 * L2 flood group
1514 *
1515 * @param fwd the forwarding objective
1516 * @return A collection of flow rules, or an empty set
1517 */
1518 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1519 List<FlowRule> rules = new ArrayList<>();
1520
1521 // Build filtered selector
1522 TrafficSelector selector = fwd.selector();
1523 EthCriterion ethCriterion = (EthCriterion) selector
1524 .getCriterion(Criterion.Type.ETH_DST);
1525 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1526 .getCriterion(Criterion.Type.VLAN_VID);
1527
1528 if (vlanIdCriterion == null) {
1529 log.warn("Forwarding objective for bridging requires vlan. Not "
1530 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1531 fail(fwd, ObjectiveError.BADPARAMS);
1532 return Collections.emptySet();
1533 }
1534
1535 TrafficSelector.Builder filteredSelectorBuilder =
1536 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001537
1538 if (!ethCriterion.mac().equals(NONE) &&
1539 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001540 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1541 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1542 fwd.id(), fwd.nextId(), deviceId);
1543 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001544 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001545 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1546 + "in dev:{} for vlan:{}",
1547 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1548 }
Charles Chand1172632017-03-15 17:33:09 -07001549 if (requireVlanExtensions()) {
1550 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1551 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1552 } else {
1553 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1554 }
Saurav Das4ce45962015-11-24 23:21:05 -08001555 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1556
1557 if (fwd.treatment() != null) {
1558 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1559 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1560 }
1561
1562 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1563 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001564 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001565 if (next != null) {
1566 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1567 // we only need the top level group's key to point the flow to it
1568 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1569 if (group != null) {
1570 treatmentBuilder.deferred().group(group.id());
1571 } else {
1572 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1573 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1574 fail(fwd, ObjectiveError.GROUPMISSING);
1575 return Collections.emptySet();
1576 }
1577 }
1578 }
1579 treatmentBuilder.immediate().transition(ACL_TABLE);
1580 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1581
1582 // Build bridging table entries
1583 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1584 flowRuleBuilder.fromApp(fwd.appId())
1585 .withPriority(fwd.priority())
1586 .forDevice(deviceId)
1587 .withSelector(filteredSelector)
1588 .withTreatment(filteredTreatment)
1589 .forTable(BRIDGING_TABLE);
1590 if (fwd.permanent()) {
1591 flowRuleBuilder.makePermanent();
1592 } else {
1593 flowRuleBuilder.makeTemporary(fwd.timeout());
1594 }
1595 rules.add(flowRuleBuilder.build());
1596 return rules;
1597 }
1598
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001599 //////////////////////////////////////
1600 // Helper Methods and Classes
1601 //////////////////////////////////////
1602
1603 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1604 TrafficSelector selector = fwd.selector();
1605 EthTypeCriterion ethType = (EthTypeCriterion) selector
1606 .getCriterion(Criterion.Type.ETH_TYPE);
1607 return !((ethType == null) ||
1608 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001609 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1610 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001611 }
1612
1613 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1614 TrafficSelector selector = fwd.selector();
1615 EthCriterion ethDst = (EthCriterion) selector
1616 .getCriterion(Criterion.Type.ETH_DST);
1617 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1618 .getCriterion(Criterion.Type.VLAN_VID);
1619 return !(ethDst == null && vlanId == null);
1620 }
1621
Charles Chan93090352018-03-02 13:26:22 -08001622 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001623 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1624 if (next != null) {
1625 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1626 if (gkeys != null && !gkeys.isEmpty()) {
1627 return next;
1628 } else {
1629 log.warn("Empty next group found in FlowObjective store for "
1630 + "next-id:{} in dev:{}", nextId, deviceId);
1631 }
1632 } else {
1633 log.warn("next-id {} not found in Flow objective store for dev:{}",
1634 nextId, deviceId);
1635 }
1636 return null;
1637 }
1638
Charles Chan188ebf52015-12-23 00:15:11 -08001639 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001640 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001641 }
1642
Charles Chan188ebf52015-12-23 00:15:11 -08001643 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001644 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001645 }
Saurav Das24431192016-03-07 19:13:00 -08001646
Saurav Das24431192016-03-07 19:13:00 -08001647 @Override
1648 public List<String> getNextMappings(NextGroup nextGroup) {
1649 List<String> mappings = new ArrayList<>();
1650 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1651 for (Deque<GroupKey> gkd : gkeys) {
1652 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001653 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001654 for (GroupKey gk : gkd) {
1655 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001656 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001657 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001658 continue;
1659 }
1660 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1661 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001662 lastGroup = g;
1663 }
1664 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001665 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001666 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001667 lastGroupIns = lastGroup.buckets().buckets().get(0)
1668 .treatment().allInstructions();
1669 }
1670 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001671 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001672 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001673 }
1674 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001675 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001676 }
1677 return mappings;
1678 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001679
Saurav Dasa89b95a2018-02-14 14:14:54 -08001680 /**
1681 * Returns true iff the given selector matches on BOS==true, indicating that
1682 * the selector is trying to match on a label that is bottom-of-stack.
1683 *
1684 * @param selector the given match
1685 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1686 * expressed in the given selector
1687 */
Pier Ventre140a8942016-11-02 07:26:38 -07001688 static boolean isMplsBos(TrafficSelector selector) {
1689 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1690 return bosCriterion != null && bosCriterion.mplsBos();
1691 }
1692
Saurav Dasa89b95a2018-02-14 14:14:54 -08001693 /**
1694 * Returns true iff the given selector matches on BOS==false, indicating
1695 * that the selector is trying to match on a label that is not the
1696 * bottom-of-stack label.
1697 *
1698 * @param selector the given match
1699 * @return true iff BoS==false;
1700 * false if BOS==true, or BOS matching is not expressed in the given selector
1701 */
Pier Ventre140a8942016-11-02 07:26:38 -07001702 static boolean isNotMplsBos(TrafficSelector selector) {
1703 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1704 return bosCriterion != null && !bosCriterion.mplsBos();
1705 }
1706
Saurav Dasa89b95a2018-02-14 14:14:54 -08001707 /**
1708 * Returns true iff the forwarding objective includes a treatment to pop the
1709 * MPLS label.
1710 *
1711 * @param fwd the given forwarding objective
1712 * @return true iff mpls pop treatment exists
1713 */
1714 static boolean isMplsPop(ForwardingObjective fwd) {
1715 if (fwd.treatment() != null) {
1716 for (Instruction instr : fwd.treatment().allInstructions()) {
1717 if (instr instanceof L2ModificationInstruction
1718 && ((L2ModificationInstruction) instr)
1719 .subtype() == L2SubType.MPLS_POP) {
1720 return true;
1721 }
1722 }
1723 }
1724 return false;
1725 }
1726
Charles Chand9e47c62017-10-05 15:17:15 -07001727 private static boolean isIpv6(TrafficSelector selector) {
1728 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1729 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1730 }
1731
Charles Chan93090352018-03-02 13:26:22 -08001732 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001733 if (selector == null) {
1734 return null;
1735 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001736 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1737 return (criterion == null)
1738 ? null : ((VlanIdCriterion) criterion).vlanId();
1739 }
1740
Charles Chand9e47c62017-10-05 15:17:15 -07001741 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001742 if (selector == null) {
1743 return null;
1744 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001745 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1746 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1747 }
Charles Chand55e84d2016-03-30 17:54:24 -07001748
1749 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001750 if (treatment == null) {
1751 return null;
1752 }
Charles Chand55e84d2016-03-30 17:54:24 -07001753 for (Instruction i : treatment.allInstructions()) {
1754 if (i instanceof ModVlanIdInstruction) {
1755 return ((ModVlanIdInstruction) i).vlanId();
1756 }
1757 }
1758 return null;
1759 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001760
Charles Chand57552d2018-03-02 15:41:41 -08001761 private static MacAddress readEthDstFromTreatment(TrafficTreatment treatment) {
1762 if (treatment == null) {
1763 return null;
1764 }
1765 for (Instruction i : treatment.allInstructions()) {
1766 if (i instanceof ModEtherInstruction) {
1767 ModEtherInstruction modEtherInstruction = (ModEtherInstruction) i;
1768 if (modEtherInstruction.subtype() == L2SubType.ETH_DST) {
1769 return modEtherInstruction.mac();
1770 }
1771 }
1772 }
1773 return null;
1774 }
1775
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001776 /**
1777 * Utility class that retries sending flows a fixed number of times, even if
1778 * some of the attempts are successful. Used only for forwarding objectives.
1779 */
Alex Yashchuka3679532017-12-08 17:40:05 +02001780 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001781 int attempts = MAX_RETRY_ATTEMPTS;
1782 private Collection<FlowRule> retryFlows;
1783 private ForwardingObjective fwd;
1784
Charles Chan93090352018-03-02 13:26:22 -08001785 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001786 this.fwd = fwd;
1787 this.retryFlows = retryFlows;
1788 }
1789
1790 @Override
1791 public void run() {
1792 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1793 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1794 sendForward(fwd, retryFlows);
1795 if (--attempts > 0) {
1796 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1797 }
1798 }
1799 }
1800
Saurav Das822c4e22015-10-23 10:51:11 -07001801}