Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 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 Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.pipeline; |
| 17 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 18 | import org.onlab.osgi.ServiceDirectory; |
| 19 | import org.onlab.packet.Ethernet; |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onlab.util.KryoNamespace; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.core.CoreService; |
| 24 | import org.onosproject.net.DeviceId; |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 25 | import org.onosproject.net.behaviour.NextGroup; |
| 26 | import org.onosproject.net.behaviour.Pipeliner; |
| 27 | import org.onosproject.net.behaviour.PipelinerContext; |
| 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.onosproject.net.flow.DefaultFlowRule; |
| 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 32 | import org.onosproject.net.flow.FlowRule; |
| 33 | import org.onosproject.net.flow.FlowRuleOperations; |
| 34 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 35 | import org.onosproject.net.flow.FlowRuleService; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.flow.criteria.Criteria; |
| 39 | import org.onosproject.net.flow.criteria.Criterion; |
| 40 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 41 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 42 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 43 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 44 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 45 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 46 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 47 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 48 | import org.onosproject.net.flowobjective.NextObjective; |
| 49 | import org.onosproject.net.flowobjective.Objective; |
| 50 | import org.onosproject.net.flowobjective.ObjectiveError; |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 51 | import org.onosproject.store.serializers.KryoNamespaces; |
Jonathan Hart | e54bdbf | 2015-11-17 11:29:37 -0800 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | |
| 54 | import java.util.ArrayList; |
| 55 | import java.util.Collection; |
| 56 | import java.util.Collections; |
Jonathan Hart | e54bdbf | 2015-11-17 11:29:37 -0800 | [diff] [blame] | 57 | |
| 58 | import static org.slf4j.LoggerFactory.getLogger; |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Simple 2-Table Pipeline for Software/NPU based routers. This pipeline |
| 62 | * does not forward IP traffic to next-hop groups. Instead it forwards traffic |
| 63 | * using OF FlowMod actions. |
| 64 | */ |
| 65 | public class SoftRouterPipeline extends AbstractHandlerBehaviour implements Pipeliner { |
| 66 | |
| 67 | protected static final int FILTER_TABLE = 0; |
| 68 | protected static final int FIB_TABLE = 1; |
| 69 | |
| 70 | private static final int DROP_PRIORITY = 0; |
| 71 | private static final int DEFAULT_PRIORITY = 0x8000; |
| 72 | private static final int HIGHEST_PRIORITY = 0xffff; |
| 73 | |
| 74 | private ServiceDirectory serviceDirectory; |
| 75 | protected FlowRuleService flowRuleService; |
| 76 | private CoreService coreService; |
| 77 | private FlowObjectiveStore flowObjectiveStore; |
| 78 | protected DeviceId deviceId; |
| 79 | protected ApplicationId appId; |
| 80 | private ApplicationId driverId; |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 81 | |
| 82 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
| 83 | .register(DummyGroup.class) |
| 84 | .register(KryoNamespaces.API) |
| 85 | .register(byte[].class) |
| 86 | .build(); |
| 87 | |
| 88 | private final Logger log = getLogger(getClass()); |
| 89 | |
| 90 | @Override |
| 91 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 92 | this.serviceDirectory = context.directory(); |
| 93 | this.deviceId = deviceId; |
| 94 | coreService = serviceDirectory.get(CoreService.class); |
| 95 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 96 | flowObjectiveStore = context.store(); |
| 97 | driverId = coreService.registerApplication( |
Jonathan Hart | e54bdbf | 2015-11-17 11:29:37 -0800 | [diff] [blame] | 98 | "org.onosproject.driver.SoftRouterPipeline"); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame^] | 99 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 100 | initializePipeline(); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void filter(FilteringObjective filteringObjective) { |
| 105 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 106 | processFilter(filteringObjective, |
| 107 | filteringObjective.op() == Objective.Operation.ADD, |
| 108 | filteringObjective.appId()); |
| 109 | } else { |
| 110 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void forward(ForwardingObjective fwd) { |
| 116 | Collection<FlowRule> rules; |
| 117 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
| 118 | |
| 119 | rules = processForward(fwd); |
| 120 | switch (fwd.op()) { |
| 121 | case ADD: |
| 122 | rules.stream() |
| 123 | .filter(rule -> rule != null) |
| 124 | .forEach(flowOpsBuilder::add); |
| 125 | break; |
| 126 | case REMOVE: |
| 127 | rules.stream() |
| 128 | .filter(rule -> rule != null) |
| 129 | .forEach(flowOpsBuilder::remove); |
| 130 | break; |
| 131 | default: |
| 132 | fail(fwd, ObjectiveError.UNKNOWN); |
| 133 | log.warn("Unknown forwarding type {}", fwd.op()); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 138 | @Override |
| 139 | public void onSuccess(FlowRuleOperations ops) { |
| 140 | pass(fwd); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onError(FlowRuleOperations ops) { |
| 145 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 146 | } |
| 147 | })); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void next(NextObjective nextObjective) { |
| 153 | switch (nextObjective.type()) { |
| 154 | case SIMPLE: |
| 155 | Collection<TrafficTreatment> treatments = nextObjective.next(); |
| 156 | if (treatments.size() != 1) { |
| 157 | log.error("Next Objectives of type Simple should only have a " |
| 158 | + "single Traffic Treatment. Next Objective Id:{}", nextObjective.id()); |
| 159 | fail(nextObjective, ObjectiveError.BADPARAMS); |
| 160 | return; |
| 161 | } |
| 162 | processSimpleNextObjective(nextObjective); |
| 163 | break; |
| 164 | case HASHED: |
| 165 | case BROADCAST: |
| 166 | case FAILOVER: |
| 167 | fail(nextObjective, ObjectiveError.UNSUPPORTED); |
| 168 | log.warn("Unsupported next objective type {}", nextObjective.type()); |
| 169 | break; |
| 170 | default: |
| 171 | fail(nextObjective, ObjectiveError.UNKNOWN); |
| 172 | log.warn("Unknown next objective type {}", nextObjective.type()); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private void pass(Objective obj) { |
| 177 | if (obj.context().isPresent()) { |
| 178 | obj.context().get().onSuccess(obj); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private void fail(Objective obj, ObjectiveError error) { |
| 183 | if (obj.context().isPresent()) { |
| 184 | obj.context().get().onError(obj, error); |
| 185 | } |
| 186 | } |
| 187 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 188 | private void initializePipeline() { |
| 189 | //Drop rules for both tables |
| 190 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 191 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 192 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 193 | |
| 194 | treatment.drop(); |
| 195 | |
| 196 | FlowRule rule = DefaultFlowRule.builder() |
| 197 | .forDevice(deviceId) |
| 198 | .withSelector(selector.build()) |
| 199 | .withTreatment(treatment.build()) |
| 200 | .withPriority(DROP_PRIORITY) |
| 201 | .fromApp(driverId) |
| 202 | .makePermanent() |
| 203 | .forTable(FILTER_TABLE) |
| 204 | .build(); |
| 205 | ops = ops.add(rule); |
| 206 | |
| 207 | rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 208 | .withSelector(selector.build()) |
| 209 | .withTreatment(treatment.build()) |
| 210 | .withPriority(DROP_PRIORITY) |
| 211 | .fromApp(driverId) |
| 212 | .makePermanent() |
| 213 | .forTable(FIB_TABLE) |
| 214 | .build(); |
| 215 | ops = ops.add(rule); |
| 216 | |
| 217 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 218 | @Override |
| 219 | public void onSuccess(FlowRuleOperations ops) { |
| 220 | log.info("Provisioned drop rules in both tables"); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void onError(FlowRuleOperations ops) { |
| 225 | log.info("Failed to provision drop rules"); |
| 226 | } |
| 227 | })); |
| 228 | } |
| 229 | |
| 230 | private void processFilter(FilteringObjective filt, boolean install, |
| 231 | ApplicationId applicationId) { |
| 232 | // This driver only processes filtering criteria defined with switch |
| 233 | // ports as the key |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame^] | 234 | PortCriterion p; |
| 235 | EthCriterion e = null; |
| 236 | VlanIdCriterion v = null; |
| 237 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 238 | if (!filt.key().equals(Criteria.dummy()) && |
| 239 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 240 | p = (PortCriterion) filt.key(); |
| 241 | } else { |
| 242 | log.warn("No key defined in filtering objective from app: {}. Not" |
| 243 | + "processing filtering objective", applicationId); |
| 244 | fail(filt, ObjectiveError.UNKNOWN); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // convert filtering conditions for switch-intfs into flowrules |
| 249 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 250 | for (Criterion c : filt.conditions()) { |
| 251 | if (c.type() == Criterion.Type.ETH_DST) { |
| 252 | e = (EthCriterion) c; |
| 253 | } else if (c.type() == Criterion.Type.VLAN_VID) { |
| 254 | v = (VlanIdCriterion) c; |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 255 | } else { |
| 256 | log.error("Unsupported filter {}", c); |
| 257 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 258 | return; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | log.debug("adding Port/VLAN/MAC filtering rules in filter table: {}/{}/{}", |
| 263 | p.port(), v.vlanId(), e.mac()); |
| 264 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 265 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 266 | selector.matchInPort(p.port()); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame^] | 267 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 268 | selector.matchEthDst(e.mac()); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame^] | 269 | selector.matchVlanId(v.vlanId()); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 270 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 271 | if (!v.vlanId().equals(VlanId.NONE)) { |
| 272 | treatment.popVlan(); |
| 273 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame^] | 274 | treatment.transition(FIB_TABLE); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 275 | FlowRule rule = DefaultFlowRule.builder() |
| 276 | .forDevice(deviceId) |
| 277 | .withSelector(selector.build()) |
| 278 | .withTreatment(treatment.build()) |
| 279 | .withPriority(DEFAULT_PRIORITY) |
| 280 | .fromApp(applicationId) |
| 281 | .makePermanent() |
| 282 | .forTable(FILTER_TABLE).build(); |
| 283 | ops = ops.add(rule); |
| 284 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 285 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 286 | // apply filtering flow rules |
| 287 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 288 | @Override |
| 289 | public void onSuccess(FlowRuleOperations ops) { |
| 290 | log.info("Applied filtering rules"); |
| 291 | pass(filt); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void onError(FlowRuleOperations ops) { |
| 296 | log.info("Failed to apply filtering rules"); |
| 297 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 298 | } |
| 299 | })); |
| 300 | } |
| 301 | |
| 302 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 303 | switch (fwd.flag()) { |
| 304 | case SPECIFIC: |
| 305 | return processSpecific(fwd); |
| 306 | case VERSATILE: |
| 307 | return processVersatile(fwd); |
| 308 | default: |
| 309 | fail(fwd, ObjectiveError.UNKNOWN); |
| 310 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 311 | } |
| 312 | return Collections.emptySet(); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * SoftRouter has a single versatile table - the filter table. All versatile |
| 317 | * flow rules must include the filtering rules. |
| 318 | * |
| 319 | * @param fwd The forwarding objective of type versatile |
| 320 | * @return A collection of flow rules meant to be delivered to the flowrule |
| 321 | * subsystem. May return empty collection in case of failures. |
| 322 | */ |
| 323 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame^] | 324 | Collection<FlowRule> flowrules = new ArrayList<>(); |
| 325 | |
| 326 | FlowRule rule = DefaultFlowRule.builder() |
| 327 | .withSelector(fwd.selector()) |
| 328 | .withTreatment(fwd.treatment()) |
| 329 | .makePermanent() |
| 330 | .forDevice(deviceId) |
| 331 | .fromApp(fwd.appId()) |
| 332 | .withPriority(fwd.priority()) |
| 333 | .build(); |
| 334 | |
| 335 | flowrules.add(rule); |
| 336 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 337 | return flowrules; |
| 338 | } |
| 339 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 340 | /** |
| 341 | * SoftRouter has a single specific table - the FIB Table. It emulates |
| 342 | * LPM matching of dstIP by using higher priority flows for longer prefixes. |
| 343 | * Flows are forwarded using flow-actions |
| 344 | * |
| 345 | * @param fwd The forwarding objective of type simple |
| 346 | * @return A collection of flow rules meant to be delivered to the flowrule |
| 347 | * subsystem. Typically the returned collection has a single flowrule. |
| 348 | * May return empty collection in case of failures. |
| 349 | * |
| 350 | */ |
| 351 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
| 352 | log.debug("Processing specific forwarding objective"); |
| 353 | TrafficSelector selector = fwd.selector(); |
| 354 | EthTypeCriterion ethType = |
| 355 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 356 | // XXX currently supporting only the L3 unicast table |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 357 | if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) { |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 358 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 359 | return Collections.emptySet(); |
| 360 | } |
| 361 | |
| 362 | TrafficSelector filteredSelector = |
| 363 | DefaultTrafficSelector.builder() |
| 364 | .matchEthType(Ethernet.TYPE_IPV4) |
| 365 | .matchIPDst(((IPCriterion) |
| 366 | selector.getCriterion(Criterion.Type.IPV4_DST)).ip()) |
| 367 | .build(); |
| 368 | |
| 369 | TrafficTreatment tt = null; |
| 370 | if (fwd.nextId() != null) { |
| 371 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 372 | if (next == null) { |
| 373 | log.error("next-id {} does not exist in store", fwd.nextId()); |
| 374 | return Collections.emptySet(); |
| 375 | } |
| 376 | tt = appKryo.deserialize(next.data()); |
| 377 | if (tt == null) { |
| 378 | log.error("Error in deserializing next-id {}", fwd.nextId()); |
| 379 | return Collections.emptySet(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 384 | .fromApp(fwd.appId()) |
| 385 | .withPriority(fwd.priority()) |
| 386 | .forDevice(deviceId) |
| 387 | .withSelector(filteredSelector) |
| 388 | .withTreatment(tt); |
| 389 | |
| 390 | if (fwd.permanent()) { |
| 391 | ruleBuilder.makePermanent(); |
| 392 | } else { |
| 393 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 394 | } |
| 395 | |
| 396 | ruleBuilder.forTable(FIB_TABLE); |
| 397 | return Collections.singletonList(ruleBuilder.build()); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Next Objectives are stored as dummy groups for retrieval later |
| 402 | * when Forwarding Objectives reference the next objective id. At that point |
| 403 | * the dummy group is fetched from the distributed store and the enclosed |
| 404 | * treatment is applied as a flow rule action. |
| 405 | * |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 406 | * @param nextObj the next objective of type simple |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 407 | */ |
| 408 | private void processSimpleNextObjective(NextObjective nextObj) { |
| 409 | // Simple next objective has a single treatment (not a collection) |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 410 | TrafficTreatment treatment = nextObj.next().iterator().next(); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 411 | flowObjectiveStore.putNextGroup(nextObj.id(), |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 412 | new DummyGroup(treatment)); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 415 | private class DummyGroup implements NextGroup { |
| 416 | TrafficTreatment nextActions; |
| 417 | |
| 418 | public DummyGroup(TrafficTreatment next) { |
| 419 | this.nextActions = next; |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public byte[] data() { |
| 424 | return appKryo.serialize(nextActions); |
| 425 | } |
| 426 | |
| 427 | } |
| 428 | |
| 429 | } |