blob: 9f2e84ca769c91b0df9735a484c57b42c1acf571 [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;
Saurav Das8a0732e2015-11-20 15:27:53 -080020import org.onlab.packet.Ethernet;
Julia Ferguson65428c32017-08-10 18:15:24 +000021import org.onlab.packet.IpAddress;
Flavio Castroe10fa242016-01-15 12:43:51 -080022import org.onlab.packet.IpPrefix;
Saurav Das2857f382015-11-03 14:39:27 -080023import org.onlab.packet.VlanId;
24import org.onosproject.core.ApplicationId;
Yi Tsengfa394de2017-02-01 11:26:40 -080025import org.onosproject.core.GroupId;
Saurav Das2857f382015-11-03 14:39:27 -080026import org.onosproject.net.Port;
27import org.onosproject.net.PortNumber;
Saurav Das8a0732e2015-11-20 15:27:53 -080028import org.onosproject.net.behaviour.NextGroup;
Charles Chan425854b2016-04-11 15:32:12 -070029import org.onosproject.net.behaviour.PipelinerContext;
Saurav Das558afec2015-05-31 17:12:48 -070030import org.onosproject.net.flow.DefaultFlowRule;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.FlowRuleOperations;
35import org.onosproject.net.flow.FlowRuleOperationsContext;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
Saurav Das4ce45962015-11-24 23:21:05 -080038import org.onosproject.net.flow.criteria.Criteria;
Saurav Das8a0732e2015-11-20 15:27:53 -080039import org.onosproject.net.flow.criteria.Criterion;
Saurav Das4ce45962015-11-24 23:21:05 -080040import org.onosproject.net.flow.criteria.EthCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080041import org.onosproject.net.flow.criteria.EthTypeCriterion;
42import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080043import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
44import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080045import org.onosproject.net.flow.criteria.MplsBosCriterion;
46import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das2857f382015-11-03 14:39:27 -080047import org.onosproject.net.flow.criteria.PortCriterion;
48import org.onosproject.net.flow.criteria.VlanIdCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080049import org.onosproject.net.flow.instructions.Instruction;
Saurav Das52025962016-01-28 22:30:01 -080050import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chan40132b32017-01-22 00:19:37 -080051import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080052import org.onosproject.net.flowobjective.ForwardingObjective;
53import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan0f43e472017-02-14 14:00:16 -080054import org.onosproject.net.group.DefaultGroupBucket;
55import org.onosproject.net.group.DefaultGroupDescription;
56import org.onosproject.net.group.DefaultGroupKey;
Saurav Das8a0732e2015-11-20 15:27:53 -080057import org.onosproject.net.group.Group;
Charles Chan0f43e472017-02-14 14:00:16 -080058import org.onosproject.net.group.GroupBucket;
59import org.onosproject.net.group.GroupBuckets;
60import org.onosproject.net.group.GroupDescription;
Saurav Das8a0732e2015-11-20 15:27:53 -080061import org.onosproject.net.group.GroupKey;
Charles Chanf57a8252016-06-29 19:12:37 -070062import org.onosproject.net.packet.PacketPriority;
Saurav Das558afec2015-05-31 17:12:48 -070063import org.slf4j.Logger;
64
Jonathan Hart855179c2016-04-26 07:40:04 -070065import java.util.ArrayList;
66import java.util.Collection;
67import java.util.Collections;
68import java.util.Deque;
69import java.util.List;
Charles Chan0f43e472017-02-14 14:00:16 -080070import java.util.Objects;
Jonathan Hart855179c2016-04-26 07:40:04 -070071
Pier Ventree0ae7a32016-11-23 09:57:42 -080072import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chand05f54b2017-02-13 11:56:54 -080073import static org.onlab.packet.MacAddress.BROADCAST;
74import static org.onlab.packet.MacAddress.NONE;
Yi Tsengef19de12017-04-24 11:33:05 -070075import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*;
Jonathan Hart855179c2016-04-26 07:40:04 -070076import static org.slf4j.LoggerFactory.getLogger;
77
Saurav Das558afec2015-05-31 17:12:48 -070078
79/**
Charles Chan40132b32017-01-22 00:19:37 -080080 * Driver for software switch emulation of the OFDPA pipeline.
Saurav Das52025962016-01-28 22:30:01 -080081 * The software switch is the CPqD OF 1.3 switch. Unfortunately the CPqD switch
82 * does not handle vlan tags and mpls labels simultaneously, which requires us
83 * to do some workarounds in the driver. This driver is meant for the use of
84 * the cpqd switch when MPLS is required. As a result this driver works only
85 * on incoming untagged packets.
Saurav Das558afec2015-05-31 17:12:48 -070086 */
Charles Chan361154b2016-03-24 10:23:39 -070087public class CpqdOfdpa2Pipeline extends Ofdpa2Pipeline {
Saurav Das558afec2015-05-31 17:12:48 -070088
89 private final Logger log = getLogger(getClass());
90
Charles Chan40132b32017-01-22 00:19:37 -080091 /**
Charles Chan0f43e472017-02-14 14:00:16 -080092 * Table that determines whether VLAN is popped before punting to controller.
93 * <p>
94 * This is a non-OFDPA table to emulate OFDPA packet in behavior.
95 * VLAN will be popped before punting if the VLAN is internally assigned.
96 * <p>
97 * Also note that 63 is the max table number in CpqD.
98 */
99 private static final int PUNT_TABLE = 63;
100
101 /**
102 * A static indirect group that pop vlan and punt to controller.
103 * <p>
104 * The purpose of using a group instead of immediate action is that this
105 * won't affect another copy on the data plane when write action exists.
106 */
107 private static final int POP_VLAN_PUNT_GROUP_ID = 0xc0000000;
108
Charles Chan053b1cb2017-03-22 16:56:35 -0700109 @Override
110 protected boolean requireVlanExtensions() {
111 return false;
112 }
113
Charles Chan0f43e472017-02-14 14:00:16 -0800114 /**
Charles Chan40132b32017-01-22 00:19:37 -0800115 * Determines whether this pipeline support copy ttl instructions or not.
116 *
117 * @return true if copy ttl instructions are supported
118 */
119 protected boolean supportCopyTtl() {
120 return true;
121 }
122
Charles Chan0f43e472017-02-14 14:00:16 -0800123 /**
124 * Determines whether this pipeline support push mpls to vlan-tagged packets or not.
125 * <p>
126 * If not support, pop vlan before push entering unicast and mpls table.
127 * Side effect: HostService learns redundant hosts with same MAC but
128 * different VLAN. No known side effect on the network reachability.
129 *
130 * @return true if push mpls to vlan-tagged packets is supported
131 */
132 protected boolean supportTaggedMpls() {
133 return false;
134 }
135
136 /**
137 * Determines whether this pipeline support punt action in group bucket.
138 *
139 * @return true if punt action in group bucket is supported
140 */
141 protected boolean supportPuntGroup() {
142 return false;
143 }
144
Charles Chan425854b2016-04-11 15:32:12 -0700145 @Override
Charles Chan40132b32017-01-22 00:19:37 -0800146 protected void initDriverId() {
Charles Chan425854b2016-04-11 15:32:12 -0700147 driverId = coreService.registerApplication(
148 "org.onosproject.driver.CpqdOfdpa2Pipeline");
Charles Chan40132b32017-01-22 00:19:37 -0800149 }
Charles Chan425854b2016-04-11 15:32:12 -0700150
Charles Chan40132b32017-01-22 00:19:37 -0800151 @Override
152 protected void initGroupHander(PipelinerContext context) {
153 groupHandler = new CpqdOfdpa2GroupHandler();
154 groupHandler.init(deviceId, context);
Charles Chan425854b2016-04-11 15:32:12 -0700155 }
156
Saurav Das4ce45962015-11-24 23:21:05 -0800157 /*
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700158 * Cpqd emulation does not require the non OF-standard rules for
159 * matching untagged packets that ofdpa uses.
Saurav Das4ce45962015-11-24 23:21:05 -0800160 *
161 * (non-Javadoc)
162 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processVlanIdFilter
163 */
Saurav Das558afec2015-05-31 17:12:48 -0700164 @Override
Saurav Das2857f382015-11-03 14:39:27 -0800165 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
166 VlanIdCriterion vidCriterion,
167 VlanId assignedVlan,
168 ApplicationId applicationId) {
Charles Chan79769232016-07-05 16:34:39 -0700169 List<FlowRule> rules = new ArrayList<>();
Saurav Das2857f382015-11-03 14:39:27 -0800170 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
171 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
172 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800173 treatment.transition(TMAC_TABLE);
174
Saurav Das2857f382015-11-03 14:39:27 -0800175 if (vidCriterion.vlanId() == VlanId.NONE) {
176 // untagged packets are assigned vlans
177 treatment.pushVlan().setVlanId(assignedVlan);
178 }
Saurav Das2857f382015-11-03 14:39:27 -0800179
180 // ofdpa cannot match on ALL portnumber, so we need to use separate
181 // rules for each port.
182 List<PortNumber> portnums = new ArrayList<PortNumber>();
183 if (portCriterion.port() == PortNumber.ALL) {
184 for (Port port : deviceService.getPorts(deviceId)) {
185 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
186 portnums.add(port.number());
187 }
188 }
189 } else {
190 portnums.add(portCriterion.port());
191 }
Saurav Das4f980082015-11-05 13:39:15 -0800192
Saurav Das2857f382015-11-03 14:39:27 -0800193 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800194 // NOTE: Emulating OFDPA behavior by popping off internal assigned
195 // VLAN before sending to controller
196 if (supportPuntGroup() && vidCriterion.vlanId() == VlanId.NONE) {
Yi Tsengef19de12017-04-24 11:33:05 -0700197 GroupKey groupKey = popVlanPuntGroupKey();
Charles Chan0f43e472017-02-14 14:00:16 -0800198 Group group = groupService.getGroup(deviceId, groupKey);
199 if (group != null) {
200 rules.add(buildPuntTableRule(pnum, assignedVlan));
201 } else {
202 log.info("popVlanPuntGroup not found in dev:{}", deviceId);
203 return Collections.emptyList();
204 }
205 }
206
Saurav Das4f980082015-11-05 13:39:15 -0800207 // create rest of flowrule
Saurav Das2857f382015-11-03 14:39:27 -0800208 selector.matchInPort(pnum);
209 FlowRule rule = DefaultFlowRule.builder()
210 .forDevice(deviceId)
211 .withSelector(selector.build())
212 .withTreatment(treatment.build())
213 .withPriority(DEFAULT_PRIORITY)
214 .fromApp(applicationId)
215 .makePermanent()
216 .forTable(VLAN_TABLE).build();
217 rules.add(rule);
218 }
Charles Chanf57a8252016-06-29 19:12:37 -0700219
Saurav Das2857f382015-11-03 14:39:27 -0800220 return rules;
221 }
222
Pier Ventree0ae7a32016-11-23 09:57:42 -0800223 /**
Charles Chan0f43e472017-02-14 14:00:16 -0800224 * Creates punt table entry that matches IN_PORT and VLAN_VID and points to
225 * a group that pop vlan and punt.
226 *
227 * @param portNumber port number
228 * @param assignedVlan internally assigned vlan id
229 * @return punt table flow rule
230 */
231 private FlowRule buildPuntTableRule(PortNumber portNumber, VlanId assignedVlan) {
232 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
233 .matchInPort(portNumber)
234 .matchVlanId(assignedVlan);
235 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
Yi Tsengfa394de2017-02-01 11:26:40 -0800236 .group(new GroupId(POP_VLAN_PUNT_GROUP_ID));
Charles Chan0f43e472017-02-14 14:00:16 -0800237
238 return DefaultFlowRule.builder()
239 .forDevice(deviceId)
240 .withSelector(sbuilder.build())
241 .withTreatment(tbuilder.build())
242 .withPriority(PacketPriority.CONTROL.priorityValue())
243 .fromApp(driverId)
244 .makePermanent()
245 .forTable(PUNT_TABLE).build();
246 }
247
248 /**
Pier Ventree0ae7a32016-11-23 09:57:42 -0800249 * Builds a punt to the controller rule for the arp protocol.
Charles Chan0f43e472017-02-14 14:00:16 -0800250 * <p>
251 * NOTE: CpqD cannot punt correctly in group bucket. The current impl will
252 * pop VLAN before sending to controller disregarding whether
253 * it's an internally assigned VLAN or a natural VLAN.
254 * Therefore, trunk port is not supported in CpqD.
Pier Ventree0ae7a32016-11-23 09:57:42 -0800255 *
256 * @param assignedVlan the internal assigned vlan id
257 * @param applicationId the application id
258 * @return the punt flow rule for the arp
259 */
260 private FlowRule buildArpPunt(VlanId assignedVlan, ApplicationId applicationId) {
261 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
262 .matchEthType(Ethernet.TYPE_ARP)
263 .matchVlanId(assignedVlan);
264 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
265 .popVlan()
266 .punt();
267
268 return DefaultFlowRule.builder()
269 .forDevice(deviceId)
270 .withSelector(sbuilder.build())
271 .withTreatment(tbuilder.build())
272 .withPriority(PacketPriority.CONTROL.priorityValue() + 1)
273 .fromApp(applicationId)
274 .makePermanent()
275 .forTable(ACL_TABLE).build();
276 }
277
278 /**
279 * Builds a punt to the controller rule for the icmp v6 messages.
Charles Chan0f43e472017-02-14 14:00:16 -0800280 * <p>
281 * NOTE: CpqD cannot punt correctly in group bucket. The current impl will
282 * pop VLAN before sending to controller disregarding whether
283 * it's an internally assigned VLAN or a natural VLAN.
284 * Therefore, trunk port is not supported in CpqD.
Pier Ventree0ae7a32016-11-23 09:57:42 -0800285 *
286 * @param assignedVlan the internal assigned vlan id
287 * @param applicationId the application id
288 * @return the punt flow rule for the icmp v6 messages
289 */
290 private FlowRule buildIcmpV6Punt(VlanId assignedVlan, ApplicationId applicationId) {
291 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder()
292 .matchVlanId(assignedVlan)
293 .matchEthType(Ethernet.TYPE_IPV6)
294 .matchIPProtocol(PROTOCOL_ICMP6);
295 TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder()
296 .popVlan()
297 .punt();
298
299 return DefaultFlowRule.builder()
300 .forDevice(deviceId)
301 .withSelector(sbuilder.build())
302 .withTreatment(tbuilder.build())
303 .withPriority(PacketPriority.CONTROL.priorityValue() + 1)
304 .fromApp(applicationId)
305 .makePermanent()
306 .forTable(ACL_TABLE).build();
307 }
308
Saurav Das4ce45962015-11-24 23:21:05 -0800309 /*
310 * Cpqd emulation does not handle vlan tags and mpls labels correctly.
311 * Workaround requires popping off the VLAN tags in the TMAC table.
312 *
313 * (non-Javadoc)
314 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthDstFilter
315 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800316 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800317 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
318 EthCriterion ethCriterion,
319 VlanIdCriterion vidCriterion,
320 VlanId assignedVlan,
321 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800322 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
323 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
324 return processEthDstOnlyFilter(ethCriterion, applicationId);
325 }
326
Charles Chan5b9df8d2016-03-28 22:21:40 -0700327 // Multicast MAC
328 if (ethCriterion.mask() != null) {
329 return processMcastEthDstFilter(ethCriterion, applicationId);
330 }
331
Saurav Das4ce45962015-11-24 23:21:05 -0800332 //handling untagged packets via assigned VLAN
333 if (vidCriterion.vlanId() == VlanId.NONE) {
334 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
335 }
336 // ofdpa cannot match on ALL portnumber, so we need to use separate
337 // rules for each port.
338 List<PortNumber> portnums = new ArrayList<PortNumber>();
339 if (portCriterion.port() == PortNumber.ALL) {
340 for (Port port : deviceService.getPorts(deviceId)) {
341 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
342 portnums.add(port.number());
343 }
344 }
345 } else {
346 portnums.add(portCriterion.port());
347 }
348
349 List<FlowRule> rules = new ArrayList<FlowRule>();
350 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800351 // TMAC rules for unicast IP packets
Saurav Das4ce45962015-11-24 23:21:05 -0800352 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
353 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
354 selector.matchInPort(pnum);
355 selector.matchVlanId(vidCriterion.vlanId());
356 selector.matchEthType(Ethernet.TYPE_IPV4);
357 selector.matchEthDst(ethCriterion.mac());
Charles Chan0f43e472017-02-14 14:00:16 -0800358 if (!supportTaggedMpls()) {
359 treatment.popVlan();
360 }
Saurav Das4ce45962015-11-24 23:21:05 -0800361 treatment.transition(UNICAST_ROUTING_TABLE);
362 FlowRule rule = DefaultFlowRule.builder()
363 .forDevice(deviceId)
364 .withSelector(selector.build())
365 .withTreatment(treatment.build())
366 .withPriority(DEFAULT_PRIORITY)
367 .fromApp(applicationId)
368 .makePermanent()
369 .forTable(TMAC_TABLE).build();
370 rules.add(rule);
Charles Chan0f43e472017-02-14 14:00:16 -0800371
372 // TMAC rules for MPLS packets
Saurav Das4ce45962015-11-24 23:21:05 -0800373 selector = DefaultTrafficSelector.builder();
374 treatment = DefaultTrafficTreatment.builder();
375 selector.matchInPort(pnum);
376 selector.matchVlanId(vidCriterion.vlanId());
377 selector.matchEthType(Ethernet.MPLS_UNICAST);
378 selector.matchEthDst(ethCriterion.mac());
Charles Chan0f43e472017-02-14 14:00:16 -0800379 if (!supportTaggedMpls()) {
380 treatment.popVlan();
381 }
Saurav Das4ce45962015-11-24 23:21:05 -0800382 treatment.transition(MPLS_TABLE_0);
383 rule = DefaultFlowRule.builder()
384 .forDevice(deviceId)
385 .withSelector(selector.build())
386 .withTreatment(treatment.build())
387 .withPriority(DEFAULT_PRIORITY)
388 .fromApp(applicationId)
389 .makePermanent()
390 .forTable(TMAC_TABLE).build();
391 rules.add(rule);
Charles Chan0f43e472017-02-14 14:00:16 -0800392
393 // TMAC rules for IPv6 packets
Pier Ventree0ae7a32016-11-23 09:57:42 -0800394 selector = DefaultTrafficSelector.builder();
395 treatment = DefaultTrafficTreatment.builder();
396 selector.matchInPort(pnum);
397 selector.matchVlanId(vidCriterion.vlanId());
398 selector.matchEthType(Ethernet.TYPE_IPV6);
399 selector.matchEthDst(ethCriterion.mac());
Charles Chan0f43e472017-02-14 14:00:16 -0800400 if (!supportTaggedMpls()) {
401 treatment.popVlan();
402 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800403 treatment.transition(UNICAST_ROUTING_TABLE);
404 rule = DefaultFlowRule.builder()
405 .forDevice(deviceId)
406 .withSelector(selector.build())
407 .withTreatment(treatment.build())
408 .withPriority(DEFAULT_PRIORITY)
409 .fromApp(applicationId)
410 .makePermanent()
411 .forTable(TMAC_TABLE).build();
412 rules.add(rule);
Saurav Das4ce45962015-11-24 23:21:05 -0800413 }
414 return rules;
415 }
416
Charles Chan5270ed02016-01-30 23:22:37 -0800417 @Override
418 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
Yi Tsengef19de12017-04-24 11:33:05 -0700419 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800420 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
421 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
422 selector.matchEthType(Ethernet.TYPE_IPV4);
423 selector.matchEthDst(ethCriterion.mac());
Charles Chan0f43e472017-02-14 14:00:16 -0800424 if (!supportTaggedMpls()) {
425 treatment.popVlan();
426 }
Charles Chan5270ed02016-01-30 23:22:37 -0800427 treatment.transition(UNICAST_ROUTING_TABLE);
428 FlowRule rule = DefaultFlowRule.builder()
429 .forDevice(deviceId)
430 .withSelector(selector.build())
431 .withTreatment(treatment.build())
432 .withPriority(DEFAULT_PRIORITY)
433 .fromApp(applicationId)
434 .makePermanent()
435 .forTable(TMAC_TABLE).build();
436 return ImmutableList.<FlowRule>builder().add(rule).build();
437 }
438
Saurav Das4ce45962015-11-24 23:21:05 -0800439 /*
440 * Cpqd emulation allows MPLS ecmp.
441 *
442 * (non-Javadoc)
443 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthTypeSpecific
444 */
445 @Override
446 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800447 TrafficSelector selector = fwd.selector();
448 EthTypeCriterion ethType =
449 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
450 if ((ethType == null) ||
451 (ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
Pier Ventree0ae7a32016-11-23 09:57:42 -0800452 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST) &&
453 (ethType.ethType().toShort() != Ethernet.TYPE_IPV6)) {
Saurav Das4ce45962015-11-24 23:21:05 -0800454 log.warn("processSpecific: Unsupported forwarding objective criteria"
455 + "ethType:{} in dev:{}", ethType, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800456 fail(fwd, ObjectiveError.UNSUPPORTED);
457 return Collections.emptySet();
458 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800459 boolean defaultRule = false;
Saurav Das8a0732e2015-11-20 15:27:53 -0800460 int forTableId = -1;
461 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800462 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
463
Saurav Das8a0732e2015-11-20 15:27:53 -0800464 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800465 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700466 if (ipv4Dst.isMulticast()) {
467 if (ipv4Dst.prefixLength() != 32) {
468 log.warn("Multicast specific forwarding objective can only be /32");
469 fail(fwd, ObjectiveError.BADPARAMS);
470 return ImmutableSet.of();
471 }
472 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
473 if (assignedVlan == null) {
474 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
475 fail(fwd, ObjectiveError.BADPARAMS);
476 return ImmutableSet.of();
477 }
478 filteredSelector.matchVlanId(assignedVlan);
479 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
480 forTableId = MULTICAST_ROUTING_TABLE;
481 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
482 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800483 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700484 if (ipv4Dst.prefixLength() == 0) {
485 // The entire IPV4_DST field is wildcarded intentionally
486 filteredSelector.matchEthType(Ethernet.TYPE_IPV4);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700487 } else {
Charles Chanf9e98652016-09-07 16:54:23 -0700488 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
Charles Chan5b9df8d2016-03-28 22:21:40 -0700489 }
490 forTableId = UNICAST_ROUTING_TABLE;
491 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
492 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800493 }
Pier Ventree0ae7a32016-11-23 09:57:42 -0800494 } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
Julia Ferguson65428c32017-08-10 18:15:24 +0000495 IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip();
496 if (ipv6Dst.isMulticast()) {
497 if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) {
498 log.debug("Multicast specific IPv6 forwarding objective can only be /128");
499 fail(fwd, ObjectiveError.BADPARAMS);
500 return ImmutableSet.of();
501 }
502 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
503 if (assignedVlan == null) {
504 log.debug("VLAN ID required by multicast specific fwd obj is missing. Abort.");
505 fail(fwd, ObjectiveError.BADPARAMS);
506 return ImmutableSet.of();
507 }
508 filteredSelector.matchVlanId(assignedVlan);
509 filteredSelector.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst);
510 forTableId = MULTICAST_ROUTING_TABLE;
511 log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}"
512 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
513 } else {
514 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
515 return Collections.emptyList();
516 }
517 forTableId = UNICAST_ROUTING_TABLE;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800518 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800519 } else {
520 filteredSelector
521 .matchEthType(Ethernet.MPLS_UNICAST)
522 .matchMplsLabel(((MplsCriterion)
523 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
524 MplsBosCriterion bos = (MplsBosCriterion) selector
525 .getCriterion(Criterion.Type.MPLS_BOS);
526 if (bos != null) {
527 filteredSelector.matchMplsBos(bos.mplsBos());
528 }
529 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800530 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
531 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800532 }
533
534 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
535 if (fwd.treatment() != null) {
536 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chan40132b32017-01-22 00:19:37 -0800537 if (!supportCopyTtl() && i instanceof L3ModificationInstruction) {
538 L3ModificationInstruction l3instr = (L3ModificationInstruction) i;
539 if (l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_IN) ||
540 l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_OUT)) {
541 continue;
542 }
543 }
Charles Chan7d10b162015-12-07 18:54:45 -0800544 /*
545 * NOTE: OF-DPA does not support immediate instruction in
546 * L3 unicast and MPLS table.
547 */
548 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800549 }
550 }
551
552 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800553 NextGroup next = getGroupForNextObjective(fwd.nextId());
554 if (next != null) {
555 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
556 // we only need the top level group's key to point the flow to it
557 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
558 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700559 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
560 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das423fe2b2015-12-04 10:52:59 -0800561 fail(fwd, ObjectiveError.GROUPMISSING);
562 return Collections.emptySet();
563 }
564 tb.deferred().group(group.id());
Saurav Das8a0732e2015-11-20 15:27:53 -0800565 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800566 }
567 tb.transition(ACL_TABLE);
568 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
569 .fromApp(fwd.appId())
570 .withPriority(fwd.priority())
571 .forDevice(deviceId)
572 .withSelector(filteredSelector.build())
573 .withTreatment(tb.build())
574 .forTable(forTableId);
575
576 if (fwd.permanent()) {
577 ruleBuilder.makePermanent();
578 } else {
579 ruleBuilder.makeTemporary(fwd.timeout());
580 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800581 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
582 flowRuleCollection.add(ruleBuilder.build());
583 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800584 flowRuleCollection.add(
585 defaultRoute(fwd, complementarySelector, forTableId, tb)
586 );
Flavio Castroe10fa242016-01-15 12:43:51 -0800587 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
588 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800589 return flowRuleCollection;
Saurav Das8a0732e2015-11-20 15:27:53 -0800590 }
591
Charles Chan1e492d32016-01-30 23:22:37 -0800592 @Override
593 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
594 List<FlowRule> rules = new ArrayList<>();
595
596 // Build filtered selector
597 TrafficSelector selector = fwd.selector();
598 EthCriterion ethCriterion = (EthCriterion) selector
599 .getCriterion(Criterion.Type.ETH_DST);
600 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
601 .getCriterion(Criterion.Type.VLAN_VID);
602
603 if (vlanIdCriterion == null) {
604 log.warn("Forwarding objective for bridging requires vlan. Not "
605 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
606 fail(fwd, ObjectiveError.BADPARAMS);
607 return Collections.emptySet();
608 }
609
610 TrafficSelector.Builder filteredSelectorBuilder =
611 DefaultTrafficSelector.builder();
612 // Do not match MacAddress for subnet broadcast entry
Charles Chand05f54b2017-02-13 11:56:54 -0800613 if (!ethCriterion.mac().equals(NONE) && !ethCriterion.mac().equals(BROADCAST)) {
Charles Chan1e492d32016-01-30 23:22:37 -0800614 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
615 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
616 fwd.id(), fwd.nextId(), deviceId);
617 } else {
618 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
619 + "in dev:{} for vlan:{}",
620 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
621 }
622 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
623 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
624
625 if (fwd.treatment() != null) {
626 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
627 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
628 }
629
630 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
631 if (fwd.nextId() != null) {
632 NextGroup next = getGroupForNextObjective(fwd.nextId());
633 if (next != null) {
634 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
635 // we only need the top level group's key to point the flow to it
636 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
637 if (group != null) {
638 treatmentBuilder.deferred().group(group.id());
639 } else {
640 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
641 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
642 fail(fwd, ObjectiveError.GROUPMISSING);
643 return Collections.emptySet();
644 }
645 }
646 }
647 treatmentBuilder.immediate().transition(ACL_TABLE);
648 TrafficTreatment filteredTreatment = treatmentBuilder.build();
649
650 // Build bridging table entries
651 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
652 flowRuleBuilder.fromApp(fwd.appId())
653 .withPriority(fwd.priority())
654 .forDevice(deviceId)
655 .withSelector(filteredSelector)
656 .withTreatment(filteredTreatment)
657 .forTable(BRIDGING_TABLE);
658 if (fwd.permanent()) {
659 flowRuleBuilder.makePermanent();
660 } else {
661 flowRuleBuilder.makeTemporary(fwd.timeout());
662 }
663 rules.add(flowRuleBuilder.build());
664 return rules;
665 }
666
Saurav Das52025962016-01-28 22:30:01 -0800667 /*
668 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
669 * ACL table. Because we pop off vlan tags in TMAC table,
670 * we need to avoid matching on vlans in the ACL table.
671 */
672 @Override
673 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
674 log.info("Processing versatile forwarding objective");
675
676 EthTypeCriterion ethType =
677 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
678 if (ethType == null) {
679 log.error("Versatile forwarding objective must include ethType");
680 fail(fwd, ObjectiveError.BADPARAMS);
681 return Collections.emptySet();
682 }
683 if (fwd.nextId() == null && fwd.treatment() == null) {
684 log.error("Forwarding objective {} from {} must contain "
685 + "nextId or Treatment", fwd.selector(), fwd.appId());
686 return Collections.emptySet();
687 }
688
689 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
690 fwd.selector().criteria().forEach(criterion -> {
691 if (criterion instanceof VlanIdCriterion) {
692 // avoid matching on vlans
693 return;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800694 } else if (criterion instanceof Icmpv6TypeCriterion ||
695 criterion instanceof Icmpv6CodeCriterion) {
696 /*
697 * We silenty discard these criterions, our current
698 * OFDPA platform does not support these matches on
699 * the ACL table.
700 */
701 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das52025962016-01-28 22:30:01 -0800702 } else {
703 sbuilder.add(criterion);
704 }
705 });
706
707 // XXX driver does not currently do type checking as per Tables 65-67 in
708 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
709 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
710 if (fwd.treatment() != null) {
711 for (Instruction ins : fwd.treatment().allInstructions()) {
712 if (ins instanceof OutputInstruction) {
713 OutputInstruction o = (OutputInstruction) ins;
714 if (o.port() == PortNumber.CONTROLLER) {
Charles Chan0f43e472017-02-14 14:00:16 -0800715 ttBuilder.transition(PUNT_TABLE);
Saurav Das52025962016-01-28 22:30:01 -0800716 } else {
717 log.warn("Only allowed treatments in versatile forwarding "
718 + "objectives are punts to the controller");
719 }
720 } else {
721 log.warn("Cannot process instruction in versatile fwd {}", ins);
722 }
723 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800724 if (fwd.treatment().clearedDeferred()) {
725 ttBuilder.wipeDeferred();
726 }
Saurav Das52025962016-01-28 22:30:01 -0800727 }
728 if (fwd.nextId() != null) {
729 // overide case
730 NextGroup next = getGroupForNextObjective(fwd.nextId());
731 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
732 // we only need the top level group's key to point the flow to it
733 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
734 if (group == null) {
735 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
736 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
737 fail(fwd, ObjectiveError.GROUPMISSING);
738 return Collections.emptySet();
739 }
740 ttBuilder.deferred().group(group.id());
741 }
742
743 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
744 .fromApp(fwd.appId())
745 .withPriority(fwd.priority())
746 .forDevice(deviceId)
747 .withSelector(sbuilder.build())
748 .withTreatment(ttBuilder.build())
749 .makePermanent()
750 .forTable(ACL_TABLE);
751 return Collections.singletonList(ruleBuilder.build());
752 }
753
754 /*
755 * Cpqd emulation requires table-miss-entries in forwarding tables.
756 * Real OFDPA does not require these rules as they are put in by default.
757 *
758 * (non-Javadoc)
759 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#initializePipeline()
760 */
Saurav Das2857f382015-11-03 14:39:27 -0800761 @Override
Saurav Das558afec2015-05-31 17:12:48 -0700762 protected void initializePipeline() {
Charles Chanf6ec1532017-02-08 16:10:40 -0800763 initTableMiss(PORT_TABLE, VLAN_TABLE, null);
764 initTableMiss(VLAN_TABLE, ACL_TABLE, null);
765 initTableMiss(TMAC_TABLE, BRIDGING_TABLE, null);
766 initTableMiss(UNICAST_ROUTING_TABLE, ACL_TABLE, null);
767 initTableMiss(MULTICAST_ROUTING_TABLE, ACL_TABLE, null);
768 initTableMiss(MPLS_TABLE_0, MPLS_TABLE_1, null);
769 initTableMiss(MPLS_TABLE_1, ACL_TABLE, null);
770 initTableMiss(BRIDGING_TABLE, ACL_TABLE, null);
771 initTableMiss(ACL_TABLE, -1, null);
Charles Chan0f43e472017-02-14 14:00:16 -0800772
773 if (supportPuntGroup()) {
774 initTableMiss(PUNT_TABLE, -1,
775 DefaultTrafficTreatment.builder().punt().build());
776 initPopVlanPuntGroup();
777 } else {
778 initTableMiss(PUNT_TABLE, -1,
779 DefaultTrafficTreatment.builder().popVlan().punt().build());
780 }
Saurav Das558afec2015-05-31 17:12:48 -0700781 }
782
Charles Chanf6ec1532017-02-08 16:10:40 -0800783 /**
784 * Install table-miss flow entry.
785 *
786 * If treatment exists, use it directly.
787 * Else if treatment does not exist but nextTable > 0, transit to next table.
788 * Else apply empty treatment.
789 *
790 * @param thisTable this table ID
791 * @param nextTable next table ID
792 * @param treatment traffic treatment to apply.
793 */
794 private void initTableMiss(int thisTable, int nextTable, TrafficTreatment treatment) {
Saurav Das558afec2015-05-31 17:12:48 -0700795 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
Charles Chanf6ec1532017-02-08 16:10:40 -0800796 TrafficSelector selector = DefaultTrafficSelector.builder().build();
Saurav Das558afec2015-05-31 17:12:48 -0700797
Charles Chanf6ec1532017-02-08 16:10:40 -0800798 if (treatment == null) {
799 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
800 if (nextTable > 0) {
801 tBuilder.transition(nextTable);
Saurav Das558afec2015-05-31 17:12:48 -0700802 }
Charles Chanf6ec1532017-02-08 16:10:40 -0800803 treatment = tBuilder.build();
804 }
Saurav Das558afec2015-05-31 17:12:48 -0700805
Charles Chanb7504392017-02-10 12:51:04 -0800806 FlowRule rule = DefaultFlowRule.builder()
807 .forDevice(deviceId)
Charles Chanf6ec1532017-02-08 16:10:40 -0800808 .withSelector(selector)
809 .withTreatment(treatment)
Charles Chanb7504392017-02-10 12:51:04 -0800810 .withPriority(LOWEST_PRIORITY)
811 .fromApp(driverId)
812 .makePermanent()
Charles Chanf6ec1532017-02-08 16:10:40 -0800813 .forTable(thisTable).build();
Charles Chanb7504392017-02-10 12:51:04 -0800814 ops = ops.add(rule);
Saurav Das2857f382015-11-03 14:39:27 -0800815
816 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
817 @Override
818 public void onSuccess(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800819 log.info("Initialized table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -0800820 }
Saurav Das2857f382015-11-03 14:39:27 -0800821 @Override
822 public void onError(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800823 log.warn("Failed to initialize table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -0800824 }
825 }));
826 }
Charles Chan0f43e472017-02-14 14:00:16 -0800827
828 /**
829 * Builds a indirect group contains pop_vlan and punt actions.
830 * <p>
831 * Using group instead of immediate action to ensure that
832 * the copy of packet on the data plane is not affected by the pop vlan action.
833 */
834 private void initPopVlanPuntGroup() {
Yi Tsengef19de12017-04-24 11:33:05 -0700835 GroupKey groupKey = popVlanPuntGroupKey();
Charles Chan0f43e472017-02-14 14:00:16 -0800836 TrafficTreatment bucketTreatment = DefaultTrafficTreatment.builder()
837 .popVlan().punt().build();
838 GroupBucket bucket =
839 DefaultGroupBucket.createIndirectGroupBucket(bucketTreatment);
840 GroupDescription groupDesc =
841 new DefaultGroupDescription(
842 deviceId,
843 GroupDescription.Type.INDIRECT,
844 new GroupBuckets(Collections.singletonList(bucket)),
845 groupKey,
846 POP_VLAN_PUNT_GROUP_ID,
847 driverId);
848 groupService.addGroup(groupDesc);
849
850 log.info("Initialized pop vlan punt group on {}", deviceId);
851 }
Yi Tsengef19de12017-04-24 11:33:05 -0700852
853 /**
854 * Generates group key for a static indirect group that pop vlan and punt to
855 * controller.
856 *
857 * @return the group key of the indirect table
858 */
859 private GroupKey popVlanPuntGroupKey() {
860 int hash = POP_VLAN_PUNT_GROUP_ID | (Objects.hash(deviceId) & FOUR_BIT_MASK);
861 return new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(hash));
862 }
Saurav Das558afec2015-05-31 17:12:48 -0700863}