Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Daniele Moro | 06aac70 | 2021-07-19 22:39:22 +0200 | [diff] [blame] | 189 | @Override |
| 190 | public void purgeAll(ApplicationId appId) { |
| 191 | flowRuleService.purgeFlowRules(deviceId, appId); |
| 192 | } |
| 193 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 194 | private void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 195 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | private void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 199 | obj.context().ifPresent(context -> context.onError(obj, error)); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 202 | private void initializePipeline() { |
| 203 | //Drop rules for both tables |
| 204 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 205 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 206 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 207 | |
| 208 | treatment.drop(); |
| 209 | |
| 210 | FlowRule rule = DefaultFlowRule.builder() |
| 211 | .forDevice(deviceId) |
| 212 | .withSelector(selector.build()) |
| 213 | .withTreatment(treatment.build()) |
| 214 | .withPriority(DROP_PRIORITY) |
| 215 | .fromApp(driverId) |
| 216 | .makePermanent() |
| 217 | .forTable(FILTER_TABLE) |
| 218 | .build(); |
| 219 | ops = ops.add(rule); |
| 220 | |
| 221 | rule = DefaultFlowRule.builder().forDevice(deviceId) |
| 222 | .withSelector(selector.build()) |
| 223 | .withTreatment(treatment.build()) |
| 224 | .withPriority(DROP_PRIORITY) |
| 225 | .fromApp(driverId) |
| 226 | .makePermanent() |
| 227 | .forTable(FIB_TABLE) |
| 228 | .build(); |
| 229 | ops = ops.add(rule); |
| 230 | |
| 231 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 232 | @Override |
| 233 | public void onSuccess(FlowRuleOperations ops) { |
| 234 | log.info("Provisioned drop rules in both tables"); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void onError(FlowRuleOperations ops) { |
| 239 | log.info("Failed to provision drop rules"); |
| 240 | } |
| 241 | })); |
| 242 | } |
| 243 | |
| 244 | private void processFilter(FilteringObjective filt, boolean install, |
| 245 | ApplicationId applicationId) { |
| 246 | // This driver only processes filtering criteria defined with switch |
| 247 | // ports as the key |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 248 | PortCriterion p; |
| 249 | EthCriterion e = null; |
| 250 | VlanIdCriterion v = null; |
| 251 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 252 | if (!filt.key().equals(Criteria.dummy()) && |
| 253 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 254 | p = (PortCriterion) filt.key(); |
| 255 | } else { |
| 256 | log.warn("No key defined in filtering objective from app: {}. Not" |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 257 | + "processing filtering objective", applicationId); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 258 | fail(filt, ObjectiveError.UNKNOWN); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | // convert filtering conditions for switch-intfs into flowrules |
| 263 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 264 | for (Criterion c : filt.conditions()) { |
gaurav | adc9004 | 2016-05-09 23:17:05 +0530 | [diff] [blame] | 265 | if (c.type() == Criterion.Type.ETH_DST || |
| 266 | c.type() == Criterion.Type.ETH_DST_MASKED) { |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 267 | e = (EthCriterion) c; |
| 268 | } else if (c.type() == Criterion.Type.VLAN_VID) { |
| 269 | v = (VlanIdCriterion) c; |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 270 | } else { |
| 271 | log.error("Unsupported filter {}", c); |
| 272 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 273 | return; |
| 274 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 275 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 278 | if (v == null || e == null) { |
| 279 | log.warn("Soft Router Pipeline ETH_DST and/or VLAN_ID not specified"); |
| 280 | fail(filt, ObjectiveError.BADPARAMS); |
| 281 | return; |
| 282 | } |
| 283 | |
Jonathan Hart | f8035d3 | 2016-06-16 16:23:26 -0700 | [diff] [blame] | 284 | log.debug("Modifying Port/VLAN/MAC filtering rules in filter table: {}/{}/{}", |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 285 | p.port(), v.vlanId(), e.mac()); |
| 286 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 287 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 288 | selector.matchInPort(p.port()); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 289 | |
gaurav | adc9004 | 2016-05-09 23:17:05 +0530 | [diff] [blame] | 290 | //Multicast MAC |
| 291 | if (e.mask() != null) { |
| 292 | selector.matchEthDstMasked(e.mac(), e.mask()); |
| 293 | } else { |
| 294 | selector.matchEthDst(e.mac()); |
| 295 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 296 | selector.matchVlanId(v.vlanId()); |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 297 | if (!v.vlanId().equals(VlanId.NONE)) { |
| 298 | treatment.popVlan(); |
| 299 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 300 | treatment.transition(FIB_TABLE); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 301 | FlowRule rule = DefaultFlowRule.builder() |
| 302 | .forDevice(deviceId) |
| 303 | .withSelector(selector.build()) |
| 304 | .withTreatment(treatment.build()) |
| 305 | .withPriority(DEFAULT_PRIORITY) |
| 306 | .fromApp(applicationId) |
| 307 | .makePermanent() |
| 308 | .forTable(FILTER_TABLE).build(); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 309 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 310 | ops = install ? ops.add(rule) : ops.remove(rule); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 311 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 312 | // apply filtering flow rules |
| 313 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 314 | @Override |
| 315 | public void onSuccess(FlowRuleOperations ops) { |
| 316 | log.info("Applied filtering rules"); |
| 317 | pass(filt); |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public void onError(FlowRuleOperations ops) { |
| 322 | log.info("Failed to apply filtering rules"); |
| 323 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 324 | } |
| 325 | })); |
| 326 | } |
| 327 | |
| 328 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 329 | switch (fwd.flag()) { |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 330 | case SPECIFIC: |
| 331 | return processSpecific(fwd); |
| 332 | case VERSATILE: |
| 333 | return processVersatile(fwd); |
| 334 | default: |
| 335 | fail(fwd, ObjectiveError.UNKNOWN); |
| 336 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 337 | } |
| 338 | return Collections.emptySet(); |
| 339 | } |
| 340 | |
| 341 | /** |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 342 | * SoftRouter has a single versatile table - the filter table. |
| 343 | * This table can be used to filter entries that reach the next table (FIB table). |
| 344 | * It can also be used to punt packets to the controller and/or bypass |
| 345 | * the FIB table to forward out of a port. |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 346 | * |
| 347 | * @param fwd The forwarding objective of type versatile |
| 348 | * @return A collection of flow rules meant to be delivered to the flowrule |
| 349 | * subsystem. May return empty collection in case of failures. |
| 350 | */ |
| 351 | private Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 352 | log.debug("Received versatile fwd: to next:{}", fwd.nextId()); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 353 | Collection<FlowRule> flowrules = new ArrayList<>(); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 354 | if (fwd.nextId() == null && fwd.treatment() == null) { |
| 355 | log.error("Forwarding objective {} from {} must contain " |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 356 | + "nextId or Treatment", fwd.selector(), fwd.appId()); |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 357 | return Collections.emptySet(); |
| 358 | } |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 359 | |
| 360 | int tableId = FILTER_TABLE; |
| 361 | |
| 362 | // A punt rule for IP traffic should be directed to the FIB table |
| 363 | // so that it only takes effect if the packet misses the FIB rules |
| 364 | if (fwd.treatment() != null && containsPunt(fwd.treatment()) && |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 365 | fwd.selector() != null && matchesIp(fwd.selector()) && |
| 366 | !matchesControlTraffic(fwd.selector())) { |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 367 | tableId = FIB_TABLE; |
| 368 | } |
| 369 | |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 370 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 371 | if (fwd.treatment() != null) { |
| 372 | fwd.treatment().immediate().forEach(ins -> ttBuilder.add(ins)); |
| 373 | } |
| 374 | //convert nextId to flow actions |
| 375 | if (fwd.nextId() != null) { |
| 376 | // only acceptable value is output to port |
| 377 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 378 | if (next == null) { |
| 379 | log.error("next-id {} does not exist in store", fwd.nextId()); |
| 380 | return Collections.emptySet(); |
| 381 | } |
| 382 | TrafficTreatment nt = appKryo.deserialize(next.data()); |
| 383 | if (nt == null) { |
| 384 | log.error("Error in deserializing next-id {}", fwd.nextId()); |
| 385 | return Collections.emptySet(); |
| 386 | } |
| 387 | for (Instruction ins : nt.allInstructions()) { |
| 388 | if (ins instanceof OutputInstruction) { |
| 389 | ttBuilder.add(ins); |
| 390 | } |
| 391 | } |
| 392 | } |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 393 | |
| 394 | FlowRule rule = DefaultFlowRule.builder() |
| 395 | .withSelector(fwd.selector()) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 396 | .withTreatment(ttBuilder.build()) |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 397 | .forTable(tableId) |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 398 | .makePermanent() |
| 399 | .forDevice(deviceId) |
| 400 | .fromApp(fwd.appId()) |
| 401 | .withPriority(fwd.priority()) |
| 402 | .build(); |
| 403 | |
| 404 | flowrules.add(rule); |
| 405 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 406 | return flowrules; |
| 407 | } |
| 408 | |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 409 | private boolean containsPunt(TrafficTreatment treatment) { |
| 410 | return treatment.immediate().stream() |
| 411 | .anyMatch(i -> i.type().equals(Instruction.Type.OUTPUT) |
| 412 | && ((OutputInstruction) i).port().equals(PortNumber.CONTROLLER)); |
| 413 | } |
| 414 | |
| 415 | private boolean matchesIp(TrafficSelector selector) { |
| 416 | EthTypeCriterion c = (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 417 | return c != null && (c.ethType().equals(EthType.EtherType.IPV4.ethType()) || |
| 418 | c.ethType().equals(EthType.EtherType.IPV6.ethType())); |
| 419 | } |
| 420 | |
| 421 | private boolean matchesControlTraffic(TrafficSelector selector) { |
| 422 | EthTypeCriterion c = (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 423 | if (c != null && c.ethType().equals(EthType.EtherType.ARP.ethType())) { |
| 424 | return true; |
| 425 | } else if (c != null && c.ethType().equals(EthType.EtherType.IPV6.ethType())) { |
| 426 | IPProtocolCriterion i = (IPProtocolCriterion) selector.getCriterion(Criterion.Type.IP_PROTO); |
| 427 | if (i != null && i.protocol() == PROTOCOL_ICMP6) { |
| 428 | Icmpv6TypeCriterion ic = (Icmpv6TypeCriterion) selector.getCriterion(Criterion.Type.ICMPV6_TYPE); |
| 429 | if (ic.icmpv6Type() != ECHO_REQUEST && ic.icmpv6Type() != ECHO_REPLY) { |
| 430 | return true; |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | return false; |
Jonathan Hart | 63eeac3 | 2016-06-20 15:55:16 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 437 | /** |
| 438 | * SoftRouter has a single specific table - the FIB Table. It emulates |
| 439 | * LPM matching of dstIP by using higher priority flows for longer prefixes. |
| 440 | * Flows are forwarded using flow-actions |
| 441 | * |
| 442 | * @param fwd The forwarding objective of type simple |
| 443 | * @return A collection of flow rules meant to be delivered to the flowrule |
| 444 | * subsystem. Typically the returned collection has a single flowrule. |
| 445 | * May return empty collection in case of failures. |
| 446 | * |
| 447 | */ |
| 448 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 449 | log.debug("Processing specific forwarding objective to next:{}", fwd.nextId()); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 450 | TrafficSelector selector = fwd.selector(); |
| 451 | EthTypeCriterion ethType = |
| 452 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
| 453 | // XXX currently supporting only the L3 unicast table |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 454 | if (ethType == null || (ethType.ethType().toShort() != TYPE_IPV4 |
| 455 | && ethType.ethType().toShort() != Ethernet.TYPE_IPV6)) { |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 456 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 457 | return Collections.emptySet(); |
| 458 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 459 | //We build the selector according the eth type. |
| 460 | IpPrefix ipPrefix; |
| 461 | TrafficSelector.Builder filteredSelector; |
| 462 | if (ethType.ethType().toShort() == TYPE_IPV4) { |
| 463 | ipPrefix = ((IPCriterion) |
| 464 | selector.getCriterion(Criterion.Type.IPV4_DST)).ip(); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 465 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 466 | filteredSelector = DefaultTrafficSelector.builder() |
| 467 | .matchEthType(TYPE_IPV4); |
| 468 | } else { |
| 469 | ipPrefix = ((IPCriterion) |
| 470 | selector.getCriterion(Criterion.Type.IPV6_DST)).ip(); |
Jonathan Hart | 29be97d | 2016-02-02 14:23:48 -0800 | [diff] [blame] | 471 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 472 | filteredSelector = DefaultTrafficSelector.builder() |
| 473 | .matchEthType(Ethernet.TYPE_IPV6); |
| 474 | } |
| 475 | // If the prefix is different from the default via. |
Jonathan Hart | 29be97d | 2016-02-02 14:23:48 -0800 | [diff] [blame] | 476 | if (ipPrefix.prefixLength() != 0) { |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 477 | if (ethType.ethType().toShort() == TYPE_IPV4) { |
| 478 | filteredSelector.matchIPDst(ipPrefix); |
| 479 | } else { |
| 480 | filteredSelector.matchIPv6Dst(ipPrefix); |
| 481 | } |
Jonathan Hart | 29be97d | 2016-02-02 14:23:48 -0800 | [diff] [blame] | 482 | } |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 483 | |
| 484 | TrafficTreatment tt = null; |
| 485 | if (fwd.nextId() != null) { |
| 486 | NextGroup next = flowObjectiveStore.getNextGroup(fwd.nextId()); |
| 487 | if (next == null) { |
| 488 | log.error("next-id {} does not exist in store", fwd.nextId()); |
| 489 | return Collections.emptySet(); |
| 490 | } |
| 491 | tt = appKryo.deserialize(next.data()); |
| 492 | if (tt == null) { |
| 493 | log.error("Error in deserializing next-id {}", fwd.nextId()); |
| 494 | return Collections.emptySet(); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 499 | .fromApp(fwd.appId()) |
| 500 | .withPriority(fwd.priority()) |
| 501 | .forDevice(deviceId) |
Jonathan Hart | 29be97d | 2016-02-02 14:23:48 -0800 | [diff] [blame] | 502 | .withSelector(filteredSelector.build()); |
Jonathan Hart | 3930f63 | 2015-10-19 12:12:51 -0700 | [diff] [blame] | 503 | |
| 504 | if (tt != null) { |
| 505 | ruleBuilder.withTreatment(tt); |
| 506 | } |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 507 | |
| 508 | if (fwd.permanent()) { |
| 509 | ruleBuilder.makePermanent(); |
| 510 | } else { |
| 511 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 512 | } |
| 513 | |
| 514 | ruleBuilder.forTable(FIB_TABLE); |
| 515 | return Collections.singletonList(ruleBuilder.build()); |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Next Objectives are stored as dummy groups for retrieval later |
| 520 | * when Forwarding Objectives reference the next objective id. At that point |
| 521 | * the dummy group is fetched from the distributed store and the enclosed |
| 522 | * treatment is applied as a flow rule action. |
| 523 | * |
alshabib | caf1ca2 | 2015-06-25 15:18:16 -0700 | [diff] [blame] | 524 | * @param nextObj the next objective of type simple |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 525 | */ |
| 526 | private void processSimpleNextObjective(NextObjective nextObj) { |
| 527 | // Simple next objective has a single treatment (not a collection) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 528 | log.debug("Received nextObj {}", nextObj.id()); |
| 529 | // delay processing to emulate group creation |
| 530 | delay(50); |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 531 | TrafficTreatment treatment = nextObj.next().iterator().next(); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 532 | flowObjectiveStore.putNextGroup(nextObj.id(), |
Saurav Das | 6c44a63 | 2015-05-30 22:05:22 -0700 | [diff] [blame] | 533 | new DummyGroup(treatment)); |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 536 | private class DummyGroup implements NextGroup { |
| 537 | TrafficTreatment nextActions; |
| 538 | |
| 539 | public DummyGroup(TrafficTreatment next) { |
| 540 | this.nextActions = next; |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public byte[] data() { |
| 545 | return appKryo.serialize(nextActions); |
| 546 | } |
| 547 | |
| 548 | } |
| 549 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 550 | @Override |
| 551 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 552 | // nextObjectives converted to flow-actions not groups |
| 553 | return Collections.emptyList(); |
| 554 | } |
| 555 | |
Saurav Das | decd7a6 | 2015-05-16 22:39:47 -0700 | [diff] [blame] | 556 | } |