blob: 9799197fc732311f2d63b14dc5da497fb827bb3c [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
Harshada Chaundkarcb787512019-08-05 15:33:30 +0000143 protected boolean requireEthType() {
144 return false;
145 }
146
147 @Override
Charles Chan40132b32017-01-22 00:19:37 -0800148 protected void initDriverId() {
Charles Chan425854b2016-04-11 15:32:12 -0700149 driverId = coreService.registerApplication(
Charles Chanab591602019-01-22 17:25:04 -0800150 "org.onosproject.driver.OvsOfdpaPipeline");
Charles Chan40132b32017-01-22 00:19:37 -0800151 }
Charles Chan425854b2016-04-11 15:32:12 -0700152
Charles Chan40132b32017-01-22 00:19:37 -0800153 @Override
154 protected void initGroupHander(PipelinerContext context) {
Charles Chanab591602019-01-22 17:25:04 -0800155 groupHandler = new OvsOfdpaGroupHandler();
Charles Chan40132b32017-01-22 00:19:37 -0800156 groupHandler.init(deviceId, context);
Charles Chan425854b2016-04-11 15:32:12 -0700157 }
158
Andreas Pantelopoulos89369462018-05-30 14:30:24 -0700159 @Override
160 public void init(DeviceId deviceId, PipelinerContext context) {
pierf37ce522020-03-20 11:00:38 +0100161 synchronized (this) {
162 if (isReady()) {
163 return;
164 }
165
166 // Terminate internal references
167 // We are terminating the references here
168 // because when the device is offline the apps
169 // are still sending flowobjectives
170 if (groupChecker != null) {
171 groupChecker.shutdown();
172 }
pier1b7dd122020-02-28 09:24:11 +0100173 // create a new executor at each init and a new empty queue
174 groupChecker = Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/driver",
175 "ovs-ofdpa-%d", log));
pierf37ce522020-03-20 11:00:38 +0100176 if (flowRuleQueue != null) {
177 flowRuleQueue.clear();
178 }
pier1b7dd122020-02-28 09:24:11 +0100179 flowRuleQueue = new ConcurrentLinkedQueue<>();
180 groupCheckerLock = new ReentrantLock();
181 groupChecker.scheduleAtFixedRate(new PopVlanPuntGroupChecker(), 20, 50, TimeUnit.MILLISECONDS);
182 super.init(deviceId, context);
183 }
Andreas Pantelopoulos89369462018-05-30 14:30:24 -0700184 }
pier9469f3e2019-04-17 17:05:08 +0200185
Andrea Campanella7c977b92018-05-30 21:39:45 -0700186 protected void processFilter(FilteringObjective filteringObjective,
187 boolean install,
188 ApplicationId applicationId) {
pier9469f3e2019-04-17 17:05:08 +0200189 if (OfdpaPipelineUtility.isDoubleTagged(filteringObjective)) {
Andrea Campanella7c977b92018-05-30 21:39:45 -0700190 processDoubleTaggedFilter(filteringObjective, install, applicationId);
191 } else {
192 // If it is not a double-tagged filter, we fall back
193 // to the OFDPA 2.0 pipeline.
194 super.processFilter(filteringObjective, install, applicationId);
195 }
196 }
197
198 /**
Andrea Campanella7c977b92018-05-30 21:39:45 -0700199 * Determines if the forwarding objective will be used for double-tagged packets.
200 *
201 * @param fwd Forwarding objective
202 * @return True if the objective was created for double-tagged packets, false otherwise.
203 */
204 private boolean isDoubleTagged(ForwardingObjective fwd) {
205 if (fwd.nextId() != null) {
206 NextGroup next = getGroupForNextObjective(fwd.nextId());
207 if (next != null) {
208 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
209 // we only need the top level group's key
210 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
211 if (group != null) {
212 int groupId = group.id().id();
213 if (((groupId & ~TYPE_MASK) == L3_UNICAST_TYPE) &&
214 ((groupId & TYPE_L3UG_DOUBLE_VLAN_MASK) == TYPE_L3UG_DOUBLE_VLAN_MASK)) {
215 return true;
216 }
217 }
218 }
219 }
220 return false;
221 }
222
223 /**
224 * Configure filtering rules of outer and inner VLAN IDs, and a MAC address.
225 * Filtering happens in three tables (VLAN_TABLE, VLAN_1_TABLE, TMAC_TABLE).
226 *
227 * @param filteringObjective the filtering objective
228 * @param install true to add, false to remove
229 * @param applicationId for application programming this filter
230 */
231 private void processDoubleTaggedFilter(FilteringObjective filteringObjective,
232 boolean install,
233 ApplicationId applicationId) {
234 PortCriterion portCriterion = null;
235 EthCriterion ethCriterion = null;
236 VlanIdCriterion innervidCriterion = null;
237 VlanIdCriterion outerVidCriterion = null;
238 boolean popVlan = false;
239 TrafficTreatment meta = filteringObjective.meta();
240 if (!filteringObjective.key().equals(Criteria.dummy()) &&
241 filteringObjective.key().type() == Criterion.Type.IN_PORT) {
242 portCriterion = (PortCriterion) filteringObjective.key();
243 }
244 if (portCriterion == null) {
245 log.warn("No IN_PORT defined in filtering objective from app: {}" +
246 "Failed to program VLAN tables.", applicationId);
247 return;
248 } else {
249 log.debug("Received filtering objective for dev/port: {}/{}", deviceId,
250 portCriterion.port());
251 }
252
253 // meta should have only one instruction, popVlan.
254 if (meta != null && meta.allInstructions().size() == 1) {
255 L2ModificationInstruction l2Inst = (L2ModificationInstruction) meta.allInstructions().get(0);
256 if (l2Inst.subtype().equals(L2ModificationInstruction.L2SubType.VLAN_POP)) {
257 popVlan = true;
258 } else {
259 log.warn("Filtering objective can have only VLAN_POP instruction.");
260 return;
261 }
262 } else {
263 log.warn("Filtering objective should have one instruction.");
264 return;
265 }
266
267 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
268 for (Criterion criterion : filteringObjective.conditions()) {
269 switch (criterion.type()) {
270 case ETH_DST:
271 case ETH_DST_MASKED:
272 ethCriterion = (EthCriterion) criterion;
273 break;
274 case VLAN_VID:
Daniele Morofa382c22019-07-12 17:58:54 -0700275 outerVidCriterion = (VlanIdCriterion) criterion;
276 break;
277 case INNER_VLAN_VID:
278 innervidCriterion = (VlanIdCriterion) criterion;
Andrea Campanella7c977b92018-05-30 21:39:45 -0700279 break;
280 default:
281 log.warn("Unsupported filter {}", criterion);
282 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
283 return;
284 }
285 }
286
287 if (innervidCriterion == null || outerVidCriterion == null) {
288 log.warn("filtering objective should have two vidCriterion.");
289 return;
290 }
291
292 if (ethCriterion == null || ethCriterion.mac().equals(NONE)) {
293 // NOTE: it is possible that a filtering objective only has vidCriterion
294 log.warn("filtering objective missing dstMac, cannot program TMAC table");
295 return;
296 } else {
297 MacAddress unicastMac = readEthDstFromTreatment(filteringObjective.meta());
298 List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion, innervidCriterion,
299 innervidCriterion.vlanId(), unicastMac,
300 applicationId);
301 for (List<FlowRule> flowRules : allStages) {
302 log.trace("Starting a new flow rule stage for TMAC table flow");
303 ops.newStage();
304
305 for (FlowRule flowRule : flowRules) {
306 log.trace("{} flow rules in TMAC table: {} for dev: {}",
307 (install) ? "adding" : "removing", flowRules, deviceId);
308 if (install) {
309 ops = ops.add(flowRule);
310 } else {
311 // NOTE: Only remove TMAC flow when there is no more enabled port within the
312 // same VLAN on this device if TMAC doesn't support matching on in_port.
313 if (matchInPortTmacTable()
314 || (filteringObjective.meta() != null
315 && filteringObjective.meta().clearedDeferred())) {
316 ops = ops.remove(flowRule);
317 } else {
318 log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow");
319 }
320 }
321 }
322 }
323 }
324
325 List<FlowRule> rules;
326 rules = processDoubleVlanIdFilter(portCriterion, innervidCriterion,
327 outerVidCriterion, popVlan, applicationId);
328 for (FlowRule flowRule : rules) {
329 log.trace("{} flow rule in VLAN table: {} for dev: {}",
330 (install) ? "adding" : "removing", flowRule, deviceId);
331 ops = install ? ops.add(flowRule) : ops.remove(flowRule);
332 }
333
334 // apply filtering flow rules
335 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
336 @Override
337 public void onSuccess(FlowRuleOperations ops) {
338 log.debug("Applied {} filtering rules in device {}",
339 ops.stages().get(0).size(), deviceId);
340 pass(filteringObjective);
341 }
342
343 @Override
344 public void onError(FlowRuleOperations ops) {
345 log.info("Failed to apply all filtering rules in dev {}", deviceId);
346 fail(filteringObjective, ObjectiveError.FLOWINSTALLATIONFAILED);
347 }
348 }));
349
350 }
351 /**
352 * Internal implementation of processDoubleVlanIdFilter.
353 *
354 * @param portCriterion port on device for which this filter is programmed
355 * @param innerVidCriterion inner vlan
356 * @param outerVidCriterion outer vlan
357 * @param popVlan true if outer vlan header needs to be removed
358 * @param applicationId for application programming this filter
359 * @return flow rules for port-vlan filters
360 */
361 private List<FlowRule> processDoubleVlanIdFilter(PortCriterion portCriterion,
362 VlanIdCriterion innerVidCriterion,
363 VlanIdCriterion outerVidCriterion,
364 boolean popVlan,
365 ApplicationId applicationId) {
366 List<FlowRule> rules = new ArrayList<>();
367 TrafficSelector.Builder outerSelector = DefaultTrafficSelector.builder();
368 TrafficTreatment.Builder outerTreatment = DefaultTrafficTreatment.builder();
369 TrafficSelector.Builder innerSelector = DefaultTrafficSelector.builder();
370 TrafficTreatment.Builder innerTreatment = DefaultTrafficTreatment.builder();
371
372 VlanId outerVlanId = outerVidCriterion.vlanId();
373 VlanId innerVlanId = innerVidCriterion.vlanId();
374 PortNumber portNumber = portCriterion.port();
375 // Check arguments
376 if (PortNumber.ALL.equals(portNumber)
377 || outerVlanId.equals(VlanId.NONE)
378 || innerVlanId.equals(VlanId.NONE)) {
379 log.warn("Incomplete Filtering Objective. " +
380 "VLAN Table cannot be programmed for {}", deviceId);
381 return ImmutableList.of();
382 } else {
383 outerSelector.matchInPort(portNumber);
384 innerSelector.matchInPort(portNumber);
385 outerTreatment.transition(VLAN_1_TABLE);
386 innerTreatment.transition(TMAC_TABLE);
387 outerTreatment.writeMetadata(outerVlanId.toShort(), 0xFFF);
388
389 outerSelector.matchVlanId(outerVlanId);
390 innerSelector.matchVlanId(innerVlanId);
391 //force recompilation
392 //FIXME might be issue due tu /fff mask
393 innerSelector.matchMetadata(outerVlanId.toShort());
394
395 if (popVlan) {
396 outerTreatment.popVlan();
397 }
398 }
399
400 // NOTE: for double-tagged packets, restore original outer vlan
401 // before sending it to the controller.
Charles Chanab591602019-01-22 17:25:04 -0800402 GroupKey groupKey = popVlanPuntGroupKey();
403 Group group = groupService.getGroup(deviceId, groupKey);
404 if (group != null) {
405 // push outer vlan and send to controller
406 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
407 .matchInPort(portNumber)
408 .matchVlanId(innerVlanId);
409 Host host = handler().get(HostService.class).getConnectedHosts(ConnectPoint.
410 deviceConnectPoint(deviceId + "/" + portNumber.toLong())).stream().filter(h ->
411 h.vlan().equals(outerVlanId)).findFirst().orElse(null);
412 EthType vlanType = EthType.EtherType.VLAN.ethType();
413 if (host != null) {
414 vlanType = host.tpid();
Andrea Campanella7c977b92018-05-30 21:39:45 -0700415 }
Charles Chanab591602019-01-22 17:25:04 -0800416 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
417 .pushVlan(vlanType).setVlanId(outerVlanId).punt();
418
419 rules.add(DefaultFlowRule.builder()
420 .forDevice(deviceId)
421 .withSelector(sbuilder.build())
422 .withTreatment(tbuilder.build())
423 .withPriority(PacketPriority.CONTROL.priorityValue())
424 .fromApp(driverId)
425 .makePermanent()
426 .forTable(PUNT_TABLE).build());
427 } else {
428 log.info("popVlanPuntGroup not found in dev:{}", deviceId);
429 return Collections.emptyList();
Andrea Campanella7c977b92018-05-30 21:39:45 -0700430 }
Charles Chanab591602019-01-22 17:25:04 -0800431
Andrea Campanella7c977b92018-05-30 21:39:45 -0700432 FlowRule outerRule = DefaultFlowRule.builder()
433 .forDevice(deviceId)
434 .withSelector(outerSelector.build())
435 .withTreatment(outerTreatment.build())
436 .withPriority(DEFAULT_PRIORITY)
437 .fromApp(applicationId)
438 .makePermanent()
439 .forTable(VLAN_TABLE)
440 .build();
441 FlowRule innerRule = DefaultFlowRule.builder()
442 .forDevice(deviceId)
443 .withSelector(innerSelector.build())
444 .withTreatment(innerTreatment.build())
445 .withPriority(DEFAULT_PRIORITY)
446 .fromApp(applicationId)
447 .makePermanent()
448 .forTable(VLAN_1_TABLE)
449 .build();
450 rules.add(outerRule);
451 rules.add(innerRule);
452
453 return rules;
454 }
455
456 /**
457 * In the OF-DPA 2.0 pipeline, egress forwarding objectives go to the
458 * egress tables.
459 * @param fwd the forwarding objective of type 'egress'
460 * @return a collection of flow rules to be sent to the switch. An empty
461 * collection may be returned if there is a problem in processing
462 * the flow rule
463 */
464 @Override
465 protected Collection<FlowRule> processEgress(ForwardingObjective fwd) {
466 log.debug("Processing egress forwarding objective:{} in dev:{}",
467 fwd, deviceId);
468
469 List<FlowRule> rules = new ArrayList<>();
470
471 // Build selector
472 TrafficSelector.Builder sb = DefaultTrafficSelector.builder();
473 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) fwd.selector().getCriterion(Criterion.Type.VLAN_VID);
474 if (vlanIdCriterion == null) {
475 log.error("Egress forwarding objective:{} must include vlanId", fwd.id());
476 fail(fwd, ObjectiveError.BADPARAMS);
477 return rules;
478 }
479
480 Optional<Instruction> outInstr = fwd.treatment().allInstructions().stream()
481 .filter(instruction -> instruction instanceof Instructions.OutputInstruction).findFirst();
482 if (!outInstr.isPresent()) {
483 log.error("Egress forwarding objective:{} must include output port", fwd.id());
484 fail(fwd, ObjectiveError.BADPARAMS);
485 return rules;
486 }
487
488 PortNumber portNumber = ((Instructions.OutputInstruction) outInstr.get()).port();
489
490 sb.matchVlanId(vlanIdCriterion.vlanId());
491 OfdpaMatchActsetOutput actsetOutput = new OfdpaMatchActsetOutput(portNumber);
492 sb.extension(actsetOutput, deviceId);
493
494 // Build a flow rule for Egress VLAN Flow table
495 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
496 tb.transition(UNICAST_ROUTING_TABLE_1);
497 if (fwd.treatment() != null) {
498 for (Instruction instr : fwd.treatment().allInstructions()) {
499 if (instr instanceof L2ModificationInstruction &&
500 ((L2ModificationInstruction) instr).subtype() ==
501 L2ModificationInstruction.L2SubType.VLAN_ID) {
502 tb.immediate().add(instr);
503 }
504 if (instr instanceof L2ModificationInstruction &&
505 ((L2ModificationInstruction) instr).subtype() ==
506 L2ModificationInstruction.L2SubType.VLAN_PUSH) {
507 EthType ethType = ((L2ModificationInstruction.ModVlanHeaderInstruction) instr).ethernetType();
508 tb.immediate().pushVlan(ethType);
509 }
510 }
511 }
512
513 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
514 .fromApp(fwd.appId())
515 .withPriority(fwd.priority())
516 .forDevice(deviceId)
517 .withSelector(sb.build())
518 .withTreatment(tb.build())
519 .makePermanent()
520 .forTable(EGRESS_VLAN_FLOW_TABLE_IN_INGRESS);
521 rules.add(ruleBuilder.build());
522 return rules;
523 }
524
525 /**
526 * Handles forwarding rules to the IP Unicast Routing.
527 *
528 * @param fwd the forwarding objective
529 * @return A collection of flow rules, or an empty set
530 */
531 protected Collection<FlowRule> processDoubleTaggedFwd(ForwardingObjective fwd) {
532 // inner for UNICAST_ROUTING_TABLE_1, outer for UNICAST_ROUTING_TABLE
533 TrafficSelector selector = fwd.selector();
534 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
535 TrafficTreatment.Builder innerTtb = DefaultTrafficTreatment.builder();
536 TrafficTreatment.Builder outerTtb = DefaultTrafficTreatment.builder();
537
538 EthTypeCriterion ethType =
539 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
540
541 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
542 sBuilder.matchEthType(Ethernet.TYPE_IPV4);
543 sBuilder.matchVlanId(VlanId.ANY);
544 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
545 if (!ipv4Dst.isMulticast() && ipv4Dst.prefixLength() == 32) {
546 sBuilder.matchIPDst(ipv4Dst);
547 if (fwd.nextId() != null) {
548 NextGroup next = getGroupForNextObjective(fwd.nextId());
549 if (next != null) {
550 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
551 // we only need the top level group's key to point the flow to it
552 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
553 if (group == null) {
554 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
555 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
556 fail(fwd, ObjectiveError.GROUPMISSING);
557 return Collections.emptySet();
558 }
559 outerTtb.immediate().setVlanId(extractDummyVlanIdFromGroupId(group.id().id()));
560 //ACTSET_OUTPUT in OVS will match output action in write_action() set.
561 outerTtb.deferred().setOutput(extractOutputPortFromGroupId(group.id().id()));
562 outerTtb.transition(EGRESS_VLAN_FLOW_TABLE_IN_INGRESS);
563 innerTtb.deferred().group(group.id());
564 innerTtb.transition(ACL_TABLE);
565
566 FlowRule.Builder innerRuleBuilder = DefaultFlowRule.builder()
567 .fromApp(fwd.appId())
568 .withPriority(fwd.priority())
569 .forDevice(deviceId)
570 .withSelector(sBuilder.build())
571 .withTreatment(innerTtb.build())
572 .forTable(UNICAST_ROUTING_TABLE_1);
573 if (fwd.permanent()) {
574 innerRuleBuilder.makePermanent();
575 } else {
576 innerRuleBuilder.makeTemporary(fwd.timeout());
577 }
578 Collection<FlowRule> flowRuleCollection = new HashSet<>();
579 flowRuleCollection.add(innerRuleBuilder.build());
580
581 FlowRule.Builder outerRuleBuilder = DefaultFlowRule.builder()
582 .fromApp(fwd.appId())
583 .withPriority(fwd.priority())
584 .forDevice(deviceId)
585 .withSelector(sBuilder.build())
586 .withTreatment(outerTtb.build())
587 .forTable(UNICAST_ROUTING_TABLE);
588 if (fwd.permanent()) {
589 outerRuleBuilder.makePermanent();
590 } else {
591 outerRuleBuilder.makeTemporary(fwd.timeout());
592 }
593 flowRuleCollection.add(innerRuleBuilder.build());
594 flowRuleCollection.add(outerRuleBuilder.build());
595 return flowRuleCollection;
596 } else {
597 log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}",
598 fwd.nextId(), deviceId, fwd.id());
599 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
600 return Collections.emptySet();
601 }
602 } else {
603 log.warn("NextId is not specified in fwd:{}", fwd.id());
604 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
605 return Collections.emptySet();
606 }
607 }
608 }
609 return Collections.emptySet();
610 }
611
612 private static VlanId extractDummyVlanIdFromGroupId(int groupId) {
613 short vlanId = (short) ((groupId & 0x7FF8000) >> 15);
614 return VlanId.vlanId(vlanId);
615 }
616
617 private static PortNumber extractOutputPortFromGroupId(int groupId) {
618 return PortNumber.portNumber(groupId & 0x7FFF);
619 }
620
Saurav Das4ce45962015-11-24 23:21:05 -0800621 /*
Charles Chanab591602019-01-22 17:25:04 -0800622 * Open vSwitch emulation does not require the non OF-standard rules for
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700623 * matching untagged packets that ofdpa uses.
Saurav Das4ce45962015-11-24 23:21:05 -0800624 *
625 * (non-Javadoc)
626 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processVlanIdFilter
627 */
Saurav Das558afec2015-05-31 17:12:48 -0700628 @Override
Charles Chan66291502018-03-02 16:43:28 -0800629 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
pier6aef5b72019-06-10 17:10:26 +0200630 VlanIdCriterion vidCriterion,
631 VlanId assignedVlan,
632 ApplicationId applicationId,
633 boolean install) {
Charles Chan79769232016-07-05 16:34:39 -0700634 List<FlowRule> rules = new ArrayList<>();
Saurav Das2857f382015-11-03 14:39:27 -0800635 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
636 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
637 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800638 treatment.transition(TMAC_TABLE);
639
Saurav Das2857f382015-11-03 14:39:27 -0800640 if (vidCriterion.vlanId() == VlanId.NONE) {
641 // untagged packets are assigned vlans
642 treatment.pushVlan().setVlanId(assignedVlan);
Piere5bff482018-03-07 11:42:50 +0100643 } else if (!vidCriterion.vlanId().equals(assignedVlan)) {
644 // Rewrite with assigned vlans
645 treatment.setVlanId(assignedVlan);
Saurav Das2857f382015-11-03 14:39:27 -0800646 }
Saurav Das2857f382015-11-03 14:39:27 -0800647
648 // ofdpa cannot match on ALL portnumber, so we need to use separate
649 // rules for each port.
Charles Chan50d900c2018-03-02 13:26:22 -0800650 List<PortNumber> portnums = new ArrayList<>();
Ray Milkeybe9f3bc2018-05-10 12:42:51 -0700651 if (portCriterion != null) {
652 if (portCriterion.port() == PortNumber.ALL) {
653 for (Port port : deviceService.getPorts(deviceId)) {
654 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
655 portnums.add(port.number());
656 }
Saurav Das2857f382015-11-03 14:39:27 -0800657 }
Ray Milkeybe9f3bc2018-05-10 12:42:51 -0700658 } else {
659 portnums.add(portCriterion.port());
Saurav Das2857f382015-11-03 14:39:27 -0800660 }
Saurav Das2857f382015-11-03 14:39:27 -0800661 }
Saurav Das4f980082015-11-05 13:39:15 -0800662
Saurav Das2857f382015-11-03 14:39:27 -0800663 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800664 // NOTE: Emulating OFDPA behavior by popping off internal assigned
665 // VLAN before sending to controller
Charles Chanab591602019-01-22 17:25:04 -0800666 if (vidCriterion.vlanId() == VlanId.NONE) {
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700667 try {
668 groupCheckerLock.lock();
669 if (flowRuleQueue == null) {
670 // this means that the group has been created
671 // and that groupChecker has destroyed the queue
672 log.debug("Installing punt table rule for untagged port {} and vlan {}.",
673 pnum, assignedVlan);
674 rules.add(buildPuntTableRule(pnum, assignedVlan));
675 } else {
676 // The VLAN punt group may be held back due to device initial audit.
677 // In that case, we queue all punt table flow until the group has been created.
678 log.debug("popVlanPuntGroup not found in dev:{}, queueing this flow rule.", deviceId);
679 flowRuleQueue.add(buildPuntTableRule(pnum, assignedVlan));
Andreas Pantelopoulos89369462018-05-30 14:30:24 -0700680 }
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700681 } finally {
682 groupCheckerLock.unlock();
Charles Chan0f43e472017-02-14 14:00:16 -0800683 }
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700684 } else if (vidCriterion.vlanId() != VlanId.NONE) {
685 // for tagged ports just forward to the controller
686 log.debug("Installing punt rule for tagged port {} and vlan {}.", pnum, vidCriterion.vlanId());
687 rules.add(buildPuntTableRuleTagged(pnum, vidCriterion.vlanId()));
Charles Chan0f43e472017-02-14 14:00:16 -0800688 }
689
Saurav Das4f980082015-11-05 13:39:15 -0800690 // create rest of flowrule
Saurav Das2857f382015-11-03 14:39:27 -0800691 selector.matchInPort(pnum);
692 FlowRule rule = DefaultFlowRule.builder()
693 .forDevice(deviceId)
694 .withSelector(selector.build())
695 .withTreatment(treatment.build())
696 .withPriority(DEFAULT_PRIORITY)
697 .fromApp(applicationId)
698 .makePermanent()
699 .forTable(VLAN_TABLE).build();
700 rules.add(rule);
701 }
Charles Chanf57a8252016-06-29 19:12:37 -0700702
Charles Chan66291502018-03-02 16:43:28 -0800703 return ImmutableList.of(rules);
Saurav Das2857f382015-11-03 14:39:27 -0800704 }
705
Pier Ventree0ae7a32016-11-23 09:57:42 -0800706 /**
Charles Chan0f43e472017-02-14 14:00:16 -0800707 * Creates punt table entry that matches IN_PORT and VLAN_VID and points to
708 * a group that pop vlan and punt.
709 *
710 * @param portNumber port number
711 * @param assignedVlan internally assigned vlan id
712 * @return punt table flow rule
713 */
714 private FlowRule buildPuntTableRule(PortNumber portNumber, VlanId assignedVlan) {
715 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
716 .matchInPort(portNumber)
717 .matchVlanId(assignedVlan);
718 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
Yi Tsengfa394de2017-02-01 11:26:40 -0800719 .group(new GroupId(POP_VLAN_PUNT_GROUP_ID));
Charles Chan0f43e472017-02-14 14:00:16 -0800720
721 return DefaultFlowRule.builder()
722 .forDevice(deviceId)
723 .withSelector(sbuilder.build())
724 .withTreatment(tbuilder.build())
725 .withPriority(PacketPriority.CONTROL.priorityValue())
726 .fromApp(driverId)
727 .makePermanent()
728 .forTable(PUNT_TABLE).build();
729 }
730
731 /**
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -0700732 * Creates punt table entry that matches IN_PORT and VLAN_VID and forwards
733 * packet to controller tagged.
734 *
735 * @param portNumber port number
736 * @param packetVlan vlan tag of the packet
737 * @return punt table flow rule
738 */
739 private FlowRule buildPuntTableRuleTagged(PortNumber portNumber, VlanId packetVlan) {
740 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
741 .matchInPort(portNumber)
742 .matchVlanId(packetVlan);
743 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder().punt();
744
745 return DefaultFlowRule.builder()
746 .forDevice(deviceId)
747 .withSelector(sbuilder.build())
748 .withTreatment(tbuilder.build())
749 .withPriority(PacketPriority.CONTROL.priorityValue())
750 .fromApp(driverId)
751 .makePermanent()
752 .forTable(PUNT_TABLE).build();
753 }
754
Saurav Das8a0732e2015-11-20 15:27:53 -0800755 @Override
Charles Chan66291502018-03-02 16:43:28 -0800756 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das4ce45962015-11-24 23:21:05 -0800757 EthCriterion ethCriterion,
758 VlanIdCriterion vidCriterion,
759 VlanId assignedVlan,
Charles Chan45b69ab2018-03-02 15:41:41 -0800760 MacAddress unicastMac,
Saurav Das4ce45962015-11-24 23:21:05 -0800761 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800762 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
763 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
764 return processEthDstOnlyFilter(ethCriterion, applicationId);
765 }
766
Charles Chan5b9df8d2016-03-28 22:21:40 -0700767 // Multicast MAC
768 if (ethCriterion.mask() != null) {
Charles Chan45b69ab2018-03-02 15:41:41 -0800769 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700770 }
771
Saurav Das4ce45962015-11-24 23:21:05 -0800772 //handling untagged packets via assigned VLAN
773 if (vidCriterion.vlanId() == VlanId.NONE) {
774 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
775 }
776 // ofdpa cannot match on ALL portnumber, so we need to use separate
777 // rules for each port.
Charles Chan50d900c2018-03-02 13:26:22 -0800778 List<PortNumber> portnums = new ArrayList<>();
Ray Milkey74e59132018-01-17 15:24:52 -0800779 if (portCriterion != null) {
780 if (portCriterion.port() == PortNumber.ALL) {
781 for (Port port : deviceService.getPorts(deviceId)) {
782 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
783 portnums.add(port.number());
784 }
Saurav Das4ce45962015-11-24 23:21:05 -0800785 }
Ray Milkey74e59132018-01-17 15:24:52 -0800786 } else {
787 portnums.add(portCriterion.port());
Saurav Das4ce45962015-11-24 23:21:05 -0800788 }
Saurav Das4ce45962015-11-24 23:21:05 -0800789 }
790
Charles Chan50d900c2018-03-02 13:26:22 -0800791 List<FlowRule> rules = new ArrayList<>();
Saurav Das4ce45962015-11-24 23:21:05 -0800792 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800793 // TMAC rules for unicast IP packets
Saurav Das4ce45962015-11-24 23:21:05 -0800794 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
795 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
796 selector.matchInPort(pnum);
797 selector.matchVlanId(vidCriterion.vlanId());
798 selector.matchEthType(Ethernet.TYPE_IPV4);
799 selector.matchEthDst(ethCriterion.mac());
Saurav Das4ce45962015-11-24 23:21:05 -0800800 treatment.transition(UNICAST_ROUTING_TABLE);
801 FlowRule rule = DefaultFlowRule.builder()
802 .forDevice(deviceId)
803 .withSelector(selector.build())
804 .withTreatment(treatment.build())
805 .withPriority(DEFAULT_PRIORITY)
806 .fromApp(applicationId)
807 .makePermanent()
808 .forTable(TMAC_TABLE).build();
809 rules.add(rule);
Charles Chan0f43e472017-02-14 14:00:16 -0800810
811 // TMAC rules for MPLS packets
Saurav Das4ce45962015-11-24 23:21:05 -0800812 selector = DefaultTrafficSelector.builder();
813 treatment = DefaultTrafficTreatment.builder();
814 selector.matchInPort(pnum);
815 selector.matchVlanId(vidCriterion.vlanId());
816 selector.matchEthType(Ethernet.MPLS_UNICAST);
817 selector.matchEthDst(ethCriterion.mac());
Saurav Das4ce45962015-11-24 23:21:05 -0800818 treatment.transition(MPLS_TABLE_0);
819 rule = DefaultFlowRule.builder()
820 .forDevice(deviceId)
821 .withSelector(selector.build())
822 .withTreatment(treatment.build())
823 .withPriority(DEFAULT_PRIORITY)
824 .fromApp(applicationId)
825 .makePermanent()
826 .forTable(TMAC_TABLE).build();
827 rules.add(rule);
Charles Chan0f43e472017-02-14 14:00:16 -0800828
829 // TMAC rules for IPv6 packets
Pier Ventree0ae7a32016-11-23 09:57:42 -0800830 selector = DefaultTrafficSelector.builder();
831 treatment = DefaultTrafficTreatment.builder();
832 selector.matchInPort(pnum);
833 selector.matchVlanId(vidCriterion.vlanId());
834 selector.matchEthType(Ethernet.TYPE_IPV6);
835 selector.matchEthDst(ethCriterion.mac());
Pier Ventree0ae7a32016-11-23 09:57:42 -0800836 treatment.transition(UNICAST_ROUTING_TABLE);
837 rule = DefaultFlowRule.builder()
838 .forDevice(deviceId)
839 .withSelector(selector.build())
840 .withTreatment(treatment.build())
841 .withPriority(DEFAULT_PRIORITY)
842 .fromApp(applicationId)
843 .makePermanent()
844 .forTable(TMAC_TABLE).build();
845 rules.add(rule);
Saurav Das4ce45962015-11-24 23:21:05 -0800846 }
Charles Chan66291502018-03-02 16:43:28 -0800847 return ImmutableList.of(rules);
Saurav Das4ce45962015-11-24 23:21:05 -0800848 }
849
Charles Chan5270ed02016-01-30 23:22:37 -0800850 @Override
Charles Chan66291502018-03-02 16:43:28 -0800851 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700852 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800853 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
854 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
855 selector.matchEthType(Ethernet.TYPE_IPV4);
856 selector.matchEthDst(ethCriterion.mac());
Charles Chan5270ed02016-01-30 23:22:37 -0800857 treatment.transition(UNICAST_ROUTING_TABLE);
858 FlowRule rule = DefaultFlowRule.builder()
859 .forDevice(deviceId)
860 .withSelector(selector.build())
861 .withTreatment(treatment.build())
862 .withPriority(DEFAULT_PRIORITY)
863 .fromApp(applicationId)
864 .makePermanent()
865 .forTable(TMAC_TABLE).build();
Charles Chan66291502018-03-02 16:43:28 -0800866 return ImmutableList.of(ImmutableList.of(rule));
Charles Chan5270ed02016-01-30 23:22:37 -0800867 }
868
Saurav Das4ce45962015-11-24 23:21:05 -0800869 /*
Charles Chanab591602019-01-22 17:25:04 -0800870 * Open vSwitch emulation allows MPLS ECMP.
Saurav Das4ce45962015-11-24 23:21:05 -0800871 *
872 * (non-Javadoc)
873 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthTypeSpecific
874 */
875 @Override
876 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Andrea Campanella7c977b92018-05-30 21:39:45 -0700877 if (isDoubleTagged(fwd)) {
878 return processDoubleTaggedFwd(fwd);
879 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800880 TrafficSelector selector = fwd.selector();
881 EthTypeCriterion ethType =
882 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
883 if ((ethType == null) ||
884 (ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -0800885 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST) &&
886 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6)) {
Saurav Das4ce45962015-11-24 23:21:05 -0800887 log.warn("processSpecific: Unsupported forwarding objective criteria"
888 + "ethType:{} in dev:{}", ethType, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800889 fail(fwd, ObjectiveError.UNSUPPORTED);
890 return Collections.emptySet();
891 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800892 boolean defaultRule = false;
Charles Chan50d900c2018-03-02 13:26:22 -0800893 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800894 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800895 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
896
Saurav Das8a0732e2015-11-20 15:27:53 -0800897 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800898 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700899 if (ipv4Dst.isMulticast()) {
900 if (ipv4Dst.prefixLength() != 32) {
901 log.warn("Multicast specific forwarding objective can only be /32");
902 fail(fwd, ObjectiveError.BADPARAMS);
903 return ImmutableSet.of();
904 }
905 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
906 if (assignedVlan == null) {
907 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
908 fail(fwd, ObjectiveError.BADPARAMS);
909 return ImmutableSet.of();
910 }
911 filteredSelector.matchVlanId(assignedVlan);
912 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
913 forTableId = MULTICAST_ROUTING_TABLE;
914 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
915 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800916 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700917 if (ipv4Dst.prefixLength() == 0) {
918 // The entire IPV4_DST field is wildcarded intentionally
919 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700920 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700921 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700922 }
923 forTableId = UNICAST_ROUTING_TABLE;
924 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
925 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800926 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800927 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000928 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
929 if (ipv6Dst.isMulticast()) {
930 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
931 log.debug("Multicast specific IPv6 forwarding objective can only be /128");
932 fail(fwd, ObjectiveError.BADPARAMS);
933 return ImmutableSet.of();
934 }
935 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
936 if (assignedVlan == null) {
937 log.debug("VLAN ID required by multicast specific fwd obj is missing. Abort.");
938 fail(fwd, ObjectiveError.BADPARAMS);
939 return ImmutableSet.of();
940 }
941 filteredSelector.matchVlanId(assignedVlan);
942 filteredSelector.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
943 forTableId = MULTICAST_ROUTING_TABLE;
944 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
945 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
946 } else {
947 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
948 return Collections.emptyList();
949 }
950 forTableId = UNICAST_ROUTING_TABLE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800951 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800952 } else {
953 filteredSelector
954 .matchEthType(Ethernet.MPLS_UNICAST)
955 .matchMplsLabel(((MplsCriterion)
956 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
957 MplsBosCriterion bos = (MplsBosCriterion) selector
958 .getCriterion(Criterion.Type.MPLS_BOS);
959 if (bos != null) {
960 filteredSelector.matchMplsBos(bos.mplsBos());
961 }
962 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800963 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
964 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800965 }
966
967 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
968 if (fwd.treatment() != null) {
969 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chanab591602019-01-22 17:25:04 -0800970 if (i instanceof L3ModificationInstruction) {
Charles Chan40132b32017-01-22 00:19:37 -0800971 L3ModificationInstruction l3instr = (L3ModificationInstruction) i;
972 if (l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_IN) ||
973 l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_OUT)) {
974 continue;
975 }
976 }
Charles Chan7d10b162015-12-07 18:54:45 -0800977 /*
978 * NOTE: OF-DPA does not support immediate instruction in
979 * L3 unicast and MPLS table.
980 */
981 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800982 }
983 }
984
985 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800986 NextGroup next = getGroupForNextObjective(fwd.nextId());
987 if (next != null) {
988 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
989 // we only need the top level group's key to point the flow to it
990 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
991 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700992 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
993 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das423fe2b2015-12-04 10:52:59 -0800994 fail(fwd, ObjectiveError.GROUPMISSING);
995 return Collections.emptySet();
996 }
997 tb.deferred().group(group.id());
Saurav Das8a0732e2015-11-20 15:27:53 -0800998 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800999 }
1000 tb.transition(ACL_TABLE);
1001 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
1002 .fromApp(fwd.appId())
1003 .withPriority(fwd.priority())
1004 .forDevice(deviceId)
1005 .withSelector(filteredSelector.build())
1006 .withTreatment(tb.build())
1007 .forTable(forTableId);
1008
1009 if (fwd.permanent()) {
1010 ruleBuilder.makePermanent();
1011 } else {
1012 ruleBuilder.makeTemporary(fwd.timeout());
1013 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001014 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
1015 flowRuleCollection.add(ruleBuilder.build());
1016 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -08001017 flowRuleCollection.add(
1018 defaultRoute(fwd, complementarySelector, forTableId, tb)
1019 );
Flavio Castroe10fa242016-01-15 12:43:51 -08001020 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
1021 }
Flavio Castroe10fa242016-01-15 12:43:51 -08001022 return flowRuleCollection;
Saurav Das8a0732e2015-11-20 15:27:53 -08001023 }
1024
Charles Chan1e492d32016-01-30 23:22:37 -08001025 @Override
1026 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
1027 List<FlowRule> rules = new ArrayList<>();
1028
1029 // Build filtered selector
1030 TrafficSelector selector = fwd.selector();
1031 EthCriterion ethCriterion = (EthCriterion) selector
1032 .getCriterion(Criterion.Type.ETH_DST);
1033 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
Andrea Campanella7c977b92018-05-30 21:39:45 -07001034 .getCriterion(VLAN_VID);
Charles Chan1e492d32016-01-30 23:22:37 -08001035
1036 if (vlanIdCriterion == null) {
1037 log.warn("Forwarding objective for bridging requires vlan. Not "
1038 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
1039 fail(fwd, ObjectiveError.BADPARAMS);
1040 return Collections.emptySet();
1041 }
1042
1043 TrafficSelector.Builder filteredSelectorBuilder =
1044 DefaultTrafficSelector.builder();
1045 // Do not match MacAddress for subnet broadcast entry
Charles Chand05f54b2017-02-13 11:56:54 -08001046 if (!ethCriterion.mac().equals(NONE) && !ethCriterion.mac().equals(BROADCAST)) {
Charles Chan1e492d32016-01-30 23:22:37 -08001047 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
1048 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
1049 fwd.id(), fwd.nextId(), deviceId);
1050 } else {
1051 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
1052 + "in dev:{} for vlan:{}",
1053 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
1054 }
1055 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
1056 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
1057
1058 if (fwd.treatment() != null) {
1059 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
1060 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
1061 }
1062
1063 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
1064 if (fwd.nextId() != null) {
1065 NextGroup next = getGroupForNextObjective(fwd.nextId());
1066 if (next != null) {
1067 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1068 // we only need the top level group's key to point the flow to it
1069 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1070 if (group != null) {
1071 treatmentBuilder.deferred().group(group.id());
1072 } else {
1073 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
1074 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
1075 fail(fwd, ObjectiveError.GROUPMISSING);
1076 return Collections.emptySet();
1077 }
1078 }
1079 }
1080 treatmentBuilder.immediate().transition(ACL_TABLE);
1081 TrafficTreatment filteredTreatment = treatmentBuilder.build();
1082
1083 // Build bridging table entries
1084 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
1085 flowRuleBuilder.fromApp(fwd.appId())
1086 .withPriority(fwd.priority())
1087 .forDevice(deviceId)
1088 .withSelector(filteredSelector)
1089 .withTreatment(filteredTreatment)
1090 .forTable(BRIDGING_TABLE);
1091 if (fwd.permanent()) {
1092 flowRuleBuilder.makePermanent();
1093 } else {
1094 flowRuleBuilder.makeTemporary(fwd.timeout());
1095 }
1096 rules.add(flowRuleBuilder.build());
1097 return rules;
1098 }
1099
Saurav Das52025962016-01-28 22:30:01 -08001100 @Override
Charles Chanab591602019-01-22 17:25:04 -08001101 protected TrafficTreatment.Builder versatileTreatmentBuilder(ForwardingObjective fwd) {
Saurav Das52025962016-01-28 22:30:01 -08001102 // XXX driver does not currently do type checking as per Tables 65-67 in
1103 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
1104 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
1105 if (fwd.treatment() != null) {
1106 for (Instruction ins : fwd.treatment().allInstructions()) {
1107 if (ins instanceof OutputInstruction) {
1108 OutputInstruction o = (OutputInstruction) ins;
Charles Chanab591602019-01-22 17:25:04 -08001109 if (PortNumber.CONTROLLER.equals(o.port())) {
Charles Chan0f43e472017-02-14 14:00:16 -08001110 ttBuilder.transition(PUNT_TABLE);
Saurav Das52025962016-01-28 22:30:01 -08001111 } else {
1112 log.warn("Only allowed treatments in versatile forwarding "
1113 + "objectives are punts to the controller");
1114 }
Charles Chanf76de302018-06-15 18:54:18 -07001115 } else if (ins instanceof NoActionInstruction) {
1116 // No action is allowed and nothing needs to be done
Saurav Das52025962016-01-28 22:30:01 -08001117 } else {
1118 log.warn("Cannot process instruction in versatile fwd {}", ins);
1119 }
1120 }
Charles Chan2df0e8a2017-01-09 11:45:08 -08001121 if (fwd.treatment().clearedDeferred()) {
1122 ttBuilder.wipeDeferred();
1123 }
Saurav Das52025962016-01-28 22:30:01 -08001124 }
1125 if (fwd.nextId() != null) {
Charles Chanab591602019-01-22 17:25:04 -08001126 // Override case
Saurav Das52025962016-01-28 22:30:01 -08001127 NextGroup next = getGroupForNextObjective(fwd.nextId());
Charles Chanab591602019-01-22 17:25:04 -08001128 if (next == null) {
1129 fail(fwd, ObjectiveError.BADPARAMS);
1130 return null;
1131 }
Saurav Das52025962016-01-28 22:30:01 -08001132 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
1133 // we only need the top level group's key to point the flow to it
1134 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
1135 if (group == null) {
1136 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
Charles Chanab591602019-01-22 17:25:04 -08001137 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das52025962016-01-28 22:30:01 -08001138 fail(fwd, ObjectiveError.GROUPMISSING);
Charles Chanab591602019-01-22 17:25:04 -08001139 return null;
Saurav Das52025962016-01-28 22:30:01 -08001140 }
1141 ttBuilder.deferred().group(group.id());
1142 }
Charles Chanab591602019-01-22 17:25:04 -08001143 return ttBuilder;
Saurav Das52025962016-01-28 22:30:01 -08001144 }
1145
1146 /*
Charles Chanab591602019-01-22 17:25:04 -08001147 * Open vSwitch emulation requires table-miss-entries in forwarding tables.
Saurav Das52025962016-01-28 22:30:01 -08001148 * Real OFDPA does not require these rules as they are put in by default.
1149 *
1150 * (non-Javadoc)
1151 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#initializePipeline()
1152 */
Saurav Das2857f382015-11-03 14:39:27 -08001153 @Override
Saurav Das558afec2015-05-31 17:12:48 -07001154 protected void initializePipeline() {
Charles Chanf6ec1532017-02-08 16:10:40 -08001155 initTableMiss(PORT_TABLE, VLAN_TABLE, null);
1156 initTableMiss(VLAN_TABLE, ACL_TABLE, null);
Andrea Campanella7c977b92018-05-30 21:39:45 -07001157 initTableMiss(VLAN_1_TABLE, ACL_TABLE, null);
Charles Chanf6ec1532017-02-08 16:10:40 -08001158 initTableMiss(TMAC_TABLE, BRIDGING_TABLE, null);
1159 initTableMiss(UNICAST_ROUTING_TABLE, ACL_TABLE, null);
1160 initTableMiss(MULTICAST_ROUTING_TABLE, ACL_TABLE, null);
Andrea Campanella7c977b92018-05-30 21:39:45 -07001161 initTableMiss(EGRESS_VLAN_FLOW_TABLE_IN_INGRESS, ACL_TABLE, null);
1162 initTableMiss(UNICAST_ROUTING_TABLE_1, ACL_TABLE, null);
Charles Chanf6ec1532017-02-08 16:10:40 -08001163 initTableMiss(MPLS_TABLE_0, MPLS_TABLE_1, null);
1164 initTableMiss(MPLS_TABLE_1, ACL_TABLE, null);
1165 initTableMiss(BRIDGING_TABLE, ACL_TABLE, null);
1166 initTableMiss(ACL_TABLE, -1, null);
Charles Chan7f9d8462019-10-17 20:09:45 -07001167 initPuntTable();
Charles Chan0f43e472017-02-14 14:00:16 -08001168
Charles Chanab591602019-01-22 17:25:04 -08001169 initPopVlanPuntGroup();
Saurav Das558afec2015-05-31 17:12:48 -07001170 }
1171
Charles Chanf6ec1532017-02-08 16:10:40 -08001172 /**
1173 * Install table-miss flow entry.
1174 *
1175 * If treatment exists, use it directly.
1176 * Else if treatment does not exist but nextTable > 0, transit to next table.
1177 * Else apply empty treatment.
1178 *
1179 * @param thisTable this table ID
1180 * @param nextTable next table ID
1181 * @param treatment traffic treatment to apply.
1182 */
1183 private void initTableMiss(int thisTable, int nextTable, TrafficTreatment treatment) {
Saurav Das558afec2015-05-31 17:12:48 -07001184 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
Charles Chanf6ec1532017-02-08 16:10:40 -08001185 TrafficSelector selector = DefaultTrafficSelector.builder().build();
Saurav Das558afec2015-05-31 17:12:48 -07001186
Charles Chanf6ec1532017-02-08 16:10:40 -08001187 if (treatment == null) {
1188 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
1189 if (nextTable > 0) {
1190 tBuilder.transition(nextTable);
Saurav Das558afec2015-05-31 17:12:48 -07001191 }
Charles Chanf6ec1532017-02-08 16:10:40 -08001192 treatment = tBuilder.build();
1193 }
Saurav Das558afec2015-05-31 17:12:48 -07001194
Charles Chanb7504392017-02-10 12:51:04 -08001195 FlowRule rule = DefaultFlowRule.builder()
1196 .forDevice(deviceId)
Charles Chanf6ec1532017-02-08 16:10:40 -08001197 .withSelector(selector)
1198 .withTreatment(treatment)
Charles Chanb7504392017-02-10 12:51:04 -08001199 .withPriority(LOWEST_PRIORITY)
1200 .fromApp(driverId)
1201 .makePermanent()
Charles Chanf6ec1532017-02-08 16:10:40 -08001202 .forTable(thisTable).build();
Charles Chanb7504392017-02-10 12:51:04 -08001203 ops = ops.add(rule);
Saurav Das2857f382015-11-03 14:39:27 -08001204
1205 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
1206 @Override
1207 public void onSuccess(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -08001208 log.info("Initialized table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -08001209 }
Saurav Das2857f382015-11-03 14:39:27 -08001210 @Override
1211 public void onError(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -08001212 log.warn("Failed to initialize table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -08001213 }
1214 }));
1215 }
Charles Chan0f43e472017-02-14 14:00:16 -08001216
1217 /**
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001218 * Install lldp/bbdp matching rules at table PUNT_TABLE
1219 * that forward traffic to controller.
1220 *
1221 */
Charles Chan7f9d8462019-10-17 20:09:45 -07001222 private void initPuntTable() {
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001223 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
1224 TrafficTreatment treatment = DefaultTrafficTreatment.builder().punt().build();
1225
Charles Chan7f9d8462019-10-17 20:09:45 -07001226 // Add punt rule for LLDP and BDDP
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001227 TrafficSelector.Builder lldpSelector = DefaultTrafficSelector.builder();
1228 lldpSelector.matchEthType(EthType.EtherType.LLDP.ethType().toShort());
1229 FlowRule lldpRule = DefaultFlowRule.builder()
1230 .forDevice(deviceId)
1231 .withSelector(lldpSelector.build())
1232 .withTreatment(treatment)
1233 .withPriority(HIGHEST_PRIORITY)
1234 .fromApp(driverId)
1235 .makePermanent()
1236 .forTable(PUNT_TABLE).build();
1237 ops = ops.add(lldpRule);
1238
1239 TrafficSelector.Builder bbdpSelector = DefaultTrafficSelector.builder();
1240 bbdpSelector.matchEthType(EthType.EtherType.BDDP.ethType().toShort());
1241 FlowRule bbdpRule = DefaultFlowRule.builder()
1242 .forDevice(deviceId)
1243 .withSelector(bbdpSelector.build())
1244 .withTreatment(treatment)
1245 .withPriority(HIGHEST_PRIORITY)
1246 .fromApp(driverId)
1247 .makePermanent()
1248 .forTable(PUNT_TABLE).build();
1249 ops.add(bbdpRule);
1250
1251 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
1252 @Override
1253 public void onSuccess(FlowRuleOperations ops) {
Charles Chan7f9d8462019-10-17 20:09:45 -07001254 log.info("Initialized table {} on {}", PUNT_TABLE, deviceId);
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001255 }
1256 @Override
1257 public void onError(FlowRuleOperations ops) {
Charles Chan7f9d8462019-10-17 20:09:45 -07001258 log.warn("Failed to initialize table {} on {}", PUNT_TABLE, deviceId);
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001259 }
1260 }));
1261 }
1262
1263 /**
Charles Chan0f43e472017-02-14 14:00:16 -08001264 * Builds a indirect group contains pop_vlan and punt actions.
1265 * <p>
1266 * Using group instead of immediate action to ensure that
1267 * the copy of packet on the data plane is not affected by the pop vlan action.
1268 */
1269 private void initPopVlanPuntGroup() {
Yi Tsengef19de12017-04-24 11:33:05 -07001270 GroupKey groupKey = popVlanPuntGroupKey();
Charles Chan0f43e472017-02-14 14:00:16 -08001271 TrafficTreatment bucketTreatment = DefaultTrafficTreatment.builder()
1272 .popVlan().punt().build();
1273 GroupBucket bucket =
1274 DefaultGroupBucket.createIndirectGroupBucket(bucketTreatment);
1275 GroupDescription groupDesc =
1276 new DefaultGroupDescription(
1277 deviceId,
1278 GroupDescription.Type.INDIRECT,
1279 new GroupBuckets(Collections.singletonList(bucket)),
1280 groupKey,
1281 POP_VLAN_PUNT_GROUP_ID,
1282 driverId);
1283 groupService.addGroup(groupDesc);
1284
1285 log.info("Initialized pop vlan punt group on {}", deviceId);
1286 }
Yi Tsengef19de12017-04-24 11:33:05 -07001287
1288 /**
1289 * Generates group key for a static indirect group that pop vlan and punt to
1290 * controller.
1291 *
1292 * @return the group key of the indirect table
1293 */
1294 private GroupKey popVlanPuntGroupKey() {
Charles Chan367c1c12018-10-19 16:23:28 -07001295 int hash = POP_VLAN_PUNT_GROUP_ID | (Objects.hash(deviceId) & FOUR_NIBBLE_MASK);
Yi Tsengef19de12017-04-24 11:33:05 -07001296 return new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(hash));
1297 }
Andreas Pantelopoulos89369462018-05-30 14:30:24 -07001298
1299 private class PopVlanPuntGroupChecker implements Runnable {
1300 @Override
1301 public void run() {
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001302 try {
1303 groupCheckerLock.lock();
1304 // this can happen outside of the lock but I think it is safer
1305 // to include it here.
1306 Group group = groupService.getGroup(deviceId, popVlanPuntGroupKey());
Charles Chan4a288d92019-02-16 00:11:18 +00001307 if (group != null) {
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001308 log.debug("PopVlanPuntGroupChecker: Installing {} missing rules at punt table.",
1309 flowRuleQueue.size());
Andreas Pantelopoulos89369462018-05-30 14:30:24 -07001310
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001311 // if we have pending flow rules install them
1312 if (flowRuleQueue.size() > 0) {
1313 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
1314 // we should not care about the context here, it can only be add
1315 // since when removing the rules the group should be there already.
1316 flowRuleQueue.forEach(ops::add);
1317 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
1318 @Override
1319 public void onSuccess(FlowRuleOperations ops) {
1320 log.debug("Applied {} pop vlan punt rules in device {}",
1321 ops.stages().get(0).size(), deviceId);
1322 }
1323
1324 @Override
1325 public void onError(FlowRuleOperations ops) {
1326 log.error("Failed to apply all pop vlan punt rules in dev {}", deviceId);
1327 }
1328 }));
1329 }
1330 // this signifies that the group is created and now
1331 // flow rules can be installed directly
1332 flowRuleQueue = null;
pier02a331a2020-01-23 11:59:56 +01001333 // Schedule with an initial delay the miss table flow rule installation
1334 // the delay is to make sure the queued flows are all installed before
1335 // pushing the table miss flow rule
1336 // TODO it can be further optimized by using context and completable future
1337 groupChecker.schedule(new TableMissFlowInstaller(), 5000, TimeUnit.MILLISECONDS);
Andreas Pantelopoulos89369462018-05-30 14:30:24 -07001338 }
Andreas Pantelopoulos470865e2018-06-04 15:10:35 -07001339 } finally {
1340 groupCheckerLock.unlock();
Andreas Pantelopoulos89369462018-05-30 14:30:24 -07001341 }
1342 }
1343 }
pier02a331a2020-01-23 11:59:56 +01001344
1345 private class TableMissFlowInstaller implements Runnable {
1346 @Override
1347 public void run() {
1348 // Add table miss flow rule
1349 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
1350 TrafficSelector.Builder defaultSelector = DefaultTrafficSelector.builder();
1351 TrafficTreatment treatment = DefaultTrafficTreatment.builder().punt().build();
1352 FlowRule defaultRule = DefaultFlowRule.builder()
1353 .forDevice(deviceId)
1354 .withSelector(defaultSelector.build())
1355 .withTreatment(treatment)
1356 .withPriority(LOWEST_PRIORITY)
1357 .fromApp(driverId)
1358 .makePermanent()
1359 .forTable(PUNT_TABLE).build();
1360 ops.add(defaultRule);
1361 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
1362 @Override
1363 public void onSuccess(FlowRuleOperations ops) {
1364 log.info("Initialized table miss flow rule {} on {}", PUNT_TABLE, deviceId);
1365 }
1366 @Override
1367 public void onError(FlowRuleOperations ops) {
1368 log.warn("Failed to initialize table miss flow rule {} on {}", PUNT_TABLE, deviceId);
1369 }
1370 }));
1371 // shutdown the group checker gracefully
1372 groupChecker.shutdown();
1373 }
1374 }
Saurav Das558afec2015-05-31 17:12:48 -07001375}