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