blob: 082adbb34781d637eb640d3dc09e4dfdb0cde082 [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;
Jonathan Hart855179c2016-04-26 07:40:04 -070022import org.onlab.packet.MacAddress;
Saurav Das2857f382015-11-03 14:39:27 -080023import org.onlab.packet.VlanId;
24import org.onosproject.core.ApplicationId;
Charles Chan425854b2016-04-11 15:32:12 -070025import org.onosproject.core.CoreService;
26import org.onosproject.net.DeviceId;
Saurav Das2857f382015-11-03 14:39:27 -080027import org.onosproject.net.Port;
28import org.onosproject.net.PortNumber;
Saurav Das8a0732e2015-11-20 15:27:53 -080029import org.onosproject.net.behaviour.NextGroup;
Charles Chan425854b2016-04-11 15:32:12 -070030import org.onosproject.net.behaviour.PipelinerContext;
31import org.onosproject.net.device.DeviceService;
Saurav Das558afec2015-05-31 17:12:48 -070032import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.FlowRule;
36import org.onosproject.net.flow.FlowRuleOperations;
37import org.onosproject.net.flow.FlowRuleOperationsContext;
Charles Chan425854b2016-04-11 15:32:12 -070038import org.onosproject.net.flow.FlowRuleService;
Saurav Das558afec2015-05-31 17:12:48 -070039import 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;
46import org.onosproject.net.flow.criteria.MplsBosCriterion;
47import org.onosproject.net.flow.criteria.MplsCriterion;
Saurav Das2857f382015-11-03 14:39:27 -080048import org.onosproject.net.flow.criteria.PortCriterion;
49import org.onosproject.net.flow.criteria.VlanIdCriterion;
Saurav Das8a0732e2015-11-20 15:27:53 -080050import org.onosproject.net.flow.instructions.Instruction;
Saurav Das52025962016-01-28 22:30:01 -080051import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
52import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
53import org.onosproject.net.flowobjective.FilteringObjective;
Saurav Das8a0732e2015-11-20 15:27:53 -080054import org.onosproject.net.flowobjective.ForwardingObjective;
55import org.onosproject.net.flowobjective.ObjectiveError;
56import org.onosproject.net.group.Group;
57import org.onosproject.net.group.GroupKey;
Charles Chan425854b2016-04-11 15:32:12 -070058import org.onosproject.net.group.GroupService;
Saurav Das558afec2015-05-31 17:12:48 -070059import org.slf4j.Logger;
60
Jonathan Hart855179c2016-04-26 07:40:04 -070061import java.util.ArrayList;
62import java.util.Collection;
63import java.util.Collections;
64import java.util.Deque;
65import java.util.List;
66
67import static org.slf4j.LoggerFactory.getLogger;
68
Saurav Das558afec2015-05-31 17:12:48 -070069
70/**
Saurav Das822c4e22015-10-23 10:51:11 -070071 * Driver for software switch emulation of the OFDPA 2.0 pipeline.
Saurav Das52025962016-01-28 22:30:01 -080072 * The software switch is the CPqD OF 1.3 switch. Unfortunately the CPqD switch
73 * does not handle vlan tags and mpls labels simultaneously, which requires us
74 * to do some workarounds in the driver. This driver is meant for the use of
75 * the cpqd switch when MPLS is required. As a result this driver works only
76 * on incoming untagged packets.
Saurav Das558afec2015-05-31 17:12:48 -070077 */
Charles Chan361154b2016-03-24 10:23:39 -070078public class CpqdOfdpa2Pipeline extends Ofdpa2Pipeline {
Saurav Das558afec2015-05-31 17:12:48 -070079
80 private final Logger log = getLogger(getClass());
81
Charles Chan425854b2016-04-11 15:32:12 -070082 @Override
83 public void init(DeviceId deviceId, PipelinerContext context) {
84 this.deviceId = deviceId;
85
86 // Initialize OFDPA group handler
87 groupHandler = new CpqdOfdpa2GroupHandler();
88 groupHandler.init(deviceId, context);
89
90 serviceDirectory = context.directory();
91 coreService = serviceDirectory.get(CoreService.class);
92 flowRuleService = serviceDirectory.get(FlowRuleService.class);
93 groupService = serviceDirectory.get(GroupService.class);
94 flowObjectiveStore = context.store();
95 deviceService = serviceDirectory.get(DeviceService.class);
96
97 driverId = coreService.registerApplication(
98 "org.onosproject.driver.CpqdOfdpa2Pipeline");
99
100 initializePipeline();
101 }
102
Saurav Das4ce45962015-11-24 23:21:05 -0800103 /*
Saurav Das52025962016-01-28 22:30:01 -0800104 * CPQD emulation does not require special untagged packet handling, unlike
105 * the real ofdpa.
106 */
107 @Override
108 protected void processFilter(FilteringObjective filt,
109 boolean install, ApplicationId applicationId) {
110 // This driver only processes filtering criteria defined with switch
111 // ports as the key
112 PortCriterion portCriterion = null;
113 EthCriterion ethCriterion = null;
114 VlanIdCriterion vidCriterion = null;
115 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
116 if (!filt.key().equals(Criteria.dummy()) &&
117 filt.key().type() == Criterion.Type.IN_PORT) {
118 portCriterion = (PortCriterion) filt.key();
119 } else {
120 log.warn("No key defined in filtering objective from app: {}. Not"
121 + "processing filtering objective", applicationId);
122 fail(filt, ObjectiveError.UNKNOWN);
123 return;
124 }
125 // convert filtering conditions for switch-intfs into flowrules
126 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
127 for (Criterion criterion : filt.conditions()) {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700128 if (criterion.type() == Criterion.Type.ETH_DST ||
129 criterion.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Das52025962016-01-28 22:30:01 -0800130 ethCriterion = (EthCriterion) criterion;
131 } else if (criterion.type() == Criterion.Type.VLAN_VID) {
132 vidCriterion = (VlanIdCriterion) criterion;
133 } else if (criterion.type() == Criterion.Type.IPV4_DST) {
134 ips.add((IPCriterion) criterion);
135 } else {
136 log.error("Unsupported filter {}", criterion);
137 fail(filt, ObjectiveError.UNSUPPORTED);
138 return;
139 }
140 }
141
142 VlanId assignedVlan = null;
143 // For VLAN cross-connect packets, use the configured VLAN
144 if (vidCriterion != null) {
145 if (vidCriterion.vlanId() != VlanId.NONE) {
146 assignedVlan = vidCriterion.vlanId();
147
148 // For untagged packets, assign a VLAN ID
149 } else {
150 if (filt.meta() == null) {
151 log.error("Missing metadata in filtering objective required " +
152 "for vlan assignment in dev {}", deviceId);
153 fail(filt, ObjectiveError.BADPARAMS);
154 return;
155 }
156 for (Instruction i : filt.meta().allInstructions()) {
157 if (i instanceof ModVlanIdInstruction) {
158 assignedVlan = ((ModVlanIdInstruction) i).vlanId();
159 }
160 }
161 if (assignedVlan == null) {
162 log.error("Driver requires an assigned vlan-id to tag incoming "
163 + "untagged packets. Not processing vlan filters on "
164 + "device {}", deviceId);
165 fail(filt, ObjectiveError.BADPARAMS);
166 return;
167 }
168 }
169 }
170
171 if (ethCriterion == null || ethCriterion.mac().equals(MacAddress.NONE)) {
172 log.debug("filtering objective missing dstMac, cannot program TMAC table");
173 } else {
174 for (FlowRule tmacRule : processEthDstFilter(portCriterion, ethCriterion,
175 vidCriterion, assignedVlan,
176 applicationId)) {
177 log.debug("adding MAC filtering rules in TMAC table: {} for dev: {}",
178 tmacRule, deviceId);
179 ops = install ? ops.add(tmacRule) : ops.remove(tmacRule);
180 }
181 }
182
183 if (ethCriterion == null || vidCriterion == null) {
184 log.debug("filtering objective missing dstMac or VLAN, "
185 + "cannot program VLAN Table");
186 } else {
187 List<FlowRule> allRules = processVlanIdFilter(
188 portCriterion, vidCriterion, assignedVlan, applicationId);
189 for (FlowRule rule : allRules) {
190 log.debug("adding VLAN filtering rule in VLAN table: {} for dev: {}",
191 rule, deviceId);
192 ops = install ? ops.add(rule) : ops.remove(rule);
193 }
194 }
195
196 for (IPCriterion ipaddr : ips) {
197 // since we ignore port information for IP rules, and the same (gateway) IP
198 // can be configured on multiple ports, we make sure that we send
199 // only a single rule to the switch.
200 if (!sentIpFilters.contains(ipaddr)) {
201 sentIpFilters.add(ipaddr);
202 log.debug("adding IP filtering rules in ACL table {} for dev: {}",
203 ipaddr, deviceId);
204 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
205 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
206 selector.matchEthType(Ethernet.TYPE_IPV4);
207 selector.matchIPDst(ipaddr.ip());
208 treatment.setOutput(PortNumber.CONTROLLER);
209 FlowRule rule = DefaultFlowRule.builder()
210 .forDevice(deviceId)
211 .withSelector(selector.build())
212 .withTreatment(treatment.build())
213 .withPriority(HIGHEST_PRIORITY)
214 .fromApp(applicationId)
215 .makePermanent()
216 .forTable(ACL_TABLE).build();
217 ops = install ? ops.add(rule) : ops.remove(rule);
218 }
219 }
220
221 // apply filtering flow rules
222 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
223 @Override
224 public void onSuccess(FlowRuleOperations ops) {
225 log.info("Applied {} filtering rules in device {}",
226 ops.stages().get(0).size(), deviceId);
227 pass(filt);
228 }
229
230 @Override
231 public void onError(FlowRuleOperations ops) {
232 log.info("Failed to apply all filtering rules in dev {}", deviceId);
233 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
234 }
235 }));
236
237 }
238
239 /*
Saurav Das4ce45962015-11-24 23:21:05 -0800240 * Cpqd emulation does not require the non-OF standard rules for
241 * matching untagged packets.
242 *
243 * (non-Javadoc)
244 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processVlanIdFilter
245 */
Saurav Das558afec2015-05-31 17:12:48 -0700246 @Override
Saurav Das2857f382015-11-03 14:39:27 -0800247 protected List<FlowRule> processVlanIdFilter(PortCriterion portCriterion,
248 VlanIdCriterion vidCriterion,
249 VlanId assignedVlan,
250 ApplicationId applicationId) {
251 List<FlowRule> rules = new ArrayList<FlowRule>();
252 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
253 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
254 selector.matchVlanId(vidCriterion.vlanId());
Saurav Das4f980082015-11-05 13:39:15 -0800255 treatment.transition(TMAC_TABLE);
256
257 VlanId storeVlan = null;
Saurav Das2857f382015-11-03 14:39:27 -0800258 if (vidCriterion.vlanId() == VlanId.NONE) {
259 // untagged packets are assigned vlans
260 treatment.pushVlan().setVlanId(assignedVlan);
Saurav Das4f980082015-11-05 13:39:15 -0800261 storeVlan = assignedVlan;
262 } else {
263 storeVlan = vidCriterion.vlanId();
Saurav Das2857f382015-11-03 14:39:27 -0800264 }
Saurav Das2857f382015-11-03 14:39:27 -0800265
266 // ofdpa cannot match on ALL portnumber, so we need to use separate
267 // rules for each port.
268 List<PortNumber> portnums = new ArrayList<PortNumber>();
269 if (portCriterion.port() == PortNumber.ALL) {
270 for (Port port : deviceService.getPorts(deviceId)) {
271 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
272 portnums.add(port.number());
273 }
274 }
275 } else {
276 portnums.add(portCriterion.port());
277 }
Saurav Das4f980082015-11-05 13:39:15 -0800278
Saurav Das2857f382015-11-03 14:39:27 -0800279 for (PortNumber pnum : portnums) {
Saurav Das4f980082015-11-05 13:39:15 -0800280 // create rest of flowrule
Saurav Das2857f382015-11-03 14:39:27 -0800281 selector.matchInPort(pnum);
282 FlowRule rule = DefaultFlowRule.builder()
283 .forDevice(deviceId)
284 .withSelector(selector.build())
285 .withTreatment(treatment.build())
286 .withPriority(DEFAULT_PRIORITY)
287 .fromApp(applicationId)
288 .makePermanent()
289 .forTable(VLAN_TABLE).build();
290 rules.add(rule);
291 }
292 return rules;
293 }
294
Saurav Das4ce45962015-11-24 23:21:05 -0800295 /*
296 * Cpqd emulation does not handle vlan tags and mpls labels correctly.
297 * Workaround requires popping off the VLAN tags in the TMAC table.
298 *
299 * (non-Javadoc)
300 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthDstFilter
301 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800302 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800303 protected List<FlowRule> processEthDstFilter(PortCriterion portCriterion,
304 EthCriterion ethCriterion,
305 VlanIdCriterion vidCriterion,
306 VlanId assignedVlan,
307 ApplicationId applicationId) {
Charles Chan5270ed02016-01-30 23:22:37 -0800308 // Consider PortNumber.ANY as wildcard. Match ETH_DST only
309 if (portCriterion != null && portCriterion.port() == PortNumber.ANY) {
310 return processEthDstOnlyFilter(ethCriterion, applicationId);
311 }
312
Charles Chan5b9df8d2016-03-28 22:21:40 -0700313 // Multicast MAC
314 if (ethCriterion.mask() != null) {
315 return processMcastEthDstFilter(ethCriterion, applicationId);
316 }
317
Saurav Das4ce45962015-11-24 23:21:05 -0800318 //handling untagged packets via assigned VLAN
319 if (vidCriterion.vlanId() == VlanId.NONE) {
320 vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan);
321 }
322 // ofdpa cannot match on ALL portnumber, so we need to use separate
323 // rules for each port.
324 List<PortNumber> portnums = new ArrayList<PortNumber>();
325 if (portCriterion.port() == PortNumber.ALL) {
326 for (Port port : deviceService.getPorts(deviceId)) {
327 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
328 portnums.add(port.number());
329 }
330 }
331 } else {
332 portnums.add(portCriterion.port());
333 }
334
335 List<FlowRule> rules = new ArrayList<FlowRule>();
336 for (PortNumber pnum : portnums) {
337 // for unicast IP packets
338 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
339 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
340 selector.matchInPort(pnum);
341 selector.matchVlanId(vidCriterion.vlanId());
342 selector.matchEthType(Ethernet.TYPE_IPV4);
343 selector.matchEthDst(ethCriterion.mac());
344 /*
345 * Note: CpqD switches do not handle MPLS-related operation properly
346 * for a packet with VLAN tag. We pop VLAN here as a workaround.
347 * Side effect: HostService learns redundant hosts with same MAC but
348 * different VLAN. No known side effect on the network reachability.
349 */
350 treatment.popVlan();
351 treatment.transition(UNICAST_ROUTING_TABLE);
352 FlowRule rule = DefaultFlowRule.builder()
353 .forDevice(deviceId)
354 .withSelector(selector.build())
355 .withTreatment(treatment.build())
356 .withPriority(DEFAULT_PRIORITY)
357 .fromApp(applicationId)
358 .makePermanent()
359 .forTable(TMAC_TABLE).build();
360 rules.add(rule);
361 //for MPLS packets
362 selector = DefaultTrafficSelector.builder();
363 treatment = DefaultTrafficTreatment.builder();
364 selector.matchInPort(pnum);
365 selector.matchVlanId(vidCriterion.vlanId());
366 selector.matchEthType(Ethernet.MPLS_UNICAST);
367 selector.matchEthDst(ethCriterion.mac());
368 // workaround here again
369 treatment.popVlan();
370 treatment.transition(MPLS_TABLE_0);
371 rule = DefaultFlowRule.builder()
372 .forDevice(deviceId)
373 .withSelector(selector.build())
374 .withTreatment(treatment.build())
375 .withPriority(DEFAULT_PRIORITY)
376 .fromApp(applicationId)
377 .makePermanent()
378 .forTable(TMAC_TABLE).build();
379 rules.add(rule);
380 }
381 return rules;
382 }
383
Charles Chan5270ed02016-01-30 23:22:37 -0800384 @Override
385 protected List<FlowRule> processEthDstOnlyFilter(EthCriterion ethCriterion,
386 ApplicationId applicationId) {
387 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
388 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
389 selector.matchEthType(Ethernet.TYPE_IPV4);
390 selector.matchEthDst(ethCriterion.mac());
391 /*
392 * Note: CpqD switches do not handle MPLS-related operation properly
393 * for a packet with VLAN tag. We pop VLAN here as a workaround.
394 * Side effect: HostService learns redundant hosts with same MAC but
395 * different VLAN. No known side effect on the network reachability.
396 */
397 treatment.popVlan();
398 treatment.transition(UNICAST_ROUTING_TABLE);
399 FlowRule rule = DefaultFlowRule.builder()
400 .forDevice(deviceId)
401 .withSelector(selector.build())
402 .withTreatment(treatment.build())
403 .withPriority(DEFAULT_PRIORITY)
404 .fromApp(applicationId)
405 .makePermanent()
406 .forTable(TMAC_TABLE).build();
407 return ImmutableList.<FlowRule>builder().add(rule).build();
408 }
409
Saurav Das4ce45962015-11-24 23:21:05 -0800410 /*
411 * Cpqd emulation allows MPLS ecmp.
412 *
413 * (non-Javadoc)
414 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#processEthTypeSpecific
415 */
416 @Override
417 protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800418 TrafficSelector selector = fwd.selector();
419 EthTypeCriterion ethType =
420 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
421 if ((ethType == null) ||
422 (ethType.ethType().toShort() != Ethernet.TYPE_IPV4) &&
423 (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) {
Saurav Das4ce45962015-11-24 23:21:05 -0800424 log.warn("processSpecific: Unsupported forwarding objective criteria"
425 + "ethType:{} in dev:{}", ethType, deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800426 fail(fwd, ObjectiveError.UNSUPPORTED);
427 return Collections.emptySet();
428 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800429 boolean defaultRule = false;
Saurav Das8a0732e2015-11-20 15:27:53 -0800430 int forTableId = -1;
431 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
Flavio Castroe10fa242016-01-15 12:43:51 -0800432 TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
433
434 /*
435 * NOTE: The switch does not support matching 0.0.0.0/0.
436 * Split it into 0.0.0.0/1 and 128.0.0.0/1
437 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800438 if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) {
Flavio Castroe10fa242016-01-15 12:43:51 -0800439 IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
Charles Chan5b9df8d2016-03-28 22:21:40 -0700440 if (ipv4Dst.isMulticast()) {
441 if (ipv4Dst.prefixLength() != 32) {
442 log.warn("Multicast specific forwarding objective can only be /32");
443 fail(fwd, ObjectiveError.BADPARAMS);
444 return ImmutableSet.of();
445 }
446 VlanId assignedVlan = readVlanFromSelector(fwd.meta());
447 if (assignedVlan == null) {
448 log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort.");
449 fail(fwd, ObjectiveError.BADPARAMS);
450 return ImmutableSet.of();
451 }
452 filteredSelector.matchVlanId(assignedVlan);
453 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
454 forTableId = MULTICAST_ROUTING_TABLE;
455 log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}"
456 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800457 } else {
Charles Chan5b9df8d2016-03-28 22:21:40 -0700458 if (ipv4Dst.prefixLength() > 0) {
459 filteredSelector.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst);
460 } else {
461 filteredSelector.matchEthType(Ethernet.TYPE_IPV4)
462 .matchIPDst(IpPrefix.valueOf("0.0.0.0/1"));
463 complementarySelector.matchEthType(Ethernet.TYPE_IPV4)
464 .matchIPDst(IpPrefix.valueOf("128.0.0.0/1"));
465 defaultRule = true;
466 }
467 forTableId = UNICAST_ROUTING_TABLE;
468 log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}"
469 + " in dev:{}", fwd.id(), fwd.nextId(), deviceId);
Flavio Castroe10fa242016-01-15 12:43:51 -0800470 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800471 } else {
472 filteredSelector
473 .matchEthType(Ethernet.MPLS_UNICAST)
474 .matchMplsLabel(((MplsCriterion)
475 selector.getCriterion(Criterion.Type.MPLS_LABEL)).label());
476 MplsBosCriterion bos = (MplsBosCriterion) selector
477 .getCriterion(Criterion.Type.MPLS_BOS);
478 if (bos != null) {
479 filteredSelector.matchMplsBos(bos.mplsBos());
480 }
481 forTableId = MPLS_TABLE_1;
Saurav Das4ce45962015-11-24 23:21:05 -0800482 log.debug("processing MPLS specific forwarding objective {} -> next:{}"
483 + " in dev {}", fwd.id(), fwd.nextId(), deviceId);
Saurav Das8a0732e2015-11-20 15:27:53 -0800484 }
485
486 TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder();
487 if (fwd.treatment() != null) {
488 for (Instruction i : fwd.treatment().allInstructions()) {
Charles Chan7d10b162015-12-07 18:54:45 -0800489 /*
490 * NOTE: OF-DPA does not support immediate instruction in
491 * L3 unicast and MPLS table.
492 */
493 tb.deferred().add(i);
Saurav Das8a0732e2015-11-20 15:27:53 -0800494 }
495 }
496
497 if (fwd.nextId() != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800498 NextGroup next = getGroupForNextObjective(fwd.nextId());
499 if (next != null) {
500 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
501 // we only need the top level group's key to point the flow to it
502 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
503 if (group == null) {
504 log.warn("The group left!");
505 fail(fwd, ObjectiveError.GROUPMISSING);
506 return Collections.emptySet();
507 }
508 tb.deferred().group(group.id());
Saurav Das8a0732e2015-11-20 15:27:53 -0800509 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800510 }
511 tb.transition(ACL_TABLE);
512 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
513 .fromApp(fwd.appId())
514 .withPriority(fwd.priority())
515 .forDevice(deviceId)
516 .withSelector(filteredSelector.build())
517 .withTreatment(tb.build())
518 .forTable(forTableId);
519
520 if (fwd.permanent()) {
521 ruleBuilder.makePermanent();
522 } else {
523 ruleBuilder.makeTemporary(fwd.timeout());
524 }
Flavio Castroe10fa242016-01-15 12:43:51 -0800525 Collection<FlowRule> flowRuleCollection = new ArrayList<>();
526 flowRuleCollection.add(ruleBuilder.build());
527 if (defaultRule) {
528 FlowRule.Builder rule = DefaultFlowRule.builder()
529 .fromApp(fwd.appId())
530 .withPriority(fwd.priority())
531 .forDevice(deviceId)
532 .withSelector(complementarySelector.build())
533 .withTreatment(tb.build())
534 .forTable(forTableId);
535 if (fwd.permanent()) {
536 rule.makePermanent();
537 } else {
538 rule.makeTemporary(fwd.timeout());
539 }
540 flowRuleCollection.add(rule.build());
541 log.debug("Default rule 0.0.0.0/0 is being installed two rules");
542 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800543
Flavio Castroe10fa242016-01-15 12:43:51 -0800544 return flowRuleCollection;
Saurav Das8a0732e2015-11-20 15:27:53 -0800545 }
546
Charles Chan1e492d32016-01-30 23:22:37 -0800547 @Override
548 protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) {
549 List<FlowRule> rules = new ArrayList<>();
550
551 // Build filtered selector
552 TrafficSelector selector = fwd.selector();
553 EthCriterion ethCriterion = (EthCriterion) selector
554 .getCriterion(Criterion.Type.ETH_DST);
555 VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector
556 .getCriterion(Criterion.Type.VLAN_VID);
557
558 if (vlanIdCriterion == null) {
559 log.warn("Forwarding objective for bridging requires vlan. Not "
560 + "installing fwd:{} in dev:{}", fwd.id(), deviceId);
561 fail(fwd, ObjectiveError.BADPARAMS);
562 return Collections.emptySet();
563 }
564
565 TrafficSelector.Builder filteredSelectorBuilder =
566 DefaultTrafficSelector.builder();
567 // Do not match MacAddress for subnet broadcast entry
568 if (!ethCriterion.mac().equals(MacAddress.NONE)) {
569 filteredSelectorBuilder.matchEthDst(ethCriterion.mac());
570 log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}",
571 fwd.id(), fwd.nextId(), deviceId);
572 } else {
573 log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} "
574 + "in dev:{} for vlan:{}",
575 fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId());
576 }
577 filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId());
578 TrafficSelector filteredSelector = filteredSelectorBuilder.build();
579
580 if (fwd.treatment() != null) {
581 log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table"
582 + "for dev:{}. Expecting only nextId", fwd.id(), deviceId);
583 }
584
585 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
586 if (fwd.nextId() != null) {
587 NextGroup next = getGroupForNextObjective(fwd.nextId());
588 if (next != null) {
589 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
590 // we only need the top level group's key to point the flow to it
591 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
592 if (group != null) {
593 treatmentBuilder.deferred().group(group.id());
594 } else {
595 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
596 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
597 fail(fwd, ObjectiveError.GROUPMISSING);
598 return Collections.emptySet();
599 }
600 }
601 }
602 treatmentBuilder.immediate().transition(ACL_TABLE);
603 TrafficTreatment filteredTreatment = treatmentBuilder.build();
604
605 // Build bridging table entries
606 FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();
607 flowRuleBuilder.fromApp(fwd.appId())
608 .withPriority(fwd.priority())
609 .forDevice(deviceId)
610 .withSelector(filteredSelector)
611 .withTreatment(filteredTreatment)
612 .forTable(BRIDGING_TABLE);
613 if (fwd.permanent()) {
614 flowRuleBuilder.makePermanent();
615 } else {
616 flowRuleBuilder.makeTemporary(fwd.timeout());
617 }
618 rules.add(flowRuleBuilder.build());
619 return rules;
620 }
621
Saurav Das52025962016-01-28 22:30:01 -0800622 /*
623 * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the
624 * ACL table. Because we pop off vlan tags in TMAC table,
625 * we need to avoid matching on vlans in the ACL table.
626 */
627 @Override
628 protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
629 log.info("Processing versatile forwarding objective");
630
631 EthTypeCriterion ethType =
632 (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE);
633 if (ethType == null) {
634 log.error("Versatile forwarding objective must include ethType");
635 fail(fwd, ObjectiveError.BADPARAMS);
636 return Collections.emptySet();
637 }
638 if (fwd.nextId() == null && fwd.treatment() == null) {
639 log.error("Forwarding objective {} from {} must contain "
640 + "nextId or Treatment", fwd.selector(), fwd.appId());
641 return Collections.emptySet();
642 }
643
644 TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder();
645 fwd.selector().criteria().forEach(criterion -> {
646 if (criterion instanceof VlanIdCriterion) {
647 // avoid matching on vlans
648 return;
649 } else {
650 sbuilder.add(criterion);
651 }
652 });
653
654 // XXX driver does not currently do type checking as per Tables 65-67 in
655 // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller.
656 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
657 if (fwd.treatment() != null) {
658 for (Instruction ins : fwd.treatment().allInstructions()) {
659 if (ins instanceof OutputInstruction) {
660 OutputInstruction o = (OutputInstruction) ins;
661 if (o.port() == PortNumber.CONTROLLER) {
Saurav Das49831642016-02-05 13:15:20 -0800662 // emulating real ofdpa behavior by popping off internal
663 // vlan before sending to controller
664 ttBuilder.popVlan();
Saurav Das52025962016-01-28 22:30:01 -0800665 ttBuilder.add(o);
666 } else {
667 log.warn("Only allowed treatments in versatile forwarding "
668 + "objectives are punts to the controller");
669 }
670 } else {
671 log.warn("Cannot process instruction in versatile fwd {}", ins);
672 }
673 }
Charles Chan5270ed02016-01-30 23:22:37 -0800674 ttBuilder.wipeDeferred();
Saurav Das52025962016-01-28 22:30:01 -0800675 }
676 if (fwd.nextId() != null) {
677 // overide case
678 NextGroup next = getGroupForNextObjective(fwd.nextId());
679 List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());
680 // we only need the top level group's key to point the flow to it
681 Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst());
682 if (group == null) {
683 log.warn("Group with key:{} for next-id:{} not found in dev:{}",
684 gkeys.get(0).peekFirst(), fwd.nextId(), deviceId);
685 fail(fwd, ObjectiveError.GROUPMISSING);
686 return Collections.emptySet();
687 }
688 ttBuilder.deferred().group(group.id());
689 }
690
691 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
692 .fromApp(fwd.appId())
693 .withPriority(fwd.priority())
694 .forDevice(deviceId)
695 .withSelector(sbuilder.build())
696 .withTreatment(ttBuilder.build())
697 .makePermanent()
698 .forTable(ACL_TABLE);
699 return Collections.singletonList(ruleBuilder.build());
700 }
701
702 /*
703 * Cpqd emulation requires table-miss-entries in forwarding tables.
704 * Real OFDPA does not require these rules as they are put in by default.
705 *
706 * (non-Javadoc)
707 * @see org.onosproject.driver.pipeline.OFDPA2Pipeline#initializePipeline()
708 */
Saurav Das2857f382015-11-03 14:39:27 -0800709 @Override
Saurav Das558afec2015-05-31 17:12:48 -0700710 protected void initializePipeline() {
711 processPortTable();
Saurav Das2857f382015-11-03 14:39:27 -0800712 // vlan table processing not required, as default is to drop packets
713 // which can be accomplished without a table-miss-entry.
Saurav Das558afec2015-05-31 17:12:48 -0700714 processTmacTable();
715 processIpTable();
Jonathan Hart855179c2016-04-26 07:40:04 -0700716 processMulticastIpTable();
Saurav Das2857f382015-11-03 14:39:27 -0800717 processMplsTable();
Saurav Das558afec2015-05-31 17:12:48 -0700718 processBridgingTable();
Saurav Das337c7a42015-06-02 15:12:06 -0700719 processAclTable();
Saurav Das558afec2015-05-31 17:12:48 -0700720 }
721
Saurav Das558afec2015-05-31 17:12:48 -0700722 protected void processPortTable() {
723 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
724 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
725 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
726 treatment.transition(VLAN_TABLE);
727 FlowRule tmisse = DefaultFlowRule.builder()
728 .forDevice(deviceId)
729 .withSelector(selector.build())
730 .withTreatment(treatment.build())
731 .withPriority(LOWEST_PRIORITY)
732 .fromApp(driverId)
733 .makePermanent()
734 .forTable(PORT_TABLE).build();
735 ops = ops.add(tmisse);
736
737 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
738 @Override
739 public void onSuccess(FlowRuleOperations ops) {
740 log.info("Initialized port table");
741 }
742
743 @Override
744 public void onError(FlowRuleOperations ops) {
745 log.info("Failed to initialize port table");
746 }
747 }));
748 }
749
Saurav Das558afec2015-05-31 17:12:48 -0700750 protected void processTmacTable() {
751 //table miss entry
752 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
753 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
754 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
755 selector = DefaultTrafficSelector.builder();
756 treatment = DefaultTrafficTreatment.builder();
757 treatment.transition(BRIDGING_TABLE);
758 FlowRule rule = DefaultFlowRule.builder()
759 .forDevice(deviceId)
760 .withSelector(selector.build())
761 .withTreatment(treatment.build())
762 .withPriority(LOWEST_PRIORITY)
763 .fromApp(driverId)
764 .makePermanent()
765 .forTable(TMAC_TABLE).build();
766 ops = ops.add(rule);
767 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
768 @Override
769 public void onSuccess(FlowRuleOperations ops) {
770 log.info("Initialized tmac table");
771 }
772
773 @Override
774 public void onError(FlowRuleOperations ops) {
775 log.info("Failed to initialize tmac table");
776 }
777 }));
778 }
779
Saurav Das558afec2015-05-31 17:12:48 -0700780 protected void processIpTable() {
781 //table miss entry
782 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
783 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
784 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
Saurav Das558afec2015-05-31 17:12:48 -0700785 treatment.transition(ACL_TABLE);
786 FlowRule rule = DefaultFlowRule.builder()
787 .forDevice(deviceId)
788 .withSelector(selector.build())
789 .withTreatment(treatment.build())
790 .withPriority(LOWEST_PRIORITY)
791 .fromApp(driverId)
792 .makePermanent()
793 .forTable(UNICAST_ROUTING_TABLE).build();
794 ops = ops.add(rule);
795 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
796 @Override
797 public void onSuccess(FlowRuleOperations ops) {
798 log.info("Initialized IP table");
799 }
800
801 @Override
802 public void onError(FlowRuleOperations ops) {
803 log.info("Failed to initialize unicast IP table");
804 }
805 }));
806 }
807
Jonathan Hart855179c2016-04-26 07:40:04 -0700808 protected void processMulticastIpTable() {
809 //table miss entry
810 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
811 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
812 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
813 treatment.transition(ACL_TABLE);
814 FlowRule rule = DefaultFlowRule.builder()
815 .forDevice(deviceId)
816 .withSelector(selector.build())
817 .withTreatment(treatment.build())
818 .withPriority(LOWEST_PRIORITY)
819 .fromApp(driverId)
820 .makePermanent()
821 .forTable(MULTICAST_ROUTING_TABLE).build();
822 ops = ops.add(rule);
823 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
824 @Override
825 public void onSuccess(FlowRuleOperations ops) {
826 log.info("Initialized multicast IP table");
827 }
828
829 @Override
830 public void onError(FlowRuleOperations ops) {
831 log.info("Failed to initialize multicast IP table");
832 }
833 }));
834 }
835
Saurav Das2857f382015-11-03 14:39:27 -0800836 protected void processMplsTable() {
837 //table miss entry
838 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
839 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
840 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
841 selector = DefaultTrafficSelector.builder();
842 treatment = DefaultTrafficTreatment.builder();
843 treatment.transition(MPLS_TABLE_1);
844 FlowRule rule = DefaultFlowRule.builder()
845 .forDevice(deviceId)
846 .withSelector(selector.build())
847 .withTreatment(treatment.build())
848 .withPriority(LOWEST_PRIORITY)
849 .fromApp(driverId)
850 .makePermanent()
851 .forTable(MPLS_TABLE_0).build();
852 ops = ops.add(rule);
853
854 treatment.transition(ACL_TABLE);
855 rule = DefaultFlowRule.builder()
856 .forDevice(deviceId)
857 .withSelector(selector.build())
858 .withTreatment(treatment.build())
859 .withPriority(LOWEST_PRIORITY)
860 .fromApp(driverId)
861 .makePermanent()
862 .forTable(MPLS_TABLE_1).build();
863 ops = ops.add(rule);
864
865 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
866 @Override
867 public void onSuccess(FlowRuleOperations ops) {
868 log.info("Initialized MPLS tables");
869 }
870
871 @Override
872 public void onError(FlowRuleOperations ops) {
873 log.info("Failed to initialize MPLS tables");
874 }
875 }));
876 }
877
Saurav Das558afec2015-05-31 17:12:48 -0700878 private void processBridgingTable() {
879 //table miss entry
880 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
881 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
882 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
883 selector = DefaultTrafficSelector.builder();
884 treatment = DefaultTrafficTreatment.builder();
885 treatment.transition(ACL_TABLE);
886 FlowRule rule = DefaultFlowRule.builder()
887 .forDevice(deviceId)
888 .withSelector(selector.build())
889 .withTreatment(treatment.build())
890 .withPriority(LOWEST_PRIORITY)
891 .fromApp(driverId)
892 .makePermanent()
893 .forTable(BRIDGING_TABLE).build();
894 ops = ops.add(rule);
895 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
896 @Override
897 public void onSuccess(FlowRuleOperations ops) {
898 log.info("Initialized Bridging table");
899 }
900
901 @Override
902 public void onError(FlowRuleOperations ops) {
903 log.info("Failed to initialize Bridging table");
904 }
905 }));
Saurav Das337c7a42015-06-02 15:12:06 -0700906 }
Saurav Das558afec2015-05-31 17:12:48 -0700907
Saurav Dasa07f2032015-10-19 14:37:36 -0700908 protected void processAclTable() {
Saurav Das337c7a42015-06-02 15:12:06 -0700909 //table miss entry - catch all to executed action-set
910 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
911 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
912 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
913 selector = DefaultTrafficSelector.builder();
914 treatment = DefaultTrafficTreatment.builder();
915 FlowRule rule = DefaultFlowRule.builder()
916 .forDevice(deviceId)
917 .withSelector(selector.build())
918 .withTreatment(treatment.build())
919 .withPriority(LOWEST_PRIORITY)
920 .fromApp(driverId)
921 .makePermanent()
922 .forTable(ACL_TABLE).build();
923 ops = ops.add(rule);
924 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
925 @Override
926 public void onSuccess(FlowRuleOperations ops) {
927 log.info("Initialized Acl table");
928 }
929
930 @Override
931 public void onError(FlowRuleOperations ops) {
932 log.info("Failed to initialize Acl table");
933 }
934 }));
Saurav Das558afec2015-05-31 17:12:48 -0700935 }
936
937}