blob: 55e8bbf2728f24253d15392abde834897f90c1ee [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
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;
Sho SHIMIZU45906042016-01-13 23:05:54 -080060import java.util.Objects;
Jonathan Harte54bdbf2015-11-17 11:29:37 -080061
Jonathan Hart29be97d2016-02-02 14:23:48 -080062import static org.onlab.util.Tools.delay;
Jonathan Harte54bdbf2015-11-17 11:29:37 -080063import static org.slf4j.LoggerFactory.getLogger;
Saurav Dasdecd7a62015-05-16 22:39:47 -070064
65/**
66 * Simple 2-Table Pipeline for Software/NPU based routers. This pipeline
67 * does not forward IP traffic to next-hop groups. Instead it forwards traffic
68 * using OF FlowMod actions.
69 */
70public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipeliner {
71
72 protected static final int FILTER_TABLE = 0;
73 protected static final int FIB_TABLE = 1;
74
75 private static final int DROP_PRIORITY = 0;
76 private static final int DEFAULT_PRIORITY = 0x8000;
77 private static final int HIGHEST_PRIORITY = 0xffff;
78
79 private ServiceDirectory serviceDirectory;
80 protected FlowRuleService flowRuleService;
81 private CoreService coreService;
82 private FlowObjectiveStore flowObjectiveStore;
83 protected DeviceId deviceId;
84 protected ApplicationId appId;
85 private ApplicationId driverId;
Saurav Dasdecd7a62015-05-16 22:39:47 -070086
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(
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()) {
252 if (c.type() == Criterion.Type.ETH_DST) {
253 e = (EthCriterion) c;
254 } else if (c.type() == Criterion.Type.VLAN_VID) {
255 v = (VlanIdCriterion) c;
Saurav Dasdecd7a62015-05-16 22:39:47 -0700256 } else {
257 log.error("Unsupported filter {}", c);
258 fail(filt, ObjectiveError.UNSUPPORTED);
259 return;
260 }
261 }
262
263 log.debug("adding Port/VLAN/MAC filtering rules in filter table: {}/{}/{}",
264 p.port(), v.vlanId(), e.mac());
265 TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
266 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
267 selector.matchInPort(p.port());
Jonathan Hart6344f572015-12-15 08:26:25 -0800268
Saurav Dasdecd7a62015-05-16 22:39:47 -0700269 selector.matchEthDst(e.mac());
Jonathan Hart6344f572015-12-15 08:26:25 -0800270 selector.matchVlanId(v.vlanId());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700271 selector.matchEthType(Ethernet.TYPE_IPV4);
Jonathan Hartca47cd72015-12-13 12:31:09 -0800272 if (!v.vlanId().equals(VlanId.NONE)) {
273 treatment.popVlan();
274 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800275 treatment.transition(FIB_TABLE);
Saurav Dasdecd7a62015-05-16 22:39:47 -0700276 FlowRule rule = DefaultFlowRule.builder()
277 .forDevice(deviceId)
278 .withSelector(selector.build())
279 .withTreatment(treatment.build())
280 .withPriority(DEFAULT_PRIORITY)
281 .fromApp(applicationId)
282 .makePermanent()
283 .forTable(FILTER_TABLE).build();
284 ops = ops.add(rule);
285
Saurav Dasdecd7a62015-05-16 22:39:47 -0700286 ops = install ? ops.add(rule) : ops.remove(rule);
287 // apply filtering flow rules
288 flowRuleService.apply(ops.build(new FlowRuleOperationsContext() {
289 @Override
290 public void onSuccess(FlowRuleOperations ops) {
291 log.info("Applied filtering rules");
292 pass(filt);
293 }
294
295 @Override
296 public void onError(FlowRuleOperations ops) {
297 log.info("Failed to apply filtering rules");
298 fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED);
299 }
300 }));
301 }
302
303 private Collection<FlowRule> processForward(ForwardingObjective fwd) {
304 switch (fwd.flag()) {
305 case SPECIFIC:
306 return processSpecific(fwd);
307 case VERSATILE:
308 return processVersatile(fwd);
309 default:
310 fail(fwd, ObjectiveError.UNKNOWN);
311 log.warn("Unknown forwarding flag {}", fwd.flag());
312 }
313 return Collections.emptySet();
314 }
315
316 /**
Saurav Das49cb5a12016-01-16 22:54:07 -0800317 * SoftRouter has a single versatile table - the filter table.
318 * This table can be used to filter entries that reach the next table (FIB table).
319 * It can also be used to punt packets to the controller and/or bypass
320 * the FIB table to forward out of a port.
Saurav Dasdecd7a62015-05-16 22:39:47 -0700321 *
322 * @param fwd The forwarding objective of type versatile
323 * @return A collection of flow rules meant to be delivered to the flowrule
324 * subsystem. May return empty collection in case of failures.
325 */
326 private Collection<FlowRule> processVersatile(ForwardingObjective fwd) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800327 log.debug("Received versatile fwd: to next:{}", fwd.nextId());
Jonathan Hart6344f572015-12-15 08:26:25 -0800328 Collection<FlowRule> flowrules = new ArrayList<>();
Saurav Das49cb5a12016-01-16 22:54:07 -0800329 if (fwd.nextId() == null && fwd.treatment() == null) {
330 log.error("Forwarding objective {} from {} must contain "
331 + "nextId or Treatment", fwd.selector(), fwd.appId());
332 return Collections.emptySet();
333 }
334 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
335 if (fwd.treatment() != null) {
336 fwd.treatment().immediate().forEach(ins -> ttBuilder.add(ins));
337 }
338 //convert nextId to flow actions
339 if (fwd.nextId() != null) {
340 // only acceptable value is output to port
341 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
342 if (next == null) {
343 log.error("next-id {} does not exist in store", fwd.nextId());
344 return Collections.emptySet();
345 }
346 TrafficTreatment nt = appKryo.deserialize(next.data());
347 if (nt == null) {
348 log.error("Error in deserializing next-id {}", fwd.nextId());
349 return Collections.emptySet();
350 }
351 for (Instruction ins : nt.allInstructions()) {
352 if (ins instanceof OutputInstruction) {
353 ttBuilder.add(ins);
354 }
355 }
356 }
Jonathan Hart6344f572015-12-15 08:26:25 -0800357
358 FlowRule rule = DefaultFlowRule.builder()
359 .withSelector(fwd.selector())
Saurav Das49cb5a12016-01-16 22:54:07 -0800360 .withTreatment(ttBuilder.build())
Jonathan Hart6344f572015-12-15 08:26:25 -0800361 .makePermanent()
362 .forDevice(deviceId)
363 .fromApp(fwd.appId())
364 .withPriority(fwd.priority())
365 .build();
366
367 flowrules.add(rule);
368
Saurav Dasdecd7a62015-05-16 22:39:47 -0700369 return flowrules;
370 }
371
Saurav Dasdecd7a62015-05-16 22:39:47 -0700372 /**
373 * SoftRouter has a single specific table - the FIB Table. It emulates
374 * LPM matching of dstIP by using higher priority flows for longer prefixes.
375 * Flows are forwarded using flow-actions
376 *
377 * @param fwd The forwarding objective of type simple
378 * @return A collection of flow rules meant to be delivered to the flowrule
379 * subsystem. Typically the returned collection has a single flowrule.
380 * May return empty collection in case of failures.
381 *
382 */
383 private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
Saurav Das49cb5a12016-01-16 22:54:07 -0800384 log.debug("Processing specific forwarding objective to next:{}", fwd.nextId());
Saurav Dasdecd7a62015-05-16 22:39:47 -0700385 TrafficSelector selector = fwd.selector();
386 EthTypeCriterion ethType =
387 (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE);
388 // XXX currently supporting only the L3 unicast table
alshabibcaf1ca22015-06-25 15:18:16 -0700389 if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) {
Saurav Dasdecd7a62015-05-16 22:39:47 -0700390 fail(fwd, ObjectiveError.UNSUPPORTED);
391 return Collections.emptySet();
392 }
393
Jonathan Hart29be97d2016-02-02 14:23:48 -0800394 IpPrefix ipPrefix = ((IPCriterion)
395 selector.getCriterion(Criterion.Type.IPV4_DST)).ip();
396
397 TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder()
398 .matchEthType(Ethernet.TYPE_IPV4);
399
400 if (ipPrefix.prefixLength() != 0) {
401 filteredSelector.matchIPDst(ipPrefix);
402 }
Saurav Dasdecd7a62015-05-16 22:39:47 -0700403
404 TrafficTreatment tt = null;
405 if (fwd.nextId() != null) {
406 NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId());
407 if (next == null) {
408 log.error("next-id {} does not exist in store", fwd.nextId());
409 return Collections.emptySet();
410 }
411 tt = appKryo.deserialize(next.data());
412 if (tt == null) {
413 log.error("Error in deserializing next-id {}", fwd.nextId());
414 return Collections.emptySet();
415 }
416 }
417
418 FlowRule.Builder ruleBuilder = DefaultFlowRule.builder()
419 .fromApp(fwd.appId())
420 .withPriority(fwd.priority())
421 .forDevice(deviceId)
Jonathan Hart29be97d2016-02-02 14:23:48 -0800422 .withSelector(filteredSelector.build());
Jonathan Hart3930f632015-10-19 12:12:51 -0700423
424 if (tt != null) {
425 ruleBuilder.withTreatment(tt);
426 }
Saurav Dasdecd7a62015-05-16 22:39:47 -0700427
428 if (fwd.permanent()) {
429 ruleBuilder.makePermanent();
430 } else {
431 ruleBuilder.makeTemporary(fwd.timeout());
432 }
433
434 ruleBuilder.forTable(FIB_TABLE);
435 return Collections.singletonList(ruleBuilder.build());
436 }
437
438 /**
439 * Next Objectives are stored as dummy groups for retrieval later
440 * when Forwarding Objectives reference the next objective id. At that point
441 * the dummy group is fetched from the distributed store and the enclosed
442 * treatment is applied as a flow rule action.
443 *
alshabibcaf1ca22015-06-25 15:18:16 -0700444 * @param nextObj the next objective of type simple
Saurav Dasdecd7a62015-05-16 22:39:47 -0700445 */
446 private void processSimpleNextObjective(NextObjective nextObj) {
447 // Simple next objective has a single treatment (not a collection)
Saurav Das49cb5a12016-01-16 22:54:07 -0800448 log.debug("Received nextObj {}", nextObj.id());
449 // delay processing to emulate group creation
450 delay(50);
Saurav Das6c44a632015-05-30 22:05:22 -0700451 TrafficTreatment treatment = nextObj.next().iterator().next();
Saurav Dasdecd7a62015-05-16 22:39:47 -0700452 flowObjectiveStore.putNextGroup(nextObj.id(),
Saurav Das6c44a632015-05-30 22:05:22 -0700453 new DummyGroup(treatment));
Saurav Dasdecd7a62015-05-16 22:39:47 -0700454 }
455
Saurav Dasdecd7a62015-05-16 22:39:47 -0700456 private class DummyGroup implements NextGroup {
457 TrafficTreatment nextActions;
458
459 public DummyGroup(TrafficTreatment next) {
460 this.nextActions = next;
461 }
462
463 @Override
464 public byte[] data() {
465 return appKryo.serialize(nextActions);
466 }
467
468 }
469
470}