blob: ff9f2c087b20406a4d424bf5fabb07dbb43f72ad [file] [log] [blame]
Saurav Das822c4e22015-10-23 10:51:11 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Das822c4e22015-10-23 10:51:11 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Yi Tsengef19de12017-04-24 11:33:05 -070016package org.onosproject.driver.pipeline.ofdpa;
Saurav Das822c4e22015-10-23 10:51:11 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
Yi Tseng47f82dc2017-03-05 22:48:39 -080019import com.google.common.collect.Sets;
Saurav Das822c4e22015-10-23 10:51:11 -070020import org.onlab.osgi.ServiceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -070021import org.onlab.packet.Ethernet;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020022import org.onlab.packet.EthType.EtherType;
Julia Ferguson65428c32017-08-10 18:15:24 +000023import org.onlab.packet.IpAddress;
Charles Chan14967c22015-12-07 11:11:50 -080024import org.onlab.packet.IpPrefix;
Saurav Das822c4e22015-10-23 10:51:11 -070025import org.onlab.packet.VlanId;
26import org.onlab.util.KryoNamespace;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Charles Chancad338a2016-09-16 18:03:11 -070029import org.onosproject.driver.extensions.Ofdpa3MplsType;
30import org.onosproject.driver.extensions.Ofdpa3SetMplsType;
Charles Chan14967c22015-12-07 11:11:50 -080031import org.onosproject.driver.extensions.OfdpaMatchVlanVid;
32import org.onosproject.driver.extensions.OfdpaSetVlanVid;
Saurav Das822c4e22015-10-23 10:51:11 -070033import org.onosproject.net.DeviceId;
34import org.onosproject.net.Port;
35import org.onosproject.net.PortNumber;
36import org.onosproject.net.behaviour.NextGroup;
37import org.onosproject.net.behaviour.Pipeliner;
38import org.onosproject.net.behaviour.PipelinerContext;
39import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.driver.AbstractHandlerBehaviour;
41import org.onosproject.net.flow.DefaultFlowRule;
42import org.onosproject.net.flow.DefaultTrafficSelector;
43import org.onosproject.net.flow.DefaultTrafficTreatment;
44import org.onosproject.net.flow.FlowRule;
45import org.onosproject.net.flow.FlowRuleOperations;
46import org.onosproject.net.flow.FlowRuleOperationsContext;
47import org.onosproject.net.flow.FlowRuleService;
48import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.flow.criteria.Criteria;
51import org.onosproject.net.flow.criteria.Criterion;
52import org.onosproject.net.flow.criteria.EthCriterion;
53import org.onosproject.net.flow.criteria.EthTypeCriterion;
Charles Chan14967c22015-12-07 11:11:50 -080054import org.onosproject.net.flow.criteria.ExtensionCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070055import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080056import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
57import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080058import org.onosproject.net.flow.criteria.MplsBosCriterion;
59import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070060import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070061import org.onosproject.net.flow.criteria.TcpPortCriterion;
62import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070063import org.onosproject.net.flow.criteria.VlanIdCriterion;
64import org.onosproject.net.flow.instructions.Instruction;
65import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
66import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080067import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070068import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020069import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080070import org.onosproject.net.flow.instructions.L3ModificationInstruction;
71import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070072import org.onosproject.net.flowobjective.FilteringObjective;
73import org.onosproject.net.flowobjective.FlowObjectiveStore;
74import org.onosproject.net.flowobjective.ForwardingObjective;
75import org.onosproject.net.flowobjective.NextObjective;
76import org.onosproject.net.flowobjective.Objective;
77import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070078import org.onosproject.net.group.DefaultGroupKey;
79import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070082import org.onosproject.store.serializers.KryoNamespaces;
83import org.slf4j.Logger;
84
Pier Ventree0ae7a32016-11-23 09:57:42 -080085import java.util.ArrayDeque;
86import java.util.ArrayList;
87import java.util.Collection;
88import java.util.Collections;
89import java.util.Deque;
90import java.util.List;
91import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080092import java.util.concurrent.ScheduledExecutorService;
93import java.util.concurrent.TimeUnit;
94
95import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080096import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -070097import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
98import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080099import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800100import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700101import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700102import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100103import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800104import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700105import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800106
Saurav Das822c4e22015-10-23 10:51:11 -0700107/**
108 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700109 */
Charles Chan361154b2016-03-24 10:23:39 -0700110public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Pier Ventre42287df2016-11-09 14:17:26 -0800111
Saurav Das822c4e22015-10-23 10:51:11 -0700112 protected static final int PORT_TABLE = 0;
113 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800114 protected static final int VLAN_1_TABLE = 11;
115 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
116 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700117 protected static final int TMAC_TABLE = 20;
118 protected static final int UNICAST_ROUTING_TABLE = 30;
119 protected static final int MULTICAST_ROUTING_TABLE = 40;
120 protected static final int MPLS_TABLE_0 = 23;
121 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700122 protected static final int MPLS_L3_TYPE_TABLE = 27;
123 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700124 protected static final int BRIDGING_TABLE = 50;
125 protected static final int ACL_TABLE = 60;
126 protected static final int MAC_LEARNING_TABLE = 254;
127 protected static final long OFPP_MAX = 0xffffff00L;
128
Saurav Das52025962016-01-28 22:30:01 -0800129 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800130 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700131 protected static final int LOWEST_PRIORITY = 0x0;
132
Pier Ventre42287df2016-11-09 14:17:26 -0800133 protected static final int MPLS_L2_PORT_PRIORITY = 2;
134
135 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;
139
140 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
141 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
142
Saurav Das822c4e22015-10-23 10:51:11 -0700143 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700144 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700145 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700146 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800147 protected GroupService groupService;
148 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700149 protected DeviceId deviceId;
150 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700151 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800152 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700153 .register(KryoNamespaces.API)
154 .register(GroupKey.class)
155 .register(DefaultGroupKey.class)
156 .register(OfdpaNextGroup.class)
157 .register(ArrayDeque.class)
158 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700159
Charles Chan425854b2016-04-11 15:32:12 -0700160 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700161
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700162 // flows installations to be retried
163 protected ScheduledExecutorService executorService
164 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
165 protected static final int MAX_RETRY_ATTEMPTS = 10;
166 protected static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800167
Saurav Das822c4e22015-10-23 10:51:11 -0700168 @Override
169 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700170 this.deviceId = deviceId;
171
Charles Chan425854b2016-04-11 15:32:12 -0700172 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700173 coreService = serviceDirectory.get(CoreService.class);
174 flowRuleService = serviceDirectory.get(FlowRuleService.class);
175 groupService = serviceDirectory.get(GroupService.class);
176 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700177 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700178
Charles Chan40132b32017-01-22 00:19:37 -0800179 initDriverId();
180 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700181
Saurav Das822c4e22015-10-23 10:51:11 -0700182 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700183 }
184
Charles Chan40132b32017-01-22 00:19:37 -0800185 protected void initDriverId() {
186 driverId = coreService.registerApplication(
187 "org.onosproject.driver.Ofdpa2Pipeline");
188 }
189
190 protected void initGroupHander(PipelinerContext context) {
191 groupHandler = new Ofdpa2GroupHandler();
192 groupHandler.init(deviceId, context);
193 }
194
Saurav Das822c4e22015-10-23 10:51:11 -0700195 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800196 // OF-DPA does not require initializing the pipeline as it puts default
197 // rules automatically in the hardware. However emulation of OFDPA in
198 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700199 }
200
Charles Chand1172632017-03-15 17:33:09 -0700201 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200202 * Determines whether this pipeline requires MPLS POP instruction.
203 *
204 * @return true to use MPLS POP instruction
205 */
206 public boolean requireMplsPop() {
207 return true;
208 }
209
210 /**
211 * Determines whether this pipeline requires MPLS BOS match.
212 *
213 * @return true to use MPLS BOS match
214 */
215 public boolean requireMplsBosMatch() {
216 return true;
217 }
218
219 /**
220 * Determines whether this pipeline requires MPLS TTL decrement and copy.
221 *
222 * @return true to use MPLS TTL decrement and copy
223 */
224 public boolean requireMplsTtlModification() {
225 return true;
226 }
227
228 /**
Charles Chand1172632017-03-15 17:33:09 -0700229 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
230 *
231 * @return true to use the extensions
232 */
233 protected boolean requireVlanExtensions() {
234 return true;
235 }
236
Saurav Das86d13e82017-04-28 17:03:48 -0700237 /**
238 * Determines whether in-port should be matched on in TMAC table rules.
239 *
240 * @return true if match on in-port should be programmed
241 */
242 protected boolean matchInPortTmacTable() {
243 return true;
244 }
245
Charles Chand9e47c62017-10-05 15:17:15 -0700246 /**
247 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
248 *
249 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
250 */
251 protected boolean supportIpv6L4Dst() {
252 return true;
253 }
254
Saurav Das822c4e22015-10-23 10:51:11 -0700255 //////////////////////////////////////
256 // Flow Objectives
257 //////////////////////////////////////
258
259 @Override
260 public void filter(FilteringObjective filteringObjective) {
261 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
262 processFilter(filteringObjective,
263 filteringObjective.op() == Objective.Operation.ADD,
264 filteringObjective.appId());
265 } else {
266 // Note that packets that don't match the PERMIT filter are
267 // automatically denied. The DENY filter is used to deny packets
268 // that are otherwise permitted by the PERMIT filter.
269 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530270 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700271 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
272 }
273 }
274
275 @Override
276 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700277 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700278 if (rules == null || rules.isEmpty()) {
279 // Assumes fail message has already been generated to the objective
280 // context. Returning here prevents spurious pass message to be
281 // generated by FlowRule service for empty flowOps.
282 return;
283 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700284 sendForward(fwd, rules);
285 }
286
287 protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
288 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700289 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700290 case ADD:
291 rules.stream()
292 .filter(Objects::nonNull)
293 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800294 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700295 break;
296 case REMOVE:
297 rules.stream()
298 .filter(Objects::nonNull)
299 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800300 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700301 break;
302 default:
303 fail(fwd, ObjectiveError.UNKNOWN);
304 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700305 }
306
Saurav Das822c4e22015-10-23 10:51:11 -0700307 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
308 @Override
309 public void onSuccess(FlowRuleOperations ops) {
310 pass(fwd);
311 }
312
313 @Override
314 public void onError(FlowRuleOperations ops) {
315 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
316 }
317 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700318 }
319
320 @Override
321 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800322 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
323 switch (nextObjective.op()) {
324 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800325 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800326 log.warn("Cannot add next {} that already exists in device {}",
327 nextObjective.id(), deviceId);
328 return;
329 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700330 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800331 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700332 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800333 break;
334 case ADD_TO_EXISTING:
335 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700336 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800337 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700338 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800339 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800340 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800341 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
342 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800343
344 // by design multiple pending bucket is allowed for the group
345 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
346 if (pendBkts == null) {
347 pendBkts = Sets.newHashSet();
348 }
349 pendBkts.add(nextObjective);
350 return pendBkts;
351 });
Saurav Das4f980082015-11-05 13:39:15 -0800352 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800353 break;
354 case REMOVE:
355 if (nextGroup == null) {
356 log.warn("Cannot remove next {} that does not exist in device {}",
357 nextObjective.id(), deviceId);
358 return;
359 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700360 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800361 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700362 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800363 break;
364 case REMOVE_FROM_EXISTING:
365 if (nextGroup == null) {
366 log.warn("Cannot remove from next {} that does not exist in device {}",
367 nextObjective.id(), deviceId);
368 return;
369 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700370 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800371 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700372 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800373 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700374 case VERIFY:
375 if (nextGroup == null) {
376 log.warn("Cannot verify next {} that does not exist in device {}",
377 nextObjective.id(), deviceId);
378 return;
379 }
380 log.debug("Processing NextObjective id {} in dev {} - verify",
381 nextObjective.id(), deviceId);
382 groupHandler.verifyGroup(nextObjective, nextGroup);
383 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800384 default:
Saurav Das4f980082015-11-05 13:39:15 -0800385 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700386 }
387 }
388
389 //////////////////////////////////////
390 // Flow handling
391 //////////////////////////////////////
392
393 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700394 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
395 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700396 *
397 * @param filt the filtering objective
398 * @param install indicates whether to add or remove the objective
399 * @param applicationId the application that sent this objective
400 */
Saurav Das52025962016-01-28 22:30:01 -0800401 protected void processFilter(FilteringObjective filt,
402 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700403 // This driver only processes filtering criteria defined with switch
404 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530405 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700406 EthCriterion ethCriterion = null;
407 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700408 if (!filt.key().equals(Criteria.dummy()) &&
409 filt.key().type() == Criterion.Type.IN_PORT) {
410 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700411 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530412 if (portCriterion == null) {
413 log.debug("No IN_PORT defined in filtering objective from app: {}",
414 applicationId);
415 } else {
416 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
417 portCriterion.port());
418 }
Saurav Das822c4e22015-10-23 10:51:11 -0700419 // convert filtering conditions for switch-intfs into flowrules
420 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
421 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700422 switch (criterion.type()) {
423 case ETH_DST:
424 case ETH_DST_MASKED:
425 ethCriterion = (EthCriterion) criterion;
426 break;
427 case VLAN_VID:
428 vidCriterion = (VlanIdCriterion) criterion;
429 break;
430 default:
431 log.warn("Unsupported filter {}", criterion);
432 fail(filt, ObjectiveError.UNSUPPORTED);
433 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700434 }
435 }
436
Saurav Das0e99e2b2015-10-28 12:39:42 -0700437 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800438 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700439 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800440 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800441 assignedVlan = vidCriterion.vlanId();
Charles Chanc550f2e2017-12-19 19:55:57 -0800442 } else if (filt.meta() != null) {
443 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700444 }
Charles Chane849c192016-01-11 18:28:54 -0800445
Charles Chand55e84d2016-03-30 17:54:24 -0700446 if (assignedVlan == null) {
447 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800448 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700449 log.debug("VLAN ID in criterion={}, metadata={}",
450 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
451 fail(filt, ObjectiveError.BADPARAMS);
452 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700453 }
454 }
455
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800456 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700457 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700458 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700459 } else {
460 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700461 vidCriterion, assignedVlan,
462 applicationId)) {
Saurav Das961beb22017-03-29 19:09:17 -0700463 log.trace("{} MAC filtering rules in TMAC table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800464 (install) ? "adding" : "removing", tmacRule, deviceId);
Charles Chanc550f2e2017-12-19 19:55:57 -0800465
466 if (install) {
467 ops = ops.add(tmacRule);
468 } else {
469 // NOTE: Only remove TMAC flow when there is no more enabled port within the
470 // same VLAN on this device if TMAC doesn't support matching on in_port.
471 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
472 ops = ops.remove(tmacRule);
473 } else {
474 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
475 }
476 }
Saurav Das822c4e22015-10-23 10:51:11 -0700477 }
478 }
479
Charles Chan985b12e2016-05-11 19:47:22 -0700480 if (vidCriterion == null) {
481 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530482 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700483 } else {
Charles Chan14967c22015-12-07 11:11:50 -0800484 /*
485 * NOTE: Separate vlan filtering rules and assignment rules
486 * into different stage in order to guarantee that filtering rules
Saurav Das52025962016-01-28 22:30:01 -0800487 * always go first, as required by ofdpa.
Charles Chan14967c22015-12-07 11:11:50 -0800488 */
489 List<FlowRule> allRules = processVlanIdFilter(
490 portCriterion, vidCriterion, assignedVlan, applicationId);
491 List<FlowRule> filteringRules = new ArrayList<>();
492 List<FlowRule> assignmentRules = new ArrayList<>();
493
494 allRules.forEach(flowRule -> {
Charles Chand1172632017-03-15 17:33:09 -0700495 VlanId vlanId;
496 if (requireVlanExtensions()) {
497 ExtensionCriterion extCriterion =
498 (ExtensionCriterion) flowRule.selector().getCriterion(Criterion.Type.EXTENSION);
499 vlanId = ((OfdpaMatchVlanVid) extCriterion.extensionSelector()).vlanId();
500 } else {
501 VlanIdCriterion vlanIdCriterion =
502 (VlanIdCriterion) flowRule.selector().getCriterion(Criterion.Type.VLAN_VID);
503 vlanId = vlanIdCriterion.vlanId();
504 }
Charles Chanbe8aea42016-02-24 12:04:47 -0800505 if (!vlanId.equals(VlanId.NONE)) {
Charles Chan14967c22015-12-07 11:11:50 -0800506 filteringRules.add(flowRule);
507 } else {
508 assignmentRules.add(flowRule);
509 }
510 });
511
512 for (FlowRule filteringRule : filteringRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700513 log.trace("{} VLAN filtering rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800514 (install) ? "adding" : "removing", filteringRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800515 ops = install ? ops.add(filteringRule) : ops.remove(filteringRule);
516 }
517
518 ops.newStage();
519
520 for (FlowRule assignmentRule : assignmentRules) {
Saurav Das961beb22017-03-29 19:09:17 -0700521 log.trace("{} VLAN assignment rule in VLAN table: {} for dev: {}",
Saurav Das018605f2017-02-18 14:05:44 -0800522 (install) ? "adding" : "removing", assignmentRule, deviceId);
Charles Chan14967c22015-12-07 11:11:50 -0800523 ops = install ? ops.add(assignmentRule) : ops.remove(assignmentRule);
Saurav Das822c4e22015-10-23 10:51:11 -0700524 }
525 }
526
Saurav Das822c4e22015-10-23 10:51:11 -0700527 // apply filtering flow rules
528 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
529 @Override
530 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800531 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700532 ops.stages().get(0).size(), deviceId);
533 pass(filt);
534 }
535
536 @Override
537 public void onError(FlowRuleOperations ops) {
538 log.info("Failed to apply all filtering rules in dev {}", deviceId);
539 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
540 }
541 }));
542
543 }
544
545 /**
Charles Chand1172632017-03-15 17:33:09 -0700546 * Internal implementation of processVlanIdFilter.
547 * <p>
548 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
549 * Since it is non-OF spec, we need an extension treatment for that.
550 * The useVlanExtension must be set to false for OFDPA i12.
551 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700552 *
Charles Chanf9e98652016-09-07 16:54:23 -0700553 * @param portCriterion port on device for which this filter is programmed
554 * @param vidCriterion vlan assigned to port, or NONE for untagged
555 * @param assignedVlan assigned vlan-id for untagged packets
556 * @param applicationId for application programming this filter
Saurav Das822c4e22015-10-23 10:51:11 -0700557 * @return list of FlowRule for port-vlan filters
558 */
559 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700560 VlanIdCriterion vidCriterion,
561 VlanId assignedVlan,
562 ApplicationId applicationId) {
Charles Chan14967c22015-12-07 11:11:50 -0800563 List<FlowRule> rules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700564 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
565 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800566 TrafficSelector.Builder preSelector = null;
567 TrafficTreatment.Builder preTreatment = null;
568
Saurav Das4f980082015-11-05 13:39:15 -0800569 treatment.transition(TMAC_TABLE);
570
Saurav Das822c4e22015-10-23 10:51:11 -0700571 if (vidCriterion.vlanId() == VlanId.NONE) {
572 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700573 preSelector = DefaultTrafficSelector.builder();
574 if (requireVlanExtensions()) {
575 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
576 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700577 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
578 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700579
580 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
581 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700582 } else {
Charles Chand1172632017-03-15 17:33:09 -0700583 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700584 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700585
586 preSelector.matchVlanId(assignedVlan);
587 }
588 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
589 } else {
590 if (requireVlanExtensions()) {
591 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
592 selector.extension(ofdpaMatchVlanVid, deviceId);
593 } else {
594 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700595 }
Charles Chan14967c22015-12-07 11:11:50 -0800596
Charles Chand55e84d2016-03-30 17:54:24 -0700597 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700598 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800599 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
600 treatment.extension(ofdpaSetVlanVid, deviceId);
601 } else {
602 treatment.setVlanId(assignedVlan);
603 }
Charles Chand55e84d2016-03-30 17:54:24 -0700604 }
Saurav Das822c4e22015-10-23 10:51:11 -0700605 }
Saurav Das822c4e22015-10-23 10:51:11 -0700606
607 // ofdpa cannot match on ALL portnumber, so we need to use separate
608 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800609 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530610 if (portCriterion != null) {
611 if (PortNumber.ALL.equals(portCriterion.port())) {
612 for (Port port : deviceService.getPorts(deviceId)) {
613 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
614 portnums.add(port.number());
615 }
Saurav Das822c4e22015-10-23 10:51:11 -0700616 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530617 } else {
618 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700619 }
620 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530621 log.warn("Filtering Objective missing Port Criterion . " +
622 "VLAN Table cannot be programmed for {}",
623 deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700624 }
Saurav Das4f980082015-11-05 13:39:15 -0800625
Saurav Das822c4e22015-10-23 10:51:11 -0700626 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800627 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700628 selector.matchInPort(pnum);
629 FlowRule rule = DefaultFlowRule.builder()
630 .forDevice(deviceId)
631 .withSelector(selector.build())
632 .withTreatment(treatment.build())
633 .withPriority(DEFAULT_PRIORITY)
634 .fromApp(applicationId)
635 .makePermanent()
636 .forTable(VLAN_TABLE).build();
Charles Chan14967c22015-12-07 11:11:50 -0800637
638 if (preSelector != null) {
639 preSelector.matchInPort(pnum);
640 FlowRule preRule = DefaultFlowRule.builder()
641 .forDevice(deviceId)
642 .withSelector(preSelector.build())
643 .withTreatment(preTreatment.build())
644 .withPriority(DEFAULT_PRIORITY)
645 .fromApp(applicationId)
646 .makePermanent()
647 .forTable(VLAN_TABLE).build();
648 rules.add(preRule);
649 }
650
Saurav Das822c4e22015-10-23 10:51:11 -0700651 rules.add(rule);
652 }
653 return rules;
654 }
655
656 /**
657 * Allows routed packets with correct destination MAC to be directed
658 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700659 *
660 * @param portCriterion port on device for which this filter is programmed
661 * @param ethCriterion dstMac of device for which is filter is programmed
662 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700663 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700664 * @param applicationId for application programming this filter
665 * @return list of FlowRule for port-vlan filters
666
667 */
668 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
669 EthCriterion ethCriterion,
670 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700671 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700672 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800673 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530674 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800675 return processEthDstOnlyFilter(ethCriterion, applicationId);
676 }
677
Charles Chan5b9df8d2016-03-28 22:21:40 -0700678 // Multicast MAC
679 if (ethCriterion.mask() != null) {
Charles Chanb4879a52017-10-20 19:09:16 -0700680 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700681 }
682
Saurav Das822c4e22015-10-23 10:51:11 -0700683 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530684 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700685 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700686 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530687 List<FlowRule> rules = new ArrayList<>();
688 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
689 if (vidCriterion != null && requireVlanExtensions()) {
690 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
691 }
Saurav Das822c4e22015-10-23 10:51:11 -0700692 // ofdpa cannot match on ALL portnumber, so we need to use separate
693 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700694 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530695 if (portCriterion != null) {
696 if (PortNumber.ALL.equals(portCriterion.port())) {
697 for (Port port : deviceService.getPorts(deviceId)) {
698 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
699 portnums.add(port.number());
700 }
Saurav Das822c4e22015-10-23 10:51:11 -0700701 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530702 } else {
703 portnums.add(portCriterion.port());
704 }
705 for (PortNumber pnum : portnums) {
706 rules.add(buildTmacRuleForIpv4(ethCriterion,
707 vidCriterion,
708 ofdpaMatchVlanVid,
709 applicationId,
710 pnum));
711 rules.add(buildTmacRuleForMpls(ethCriterion,
712 vidCriterion,
713 ofdpaMatchVlanVid,
714 applicationId,
715 pnum));
716 rules.add(buildTmacRuleForIpv6(ethCriterion,
717 vidCriterion,
718 ofdpaMatchVlanVid,
719 applicationId,
720 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700721 }
722 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530723 rules.add(buildTmacRuleForIpv4(ethCriterion,
724 vidCriterion,
725 ofdpaMatchVlanVid,
726 applicationId,
727 null));
728 rules.add(buildTmacRuleForMpls(ethCriterion,
729 vidCriterion,
730 ofdpaMatchVlanVid,
731 applicationId,
732 null));
733 rules.add(buildTmacRuleForIpv6(ethCriterion,
734 vidCriterion,
735 ofdpaMatchVlanVid,
736 applicationId,
737 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700738 }
739 return rules;
740 }
741
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530742 /**
743 * Builds TMAC rules for IPv4 packets.
744 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800745 * @param ethCriterion dst mac matching
746 * @param vidCriterion vlan id assigned to the port
747 * @param ofdpaMatchVlanVid OFDPA vlan id matching
748 * @param applicationId application id
749 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530750 * @return TMAC rule for IPV4 packets
751 */
752 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
753 VlanIdCriterion vidCriterion,
754 OfdpaMatchVlanVid ofdpaMatchVlanVid,
755 ApplicationId applicationId,
756 PortNumber pnum) {
757 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
758 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
759 if (pnum != null) {
760 if (matchInPortTmacTable()) {
761 selector.matchInPort(pnum);
762 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800763 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530764 "ignoring the IN_PORT criteria");
765 }
766 }
767 if (vidCriterion != null) {
768 if (requireVlanExtensions()) {
769 selector.extension(ofdpaMatchVlanVid, deviceId);
770 } else {
771 selector.matchVlanId(vidCriterion.vlanId());
772 }
773 }
774 selector.matchEthType(Ethernet.TYPE_IPV4);
775 selector.matchEthDst(ethCriterion.mac());
776 treatment.transition(UNICAST_ROUTING_TABLE);
777 return DefaultFlowRule.builder()
778 .forDevice(deviceId)
779 .withSelector(selector.build())
780 .withTreatment(treatment.build())
781 .withPriority(DEFAULT_PRIORITY)
782 .fromApp(applicationId)
783 .makePermanent()
784 .forTable(TMAC_TABLE).build();
785 }
786
787 /**
788 * Builds TMAC rule for MPLS packets.
789 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800790 * @param ethCriterion dst mac matching
791 * @param vidCriterion vlan id assigned to the port
792 * @param ofdpaMatchVlanVid OFDPA vlan id matching
793 * @param applicationId application id
794 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530795 * @return TMAC rule for MPLS packets
796 */
797 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
798 VlanIdCriterion vidCriterion,
799 OfdpaMatchVlanVid ofdpaMatchVlanVid,
800 ApplicationId applicationId,
801 PortNumber pnum) {
802 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
803 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
804 if (pnum != null) {
805 if (matchInPortTmacTable()) {
806 selector.matchInPort(pnum);
807 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800808 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530809 "ignoring the IN_PORT criteria");
810 }
811 }
812 if (vidCriterion != null) {
813 if (requireVlanExtensions()) {
814 selector.extension(ofdpaMatchVlanVid, deviceId);
815 } else {
816 selector.matchVlanId(vidCriterion.vlanId());
817 }
818 }
819 selector.matchEthType(Ethernet.MPLS_UNICAST);
820 selector.matchEthDst(ethCriterion.mac());
821 treatment.transition(MPLS_TABLE_0);
822 return DefaultFlowRule.builder()
823 .forDevice(deviceId)
824 .withSelector(selector.build())
825 .withTreatment(treatment.build())
826 .withPriority(DEFAULT_PRIORITY)
827 .fromApp(applicationId)
828 .makePermanent()
829 .forTable(TMAC_TABLE).build();
830 }
831
832 /**
833 * Builds TMAC rules for IPv6 packets.
834 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800835 * @param ethCriterion dst mac matching
836 * @param vidCriterion vlan id assigned to the port
837 * @param ofdpaMatchVlanVid OFDPA vlan id matching
838 * @param applicationId application id
839 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530840 * @return TMAC rule for IPV6 packets
841 */
842 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
843 VlanIdCriterion vidCriterion,
844 OfdpaMatchVlanVid ofdpaMatchVlanVid,
845 ApplicationId applicationId,
846 PortNumber pnum) {
847 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
848 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
849 if (pnum != null) {
850 if (matchInPortTmacTable()) {
851 selector.matchInPort(pnum);
852 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800853 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530854 "ignoring the IN_PORT criteria");
855 }
856 }
857 if (vidCriterion != null) {
858 if (requireVlanExtensions()) {
859 selector.extension(ofdpaMatchVlanVid, deviceId);
860 } else {
861 selector.matchVlanId(vidCriterion.vlanId());
862 }
863 }
864 selector.matchEthType(Ethernet.TYPE_IPV6);
865 selector.matchEthDst(ethCriterion.mac());
866 treatment.transition(UNICAST_ROUTING_TABLE);
867 return DefaultFlowRule.builder()
868 .forDevice(deviceId)
869 .withSelector(selector.build())
870 .withTreatment(treatment.build())
871 .withPriority(DEFAULT_PRIORITY)
872 .fromApp(applicationId)
873 .makePermanent()
874 .forTable(TMAC_TABLE).build();
875 }
876
Charles Chan5270ed02016-01-30 23:22:37 -0800877 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700878 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800879 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
880
Charles Chan5270ed02016-01-30 23:22:37 -0800881 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
882 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
883 selector.matchEthType(Ethernet.TYPE_IPV4);
884 selector.matchEthDst(ethCriterion.mac());
885 treatment.transition(UNICAST_ROUTING_TABLE);
886 FlowRule rule = DefaultFlowRule.builder()
887 .forDevice(deviceId)
888 .withSelector(selector.build())
889 .withTreatment(treatment.build())
890 .withPriority(DEFAULT_PRIORITY)
891 .fromApp(applicationId)
892 .makePermanent()
893 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800894 builder.add(rule);
895
896 selector = DefaultTrafficSelector.builder();
897 treatment = DefaultTrafficTreatment.builder();
898 selector.matchEthType(Ethernet.TYPE_IPV6);
899 selector.matchEthDst(ethCriterion.mac());
900 treatment.transition(UNICAST_ROUTING_TABLE);
901 rule = DefaultFlowRule.builder()
902 .forDevice(deviceId)
903 .withSelector(selector.build())
904 .withTreatment(treatment.build())
905 .withPriority(DEFAULT_PRIORITY)
906 .fromApp(applicationId)
907 .makePermanent()
908 .forTable(TMAC_TABLE).build();
909 return builder.add(rule).build();
Charles Chan5270ed02016-01-30 23:22:37 -0800910 }
911
Charles Chan5b9df8d2016-03-28 22:21:40 -0700912 protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700913 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700914 ApplicationId applicationId) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000915 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700916 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
917 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700918 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000919
Charles Chanb4879a52017-10-20 19:09:16 -0700920 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
921 selector.matchEthType(Ethernet.TYPE_IPV4);
922 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
923 selector.matchVlanId(assignedVlan);
924 treatment.transition(MULTICAST_ROUTING_TABLE);
925 rule = DefaultFlowRule.builder()
926 .forDevice(deviceId)
927 .withSelector(selector.build())
928 .withTreatment(treatment.build())
929 .withPriority(DEFAULT_PRIORITY)
930 .fromApp(applicationId)
931 .makePermanent()
932 .forTable(TMAC_TABLE).build();
933 builder.add(rule);
934 }
935
936 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
937 selector = DefaultTrafficSelector.builder();
938 treatment = DefaultTrafficTreatment.builder();
939 selector.matchEthType(Ethernet.TYPE_IPV6);
940 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
941 selector.matchVlanId(assignedVlan);
942 treatment.transition(MULTICAST_ROUTING_TABLE);
943 rule = DefaultFlowRule.builder()
944 .forDevice(deviceId)
945 .withSelector(selector.build())
946 .withTreatment(treatment.build())
947 .withPriority(DEFAULT_PRIORITY)
948 .fromApp(applicationId)
949 .makePermanent()
950 .forTable(TMAC_TABLE).build();
951 builder.add(rule);
952 }
953 return builder.build();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700954 }
955
Saurav Das822c4e22015-10-23 10:51:11 -0700956 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
957 switch (fwd.flag()) {
958 case SPECIFIC:
959 return processSpecific(fwd);
960 case VERSATILE:
961 return processVersatile(fwd);
962 default:
963 fail(fwd, ObjectiveError.UNKNOWN);
964 log.warn("Unknown forwarding flag {}", fwd.flag());
965 }
966 return Collections.emptySet();
967 }
968
969 /**
970 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
971 * ACL table.
972 * @param fwd the forwarding objective of type 'versatile'
973 * @return a collection of flow rules to be sent to the switch. An empty
974 * collection may be returned if there is a problem in processing
975 * the flow rule
976 */
Saurav Das52025962016-01-28 22:30:01 -0800977 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800978 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800979 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700980
981 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800982 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700983 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800984 log.error("Versatile forwarding objective:{} must include ethType",
985 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700986 fail(fwd, ObjectiveError.BADPARAMS);
987 return Collections.emptySet();
988 }
989 if (fwd.nextId() == null && fwd.treatment() == null) {
990 log.error("Forwarding objective {} from {} must contain "
991 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800992 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700993 return Collections.emptySet();
994 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800995
Saurav Das77b5e902016-01-27 17:01:59 -0800996 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
997 fwd.selector().criteria().forEach(criterion -> {
998 if (criterion instanceof VlanIdCriterion) {
999 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
1000 // ensure that match does not include vlan = NONE as OF-DPA does not
1001 // match untagged packets this way in the ACL table.
1002 if (vlanId.equals(VlanId.NONE)) {
1003 return;
1004 }
Charles Chand1172632017-03-15 17:33:09 -07001005 if (requireVlanExtensions()) {
1006 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1007 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1008 } else {
1009 sbuilder.matchVlanId(vlanId);
1010 }
Charles Chan09bf2692018-01-11 11:48:18 -08001011 } else if (criterion instanceof Icmpv6TypeCriterion) {
1012 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1013 sbuilder.matchIcmpv6Type(icmpv6Type);
1014 } else if (criterion instanceof Icmpv6CodeCriterion) {
1015 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1016 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001017 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1018 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1019 // Currently L4 dst port matching is only used by DHCP relay feature
1020 // and therefore is safe to be replaced with L4 src port matching.
1021 // We need to revisit this if L4 dst port is used for other purpose in the future.
1022 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001023 switch (criterion.type()) {
1024 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001025 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001026 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001027 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001028 break;
1029 default:
1030 sbuilder.add(criterion);
1031 }
1032 } else {
1033 sbuilder.add(criterion);
1034 }
Saurav Das77b5e902016-01-27 17:01:59 -08001035 } else {
1036 sbuilder.add(criterion);
1037 }
1038 });
1039
Saurav Das822c4e22015-10-23 10:51:11 -07001040 // XXX driver does not currently do type checking as per Tables 65-67 in
1041 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001042 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1043 if (fwd.treatment() != null) {
1044 for (Instruction ins : fwd.treatment().allInstructions()) {
1045 if (ins instanceof OutputInstruction) {
1046 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001047 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001048 ttBuilder.add(o);
1049 } else {
1050 log.warn("Only allowed treatments in versatile forwarding "
1051 + "objectives are punts to the controller");
1052 }
1053 } else {
1054 log.warn("Cannot process instruction in versatile fwd {}", ins);
1055 }
Saurav Das822c4e22015-10-23 10:51:11 -07001056 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001057 if (fwd.treatment().clearedDeferred()) {
1058 ttBuilder.wipeDeferred();
1059 }
Saurav Das822c4e22015-10-23 10:51:11 -07001060 }
Saurav Das822c4e22015-10-23 10:51:11 -07001061 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001062 // overide case
1063 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301064 if (next == null) {
1065 fail(fwd, ObjectiveError.BADPARAMS);
1066 return Collections.emptySet();
1067 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001068 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1069 // we only need the top level group's key to point the flow to it
1070 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1071 if (group == null) {
1072 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1073 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1074 fail(fwd, ObjectiveError.GROUPMISSING);
1075 return Collections.emptySet();
1076 }
1077 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001078 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001079
1080 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1081 .fromApp(fwd.appId())
1082 .withPriority(fwd.priority())
1083 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001084 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001085 .withTreatment(ttBuilder.build())
1086 .makePermanent()
1087 .forTable(ACL_TABLE);
1088 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001089 }
1090
1091 /**
1092 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001093 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001094 *
1095 * @param fwd the forwarding objective of type 'specific'
1096 * @return a collection of flow rules. Typically there will be only one
1097 * for this type of forwarding objective. An empty set may be
1098 * returned if there is an issue in processing the objective.
1099 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001100 protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001101 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001102 fwd.id(), deviceId, fwd.nextId());
1103 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1104 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1105
1106 if (isEthTypeObj) {
1107 return processEthTypeSpecific(fwd);
1108 } else if (isEthDstObj) {
1109 return processEthDstSpecific(fwd);
1110 } else {
1111 log.warn("processSpecific: Unsupported forwarding objective "
1112 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001113 fail(fwd, ObjectiveError.UNSUPPORTED);
1114 return Collections.emptySet();
1115 }
Saurav Das4ce45962015-11-24 23:21:05 -08001116 }
1117
Saurav Das4ce45962015-11-24 23:21:05 -08001118 /**
1119 * Handles forwarding rules to the IP and MPLS tables.
1120 *
1121 * @param fwd the forwarding objective
1122 * @return A collection of flow rules, or an empty set
1123 */
1124 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001125 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001126 }
1127
1128 /**
1129 * Internal implementation of processEthTypeSpecific.
1130 * <p>
1131 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1132 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1133 * The allowDefaultRoute must be set to false for OFDPA i12.
1134 * </p>
1135 *
1136 * @param fwd the forwarding objective
1137 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001138 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001139 * @return A collection of flow rules, or an empty set
1140 */
1141 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001142 boolean allowDefaultRoute,
1143 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001144 TrafficSelector selector = fwd.selector();
1145 EthTypeCriterion ethType =
1146 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Flavio Castroe10fa242016-01-15 12:43:51 -08001147 boolean popMpls = false;
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001148 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001149 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001150 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001151 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001152 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001153
Saurav Das8a0732e2015-11-20 15:27:53 -08001154 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001155 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1156 return Collections.emptyList();
1157 }
1158 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001159 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001160 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001161 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001162 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001163 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001164 }
Charles Chan14967c22015-12-07 11:11:50 -08001165 if (fwd.treatment() != null) {
1166 for (Instruction instr : fwd.treatment().allInstructions()) {
1167 if (instr instanceof L3ModificationInstruction &&
1168 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Saurav Das9c705342017-01-06 11:36:01 -08001169 // XXX decrementing IP ttl is done automatically for routing, this
1170 // action is ignored or rejected in ofdpa as it is not fully implemented
1171 //tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001172 }
1173 }
1174 }
1175
Pier Ventree0ae7a32016-11-23 09:57:42 -08001176 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1177 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1178 return Collections.emptyList();
1179 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001180 //We need to set the proper next table
1181 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1182 if (ipv6Dst.isMulticast()) {
1183 forTableId = MULTICAST_ROUTING_TABLE;
1184 } else {
1185 forTableId = UNICAST_ROUTING_TABLE;
1186 }
1187
Pier Ventree0ae7a32016-11-23 09:57:42 -08001188 if (fwd.treatment() != null) {
1189 for (Instruction instr : fwd.treatment().allInstructions()) {
1190 if (instr instanceof L3ModificationInstruction &&
1191 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1192 // XXX decrementing IP ttl is done automatically for routing, this
1193 // action is ignored or rejected in ofdpa as it is not fully implemented
1194 //tb.deferred().add(instr);
1195 }
1196 }
1197 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001198 } else {
1199 filteredSelector
1200 .matchEthType(Ethernet.MPLS_UNICAST)
1201 .matchMplsLabel(((MplsCriterion)
1202 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1203 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001204 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001205 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001206 filteredSelector.matchMplsBos(bos.mplsBos());
1207 }
1208 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001209 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1210 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001211
Charles Chan14967c22015-12-07 11:11:50 -08001212 if (fwd.treatment() != null) {
1213 for (Instruction instr : fwd.treatment().allInstructions()) {
1214 if (instr instanceof L2ModificationInstruction &&
1215 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001216 popMpls = true;
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001217 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001218 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001219 if (requireMplsPop()) {
1220 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1221 tb.immediate().popMpls();
1222 }
1223 } else {
1224 // Skip mpls pop action for mpls_unicast label
1225 if (instr instanceof ModMplsHeaderInstruction &&
1226 !((ModMplsHeaderInstruction) instr).ethernetType()
1227 .equals(EtherType.MPLS_UNICAST.ethType())) {
1228 tb.immediate().add(instr);
1229 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001230 }
Charles Chan14967c22015-12-07 11:11:50 -08001231 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001232
1233 if (requireMplsTtlModification()) {
1234 if (instr instanceof L3ModificationInstruction &&
1235 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1236 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1237 tb.immediate().decMplsTtl();
1238 }
1239 if (instr instanceof L3ModificationInstruction &&
1240 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1241 tb.immediate().add(instr);
1242 }
Charles Chan14967c22015-12-07 11:11:50 -08001243 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001244 }
1245 }
1246 }
Saurav Das822c4e22015-10-23 10:51:11 -07001247
1248 if (fwd.nextId() != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001249 if (forTableId == MPLS_TABLE_1 && !popMpls) {
1250 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
Saurav Das25190812016-05-27 13:54:07 -07001251 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1252 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001253 // XXX We could convert to forwarding to a single-port, via a MPLS interface,
1254 // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective.
1255 // Also the pop-mpls logic used here won't work in non-BoS case.
Saurav Das4ce45962015-11-24 23:21:05 -08001256 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
Saurav Das8a0732e2015-11-20 15:27:53 -08001257 return Collections.emptySet();
1258 }
1259
Saurav Das423fe2b2015-12-04 10:52:59 -08001260 NextGroup next = getGroupForNextObjective(fwd.nextId());
1261 if (next != null) {
1262 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1263 // we only need the top level group's key to point the flow to it
1264 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
Pier Luigi075f1012018-02-01 10:23:12 +01001265 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
Pier Ventre140a8942016-11-02 07:26:38 -07001266 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1267 + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}"
1268 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1269 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1270 return Collections.emptySet();
1271 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001272 if (group == null) {
1273 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1274 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1275 fail(fwd, ObjectiveError.GROUPMISSING);
1276 return Collections.emptySet();
1277 }
1278 tb.deferred().group(group.id());
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001279 // check if group is empty
1280 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
1281 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1282 Integer.toHexString(group.id().id()), deviceId, fwd.id());
1283 emptyGroup = true;
1284 }
Saurav Das25190812016-05-27 13:54:07 -07001285 } else {
1286 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1287 fwd.nextId(), deviceId, fwd.id());
1288 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1289 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001290 }
Saurav Das822c4e22015-10-23 10:51:11 -07001291 }
Charles Chancad338a2016-09-16 18:03:11 -07001292
1293 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001294 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001295 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001296 // set mpls type as apply_action
1297 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001298 }
1299 tb.transition(mplsNextTable);
1300 } else {
1301 tb.transition(ACL_TABLE);
1302 }
1303
Saurav Das822c4e22015-10-23 10:51:11 -07001304 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1305 .fromApp(fwd.appId())
1306 .withPriority(fwd.priority())
1307 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001308 .withSelector(filteredSelector.build())
1309 .withTreatment(tb.build())
1310 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001311
1312 if (fwd.permanent()) {
1313 ruleBuilder.makePermanent();
1314 } else {
1315 ruleBuilder.makeTemporary(fwd.timeout());
1316 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001317 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1318 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001319 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001320 flowRuleCollection.add(
1321 defaultRoute(fwd, complementarySelector, forTableId, tb)
1322 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001323 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1324 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001325 // XXX retrying flows may be necessary due to bug CORD-554
1326 if (emptyGroup) {
1327 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1328 RETRY_MS, TimeUnit.MILLISECONDS);
1329 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001330 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001331 }
1332
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001333 protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
1334 TrafficSelector.Builder extBuilder,
1335 ForwardingObjective fwd,
1336 boolean allowDefaultRoute) {
1337 TrafficSelector selector = fwd.selector();
1338
1339 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1340 if (ipv4Dst.isMulticast()) {
1341 if (ipv4Dst.prefixLength() != 32) {
1342 log.warn("Multicast specific forwarding objective can only be /32");
1343 fail(fwd, ObjectiveError.BADPARAMS);
1344 return -1;
1345 }
1346 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1347 if (assignedVlan == null) {
1348 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1349 fail(fwd, ObjectiveError.BADPARAMS);
1350 return -1;
1351 }
Charles Chand1172632017-03-15 17:33:09 -07001352 if (requireVlanExtensions()) {
1353 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1354 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1355 } else {
1356 builderToUpdate.matchVlanId(assignedVlan);
1357 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001358 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1359 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1360 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1361 } else {
1362 if (ipv4Dst.prefixLength() == 0) {
1363 if (allowDefaultRoute) {
1364 // The entire IPV4_DST field is wildcarded intentionally
1365 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1366 } else {
1367 // NOTE: The switch does not support matching 0.0.0.0/0
1368 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1369 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1370 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1371 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1372 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1373 }
1374 } else {
1375 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1376 }
1377 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1378 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1379 }
1380 return 0;
1381 }
1382
1383 /**
1384 * Helper method to build Ipv6 selector using the selector provided by
1385 * a forwarding objective.
1386 *
1387 * @param builderToUpdate the builder to update
1388 * @param fwd the selector to read
1389 * @return 0 if the update ends correctly. -1 if the matches
1390 * are not yet supported
1391 */
1392 protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
1393 ForwardingObjective fwd) {
1394
1395 TrafficSelector selector = fwd.selector();
1396
1397 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1398 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001399 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1400 log.warn("Multicast specific forwarding objective can only be /128");
1401 fail(fwd, ObjectiveError.BADPARAMS);
1402 return -1;
1403 }
1404 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1405 if (assignedVlan == null) {
1406 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1407 fail(fwd, ObjectiveError.BADPARAMS);
1408 return -1;
1409 }
1410 if (requireVlanExtensions()) {
1411 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1412 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1413 } else {
1414 builderToUpdate.matchVlanId(assignedVlan);
1415 }
1416 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1417 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1418 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1419 } else {
1420 if (ipv6Dst.prefixLength() != 0) {
1421 builderToUpdate.matchIPv6Dst(ipv6Dst);
1422 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001423 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1424 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1425 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001426 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001427 return 0;
1428 }
1429
Pier Ventree0ae7a32016-11-23 09:57:42 -08001430 protected FlowRule defaultRoute(ForwardingObjective fwd,
1431 TrafficSelector.Builder complementarySelector,
1432 int forTableId,
1433 TrafficTreatment.Builder tb) {
1434 FlowRule.Builder rule = DefaultFlowRule.builder()
1435 .fromApp(fwd.appId())
1436 .withPriority(fwd.priority())
1437 .forDevice(deviceId)
1438 .withSelector(complementarySelector.build())
1439 .withTreatment(tb.build())
1440 .forTable(forTableId);
1441 if (fwd.permanent()) {
1442 rule.makePermanent();
1443 } else {
1444 rule.makeTemporary(fwd.timeout());
1445 }
1446 return rule.build();
1447 }
1448
Saurav Das4ce45962015-11-24 23:21:05 -08001449 /**
1450 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1451 * allowed in the bridging table - instead we use L2 Interface group or
1452 * L2 flood group
1453 *
1454 * @param fwd the forwarding objective
1455 * @return A collection of flow rules, or an empty set
1456 */
1457 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1458 List<FlowRule> rules = new ArrayList<>();
1459
1460 // Build filtered selector
1461 TrafficSelector selector = fwd.selector();
1462 EthCriterion ethCriterion = (EthCriterion) selector
1463 .getCriterion(Criterion.Type.ETH_DST);
1464 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1465 .getCriterion(Criterion.Type.VLAN_VID);
1466
1467 if (vlanIdCriterion == null) {
1468 log.warn("Forwarding objective for bridging requires vlan. Not "
1469 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1470 fail(fwd, ObjectiveError.BADPARAMS);
1471 return Collections.emptySet();
1472 }
1473
1474 TrafficSelector.Builder filteredSelectorBuilder =
1475 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001476
1477 if (!ethCriterion.mac().equals(NONE) &&
1478 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001479 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1480 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1481 fwd.id(), fwd.nextId(), deviceId);
1482 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001483 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001484 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1485 + "in dev:{} for vlan:{}",
1486 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1487 }
Charles Chand1172632017-03-15 17:33:09 -07001488 if (requireVlanExtensions()) {
1489 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1490 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1491 } else {
1492 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1493 }
Saurav Das4ce45962015-11-24 23:21:05 -08001494 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1495
1496 if (fwd.treatment() != null) {
1497 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1498 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1499 }
1500
1501 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1502 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001503 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001504 if (next != null) {
1505 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1506 // we only need the top level group's key to point the flow to it
1507 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1508 if (group != null) {
1509 treatmentBuilder.deferred().group(group.id());
1510 } else {
1511 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1512 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1513 fail(fwd, ObjectiveError.GROUPMISSING);
1514 return Collections.emptySet();
1515 }
1516 }
1517 }
1518 treatmentBuilder.immediate().transition(ACL_TABLE);
1519 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1520
1521 // Build bridging table entries
1522 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1523 flowRuleBuilder.fromApp(fwd.appId())
1524 .withPriority(fwd.priority())
1525 .forDevice(deviceId)
1526 .withSelector(filteredSelector)
1527 .withTreatment(filteredTreatment)
1528 .forTable(BRIDGING_TABLE);
1529 if (fwd.permanent()) {
1530 flowRuleBuilder.makePermanent();
1531 } else {
1532 flowRuleBuilder.makeTemporary(fwd.timeout());
1533 }
1534 rules.add(flowRuleBuilder.build());
1535 return rules;
1536 }
1537
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001538 //////////////////////////////////////
1539 // Helper Methods and Classes
1540 //////////////////////////////////////
1541
1542 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1543 TrafficSelector selector = fwd.selector();
1544 EthTypeCriterion ethType = (EthTypeCriterion) selector
1545 .getCriterion(Criterion.Type.ETH_TYPE);
1546 return !((ethType == null) ||
1547 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001548 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1549 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001550 }
1551
1552 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1553 TrafficSelector selector = fwd.selector();
1554 EthCriterion ethDst = (EthCriterion) selector
1555 .getCriterion(Criterion.Type.ETH_DST);
1556 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1557 .getCriterion(Criterion.Type.VLAN_VID);
1558 return !(ethDst == null && vlanId == null);
1559 }
1560
Saurav Das423fe2b2015-12-04 10:52:59 -08001561 protected NextGroup getGroupForNextObjective(Integer nextId) {
1562 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1563 if (next != null) {
1564 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1565 if (gkeys != null && !gkeys.isEmpty()) {
1566 return next;
1567 } else {
1568 log.warn("Empty next group found in FlowObjective store for "
1569 + "next-id:{} in dev:{}", nextId, deviceId);
1570 }
1571 } else {
1572 log.warn("next-id {} not found in Flow objective store for dev:{}",
1573 nextId, deviceId);
1574 }
1575 return null;
1576 }
1577
Charles Chan188ebf52015-12-23 00:15:11 -08001578 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001579 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001580 }
1581
Charles Chan188ebf52015-12-23 00:15:11 -08001582 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001583 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001584 }
Saurav Das24431192016-03-07 19:13:00 -08001585
Saurav Das24431192016-03-07 19:13:00 -08001586 @Override
1587 public List<String> getNextMappings(NextGroup nextGroup) {
1588 List<String> mappings = new ArrayList<>();
1589 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1590 for (Deque<GroupKey> gkd : gkeys) {
1591 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001592 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001593 for (GroupKey gk : gkd) {
1594 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001595 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001596 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001597 continue;
1598 }
1599 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1600 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001601 lastGroup = g;
1602 }
1603 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001604 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001605 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001606 lastGroupIns = lastGroup.buckets().buckets().get(0)
1607 .treatment().allInstructions();
1608 }
1609 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001610 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001611 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001612 }
1613 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001614 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001615 }
1616 return mappings;
1617 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001618
Pier Ventre140a8942016-11-02 07:26:38 -07001619 static boolean isMplsBos(TrafficSelector selector) {
1620 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1621 return bosCriterion != null && bosCriterion.mplsBos();
1622 }
1623
1624 static boolean isNotMplsBos(TrafficSelector selector) {
1625 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1626 return bosCriterion != null && !bosCriterion.mplsBos();
1627 }
1628
Charles Chand9e47c62017-10-05 15:17:15 -07001629 private static boolean isIpv6(TrafficSelector selector) {
1630 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1631 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1632 }
1633
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001634 public static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001635 if (selector == null) {
1636 return null;
1637 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001638 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1639 return (criterion == null)
1640 ? null : ((VlanIdCriterion) criterion).vlanId();
1641 }
1642
Charles Chand9e47c62017-10-05 15:17:15 -07001643 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001644 if (selector == null) {
1645 return null;
1646 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001647 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1648 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1649 }
Charles Chand55e84d2016-03-30 17:54:24 -07001650
1651 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001652 if (treatment == null) {
1653 return null;
1654 }
Charles Chand55e84d2016-03-30 17:54:24 -07001655 for (Instruction i : treatment.allInstructions()) {
1656 if (i instanceof ModVlanIdInstruction) {
1657 return ((ModVlanIdInstruction) i).vlanId();
1658 }
1659 }
1660 return null;
1661 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001662
1663 /**
1664 * Utility class that retries sending flows a fixed number of times, even if
1665 * some of the attempts are successful. Used only for forwarding objectives.
1666 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001667 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001668 int attempts = MAX_RETRY_ATTEMPTS;
1669 private Collection<FlowRule> retryFlows;
1670 private ForwardingObjective fwd;
1671
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001672 public RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001673 this.fwd = fwd;
1674 this.retryFlows = retryFlows;
1675 }
1676
1677 @Override
1678 public void run() {
1679 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1680 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1681 sendForward(fwd, retryFlows);
1682 if (--attempts > 0) {
1683 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1684 }
1685 }
1686 }
1687
Saurav Das822c4e22015-10-23 10:51:11 -07001688}