blob: 4618bb0dea2247774f5ebf7d6fd579a872ef9524 [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 Dasdecd7a62015-05-16 22:39:47 -070016package org.onosproject.driver.pipeline;
17
Saurav Dasdecd7a62015-05-16 22:39:47 -070018import org.onlab.osgi.ServiceDirectory;
19import org.onlab.packet.Ethernet;
Jonathan Hart29be97d2016-02-02 14:23:48 -080020import org.onlab.packet.IpPrefix;
Saurav Dasdecd7a62015-05-16 22:39:47 -070021import org.onlab.packet.VlanId;
22import org.onlab.util.KryoNamespace;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.DeviceId;
Saurav Dasdecd7a62015-05-16 22:39:47 -070026import org.onosproject.net.behaviour.NextGroup;
27import org.onosproject.net.behaviour.Pipeliner;
28import org.onosproject.net.behaviour.PipelinerContext;
29import org.onosproject.net.driver.AbstractHandlerBehaviour;
30import org.onosproject.net.flow.DefaultFlowRule;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.FlowRuleOperations;
35import org.onosproject.net.flow.FlowRuleOperationsContext;
36import org.onosproject.net.flow.FlowRuleService;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
39import org.onosproject.net.flow.criteria.Criteria;
40import org.onosproject.net.flow.criteria.Criterion;
41import org.onosproject.net.flow.criteria.EthCriterion;
42import org.onosproject.net.flow.criteria.EthTypeCriterion;
43import org.onosproject.net.flow.criteria.IPCriterion;
44import org.onosproject.net.flow.criteria.PortCriterion;
45import org.onosproject.net.flow.criteria.VlanIdCriterion;
Saurav Das49cb5a12016-01-16 22:54:07 -080046import org.onosproject.net.flow.instructions.Instruction;
47import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Saurav Dasdecd7a62015-05-16 22:39:47 -070048import org.onosproject.net.flowobjective.FilteringObjective;
49import org.onosproject.net.flowobjective.FlowObjectiveStore;
50import org.onosproject.net.flowobjective.ForwardingObjective;
51import org.onosproject.net.flowobjective.NextObjective;
52import org.onosproject.net.flowobjective.Objective;
53import org.onosproject.net.flowobjective.ObjectiveError;
Saurav Dasdecd7a62015-05-16 22:39:47 -070054import org.onosproject.store.serializers.KryoNamespaces;
Jonathan Harte54bdbf2015-11-17 11:29:37 -080055import org.slf4j.Logger;
56
57import java.util.ArrayList;
58import java.util.Collection;
59import java.util.Collections;
Saurav Das24431192016-03-07 19:13:00 -080060import java.util.List;
Sho SHIMIZU45906042016-01-13 23:05:54 -080061import java.util.Objects;
Jonathan Harte54bdbf2015-11-17 11:29:37 -080062
Jonathan Hart29be97d2016-02-02 14:23:48 -080063import static org.onlab.util.Tools.delay;
Jonathan Harte54bdbf2015-11-17 11:29:37 -080064import static org.slf4j.LoggerFactory.getLogger;
Saurav Dasdecd7a62015-05-16 22:39:47 -070065
66/**
67 * Simple 2-Table Pipeline for Software/NPU based routers. This pipeline
68 * does not forward IP traffic to next-hop groups. Instead it forwards traffic
69 * using OF FlowMod actions.
70 */
71public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipeliner {
72
73 protected static final int FILTER_TABLE = 0;
74 protected static final int FIB_TABLE = 1;
75
76 private static final int DROP_PRIORITY = 0;
77 private static final int DEFAULT_PRIORITY = 0x8000;
78 private static final int HIGHEST_PRIORITY = 0xffff;
79
80 private ServiceDirectory serviceDirectory;
81 protected FlowRuleService flowRuleService;
82 private CoreService coreService;
83 private FlowObjectiveStore flowObjectiveStore;
84 protected DeviceId deviceId;
85 protected ApplicationId appId;
86 private ApplicationId driverId;
Saurav Dasdecd7a62015-05-16 22:39:47 -070087
88 private KryoNamespace appKryo = new KryoNamespace.Builder()
Jonathan Hart888eeda2016-05-20 13:42:26 -070089 .register(KryoNamespaces.API)
90 .register(DummyGroup.class)
91 .build();
Saurav Dasdecd7a62015-05-16 22:39:47 -070092
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(
Jonathan Harte54bdbf2015-11-17 11:29:37 -0800103 "org.onosproject.driver.SoftRouterPipeline");
Jonathan Hart6344f572015-12-15 08:26:25 -0800104
Saurav Dasdecd7a62015-05-16 22:39:47 -0700105 initializePipeline();
106 }
107
108 @Override
109 public void filter(FilteringObjective filteringObjective) {
110 if (filteringObjective.type() == FilteringObjective.Type.PERMIT) {
111 processFilter(filteringObjective,
112 filteringObjective.op() == Objective.Operation.ADD,
113 filteringObjective.appId());
114 } else {
115 fail(filteringObjective, ObjectiveError.UNSUPPORTED);
116 }
117 }
118
119 @Override
120 public void forward(ForwardingObjective fwd) {
121 Collection<FlowRule> rules;
122 FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
123
124 rules = processForward(fwd);
125 switch (fwd.op()) {
126 case ADD:
127 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800128 .filter(Objects::nonNull)
Saurav Dasdecd7a62015-05-16 22:39:47 -0700129 .forEach(flowOpsBuilder::add);
130 break;
131 case REMOVE:
132 rules.stream()
Sho SHIMIZU45906042016-01-13 23:05:54 -0800133 .filter(Objects::nonNull)
Saurav Dasdecd7a62015-05-16 22:39:47 -0700134 .forEach(flowOpsBuilder::remove);
135 break;
136 default:
137 fail(fwd, ObjectiveError.UNKNOWN);
138 log.warn("Unknown forwarding type {}", fwd.op());
139 }
140
141
142 flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() {
143 @Override
144 public void onSuccess(FlowRuleOperations ops) {
145 pass(fwd);
146 }
147
148 @Override
149 public void onError(FlowRuleOperations ops) {
150 fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED);
151 }
152 }));
153
154 }
155
156 @Override
157 public void next(NextObjective nextObjective) {
158 switch (nextObjective.type()) {
159 case SIMPLE:
160 Collection<TrafficTreatment> treatments = nextObjective.next();
161 if (treatments.size() != 1) {
162 log.error("Next Objectives of type Simple should only have a "
163 + "single Traffic Treatment. Next Objective Id:{}", nextObjective.id());
164 fail(nextObjective, ObjectiveError.BADPARAMS);
165 return;
166 }
167 processSimpleNextObjective(nextObjective);
168 break;
169 case HASHED:
170 case BROADCAST:
171 case FAILOVER:
172 fail(nextObjective, ObjectiveError.UNSUPPORTED);
173 log.warn("Unsupported next objective type {}", nextObjective.type());
174 break;
175 default:
176 fail(nextObjective, ObjectiveError.UNKNOWN);
177 log.warn("Unknown next objective type {}", nextObjective.type());
178 }
179 }
180
181 private void pass(Objective obj) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800182 obj.context().ifPresent(context -> context.onSuccess(obj));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700183 }
184
185 private void fail(Objective obj, ObjectiveError error) {
Sho SHIMIZUef7e2902016-02-12 18:38:29 -0800186 obj.context().ifPresent(context -> context.onError(obj, error));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700187 }
188
Saurav Dasdecd7a62015-05-16 22:39:47 -0700189 private void initializePipeline() {
190 //Drop rules for both tables
191 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
192 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
193 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
194
195 treatment.drop();
196
197 FlowRule rule = DefaultFlowRule.builder()
198 .forDevice(deviceId)
199 .withSelector(selector.build())
200 .withTreatment(treatment.build())
201 .withPriority(DROP_PRIORITY)
202 .fromApp(driverId)
203 .makePermanent()
204 .forTable(FILTER_TABLE)
205 .build();
206 ops = ops.add(rule);
207
208 rule = DefaultFlowRule.builder().forDevice(deviceId)
209 .withSelector(selector.build())
210 .withTreatment(treatment.build())
211 .withPriority(DROP_PRIORITY)
212 .fromApp(driverId)
213 .makePermanent()
214 .forTable(FIB_TABLE)
215 .build();
216 ops = ops.add(rule);
217
218 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
219 @Override
220 public void onSuccess(FlowRuleOperations ops) {
221 log.info("Provisioned drop rules in both tables");
222 }
223
224 @Override
225 public void onError(FlowRuleOperations ops) {
226 log.info("Failed to provision drop rules");
227 }
228 }));
229 }
230
231 private void processFilter(FilteringObjective filt, boolean install,
232 ApplicationId applicationId) {
233 // This driver only processes filtering criteria defined with switch
234 // ports as the key
Jonathan Hart6344f572015-12-15 08:26:25 -0800235 PortCriterion p;
236 EthCriterion e = null;
237 VlanIdCriterion v = null;
238
Saurav Dasdecd7a62015-05-16 22:39:47 -0700239 if (!filt.key().equals(Criteria.dummy()) &&
240 filt.key().type() == Criterion.Type.IN_PORT) {
241 p = (PortCriterion) filt.key();
242 } else {
243 log.warn("No key defined in filtering objective from app: {}. Not"
244 + "processing filtering objective", applicationId);
245 fail(filt, ObjectiveError.UNKNOWN);
246 return;
247 }
248
249 // convert filtering conditions for switch-intfs into flowrules
250 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
251 for (Criterion c : filt.conditions()) {
gauravadc90042016-05-09 23:17:05 +0530252 if (c.type() == Criterion.Type.ETH_DST ||
253 c.type() == Criterion.Type.ETH_DST_MASKED) {
Saurav Dasdecd7a62015-05-16 22:39:47 -0700254 e = (EthCriterion) c;
255 } else if (c.type() == Criterion.Type.VLAN_VID) {
256 v = (VlanIdCriterion) c;
Saurav Dasdecd7a62015-05-16 22:39:47 -0700257 } else {
258 log.error("Unsupported filter {}", c);
259 fail(filt, ObjectiveError.UNSUPPORTED);
260 return;
261 }
262 }
263
Jonathan Hartf8035d32016-06-16 16:23:26 -0700264 log.debug("Modifying Port/VLAN/MAC filtering rules in filter table: {}/{}/{}",
Saurav Dasdecd7a62015-05-16 22:39:47 -0700265 p.port(), v.vlanId(), e.mac());
266 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
267 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
268 selector.matchInPort(p.port());
Jonathan Hart6344f572015-12-15 08:26:25 -0800269
gauravadc90042016-05-09 23:17:05 +0530270 //Multicast MAC
271 if (e.mask() != null) {
272 selector.matchEthDstMasked(e.mac(), e.mask());
273 } else {
274 selector.matchEthDst(e.mac());
275 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800276 selector.matchVlanId(v.vlanId());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700277 selector.matchEthType(Ethernet.TYPE_IPV4);
Jonathan Hartca47cd72015-12-13 12:31:09 -0800278 if (!v.vlanId().equals(VlanId.NONE)) {
279 treatment.popVlan();
280 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800281 treatment.transition(FIB_TABLE);
Saurav Dasdecd7a62015-05-16 22:39:47 -0700282 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(FILTER_TABLE).build();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700290
Saurav Dasdecd7a62015-05-16 22:39:47 -0700291 ops = install ? ops.add(rule) : ops.remove(rule);
292 // apply filtering flow rules
293 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
294 @Override
295 public void onSuccess(FlowRuleOperations ops) {
296 log.info("Applied filtering rules");
297 pass(filt);
298 }
299
300 @Override
301 public void onError(FlowRuleOperations ops) {
302 log.info("Failed to apply filtering rules");
303 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
304 }
305 }));
306 }
307
308 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
309 switch (fwd.flag()) {
310 case SPECIFIC:
311 return processSpecific(fwd);
312 case VERSATILE:
313 return processVersatile(fwd);
314 default:
315 fail(fwd, ObjectiveError.UNKNOWN);
316 log.warn("Unknown forwarding flag {}", fwd.flag());
317 }
318 return Collections.emptySet();
319 }
320
321 /**
Saurav Das49cb5a12016-01-16 22:54:07 -0800322 * SoftRouter has a single versatile table - the filter table.
323 * This table can be used to filter entries that reach the next table (FIB table).
324 * It can also be used to punt packets to the controller and/or bypass
325 * the FIB table to forward out of a port.
Saurav Dasdecd7a62015-05-16 22:39:47 -0700326 *
327 * @param fwd The forwarding objective of type versatile
328 * @return A collection of flow rules meant to be delivered to the flowrule
329 * subsystem. May return empty collection in case of failures.
330 */
331 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800332 log.debug("Received versatile fwd: to next:{}", fwd.nextId());
Jonathan Hart6344f572015-12-15 08:26:25 -0800333 Collection<FlowRule> flowrules = new ArrayList<>();
Saurav Das49cb5a12016-01-16 22:54:07 -0800334 if (fwd.nextId() == null && fwd.treatment() == null) {
335 log.error("Forwarding objective {} from {} must contain "
336 + "nextId or Treatment", fwd.selector(), fwd.appId());
337 return Collections.emptySet();
338 }
339 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
340 if (fwd.treatment() != null) {
341 fwd.treatment().immediate().forEach(ins -> ttBuilder.add(ins));
342 }
343 //convert nextId to flow actions
344 if (fwd.nextId() != null) {
345 // only acceptable value is output to port
346 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
347 if (next == null) {
348 log.error("next-id {} does not exist in store", fwd.nextId());
349 return Collections.emptySet();
350 }
351 TrafficTreatment nt = appKryo.deserialize(next.data());
352 if (nt == null) {
353 log.error("Error in deserializing next-id {}", fwd.nextId());
354 return Collections.emptySet();
355 }
356 for (Instruction ins : nt.allInstructions()) {
357 if (ins instanceof OutputInstruction) {
358 ttBuilder.add(ins);
359 }
360 }
361 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800362
363 FlowRule rule = DefaultFlowRule.builder()
364 .withSelector(fwd.selector())
Saurav Das49cb5a12016-01-16 22:54:07 -0800365 .withTreatment(ttBuilder.build())
Jonathan Hart6344f572015-12-15 08:26:25 -0800366 .makePermanent()
367 .forDevice(deviceId)
368 .fromApp(fwd.appId())
369 .withPriority(fwd.priority())
370 .build();
371
372 flowrules.add(rule);
373
Saurav Dasdecd7a62015-05-16 22:39:47 -0700374 return flowrules;
375 }
376
Saurav Dasdecd7a62015-05-16 22:39:47 -0700377 /**
378 * SoftRouter has a single specific table - the FIB Table. It emulates
379 * LPM matching of dstIP by using higher priority flows for longer prefixes.
380 * Flows are forwarded using flow-actions
381 *
382 * @param fwd The forwarding objective of type simple
383 * @return A collection of flow rules meant to be delivered to the flowrule
384 * subsystem. Typically the returned collection has a single flowrule.
385 * May return empty collection in case of failures.
386 *
387 */
388 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800389 log.debug("Processing specific forwarding objective to next:{}", fwd.nextId());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700390 TrafficSelector selector = fwd.selector();
391 EthTypeCriterion ethType =
392 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
393 // XXX currently supporting only the L3 unicast table
alshabibcaf1ca22015-06-25 15:18:16 -0700394 if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) {
Saurav Dasdecd7a62015-05-16 22:39:47 -0700395 fail(fwd, ObjectiveError.UNSUPPORTED);
396 return Collections.emptySet();
397 }
398
Jonathan Hart29be97d2016-02-02 14:23:48 -0800399 IpPrefix ipPrefix = ((IPCriterion)
400 selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
401
402 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder()
403 .matchEthType(Ethernet.TYPE_IPV4);
404
405 if (ipPrefix.prefixLength() != 0) {
406 filteredSelector.matchIPDst(ipPrefix);
407 }
Saurav Dasdecd7a62015-05-16 22:39:47 -0700408
409 TrafficTreatment tt = null;
410 if (fwd.nextId() != null) {
411 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
412 if (next == null) {
413 log.error("next-id {} does not exist in store", fwd.nextId());
414 return Collections.emptySet();
415 }
416 tt = appKryo.deserialize(next.data());
417 if (tt == null) {
418 log.error("Error in deserializing next-id {}", fwd.nextId());
419 return Collections.emptySet();
420 }
421 }
422
423 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
424 .fromApp(fwd.appId())
425 .withPriority(fwd.priority())
426 .forDevice(deviceId)
Jonathan Hart29be97d2016-02-02 14:23:48 -0800427 .withSelector(filteredSelector.build());
Jonathan Hart3930f632015-10-19 12:12:51 -0700428
429 if (tt != null) {
430 ruleBuilder.withTreatment(tt);
431 }
Saurav Dasdecd7a62015-05-16 22:39:47 -0700432
433 if (fwd.permanent()) {
434 ruleBuilder.makePermanent();
435 } else {
436 ruleBuilder.makeTemporary(fwd.timeout());
437 }
438
439 ruleBuilder.forTable(FIB_TABLE);
440 return Collections.singletonList(ruleBuilder.build());
441 }
442
443 /**
444 * Next Objectives are stored as dummy groups for retrieval later
445 * when Forwarding Objectives reference the next objective id. At that point
446 * the dummy group is fetched from the distributed store and the enclosed
447 * treatment is applied as a flow rule action.
448 *
alshabibcaf1ca22015-06-25 15:18:16 -0700449 * @param nextObj the next objective of type simple
Saurav Dasdecd7a62015-05-16 22:39:47 -0700450 */
451 private void processSimpleNextObjective(NextObjective nextObj) {
452 // Simple next objective has a single treatment (not a collection)
Saurav Das49cb5a12016-01-16 22:54:07 -0800453 log.debug("Received nextObj {}", nextObj.id());
454 // delay processing to emulate group creation
455 delay(50);
Saurav Das6c44a632015-05-30 22:05:22 -0700456 TrafficTreatment treatment = nextObj.next().iterator().next();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700457 flowObjectiveStore.putNextGroup(nextObj.id(),
Saurav Das6c44a632015-05-30 22:05:22 -0700458 new DummyGroup(treatment));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700459 }
460
Saurav Dasdecd7a62015-05-16 22:39:47 -0700461 private class DummyGroup implements NextGroup {
462 TrafficTreatment nextActions;
463
464 public DummyGroup(TrafficTreatment next) {
465 this.nextActions = next;
466 }
467
468 @Override
469 public byte[] data() {
470 return appKryo.serialize(nextActions);
471 }
472
473 }
474
Saurav Das24431192016-03-07 19:13:00 -0800475 @Override
476 public List<String> getNextMappings(NextGroup nextGroup) {
477 // nextObjectives converted to flow-actions not groups
478 return Collections.emptyList();
479 }
480
Saurav Dasdecd7a62015-05-16 22:39:47 -0700481}