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() |
Jonathan Hart | 888eeda | 2016-05-20 13:42:26 -0700 | [diff] [blame] | 89 | .register(KryoNamespaces.API) |
| 90 | .register(DummyGroup.class) |
| 91 | .build(); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 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 Hart | e54bdbf | 2015-11-17 11:29:37 -0800 | [diff] [blame] | 103 | "org.onosproject.driver.SoftRouterPipeline"); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 104 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 105 | 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 SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 128 | .filter(Objects::nonNull) |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 129 | .forEach(flowOpsBuilder::add); |
| 130 | break; |
| 131 | case REMOVE: |
| 132 | rules.stream() |
Sho SHIMIZU | 4590604 | 2016-01-13 23:05:54 -0800 | [diff] [blame] | 133 | .filter(Objects::nonNull) |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 134 | .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 SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 182 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 186 | obj.context().ifPresent(context -> context.onError(obj, error)); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 189 | 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 Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 235 | PortCriterion p; |
| 236 | EthCriterion e = null; |
| 237 | VlanIdCriterion v = null; |
| 238 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 239 | 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()) { |
gaurav | adc9004 | 2016-05-09 23:17:05 +0530 | [diff] [blame] | 252 | if (c.type() == Criterion.Type.ETH_DST || |
| 253 | c.type() == Criterion.Type.ETH_DST_MASKED) { |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 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 | |
gaurav | adc9004 | 2016-05-09 23:17:05 +0530 | [diff] [blame] | 270 | //Multicast MAC |
| 271 | if (e.mask() != null) { |
| 272 | selector.matchEthDstMasked(e.mac(), e.mask()); |
| 273 | } else { |
| 274 | selector.matchEthDst(e.mac()); |
| 275 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 276 | selector.matchVlanId(v.vlanId()); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 277 | selector.matchEthType(Ethernet.TYPE_IPV4); |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 278 | if (!v.vlanId().equals(VlanId.NONE)) { |
| 279 | treatment.popVlan(); |
| 280 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 281 | treatment.transition(FIB_TABLE); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 282 | 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(); |
| 290 | ops = ops.add(rule); |
| 291 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 292 | ops = install ? ops.add(rule) : ops.remove(rule); |
| 293 | // apply filtering flow rules |
| 294 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 295 | @Override |
| 296 | public void onSuccess(FlowRuleOperations ops) { |
| 297 | log.info("Applied filtering rules"); |
| 298 | pass(filt); |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public void onError(FlowRuleOperations ops) { |
| 303 | log.info("Failed to apply filtering rules"); |
| 304 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 305 | } |
| 306 | })); |
| 307 | } |
| 308 | |
| 309 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 310 | switch (fwd.flag()) { |
| 311 | case SPECIFIC: |
| 312 | return processSpecific(fwd); |
| 313 | case VERSATILE: |
| 314 | return processVersatile(fwd); |
| 315 | default: |
| 316 | fail(fwd, ObjectiveError.UNKNOWN); |
| 317 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 318 | } |
| 319 | return Collections.emptySet(); |
| 320 | } |
| 321 | |
| 322 | /** |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 323 | * SoftRouter has a single versatile table - the filter table. |
| 324 | * This table can be used to filter entries that reach the next table (FIB table). |
| 325 | * It can also be used to punt packets to the controller and/or bypass |
| 326 | * the FIB table to forward out of a port. |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 327 | * |
| 328 | * @param fwd The forwarding objective of type versatile |
| 329 | * @return A collection of flow rules meant to be delivered to the flowrule |
| 330 | * subsystem. May return empty collection in case of failures. |
| 331 | */ |
| 332 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 333 | log.debug("Received versatile fwd: to next:{}", fwd.nextId()); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 334 | Collection<FlowRule> flowrules = new ArrayList<>(); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 335 | if (fwd.nextId() == null && fwd.treatment() == null) { |
| 336 | log.error("Forwarding objective {} from {} must contain " |
| 337 | + "nextId or Treatment", fwd.selector(), fwd.appId()); |
| 338 | return Collections.emptySet(); |
| 339 | } |
| 340 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 341 | if (fwd.treatment() != null) { |
| 342 | fwd.treatment().immediate().forEach(ins -> ttBuilder.add(ins)); |
| 343 | } |
| 344 | //convert nextId to flow actions |
| 345 | if (fwd.nextId() != null) { |
| 346 | // only acceptable value is output to port |
| 347 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 348 | if (next == null) { |
| 349 | log.error("next-id {} does not exist in store", fwd.nextId()); |
| 350 | return Collections.emptySet(); |
| 351 | } |
| 352 | TrafficTreatment nt = appKryo.deserialize(next.data()); |
| 353 | if (nt == null) { |
| 354 | log.error("Error in deserializing next-id {}", fwd.nextId()); |
| 355 | return Collections.emptySet(); |
| 356 | } |
| 357 | for (Instruction ins : nt.allInstructions()) { |
| 358 | if (ins instanceof OutputInstruction) { |
| 359 | ttBuilder.add(ins); |
| 360 | } |
| 361 | } |
| 362 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 363 | |
| 364 | FlowRule rule = DefaultFlowRule.builder() |
| 365 | .withSelector(fwd.selector()) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 366 | .withTreatment(ttBuilder.build()) |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 367 | .makePermanent() |
| 368 | .forDevice(deviceId) |
| 369 | .fromApp(fwd.appId()) |
| 370 | .withPriority(fwd.priority()) |
| 371 | .build(); |
| 372 | |
| 373 | flowrules.add(rule); |
| 374 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 375 | return flowrules; |
| 376 | } |
| 377 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 378 | /** |
| 379 | * SoftRouter has a single specific table - the FIB Table. It emulates |
| 380 | * LPM matching of dstIP by using higher priority flows for longer prefixes. |
| 381 | * Flows are forwarded using flow-actions |
| 382 | * |
| 383 | * @param fwd The forwarding objective of type simple |
| 384 | * @return A collection of flow rules meant to be delivered to the flowrule |
| 385 | * subsystem. Typically the returned collection has a single flowrule. |
| 386 | * May return empty collection in case of failures. |
| 387 | * |
| 388 | */ |
| 389 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 390 | log.debug("Processing specific forwarding objective to next:{}", fwd.nextId()); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 391 | TrafficSelector selector = fwd.selector(); |
| 392 | EthTypeCriterion ethType = |
| 393 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 394 | // XXX currently supporting only the L3 unicast table |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 395 | if (ethType == null || ethType.ethType().toShort() != Ethernet.TYPE_IPV4) { |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 396 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 397 | return Collections.emptySet(); |
| 398 | } |
| 399 | |
Jonathan Hart | 29be97d | 2016-02-02 14:23:48 -0800 | [diff] [blame] | 400 | IpPrefix ipPrefix = ((IPCriterion) |
| 401 | selector.getCriterion(Criterion.Type.IPV4_DST)).ip(); |
| 402 | |
| 403 | TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder() |
| 404 | .matchEthType(Ethernet.TYPE_IPV4); |
| 405 | |
| 406 | if (ipPrefix.prefixLength() != 0) { |
| 407 | filteredSelector.matchIPDst(ipPrefix); |
| 408 | } |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 409 | |
| 410 | TrafficTreatment tt = null; |
| 411 | if (fwd.nextId() != null) { |
| 412 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 413 | if (next == null) { |
| 414 | log.error("next-id {} does not exist in store", fwd.nextId()); |
| 415 | return Collections.emptySet(); |
| 416 | } |
| 417 | tt = appKryo.deserialize(next.data()); |
| 418 | if (tt == null) { |
| 419 | log.error("Error in deserializing next-id {}", fwd.nextId()); |
| 420 | return Collections.emptySet(); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 425 | .fromApp(fwd.appId()) |
| 426 | .withPriority(fwd.priority()) |
| 427 | .forDevice(deviceId) |
Jonathan Hart | 29be97d | 2016-02-02 14:23:48 -0800 | [diff] [blame] | 428 | .withSelector(filteredSelector.build()); |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 429 | |
| 430 | if (tt != null) { |
| 431 | ruleBuilder.withTreatment(tt); |
| 432 | } |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 433 | |
| 434 | if (fwd.permanent()) { |
| 435 | ruleBuilder.makePermanent(); |
| 436 | } else { |
| 437 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 438 | } |
| 439 | |
| 440 | ruleBuilder.forTable(FIB_TABLE); |
| 441 | return Collections.singletonList(ruleBuilder.build()); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Next Objectives are stored as dummy groups for retrieval later |
| 446 | * when Forwarding Objectives reference the next objective id. At that point |
| 447 | * the dummy group is fetched from the distributed store and the enclosed |
| 448 | * treatment is applied as a flow rule action. |
| 449 | * |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 450 | * @param nextObj the next objective of type simple |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 451 | */ |
| 452 | private void processSimpleNextObjective(NextObjective nextObj) { |
| 453 | // Simple next objective has a single treatment (not a collection) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 454 | log.debug("Received nextObj {}", nextObj.id()); |
| 455 | // delay processing to emulate group creation |
| 456 | delay(50); |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 457 | TrafficTreatment treatment = nextObj.next().iterator().next(); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 458 | flowObjectiveStore.putNextGroup(nextObj.id(), |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 459 | new DummyGroup(treatment)); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 462 | private class DummyGroup implements NextGroup { |
| 463 | TrafficTreatment nextActions; |
| 464 | |
| 465 | public DummyGroup(TrafficTreatment next) { |
| 466 | this.nextActions = next; |
| 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public byte[] data() { |
| 471 | return appKryo.serialize(nextActions); |
| 472 | } |
| 473 | |
| 474 | } |
| 475 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 476 | @Override |
| 477 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 478 | // nextObjectives converted to flow-actions not groups |
| 479 | return Collections.emptyList(); |
| 480 | } |
| 481 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 482 | } |