blob: 7add4d0809be396050da8a74efd927a3be336a32 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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 */
Saurav Das558afec2015-05-31 17:12:48 -070016package org.onosproject.driver.pipeline;
17
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;
Flavio Castroe10fa242016-01-15 12:43:51 -080021import org.onlab.packet.IpPrefix;
Saurav Das2857f382015-11-03 14:39:27 -080022import org.onlab.packet.VlanId;
23import org.onosproject.core.ApplicationId;
Yi Tsengfa394de2017-02-01 11:26:40 -080024import org.onosproject.core.GroupId;
Saurav Das2857f382015-11-03 14:39:27 -080025import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
Saurav Das8a0732e2015-11-20 15:27:53 -080027import org.onosproject.net.behaviour.NextGroup;
Charles Chan425854b2016-04-11 15:32:12 -070028import org.onosproject.net.behaviour.PipelinerContext;
Saurav Das558afec2015-05-31 17:12:48 -070029import org.onosproject.net.flow.DefaultFlowRule;
30import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.FlowRule;
33import org.onosproject.net.flow.FlowRuleOperations;
34import org.onosproject.net.flow.FlowRuleOperationsContext;
35import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
Saurav Das4ce45962015-11-24 23:21:05 -080037import org.onosproject.net.flow.criteria.Criteria;
Saurav Das8a0732e2015-11-20 15:27:53 -080038import org.onosproject.net.flow.criteria.Criterion;
Saurav Das4ce45962015-11-24 23:21:05 -080039import org.onosproject.net.flow.criteria.EthCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080040import org.onosproject.net.flow.criteria.EthTypeCriterion;
41import org.onosproject.net.flow.criteria.IPCriterion;
Pier Ventree0ae7a32016-11-23 09:57:42 -080042import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion;
43import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080044import org.onosproject.net.flow.criteria.MplsBosCriterion;
45import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das2857f382015-11-03 14:39:27 -080046import org.onosproject.net.flow.criteria.PortCriterion;
47import org.onosproject.net.flow.criteria.VlanIdCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080048import org.onosproject.net.flow.instructions.Instruction;
Saurav Das52025962016-01-28 22:30:01 -080049import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Charles Chan40132b32017-01-22 00:19:37 -080050import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Saurav Das8a0732e2015-11-20 15:27:53 -080051import org.onosproject.net.flowobjective.ForwardingObjective;
52import org.onosproject.net.flowobjective.ObjectiveError;
Charles Chan0f43e472017-02-14 14:00:16 -080053import org.onosproject.net.group.DefaultGroupBucket;
54import org.onosproject.net.group.DefaultGroupDescription;
55import org.onosproject.net.group.DefaultGroupKey;
Saurav Das8a0732e2015-11-20 15:27:53 -080056import org.onosproject.net.group.Group;
Charles Chan0f43e472017-02-14 14:00:16 -080057import org.onosproject.net.group.GroupBucket;
58import org.onosproject.net.group.GroupBuckets;
59import org.onosproject.net.group.GroupDescription;
Saurav Das8a0732e2015-11-20 15:27:53 -080060import org.onosproject.net.group.GroupKey;
Charles Chanf57a8252016-06-29 19:12:37 -070061import org.onosproject.net.packet.PacketPriority;
Saurav Das558afec2015-05-31 17:12:48 -070062import org.slf4j.Logger;
63
Jonathan Hart855179c2016-04-26 07:40:04 -070064import java.util.ArrayList;
65import java.util.Collection;
66import java.util.Collections;
67import java.util.Deque;
68import java.util.List;
Charles Chan0f43e472017-02-14 14:00:16 -080069import java.util.Objects;
Jonathan Hart855179c2016-04-26 07:40:04 -070070
Pier Ventree0ae7a32016-11-23 09:57:42 -080071import static org.onlab.packet.IPv6.PROTOCOL_ICMP6;
Charles Chand05f54b2017-02-13 11:56:54 -080072import static org.onlab.packet.MacAddress.BROADCAST;
73import static org.onlab.packet.MacAddress.NONE;
Charles Chan0f43e472017-02-14 14:00:16 -080074import static org.onosproject.driver.pipeline.Ofdpa2GroupHandler.FOUR_BIT_MASK;
Jonathan Hart855179c2016-04-26 07:40:04 -070075import static org.slf4j.LoggerFactory.getLogger;
76
Saurav Das558afec2015-05-31 17:12:48 -070077
78/**
Charles Chan40132b32017-01-22 00:19:37 -080079 * Driver for software switch emulation of the OFDPA pipeline.
Saurav Das52025962016-01-28 22:30:01 -080080 * The software switch is the CPqD OF 1.3 switch. Unfortunately the CPqD switch
81 * does not handle vlan tags and mpls labels simultaneously, which requires us
82 * to do some workarounds in the driver. This driver is meant for the use of
83 * the cpqd switch when MPLS is required. As a result this driver works only
84 * on incoming untagged packets.
Saurav Das558afec2015-05-31 17:12:48 -070085 */
Charles Chan361154b2016-03-24 10:23:39 -070086public class CpqdOfdpa2Pipeline extends Ofdpa2Pipeline {
Saurav Das558afec2015-05-31 17:12:48 -070087
88 private final Logger log = getLogger(getClass());
89
Charles Chan40132b32017-01-22 00:19:37 -080090 /**
Charles Chan0f43e472017-02-14 14:00:16 -080091 * Table that determines whether VLAN is popped before punting to controller.
92 * <p>
93 * This is a non-OFDPA table to emulate OFDPA packet in behavior.
94 * VLAN will be popped before punting if the VLAN is internally assigned.
95 * <p>
96 * Also note that 63 is the max table number in CpqD.
97 */
98 private static final int PUNT_TABLE = 63;
99
100 /**
101 * A static indirect group that pop vlan and punt to controller.
102 * <p>
103 * The purpose of using a group instead of immediate action is that this
104 * won't affect another copy on the data plane when write action exists.
105 */
106 private static final int POP_VLAN_PUNT_GROUP_ID = 0xc0000000;
107
Charles Chan053b1cb2017-03-22 16:56:35 -0700108 @Override
109 protected boolean requireVlanExtensions() {
110 return false;
111 }
112
Charles Chan0f43e472017-02-14 14:00:16 -0800113 /**
Charles Chan40132b32017-01-22 00:19:37 -0800114 * Determines whether this pipeline support copy ttl instructions or not.
115 *
116 * @return true if copy ttl instructions are supported
117 */
118 protected boolean supportCopyTtl() {
119 return true;
120 }
121
Charles Chan0f43e472017-02-14 14:00:16 -0800122 /**
123 * Determines whether this pipeline support push mpls to vlan-tagged packets or not.
124 * <p>
125 * If not support, pop vlan before push entering unicast and mpls table.
126 * Side effect: HostService learns redundant hosts with same MAC but
127 * different VLAN. No known side effect on the network reachability.
128 *
129 * @return true if push mpls to vlan-tagged packets is supported
130 */
131 protected boolean supportTaggedMpls() {
132 return false;
133 }
134
135 /**
136 * Determines whether this pipeline support punt action in group bucket.
137 *
138 * @return true if punt action in group bucket is supported
139 */
140 protected boolean supportPuntGroup() {
141 return false;
142 }
143
Charles Chan425854b2016-04-11 15:32:12 -0700144 @Override
Charles Chan40132b32017-01-22 00:19:37 -0800145 protected void initDriverId() {
Charles Chan425854b2016-04-11 15:32:12 -0700146 driverId = coreService.registerApplication(
147 "org.onosproject.driver.CpqdOfdpa2Pipeline");
Charles Chan40132b32017-01-22 00:19:37 -0800148 }
Charles Chan425854b2016-04-11 15:32:12 -0700149
Charles Chan40132b32017-01-22 00:19:37 -0800150 @Override
151 protected void initGroupHander(PipelinerContext context) {
152 groupHandler = new CpqdOfdpa2GroupHandler();
153 groupHandler.init(deviceId, context);
Charles Chan425854b2016-04-11 15:32:12 -0700154 }
155
Saurav Das4ce45962015-11-24 23:21:05 -0800156 /*
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700157 * Cpqd emulation does not require the non OF-standard rules for
158 * matching untagged packets that ofdpa uses.
Saurav Das4ce45962015-11-24 23:21:05 -0800159 *
160 * (non-Javadoc)
161 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processVlanIdFilter
162 */
Saurav Das558afec2015-05-31 17:12:48 -0700163 @Override
Saurav Das2857f382015-11-03 14:39:27 -0800164 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
165 VlanIdCriterion vidCriterion,
166 VlanId assignedVlan,
167 ApplicationId applicationId) {
Charles Chan79769232016-07-05 16:34:39 -0700168 List<FlowRule> rules = new ArrayList<>();
Saurav Das2857f382015-11-03 14:39:27 -0800169 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
170 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
171 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800172 treatment.transition(TMAC_TABLE);
173
Saurav Das2857f382015-11-03 14:39:27 -0800174 if (vidCriterion.vlanId() == VlanId.NONE) {
175 // untagged packets are assigned vlans
176 treatment.pushVlan().setVlanId(assignedVlan);
177 }
Saurav Das2857f382015-11-03 14:39:27 -0800178
179 // ofdpa cannot match on ALL portnumber, so we need to use separate
180 // rules for each port.
181 List<PortNumber> portnums = new ArrayList<PortNumber>();
182 if (portCriterion.port() == PortNumber.ALL) {
183 for (Port port : deviceService.getPorts(deviceId)) {
184 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
185 portnums.add(port.number());
186 }
187 }
188 } else {
189 portnums.add(portCriterion.port());
190 }
Saurav Das4f980082015-11-05 13:39:15 -0800191
Saurav Das2857f382015-11-03 14:39:27 -0800192 for (PortNumber pnum : portnums) {
Charles Chan0f43e472017-02-14 14:00:16 -0800193 // NOTE: Emulating OFDPA behavior by popping off internal assigned
194 // VLAN before sending to controller
195 if (supportPuntGroup() && vidCriterion.vlanId() == VlanId.NONE) {
196 GroupKey groupKey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(
197 POP_VLAN_PUNT_GROUP_ID | (Objects.hash(deviceId) & FOUR_BIT_MASK)));
198 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,
419 ApplicationId applicationId) {
420 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) {
495 if (buildIpv6Selector(filteredSelector, fwd) < 0) {
496 return Collections.emptyList();
497 }
498 forTableId = UNICAST_ROUTING_TABLE;
Saurav Das8a0732e2015-11-20 15:27:53 -0800499 } else {
500 filteredSelector
501 .matchEthType(Ethernet.MPLS_UNICAST)
502 .matchMplsLabel(((MplsCriterion)
503 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
504 MplsBosCriterion bos = (MplsBosCriterion) selector
505 .getCriterion(Criterion.Type.MPLS_BOS);
506 if (bos != null) {
507 filteredSelector.matchMplsBos(bos.mplsBos());
508 }
509 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800510 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
511 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800512 }
513
514 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
515 if (fwd.treatment() != null) {
516 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chan40132b32017-01-22 00:19:37 -0800517 if (!supportCopyTtl() && i instanceof L3ModificationInstruction) {
518 L3ModificationInstruction l3instr = (L3ModificationInstruction) i;
519 if (l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_IN) ||
520 l3instr.subtype().equals(L3ModificationInstruction.L3SubType.TTL_OUT)) {
521 continue;
522 }
523 }
Charles Chan7d10b162015-12-07 18:54:45 -0800524 /*
525 * NOTE: OF-DPA does not support immediate instruction in
526 * L3 unicast and MPLS table.
527 */
528 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800529 }
530 }
531
532 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800533 NextGroup next = getGroupForNextObjective(fwd.nextId());
534 if (next != null) {
535 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
536 // we only need the top level group's key to point the flow to it
537 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
538 if (group == null) {
Saurav Das1ce0a7b2016-10-21 14:06:29 -0700539 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
540 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
Saurav Das423fe2b2015-12-04 10:52:59 -0800541 fail(fwd, ObjectiveError.GROUPMISSING);
542 return Collections.emptySet();
543 }
544 tb.deferred().group(group.id());
Saurav Das8a0732e2015-11-20 15:27:53 -0800545 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800546 }
547 tb.transition(ACL_TABLE);
548 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
549 .fromApp(fwd.appId())
550 .withPriority(fwd.priority())
551 .forDevice(deviceId)
552 .withSelector(filteredSelector.build())
553 .withTreatment(tb.build())
554 .forTable(forTableId);
555
556 if (fwd.permanent()) {
557 ruleBuilder.makePermanent();
558 } else {
559 ruleBuilder.makeTemporary(fwd.timeout());
560 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800561 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
562 flowRuleCollection.add(ruleBuilder.build());
563 if (defaultRule) {
Pier Ventree0ae7a32016-11-23 09:57:42 -0800564 flowRuleCollection.add(
565 defaultRoute(fwd, complementarySelector, forTableId, tb)
566 );
Flavio Castroe10fa242016-01-15 12:43:51 -0800567 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
568 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800569 return flowRuleCollection;
Saurav Das8a0732e2015-11-20 15:27:53 -0800570 }
571
Charles Chan1e492d32016-01-30 23:22:37 -0800572 @Override
573 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
574 List<FlowRule> rules = new ArrayList<>();
575
576 // Build filtered selector
577 TrafficSelector selector = fwd.selector();
578 EthCriterion ethCriterion = (EthCriterion) selector
579 .getCriterion(Criterion.Type.ETH_DST);
580 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
581 .getCriterion(Criterion.Type.VLAN_VID);
582
583 if (vlanIdCriterion == null) {
584 log.warn("Forwarding objective for bridging requires vlan. Not "
585 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
586 fail(fwd, ObjectiveError.BADPARAMS);
587 return Collections.emptySet();
588 }
589
590 TrafficSelector.Builder filteredSelectorBuilder =
591 DefaultTrafficSelector.builder();
592 // Do not match MacAddress for subnet broadcast entry
Charles Chand05f54b2017-02-13 11:56:54 -0800593 if (!ethCriterion.mac().equals(NONE) && !ethCriterion.mac().equals(BROADCAST)) {
Charles Chan1e492d32016-01-30 23:22:37 -0800594 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
595 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
596 fwd.id(), fwd.nextId(), deviceId);
597 } else {
598 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
599 + "in dev:{} for vlan:{}",
600 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
601 }
602 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
603 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
604
605 if (fwd.treatment() != null) {
606 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
607 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
608 }
609
610 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
611 if (fwd.nextId() != null) {
612 NextGroup next = getGroupForNextObjective(fwd.nextId());
613 if (next != null) {
614 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
615 // we only need the top level group's key to point the flow to it
616 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
617 if (group != null) {
618 treatmentBuilder.deferred().group(group.id());
619 } else {
620 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
621 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
622 fail(fwd, ObjectiveError.GROUPMISSING);
623 return Collections.emptySet();
624 }
625 }
626 }
627 treatmentBuilder.immediate().transition(ACL_TABLE);
628 TrafficTreatment filteredTreatment = treatmentBuilder.build();
629
630 // Build bridging table entries
631 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
632 flowRuleBuilder.fromApp(fwd.appId())
633 .withPriority(fwd.priority())
634 .forDevice(deviceId)
635 .withSelector(filteredSelector)
636 .withTreatment(filteredTreatment)
637 .forTable(BRIDGING_TABLE);
638 if (fwd.permanent()) {
639 flowRuleBuilder.makePermanent();
640 } else {
641 flowRuleBuilder.makeTemporary(fwd.timeout());
642 }
643 rules.add(flowRuleBuilder.build());
644 return rules;
645 }
646
Saurav Das52025962016-01-28 22:30:01 -0800647 /*
648 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
649 * ACL table. Because we pop off vlan tags in TMAC table,
650 * we need to avoid matching on vlans in the ACL table.
651 */
652 @Override
653 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
654 log.info("Processing versatile forwarding objective");
655
656 EthTypeCriterion ethType =
657 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
658 if (ethType == null) {
659 log.error("Versatile forwarding objective must include ethType");
660 fail(fwd, ObjectiveError.BADPARAMS);
661 return Collections.emptySet();
662 }
663 if (fwd.nextId() == null && fwd.treatment() == null) {
664 log.error("Forwarding objective {} from {} must contain "
665 + "nextId or Treatment", fwd.selector(), fwd.appId());
666 return Collections.emptySet();
667 }
668
669 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
670 fwd.selector().criteria().forEach(criterion -> {
671 if (criterion instanceof VlanIdCriterion) {
672 // avoid matching on vlans
673 return;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800674 } else if (criterion instanceof Icmpv6TypeCriterion ||
675 criterion instanceof Icmpv6CodeCriterion) {
676 /*
677 * We silenty discard these criterions, our current
678 * OFDPA platform does not support these matches on
679 * the ACL table.
680 */
681 log.warn("ICMPv6 Type and ICMPv6 Code are not supported");
Saurav Das52025962016-01-28 22:30:01 -0800682 } else {
683 sbuilder.add(criterion);
684 }
685 });
686
687 // XXX driver does not currently do type checking as per Tables 65-67 in
688 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
689 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
690 if (fwd.treatment() != null) {
691 for (Instruction ins : fwd.treatment().allInstructions()) {
692 if (ins instanceof OutputInstruction) {
693 OutputInstruction o = (OutputInstruction) ins;
694 if (o.port() == PortNumber.CONTROLLER) {
Charles Chan0f43e472017-02-14 14:00:16 -0800695 ttBuilder.transition(PUNT_TABLE);
Saurav Das52025962016-01-28 22:30:01 -0800696 } else {
697 log.warn("Only allowed treatments in versatile forwarding "
698 + "objectives are punts to the controller");
699 }
700 } else {
701 log.warn("Cannot process instruction in versatile fwd {}", ins);
702 }
703 }
Charles Chan2df0e8a2017-01-09 11:45:08 -0800704 if (fwd.treatment().clearedDeferred()) {
705 ttBuilder.wipeDeferred();
706 }
Saurav Das52025962016-01-28 22:30:01 -0800707 }
708 if (fwd.nextId() != null) {
709 // overide case
710 NextGroup next = getGroupForNextObjective(fwd.nextId());
711 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
712 // we only need the top level group's key to point the flow to it
713 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
714 if (group == null) {
715 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
716 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
717 fail(fwd, ObjectiveError.GROUPMISSING);
718 return Collections.emptySet();
719 }
720 ttBuilder.deferred().group(group.id());
721 }
722
723 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
724 .fromApp(fwd.appId())
725 .withPriority(fwd.priority())
726 .forDevice(deviceId)
727 .withSelector(sbuilder.build())
728 .withTreatment(ttBuilder.build())
729 .makePermanent()
730 .forTable(ACL_TABLE);
731 return Collections.singletonList(ruleBuilder.build());
732 }
733
734 /*
735 * Cpqd emulation requires table-miss-entries in forwarding tables.
736 * Real OFDPA does not require these rules as they are put in by default.
737 *
738 * (non-Javadoc)
739 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#initializePipeline()
740 */
Saurav Das2857f382015-11-03 14:39:27 -0800741 @Override
Saurav Das558afec2015-05-31 17:12:48 -0700742 protected void initializePipeline() {
Charles Chanf6ec1532017-02-08 16:10:40 -0800743 initTableMiss(PORT_TABLE, VLAN_TABLE, null);
744 initTableMiss(VLAN_TABLE, ACL_TABLE, null);
745 initTableMiss(TMAC_TABLE, BRIDGING_TABLE, null);
746 initTableMiss(UNICAST_ROUTING_TABLE, ACL_TABLE, null);
747 initTableMiss(MULTICAST_ROUTING_TABLE, ACL_TABLE, null);
748 initTableMiss(MPLS_TABLE_0, MPLS_TABLE_1, null);
749 initTableMiss(MPLS_TABLE_1, ACL_TABLE, null);
750 initTableMiss(BRIDGING_TABLE, ACL_TABLE, null);
751 initTableMiss(ACL_TABLE, -1, null);
Charles Chan0f43e472017-02-14 14:00:16 -0800752
753 if (supportPuntGroup()) {
754 initTableMiss(PUNT_TABLE, -1,
755 DefaultTrafficTreatment.builder().punt().build());
756 initPopVlanPuntGroup();
757 } else {
758 initTableMiss(PUNT_TABLE, -1,
759 DefaultTrafficTreatment.builder().popVlan().punt().build());
760 }
Saurav Das558afec2015-05-31 17:12:48 -0700761 }
762
Charles Chanf6ec1532017-02-08 16:10:40 -0800763 /**
764 * Install table-miss flow entry.
765 *
766 * If treatment exists, use it directly.
767 * Else if treatment does not exist but nextTable > 0, transit to next table.
768 * Else apply empty treatment.
769 *
770 * @param thisTable this table ID
771 * @param nextTable next table ID
772 * @param treatment traffic treatment to apply.
773 */
774 private void initTableMiss(int thisTable, int nextTable, TrafficTreatment treatment) {
Saurav Das558afec2015-05-31 17:12:48 -0700775 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
Charles Chanf6ec1532017-02-08 16:10:40 -0800776 TrafficSelector selector = DefaultTrafficSelector.builder().build();
Saurav Das558afec2015-05-31 17:12:48 -0700777
Charles Chanf6ec1532017-02-08 16:10:40 -0800778 if (treatment == null) {
779 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
780 if (nextTable > 0) {
781 tBuilder.transition(nextTable);
Saurav Das558afec2015-05-31 17:12:48 -0700782 }
Charles Chanf6ec1532017-02-08 16:10:40 -0800783 treatment = tBuilder.build();
784 }
Saurav Das558afec2015-05-31 17:12:48 -0700785
Charles Chanb7504392017-02-10 12:51:04 -0800786 FlowRule rule = DefaultFlowRule.builder()
787 .forDevice(deviceId)
Charles Chanf6ec1532017-02-08 16:10:40 -0800788 .withSelector(selector)
789 .withTreatment(treatment)
Charles Chanb7504392017-02-10 12:51:04 -0800790 .withPriority(LOWEST_PRIORITY)
791 .fromApp(driverId)
792 .makePermanent()
Charles Chanf6ec1532017-02-08 16:10:40 -0800793 .forTable(thisTable).build();
Charles Chanb7504392017-02-10 12:51:04 -0800794 ops = ops.add(rule);
Saurav Das2857f382015-11-03 14:39:27 -0800795
796 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
797 @Override
798 public void onSuccess(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800799 log.info("Initialized table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -0800800 }
Saurav Das2857f382015-11-03 14:39:27 -0800801 @Override
802 public void onError(FlowRuleOperations ops) {
Charles Chanf6ec1532017-02-08 16:10:40 -0800803 log.warn("Failed to initialize table {} on {}", thisTable, deviceId);
Saurav Das2857f382015-11-03 14:39:27 -0800804 }
805 }));
806 }
Charles Chan0f43e472017-02-14 14:00:16 -0800807
808 /**
809 * Builds a indirect group contains pop_vlan and punt actions.
810 * <p>
811 * Using group instead of immediate action to ensure that
812 * the copy of packet on the data plane is not affected by the pop vlan action.
813 */
814 private void initPopVlanPuntGroup() {
815 GroupKey groupKey = new DefaultGroupKey(Ofdpa2Pipeline.appKryo.serialize(
816 POP_VLAN_PUNT_GROUP_ID | (Objects.hash(deviceId) & FOUR_BIT_MASK)));
817 TrafficTreatment bucketTreatment = DefaultTrafficTreatment.builder()
818 .popVlan().punt().build();
819 GroupBucket bucket =
820 DefaultGroupBucket.createIndirectGroupBucket(bucketTreatment);
821 GroupDescription groupDesc =
822 new DefaultGroupDescription(
823 deviceId,
824 GroupDescription.Type.INDIRECT,
825 new GroupBuckets(Collections.singletonList(bucket)),
826 groupKey,
827 POP_VLAN_PUNT_GROUP_ID,
828 driverId);
829 groupService.addGroup(groupDesc);
830
831 log.info("Initialized pop vlan punt group on {}", deviceId);
832 }
Saurav Das558afec2015-05-31 17:12:48 -0700833}