blob: 8c20f50199f17718addef39255403dca98f7d2ec [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska58de4162015-09-10 16:15:33 -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 Das558afec2015-05-31 17:12:48 -070017
Charles Chan5270ed02016-01-30 23:22:37 -080018import com.google.common.collect.ImmutableList;
Charles Chan5b9df8d2016-03-28 22:21:40 -070019import com.google.common.collect.ImmutableSet;
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -070020import org.onlab.packet.EthType;
Saurav Das8a0732e2015-11-20 15:27:53 -080021import org.onlab.packet.Ethernet;
Julia Ferguson65428c32017-08-10 18:15:24 +000022import org.onlab.packet.IpAddress;
Flavio Castroe10fa242016-01-15 12:43:51 -080023import org.onlab.packet.IpPrefix;
Charles Chan45b69ab2018-03-02 15:41:41 -080024import org.onlab.packet.MacAddress;
Saurav Das2857f382015-11-03 14:39:27 -080025import org.onlab.packet.VlanId;
26import org.onosproject.core.ApplicationId;
Yi Tsengfa394de2017-02-01 11:26:40 -080027import org.onosproject.core.GroupId;
Andrea Campanella7c977b92018-05-30 21:39:45 -070028import org.onosproject.net.ConnectPoint;
Andreas Pantelopoulos89369462018-05-30 14:30:24 -070029import org.onosproject.net.DeviceId;
Andrea Campanella7c977b92018-05-30 21:39:45 -070030import org.onosproject.driver.extensions.OfdpaMatchActsetOutput;
31import org.onosproject.net.Host;
Saurav Das2857f382015-11-03 14:39:27 -080032import org.onosproject.net.Port;
33import org.onosproject.net.PortNumber;
Saurav Das8a0732e2015-11-20 15:27:53 -080034import org.onosproject.net.behaviour.NextGroup;
Charles Chan425854b2016-04-11 15:32:12 -070035import org.onosproject.net.behaviour.PipelinerContext;
Saurav Das558afec2015-05-31 17:12:48 -070036import org.onosproject.net.flow.DefaultFlowRule;
37import org.onosproject.net.flow.DefaultTrafficSelector;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
39import org.onosproject.net.flow.FlowRule;
40import org.onosproject.net.flow.FlowRuleOperations;
41import org.onosproject.net.flow.FlowRuleOperationsContext;
42import org.onosproject.net.flow.TrafficSelector;
43import org.onosproject.net.flow.TrafficTreatment;
Saurav Das4ce45962015-11-24 23:21:05 -080044import org.onosproject.net.flow.criteria.Criteria;
Saurav Das8a0732e2015-11-20 15:27:53 -080045import org.onosproject.net.flow.criteria.Criterion;
Saurav Das4ce45962015-11-24 23:21:05 -080046import org.onosproject.net.flow.criteria.EthCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080047import org.onosproject.net.flow.criteria.EthTypeCriterion;
48import org.onosproject.net.flow.criteria.IPCriterion;
49import org.onosproject.net.flow.criteria.MplsBosCriterion;
50import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das2857f382015-11-03 14:39:27 -080051import org.onosproject.net.flow.criteria.PortCriterion;
52import org.onosproject.net.flow.criteria.VlanIdCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080053import org.onosproject.net.flow.instructions.Instruction;
Andrea Campanella7c977b92018-05-30 21:39:45 -070054import org.onosproject.net.flow.instructions.Instructions;
Saurav Das52025962016-01-28 22:30:01 -080055import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chanf76de302018-06-15 18:54:18 -070056import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Andrea Campanella7c977b92018-05-30 21:39:45 -070057import org.onosproject.net.flow.instructions.L2ModificationInstruction;
Charles Chan40132b32017-01-22 00:19:37 -080058import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Andrea Campanella7c977b92018-05-30 21:39:45 -070059import org.onosproject.net.flowobjective.FilteringObjective;
Saurav Das8a0732e2015-11-20 15:27:53 -080060import org.onosproject.net.flowobjective.ForwardingObjective;
61import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan0f43e472017-02-14 14:00:16 -080062import org.onosproject.net.group.DefaultGroupBucket;
63import org.onosproject.net.group.DefaultGroupDescription;
64import org.onosproject.net.group.DefaultGroupKey;
Saurav Das8a0732e2015-11-20 15:27:53 -080065import org.onosproject.net.group.Group;
Charles Chan0f43e472017-02-14 14:00:16 -080066import org.onosproject.net.group.GroupBucket;
67import org.onosproject.net.group.GroupBuckets;
68import org.onosproject.net.group.GroupDescription;
Saurav Das8a0732e2015-11-20 15:27:53 -080069import org.onosproject.net.group.GroupKey;
Andrea Campanella7c977b92018-05-30 21:39:45 -070070import org.onosproject.net.host.HostService;
Charles Chanf57a8252016-06-29 19:12:37 -070071import org.onosproject.net.packet.PacketPriority;
Saurav Das558afec2015-05-31 17:12:48 -070072import org.slf4j.Logger;
73
Jonathan Hart855179c2016-04-26 07:40:04 -070074import java.util.ArrayList;
75import java.util.Collection;
76import java.util.Collections;
77import java.util.Deque;
Andrea Campanella7c977b92018-05-30 21:39:45 -070078import java.util.HashSet;
Jonathan Hart855179c2016-04-26 07:40:04 -070079import java.util.List;
Charles Chan0f43e472017-02-14 14:00:16 -080080import java.util.Objects;
Andreas Pantelopoulos89369462018-05-30 14:30:24 -070081import java.util.Queue;
82import java.util.concurrent.ConcurrentLinkedQueue;
83import java.util.concurrent.Executors;
84import java.util.concurrent.ScheduledExecutorService;
85import java.util.concurrent.TimeUnit;
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -070086import java.util.concurrent.locks.ReentrantLock;
Andrea Campanella7c977b92018-05-30 21:39:45 -070087import java.util.Optional;
Jonathan Hart855179c2016-04-26 07:40:04 -070088
Charles Chand05f54b2017-02-13 11:56:54 -080089import static org.onlab.packet.MacAddress.BROADCAST;
90import static org.onlab.packet.MacAddress.NONE;
Andreas Pantelopoulos89369462018-05-30 14:30:24 -070091import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070092import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier9469f3e2019-04-17 17:05:08 +020093import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
Andrea Campanella7c977b92018-05-30 21:39:45 -070094import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID;
Jonathan Hart855179c2016-04-26 07:40:04 -070095import static org.slf4j.LoggerFactory.getLogger;
96
Saurav Das558afec2015-05-31 17:12:48 -070097/**
Charles Chanab591602019-01-22 17:25:04 -080098 * Driver for Open vSwitch emulation of the OFDPA pipeline.
Saurav Das558afec2015-05-31 17:12:48 -070099 */
Charles Chanab591602019-01-22 17:25:04 -0800100public class OvsOfdpaPipeline extends Ofdpa2Pipeline {
Saurav Das558afec2015-05-31 17:12:48 -0700101
102 private final Logger log = getLogger(getClass());
103
Andrea Campanella7c977b92018-05-30 21:39:45 -0700104 private static final int EGRESS_VLAN_FLOW_TABLE_IN_INGRESS = 31;
105 private static final int UNICAST_ROUTING_TABLE_1 = 32;
Charles Chan40132b32017-01-22 00:19:37 -0800106 /**
Charles Chan0f43e472017-02-14 14:00:16 -0800107 * Table that determines whether VLAN is popped before punting to controller.
108 * <p>
109 * This is a non-OFDPA table to emulate OFDPA packet in behavior.
110 * VLAN will be popped before punting if the VLAN is internally assigned.
Charles Chan0f43e472017-02-14 14:00:16 -0800111 */
112 private static final int PUNT_TABLE = 63;
113
114 /**
115 * A static indirect group that pop vlan and punt to controller.
116 * <p>
117 * The purpose of using a group instead of immediate action is that this
118 * won't affect another copy on the data plane when write action exists.
119 */
Charles Chan07372342018-08-14 18:31:17 -0700120 private static final int POP_VLAN_PUNT_GROUP_ID = 0xd0000000;
Charles Chan0f43e472017-02-14 14:00:16 -0800121
Andreas Pantelopoulos89369462018-05-30 14:30:24 -0700122 /**
123 * Executor for group checker thread that checks pop vlan punt group.
124 */
125 private ScheduledExecutorService groupChecker;
126
127 /**
128 * Queue for passing pop vlan punt group flow rules to the GroupChecker thread.
129 */
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700130 private Queue<FlowRule> flowRuleQueue;
131
132 /**
133 * Lock used in synchronizing driver thread with groupCheckerThread.
134 */
135 private ReentrantLock groupCheckerLock;
Andreas Pantelopoulos89369462018-05-30 14:30:24 -0700136
Charles Chan053b1cb2017-03-22 16:56:35 -0700137 @Override
138 protected boolean requireVlanExtensions() {
139 return false;
140 }
141
Charles Chan425854b2016-04-11 15:32:12 -0700142 @Override
Charles Chan40132b32017-01-22 00:19:37 -0800143 protected void initDriverId() {
Charles Chan425854b2016-04-11 15:32:12 -0700144 driverId = coreService.registerApplication(
Charles Chanab591602019-01-22 17:25:04 -0800145 "org.onosproject.driver.OvsOfdpaPipeline");
Charles Chan40132b32017-01-22 00:19:37 -0800146 }
Charles Chan425854b2016-04-11 15:32:12 -0700147
Charles Chan40132b32017-01-22 00:19:37 -0800148 @Override
149 protected void initGroupHander(PipelinerContext context) {
Charles Chanab591602019-01-22 17:25:04 -0800150 groupHandler = new OvsOfdpaGroupHandler();
Charles Chan40132b32017-01-22 00:19:37 -0800151 groupHandler.init(deviceId, context);
Charles Chan425854b2016-04-11 15:32:12 -0700152 }
153
Andreas Pantelopoulos89369462018-05-30 14:30:24 -0700154 @Override
155 public void init(DeviceId deviceId, PipelinerContext context) {
Charles Chanab591602019-01-22 17:25:04 -0800156 // create a new executor at each init and a new empty queue
157 groupChecker = Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/driver",
158 "ovs-ofdpa-%d", log));
159 flowRuleQueue = new ConcurrentLinkedQueue<>();
160 groupCheckerLock = new ReentrantLock();
161 groupChecker.scheduleAtFixedRate(new PopVlanPuntGroupChecker(), 20, 50, TimeUnit.MILLISECONDS);
162 super.init(deviceId, context);
Andreas Pantelopoulos89369462018-05-30 14:30:24 -0700163 }
pier9469f3e2019-04-17 17:05:08 +0200164
Andrea Campanella7c977b92018-05-30 21:39:45 -0700165 protected void processFilter(FilteringObjective filteringObjective,
166 boolean install,
167 ApplicationId applicationId) {
pier9469f3e2019-04-17 17:05:08 +0200168 if (OfdpaPipelineUtility.isDoubleTagged(filteringObjective)) {
Andrea Campanella7c977b92018-05-30 21:39:45 -0700169 processDoubleTaggedFilter(filteringObjective, install, applicationId);
170 } else {
171 // If it is not a double-tagged filter, we fall back
172 // to the OFDPA 2.0 pipeline.
173 super.processFilter(filteringObjective, install, applicationId);
174 }
175 }
176
177 /**
Andrea Campanella7c977b92018-05-30 21:39:45 -0700178 * Determines if the forwarding objective will be used for double-tagged packets.
179 *
180 * @param fwd Forwarding objective
181 * @return True if the objective was created for double-tagged packets, false otherwise.
182 */
183 private boolean isDoubleTagged(ForwardingObjective fwd) {
184 if (fwd.nextId() != null) {
185 NextGroup next = getGroupForNextObjective(fwd.nextId());
186 if (next != null) {
187 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
188 // we only need the top level group's key
189 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
190 if (group != null) {
191 int groupId = group.id().id();
192 if (((groupId & ~TYPE_MASK) == L3_UNICAST_TYPE) &&
193 ((groupId & TYPE_L3UG_DOUBLE_VLAN_MASK) == TYPE_L3UG_DOUBLE_VLAN_MASK)) {
194 return true;
195 }
196 }
197 }
198 }
199 return false;
200 }
201
202 /**
203 * Configure filtering rules of outer and inner VLAN IDs, and a MAC address.
204 * Filtering happens in three tables (VLAN_TABLE, VLAN_1_TABLE, TMAC_TABLE).
205 *
206 * @param filteringObjective the filtering objective
207 * @param install true to add, false to remove
208 * @param applicationId for application programming this filter
209 */
210 private void processDoubleTaggedFilter(FilteringObjective filteringObjective,
211 boolean install,
212 ApplicationId applicationId) {
213 PortCriterion portCriterion = null;
214 EthCriterion ethCriterion = null;
215 VlanIdCriterion innervidCriterion = null;
216 VlanIdCriterion outerVidCriterion = null;
217 boolean popVlan = false;
218 TrafficTreatment meta = filteringObjective.meta();
219 if (!filteringObjective.key().equals(Criteria.dummy()) &&
220 filteringObjective.key().type() == Criterion.Type.IN_PORT) {
221 portCriterion = (PortCriterion) filteringObjective.key();
222 }
223 if (portCriterion == null) {
224 log.warn("No IN_PORT defined in filtering objective from app: {}" +
225 "Failed to program VLAN tables.", applicationId);
226 return;
227 } else {
228 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
229 portCriterion.port());
230 }
231
232 // meta should have only one instruction, popVlan.
233 if (meta != null && meta.allInstructions().size() == 1) {
234 L2ModificationInstruction l2Inst = (L2ModificationInstruction) meta.allInstructions().get(0);
235 if (l2Inst.subtype().equals(L2ModificationInstruction.L2SubType.VLAN_POP)) {
236 popVlan = true;
237 } else {
238 log.warn("Filtering objective can have only VLAN_POP instruction.");
239 return;
240 }
241 } else {
242 log.warn("Filtering objective should have one instruction.");
243 return;
244 }
245
246 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
247 for (Criterion criterion : filteringObjective.conditions()) {
248 switch (criterion.type()) {
249 case ETH_DST:
250 case ETH_DST_MASKED:
251 ethCriterion = (EthCriterion) criterion;
252 break;
253 case VLAN_VID:
254 if (innervidCriterion == null) {
255 innervidCriterion = (VlanIdCriterion) criterion;
256 } else {
257 outerVidCriterion = innervidCriterion;
258 innervidCriterion = (VlanIdCriterion) criterion;
259 }
260 break;
261 default:
262 log.warn("Unsupported filter {}", criterion);
263 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
264 return;
265 }
266 }
267
268 if (innervidCriterion == null || outerVidCriterion == null) {
269 log.warn("filtering objective should have two vidCriterion.");
270 return;
271 }
272
273 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
274 // NOTE: it is possible that a filtering objective only has vidCriterion
275 log.warn("filtering objective missing dstMac, cannot program TMAC table");
276 return;
277 } else {
278 MacAddress unicastMac = readEthDstFromTreatment(filteringObjective.meta());
279 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion, innervidCriterion,
280 innervidCriterion.vlanId(), unicastMac,
281 applicationId);
282 for (List<FlowRule> flowRules : allStages) {
283 log.trace("Starting a new flow rule stage for TMAC table flow");
284 ops.newStage();
285
286 for (FlowRule flowRule : flowRules) {
287 log.trace("{} flow rules in TMAC table: {} for dev: {}",
288 (install) ? "adding" : "removing", flowRules, deviceId);
289 if (install) {
290 ops = ops.add(flowRule);
291 } else {
292 // NOTE: Only remove TMAC flow when there is no more enabled port within the
293 // same VLAN on this device if TMAC doesn't support matching on in_port.
294 if (matchInPortTmacTable()
295 || (filteringObjective.meta() != null
296 && filteringObjective.meta().clearedDeferred())) {
297 ops = ops.remove(flowRule);
298 } else {
299 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
300 }
301 }
302 }
303 }
304 }
305
306 List<FlowRule> rules;
307 rules = processDoubleVlanIdFilter(portCriterion, innervidCriterion,
308 outerVidCriterion, popVlan, applicationId);
309 for (FlowRule flowRule : rules) {
310 log.trace("{} flow rule in VLAN table: {} for dev: {}",
311 (install) ? "adding" : "removing", flowRule, deviceId);
312 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
313 }
314
315 // apply filtering flow rules
316 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
317 @Override
318 public void onSuccess(FlowRuleOperations ops) {
319 log.debug("Applied {} filtering rules in device {}",
320 ops.stages().get(0).size(), deviceId);
321 pass(filteringObjective);
322 }
323
324 @Override
325 public void onError(FlowRuleOperations ops) {
326 log.info("Failed to apply all filtering rules in dev {}", deviceId);
327 fail(filteringObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
328 }
329 }));
330
331 }
332 /**
333 * Internal implementation of processDoubleVlanIdFilter.
334 *
335 * @param portCriterion port on device for which this filter is programmed
336 * @param innerVidCriterion inner vlan
337 * @param outerVidCriterion outer vlan
338 * @param popVlan true if outer vlan header needs to be removed
339 * @param applicationId for application programming this filter
340 * @return flow rules for port-vlan filters
341 */
342 private List<FlowRule> processDoubleVlanIdFilter(PortCriterion portCriterion,
343 VlanIdCriterion innerVidCriterion,
344 VlanIdCriterion outerVidCriterion,
345 boolean popVlan,
346 ApplicationId applicationId) {
347 List<FlowRule> rules = new ArrayList<>();
348 TrafficSelector.Builder outerSelector = DefaultTrafficSelector.builder();
349 TrafficTreatment.Builder outerTreatment = DefaultTrafficTreatment.builder();
350 TrafficSelector.Builder innerSelector = DefaultTrafficSelector.builder();
351 TrafficTreatment.Builder innerTreatment = DefaultTrafficTreatment.builder();
352
353 VlanId outerVlanId = outerVidCriterion.vlanId();
354 VlanId innerVlanId = innerVidCriterion.vlanId();
355 PortNumber portNumber = portCriterion.port();
356 // Check arguments
357 if (PortNumber.ALL.equals(portNumber)
358 || outerVlanId.equals(VlanId.NONE)
359 || innerVlanId.equals(VlanId.NONE)) {
360 log.warn("Incomplete Filtering Objective. " +
361 "VLAN Table cannot be programmed for {}", deviceId);
362 return ImmutableList.of();
363 } else {
364 outerSelector.matchInPort(portNumber);
365 innerSelector.matchInPort(portNumber);
366 outerTreatment.transition(VLAN_1_TABLE);
367 innerTreatment.transition(TMAC_TABLE);
368 outerTreatment.writeMetadata(outerVlanId.toShort(), 0xFFF);
369
370 outerSelector.matchVlanId(outerVlanId);
371 innerSelector.matchVlanId(innerVlanId);
372 //force recompilation
373 //FIXME might be issue due tu /fff mask
374 innerSelector.matchMetadata(outerVlanId.toShort());
375
376 if (popVlan) {
377 outerTreatment.popVlan();
378 }
379 }
380
381 // NOTE: for double-tagged packets, restore original outer vlan
382 // before sending it to the controller.
Charles Chanab591602019-01-22 17:25:04 -0800383 GroupKey groupKey = popVlanPuntGroupKey();
384 Group group = groupService.getGroup(deviceId, groupKey);
385 if (group != null) {
386 // push outer vlan and send to controller
387 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
388 .matchInPort(portNumber)
389 .matchVlanId(innerVlanId);
390 Host host = handler().get(HostService.class).getConnectedHosts(ConnectPoint.
391 deviceConnectPoint(deviceId + "/" + portNumber.toLong())).stream().filter(h ->
392 h.vlan().equals(outerVlanId)).findFirst().orElse(null);
393 EthType vlanType = EthType.EtherType.VLAN.ethType();
394 if (host != null) {
395 vlanType = host.tpid();
Andrea Campanella7c977b92018-05-30 21:39:45 -0700396 }
Charles Chanab591602019-01-22 17:25:04 -0800397 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
398 .pushVlan(vlanType).setVlanId(outerVlanId).punt();
399
400 rules.add(DefaultFlowRule.builder()
401 .forDevice(deviceId)
402 .withSelector(sbuilder.build())
403 .withTreatment(tbuilder.build())
404 .withPriority(PacketPriority.CONTROL.priorityValue())
405 .fromApp(driverId)
406 .makePermanent()
407 .forTable(PUNT_TABLE).build());
408 } else {
409 log.info("popVlanPuntGroup not found in dev:{}", deviceId);
410 return Collections.emptyList();
Andrea Campanella7c977b92018-05-30 21:39:45 -0700411 }
Charles Chanab591602019-01-22 17:25:04 -0800412
Andrea Campanella7c977b92018-05-30 21:39:45 -0700413 FlowRule outerRule = DefaultFlowRule.builder()
414 .forDevice(deviceId)
415 .withSelector(outerSelector.build())
416 .withTreatment(outerTreatment.build())
417 .withPriority(DEFAULT_PRIORITY)
418 .fromApp(applicationId)
419 .makePermanent()
420 .forTable(VLAN_TABLE)
421 .build();
422 FlowRule innerRule = DefaultFlowRule.builder()
423 .forDevice(deviceId)
424 .withSelector(innerSelector.build())
425 .withTreatment(innerTreatment.build())
426 .withPriority(DEFAULT_PRIORITY)
427 .fromApp(applicationId)
428 .makePermanent()
429 .forTable(VLAN_1_TABLE)
430 .build();
431 rules.add(outerRule);
432 rules.add(innerRule);
433
434 return rules;
435 }
436
437 /**
438 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
439 * egress tables.
440 * @param fwd the forwarding objective of type 'egress'
441 * @return a collection of flow rules to be sent to the switch. An empty
442 * collection may be returned if there is a problem in processing
443 * the flow rule
444 */
445 @Override
446 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
447 log.debug("Processing egress forwarding objective:{} in dev:{}",
448 fwd, deviceId);
449
450 List<FlowRule> rules = new ArrayList<>();
451
452 // Build selector
453 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
454 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
455 if (vlanIdCriterion == null) {
456 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
457 fail(fwd, ObjectiveError.BADPARAMS);
458 return rules;
459 }
460
461 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
462 .filter(instruction -> instruction instanceof Instructions.OutputInstruction).findFirst();
463 if (!outInstr.isPresent()) {
464 log.error("Egress forwarding objective:{} must include output port", fwd.id());
465 fail(fwd, ObjectiveError.BADPARAMS);
466 return rules;
467 }
468
469 PortNumber portNumber = ((Instructions.OutputInstruction) outInstr.get()).port();
470
471 sb.matchVlanId(vlanIdCriterion.vlanId());
472 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
473 sb.extension(actsetOutput, deviceId);
474
475 // Build a flow rule for Egress VLAN Flow table
476 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
477 tb.transition(UNICAST_ROUTING_TABLE_1);
478 if (fwd.treatment() != null) {
479 for (Instruction instr : fwd.treatment().allInstructions()) {
480 if (instr instanceof L2ModificationInstruction &&
481 ((L2ModificationInstruction) instr).subtype() ==
482 L2ModificationInstruction.L2SubType.VLAN_ID) {
483 tb.immediate().add(instr);
484 }
485 if (instr instanceof L2ModificationInstruction &&
486 ((L2ModificationInstruction) instr).subtype() ==
487 L2ModificationInstruction.L2SubType.VLAN_PUSH) {
488 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
489 tb.immediate().pushVlan(ethType);
490 }
491 }
492 }
493
494 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
495 .fromApp(fwd.appId())
496 .withPriority(fwd.priority())
497 .forDevice(deviceId)
498 .withSelector(sb.build())
499 .withTreatment(tb.build())
500 .makePermanent()
501 .forTable(EGRESS_VLAN_FLOW_TABLE_IN_INGRESS);
502 rules.add(ruleBuilder.build());
503 return rules;
504 }
505
506 /**
507 * Handles forwarding rules to the IP Unicast Routing.
508 *
509 * @param fwd the forwarding objective
510 * @return A collection of flow rules, or an empty set
511 */
512 protected Collection<FlowRule> processDoubleTaggedFwd(ForwardingObjective fwd) {
513 // inner for UNICAST_ROUTING_TABLE_1, outer for UNICAST_ROUTING_TABLE
514 TrafficSelector selector = fwd.selector();
515 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
516 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
517 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
518
519 EthTypeCriterion ethType =
520 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
521
522 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
523 sBuilder.matchEthType(Ethernet.TYPE_IPV4);
524 sBuilder.matchVlanId(VlanId.ANY);
525 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
526 if (!ipv4Dst.isMulticast() && ipv4Dst.prefixLength() == 32) {
527 sBuilder.matchIPDst(ipv4Dst);
528 if (fwd.nextId() != null) {
529 NextGroup next = getGroupForNextObjective(fwd.nextId());
530 if (next != null) {
531 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
532 // we only need the top level group's key to point the flow to it
533 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
534 if (group == null) {
535 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
536 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
537 fail(fwd, ObjectiveError.GROUPMISSING);
538 return Collections.emptySet();
539 }
540 outerTtb.immediate().setVlanId(extractDummyVlanIdFromGroupId(group.id().id()));
541 //ACTSET_OUTPUT in OVS will match output action in write_action() set.
542 outerTtb.deferred().setOutput(extractOutputPortFromGroupId(group.id().id()));
543 outerTtb.transition(EGRESS_VLAN_FLOW_TABLE_IN_INGRESS);
544 innerTtb.deferred().group(group.id());
545 innerTtb.transition(ACL_TABLE);
546
547 FlowRule.Builder innerRuleBuilder = DefaultFlowRule.builder()
548 .fromApp(fwd.appId())
549 .withPriority(fwd.priority())
550 .forDevice(deviceId)
551 .withSelector(sBuilder.build())
552 .withTreatment(innerTtb.build())
553 .forTable(UNICAST_ROUTING_TABLE_1);
554 if (fwd.permanent()) {
555 innerRuleBuilder.makePermanent();
556 } else {
557 innerRuleBuilder.makeTemporary(fwd.timeout());
558 }
559 Collection<FlowRule> flowRuleCollection = new HashSet<>();
560 flowRuleCollection.add(innerRuleBuilder.build());
561
562 FlowRule.Builder outerRuleBuilder = DefaultFlowRule.builder()
563 .fromApp(fwd.appId())
564 .withPriority(fwd.priority())
565 .forDevice(deviceId)
566 .withSelector(sBuilder.build())
567 .withTreatment(outerTtb.build())
568 .forTable(UNICAST_ROUTING_TABLE);
569 if (fwd.permanent()) {
570 outerRuleBuilder.makePermanent();
571 } else {
572 outerRuleBuilder.makeTemporary(fwd.timeout());
573 }
574 flowRuleCollection.add(innerRuleBuilder.build());
575 flowRuleCollection.add(outerRuleBuilder.build());
576 return flowRuleCollection;
577 } else {
578 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
579 fwd.nextId(), deviceId, fwd.id());
580 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
581 return Collections.emptySet();
582 }
583 } else {
584 log.warn("NextId is not specified in fwd:{}", fwd.id());
585 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
586 return Collections.emptySet();
587 }
588 }
589 }
590 return Collections.emptySet();
591 }
592
593 private static VlanId extractDummyVlanIdFromGroupId(int groupId) {
594 short vlanId = (short) ((groupId & 0x7FF8000) >> 15);
595 return VlanId.vlanId(vlanId);
596 }
597
598 private static PortNumber extractOutputPortFromGroupId(int groupId) {
599 return PortNumber.portNumber(groupId & 0x7FFF);
600 }
601
Saurav Das4ce45962015-11-24 23:21:05 -0800602 /*
Charles Chanab591602019-01-22 17:25:04 -0800603 * Open vSwitch emulation does not require the non OF-standard rules for
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700604 * matching untagged packets that ofdpa uses.
Saurav Das4ce45962015-11-24 23:21:05 -0800605 *
606 * (non-Javadoc)
607 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processVlanIdFilter
608 */
Saurav Das558afec2015-05-31 17:12:48 -0700609 @Override
Charles Chan66291502018-03-02 16:43:28 -0800610 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Saurav Das2857f382015-11-03 14:39:27 -0800611 VlanIdCriterion vidCriterion,
612 VlanId assignedVlan,
613 ApplicationId applicationId) {
Charles Chan79769232016-07-05 16:34:39 -0700614 List<FlowRule> rules = new ArrayList<>();
Saurav Das2857f382015-11-03 14:39:27 -0800615 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
616 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
617 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800618 treatment.transition(TMAC_TABLE);
619
Saurav Das2857f382015-11-03 14:39:27 -0800620 if (vidCriterion.vlanId() == VlanId.NONE) {
621 // untagged packets are assigned vlans
622 treatment.pushVlan().setVlanId(assignedVlan);
Piere5bff482018-03-07 11:42:50 +0100623 } else if (!vidCriterion.vlanId().equals(assignedVlan)) {
624 // Rewrite with assigned vlans
625 treatment.setVlanId(assignedVlan);
Saurav Das2857f382015-11-03 14:39:27 -0800626 }
Saurav Das2857f382015-11-03 14:39:27 -0800627
628 // ofdpa cannot match on ALL portnumber, so we need to use separate
629 // rules for each port.
Charles Chan50d900c2018-03-02 13:26:22 -0800630 List<PortNumber> portnums = new ArrayList<>();
Ray Milkeybe9f3bc2018-05-10 12:42:51 -0700631 if (portCriterion != null) {
632 if (portCriterion.port() == PortNumber.ALL) {
633 for (Port port : deviceService.getPorts(deviceId)) {
634 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
635 portnums.add(port.number());
636 }
Saurav Das2857f382015-11-03 14:39:27 -0800637 }
Ray Milkeybe9f3bc2018-05-10 12:42:51 -0700638 } else {
639 portnums.add(portCriterion.port());
Saurav Das2857f382015-11-03 14:39:27 -0800640 }
Saurav Das2857f382015-11-03 14:39:27 -0800641 }
Saurav Das4f980082015-11-05 13:39:15 -0800642
Saurav Das2857f382015-11-03 14:39:27 -0800643 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800644 // NOTE: Emulating OFDPA behavior by popping off internal assigned
645 // VLAN before sending to controller
Charles Chanab591602019-01-22 17:25:04 -0800646 if (vidCriterion.vlanId() == VlanId.NONE) {
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700647 try {
648 groupCheckerLock.lock();
649 if (flowRuleQueue == null) {
650 // this means that the group has been created
651 // and that groupChecker has destroyed the queue
652 log.debug("Installing punt table rule for untagged port {} and vlan {}.",
653 pnum, assignedVlan);
654 rules.add(buildPuntTableRule(pnum, assignedVlan));
655 } else {
656 // The VLAN punt group may be held back due to device initial audit.
657 // In that case, we queue all punt table flow until the group has been created.
658 log.debug("popVlanPuntGroup not found in dev:{}, queueing this flow rule.", deviceId);
659 flowRuleQueue.add(buildPuntTableRule(pnum, assignedVlan));
Andreas Pantelopoulos89369462018-05-30 14:30:24 -0700660 }
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700661 } finally {
662 groupCheckerLock.unlock();
Charles Chan0f43e472017-02-14 14:00:16 -0800663 }
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700664 } else if (vidCriterion.vlanId() != VlanId.NONE) {
665 // for tagged ports just forward to the controller
666 log.debug("Installing punt rule for tagged port {} and vlan {}.", pnum, vidCriterion.vlanId());
667 rules.add(buildPuntTableRuleTagged(pnum, vidCriterion.vlanId()));
Charles Chan0f43e472017-02-14 14:00:16 -0800668 }
669
Saurav Das4f980082015-11-05 13:39:15 -0800670 // create rest of flowrule
Saurav Das2857f382015-11-03 14:39:27 -0800671 selector.matchInPort(pnum);
672 FlowRule rule = DefaultFlowRule.builder()
673 .forDevice(deviceId)
674 .withSelector(selector.build())
675 .withTreatment(treatment.build())
676 .withPriority(DEFAULT_PRIORITY)
677 .fromApp(applicationId)
678 .makePermanent()
679 .forTable(VLAN_TABLE).build();
680 rules.add(rule);
681 }
Charles Chanf57a8252016-06-29 19:12:37 -0700682
Charles Chan66291502018-03-02 16:43:28 -0800683 return ImmutableList.of(rules);
Saurav Das2857f382015-11-03 14:39:27 -0800684 }
685
Pier Ventree0ae7a32016-11-23 09:57:42 -0800686 /**
Charles Chan0f43e472017-02-14 14:00:16 -0800687 * Creates punt table entry that matches IN_PORT and VLAN_VID and points to
688 * a group that pop vlan and punt.
689 *
690 * @param portNumber port number
691 * @param assignedVlan internally assigned vlan id
692 * @return punt table flow rule
693 */
694 private FlowRule buildPuntTableRule(PortNumber portNumber, VlanId assignedVlan) {
695 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
696 .matchInPort(portNumber)
697 .matchVlanId(assignedVlan);
698 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
Yi Tsengfa394de2017-02-01 11:26:40 -0800699 .group(new GroupId(POP_VLAN_PUNT_GROUP_ID));
Charles Chan0f43e472017-02-14 14:00:16 -0800700
701 return DefaultFlowRule.builder()
702 .forDevice(deviceId)
703 .withSelector(sbuilder.build())
704 .withTreatment(tbuilder.build())
705 .withPriority(PacketPriority.CONTROL.priorityValue())
706 .fromApp(driverId)
707 .makePermanent()
708 .forTable(PUNT_TABLE).build();
709 }
710
711 /**
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700712 * Creates punt table entry that matches IN_PORT and VLAN_VID and forwards
713 * packet to controller tagged.
714 *
715 * @param portNumber port number
716 * @param packetVlan vlan tag of the packet
717 * @return punt table flow rule
718 */
719 private FlowRule buildPuntTableRuleTagged(PortNumber portNumber, VlanId packetVlan) {
720 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
721 .matchInPort(portNumber)
722 .matchVlanId(packetVlan);
723 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder().punt();
724
725 return DefaultFlowRule.builder()
726 .forDevice(deviceId)
727 .withSelector(sbuilder.build())
728 .withTreatment(tbuilder.build())
729 .withPriority(PacketPriority.CONTROL.priorityValue())
730 .fromApp(driverId)
731 .makePermanent()
732 .forTable(PUNT_TABLE).build();
733 }
734
Saurav Das8a0732e2015-11-20 15:27:53 -0800735 @Override
Charles Chan66291502018-03-02 16:43:28 -0800736 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das4ce45962015-11-24 23:21:05 -0800737 EthCriterion ethCriterion,
738 VlanIdCriterion vidCriterion,
739 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800740 MacAddress unicastMac,
Saurav Das4ce45962015-11-24 23:21:05 -0800741 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800742 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
743 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
744 return processEthDstOnlyFilter(ethCriterion, applicationId);
745 }
746
Charles Chan5b9df8d2016-03-28 22:21:40 -0700747 // Multicast MAC
748 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800749 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700750 }
751
Saurav Das4ce45962015-11-24 23:21:05 -0800752 //handling untagged packets via assigned VLAN
753 if (vidCriterion.vlanId() == VlanId.NONE) {
754 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
755 }
756 // ofdpa cannot match on ALL portnumber, so we need to use separate
757 // rules for each port.
Charles Chan50d900c2018-03-02 13:26:22 -0800758 List<PortNumber> portnums = new ArrayList<>();
Ray Milkey74e59132018-01-17 15:24:52 -0800759 if (portCriterion != null) {
760 if (portCriterion.port() == PortNumber.ALL) {
761 for (Port port : deviceService.getPorts(deviceId)) {
762 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
763 portnums.add(port.number());
764 }
Saurav Das4ce45962015-11-24 23:21:05 -0800765 }
Ray Milkey74e59132018-01-17 15:24:52 -0800766 } else {
767 portnums.add(portCriterion.port());
Saurav Das4ce45962015-11-24 23:21:05 -0800768 }
Saurav Das4ce45962015-11-24 23:21:05 -0800769 }
770
Charles Chan50d900c2018-03-02 13:26:22 -0800771 List<FlowRule> rules = new ArrayList<>();
Saurav Das4ce45962015-11-24 23:21:05 -0800772 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800773 // TMAC rules for unicast IP packets
Saurav Das4ce45962015-11-24 23:21:05 -0800774 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
775 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
776 selector.matchInPort(pnum);
777 selector.matchVlanId(vidCriterion.vlanId());
778 selector.matchEthType(Ethernet.TYPE_IPV4);
779 selector.matchEthDst(ethCriterion.mac());
Saurav Das4ce45962015-11-24 23:21:05 -0800780 treatment.transition(UNICAST_ROUTING_TABLE);
781 FlowRule rule = DefaultFlowRule.builder()
782 .forDevice(deviceId)
783 .withSelector(selector.build())
784 .withTreatment(treatment.build())
785 .withPriority(DEFAULT_PRIORITY)
786 .fromApp(applicationId)
787 .makePermanent()
788 .forTable(TMAC_TABLE).build();
789 rules.add(rule);
Charles Chan0f43e472017-02-14 14:00:16 -0800790
791 // TMAC rules for MPLS packets
Saurav Das4ce45962015-11-24 23:21:05 -0800792 selector = DefaultTrafficSelector.builder();
793 treatment = DefaultTrafficTreatment.builder();
794 selector.matchInPort(pnum);
795 selector.matchVlanId(vidCriterion.vlanId());
796 selector.matchEthType(Ethernet.MPLS_UNICAST);
797 selector.matchEthDst(ethCriterion.mac());
Saurav Das4ce45962015-11-24 23:21:05 -0800798 treatment.transition(MPLS_TABLE_0);
799 rule = DefaultFlowRule.builder()
800 .forDevice(deviceId)
801 .withSelector(selector.build())
802 .withTreatment(treatment.build())
803 .withPriority(DEFAULT_PRIORITY)
804 .fromApp(applicationId)
805 .makePermanent()
806 .forTable(TMAC_TABLE).build();
807 rules.add(rule);
Charles Chan0f43e472017-02-14 14:00:16 -0800808
809 // TMAC rules for IPv6 packets
Pier Ventree0ae7a32016-11-23 09:57:42 -0800810 selector = DefaultTrafficSelector.builder();
811 treatment = DefaultTrafficTreatment.builder();
812 selector.matchInPort(pnum);
813 selector.matchVlanId(vidCriterion.vlanId());
814 selector.matchEthType(Ethernet.TYPE_IPV6);
815 selector.matchEthDst(ethCriterion.mac());
Pier Ventree0ae7a32016-11-23 09:57:42 -0800816 treatment.transition(UNICAST_ROUTING_TABLE);
817 rule = DefaultFlowRule.builder()
818 .forDevice(deviceId)
819 .withSelector(selector.build())
820 .withTreatment(treatment.build())
821 .withPriority(DEFAULT_PRIORITY)
822 .fromApp(applicationId)
823 .makePermanent()
824 .forTable(TMAC_TABLE).build();
825 rules.add(rule);
Saurav Das4ce45962015-11-24 23:21:05 -0800826 }
Charles Chan66291502018-03-02 16:43:28 -0800827 return ImmutableList.of(rules);
Saurav Das4ce45962015-11-24 23:21:05 -0800828 }
829
Charles Chan5270ed02016-01-30 23:22:37 -0800830 @Override
Charles Chan66291502018-03-02 16:43:28 -0800831 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700832 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800833 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
834 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
835 selector.matchEthType(Ethernet.TYPE_IPV4);
836 selector.matchEthDst(ethCriterion.mac());
Charles Chan5270ed02016-01-30 23:22:37 -0800837 treatment.transition(UNICAST_ROUTING_TABLE);
838 FlowRule rule = DefaultFlowRule.builder()
839 .forDevice(deviceId)
840 .withSelector(selector.build())
841 .withTreatment(treatment.build())
842 .withPriority(DEFAULT_PRIORITY)
843 .fromApp(applicationId)
844 .makePermanent()
845 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800846 return ImmutableList.of(ImmutableList.of(rule));
Charles Chan5270ed02016-01-30 23:22:37 -0800847 }
848
Saurav Das4ce45962015-11-24 23:21:05 -0800849 /*
Charles Chanab591602019-01-22 17:25:04 -0800850 * Open vSwitch emulation allows MPLS ECMP.
Saurav Das4ce45962015-11-24 23:21:05 -0800851 *
852 * (non-Javadoc)
853 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthTypeSpecific
854 */
855 @Override
856 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Andrea Campanella7c977b92018-05-30 21:39:45 -0700857 if (isDoubleTagged(fwd)) {
858 return processDoubleTaggedFwd(fwd);
859 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800860 TrafficSelector selector = fwd.selector();
861 EthTypeCriterion ethType =
862 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
863 if ((ethType == null) ||
864 (ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -0800865 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST) &&
866 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6)) {
Saurav Das4ce45962015-11-24 23:21:05 -0800867 log.warn("processSpecific: Unsupported forwarding objective criteria"
868 + "ethType:{} in dev:{}", ethType, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800869 fail(fwd, ObjectiveError.UNSUPPORTED);
870 return Collections.emptySet();
871 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800872 boolean defaultRule = false;
Charles Chan50d900c2018-03-02 13:26:22 -0800873 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800874 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800875 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
876
Saurav Das8a0732e2015-11-20 15:27:53 -0800877 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800878 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700879 if (ipv4Dst.isMulticast()) {
880 if (ipv4Dst.prefixLength() != 32) {
881 log.warn("Multicast specific forwarding objective can only be /32");
882 fail(fwd, ObjectiveError.BADPARAMS);
883 return ImmutableSet.of();
884 }
885 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
886 if (assignedVlan == null) {
887 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
888 fail(fwd, ObjectiveError.BADPARAMS);
889 return ImmutableSet.of();
890 }
891 filteredSelector.matchVlanId(assignedVlan);
892 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
893 forTableId = MULTICAST_ROUTING_TABLE;
894 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
895 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800896 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700897 if (ipv4Dst.prefixLength() == 0) {
898 // The entire IPV4_DST field is wildcarded intentionally
899 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700900 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700901 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700902 }
903 forTableId = UNICAST_ROUTING_TABLE;
904 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
905 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800906 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800907 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000908 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
909 if (ipv6Dst.isMulticast()) {
910 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
911 log.debug("Multicast specific IPv6 forwarding objective can only be /128");
912 fail(fwd, ObjectiveError.BADPARAMS);
913 return ImmutableSet.of();
914 }
915 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
916 if (assignedVlan == null) {
917 log.debug("VLAN ID required by multicast specific fwd obj is missing. Abort.");
918 fail(fwd, ObjectiveError.BADPARAMS);
919 return ImmutableSet.of();
920 }
921 filteredSelector.matchVlanId(assignedVlan);
922 filteredSelector.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
923 forTableId = MULTICAST_ROUTING_TABLE;
924 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
925 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
926 } else {
927 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
928 return Collections.emptyList();
929 }
930 forTableId = UNICAST_ROUTING_TABLE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800931 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800932 } else {
933 filteredSelector
934 .matchEthType(Ethernet.MPLS_UNICAST)
935 .matchMplsLabel(((MplsCriterion)
936 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
937 MplsBosCriterion bos = (MplsBosCriterion) selector
938 .getCriterion(Criterion.Type.MPLS_BOS);
939 if (bos != null) {
940 filteredSelector.matchMplsBos(bos.mplsBos());
941 }
942 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800943 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
944 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800945 }
946
947 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
948 if (fwd.treatment() != null) {
949 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chanab591602019-01-22 17:25:04 -0800950 if (i instanceof L3ModificationInstruction) {
Charles Chan40132b32017-01-22 00:19:37 -0800951 L3ModificationInstruction l3instr = (L3ModificationInstruction) i;
952 if (l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_IN) ||
953 l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_OUT)) {
954 continue;
955 }
956 }
Charles Chan7d10b162015-12-07 18:54:45 -0800957 /*
958 * NOTE: OF-DPA does not support immediate instruction in
959 * L3 unicast and MPLS table.
960 */
961 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800962 }
963 }
964
965 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800966 NextGroup next = getGroupForNextObjective(fwd.nextId());
967 if (next != null) {
968 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
969 // we only need the top level group's key to point the flow to it
970 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
971 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700972 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
973 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das423fe2b2015-12-04 10:52:59 -0800974 fail(fwd, ObjectiveError.GROUPMISSING);
975 return Collections.emptySet();
976 }
977 tb.deferred().group(group.id());
Saurav Das8a0732e2015-11-20 15:27:53 -0800978 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800979 }
980 tb.transition(ACL_TABLE);
981 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
982 .fromApp(fwd.appId())
983 .withPriority(fwd.priority())
984 .forDevice(deviceId)
985 .withSelector(filteredSelector.build())
986 .withTreatment(tb.build())
987 .forTable(forTableId);
988
989 if (fwd.permanent()) {
990 ruleBuilder.makePermanent();
991 } else {
992 ruleBuilder.makeTemporary(fwd.timeout());
993 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800994 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
995 flowRuleCollection.add(ruleBuilder.build());
996 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800997 flowRuleCollection.add(
998 defaultRoute(fwd, complementarySelector, forTableId, tb)
999 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001000 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1001 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001002 return flowRuleCollection;
Saurav Das8a0732e2015-11-20 15:27:53 -08001003 }
1004
Charles Chan1e492d32016-01-30 23:22:37 -08001005 @Override
1006 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1007 List<FlowRule> rules = new ArrayList<>();
1008
1009 // Build filtered selector
1010 TrafficSelector selector = fwd.selector();
1011 EthCriterion ethCriterion = (EthCriterion) selector
1012 .getCriterion(Criterion.Type.ETH_DST);
1013 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
Andrea Campanella7c977b92018-05-30 21:39:45 -07001014 .getCriterion(VLAN_VID);
Charles Chan1e492d32016-01-30 23:22:37 -08001015
1016 if (vlanIdCriterion == null) {
1017 log.warn("Forwarding objective for bridging requires vlan. Not "
1018 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1019 fail(fwd, ObjectiveError.BADPARAMS);
1020 return Collections.emptySet();
1021 }
1022
1023 TrafficSelector.Builder filteredSelectorBuilder =
1024 DefaultTrafficSelector.builder();
1025 // Do not match MacAddress for subnet broadcast entry
Charles Chand05f54b2017-02-13 11:56:54 -08001026 if (!ethCriterion.mac().equals(NONE) && !ethCriterion.mac().equals(BROADCAST)) {
Charles Chan1e492d32016-01-30 23:22:37 -08001027 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1028 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1029 fwd.id(), fwd.nextId(), deviceId);
1030 } else {
1031 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1032 + "in dev:{} for vlan:{}",
1033 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1034 }
1035 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1036 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1037
1038 if (fwd.treatment() != null) {
1039 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1040 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1041 }
1042
1043 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1044 if (fwd.nextId() != null) {
1045 NextGroup next = getGroupForNextObjective(fwd.nextId());
1046 if (next != null) {
1047 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1048 // we only need the top level group's key to point the flow to it
1049 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1050 if (group != null) {
1051 treatmentBuilder.deferred().group(group.id());
1052 } else {
1053 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1054 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1055 fail(fwd, ObjectiveError.GROUPMISSING);
1056 return Collections.emptySet();
1057 }
1058 }
1059 }
1060 treatmentBuilder.immediate().transition(ACL_TABLE);
1061 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1062
1063 // Build bridging table entries
1064 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1065 flowRuleBuilder.fromApp(fwd.appId())
1066 .withPriority(fwd.priority())
1067 .forDevice(deviceId)
1068 .withSelector(filteredSelector)
1069 .withTreatment(filteredTreatment)
1070 .forTable(BRIDGING_TABLE);
1071 if (fwd.permanent()) {
1072 flowRuleBuilder.makePermanent();
1073 } else {
1074 flowRuleBuilder.makeTemporary(fwd.timeout());
1075 }
1076 rules.add(flowRuleBuilder.build());
1077 return rules;
1078 }
1079
Saurav Das52025962016-01-28 22:30:01 -08001080 @Override
Charles Chanab591602019-01-22 17:25:04 -08001081 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das52025962016-01-28 22:30:01 -08001082 // XXX driver does not currently do type checking as per Tables 65-67 in
1083 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
1084 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1085 if (fwd.treatment() != null) {
1086 for (Instruction ins : fwd.treatment().allInstructions()) {
1087 if (ins instanceof OutputInstruction) {
1088 OutputInstruction o = (OutputInstruction) ins;
Charles Chanab591602019-01-22 17:25:04 -08001089 if (PortNumber.CONTROLLER.equals(o.port())) {
Charles Chan0f43e472017-02-14 14:00:16 -08001090 ttBuilder.transition(PUNT_TABLE);
Saurav Das52025962016-01-28 22:30:01 -08001091 } else {
1092 log.warn("Only allowed treatments in versatile forwarding "
1093 + "objectives are punts to the controller");
1094 }
Charles Chanf76de302018-06-15 18:54:18 -07001095 } else if (ins instanceof NoActionInstruction) {
1096 // No action is allowed and nothing needs to be done
Saurav Das52025962016-01-28 22:30:01 -08001097 } else {
1098 log.warn("Cannot process instruction in versatile fwd {}", ins);
1099 }
1100 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001101 if (fwd.treatment().clearedDeferred()) {
1102 ttBuilder.wipeDeferred();
1103 }
Saurav Das52025962016-01-28 22:30:01 -08001104 }
1105 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001106 // Override case
Saurav Das52025962016-01-28 22:30:01 -08001107 NextGroup next = getGroupForNextObjective(fwd.nextId());
Charles Chanab591602019-01-22 17:25:04 -08001108 if (next == null) {
1109 fail(fwd, ObjectiveError.BADPARAMS);
1110 return null;
1111 }
Saurav Das52025962016-01-28 22:30:01 -08001112 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1113 // we only need the top level group's key to point the flow to it
1114 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1115 if (group == null) {
1116 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001117 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das52025962016-01-28 22:30:01 -08001118 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001119 return null;
Saurav Das52025962016-01-28 22:30:01 -08001120 }
1121 ttBuilder.deferred().group(group.id());
1122 }
Charles Chanab591602019-01-22 17:25:04 -08001123 return ttBuilder;
Saurav Das52025962016-01-28 22:30:01 -08001124 }
1125
1126 /*
Charles Chanab591602019-01-22 17:25:04 -08001127 * Open vSwitch emulation requires table-miss-entries in forwarding tables.
Saurav Das52025962016-01-28 22:30:01 -08001128 * Real OFDPA does not require these rules as they are put in by default.
1129 *
1130 * (non-Javadoc)
1131 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#initializePipeline()
1132 */
Saurav Das2857f382015-11-03 14:39:27 -08001133 @Override
Saurav Das558afec2015-05-31 17:12:48 -07001134 protected void initializePipeline() {
Charles Chanf6ec1532017-02-08 16:10:40 -08001135 initTableMiss(PORT_TABLE, VLAN_TABLE, null);
1136 initTableMiss(VLAN_TABLE, ACL_TABLE, null);
Andrea Campanella7c977b92018-05-30 21:39:45 -07001137 initTableMiss(VLAN_1_TABLE, ACL_TABLE, null);
Charles Chanf6ec1532017-02-08 16:10:40 -08001138 initTableMiss(TMAC_TABLE, BRIDGING_TABLE, null);
1139 initTableMiss(UNICAST_ROUTING_TABLE, ACL_TABLE, null);
1140 initTableMiss(MULTICAST_ROUTING_TABLE, ACL_TABLE, null);
Andrea Campanella7c977b92018-05-30 21:39:45 -07001141 initTableMiss(EGRESS_VLAN_FLOW_TABLE_IN_INGRESS, ACL_TABLE, null);
1142 initTableMiss(UNICAST_ROUTING_TABLE_1, ACL_TABLE, null);
Charles Chanf6ec1532017-02-08 16:10:40 -08001143 initTableMiss(MPLS_TABLE_0, MPLS_TABLE_1, null);
1144 initTableMiss(MPLS_TABLE_1, ACL_TABLE, null);
1145 initTableMiss(BRIDGING_TABLE, ACL_TABLE, null);
1146 initTableMiss(ACL_TABLE, -1, null);
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001147 linkDiscoveryPuntTableRules();
Charles Chan0f43e472017-02-14 14:00:16 -08001148
Charles Chanab591602019-01-22 17:25:04 -08001149 initPopVlanPuntGroup();
Saurav Das558afec2015-05-31 17:12:48 -07001150 }
1151
Charles Chanf6ec1532017-02-08 16:10:40 -08001152 /**
1153 * Install table-miss flow entry.
1154 *
1155 * If treatment exists, use it directly.
1156 * Else if treatment does not exist but nextTable > 0, transit to next table.
1157 * Else apply empty treatment.
1158 *
1159 * @param thisTable this table ID
1160 * @param nextTable next table ID
1161 * @param treatment traffic treatment to apply.
1162 */
1163 private void initTableMiss(int thisTable, int nextTable, TrafficTreatment treatment) {
Saurav Das558afec2015-05-31 17:12:48 -07001164 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
Charles Chanf6ec1532017-02-08 16:10:40 -08001165 TrafficSelector selector = DefaultTrafficSelector.builder().build();
Saurav Das558afec2015-05-31 17:12:48 -07001166
Charles Chanf6ec1532017-02-08 16:10:40 -08001167 if (treatment == null) {
1168 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1169 if (nextTable > 0) {
1170 tBuilder.transition(nextTable);
Saurav Das558afec2015-05-31 17:12:48 -07001171 }
Charles Chanf6ec1532017-02-08 16:10:40 -08001172 treatment = tBuilder.build();
1173 }
Saurav Das558afec2015-05-31 17:12:48 -07001174
Charles Chanb7504392017-02-10 12:51:04 -08001175 FlowRule rule = DefaultFlowRule.builder()
1176 .forDevice(deviceId)
Charles Chanf6ec1532017-02-08 16:10:40 -08001177 .withSelector(selector)
1178 .withTreatment(treatment)
Charles Chanb7504392017-02-10 12:51:04 -08001179 .withPriority(LOWEST_PRIORITY)
1180 .fromApp(driverId)
1181 .makePermanent()
Charles Chanf6ec1532017-02-08 16:10:40 -08001182 .forTable(thisTable).build();
Charles Chanb7504392017-02-10 12:51:04 -08001183 ops = ops.add(rule);
Saurav Das2857f382015-11-03 14:39:27 -08001184
1185 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
1186 @Override
1187 public void onSuccess(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -08001188 log.info("Initialized table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -08001189 }
Saurav Das2857f382015-11-03 14:39:27 -08001190 @Override
1191 public void onError(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -08001192 log.warn("Failed to initialize table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -08001193 }
1194 }));
1195 }
Charles Chan0f43e472017-02-14 14:00:16 -08001196
1197 /**
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001198 * Install lldp/bbdp matching rules at table PUNT_TABLE
1199 * that forward traffic to controller.
1200 *
1201 */
1202 private void linkDiscoveryPuntTableRules() {
1203 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
1204 TrafficTreatment treatment = DefaultTrafficTreatment.builder().punt().build();
1205
1206 TrafficSelector.Builder lldpSelector = DefaultTrafficSelector.builder();
1207 lldpSelector.matchEthType(EthType.EtherType.LLDP.ethType().toShort());
1208 FlowRule lldpRule = DefaultFlowRule.builder()
1209 .forDevice(deviceId)
1210 .withSelector(lldpSelector.build())
1211 .withTreatment(treatment)
1212 .withPriority(HIGHEST_PRIORITY)
1213 .fromApp(driverId)
1214 .makePermanent()
1215 .forTable(PUNT_TABLE).build();
1216 ops = ops.add(lldpRule);
1217
1218 TrafficSelector.Builder bbdpSelector = DefaultTrafficSelector.builder();
1219 bbdpSelector.matchEthType(EthType.EtherType.BDDP.ethType().toShort());
1220 FlowRule bbdpRule = DefaultFlowRule.builder()
1221 .forDevice(deviceId)
1222 .withSelector(bbdpSelector.build())
1223 .withTreatment(treatment)
1224 .withPriority(HIGHEST_PRIORITY)
1225 .fromApp(driverId)
1226 .makePermanent()
1227 .forTable(PUNT_TABLE).build();
1228 ops.add(bbdpRule);
1229
1230 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
1231 @Override
1232 public void onSuccess(FlowRuleOperations ops) {
1233 log.info("Added lldp/bbdp rules for table {} on {}", PUNT_TABLE, deviceId);
1234 }
1235 @Override
1236 public void onError(FlowRuleOperations ops) {
1237 log.warn("Failed to initialize lldp/bbdp rules for table {} on {}", PUNT_TABLE, deviceId);
1238 }
1239 }));
1240 }
1241
1242 /**
Charles Chan0f43e472017-02-14 14:00:16 -08001243 * Builds a indirect group contains pop_vlan and punt actions.
1244 * <p>
1245 * Using group instead of immediate action to ensure that
1246 * the copy of packet on the data plane is not affected by the pop vlan action.
1247 */
1248 private void initPopVlanPuntGroup() {
Yi Tsengef19de12017-04-24 11:33:05 -07001249 GroupKey groupKey = popVlanPuntGroupKey();
Charles Chan0f43e472017-02-14 14:00:16 -08001250 TrafficTreatment bucketTreatment = DefaultTrafficTreatment.builder()
1251 .popVlan().punt().build();
1252 GroupBucket bucket =
1253 DefaultGroupBucket.createIndirectGroupBucket(bucketTreatment);
1254 GroupDescription groupDesc =
1255 new DefaultGroupDescription(
1256 deviceId,
1257 GroupDescription.Type.INDIRECT,
1258 new GroupBuckets(Collections.singletonList(bucket)),
1259 groupKey,
1260 POP_VLAN_PUNT_GROUP_ID,
1261 driverId);
1262 groupService.addGroup(groupDesc);
1263
1264 log.info("Initialized pop vlan punt group on {}", deviceId);
1265 }
Yi Tsengef19de12017-04-24 11:33:05 -07001266
1267 /**
1268 * Generates group key for a static indirect group that pop vlan and punt to
1269 * controller.
1270 *
1271 * @return the group key of the indirect table
1272 */
1273 private GroupKey popVlanPuntGroupKey() {
Charles Chan367c1c12018-10-19 16:23:28 -07001274 int hash = POP_VLAN_PUNT_GROUP_ID | (Objects.hash(deviceId) & FOUR_NIBBLE_MASK);
Yi Tsengef19de12017-04-24 11:33:05 -07001275 return new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(hash));
1276 }
Andreas Pantelopoulos89369462018-05-30 14:30:24 -07001277
1278 private class PopVlanPuntGroupChecker implements Runnable {
1279 @Override
1280 public void run() {
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001281 try {
1282 groupCheckerLock.lock();
1283 // this can happen outside of the lock but I think it is safer
1284 // to include it here.
1285 Group group = groupService.getGroup(deviceId, popVlanPuntGroupKey());
Charles Chan4a288d92019-02-16 00:11:18 +00001286 if (group != null) {
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001287 log.debug("PopVlanPuntGroupChecker: Installing {} missing rules at punt table.",
1288 flowRuleQueue.size());
Andreas Pantelopoulos89369462018-05-30 14:30:24 -07001289
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001290 // if we have pending flow rules install them
1291 if (flowRuleQueue.size() > 0) {
1292 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
1293 // we should not care about the context here, it can only be add
1294 // since when removing the rules the group should be there already.
1295 flowRuleQueue.forEach(ops::add);
1296 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
1297 @Override
1298 public void onSuccess(FlowRuleOperations ops) {
1299 log.debug("Applied {} pop vlan punt rules in device {}",
1300 ops.stages().get(0).size(), deviceId);
1301 }
1302
1303 @Override
1304 public void onError(FlowRuleOperations ops) {
1305 log.error("Failed to apply all pop vlan punt rules in dev {}", deviceId);
1306 }
1307 }));
1308 }
1309 // this signifies that the group is created and now
1310 // flow rules can be installed directly
1311 flowRuleQueue = null;
1312 // shutdown the group checker gracefully
Andreas Pantelopoulos89369462018-05-30 14:30:24 -07001313 groupChecker.shutdown();
1314 }
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001315 } finally {
1316 groupCheckerLock.unlock();
Andreas Pantelopoulos89369462018-05-30 14:30:24 -07001317 }
1318 }
1319 }
Saurav Das558afec2015-05-31 17:12:48 -07001320}