blob: 8f7b6227444339280a81a02e653133cf9ea21ca3 [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;
54import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080055import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
56import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080057import org.onosproject.net.flow.criteria.MplsBosCriterion;
58import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070059import org.onosproject.net.flow.criteria.PortCriterion;
Charles Chand9e47c62017-10-05 15:17:15 -070060import org.onosproject.net.flow.criteria.TcpPortCriterion;
61import org.onosproject.net.flow.criteria.UdpPortCriterion;
Saurav Das822c4e22015-10-23 10:51:11 -070062import org.onosproject.net.flow.criteria.VlanIdCriterion;
63import org.onosproject.net.flow.instructions.Instruction;
64import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
65import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080066import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070067import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
Alex Yashchuk4caa8e82017-12-08 17:40:05 +020068import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Charles Chan14967c22015-12-07 11:11:50 -080069import org.onosproject.net.flow.instructions.L3ModificationInstruction;
70import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
Saurav Das822c4e22015-10-23 10:51:11 -070071import org.onosproject.net.flowobjective.FilteringObjective;
72import org.onosproject.net.flowobjective.FlowObjectiveStore;
73import org.onosproject.net.flowobjective.ForwardingObjective;
74import org.onosproject.net.flowobjective.NextObjective;
75import org.onosproject.net.flowobjective.Objective;
76import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Das822c4e22015-10-23 10:51:11 -070077import org.onosproject.net.group.DefaultGroupKey;
78import org.onosproject.net.group.Group;
Saurav Das822c4e22015-10-23 10:51:11 -070079import org.onosproject.net.group.GroupKey;
Saurav Das822c4e22015-10-23 10:51:11 -070080import org.onosproject.net.group.GroupService;
Saurav Das822c4e22015-10-23 10:51:11 -070081import org.onosproject.store.serializers.KryoNamespaces;
82import org.slf4j.Logger;
83
Pier Ventree0ae7a32016-11-23 09:57:42 -080084import java.util.ArrayDeque;
85import java.util.ArrayList;
86import java.util.Collection;
87import java.util.Collections;
88import java.util.Deque;
89import java.util.List;
90import java.util.Objects;
Pier Ventree0ae7a32016-11-23 09:57:42 -080091import java.util.concurrent.ScheduledExecutorService;
92import java.util.concurrent.TimeUnit;
93
94import static java.util.concurrent.Executors.newScheduledThreadPool;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080095import static org.onlab.packet.MacAddress.BROADCAST;
Charles Chanb4879a52017-10-20 19:09:16 -070096import static org.onlab.packet.MacAddress.IPV4_MULTICAST;
97import static org.onlab.packet.MacAddress.IPV6_MULTICAST;
Yi Tseng3a77b4f2017-02-06 15:02:17 -080098import static org.onlab.packet.MacAddress.NONE;
Pier Ventree0ae7a32016-11-23 09:57:42 -080099import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -0700100import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Charles Chand9e47c62017-10-05 15:17:15 -0700101import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE;
Pier Luigi075f1012018-02-01 10:23:12 +0100102import static org.onosproject.net.group.GroupDescription.Type.SELECT;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800103import static org.slf4j.LoggerFactory.getLogger;
Pier Ventre140a8942016-11-02 07:26:38 -0700104import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800105
Saurav Das822c4e22015-10-23 10:51:11 -0700106/**
107 * Driver for Broadcom's OF-DPA v2.0 TTP.
Saurav Das822c4e22015-10-23 10:51:11 -0700108 */
Charles Chan361154b2016-03-24 10:23:39 -0700109public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
Saurav Das822c4e22015-10-23 10:51:11 -0700110 protected static final int PORT_TABLE = 0;
111 protected static final int VLAN_TABLE = 10;
Pier Ventre42287df2016-11-09 14:17:26 -0800112 protected static final int VLAN_1_TABLE = 11;
113 protected static final int MPLS_L2_PORT_FLOW_TABLE = 13;
114 protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16;
Saurav Das822c4e22015-10-23 10:51:11 -0700115 protected static final int TMAC_TABLE = 20;
116 protected static final int UNICAST_ROUTING_TABLE = 30;
117 protected static final int MULTICAST_ROUTING_TABLE = 40;
118 protected static final int MPLS_TABLE_0 = 23;
119 protected static final int MPLS_TABLE_1 = 24;
Pier Ventre140a8942016-11-02 07:26:38 -0700120 protected static final int MPLS_L3_TYPE_TABLE = 27;
121 protected static final int MPLS_TYPE_TABLE = 29;
Saurav Das822c4e22015-10-23 10:51:11 -0700122 protected static final int BRIDGING_TABLE = 50;
123 protected static final int ACL_TABLE = 60;
124 protected static final int MAC_LEARNING_TABLE = 254;
125 protected static final long OFPP_MAX = 0xffffff00L;
126
Saurav Das52025962016-01-28 22:30:01 -0800127 protected static final int HIGHEST_PRIORITY = 0xffff;
Saurav Das2857f382015-11-03 14:39:27 -0800128 protected static final int DEFAULT_PRIORITY = 0x8000;
Saurav Das822c4e22015-10-23 10:51:11 -0700129 protected static final int LOWEST_PRIORITY = 0x0;
130
Pier Ventre42287df2016-11-09 14:17:26 -0800131 protected static final int MPLS_L2_PORT_PRIORITY = 2;
Pier Ventre42287df2016-11-09 14:17:26 -0800132 protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
133 protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
134
Pier Ventre70d53ba2016-11-17 22:26:29 -0800135 protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
Pier Ventre70d53ba2016-11-17 22:26:29 -0800136 protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
137 protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
138
Saurav Das822c4e22015-10-23 10:51:11 -0700139 private final Logger log = getLogger(getClass());
Charles Chan425854b2016-04-11 15:32:12 -0700140 protected ServiceDirectory serviceDirectory;
Saurav Das822c4e22015-10-23 10:51:11 -0700141 protected FlowRuleService flowRuleService;
Charles Chan425854b2016-04-11 15:32:12 -0700142 protected CoreService coreService;
Saurav Das8a0732e2015-11-20 15:27:53 -0800143 protected GroupService groupService;
144 protected FlowObjectiveStore flowObjectiveStore;
Saurav Das822c4e22015-10-23 10:51:11 -0700145 protected DeviceId deviceId;
146 protected ApplicationId driverId;
Saurav Das822c4e22015-10-23 10:51:11 -0700147 protected DeviceService deviceService;
Charles Chan188ebf52015-12-23 00:15:11 -0800148 protected static KryoNamespace appKryo = new KryoNamespace.Builder()
Yi Tsengef19de12017-04-24 11:33:05 -0700149 .register(KryoNamespaces.API)
150 .register(GroupKey.class)
151 .register(DefaultGroupKey.class)
152 .register(OfdpaNextGroup.class)
153 .register(ArrayDeque.class)
154 .build("Ofdpa2Pipeline");
Saurav Das822c4e22015-10-23 10:51:11 -0700155
Charles Chan425854b2016-04-11 15:32:12 -0700156 protected Ofdpa2GroupHandler groupHandler;
Saurav Das822c4e22015-10-23 10:51:11 -0700157
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700158 // flows installations to be retried
Charles Chan50d900c2018-03-02 13:26:22 -0800159 private ScheduledExecutorService executorService
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700160 = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
Charles Chan50d900c2018-03-02 13:26:22 -0800161 private static final int MAX_RETRY_ATTEMPTS = 10;
162 private static final int RETRY_MS = 1000;
Saurav Das4f980082015-11-05 13:39:15 -0800163
Saurav Das822c4e22015-10-23 10:51:11 -0700164 @Override
165 public void init(DeviceId deviceId, PipelinerContext context) {
Saurav Das822c4e22015-10-23 10:51:11 -0700166 this.deviceId = deviceId;
167
Charles Chan425854b2016-04-11 15:32:12 -0700168 serviceDirectory = context.directory();
Saurav Das822c4e22015-10-23 10:51:11 -0700169 coreService = serviceDirectory.get(CoreService.class);
170 flowRuleService = serviceDirectory.get(FlowRuleService.class);
171 groupService = serviceDirectory.get(GroupService.class);
172 flowObjectiveStore = context.store();
Saurav Das822c4e22015-10-23 10:51:11 -0700173 deviceService = serviceDirectory.get(DeviceService.class);
Saurav Das822c4e22015-10-23 10:51:11 -0700174
Charles Chan40132b32017-01-22 00:19:37 -0800175 initDriverId();
176 initGroupHander(context);
Saurav Das822c4e22015-10-23 10:51:11 -0700177
Saurav Das822c4e22015-10-23 10:51:11 -0700178 initializePipeline();
Saurav Das822c4e22015-10-23 10:51:11 -0700179 }
180
Charles Chan40132b32017-01-22 00:19:37 -0800181 protected void initDriverId() {
182 driverId = coreService.registerApplication(
183 "org.onosproject.driver.Ofdpa2Pipeline");
184 }
185
186 protected void initGroupHander(PipelinerContext context) {
187 groupHandler = new Ofdpa2GroupHandler();
188 groupHandler.init(deviceId, context);
189 }
190
Saurav Das822c4e22015-10-23 10:51:11 -0700191 protected void initializePipeline() {
Charles Chan188ebf52015-12-23 00:15:11 -0800192 // OF-DPA does not require initializing the pipeline as it puts default
193 // rules automatically in the hardware. However emulation of OFDPA in
194 // software switches does require table-miss-entries.
Saurav Das822c4e22015-10-23 10:51:11 -0700195 }
196
Charles Chand1172632017-03-15 17:33:09 -0700197 /**
Alex Yashchuk4caa8e82017-12-08 17:40:05 +0200198 * Determines whether this pipeline requires MPLS POP instruction.
199 *
200 * @return true to use MPLS POP instruction
201 */
202 public boolean requireMplsPop() {
203 return true;
204 }
205
206 /**
207 * Determines whether this pipeline requires MPLS BOS match.
208 *
209 * @return true to use MPLS BOS match
210 */
211 public boolean requireMplsBosMatch() {
212 return true;
213 }
214
215 /**
216 * Determines whether this pipeline requires MPLS TTL decrement and copy.
217 *
218 * @return true to use MPLS TTL decrement and copy
219 */
220 public boolean requireMplsTtlModification() {
221 return true;
222 }
223
224 /**
Charles Chand1172632017-03-15 17:33:09 -0700225 * Determines whether this pipeline requires OFDPA match and set VLAN extensions.
226 *
227 * @return true to use the extensions
228 */
229 protected boolean requireVlanExtensions() {
230 return true;
231 }
232
Saurav Das86d13e82017-04-28 17:03:48 -0700233 /**
234 * Determines whether in-port should be matched on in TMAC table rules.
235 *
236 * @return true if match on in-port should be programmed
237 */
238 protected boolean matchInPortTmacTable() {
239 return true;
240 }
241
Charles Chand9e47c62017-10-05 15:17:15 -0700242 /**
243 * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table.
244 *
245 * @return true if matching L4 destination port on IPv6 packets is supported in ACL table.
246 */
247 protected boolean supportIpv6L4Dst() {
248 return true;
249 }
250
Saurav Dasc568c342018-01-25 09:49:01 -0800251 /**
252 * Determines whether this driver should continue to retry flows that point
253 * to empty groups. See CORD-554.
254 *
255 * @return true if the driver should retry flows
256 */
257 protected boolean shouldRetry() {
258 return true;
259 }
260
Saurav Das822c4e22015-10-23 10:51:11 -0700261 //////////////////////////////////////
262 // Flow Objectives
263 //////////////////////////////////////
264
265 @Override
266 public void filter(FilteringObjective filteringObjective) {
267 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
268 processFilter(filteringObjective,
269 filteringObjective.op() == Objective.Operation.ADD,
270 filteringObjective.appId());
271 } else {
272 // Note that packets that don't match the PERMIT filter are
273 // automatically denied. The DENY filter is used to deny packets
274 // that are otherwise permitted by the PERMIT filter.
275 // Use ACL table flow rules here for DENY filtering objectives
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530276 log.warn("filter objective other than PERMIT currently not supported");
Saurav Das822c4e22015-10-23 10:51:11 -0700277 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
278 }
279 }
280
281 @Override
282 public void forward(ForwardingObjective fwd) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700283 Collection<FlowRule> rules = processForward(fwd);
Saurav Das25190812016-05-27 13:54:07 -0700284 if (rules == null || rules.isEmpty()) {
285 // Assumes fail message has already been generated to the objective
286 // context. Returning here prevents spurious pass message to be
287 // generated by FlowRule service for empty flowOps.
288 return;
289 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700290 sendForward(fwd, rules);
291 }
292
Charles Chan50d900c2018-03-02 13:26:22 -0800293 private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700294 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
Saurav Das822c4e22015-10-23 10:51:11 -0700295 switch (fwd.op()) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700296 case ADD:
297 rules.stream()
298 .filter(Objects::nonNull)
299 .forEach(flowOpsBuilder::add);
Saurav Dasd2fded02016-12-02 15:43:47 -0800300 log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700301 break;
302 case REMOVE:
303 rules.stream()
304 .filter(Objects::nonNull)
305 .forEach(flowOpsBuilder::remove);
Pier Ventre42287df2016-11-09 14:17:26 -0800306 log.debug("Deleting a flow rule to sw:{}", deviceId);
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700307 break;
308 default:
309 fail(fwd, ObjectiveError.UNKNOWN);
310 log.warn("Unknown forwarding type {}", fwd.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700311 }
312
Saurav Das822c4e22015-10-23 10:51:11 -0700313 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
314 @Override
315 public void onSuccess(FlowRuleOperations ops) {
316 pass(fwd);
317 }
318
319 @Override
320 public void onError(FlowRuleOperations ops) {
321 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
322 }
323 }));
Saurav Das822c4e22015-10-23 10:51:11 -0700324 }
325
326 @Override
327 public void next(NextObjective nextObjective) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800328 NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id());
329 switch (nextObjective.op()) {
330 case ADD:
Saurav Das4f980082015-11-05 13:39:15 -0800331 if (nextGroup != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800332 log.warn("Cannot add next {} that already exists in device {}",
333 nextObjective.id(), deviceId);
334 return;
335 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700336 log.debug("Processing NextObjective id {} in dev {} - add group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800337 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700338 groupHandler.addGroup(nextObjective);
Saurav Das8a0732e2015-11-20 15:27:53 -0800339 break;
340 case ADD_TO_EXISTING:
341 if (nextGroup != null) {
Saurav Das1547b3f2017-05-05 17:01:08 -0700342 log.debug("Processing NextObjective id {} in dev {} - add bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800343 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700344 groupHandler.addBucketToGroup(nextObjective, nextGroup);
Saurav Das4f980082015-11-05 13:39:15 -0800345 } else {
Saurav Das8a0732e2015-11-20 15:27:53 -0800346 // it is possible that group-chain has not been fully created yet
Saurav Das423fe2b2015-12-04 10:52:59 -0800347 log.debug("Waiting to add bucket to group for next-id:{} in dev:{}",
348 nextObjective.id(), deviceId);
Yi Tseng47f82dc2017-03-05 22:48:39 -0800349
350 // by design multiple pending bucket is allowed for the group
351 groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> {
352 if (pendBkts == null) {
353 pendBkts = Sets.newHashSet();
354 }
355 pendBkts.add(nextObjective);
356 return pendBkts;
357 });
Saurav Das4f980082015-11-05 13:39:15 -0800358 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800359 break;
360 case REMOVE:
361 if (nextGroup == null) {
362 log.warn("Cannot remove next {} that does not exist in device {}",
363 nextObjective.id(), deviceId);
364 return;
365 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700366 log.debug("Processing NextObjective id {} in dev {} - remove group",
Saurav Das8a0732e2015-11-20 15:27:53 -0800367 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700368 groupHandler.removeGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800369 break;
370 case REMOVE_FROM_EXISTING:
371 if (nextGroup == null) {
372 log.warn("Cannot remove from next {} that does not exist in device {}",
373 nextObjective.id(), deviceId);
374 return;
375 }
Saurav Das1547b3f2017-05-05 17:01:08 -0700376 log.debug("Processing NextObjective id {} in dev {} - remove bucket",
Saurav Das8a0732e2015-11-20 15:27:53 -0800377 nextObjective.id(), deviceId);
Charles Chan425854b2016-04-11 15:32:12 -0700378 groupHandler.removeBucketFromGroup(nextObjective, nextGroup);
Saurav Das8a0732e2015-11-20 15:27:53 -0800379 break;
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900380 case MODIFY:
381 if (nextGroup == null) {
382 log.warn("Cannot modify next {} that does not exist in device {}",
383 nextObjective.id(), deviceId);
384 return;
385 }
386 log.debug("Processing NextObjective id {} in dev {} - modify bucket",
387 nextObjective.id(), deviceId);
388 groupHandler.modifyBucketFromGroup(nextObjective, nextGroup);
389 break;
Saurav Dasceccf242017-08-03 18:30:35 -0700390 case VERIFY:
391 if (nextGroup == null) {
392 log.warn("Cannot verify next {} that does not exist in device {}",
393 nextObjective.id(), deviceId);
394 return;
395 }
396 log.debug("Processing NextObjective id {} in dev {} - verify",
397 nextObjective.id(), deviceId);
398 groupHandler.verifyGroup(nextObjective, nextGroup);
399 break;
Saurav Das8a0732e2015-11-20 15:27:53 -0800400 default:
Saurav Das4f980082015-11-05 13:39:15 -0800401 log.warn("Unsupported operation {}", nextObjective.op());
Saurav Das822c4e22015-10-23 10:51:11 -0700402 }
403 }
404
405 //////////////////////////////////////
406 // Flow handling
407 //////////////////////////////////////
408
409 /**
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700410 * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing)
411 * configured on switch ports happen in different tables.
Saurav Das822c4e22015-10-23 10:51:11 -0700412 *
413 * @param filt the filtering objective
414 * @param install indicates whether to add or remove the objective
415 * @param applicationId the application that sent this objective
416 */
Saurav Das52025962016-01-28 22:30:01 -0800417 protected void processFilter(FilteringObjective filt,
418 boolean install, ApplicationId applicationId) {
Saurav Das822c4e22015-10-23 10:51:11 -0700419 // This driver only processes filtering criteria defined with switch
420 // ports as the key
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530421 PortCriterion portCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700422 EthCriterion ethCriterion = null;
423 VlanIdCriterion vidCriterion = null;
Saurav Das822c4e22015-10-23 10:51:11 -0700424 if (!filt.key().equals(Criteria.dummy()) &&
425 filt.key().type() == Criterion.Type.IN_PORT) {
426 portCriterion = (PortCriterion) filt.key();
Saurav Das822c4e22015-10-23 10:51:11 -0700427 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530428 if (portCriterion == null) {
429 log.debug("No IN_PORT defined in filtering objective from app: {}",
430 applicationId);
431 } else {
432 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
433 portCriterion.port());
434 }
Saurav Das822c4e22015-10-23 10:51:11 -0700435 // convert filtering conditions for switch-intfs into flowrules
436 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
437 for (Criterion criterion : filt.conditions()) {
Yi Tseng0fdc07e2017-04-24 19:17:08 -0700438 switch (criterion.type()) {
439 case ETH_DST:
440 case ETH_DST_MASKED:
441 ethCriterion = (EthCriterion) criterion;
442 break;
443 case VLAN_VID:
444 vidCriterion = (VlanIdCriterion) criterion;
445 break;
446 default:
447 log.warn("Unsupported filter {}", criterion);
448 fail(filt, ObjectiveError.UNSUPPORTED);
449 return;
Saurav Das822c4e22015-10-23 10:51:11 -0700450 }
451 }
452
Saurav Das0e99e2b2015-10-28 12:39:42 -0700453 VlanId assignedVlan = null;
Charles Chane849c192016-01-11 18:28:54 -0800454 if (vidCriterion != null) {
Charles Chand55e84d2016-03-30 17:54:24 -0700455 // Use the VLAN in criterion if metadata is not present and the traffic is tagged
Charles Chanc550f2e2017-12-19 19:55:57 -0800456 if (!vidCriterion.vlanId().equals(VlanId.NONE)) {
Charles Chane849c192016-01-11 18:28:54 -0800457 assignedVlan = vidCriterion.vlanId();
Charles Chanc550f2e2017-12-19 19:55:57 -0800458 } else if (filt.meta() != null) {
459 assignedVlan = readVlanFromTreatment(filt.meta());
Charles Chand55e84d2016-03-30 17:54:24 -0700460 }
Charles Chane849c192016-01-11 18:28:54 -0800461
Charles Chand55e84d2016-03-30 17:54:24 -0700462 if (assignedVlan == null) {
463 log.error("Driver fails to extract VLAN information. "
Frank Wangd8ab0962017-08-11 11:09:30 +0800464 + "Not processing VLAN filters on device {}.", deviceId);
Charles Chand55e84d2016-03-30 17:54:24 -0700465 log.debug("VLAN ID in criterion={}, metadata={}",
466 readVlanFromTreatment(filt.meta()), vidCriterion.vlanId());
467 fail(filt, ObjectiveError.BADPARAMS);
468 return;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700469 }
470 }
471
Yi Tseng3a77b4f2017-02-06 15:02:17 -0800472 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
Charles Chan985b12e2016-05-11 19:47:22 -0700473 // NOTE: it is possible that a filtering objective only has vidCriterion
Saurav Das961beb22017-03-29 19:09:17 -0700474 log.warn("filtering objective missing dstMac, cannot program TMAC table");
Saurav Das822c4e22015-10-23 10:51:11 -0700475 } else {
Charles Chan66291502018-03-02 16:43:28 -0800476 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion,
477 vidCriterion, assignedVlan, applicationId);
478 for (List<FlowRule> flowRules : allStages) {
479 log.trace("Starting a new flow rule stage");
480 ops.newStage();
Charles Chanc550f2e2017-12-19 19:55:57 -0800481
Charles Chan66291502018-03-02 16:43:28 -0800482 for (FlowRule flowRule : flowRules) {
483 log.trace("{} flow rules in TMAC table: {} for dev: {}",
484 (install) ? "adding" : "removing", flowRules, deviceId);
485
486 if (install) {
487 ops = ops.add(flowRule);
Charles Chanc550f2e2017-12-19 19:55:57 -0800488 } else {
Charles Chan66291502018-03-02 16:43:28 -0800489 // NOTE: Only remove TMAC flow when there is no more enabled port within the
490 // same VLAN on this device if TMAC doesn't support matching on in_port.
491 if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) {
492 ops = ops.remove(flowRule);
493 } else {
494 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
495 }
Charles Chanc550f2e2017-12-19 19:55:57 -0800496 }
497 }
Saurav Das822c4e22015-10-23 10:51:11 -0700498 }
499 }
500
Charles Chan985b12e2016-05-11 19:47:22 -0700501 if (vidCriterion == null) {
502 // NOTE: it is possible that a filtering objective only has ethCriterion
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530503 log.info("filtering objective missing VLAN, cannot program VLAN Table");
Saurav Das822c4e22015-10-23 10:51:11 -0700504 } else {
Charles Chan66291502018-03-02 16:43:28 -0800505 List<List<FlowRule>> allStages = processVlanIdFilter(
Charles Chan14967c22015-12-07 11:11:50 -0800506 portCriterion, vidCriterion, assignedVlan, applicationId);
Charles Chan66291502018-03-02 16:43:28 -0800507 for (List<FlowRule> flowRules : allStages) {
508 log.trace("Starting a new flow rule stage");
509 ops.newStage();
Charles Chan14967c22015-12-07 11:11:50 -0800510
Charles Chan66291502018-03-02 16:43:28 -0800511 for (FlowRule flowRule : flowRules) {
512 log.trace("{} flow rules in VLAN table: {} for dev: {}",
513 (install) ? "adding" : "removing", flowRule, deviceId);
514 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
Charles Chand1172632017-03-15 17:33:09 -0700515 }
Saurav Das822c4e22015-10-23 10:51:11 -0700516 }
517 }
518
Saurav Das822c4e22015-10-23 10:51:11 -0700519 // apply filtering flow rules
520 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
521 @Override
522 public void onSuccess(FlowRuleOperations ops) {
Saurav Das018605f2017-02-18 14:05:44 -0800523 log.debug("Applied {} filtering rules in device {}",
Saurav Das822c4e22015-10-23 10:51:11 -0700524 ops.stages().get(0).size(), deviceId);
525 pass(filt);
526 }
527
528 @Override
529 public void onError(FlowRuleOperations ops) {
530 log.info("Failed to apply all filtering rules in dev {}", deviceId);
531 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
532 }
533 }));
534
535 }
536
537 /**
Charles Chand1172632017-03-15 17:33:09 -0700538 * Internal implementation of processVlanIdFilter.
539 * <p>
540 * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12.
541 * Since it is non-OF spec, we need an extension treatment for that.
542 * The useVlanExtension must be set to false for OFDPA i12.
543 * </p>
Charles Chan66291502018-03-02 16:43:28 -0800544 * <p>
545 * NOTE: Separate VLAN filtering rules and assignment rules
546 * into different stages in order to guarantee that filtering rules
547 * always go first, as required by OFDPA.
548 * </p>
Saurav Das0e99e2b2015-10-28 12:39:42 -0700549 *
Charles Chanf9e98652016-09-07 16:54:23 -0700550 * @param portCriterion port on device for which this filter is programmed
551 * @param vidCriterion vlan assigned to port, or NONE for untagged
552 * @param assignedVlan assigned vlan-id for untagged packets
553 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800554 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700555 */
Charles Chan66291502018-03-02 16:43:28 -0800556 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700557 VlanIdCriterion vidCriterion,
558 VlanId assignedVlan,
559 ApplicationId applicationId) {
Charles Chan66291502018-03-02 16:43:28 -0800560 List<FlowRule> filteringRules = new ArrayList<>();
561 List<FlowRule> assignmentRules = new ArrayList<>();
Saurav Das822c4e22015-10-23 10:51:11 -0700562 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
563 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chan14967c22015-12-07 11:11:50 -0800564 TrafficSelector.Builder preSelector = null;
565 TrafficTreatment.Builder preTreatment = null;
566
Saurav Das4f980082015-11-05 13:39:15 -0800567 treatment.transition(TMAC_TABLE);
568
Saurav Das822c4e22015-10-23 10:51:11 -0700569 if (vidCriterion.vlanId() == VlanId.NONE) {
570 // untagged packets are assigned vlans
Charles Chand1172632017-03-15 17:33:09 -0700571 preSelector = DefaultTrafficSelector.builder();
572 if (requireVlanExtensions()) {
573 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE);
574 selector.extension(ofdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700575 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
576 treatment.extension(ofdpaSetVlanVid, deviceId);
Charles Chand1172632017-03-15 17:33:09 -0700577
578 OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
579 preSelector.extension(preOfdpaMatchVlanVid, deviceId);
Charles Chanf9e98652016-09-07 16:54:23 -0700580 } else {
Charles Chand1172632017-03-15 17:33:09 -0700581 selector.matchVlanId(VlanId.NONE);
Charles Chanf9e98652016-09-07 16:54:23 -0700582 treatment.setVlanId(assignedVlan);
Charles Chand1172632017-03-15 17:33:09 -0700583
584 preSelector.matchVlanId(assignedVlan);
585 }
586 preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE);
587 } else {
588 if (requireVlanExtensions()) {
589 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
590 selector.extension(ofdpaMatchVlanVid, deviceId);
591 } else {
592 selector.matchVlanId(vidCriterion.vlanId());
Charles Chanf9e98652016-09-07 16:54:23 -0700593 }
Charles Chan14967c22015-12-07 11:11:50 -0800594
Charles Chand55e84d2016-03-30 17:54:24 -0700595 if (!assignedVlan.equals(vidCriterion.vlanId())) {
Charles Chand1172632017-03-15 17:33:09 -0700596 if (requireVlanExtensions()) {
Charles Chanc03782d2017-01-31 13:57:55 -0800597 OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan);
598 treatment.extension(ofdpaSetVlanVid, deviceId);
599 } else {
600 treatment.setVlanId(assignedVlan);
601 }
Charles Chand55e84d2016-03-30 17:54:24 -0700602 }
Saurav Das822c4e22015-10-23 10:51:11 -0700603 }
Saurav Das822c4e22015-10-23 10:51:11 -0700604
605 // ofdpa cannot match on ALL portnumber, so we need to use separate
606 // rules for each port.
Charles Chan14967c22015-12-07 11:11:50 -0800607 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530608 if (portCriterion != null) {
609 if (PortNumber.ALL.equals(portCriterion.port())) {
610 for (Port port : deviceService.getPorts(deviceId)) {
611 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
612 portnums.add(port.number());
613 }
Saurav Das822c4e22015-10-23 10:51:11 -0700614 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530615 } else {
616 portnums.add(portCriterion.port());
Saurav Das822c4e22015-10-23 10:51:11 -0700617 }
618 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530619 log.warn("Filtering Objective missing Port Criterion . " +
Charles Chan66291502018-03-02 16:43:28 -0800620 "VLAN Table cannot be programmed for {}", deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700621 }
Saurav Das4f980082015-11-05 13:39:15 -0800622
Saurav Das822c4e22015-10-23 10:51:11 -0700623 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800624 // create rest of flowrule
Saurav Das822c4e22015-10-23 10:51:11 -0700625 selector.matchInPort(pnum);
626 FlowRule rule = DefaultFlowRule.builder()
627 .forDevice(deviceId)
628 .withSelector(selector.build())
629 .withTreatment(treatment.build())
630 .withPriority(DEFAULT_PRIORITY)
631 .fromApp(applicationId)
632 .makePermanent()
633 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800634 assignmentRules.add(rule);
Charles Chan14967c22015-12-07 11:11:50 -0800635
636 if (preSelector != null) {
637 preSelector.matchInPort(pnum);
638 FlowRule preRule = DefaultFlowRule.builder()
639 .forDevice(deviceId)
640 .withSelector(preSelector.build())
641 .withTreatment(preTreatment.build())
642 .withPriority(DEFAULT_PRIORITY)
643 .fromApp(applicationId)
644 .makePermanent()
645 .forTable(VLAN_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800646 filteringRules.add(preRule);
Charles Chan14967c22015-12-07 11:11:50 -0800647 }
Saurav Das822c4e22015-10-23 10:51:11 -0700648 }
Charles Chan66291502018-03-02 16:43:28 -0800649 return ImmutableList.of(assignmentRules, filteringRules);
Saurav Das822c4e22015-10-23 10:51:11 -0700650 }
651
652 /**
653 * Allows routed packets with correct destination MAC to be directed
654 * to unicast-IP routing table or MPLS forwarding table.
Saurav Das822c4e22015-10-23 10:51:11 -0700655 *
656 * @param portCriterion port on device for which this filter is programmed
657 * @param ethCriterion dstMac of device for which is filter is programmed
658 * @param vidCriterion vlan assigned to port, or NONE for untagged
Saurav Das0e99e2b2015-10-28 12:39:42 -0700659 * @param assignedVlan assigned vlan-id for untagged packets
Saurav Das822c4e22015-10-23 10:51:11 -0700660 * @param applicationId for application programming this filter
Charles Chan66291502018-03-02 16:43:28 -0800661 * @return stages of flow rules for port-vlan filters
Saurav Das822c4e22015-10-23 10:51:11 -0700662
663 */
Charles Chan66291502018-03-02 16:43:28 -0800664 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das822c4e22015-10-23 10:51:11 -0700665 EthCriterion ethCriterion,
666 VlanIdCriterion vidCriterion,
Saurav Das0e99e2b2015-10-28 12:39:42 -0700667 VlanId assignedVlan,
Saurav Das822c4e22015-10-23 10:51:11 -0700668 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800669 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
Sivachidambaram Subramanianc598d062017-05-16 22:42:06 +0530670 if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) {
Charles Chan5270ed02016-01-30 23:22:37 -0800671 return processEthDstOnlyFilter(ethCriterion, applicationId);
672 }
673
Charles Chan5b9df8d2016-03-28 22:21:40 -0700674 // Multicast MAC
675 if (ethCriterion.mask() != null) {
Charles Chanb4879a52017-10-20 19:09:16 -0700676 return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700677 }
678
Saurav Das822c4e22015-10-23 10:51:11 -0700679 //handling untagged packets via assigned VLAN
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530680 if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700681 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
Saurav Das822c4e22015-10-23 10:51:11 -0700682 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530683 List<FlowRule> rules = new ArrayList<>();
684 OfdpaMatchVlanVid ofdpaMatchVlanVid = null;
685 if (vidCriterion != null && requireVlanExtensions()) {
686 ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId());
687 }
Saurav Das822c4e22015-10-23 10:51:11 -0700688 // ofdpa cannot match on ALL portnumber, so we need to use separate
689 // rules for each port.
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -0700690 List<PortNumber> portnums = new ArrayList<>();
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530691 if (portCriterion != null) {
692 if (PortNumber.ALL.equals(portCriterion.port())) {
693 for (Port port : deviceService.getPorts(deviceId)) {
694 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
695 portnums.add(port.number());
696 }
Saurav Das822c4e22015-10-23 10:51:11 -0700697 }
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530698 } else {
699 portnums.add(portCriterion.port());
700 }
701 for (PortNumber pnum : portnums) {
702 rules.add(buildTmacRuleForIpv4(ethCriterion,
703 vidCriterion,
704 ofdpaMatchVlanVid,
705 applicationId,
706 pnum));
707 rules.add(buildTmacRuleForMpls(ethCriterion,
708 vidCriterion,
709 ofdpaMatchVlanVid,
710 applicationId,
711 pnum));
712 rules.add(buildTmacRuleForIpv6(ethCriterion,
713 vidCriterion,
714 ofdpaMatchVlanVid,
715 applicationId,
716 pnum));
Saurav Das822c4e22015-10-23 10:51:11 -0700717 }
718 } else {
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530719 rules.add(buildTmacRuleForIpv4(ethCriterion,
720 vidCriterion,
721 ofdpaMatchVlanVid,
722 applicationId,
723 null));
724 rules.add(buildTmacRuleForMpls(ethCriterion,
725 vidCriterion,
726 ofdpaMatchVlanVid,
727 applicationId,
728 null));
729 rules.add(buildTmacRuleForIpv6(ethCriterion,
730 vidCriterion,
731 ofdpaMatchVlanVid,
732 applicationId,
733 null));
Saurav Das822c4e22015-10-23 10:51:11 -0700734 }
Charles Chan66291502018-03-02 16:43:28 -0800735 return ImmutableList.of(rules);
Saurav Das822c4e22015-10-23 10:51:11 -0700736 }
737
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530738 /**
739 * Builds TMAC rules for IPv4 packets.
740 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800741 * @param ethCriterion dst mac matching
742 * @param vidCriterion vlan id assigned to the port
743 * @param ofdpaMatchVlanVid OFDPA vlan id matching
744 * @param applicationId application id
745 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530746 * @return TMAC rule for IPV4 packets
747 */
748 private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion,
749 VlanIdCriterion vidCriterion,
750 OfdpaMatchVlanVid ofdpaMatchVlanVid,
751 ApplicationId applicationId,
752 PortNumber pnum) {
753 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
754 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
755 if (pnum != null) {
756 if (matchInPortTmacTable()) {
757 selector.matchInPort(pnum);
758 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800759 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530760 "ignoring the IN_PORT criteria");
761 }
762 }
763 if (vidCriterion != null) {
764 if (requireVlanExtensions()) {
765 selector.extension(ofdpaMatchVlanVid, deviceId);
766 } else {
767 selector.matchVlanId(vidCriterion.vlanId());
768 }
769 }
770 selector.matchEthType(Ethernet.TYPE_IPV4);
771 selector.matchEthDst(ethCriterion.mac());
772 treatment.transition(UNICAST_ROUTING_TABLE);
773 return DefaultFlowRule.builder()
774 .forDevice(deviceId)
775 .withSelector(selector.build())
776 .withTreatment(treatment.build())
777 .withPriority(DEFAULT_PRIORITY)
778 .fromApp(applicationId)
779 .makePermanent()
780 .forTable(TMAC_TABLE).build();
781 }
782
783 /**
784 * Builds TMAC rule for MPLS packets.
785 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800786 * @param ethCriterion dst mac matching
787 * @param vidCriterion vlan id assigned to the port
788 * @param ofdpaMatchVlanVid OFDPA vlan id matching
789 * @param applicationId application id
790 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530791 * @return TMAC rule for MPLS packets
792 */
793 private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion,
794 VlanIdCriterion vidCriterion,
795 OfdpaMatchVlanVid ofdpaMatchVlanVid,
796 ApplicationId applicationId,
797 PortNumber pnum) {
798 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
799 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
800 if (pnum != null) {
801 if (matchInPortTmacTable()) {
802 selector.matchInPort(pnum);
803 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800804 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530805 "ignoring the IN_PORT criteria");
806 }
807 }
808 if (vidCriterion != null) {
809 if (requireVlanExtensions()) {
810 selector.extension(ofdpaMatchVlanVid, deviceId);
811 } else {
812 selector.matchVlanId(vidCriterion.vlanId());
813 }
814 }
815 selector.matchEthType(Ethernet.MPLS_UNICAST);
816 selector.matchEthDst(ethCriterion.mac());
817 treatment.transition(MPLS_TABLE_0);
818 return DefaultFlowRule.builder()
819 .forDevice(deviceId)
820 .withSelector(selector.build())
821 .withTreatment(treatment.build())
822 .withPriority(DEFAULT_PRIORITY)
823 .fromApp(applicationId)
824 .makePermanent()
825 .forTable(TMAC_TABLE).build();
826 }
827
828 /**
829 * Builds TMAC rules for IPv6 packets.
830 *
Charles Chanc550f2e2017-12-19 19:55:57 -0800831 * @param ethCriterion dst mac matching
832 * @param vidCriterion vlan id assigned to the port
833 * @param ofdpaMatchVlanVid OFDPA vlan id matching
834 * @param applicationId application id
835 * @param pnum port number
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530836 * @return TMAC rule for IPV6 packets
837 */
838 private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion,
839 VlanIdCriterion vidCriterion,
840 OfdpaMatchVlanVid ofdpaMatchVlanVid,
841 ApplicationId applicationId,
842 PortNumber pnum) {
843 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
844 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
845 if (pnum != null) {
846 if (matchInPortTmacTable()) {
847 selector.matchInPort(pnum);
848 } else {
Saurav Dasf14d9ef2017-12-05 15:00:23 -0800849 log.debug("Pipeline does not support IN_PORT matching in TMAC table, " +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530850 "ignoring the IN_PORT criteria");
851 }
852 }
853 if (vidCriterion != null) {
854 if (requireVlanExtensions()) {
855 selector.extension(ofdpaMatchVlanVid, deviceId);
856 } else {
857 selector.matchVlanId(vidCriterion.vlanId());
858 }
859 }
860 selector.matchEthType(Ethernet.TYPE_IPV6);
861 selector.matchEthDst(ethCriterion.mac());
862 treatment.transition(UNICAST_ROUTING_TABLE);
863 return DefaultFlowRule.builder()
864 .forDevice(deviceId)
865 .withSelector(selector.build())
866 .withTreatment(treatment.build())
867 .withPriority(DEFAULT_PRIORITY)
868 .fromApp(applicationId)
869 .makePermanent()
870 .forTable(TMAC_TABLE).build();
871 }
872
Charles Chan66291502018-03-02 16:43:28 -0800873 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700874 ApplicationId applicationId) {
Pier Luigi0e358632017-01-31 09:35:05 -0800875 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
876
Charles Chan5270ed02016-01-30 23:22:37 -0800877 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
878 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
879 selector.matchEthType(Ethernet.TYPE_IPV4);
880 selector.matchEthDst(ethCriterion.mac());
881 treatment.transition(UNICAST_ROUTING_TABLE);
882 FlowRule rule = DefaultFlowRule.builder()
883 .forDevice(deviceId)
884 .withSelector(selector.build())
885 .withTreatment(treatment.build())
886 .withPriority(DEFAULT_PRIORITY)
887 .fromApp(applicationId)
888 .makePermanent()
889 .forTable(TMAC_TABLE).build();
Pier Luigi0e358632017-01-31 09:35:05 -0800890 builder.add(rule);
891
892 selector = DefaultTrafficSelector.builder();
893 treatment = DefaultTrafficTreatment.builder();
894 selector.matchEthType(Ethernet.TYPE_IPV6);
895 selector.matchEthDst(ethCriterion.mac());
896 treatment.transition(UNICAST_ROUTING_TABLE);
897 rule = DefaultFlowRule.builder()
898 .forDevice(deviceId)
899 .withSelector(selector.build())
900 .withTreatment(treatment.build())
901 .withPriority(DEFAULT_PRIORITY)
902 .fromApp(applicationId)
903 .makePermanent()
904 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800905 return ImmutableList.of(builder.add(rule).build());
Charles Chan5270ed02016-01-30 23:22:37 -0800906 }
907
Charles Chan66291502018-03-02 16:43:28 -0800908 List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion,
Charles Chanb4879a52017-10-20 19:09:16 -0700909 VlanId assignedVlan,
Yi Tsengef19de12017-04-24 11:33:05 -0700910 ApplicationId applicationId) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000911 ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700912 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
913 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Charles Chanb4879a52017-10-20 19:09:16 -0700914 FlowRule rule;
Julia Ferguson65428c32017-08-10 18:15:24 +0000915
Charles Chanb4879a52017-10-20 19:09:16 -0700916 if (IPV4_MULTICAST.equals(ethCriterion.mac())) {
917 selector.matchEthType(Ethernet.TYPE_IPV4);
918 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
919 selector.matchVlanId(assignedVlan);
920 treatment.transition(MULTICAST_ROUTING_TABLE);
921 rule = DefaultFlowRule.builder()
922 .forDevice(deviceId)
923 .withSelector(selector.build())
924 .withTreatment(treatment.build())
925 .withPriority(DEFAULT_PRIORITY)
926 .fromApp(applicationId)
927 .makePermanent()
928 .forTable(TMAC_TABLE).build();
929 builder.add(rule);
930 }
931
932 if (IPV6_MULTICAST.equals(ethCriterion.mac())) {
933 selector = DefaultTrafficSelector.builder();
934 treatment = DefaultTrafficTreatment.builder();
935 selector.matchEthType(Ethernet.TYPE_IPV6);
936 selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask());
937 selector.matchVlanId(assignedVlan);
938 treatment.transition(MULTICAST_ROUTING_TABLE);
939 rule = DefaultFlowRule.builder()
940 .forDevice(deviceId)
941 .withSelector(selector.build())
942 .withTreatment(treatment.build())
943 .withPriority(DEFAULT_PRIORITY)
944 .fromApp(applicationId)
945 .makePermanent()
946 .forTable(TMAC_TABLE).build();
947 builder.add(rule);
948 }
Charles Chan66291502018-03-02 16:43:28 -0800949 return ImmutableList.of(builder.build());
Charles Chan5b9df8d2016-03-28 22:21:40 -0700950 }
951
Saurav Das822c4e22015-10-23 10:51:11 -0700952 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
953 switch (fwd.flag()) {
954 case SPECIFIC:
955 return processSpecific(fwd);
956 case VERSATILE:
957 return processVersatile(fwd);
958 default:
959 fail(fwd, ObjectiveError.UNKNOWN);
960 log.warn("Unknown forwarding flag {}", fwd.flag());
961 }
962 return Collections.emptySet();
963 }
964
965 /**
966 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
967 * ACL table.
968 * @param fwd the forwarding objective of type 'versatile'
969 * @return a collection of flow rules to be sent to the switch. An empty
970 * collection may be returned if there is a problem in processing
971 * the flow rule
972 */
Saurav Das52025962016-01-28 22:30:01 -0800973 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das018605f2017-02-18 14:05:44 -0800974 log.debug("Processing versatile forwarding objective:{} in dev:{}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800975 fwd.id(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -0700976
977 EthTypeCriterion ethType =
Saurav Das77b5e902016-01-27 17:01:59 -0800978 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das822c4e22015-10-23 10:51:11 -0700979 if (ethType == null) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800980 log.error("Versatile forwarding objective:{} must include ethType",
981 fwd.id());
Saurav Das822c4e22015-10-23 10:51:11 -0700982 fail(fwd, ObjectiveError.BADPARAMS);
983 return Collections.emptySet();
984 }
985 if (fwd.nextId() == null && fwd.treatment() == null) {
986 log.error("Forwarding objective {} from {} must contain "
987 + "nextId or Treatment", fwd.selector(), fwd.appId());
Zsolt Haraszti9faab752016-02-17 15:55:20 -0800988 fail(fwd, ObjectiveError.BADPARAMS);
Saurav Das822c4e22015-10-23 10:51:11 -0700989 return Collections.emptySet();
990 }
Saurav Das49cb5a12016-01-16 22:54:07 -0800991
Saurav Das77b5e902016-01-27 17:01:59 -0800992 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
993 fwd.selector().criteria().forEach(criterion -> {
994 if (criterion instanceof VlanIdCriterion) {
995 VlanId vlanId = ((VlanIdCriterion) criterion).vlanId();
996 // ensure that match does not include vlan = NONE as OF-DPA does not
997 // match untagged packets this way in the ACL table.
998 if (vlanId.equals(VlanId.NONE)) {
999 return;
1000 }
Charles Chand1172632017-03-15 17:33:09 -07001001 if (requireVlanExtensions()) {
1002 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId);
1003 sbuilder.extension(ofdpaMatchVlanVid, deviceId);
1004 } else {
1005 sbuilder.matchVlanId(vlanId);
1006 }
Charles Chan09bf2692018-01-11 11:48:18 -08001007 } else if (criterion instanceof Icmpv6TypeCriterion) {
1008 byte icmpv6Type = (byte) ((Icmpv6TypeCriterion) criterion).icmpv6Type();
1009 sbuilder.matchIcmpv6Type(icmpv6Type);
1010 } else if (criterion instanceof Icmpv6CodeCriterion) {
1011 byte icmpv6Code = (byte) ((Icmpv6CodeCriterion) criterion).icmpv6Code();
1012 sbuilder.matchIcmpv6Type(icmpv6Code);
Charles Chand9e47c62017-10-05 15:17:15 -07001013 } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) {
1014 // FIXME: QMX switches do not support L4 dst port matching in ACL table.
1015 // Currently L4 dst port matching is only used by DHCP relay feature
1016 // and therefore is safe to be replaced with L4 src port matching.
1017 // We need to revisit this if L4 dst port is used for other purpose in the future.
1018 if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) {
Charles Chand9e47c62017-10-05 15:17:15 -07001019 switch (criterion.type()) {
1020 case UDP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001021 case UDP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001022 case TCP_DST:
Charles Chand9e47c62017-10-05 15:17:15 -07001023 case TCP_DST_MASKED:
Charles Chand9e47c62017-10-05 15:17:15 -07001024 break;
1025 default:
1026 sbuilder.add(criterion);
1027 }
1028 } else {
1029 sbuilder.add(criterion);
1030 }
Saurav Das77b5e902016-01-27 17:01:59 -08001031 } else {
1032 sbuilder.add(criterion);
1033 }
1034 });
1035
Saurav Das822c4e22015-10-23 10:51:11 -07001036 // XXX driver does not currently do type checking as per Tables 65-67 in
1037 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
Saurav Das49cb5a12016-01-16 22:54:07 -08001038 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1039 if (fwd.treatment() != null) {
1040 for (Instruction ins : fwd.treatment().allInstructions()) {
1041 if (ins instanceof OutputInstruction) {
1042 OutputInstruction o = (OutputInstruction) ins;
Ray Milkeyfe0e0852018-01-18 11:14:05 -08001043 if (PortNumber.CONTROLLER.equals(o.port())) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001044 ttBuilder.add(o);
1045 } else {
1046 log.warn("Only allowed treatments in versatile forwarding "
1047 + "objectives are punts to the controller");
1048 }
1049 } else {
1050 log.warn("Cannot process instruction in versatile fwd {}", ins);
1051 }
Saurav Das822c4e22015-10-23 10:51:11 -07001052 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001053 if (fwd.treatment().clearedDeferred()) {
1054 ttBuilder.wipeDeferred();
1055 }
Saurav Das822c4e22015-10-23 10:51:11 -07001056 }
Saurav Das822c4e22015-10-23 10:51:11 -07001057 if (fwd.nextId() != null) {
Saurav Das49cb5a12016-01-16 22:54:07 -08001058 // overide case
1059 NextGroup next = getGroupForNextObjective(fwd.nextId());
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +05301060 if (next == null) {
1061 fail(fwd, ObjectiveError.BADPARAMS);
1062 return Collections.emptySet();
1063 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001064 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1065 // we only need the top level group's key to point the flow to it
1066 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1067 if (group == null) {
1068 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1069 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1070 fail(fwd, ObjectiveError.GROUPMISSING);
1071 return Collections.emptySet();
1072 }
1073 ttBuilder.deferred().group(group.id());
Saurav Das822c4e22015-10-23 10:51:11 -07001074 }
Saurav Das49cb5a12016-01-16 22:54:07 -08001075
1076 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1077 .fromApp(fwd.appId())
1078 .withPriority(fwd.priority())
1079 .forDevice(deviceId)
Saurav Das77b5e902016-01-27 17:01:59 -08001080 .withSelector(sbuilder.build())
Saurav Das49cb5a12016-01-16 22:54:07 -08001081 .withTreatment(ttBuilder.build())
1082 .makePermanent()
1083 .forTable(ACL_TABLE);
1084 return Collections.singletonList(ruleBuilder.build());
Saurav Das822c4e22015-10-23 10:51:11 -07001085 }
1086
1087 /**
1088 * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table
Saurav Das8a0732e2015-11-20 15:27:53 -08001089 * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table.
Saurav Das822c4e22015-10-23 10:51:11 -07001090 *
1091 * @param fwd the forwarding objective of type 'specific'
1092 * @return a collection of flow rules. Typically there will be only one
1093 * for this type of forwarding objective. An empty set may be
1094 * returned if there is an issue in processing the objective.
1095 */
Charles Chan50d900c2018-03-02 13:26:22 -08001096 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das25190812016-05-27 13:54:07 -07001097 log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
Saurav Das4ce45962015-11-24 23:21:05 -08001098 fwd.id(), deviceId, fwd.nextId());
1099 boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
1100 boolean isEthDstObj = isSupportedEthDstObjective(fwd);
1101
1102 if (isEthTypeObj) {
1103 return processEthTypeSpecific(fwd);
1104 } else if (isEthDstObj) {
1105 return processEthDstSpecific(fwd);
1106 } else {
1107 log.warn("processSpecific: Unsupported forwarding objective "
1108 + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001109 fail(fwd, ObjectiveError.UNSUPPORTED);
1110 return Collections.emptySet();
1111 }
Saurav Das4ce45962015-11-24 23:21:05 -08001112 }
1113
Saurav Das4ce45962015-11-24 23:21:05 -08001114 /**
1115 * Handles forwarding rules to the IP and MPLS tables.
1116 *
1117 * @param fwd the forwarding objective
1118 * @return A collection of flow rules, or an empty set
1119 */
1120 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Charles Chancad338a2016-09-16 18:03:11 -07001121 return processEthTypeSpecificInternal(fwd, false, ACL_TABLE);
Charles Chanf9e98652016-09-07 16:54:23 -07001122 }
1123
1124 /**
1125 * Internal implementation of processEthTypeSpecific.
1126 * <p>
1127 * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break
1128 * the rule into 0.0.0.0/1 and 128.0.0.0/1.
1129 * The allowDefaultRoute must be set to false for OFDPA i12.
1130 * </p>
1131 *
1132 * @param fwd the forwarding objective
1133 * @param allowDefaultRoute allow wildcarded IPv4_DST or not
Ray Milkey0bb1e102016-11-10 14:51:27 -08001134 * @param mplsNextTable next MPLS table
Charles Chanf9e98652016-09-07 16:54:23 -07001135 * @return A collection of flow rules, or an empty set
1136 */
1137 protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd,
Charles Chancad338a2016-09-16 18:03:11 -07001138 boolean allowDefaultRoute,
1139 int mplsNextTable) {
Saurav Das4ce45962015-11-24 23:21:05 -08001140 TrafficSelector selector = fwd.selector();
1141 EthTypeCriterion ethType =
1142 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001143 boolean emptyGroup = false;
Charles Chan188ebf52015-12-23 00:15:11 -08001144 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -08001145 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001146 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -08001147 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
Charles Chan14967c22015-12-07 11:11:50 -08001148
Saurav Das8a0732e2015-11-20 15:27:53 -08001149 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001150 if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) {
1151 return Collections.emptyList();
1152 }
1153 // We need to set properly the next table
Flavio Castroe10fa242016-01-15 12:43:51 -08001154 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -07001155 if (ipv4Dst.isMulticast()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001156 forTableId = MULTICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001157 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -07001158 forTableId = UNICAST_ROUTING_TABLE;
Flavio Castroe10fa242016-01-15 12:43:51 -08001159 }
Charles Chan50d900c2018-03-02 13:26:22 -08001160
1161 /*
1162 // XXX decrementing IP ttl is done automatically for routing, this
1163 // action is ignored or rejected in ofdpa as it is not fully implemented
Charles Chan14967c22015-12-07 11:11:50 -08001164 if (fwd.treatment() != null) {
1165 for (Instruction instr : fwd.treatment().allInstructions()) {
1166 if (instr instanceof L3ModificationInstruction &&
1167 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
Charles Chan50d900c2018-03-02 13:26:22 -08001168 tb.deferred().add(instr);
Charles Chan14967c22015-12-07 11:11:50 -08001169 }
1170 }
1171 }
Charles Chan50d900c2018-03-02 13:26:22 -08001172 */
Pier Ventree0ae7a32016-11-23 09:57:42 -08001173 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
1174 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
1175 return Collections.emptyList();
1176 }
Kalhee Kim3d26bf02017-10-04 14:59:11 +00001177 //We need to set the proper next table
1178 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1179 if (ipv6Dst.isMulticast()) {
1180 forTableId = MULTICAST_ROUTING_TABLE;
1181 } else {
1182 forTableId = UNICAST_ROUTING_TABLE;
1183 }
1184
Charles Chan50d900c2018-03-02 13:26:22 -08001185 // XXX decrementing IP ttl is done automatically for routing, this
1186 // action is ignored or rejected in ofdpa as it is not fully implemented
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) {
Charles Chan50d900c2018-03-02 13:26:22 -08001192 tb.deferred().add(instr);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001193 }
1194 }
1195 }
Charles Chan50d900c2018-03-02 13:26:22 -08001196 */
Saurav Das8a0732e2015-11-20 15:27:53 -08001197 } else {
1198 filteredSelector
1199 .matchEthType(Ethernet.MPLS_UNICAST)
1200 .matchMplsLabel(((MplsCriterion)
1201 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
1202 MplsBosCriterion bos = (MplsBosCriterion) selector
Pier Ventre140a8942016-11-02 07:26:38 -07001203 .getCriterion(MPLS_BOS);
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001204 if (bos != null && requireMplsBosMatch()) {
Saurav Das8a0732e2015-11-20 15:27:53 -08001205 filteredSelector.matchMplsBos(bos.mplsBos());
1206 }
1207 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -08001208 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
1209 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das822c4e22015-10-23 10:51:11 -07001210
Charles Chan14967c22015-12-07 11:11:50 -08001211 if (fwd.treatment() != null) {
1212 for (Instruction instr : fwd.treatment().allInstructions()) {
1213 if (instr instanceof L2ModificationInstruction &&
1214 ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) {
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001215 // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires
Saurav Das9c705342017-01-06 11:36:01 -08001216 // setting the MPLS_TYPE so pop can happen down the pipeline
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001217 if (requireMplsPop()) {
1218 if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) {
1219 tb.immediate().popMpls();
1220 }
1221 } else {
1222 // Skip mpls pop action for mpls_unicast label
1223 if (instr instanceof ModMplsHeaderInstruction &&
1224 !((ModMplsHeaderInstruction) instr).ethernetType()
1225 .equals(EtherType.MPLS_UNICAST.ethType())) {
1226 tb.immediate().add(instr);
1227 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001228 }
Charles Chan14967c22015-12-07 11:11:50 -08001229 }
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001230
1231 if (requireMplsTtlModification()) {
1232 if (instr instanceof L3ModificationInstruction &&
1233 ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
1234 // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction
1235 tb.immediate().decMplsTtl();
1236 }
1237 if (instr instanceof L3ModificationInstruction &&
1238 ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) {
1239 tb.immediate().add(instr);
1240 }
Charles Chan14967c22015-12-07 11:11:50 -08001241 }
Saurav Das8a0732e2015-11-20 15:27:53 -08001242 }
1243 }
1244 }
Saurav Das822c4e22015-10-23 10:51:11 -07001245
1246 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001247 NextGroup next = getGroupForNextObjective(fwd.nextId());
1248 if (next != null) {
1249 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1250 // we only need the top level group's key to point the flow to it
1251 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1252 if (group == null) {
1253 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1254 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1255 fail(fwd, ObjectiveError.GROUPMISSING);
1256 return Collections.emptySet();
1257 }
Saurav Dasa4020382018-02-14 14:14:54 -08001258 if (isNotMplsBos(selector) && group.type().equals(SELECT)) {
1259 log.warn("SR CONTINUE case cannot be handled as MPLS ECMP "
1260 + "is not implemented in OF-DPA yet. Aborting flow {} -> next:{} "
1261 + "in this device {}", fwd.id(), fwd.nextId(), deviceId);
1262 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1263 return Collections.emptySet();
1264 }
Saurav Das423fe2b2015-12-04 10:52:59 -08001265 tb.deferred().group(group.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001266 // retrying flows may be necessary due to bug CORD-554
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001267 if (gkeys.size() == 1 && gkeys.get(0).size() == 1) {
Saurav Dasc568c342018-01-25 09:49:01 -08001268 if (shouldRetry()) {
1269 log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}",
1270 Integer.toHexString(group.id().id()), deviceId,
1271 fwd.id());
1272 emptyGroup = true;
1273 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001274 }
Saurav Das25190812016-05-27 13:54:07 -07001275 } else {
1276 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
1277 fwd.nextId(), deviceId, fwd.id());
1278 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
1279 return Collections.emptySet();
Saurav Das822c4e22015-10-23 10:51:11 -07001280 }
Saurav Das822c4e22015-10-23 10:51:11 -07001281 }
Charles Chancad338a2016-09-16 18:03:11 -07001282
1283 if (forTableId == MPLS_TABLE_1) {
Pier Ventre140a8942016-11-02 07:26:38 -07001284 if (mplsNextTable == MPLS_L3_TYPE_TABLE) {
Charles Chancad338a2016-09-16 18:03:11 -07001285 Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP);
Saurav Das9c705342017-01-06 11:36:01 -08001286 // set mpls type as apply_action
1287 tb.immediate().extension(setMplsType, deviceId);
Charles Chancad338a2016-09-16 18:03:11 -07001288 }
1289 tb.transition(mplsNextTable);
1290 } else {
1291 tb.transition(ACL_TABLE);
1292 }
1293
Saurav Das822c4e22015-10-23 10:51:11 -07001294 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1295 .fromApp(fwd.appId())
1296 .withPriority(fwd.priority())
1297 .forDevice(deviceId)
Saurav Das8a0732e2015-11-20 15:27:53 -08001298 .withSelector(filteredSelector.build())
1299 .withTreatment(tb.build())
1300 .forTable(forTableId);
Saurav Das822c4e22015-10-23 10:51:11 -07001301
1302 if (fwd.permanent()) {
1303 ruleBuilder.makePermanent();
1304 } else {
1305 ruleBuilder.makeTemporary(fwd.timeout());
1306 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001307 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1308 flowRuleCollection.add(ruleBuilder.build());
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001309 if (!allowDefaultRoute) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001310 flowRuleCollection.add(
1311 defaultRoute(fwd, complementarySelector, forTableId, tb)
1312 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001313 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1314 }
Saurav Dasc568c342018-01-25 09:49:01 -08001315
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001316 if (emptyGroup) {
1317 executorService.schedule(new RetryFlows(fwd, flowRuleCollection),
1318 RETRY_MS, TimeUnit.MILLISECONDS);
1319 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001320 return flowRuleCollection;
Saurav Das822c4e22015-10-23 10:51:11 -07001321 }
1322
Charles Chan50d900c2018-03-02 13:26:22 -08001323 private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001324 TrafficSelector.Builder extBuilder,
1325 ForwardingObjective fwd,
1326 boolean allowDefaultRoute) {
1327 TrafficSelector selector = fwd.selector();
1328
1329 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
1330 if (ipv4Dst.isMulticast()) {
1331 if (ipv4Dst.prefixLength() != 32) {
1332 log.warn("Multicast specific forwarding objective can only be /32");
1333 fail(fwd, ObjectiveError.BADPARAMS);
1334 return -1;
1335 }
1336 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1337 if (assignedVlan == null) {
1338 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1339 fail(fwd, ObjectiveError.BADPARAMS);
1340 return -1;
1341 }
Charles Chand1172632017-03-15 17:33:09 -07001342 if (requireVlanExtensions()) {
1343 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1344 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1345 } else {
1346 builderToUpdate.matchVlanId(assignedVlan);
1347 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001348 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1349 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
1350 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1351 } else {
1352 if (ipv4Dst.prefixLength() == 0) {
1353 if (allowDefaultRoute) {
1354 // The entire IPV4_DST field is wildcarded intentionally
1355 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4);
1356 } else {
1357 // NOTE: The switch does not support matching 0.0.0.0/0
1358 // Split it into 0.0.0.0/1 and 128.0.0.0/1
1359 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4)
1360 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
1361 extBuilder.matchEthType(Ethernet.TYPE_IPV4)
1362 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
1363 }
1364 } else {
1365 builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
1366 }
1367 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
1368 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1369 }
1370 return 0;
1371 }
1372
1373 /**
1374 * Helper method to build Ipv6 selector using the selector provided by
1375 * a forwarding objective.
1376 *
1377 * @param builderToUpdate the builder to update
1378 * @param fwd the selector to read
1379 * @return 0 if the update ends correctly. -1 if the matches
1380 * are not yet supported
1381 */
Charles Chan50d900c2018-03-02 13:26:22 -08001382 int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001383 ForwardingObjective fwd) {
1384
1385 TrafficSelector selector = fwd.selector();
1386
1387 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
1388 if (ipv6Dst.isMulticast()) {
Julia Ferguson65428c32017-08-10 18:15:24 +00001389 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
1390 log.warn("Multicast specific forwarding objective can only be /128");
1391 fail(fwd, ObjectiveError.BADPARAMS);
1392 return -1;
1393 }
1394 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
1395 if (assignedVlan == null) {
1396 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
1397 fail(fwd, ObjectiveError.BADPARAMS);
1398 return -1;
1399 }
1400 if (requireVlanExtensions()) {
1401 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan);
1402 builderToUpdate.extension(ofdpaMatchVlanVid, deviceId);
1403 } else {
1404 builderToUpdate.matchVlanId(assignedVlan);
1405 }
1406 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
1407 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
1408 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
1409 } else {
1410 if (ipv6Dst.prefixLength() != 0) {
1411 builderToUpdate.matchIPv6Dst(ipv6Dst);
1412 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001413 builderToUpdate.matchEthType(Ethernet.TYPE_IPV6);
1414 log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}"
1415 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Julia Ferguson65428c32017-08-10 18:15:24 +00001416 }
Pier Luigi3bfe32c2017-01-30 09:47:36 -08001417 return 0;
1418 }
1419
Charles Chan50d900c2018-03-02 13:26:22 -08001420 FlowRule defaultRoute(ForwardingObjective fwd,
Pier Ventree0ae7a32016-11-23 09:57:42 -08001421 TrafficSelector.Builder complementarySelector,
1422 int forTableId,
1423 TrafficTreatment.Builder tb) {
1424 FlowRule.Builder rule = DefaultFlowRule.builder()
1425 .fromApp(fwd.appId())
1426 .withPriority(fwd.priority())
1427 .forDevice(deviceId)
1428 .withSelector(complementarySelector.build())
1429 .withTreatment(tb.build())
1430 .forTable(forTableId);
1431 if (fwd.permanent()) {
1432 rule.makePermanent();
1433 } else {
1434 rule.makeTemporary(fwd.timeout());
1435 }
1436 return rule.build();
1437 }
1438
Saurav Das4ce45962015-11-24 23:21:05 -08001439 /**
1440 * Handles forwarding rules to the L2 bridging table. Flow actions are not
1441 * allowed in the bridging table - instead we use L2 Interface group or
1442 * L2 flood group
1443 *
1444 * @param fwd the forwarding objective
1445 * @return A collection of flow rules, or an empty set
1446 */
1447 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1448 List<FlowRule> rules = new ArrayList<>();
1449
1450 // Build filtered selector
1451 TrafficSelector selector = fwd.selector();
1452 EthCriterion ethCriterion = (EthCriterion) selector
1453 .getCriterion(Criterion.Type.ETH_DST);
1454 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
1455 .getCriterion(Criterion.Type.VLAN_VID);
1456
1457 if (vlanIdCriterion == null) {
1458 log.warn("Forwarding objective for bridging requires vlan. Not "
1459 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1460 fail(fwd, ObjectiveError.BADPARAMS);
1461 return Collections.emptySet();
1462 }
1463
1464 TrafficSelector.Builder filteredSelectorBuilder =
1465 DefaultTrafficSelector.builder();
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001466
1467 if (!ethCriterion.mac().equals(NONE) &&
1468 !ethCriterion.mac().equals(BROADCAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -08001469 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1470 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1471 fwd.id(), fwd.nextId(), deviceId);
1472 } else {
Yi Tseng3a77b4f2017-02-06 15:02:17 -08001473 // Use wildcard DST_MAC if the MacAddress is None or Broadcast
Saurav Das4ce45962015-11-24 23:21:05 -08001474 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1475 + "in dev:{} for vlan:{}",
1476 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1477 }
Charles Chand1172632017-03-15 17:33:09 -07001478 if (requireVlanExtensions()) {
1479 OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId());
1480 filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId);
1481 } else {
1482 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1483 }
Saurav Das4ce45962015-11-24 23:21:05 -08001484 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1485
1486 if (fwd.treatment() != null) {
1487 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1488 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1489 }
1490
1491 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1492 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001493 NextGroup next = getGroupForNextObjective(fwd.nextId());
Saurav Das4ce45962015-11-24 23:21:05 -08001494 if (next != null) {
1495 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1496 // we only need the top level group's key to point the flow to it
1497 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1498 if (group != null) {
1499 treatmentBuilder.deferred().group(group.id());
1500 } else {
1501 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1502 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1503 fail(fwd, ObjectiveError.GROUPMISSING);
1504 return Collections.emptySet();
1505 }
1506 }
1507 }
1508 treatmentBuilder.immediate().transition(ACL_TABLE);
1509 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1510
1511 // Build bridging table entries
1512 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1513 flowRuleBuilder.fromApp(fwd.appId())
1514 .withPriority(fwd.priority())
1515 .forDevice(deviceId)
1516 .withSelector(filteredSelector)
1517 .withTreatment(filteredTreatment)
1518 .forTable(BRIDGING_TABLE);
1519 if (fwd.permanent()) {
1520 flowRuleBuilder.makePermanent();
1521 } else {
1522 flowRuleBuilder.makeTemporary(fwd.timeout());
1523 }
1524 rules.add(flowRuleBuilder.build());
1525 return rules;
1526 }
1527
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001528 //////////////////////////////////////
1529 // Helper Methods and Classes
1530 //////////////////////////////////////
1531
1532 private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) {
1533 TrafficSelector selector = fwd.selector();
1534 EthTypeCriterion ethType = (EthTypeCriterion) selector
1535 .getCriterion(Criterion.Type.ETH_TYPE);
1536 return !((ethType == null) ||
1537 ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -08001538 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) &&
1539 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6));
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001540 }
1541
1542 private boolean isSupportedEthDstObjective(ForwardingObjective fwd) {
1543 TrafficSelector selector = fwd.selector();
1544 EthCriterion ethDst = (EthCriterion) selector
1545 .getCriterion(Criterion.Type.ETH_DST);
1546 VlanIdCriterion vlanId = (VlanIdCriterion) selector
1547 .getCriterion(Criterion.Type.VLAN_VID);
1548 return !(ethDst == null && vlanId == null);
1549 }
1550
Charles Chan50d900c2018-03-02 13:26:22 -08001551 NextGroup getGroupForNextObjective(Integer nextId) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001552 NextGroup next = flowObjectiveStore.getNextGroup(nextId);
1553 if (next != null) {
1554 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1555 if (gkeys != null && !gkeys.isEmpty()) {
1556 return next;
1557 } else {
1558 log.warn("Empty next group found in FlowObjective store for "
1559 + "next-id:{} in dev:{}", nextId, deviceId);
1560 }
1561 } else {
1562 log.warn("next-id {} not found in Flow objective store for dev:{}",
1563 nextId, deviceId);
1564 }
1565 return null;
1566 }
1567
Charles Chan188ebf52015-12-23 00:15:11 -08001568 protected static void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001569 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Das822c4e22015-10-23 10:51:11 -07001570 }
1571
Charles Chan188ebf52015-12-23 00:15:11 -08001572 protected static void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -08001573 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Das822c4e22015-10-23 10:51:11 -07001574 }
Saurav Das24431192016-03-07 19:13:00 -08001575
Saurav Das24431192016-03-07 19:13:00 -08001576 @Override
1577 public List<String> getNextMappings(NextGroup nextGroup) {
1578 List<String> mappings = new ArrayList<>();
1579 List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data());
1580 for (Deque<GroupKey> gkd : gkeys) {
1581 Group lastGroup = null;
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001582 StringBuilder gchain = new StringBuilder();
Saurav Das24431192016-03-07 19:13:00 -08001583 for (GroupKey gk : gkd) {
1584 Group g = groupService.getGroup(deviceId, gk);
Saurav Das8be4e3a2016-03-11 17:19:07 -08001585 if (g == null) {
Saurav Das25190812016-05-27 13:54:07 -07001586 gchain.append(" NoGrp").append(" -->");
Saurav Das8be4e3a2016-03-11 17:19:07 -08001587 continue;
1588 }
1589 gchain.append(" 0x").append(Integer.toHexString(g.id().id()))
1590 .append(" -->");
Saurav Das24431192016-03-07 19:13:00 -08001591 lastGroup = g;
1592 }
1593 // add port information for last group in group-chain
Yuta HIGUCHI2dce08a2017-04-20 21:57:48 -07001594 List<Instruction> lastGroupIns = new ArrayList<>();
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001595 if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) {
Saurav Das25190812016-05-27 13:54:07 -07001596 lastGroupIns = lastGroup.buckets().buckets().get(0)
1597 .treatment().allInstructions();
1598 }
1599 for (Instruction i: lastGroupIns) {
Saurav Das24431192016-03-07 19:13:00 -08001600 if (i instanceof OutputInstruction) {
Saurav Das8be4e3a2016-03-11 17:19:07 -08001601 gchain.append(" port:").append(((OutputInstruction) i).port());
Saurav Das24431192016-03-07 19:13:00 -08001602 }
1603 }
Saurav Das8be4e3a2016-03-11 17:19:07 -08001604 mappings.add(gchain.toString());
Saurav Das24431192016-03-07 19:13:00 -08001605 }
1606 return mappings;
1607 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001608
Saurav Dasa4020382018-02-14 14:14:54 -08001609 /**
1610 * Returns true iff the given selector matches on BOS==true, indicating that
1611 * the selector is trying to match on a label that is bottom-of-stack.
1612 *
1613 * @param selector the given match
1614 * @return true iff BoS==true; false if BOS==false, or BOS matching is not
1615 * expressed in the given selector
1616 */
Pier Ventre140a8942016-11-02 07:26:38 -07001617 static boolean isMplsBos(TrafficSelector selector) {
1618 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1619 return bosCriterion != null && bosCriterion.mplsBos();
1620 }
1621
Saurav Dasa4020382018-02-14 14:14:54 -08001622 /**
1623 * Returns true iff the given selector matches on BOS==false, indicating
1624 * that the selector is trying to match on a label that is not the
1625 * bottom-of-stack label.
1626 *
1627 * @param selector the given match
1628 * @return true iff BoS==false;
1629 * false if BOS==true, or BOS matching is not expressed in the given selector
1630 */
Pier Ventre140a8942016-11-02 07:26:38 -07001631 static boolean isNotMplsBos(TrafficSelector selector) {
1632 MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS);
1633 return bosCriterion != null && !bosCriterion.mplsBos();
1634 }
1635
Saurav Dasa4020382018-02-14 14:14:54 -08001636 /**
1637 * Returns true iff the forwarding objective includes a treatment to pop the
1638 * MPLS label.
1639 *
1640 * @param fwd the given forwarding objective
1641 * @return true iff mpls pop treatment exists
1642 */
1643 static boolean isMplsPop(ForwardingObjective fwd) {
1644 if (fwd.treatment() != null) {
1645 for (Instruction instr : fwd.treatment().allInstructions()) {
1646 if (instr instanceof L2ModificationInstruction
1647 && ((L2ModificationInstruction) instr)
1648 .subtype() == L2SubType.MPLS_POP) {
1649 return true;
1650 }
1651 }
1652 }
1653 return false;
1654 }
1655
Charles Chand9e47c62017-10-05 15:17:15 -07001656 private static boolean isIpv6(TrafficSelector selector) {
1657 EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE);
1658 return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6;
1659 }
1660
Charles Chan50d900c2018-03-02 13:26:22 -08001661 static VlanId readVlanFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001662 if (selector == null) {
1663 return null;
1664 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001665 Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID);
1666 return (criterion == null)
1667 ? null : ((VlanIdCriterion) criterion).vlanId();
1668 }
1669
Charles Chand9e47c62017-10-05 15:17:15 -07001670 static IpPrefix readIpDstFromSelector(TrafficSelector selector) {
Saurav Das59232cf2016-04-27 18:35:50 -07001671 if (selector == null) {
1672 return null;
1673 }
Charles Chan5b9df8d2016-03-28 22:21:40 -07001674 Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST);
1675 return (criterion == null) ? null : ((IPCriterion) criterion).ip();
1676 }
Charles Chand55e84d2016-03-30 17:54:24 -07001677
1678 private static VlanId readVlanFromTreatment(TrafficTreatment treatment) {
Saurav Das59232cf2016-04-27 18:35:50 -07001679 if (treatment == null) {
1680 return null;
1681 }
Charles Chand55e84d2016-03-30 17:54:24 -07001682 for (Instruction i : treatment.allInstructions()) {
1683 if (i instanceof ModVlanIdInstruction) {
1684 return ((ModVlanIdInstruction) i).vlanId();
1685 }
1686 }
1687 return null;
1688 }
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001689
1690 /**
1691 * Utility class that retries sending flows a fixed number of times, even if
1692 * some of the attempts are successful. Used only for forwarding objectives.
1693 */
Alex Yashchuk4caa8e82017-12-08 17:40:05 +02001694 public final class RetryFlows implements Runnable {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001695 int attempts = MAX_RETRY_ATTEMPTS;
1696 private Collection<FlowRule> retryFlows;
1697 private ForwardingObjective fwd;
1698
Charles Chan50d900c2018-03-02 13:26:22 -08001699 RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -07001700 this.fwd = fwd;
1701 this.retryFlows = retryFlows;
1702 }
1703
1704 @Override
1705 public void run() {
1706 log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}",
1707 MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size());
1708 sendForward(fwd, retryFlows);
1709 if (--attempts > 0) {
1710 executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS);
1711 }
1712 }
1713 }
1714
Saurav Das822c4e22015-10-23 10:51:11 -07001715}