blob: 797d79f91c363fc19457b249114ca2bae16e79cc [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 Pantelopoulosc78c1f52018-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 Chand57552d2018-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;
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -070028import org.onosproject.net.DeviceId;
Saurav Das2857f382015-11-03 14:39:27 -080029import org.onosproject.net.Port;
30import org.onosproject.net.PortNumber;
Saurav Das8a0732e2015-11-20 15:27:53 -080031import org.onosproject.net.behaviour.NextGroup;
Charles Chan425854b2016-04-11 15:32:12 -070032import org.onosproject.net.behaviour.PipelinerContext;
Saurav Das558afec2015-05-31 17:12:48 -070033import org.onosproject.net.flow.DefaultFlowRule;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
36import org.onosproject.net.flow.FlowRule;
37import org.onosproject.net.flow.FlowRuleOperations;
38import org.onosproject.net.flow.FlowRuleOperationsContext;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
Saurav Das4ce45962015-11-24 23:21:05 -080041import org.onosproject.net.flow.criteria.Criteria;
Saurav Das8a0732e2015-11-20 15:27:53 -080042import org.onosproject.net.flow.criteria.Criterion;
Saurav Das4ce45962015-11-24 23:21:05 -080043import org.onosproject.net.flow.criteria.EthCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080044import org.onosproject.net.flow.criteria.EthTypeCriterion;
45import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080046import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
47import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080048import org.onosproject.net.flow.criteria.MplsBosCriterion;
49import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das2857f382015-11-03 14:39:27 -080050import org.onosproject.net.flow.criteria.PortCriterion;
51import org.onosproject.net.flow.criteria.VlanIdCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080052import org.onosproject.net.flow.instructions.Instruction;
Saurav Das52025962016-01-28 22:30:01 -080053import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chan3fe71712018-06-15 18:54:18 -070054import org.onosproject.net.flow.instructions.Instructions.NoActionInstruction;
Charles Chan40132b32017-01-22 00:19:37 -080055import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080056import org.onosproject.net.flowobjective.ForwardingObjective;
57import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan0f43e472017-02-14 14:00:16 -080058import org.onosproject.net.group.DefaultGroupBucket;
59import org.onosproject.net.group.DefaultGroupDescription;
60import org.onosproject.net.group.DefaultGroupKey;
Saurav Das8a0732e2015-11-20 15:27:53 -080061import org.onosproject.net.group.Group;
Charles Chan0f43e472017-02-14 14:00:16 -080062import org.onosproject.net.group.GroupBucket;
63import org.onosproject.net.group.GroupBuckets;
64import org.onosproject.net.group.GroupDescription;
Saurav Das8a0732e2015-11-20 15:27:53 -080065import org.onosproject.net.group.GroupKey;
Charles Chanf57a8252016-06-29 19:12:37 -070066import org.onosproject.net.packet.PacketPriority;
Saurav Das558afec2015-05-31 17:12:48 -070067import org.slf4j.Logger;
68
Jonathan Hart855179c2016-04-26 07:40:04 -070069import java.util.ArrayList;
70import java.util.Collection;
71import java.util.Collections;
72import java.util.Deque;
73import java.util.List;
Charles Chan0f43e472017-02-14 14:00:16 -080074import java.util.Objects;
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -070075import java.util.Queue;
76import java.util.concurrent.ConcurrentLinkedQueue;
77import java.util.concurrent.Executors;
78import java.util.concurrent.ScheduledExecutorService;
79import java.util.concurrent.TimeUnit;
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -070080import java.util.concurrent.locks.ReentrantLock;
Jonathan Hart855179c2016-04-26 07:40:04 -070081
Pier Ventree0ae7a32016-11-23 09:57:42 -080082import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chand05f54b2017-02-13 11:56:54 -080083import static org.onlab.packet.MacAddress.BROADCAST;
84import static org.onlab.packet.MacAddress.NONE;
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -070085import static org.onlab.util.Tools.groupedThreads;
Yi Tsengef19de12017-04-24 11:33:05 -070086import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
pier5c817582019-04-17 17:05:08 +020087import static org.onosproject.driver.pipeline.ofdpa.OfdpaPipelineUtility.*;
Jonathan Hart855179c2016-04-26 07:40:04 -070088import static org.slf4j.LoggerFactory.getLogger;
89
Saurav Das558afec2015-05-31 17:12:48 -070090
91/**
Charles Chan40132b32017-01-22 00:19:37 -080092 * Driver for software switch emulation of the OFDPA pipeline.
Saurav Das52025962016-01-28 22:30:01 -080093 * The software switch is the CPqD OF 1.3 switch. Unfortunately the CPqD switch
94 * does not handle vlan tags and mpls labels simultaneously, which requires us
95 * to do some workarounds in the driver. This driver is meant for the use of
96 * the cpqd switch when MPLS is required. As a result this driver works only
97 * on incoming untagged packets.
Saurav Das558afec2015-05-31 17:12:48 -070098 */
Charles Chan361154b2016-03-24 10:23:39 -070099public class CpqdOfdpa2Pipeline extends Ofdpa2Pipeline {
Saurav Das558afec2015-05-31 17:12:48 -0700100
101 private final Logger log = getLogger(getClass());
102
Charles Chan40132b32017-01-22 00:19:37 -0800103 /**
Charles Chan0f43e472017-02-14 14:00:16 -0800104 * Table that determines whether VLAN is popped before punting to controller.
105 * <p>
106 * This is a non-OFDPA table to emulate OFDPA packet in behavior.
107 * VLAN will be popped before punting if the VLAN is internally assigned.
108 * <p>
109 * Also note that 63 is the max table number in CpqD.
110 */
111 private static final int PUNT_TABLE = 63;
112
113 /**
114 * A static indirect group that pop vlan and punt to controller.
115 * <p>
116 * The purpose of using a group instead of immediate action is that this
117 * won't affect another copy on the data plane when write action exists.
118 */
119 private static final int POP_VLAN_PUNT_GROUP_ID = 0xc0000000;
120
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -0700121 /**
122 * Executor for group checker thread that checks pop vlan punt group.
123 */
124 private ScheduledExecutorService groupChecker;
125
126 /**
127 * Queue for passing pop vlan punt group flow rules to the GroupChecker thread.
128 */
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700129 private Queue<FlowRule> flowRuleQueue;
130
131 /**
132 * Lock used in synchronizing driver thread with groupCheckerThread.
133 */
134 private ReentrantLock groupCheckerLock;
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -0700135
Charles Chan053b1cb2017-03-22 16:56:35 -0700136 @Override
137 protected boolean requireVlanExtensions() {
138 return false;
139 }
140
Charles Chan0f43e472017-02-14 14:00:16 -0800141 /**
Charles Chan40132b32017-01-22 00:19:37 -0800142 * Determines whether this pipeline support copy ttl instructions or not.
143 *
144 * @return true if copy ttl instructions are supported
145 */
146 protected boolean supportCopyTtl() {
147 return true;
148 }
149
Charles Chan0f43e472017-02-14 14:00:16 -0800150 /**
151 * Determines whether this pipeline support push mpls to vlan-tagged packets or not.
152 * <p>
153 * If not support, pop vlan before push entering unicast and mpls table.
154 * Side effect: HostService learns redundant hosts with same MAC but
155 * different VLAN. No known side effect on the network reachability.
156 *
157 * @return true if push mpls to vlan-tagged packets is supported
158 */
159 protected boolean supportTaggedMpls() {
160 return false;
161 }
162
163 /**
164 * Determines whether this pipeline support punt action in group bucket.
165 *
166 * @return true if punt action in group bucket is supported
167 */
168 protected boolean supportPuntGroup() {
169 return false;
170 }
171
Charles Chan425854b2016-04-11 15:32:12 -0700172 @Override
Charles Chan40132b32017-01-22 00:19:37 -0800173 protected void initDriverId() {
Charles Chan425854b2016-04-11 15:32:12 -0700174 driverId = coreService.registerApplication(
175 "org.onosproject.driver.CpqdOfdpa2Pipeline");
Charles Chan40132b32017-01-22 00:19:37 -0800176 }
Charles Chan425854b2016-04-11 15:32:12 -0700177
Charles Chan40132b32017-01-22 00:19:37 -0800178 @Override
179 protected void initGroupHander(PipelinerContext context) {
180 groupHandler = new CpqdOfdpa2GroupHandler();
181 groupHandler.init(deviceId, context);
Charles Chan425854b2016-04-11 15:32:12 -0700182 }
183
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -0700184 @Override
185 public void init(DeviceId deviceId, PipelinerContext context) {
186
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700187 if (supportPuntGroup()) {
188 // create a new executor at each init and a new empty queue
189 groupChecker = Executors.newSingleThreadScheduledExecutor(groupedThreads("onos/driver",
190 "cpqd-ofdpa-%d", log));
191 flowRuleQueue = new ConcurrentLinkedQueue<>();
192 groupCheckerLock = new ReentrantLock();
193 groupChecker.scheduleAtFixedRate(new PopVlanPuntGroupChecker(), 20, 50, TimeUnit.MILLISECONDS);
194 super.init(deviceId, context);
195 }
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -0700196 }
Saurav Das4ce45962015-11-24 23:21:05 -0800197 /*
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700198 * Cpqd emulation does not require the non OF-standard rules for
199 * matching untagged packets that ofdpa uses.
Saurav Das4ce45962015-11-24 23:21:05 -0800200 *
201 * (non-Javadoc)
202 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processVlanIdFilter
203 */
Saurav Das558afec2015-05-31 17:12:48 -0700204 @Override
Charles Chan206506b2018-03-02 16:43:28 -0800205 protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion,
Saurav Das2857f382015-11-03 14:39:27 -0800206 VlanIdCriterion vidCriterion,
207 VlanId assignedVlan,
208 ApplicationId applicationId) {
Charles Chan79769232016-07-05 16:34:39 -0700209 List<FlowRule> rules = new ArrayList<>();
Saurav Das2857f382015-11-03 14:39:27 -0800210 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
211 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
212 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800213 treatment.transition(TMAC_TABLE);
214
Saurav Das2857f382015-11-03 14:39:27 -0800215 if (vidCriterion.vlanId() == VlanId.NONE) {
216 // untagged packets are assigned vlans
217 treatment.pushVlan().setVlanId(assignedVlan);
Pier3cc7e662018-03-07 11:42:50 +0100218 } else if (!vidCriterion.vlanId().equals(assignedVlan)) {
219 // Rewrite with assigned vlans
220 treatment.setVlanId(assignedVlan);
Saurav Das2857f382015-11-03 14:39:27 -0800221 }
Saurav Das2857f382015-11-03 14:39:27 -0800222
223 // ofdpa cannot match on ALL portnumber, so we need to use separate
224 // rules for each port.
Charles Chan93090352018-03-02 13:26:22 -0800225 List<PortNumber> portnums = new ArrayList<>();
Ray Milkey94542b02018-05-10 12:42:51 -0700226 if (portCriterion != null) {
227 if (portCriterion.port() == PortNumber.ALL) {
228 for (Port port : deviceService.getPorts(deviceId)) {
229 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
230 portnums.add(port.number());
231 }
Saurav Das2857f382015-11-03 14:39:27 -0800232 }
Ray Milkey94542b02018-05-10 12:42:51 -0700233 } else {
234 portnums.add(portCriterion.port());
Saurav Das2857f382015-11-03 14:39:27 -0800235 }
Saurav Das2857f382015-11-03 14:39:27 -0800236 }
Saurav Das4f980082015-11-05 13:39:15 -0800237
Saurav Das2857f382015-11-03 14:39:27 -0800238 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800239 // NOTE: Emulating OFDPA behavior by popping off internal assigned
240 // VLAN before sending to controller
241 if (supportPuntGroup() && vidCriterion.vlanId() == VlanId.NONE) {
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700242 try {
243 groupCheckerLock.lock();
244 if (flowRuleQueue == null) {
245 // this means that the group has been created
246 // and that groupChecker has destroyed the queue
247 log.debug("Installing punt table rule for untagged port {} and vlan {}.",
248 pnum, assignedVlan);
249 rules.add(buildPuntTableRule(pnum, assignedVlan));
250 } else {
251 // The VLAN punt group may be held back due to device initial audit.
252 // In that case, we queue all punt table flow until the group has been created.
253 log.debug("popVlanPuntGroup not found in dev:{}, queueing this flow rule.", deviceId);
254 flowRuleQueue.add(buildPuntTableRule(pnum, assignedVlan));
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -0700255 }
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700256 } finally {
257 groupCheckerLock.unlock();
Charles Chan0f43e472017-02-14 14:00:16 -0800258 }
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700259 } else if (vidCriterion.vlanId() != VlanId.NONE) {
260 // for tagged ports just forward to the controller
261 log.debug("Installing punt rule for tagged port {} and vlan {}.", pnum, vidCriterion.vlanId());
262 rules.add(buildPuntTableRuleTagged(pnum, vidCriterion.vlanId()));
Charles Chan0f43e472017-02-14 14:00:16 -0800263 }
264
Saurav Das4f980082015-11-05 13:39:15 -0800265 // create rest of flowrule
Saurav Das2857f382015-11-03 14:39:27 -0800266 selector.matchInPort(pnum);
267 FlowRule rule = DefaultFlowRule.builder()
268 .forDevice(deviceId)
269 .withSelector(selector.build())
270 .withTreatment(treatment.build())
271 .withPriority(DEFAULT_PRIORITY)
272 .fromApp(applicationId)
273 .makePermanent()
274 .forTable(VLAN_TABLE).build();
275 rules.add(rule);
276 }
Charles Chanf57a8252016-06-29 19:12:37 -0700277
Charles Chan206506b2018-03-02 16:43:28 -0800278 return ImmutableList.of(rules);
Saurav Das2857f382015-11-03 14:39:27 -0800279 }
280
Pier Ventree0ae7a32016-11-23 09:57:42 -0800281 /**
Charles Chan0f43e472017-02-14 14:00:16 -0800282 * Creates punt table entry that matches IN_PORT and VLAN_VID and points to
283 * a group that pop vlan and punt.
284 *
285 * @param portNumber port number
286 * @param assignedVlan internally assigned vlan id
287 * @return punt table flow rule
288 */
289 private FlowRule buildPuntTableRule(PortNumber portNumber, VlanId assignedVlan) {
290 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
291 .matchInPort(portNumber)
292 .matchVlanId(assignedVlan);
293 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
Yi Tsengfa394de2017-02-01 11:26:40 -0800294 .group(new GroupId(POP_VLAN_PUNT_GROUP_ID));
Charles Chan0f43e472017-02-14 14:00:16 -0800295
296 return DefaultFlowRule.builder()
297 .forDevice(deviceId)
298 .withSelector(sbuilder.build())
299 .withTreatment(tbuilder.build())
300 .withPriority(PacketPriority.CONTROL.priorityValue())
301 .fromApp(driverId)
302 .makePermanent()
303 .forTable(PUNT_TABLE).build();
304 }
305
306 /**
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700307 * Creates punt table entry that matches IN_PORT and VLAN_VID and forwards
308 * packet to controller tagged.
309 *
310 * @param portNumber port number
311 * @param packetVlan vlan tag of the packet
312 * @return punt table flow rule
313 */
314 private FlowRule buildPuntTableRuleTagged(PortNumber portNumber, VlanId packetVlan) {
315 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
316 .matchInPort(portNumber)
317 .matchVlanId(packetVlan);
318 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder().punt();
319
320 return DefaultFlowRule.builder()
321 .forDevice(deviceId)
322 .withSelector(sbuilder.build())
323 .withTreatment(tbuilder.build())
324 .withPriority(PacketPriority.CONTROL.priorityValue())
325 .fromApp(driverId)
326 .makePermanent()
327 .forTable(PUNT_TABLE).build();
328 }
329
330 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800331 * Builds a punt to the controller rule for the arp protocol.
Charles Chan0f43e472017-02-14 14:00:16 -0800332 * <p>
333 * NOTE: CpqD cannot punt correctly in group bucket. The current impl will
334 * pop VLAN before sending to controller disregarding whether
335 * it's an internally assigned VLAN or a natural VLAN.
336 * Therefore, trunk port is not supported in CpqD.
Pier Ventree0ae7a32016-11-23 09:57:42 -0800337 *
338 * @param assignedVlan the internal assigned vlan id
339 * @param applicationId the application id
340 * @return the punt flow rule for the arp
341 */
342 private FlowRule buildArpPunt(VlanId assignedVlan, ApplicationId applicationId) {
343 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
344 .matchEthType(Ethernet.TYPE_ARP)
345 .matchVlanId(assignedVlan);
346 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
347 .popVlan()
348 .punt();
349
350 return DefaultFlowRule.builder()
351 .forDevice(deviceId)
352 .withSelector(sbuilder.build())
353 .withTreatment(tbuilder.build())
354 .withPriority(PacketPriority.CONTROL.priorityValue() + 1)
355 .fromApp(applicationId)
356 .makePermanent()
357 .forTable(ACL_TABLE).build();
358 }
359
360 /**
361 * Builds a punt to the controller rule for the icmp v6 messages.
Charles Chan0f43e472017-02-14 14:00:16 -0800362 * <p>
363 * NOTE: CpqD cannot punt correctly in group bucket. The current impl will
364 * pop VLAN before sending to controller disregarding whether
365 * it's an internally assigned VLAN or a natural VLAN.
366 * Therefore, trunk port is not supported in CpqD.
Pier Ventree0ae7a32016-11-23 09:57:42 -0800367 *
368 * @param assignedVlan the internal assigned vlan id
369 * @param applicationId the application id
370 * @return the punt flow rule for the icmp v6 messages
371 */
372 private FlowRule buildIcmpV6Punt(VlanId assignedVlan, ApplicationId applicationId) {
373 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
374 .matchVlanId(assignedVlan)
375 .matchEthType(Ethernet.TYPE_IPV6)
376 .matchIPProtocol(PROTOCOL_ICMP6);
377 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
378 .popVlan()
379 .punt();
380
381 return DefaultFlowRule.builder()
382 .forDevice(deviceId)
383 .withSelector(sbuilder.build())
384 .withTreatment(tbuilder.build())
385 .withPriority(PacketPriority.CONTROL.priorityValue() + 1)
386 .fromApp(applicationId)
387 .makePermanent()
388 .forTable(ACL_TABLE).build();
389 }
390
Saurav Das4ce45962015-11-24 23:21:05 -0800391 /*
392 * Cpqd emulation does not handle vlan tags and mpls labels correctly.
393 * Workaround requires popping off the VLAN tags in the TMAC table.
394 *
395 * (non-Javadoc)
396 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthDstFilter
397 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800398 @Override
Charles Chan206506b2018-03-02 16:43:28 -0800399 protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion,
Saurav Das4ce45962015-11-24 23:21:05 -0800400 EthCriterion ethCriterion,
401 VlanIdCriterion vidCriterion,
402 VlanId assignedVlan,
Charles Chand57552d2018-03-02 15:41:41 -0800403 MacAddress unicastMac,
Saurav Das4ce45962015-11-24 23:21:05 -0800404 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800405 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
406 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
407 return processEthDstOnlyFilter(ethCriterion, applicationId);
408 }
409
Charles Chan5b9df8d2016-03-28 22:21:40 -0700410 // Multicast MAC
411 if (ethCriterion.mask() != null) {
Charles Chand57552d2018-03-02 15:41:41 -0800412 return processMcastEthDstFilter(ethCriterion, assignedVlan, unicastMac, applicationId);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700413 }
414
Saurav Das4ce45962015-11-24 23:21:05 -0800415 //handling untagged packets via assigned VLAN
416 if (vidCriterion.vlanId() == VlanId.NONE) {
417 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
418 }
419 // ofdpa cannot match on ALL portnumber, so we need to use separate
420 // rules for each port.
Charles Chan93090352018-03-02 13:26:22 -0800421 List<PortNumber> portnums = new ArrayList<>();
Ray Milkeyae4e5ed2018-01-17 15:24:52 -0800422 if (portCriterion != null) {
423 if (portCriterion.port() == PortNumber.ALL) {
424 for (Port port : deviceService.getPorts(deviceId)) {
425 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
426 portnums.add(port.number());
427 }
Saurav Das4ce45962015-11-24 23:21:05 -0800428 }
Ray Milkeyae4e5ed2018-01-17 15:24:52 -0800429 } else {
430 portnums.add(portCriterion.port());
Saurav Das4ce45962015-11-24 23:21:05 -0800431 }
Saurav Das4ce45962015-11-24 23:21:05 -0800432 }
433
Charles Chan93090352018-03-02 13:26:22 -0800434 List<FlowRule> rules = new ArrayList<>();
Saurav Das4ce45962015-11-24 23:21:05 -0800435 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800436 // TMAC rules for unicast IP packets
Saurav Das4ce45962015-11-24 23:21:05 -0800437 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
438 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
439 selector.matchInPort(pnum);
440 selector.matchVlanId(vidCriterion.vlanId());
441 selector.matchEthType(Ethernet.TYPE_IPV4);
442 selector.matchEthDst(ethCriterion.mac());
Charles Chan0f43e472017-02-14 14:00:16 -0800443 if (!supportTaggedMpls()) {
444 treatment.popVlan();
445 }
Saurav Das4ce45962015-11-24 23:21:05 -0800446 treatment.transition(UNICAST_ROUTING_TABLE);
447 FlowRule rule = DefaultFlowRule.builder()
448 .forDevice(deviceId)
449 .withSelector(selector.build())
450 .withTreatment(treatment.build())
451 .withPriority(DEFAULT_PRIORITY)
452 .fromApp(applicationId)
453 .makePermanent()
454 .forTable(TMAC_TABLE).build();
455 rules.add(rule);
Charles Chan0f43e472017-02-14 14:00:16 -0800456
457 // TMAC rules for MPLS packets
Saurav Das4ce45962015-11-24 23:21:05 -0800458 selector = DefaultTrafficSelector.builder();
459 treatment = DefaultTrafficTreatment.builder();
460 selector.matchInPort(pnum);
461 selector.matchVlanId(vidCriterion.vlanId());
462 selector.matchEthType(Ethernet.MPLS_UNICAST);
463 selector.matchEthDst(ethCriterion.mac());
Charles Chan0f43e472017-02-14 14:00:16 -0800464 if (!supportTaggedMpls()) {
465 treatment.popVlan();
466 }
Saurav Das4ce45962015-11-24 23:21:05 -0800467 treatment.transition(MPLS_TABLE_0);
468 rule = DefaultFlowRule.builder()
469 .forDevice(deviceId)
470 .withSelector(selector.build())
471 .withTreatment(treatment.build())
472 .withPriority(DEFAULT_PRIORITY)
473 .fromApp(applicationId)
474 .makePermanent()
475 .forTable(TMAC_TABLE).build();
476 rules.add(rule);
Charles Chan0f43e472017-02-14 14:00:16 -0800477
478 // TMAC rules for IPv6 packets
Pier Ventree0ae7a32016-11-23 09:57:42 -0800479 selector = DefaultTrafficSelector.builder();
480 treatment = DefaultTrafficTreatment.builder();
481 selector.matchInPort(pnum);
482 selector.matchVlanId(vidCriterion.vlanId());
483 selector.matchEthType(Ethernet.TYPE_IPV6);
484 selector.matchEthDst(ethCriterion.mac());
Charles Chan0f43e472017-02-14 14:00:16 -0800485 if (!supportTaggedMpls()) {
486 treatment.popVlan();
487 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800488 treatment.transition(UNICAST_ROUTING_TABLE);
489 rule = DefaultFlowRule.builder()
490 .forDevice(deviceId)
491 .withSelector(selector.build())
492 .withTreatment(treatment.build())
493 .withPriority(DEFAULT_PRIORITY)
494 .fromApp(applicationId)
495 .makePermanent()
496 .forTable(TMAC_TABLE).build();
497 rules.add(rule);
Saurav Das4ce45962015-11-24 23:21:05 -0800498 }
Charles Chan206506b2018-03-02 16:43:28 -0800499 return ImmutableList.of(rules);
Saurav Das4ce45962015-11-24 23:21:05 -0800500 }
501
Charles Chan5270ed02016-01-30 23:22:37 -0800502 @Override
Charles Chan206506b2018-03-02 16:43:28 -0800503 protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700504 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800505 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
506 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
507 selector.matchEthType(Ethernet.TYPE_IPV4);
508 selector.matchEthDst(ethCriterion.mac());
Charles Chan0f43e472017-02-14 14:00:16 -0800509 if (!supportTaggedMpls()) {
510 treatment.popVlan();
511 }
Charles Chan5270ed02016-01-30 23:22:37 -0800512 treatment.transition(UNICAST_ROUTING_TABLE);
513 FlowRule rule = DefaultFlowRule.builder()
514 .forDevice(deviceId)
515 .withSelector(selector.build())
516 .withTreatment(treatment.build())
517 .withPriority(DEFAULT_PRIORITY)
518 .fromApp(applicationId)
519 .makePermanent()
520 .forTable(TMAC_TABLE).build();
Charles Chan206506b2018-03-02 16:43:28 -0800521 return ImmutableList.of(ImmutableList.of(rule));
Charles Chan5270ed02016-01-30 23:22:37 -0800522 }
523
Saurav Das4ce45962015-11-24 23:21:05 -0800524 /*
525 * Cpqd emulation allows MPLS ecmp.
526 *
527 * (non-Javadoc)
528 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthTypeSpecific
529 */
530 @Override
531 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800532 TrafficSelector selector = fwd.selector();
533 EthTypeCriterion ethType =
534 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
535 if ((ethType == null) ||
536 (ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -0800537 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST) &&
538 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6)) {
Saurav Das4ce45962015-11-24 23:21:05 -0800539 log.warn("processSpecific: Unsupported forwarding objective criteria"
540 + "ethType:{} in dev:{}", ethType, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800541 fail(fwd, ObjectiveError.UNSUPPORTED);
542 return Collections.emptySet();
543 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800544 boolean defaultRule = false;
Charles Chan93090352018-03-02 13:26:22 -0800545 int forTableId;
Saurav Das8a0732e2015-11-20 15:27:53 -0800546 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800547 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
548
Saurav Das8a0732e2015-11-20 15:27:53 -0800549 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800550 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700551 if (ipv4Dst.isMulticast()) {
552 if (ipv4Dst.prefixLength() != 32) {
553 log.warn("Multicast specific forwarding objective can only be /32");
554 fail(fwd, ObjectiveError.BADPARAMS);
555 return ImmutableSet.of();
556 }
557 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
558 if (assignedVlan == null) {
559 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
560 fail(fwd, ObjectiveError.BADPARAMS);
561 return ImmutableSet.of();
562 }
563 filteredSelector.matchVlanId(assignedVlan);
564 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
565 forTableId = MULTICAST_ROUTING_TABLE;
566 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
567 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800568 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700569 if (ipv4Dst.prefixLength() == 0) {
570 // The entire IPV4_DST field is wildcarded intentionally
571 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700572 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700573 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700574 }
575 forTableId = UNICAST_ROUTING_TABLE;
576 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
577 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800578 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800579 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000580 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
581 if (ipv6Dst.isMulticast()) {
582 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
583 log.debug("Multicast specific IPv6 forwarding objective can only be /128");
584 fail(fwd, ObjectiveError.BADPARAMS);
585 return ImmutableSet.of();
586 }
587 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
588 if (assignedVlan == null) {
589 log.debug("VLAN ID required by multicast specific fwd obj is missing. Abort.");
590 fail(fwd, ObjectiveError.BADPARAMS);
591 return ImmutableSet.of();
592 }
593 filteredSelector.matchVlanId(assignedVlan);
594 filteredSelector.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
595 forTableId = MULTICAST_ROUTING_TABLE;
596 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
597 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
598 } else {
599 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
600 return Collections.emptyList();
601 }
602 forTableId = UNICAST_ROUTING_TABLE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800603 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800604 } else {
605 filteredSelector
606 .matchEthType(Ethernet.MPLS_UNICAST)
607 .matchMplsLabel(((MplsCriterion)
608 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
609 MplsBosCriterion bos = (MplsBosCriterion) selector
610 .getCriterion(Criterion.Type.MPLS_BOS);
611 if (bos != null) {
612 filteredSelector.matchMplsBos(bos.mplsBos());
613 }
614 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800615 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
616 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800617 }
618
619 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
620 if (fwd.treatment() != null) {
621 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chan40132b32017-01-22 00:19:37 -0800622 if (!supportCopyTtl() && i instanceof L3ModificationInstruction) {
623 L3ModificationInstruction l3instr = (L3ModificationInstruction) i;
624 if (l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_IN) ||
625 l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_OUT)) {
626 continue;
627 }
628 }
Charles Chan7d10b162015-12-07 18:54:45 -0800629 /*
630 * NOTE: OF-DPA does not support immediate instruction in
631 * L3 unicast and MPLS table.
632 */
633 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800634 }
635 }
636
637 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800638 NextGroup next = getGroupForNextObjective(fwd.nextId());
639 if (next != null) {
640 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
641 // we only need the top level group's key to point the flow to it
642 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
643 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700644 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
645 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das423fe2b2015-12-04 10:52:59 -0800646 fail(fwd, ObjectiveError.GROUPMISSING);
647 return Collections.emptySet();
648 }
649 tb.deferred().group(group.id());
Saurav Das8a0732e2015-11-20 15:27:53 -0800650 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800651 }
652 tb.transition(ACL_TABLE);
653 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
654 .fromApp(fwd.appId())
655 .withPriority(fwd.priority())
656 .forDevice(deviceId)
657 .withSelector(filteredSelector.build())
658 .withTreatment(tb.build())
659 .forTable(forTableId);
660
661 if (fwd.permanent()) {
662 ruleBuilder.makePermanent();
663 } else {
664 ruleBuilder.makeTemporary(fwd.timeout());
665 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800666 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
667 flowRuleCollection.add(ruleBuilder.build());
668 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800669 flowRuleCollection.add(
670 defaultRoute(fwd, complementarySelector, forTableId, tb)
671 );
Flavio Castroe10fa242016-01-15 12:43:51 -0800672 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
673 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800674 return flowRuleCollection;
Saurav Das8a0732e2015-11-20 15:27:53 -0800675 }
676
Charles Chan1e492d32016-01-30 23:22:37 -0800677 @Override
678 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
679 List<FlowRule> rules = new ArrayList<>();
680
681 // Build filtered selector
682 TrafficSelector selector = fwd.selector();
683 EthCriterion ethCriterion = (EthCriterion) selector
684 .getCriterion(Criterion.Type.ETH_DST);
685 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
686 .getCriterion(Criterion.Type.VLAN_VID);
687
688 if (vlanIdCriterion == null) {
689 log.warn("Forwarding objective for bridging requires vlan. Not "
690 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
691 fail(fwd, ObjectiveError.BADPARAMS);
692 return Collections.emptySet();
693 }
694
695 TrafficSelector.Builder filteredSelectorBuilder =
696 DefaultTrafficSelector.builder();
697 // Do not match MacAddress for subnet broadcast entry
Charles Chand05f54b2017-02-13 11:56:54 -0800698 if (!ethCriterion.mac().equals(NONE) && !ethCriterion.mac().equals(BROADCAST)) {
Charles Chan1e492d32016-01-30 23:22:37 -0800699 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
700 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
701 fwd.id(), fwd.nextId(), deviceId);
702 } else {
703 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
704 + "in dev:{} for vlan:{}",
705 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
706 }
707 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
708 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
709
710 if (fwd.treatment() != null) {
711 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
712 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
713 }
714
715 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
716 if (fwd.nextId() != null) {
717 NextGroup next = getGroupForNextObjective(fwd.nextId());
718 if (next != null) {
719 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
720 // we only need the top level group's key to point the flow to it
721 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
722 if (group != null) {
723 treatmentBuilder.deferred().group(group.id());
724 } else {
725 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
726 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
727 fail(fwd, ObjectiveError.GROUPMISSING);
728 return Collections.emptySet();
729 }
730 }
731 }
732 treatmentBuilder.immediate().transition(ACL_TABLE);
733 TrafficTreatment filteredTreatment = treatmentBuilder.build();
734
735 // Build bridging table entries
736 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
737 flowRuleBuilder.fromApp(fwd.appId())
738 .withPriority(fwd.priority())
739 .forDevice(deviceId)
740 .withSelector(filteredSelector)
741 .withTreatment(filteredTreatment)
742 .forTable(BRIDGING_TABLE);
743 if (fwd.permanent()) {
744 flowRuleBuilder.makePermanent();
745 } else {
746 flowRuleBuilder.makeTemporary(fwd.timeout());
747 }
748 rules.add(flowRuleBuilder.build());
749 return rules;
750 }
751
Saurav Das52025962016-01-28 22:30:01 -0800752 /*
753 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
754 * ACL table. Because we pop off vlan tags in TMAC table,
755 * we need to avoid matching on vlans in the ACL table.
756 */
757 @Override
758 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
759 log.info("Processing versatile forwarding objective");
760
Saurav Das52025962016-01-28 22:30:01 -0800761 if (fwd.nextId() == null && fwd.treatment() == null) {
762 log.error("Forwarding objective {} from {} must contain "
763 + "nextId or Treatment", fwd.selector(), fwd.appId());
764 return Collections.emptySet();
765 }
766
767 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
768 fwd.selector().criteria().forEach(criterion -> {
769 if (criterion instanceof VlanIdCriterion) {
770 // avoid matching on vlans
771 return;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800772 } else if (criterion instanceof Icmpv6TypeCriterion ||
773 criterion instanceof Icmpv6CodeCriterion) {
774 /*
775 * We silenty discard these criterions, our current
776 * OFDPA platform does not support these matches on
777 * the ACL table.
778 */
779 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das52025962016-01-28 22:30:01 -0800780 } else {
781 sbuilder.add(criterion);
782 }
783 });
784
785 // XXX driver does not currently do type checking as per Tables 65-67 in
786 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
787 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
788 if (fwd.treatment() != null) {
789 for (Instruction ins : fwd.treatment().allInstructions()) {
790 if (ins instanceof OutputInstruction) {
791 OutputInstruction o = (OutputInstruction) ins;
792 if (o.port() == PortNumber.CONTROLLER) {
Charles Chan0f43e472017-02-14 14:00:16 -0800793 ttBuilder.transition(PUNT_TABLE);
Saurav Das52025962016-01-28 22:30:01 -0800794 } else {
795 log.warn("Only allowed treatments in versatile forwarding "
796 + "objectives are punts to the controller");
797 }
Charles Chan3fe71712018-06-15 18:54:18 -0700798 } else if (ins instanceof NoActionInstruction) {
799 // No action is allowed and nothing needs to be done
Saurav Das52025962016-01-28 22:30:01 -0800800 } else {
801 log.warn("Cannot process instruction in versatile fwd {}", ins);
802 }
803 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800804 if (fwd.treatment().clearedDeferred()) {
805 ttBuilder.wipeDeferred();
806 }
Saurav Das52025962016-01-28 22:30:01 -0800807 }
808 if (fwd.nextId() != null) {
809 // overide case
810 NextGroup next = getGroupForNextObjective(fwd.nextId());
811 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
812 // we only need the top level group's key to point the flow to it
813 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
814 if (group == null) {
815 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
816 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
817 fail(fwd, ObjectiveError.GROUPMISSING);
818 return Collections.emptySet();
819 }
820 ttBuilder.deferred().group(group.id());
821 }
822
823 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
824 .fromApp(fwd.appId())
825 .withPriority(fwd.priority())
826 .forDevice(deviceId)
827 .withSelector(sbuilder.build())
828 .withTreatment(ttBuilder.build())
829 .makePermanent()
830 .forTable(ACL_TABLE);
831 return Collections.singletonList(ruleBuilder.build());
832 }
833
834 /*
835 * Cpqd emulation requires table-miss-entries in forwarding tables.
836 * Real OFDPA does not require these rules as they are put in by default.
837 *
838 * (non-Javadoc)
839 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#initializePipeline()
840 */
Saurav Das2857f382015-11-03 14:39:27 -0800841 @Override
Saurav Das558afec2015-05-31 17:12:48 -0700842 protected void initializePipeline() {
Charles Chanf6ec1532017-02-08 16:10:40 -0800843 initTableMiss(PORT_TABLE, VLAN_TABLE, null);
844 initTableMiss(VLAN_TABLE, ACL_TABLE, null);
845 initTableMiss(TMAC_TABLE, BRIDGING_TABLE, null);
846 initTableMiss(UNICAST_ROUTING_TABLE, ACL_TABLE, null);
847 initTableMiss(MULTICAST_ROUTING_TABLE, ACL_TABLE, null);
848 initTableMiss(MPLS_TABLE_0, MPLS_TABLE_1, null);
849 initTableMiss(MPLS_TABLE_1, ACL_TABLE, null);
850 initTableMiss(BRIDGING_TABLE, ACL_TABLE, null);
851 initTableMiss(ACL_TABLE, -1, null);
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700852 linkDiscoveryPuntTableRules();
Charles Chan0f43e472017-02-14 14:00:16 -0800853
854 if (supportPuntGroup()) {
Charles Chan0f43e472017-02-14 14:00:16 -0800855 initPopVlanPuntGroup();
856 } else {
857 initTableMiss(PUNT_TABLE, -1,
858 DefaultTrafficTreatment.builder().popVlan().punt().build());
859 }
Saurav Das558afec2015-05-31 17:12:48 -0700860 }
861
Charles Chanf6ec1532017-02-08 16:10:40 -0800862 /**
863 * Install table-miss flow entry.
864 *
865 * If treatment exists, use it directly.
866 * Else if treatment does not exist but nextTable > 0, transit to next table.
867 * Else apply empty treatment.
868 *
869 * @param thisTable this table ID
870 * @param nextTable next table ID
871 * @param treatment traffic treatment to apply.
872 */
873 private void initTableMiss(int thisTable, int nextTable, TrafficTreatment treatment) {
Saurav Das558afec2015-05-31 17:12:48 -0700874 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
Charles Chanf6ec1532017-02-08 16:10:40 -0800875 TrafficSelector selector = DefaultTrafficSelector.builder().build();
Saurav Das558afec2015-05-31 17:12:48 -0700876
Charles Chanf6ec1532017-02-08 16:10:40 -0800877 if (treatment == null) {
878 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
879 if (nextTable > 0) {
880 tBuilder.transition(nextTable);
Saurav Das558afec2015-05-31 17:12:48 -0700881 }
Charles Chanf6ec1532017-02-08 16:10:40 -0800882 treatment = tBuilder.build();
883 }
Saurav Das558afec2015-05-31 17:12:48 -0700884
Charles Chanb7504392017-02-10 12:51:04 -0800885 FlowRule rule = DefaultFlowRule.builder()
886 .forDevice(deviceId)
Charles Chanf6ec1532017-02-08 16:10:40 -0800887 .withSelector(selector)
888 .withTreatment(treatment)
Charles Chanb7504392017-02-10 12:51:04 -0800889 .withPriority(LOWEST_PRIORITY)
890 .fromApp(driverId)
891 .makePermanent()
Charles Chanf6ec1532017-02-08 16:10:40 -0800892 .forTable(thisTable).build();
Charles Chanb7504392017-02-10 12:51:04 -0800893 ops = ops.add(rule);
Saurav Das2857f382015-11-03 14:39:27 -0800894
895 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
896 @Override
897 public void onSuccess(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800898 log.info("Initialized table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -0800899 }
Saurav Das2857f382015-11-03 14:39:27 -0800900 @Override
901 public void onError(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800902 log.warn("Failed to initialize table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -0800903 }
904 }));
905 }
Charles Chan0f43e472017-02-14 14:00:16 -0800906
907 /**
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700908 * Install lldp/bbdp matching rules at table PUNT_TABLE
909 * that forward traffic to controller.
910 *
911 */
912 private void linkDiscoveryPuntTableRules() {
913 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
914 TrafficTreatment treatment = DefaultTrafficTreatment.builder().punt().build();
915
916 TrafficSelector.Builder lldpSelector = DefaultTrafficSelector.builder();
917 lldpSelector.matchEthType(EthType.EtherType.LLDP.ethType().toShort());
918 FlowRule lldpRule = DefaultFlowRule.builder()
919 .forDevice(deviceId)
920 .withSelector(lldpSelector.build())
921 .withTreatment(treatment)
922 .withPriority(HIGHEST_PRIORITY)
923 .fromApp(driverId)
924 .makePermanent()
925 .forTable(PUNT_TABLE).build();
926 ops = ops.add(lldpRule);
927
928 TrafficSelector.Builder bbdpSelector = DefaultTrafficSelector.builder();
929 bbdpSelector.matchEthType(EthType.EtherType.BDDP.ethType().toShort());
930 FlowRule bbdpRule = DefaultFlowRule.builder()
931 .forDevice(deviceId)
932 .withSelector(bbdpSelector.build())
933 .withTreatment(treatment)
934 .withPriority(HIGHEST_PRIORITY)
935 .fromApp(driverId)
936 .makePermanent()
937 .forTable(PUNT_TABLE).build();
938 ops.add(bbdpRule);
939
940 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
941 @Override
942 public void onSuccess(FlowRuleOperations ops) {
943 log.info("Added lldp/bbdp rules for table {} on {}", PUNT_TABLE, deviceId);
944 }
945 @Override
946 public void onError(FlowRuleOperations ops) {
947 log.warn("Failed to initialize lldp/bbdp rules for table {} on {}", PUNT_TABLE, deviceId);
948 }
949 }));
950 }
951
952 /**
Charles Chan0f43e472017-02-14 14:00:16 -0800953 * Builds a indirect group contains pop_vlan and punt actions.
954 * <p>
955 * Using group instead of immediate action to ensure that
956 * the copy of packet on the data plane is not affected by the pop vlan action.
957 */
958 private void initPopVlanPuntGroup() {
Yi Tsengef19de12017-04-24 11:33:05 -0700959 GroupKey groupKey = popVlanPuntGroupKey();
Charles Chan0f43e472017-02-14 14:00:16 -0800960 TrafficTreatment bucketTreatment = DefaultTrafficTreatment.builder()
961 .popVlan().punt().build();
962 GroupBucket bucket =
963 DefaultGroupBucket.createIndirectGroupBucket(bucketTreatment);
964 GroupDescription groupDesc =
965 new DefaultGroupDescription(
966 deviceId,
967 GroupDescription.Type.INDIRECT,
968 new GroupBuckets(Collections.singletonList(bucket)),
969 groupKey,
970 POP_VLAN_PUNT_GROUP_ID,
971 driverId);
972 groupService.addGroup(groupDesc);
973
974 log.info("Initialized pop vlan punt group on {}", deviceId);
975 }
Yi Tsengef19de12017-04-24 11:33:05 -0700976
977 /**
978 * Generates group key for a static indirect group that pop vlan and punt to
979 * controller.
980 *
981 * @return the group key of the indirect table
982 */
983 private GroupKey popVlanPuntGroupKey() {
984 int hash = POP_VLAN_PUNT_GROUP_ID | (Objects.hash(deviceId) & FOUR_BIT_MASK);
985 return new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(hash));
986 }
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -0700987
988 private class PopVlanPuntGroupChecker implements Runnable {
989 @Override
990 public void run() {
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -0700991 try {
992 groupCheckerLock.lock();
993 // this can happen outside of the lock but I think it is safer
994 // to include it here.
995 Group group = groupService.getGroup(deviceId, popVlanPuntGroupKey());
996 if (group != null) {
997 log.debug("PopVlanPuntGroupChecker: Installing {} missing rules at punt table.",
998 flowRuleQueue.size());
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -0700999
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -07001000 // if we have pending flow rules install them
1001 if (flowRuleQueue.size() > 0) {
1002 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
1003 // we should not care about the context here, it can only be add
1004 // since when removing the rules the group should be there already.
1005 flowRuleQueue.forEach(ops::add);
1006 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
1007 @Override
1008 public void onSuccess(FlowRuleOperations ops) {
1009 log.debug("Applied {} pop vlan punt rules in device {}",
1010 ops.stages().get(0).size(), deviceId);
1011 }
1012
1013 @Override
1014 public void onError(FlowRuleOperations ops) {
1015 log.error("Failed to apply all pop vlan punt rules in dev {}", deviceId);
1016 }
1017 }));
1018 }
1019 // this signifies that the group is created and now
1020 // flow rules can be installed directly
1021 flowRuleQueue = null;
1022 // shutdown the group checker gracefully
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -07001023 groupChecker.shutdown();
1024 }
Andreas Pantelopoulosc78c1f52018-06-04 15:10:35 -07001025 } finally {
1026 groupCheckerLock.unlock();
Andreas Pantelopoulos410d7302018-05-30 14:30:24 -07001027 }
1028 }
1029 }
Saurav Das558afec2015-05-31 17:12:48 -07001030}