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