blob: 23ef5f2bdf391f4791d6eff01f8a4808625c5040 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 Dasdecd7a62015-05-16 22:39:47 -070016package org.onosproject.driver.pipeline;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.ArrayList;
21import java.util.Collection;
22import java.util.Collections;
23import java.util.concurrent.ConcurrentHashMap;
24
25import org.onlab.osgi.ServiceDirectory;
26import org.onlab.packet.Ethernet;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onlab.util.KryoNamespace;
30import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.PortNumber;
34import org.onosproject.net.behaviour.NextGroup;
35import org.onosproject.net.behaviour.Pipeliner;
36import org.onosproject.net.behaviour.PipelinerContext;
37import org.onosproject.net.driver.AbstractHandlerBehaviour;
38import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.FlowRuleOperations;
43import org.onosproject.net.flow.FlowRuleOperationsContext;
44import org.onosproject.net.flow.FlowRuleService;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.flow.criteria.Criteria;
48import org.onosproject.net.flow.criteria.Criterion;
49import org.onosproject.net.flow.criteria.EthCriterion;
50import org.onosproject.net.flow.criteria.EthTypeCriterion;
51import org.onosproject.net.flow.criteria.IPCriterion;
52import org.onosproject.net.flow.criteria.PortCriterion;
53import org.onosproject.net.flow.criteria.VlanIdCriterion;
54import org.onosproject.net.flowobjective.FilteringObjective;
55import org.onosproject.net.flowobjective.FlowObjectiveStore;
56import org.onosproject.net.flowobjective.ForwardingObjective;
57import org.onosproject.net.flowobjective.NextObjective;
58import org.onosproject.net.flowobjective.Objective;
59import org.onosproject.net.flowobjective.ObjectiveError;
60import org.slf4j.Logger;
61import org.onosproject.store.serializers.KryoNamespaces;
62
63/**
64 * Simple 2-Table Pipeline for Software/NPU based routers. This pipeline
65 * does not forward IP traffic to next-hop groups. Instead it forwards traffic
66 * using OF FlowMod actions.
67 */
68public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipeliner {
69
70 protected static final int FILTER_TABLE = 0;
71 protected static final int FIB_TABLE = 1;
72
73 private static final int DROP_PRIORITY = 0;
74 private static final int DEFAULT_PRIORITY = 0x8000;
75 private static final int HIGHEST_PRIORITY = 0xffff;
76
77 private ServiceDirectory serviceDirectory;
78 protected FlowRuleService flowRuleService;
79 private CoreService coreService;
80 private FlowObjectiveStore flowObjectiveStore;
81 protected DeviceId deviceId;
82 protected ApplicationId appId;
83 private ApplicationId driverId;
84 private Collection<Filter> filters;
85 private Collection<ForwardingObjective> pendingVersatiles;
86
87 private KryoNamespace appKryo = new KryoNamespace.Builder()
88 .register(DummyGroup.class)
89 .register(KryoNamespaces.API)
90 .register(byte[].class)
91 .build();
92
93 private final Logger log = getLogger(getClass());
94
95 @Override
96 public void init(DeviceId deviceId, PipelinerContext context) {
97 this.serviceDirectory = context.directory();
98 this.deviceId = deviceId;
99 coreService = serviceDirectory.get(CoreService.class);
100 flowRuleService = serviceDirectory.get(FlowRuleService.class);
101 flowObjectiveStore = context.store();
102 driverId = coreService.registerApplication(
103 "org.onosproject.driver.OVSCorsaPipeline");
104 filters = Collections.newSetFromMap(new ConcurrentHashMap<Filter, Boolean>());
105 pendingVersatiles = Collections.newSetFromMap(
106 new ConcurrentHashMap<ForwardingObjective, Boolean>());
107 initializePipeline();
108 }
109
110 @Override
111 public void filter(FilteringObjective filteringObjective) {
112 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
113 processFilter(filteringObjective,
114 filteringObjective.op() == Objective.Operation.ADD,
115 filteringObjective.appId());
116 } else {
117 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
118 }
119 }
120
121 @Override
122 public void forward(ForwardingObjective fwd) {
123 Collection<FlowRule> rules;
124 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
125
126 rules = processForward(fwd);
127 switch (fwd.op()) {
128 case ADD:
129 rules.stream()
130 .filter(rule -> rule != null)
131 .forEach(flowOpsBuilder::add);
132 break;
133 case REMOVE:
134 rules.stream()
135 .filter(rule -> rule != null)
136 .forEach(flowOpsBuilder::remove);
137 break;
138 default:
139 fail(fwd, ObjectiveError.UNKNOWN);
140 log.warn("Unknown forwarding type {}", fwd.op());
141 }
142
143
144 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
145 @Override
146 public void onSuccess(FlowRuleOperations ops) {
147 pass(fwd);
148 }
149
150 @Override
151 public void onError(FlowRuleOperations ops) {
152 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
153 }
154 }));
155
156 }
157
158 @Override
159 public void next(NextObjective nextObjective) {
160 switch (nextObjective.type()) {
161 case SIMPLE:
162 Collection<TrafficTreatment> treatments = nextObjective.next();
163 if (treatments.size() != 1) {
164 log.error("Next Objectives of type Simple should only have a "
165 + "single Traffic Treatment. Next Objective Id:{}", nextObjective.id());
166 fail(nextObjective, ObjectiveError.BADPARAMS);
167 return;
168 }
169 processSimpleNextObjective(nextObjective);
170 break;
171 case HASHED:
172 case BROADCAST:
173 case FAILOVER:
174 fail(nextObjective, ObjectiveError.UNSUPPORTED);
175 log.warn("Unsupported next objective type {}", nextObjective.type());
176 break;
177 default:
178 fail(nextObjective, ObjectiveError.UNKNOWN);
179 log.warn("Unknown next objective type {}", nextObjective.type());
180 }
181 }
182
183 private void pass(Objective obj) {
184 if (obj.context().isPresent()) {
185 obj.context().get().onSuccess(obj);
186 }
187 }
188
189 private void fail(Objective obj, ObjectiveError error) {
190 if (obj.context().isPresent()) {
191 obj.context().get().onError(obj, error);
192 }
193 }
194
195
196 private void initializePipeline() {
197 //Drop rules for both tables
198 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
199 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
200 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
201
202 treatment.drop();
203
204 FlowRule rule = DefaultFlowRule.builder()
205 .forDevice(deviceId)
206 .withSelector(selector.build())
207 .withTreatment(treatment.build())
208 .withPriority(DROP_PRIORITY)
209 .fromApp(driverId)
210 .makePermanent()
211 .forTable(FILTER_TABLE)
212 .build();
213 ops = ops.add(rule);
214
215 rule = DefaultFlowRule.builder().forDevice(deviceId)
216 .withSelector(selector.build())
217 .withTreatment(treatment.build())
218 .withPriority(DROP_PRIORITY)
219 .fromApp(driverId)
220 .makePermanent()
221 .forTable(FIB_TABLE)
222 .build();
223 ops = ops.add(rule);
224
225 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
226 @Override
227 public void onSuccess(FlowRuleOperations ops) {
228 log.info("Provisioned drop rules in both tables");
229 }
230
231 @Override
232 public void onError(FlowRuleOperations ops) {
233 log.info("Failed to provision drop rules");
234 }
235 }));
236 }
237
238 private void processFilter(FilteringObjective filt, boolean install,
239 ApplicationId applicationId) {
240 // This driver only processes filtering criteria defined with switch
241 // ports as the key
242 PortCriterion p; EthCriterion e = null; VlanIdCriterion v = null;
243 Collection<IPCriterion> ips = new ArrayList<IPCriterion>();
244 if (!filt.key().equals(Criteria.dummy()) &&
245 filt.key().type() == Criterion.Type.IN_PORT) {
246 p = (PortCriterion) filt.key();
247 } else {
248 log.warn("No key defined in filtering objective from app: {}. Not"
249 + "processing filtering objective", applicationId);
250 fail(filt, ObjectiveError.UNKNOWN);
251 return;
252 }
253
254 // convert filtering conditions for switch-intfs into flowrules
255 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
256 for (Criterion c : filt.conditions()) {
257 if (c.type() == Criterion.Type.ETH_DST) {
258 e = (EthCriterion) c;
259 } else if (c.type() == Criterion.Type.VLAN_VID) {
260 v = (VlanIdCriterion) c;
261 } else if (c.type() == Criterion.Type.IPV4_DST) {
262 ips.add((IPCriterion) c);
263 } else {
264 log.error("Unsupported filter {}", c);
265 fail(filt, ObjectiveError.UNSUPPORTED);
266 return;
267 }
268 }
269
270 log.debug("adding Port/VLAN/MAC filtering rules in filter table: {}/{}/{}",
271 p.port(), v.vlanId(), e.mac());
272 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
273 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
274 selector.matchInPort(p.port());
275 selector.matchVlanId(v.vlanId());
276 selector.matchEthDst(e.mac());
277 selector.matchEthType(Ethernet.TYPE_IPV4);
Saurav Das6c44a632015-05-30 22:05:22 -0700278 treatment.popVlan();
279 treatment.transition(FIB_TABLE); // all other IPs to the FIB table
Saurav Dasdecd7a62015-05-16 22:39:47 -0700280 FlowRule rule = DefaultFlowRule.builder()
281 .forDevice(deviceId)
282 .withSelector(selector.build())
283 .withTreatment(treatment.build())
284 .withPriority(DEFAULT_PRIORITY)
285 .fromApp(applicationId)
286 .makePermanent()
287 .forTable(FILTER_TABLE).build();
288 ops = ops.add(rule);
289
290 for (IPCriterion ipaddr : ips) {
Saurav Das6c44a632015-05-30 22:05:22 -0700291 log.debug("adding IP filtering rules in FILTER table: {}", ipaddr.ip());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700292 selector = DefaultTrafficSelector.builder();
293 treatment = DefaultTrafficTreatment.builder();
Saurav Das6c44a632015-05-30 22:05:22 -0700294 selector.matchInPort(p.port());
295 selector.matchVlanId(v.vlanId());
296 selector.matchEthDst(e.mac());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700297 selector.matchEthType(Ethernet.TYPE_IPV4);
Saurav Das6c44a632015-05-30 22:05:22 -0700298 selector.matchIPDst(ipaddr.ip()); // router IPs to the controller
Saurav Dasdecd7a62015-05-16 22:39:47 -0700299 treatment.setOutput(PortNumber.CONTROLLER);
300 rule = DefaultFlowRule.builder()
301 .forDevice(deviceId)
302 .withSelector(selector.build())
303 .withTreatment(treatment.build())
304 .withPriority(HIGHEST_PRIORITY)
305 .fromApp(applicationId)
306 .makePermanent()
Saurav Das6c44a632015-05-30 22:05:22 -0700307 .forTable(FILTER_TABLE).build();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700308 ops = ops.add(rule);
309 }
310
311 // cache for later use
312 Filter filter = new Filter(p, e, v, ips);
313 filters.add(filter);
314 // apply any pending versatile forwarding objectives
315 for (ForwardingObjective fwd : pendingVersatiles) {
316 Collection<FlowRule> ret = processVersatilesWithFilters(filter, fwd);
317 for (FlowRule fr : ret) {
318 ops.add(fr);
319 }
320 }
321
322 ops = install ? ops.add(rule) : ops.remove(rule);
323 // apply filtering flow rules
324 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
325 @Override
326 public void onSuccess(FlowRuleOperations ops) {
327 log.info("Applied filtering rules");
328 pass(filt);
329 }
330
331 @Override
332 public void onError(FlowRuleOperations ops) {
333 log.info("Failed to apply filtering rules");
334 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
335 }
336 }));
337 }
338
339 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
340 switch (fwd.flag()) {
341 case SPECIFIC:
342 return processSpecific(fwd);
343 case VERSATILE:
344 return processVersatile(fwd);
345 default:
346 fail(fwd, ObjectiveError.UNKNOWN);
347 log.warn("Unknown forwarding flag {}", fwd.flag());
348 }
349 return Collections.emptySet();
350 }
351
352 /**
353 * SoftRouter has a single versatile table - the filter table. All versatile
354 * flow rules must include the filtering rules.
355 *
356 * @param fwd The forwarding objective of type versatile
357 * @return A collection of flow rules meant to be delivered to the flowrule
358 * subsystem. May return empty collection in case of failures.
359 */
360 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
361 if (filters.isEmpty()) {
362 pendingVersatiles.add(fwd);
363 return Collections.emptySet();
364 }
365 Collection<FlowRule> flowrules = new ArrayList<FlowRule>();
366 for (Filter filter : filters) {
367 flowrules.addAll(processVersatilesWithFilters(filter, fwd));
368 }
369 return flowrules;
370 }
371
372 private Collection<FlowRule> processVersatilesWithFilters(
373 Filter filt, ForwardingObjective fwd) {
374 log.info("Processing versatile forwarding objective");
375 Collection<FlowRule> flows = new ArrayList<FlowRule>();
376 TrafficSelector match = fwd.selector();
377 EthTypeCriterion ethType =
378 (EthTypeCriterion) match.getCriterion(Criterion.Type.ETH_TYPE);
379 if (ethType == null) {
380 log.error("Versatile forwarding objective must include ethType");
381 fail(fwd, ObjectiveError.UNKNOWN);
382 return Collections.emptySet();
383 }
384
alshabibcaf1ca22015-06-25 15:18:16 -0700385 if (ethType.ethType().toShort() == Ethernet.TYPE_ARP) {
Saurav Dasdecd7a62015-05-16 22:39:47 -0700386 // need to install ARP request & reply flow rules for each interface filter
387
388 // rule for ARP replies
389 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
390 selector.matchInPort(filt.port());
391 selector.matchVlanId(filt.vlanId());
392 selector.matchEthDst(filt.mac());
393 selector.matchEthType(Ethernet.TYPE_ARP);
394 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
395 .fromApp(fwd.appId())
396 .withPriority(fwd.priority())
397 .forDevice(deviceId)
398 .withSelector(selector.build())
399 .withTreatment(fwd.treatment())
400 .makePermanent()
401 .forTable(FILTER_TABLE);
402 flows.add(ruleBuilder.build());
403
404 //rule for ARP requests
405 selector = DefaultTrafficSelector.builder();
406 selector.matchInPort(filt.port());
407 selector.matchVlanId(filt.vlanId());
408 selector.matchEthDst(MacAddress.BROADCAST);
409 selector.matchEthType(Ethernet.TYPE_ARP);
410 ruleBuilder = DefaultFlowRule.builder()
411 .fromApp(fwd.appId())
412 .withPriority(fwd.priority())
413 .forDevice(deviceId)
414 .withSelector(selector.build())
415 .withTreatment(fwd.treatment())
416 .makePermanent()
417 .forTable(FILTER_TABLE);
418 flows.add(ruleBuilder.build());
419
420 return flows;
421 }
422 // not handling other versatile flows
423 return Collections.emptySet();
424 }
425
426 /**
427 * SoftRouter has a single specific table - the FIB Table. It emulates
428 * LPM matching of dstIP by using higher priority flows for longer prefixes.
429 * Flows are forwarded using flow-actions
430 *
431 * @param fwd The forwarding objective of type simple
432 * @return A collection of flow rules meant to be delivered to the flowrule
433 * subsystem. Typically the returned collection has a single flowrule.
434 * May return empty collection in case of failures.
435 *
436 */
437 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
438 log.debug("Processing specific forwarding objective");
439 TrafficSelector selector = fwd.selector();
440 EthTypeCriterion ethType =
441 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
442 // XXX currently supporting only the L3 unicast table
alshabibcaf1ca22015-06-25 15:18:16 -0700443 if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) {
Saurav Dasdecd7a62015-05-16 22:39:47 -0700444 fail(fwd, ObjectiveError.UNSUPPORTED);
445 return Collections.emptySet();
446 }
447
448 TrafficSelector filteredSelector =
449 DefaultTrafficSelector.builder()
450 .matchEthType(Ethernet.TYPE_IPV4)
451 .matchIPDst(((IPCriterion)
452 selector.getCriterion(Criterion.Type.IPV4_DST)).ip())
453 .build();
454
455 TrafficTreatment tt = null;
456 if (fwd.nextId() != null) {
457 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
458 if (next == null) {
459 log.error("next-id {} does not exist in store", fwd.nextId());
460 return Collections.emptySet();
461 }
462 tt = appKryo.deserialize(next.data());
463 if (tt == null) {
464 log.error("Error in deserializing next-id {}", fwd.nextId());
465 return Collections.emptySet();
466 }
467 }
468
469 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
470 .fromApp(fwd.appId())
471 .withPriority(fwd.priority())
472 .forDevice(deviceId)
473 .withSelector(filteredSelector)
474 .withTreatment(tt);
475
476 if (fwd.permanent()) {
477 ruleBuilder.makePermanent();
478 } else {
479 ruleBuilder.makeTemporary(fwd.timeout());
480 }
481
482 ruleBuilder.forTable(FIB_TABLE);
483 return Collections.singletonList(ruleBuilder.build());
484 }
485
486 /**
487 * Next Objectives are stored as dummy groups for retrieval later
488 * when Forwarding Objectives reference the next objective id. At that point
489 * the dummy group is fetched from the distributed store and the enclosed
490 * treatment is applied as a flow rule action.
491 *
alshabibcaf1ca22015-06-25 15:18:16 -0700492 * @param nextObj the next objective of type simple
Saurav Dasdecd7a62015-05-16 22:39:47 -0700493 */
494 private void processSimpleNextObjective(NextObjective nextObj) {
495 // Simple next objective has a single treatment (not a collection)
Saurav Das6c44a632015-05-30 22:05:22 -0700496 TrafficTreatment treatment = nextObj.next().iterator().next();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700497 flowObjectiveStore.putNextGroup(nextObj.id(),
Saurav Das6c44a632015-05-30 22:05:22 -0700498 new DummyGroup(treatment));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700499 }
500
501 private class Filter {
502 private PortCriterion port;
503 private VlanIdCriterion vlan;
504 private EthCriterion eth;
505
506 @SuppressWarnings("unused")
507 private Collection<IPCriterion> ips;
508
509 public Filter(PortCriterion p, EthCriterion e, VlanIdCriterion v,
510 Collection<IPCriterion> ips) {
511 this.eth = e;
512 this.port = p;
513 this.vlan = v;
514 this.ips = ips;
515 }
516
517 public PortNumber port() {
518 return port.port();
519 }
520
521 public VlanId vlanId() {
522 return vlan.vlanId();
523 }
524
525 public MacAddress mac() {
526 return eth.mac();
527 }
528 }
529
530 private class DummyGroup implements NextGroup {
531 TrafficTreatment nextActions;
532
533 public DummyGroup(TrafficTreatment next) {
534 this.nextActions = next;
535 }
536
537 @Override
538 public byte[] data() {
539 return appKryo.serialize(nextActions);
540 }
541
542 }
543
544}