Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 17 | package org.onosproject.routing.impl; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ConcurrentHashMultiset; |
| 20 | import com.google.common.collect.HashMultimap; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Multimap; |
| 23 | import com.google.common.collect.Multiset; |
| 24 | import org.apache.felix.scr.annotations.Activate; |
| 25 | import org.apache.felix.scr.annotations.Component; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
| 27 | import org.apache.felix.scr.annotations.Reference; |
| 28 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 29 | import org.onlab.packet.Ethernet; |
| 30 | import org.onlab.packet.IpAddress; |
| 31 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 32 | import org.onlab.packet.VlanId; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
| 35 | import org.onosproject.incubator.net.intf.Interface; |
| 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 37 | import org.onosproject.net.DeviceId; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 38 | import org.onosproject.net.config.NetworkConfigEvent; |
| 39 | import org.onosproject.net.config.NetworkConfigListener; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 40 | import org.onosproject.net.config.NetworkConfigService; |
| 41 | import org.onosproject.net.device.DeviceEvent; |
| 42 | import org.onosproject.net.device.DeviceListener; |
| 43 | import org.onosproject.net.device.DeviceService; |
| 44 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 45 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 46 | import org.onosproject.net.flow.TrafficSelector; |
| 47 | import org.onosproject.net.flow.TrafficTreatment; |
| 48 | import org.onosproject.net.flow.criteria.Criteria; |
| 49 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 50 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 51 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 52 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 53 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 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.ObjectiveContext; |
| 58 | import org.onosproject.net.flowobjective.ObjectiveError; |
| 59 | import org.onosproject.routing.FibEntry; |
| 60 | import org.onosproject.routing.FibListener; |
| 61 | import org.onosproject.routing.FibUpdate; |
| 62 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 63 | import org.onosproject.routing.config.RouterConfig; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 64 | import org.slf4j.Logger; |
| 65 | import org.slf4j.LoggerFactory; |
| 66 | |
| 67 | import java.util.Collection; |
| 68 | import java.util.HashMap; |
| 69 | import java.util.Map; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 70 | import java.util.Set; |
| 71 | |
| 72 | /** |
| 73 | * Programs routes to a single OpenFlow switch. |
| 74 | */ |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 75 | @Component(immediate = true, enabled = false) |
| 76 | public class SingleSwitchFibInstaller { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 77 | |
| 78 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 79 | |
| 80 | private static final int PRIORITY_OFFSET = 100; |
| 81 | private static final int PRIORITY_MULTIPLIER = 5; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected CoreService coreService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected RoutingService routingService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected InterfaceService interfaceService; |
| 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 93 | protected NetworkConfigService networkConfigService; |
| 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected FlowObjectiveService flowObjectiveService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected DeviceService deviceService; |
| 100 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 101 | private InternalDeviceListener deviceListener; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 102 | |
| 103 | // Device id of data-plane switch - should be learned from config |
| 104 | private DeviceId deviceId; |
| 105 | |
| 106 | private ApplicationId appId; |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 107 | private ApplicationId routerAppId; |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 108 | |
| 109 | // Reference count for how many times a next hop is used by a route |
| 110 | private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); |
| 111 | |
| 112 | // Mapping from prefix to its current next hop |
| 113 | private final Map<IpPrefix, IpAddress> prefixToNextHop = Maps.newHashMap(); |
| 114 | |
| 115 | // Mapping from next hop IP to next hop object containing group info |
| 116 | private final Map<IpAddress, Integer> nextHops = Maps.newHashMap(); |
| 117 | |
| 118 | // Stores FIB updates that are waiting for groups to be set up |
| 119 | private final Multimap<NextHopGroupKey, FibEntry> pendingUpdates = HashMultimap.create(); |
| 120 | |
| 121 | |
| 122 | @Activate |
| 123 | protected void activate() { |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 124 | // TODO why are there two of the same app ID? |
| 125 | routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 126 | |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 127 | appId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 128 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 129 | deviceListener = new InternalDeviceListener(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 130 | deviceService.addListener(deviceListener); |
| 131 | |
| 132 | routingService.addFibListener(new InternalFibListener()); |
| 133 | routingService.start(); |
| 134 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 135 | updateConfig(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 136 | |
| 137 | log.info("Started"); |
| 138 | } |
| 139 | |
| 140 | @Deactivate |
| 141 | protected void deactivate() { |
| 142 | routingService.stop(); |
| 143 | |
| 144 | deviceService.removeListener(deviceListener); |
| 145 | |
| 146 | //processIntfFilters(false, configService.getInterfaces()); //TODO necessary? |
| 147 | |
| 148 | log.info("Stopped"); |
| 149 | } |
| 150 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 151 | private void updateConfig() { |
| 152 | RouterConfig routerConfig = |
| 153 | networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 154 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 155 | if (routerConfig == null) { |
| 156 | log.info("Router config not available"); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 160 | deviceId = routerConfig.getControlPlaneConnectPoint().deviceId(); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 161 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 162 | log.info("Router device ID is {}", deviceId); |
| 163 | |
| 164 | updateDevice(); |
| 165 | } |
| 166 | |
| 167 | private void updateDevice() { |
| 168 | if (deviceId != null && deviceService.isAvailable(deviceId)) { |
| 169 | processIntfFilters(true, interfaceService.getInterfaces()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 170 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | private void updateFibEntry(Collection<FibUpdate> updates) { |
| 174 | Map<FibEntry, Integer> toInstall = new HashMap<>(updates.size()); |
| 175 | |
| 176 | for (FibUpdate update : updates) { |
| 177 | FibEntry entry = update.entry(); |
| 178 | |
| 179 | addNextHop(entry); |
| 180 | |
| 181 | Integer nextId; |
| 182 | synchronized (pendingUpdates) { |
| 183 | nextId = nextHops.get(entry.nextHopIp()); |
| 184 | } |
| 185 | |
| 186 | toInstall.put(update.entry(), nextId); |
| 187 | } |
| 188 | |
| 189 | installFlows(toInstall); |
| 190 | } |
| 191 | |
| 192 | private void installFlows(Map<FibEntry, Integer> entriesToInstall) { |
| 193 | |
| 194 | for (Map.Entry<FibEntry, Integer> entry : entriesToInstall.entrySet()) { |
| 195 | FibEntry fibEntry = entry.getKey(); |
| 196 | Integer nextId = entry.getValue(); |
| 197 | |
| 198 | flowObjectiveService.forward(deviceId, |
| 199 | generateRibForwardingObj(fibEntry.prefix(), nextId).add()); |
| 200 | log.trace("Sending forwarding objective {} -> nextId:{}", fibEntry, nextId); |
| 201 | } |
| 202 | |
| 203 | } |
| 204 | |
| 205 | private synchronized void deleteFibEntry(Collection<FibUpdate> withdraws) { |
| 206 | |
| 207 | for (FibUpdate update : withdraws) { |
| 208 | FibEntry entry = update.entry(); |
| 209 | //Integer nextId = nextHops.get(entry.nextHopIp()); |
| 210 | |
| 211 | /* Group group = deleteNextHop(entry.prefix()); |
| 212 | if (group == null) { |
| 213 | log.warn("Group not found when deleting {}", entry); |
| 214 | return; |
| 215 | }*/ |
| 216 | |
| 217 | flowObjectiveService.forward(deviceId, |
| 218 | generateRibForwardingObj(entry.prefix(), null).remove()); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | |
| 224 | private ForwardingObjective.Builder generateRibForwardingObj(IpPrefix prefix, |
| 225 | Integer nextId) { |
| 226 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 227 | .matchEthType(Ethernet.TYPE_IPV4) |
| 228 | .matchIPDst(prefix) |
| 229 | .build(); |
| 230 | |
| 231 | int priority = prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET; |
| 232 | |
| 233 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
| 234 | .fromApp(appId) |
| 235 | .makePermanent() |
| 236 | .withSelector(selector) |
| 237 | .withPriority(priority) |
| 238 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 239 | |
| 240 | if (nextId == null) { |
| 241 | // Route withdraws are not specified with next hops. Generating |
| 242 | // dummy treatment as there is no equivalent nextId info. |
| 243 | fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build()); |
| 244 | } else { |
| 245 | fwdBuilder.nextStep(nextId); |
| 246 | } |
| 247 | return fwdBuilder; |
| 248 | } |
| 249 | |
| 250 | private synchronized void addNextHop(FibEntry entry) { |
| 251 | prefixToNextHop.put(entry.prefix(), entry.nextHopIp()); |
| 252 | if (nextHopsCount.count(entry.nextHopIp()) == 0) { |
| 253 | // There was no next hop in the multiset |
| 254 | |
| 255 | Interface egressIntf = interfaceService.getMatchingInterface(entry.nextHopIp()); |
| 256 | if (egressIntf == null) { |
| 257 | log.warn("no egress interface found for {}", entry); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | NextHopGroupKey groupKey = new NextHopGroupKey(entry.nextHopIp()); |
| 262 | |
| 263 | NextHop nextHop = new NextHop(entry.nextHopIp(), entry.nextHopMac(), groupKey); |
| 264 | |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 265 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 266 | .setEthSrc(egressIntf.mac()) |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 267 | .setEthDst(nextHop.mac()); |
| 268 | |
| 269 | if (!egressIntf.vlan().equals(VlanId.NONE)) { |
| 270 | treatment.pushVlan() |
| 271 | .setVlanId(egressIntf.vlan()) |
| 272 | .setVlanPcp((byte) 0); |
| 273 | } |
| 274 | |
| 275 | treatment.setOutput(egressIntf.connectPoint().port()); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 276 | |
| 277 | int nextId = flowObjectiveService.allocateNextId(); |
| 278 | |
| 279 | NextObjective nextObjective = DefaultNextObjective.builder() |
| 280 | .withId(nextId) |
Jonathan Hart | ca47cd7 | 2015-12-13 12:31:09 -0800 | [diff] [blame] | 281 | .addTreatment(treatment.build()) |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 282 | .withType(NextObjective.Type.SIMPLE) |
| 283 | .fromApp(appId) |
| 284 | .add(); // TODO add callbacks |
| 285 | |
| 286 | flowObjectiveService.next(deviceId, nextObjective); |
| 287 | |
| 288 | nextHops.put(nextHop.ip(), nextId); |
| 289 | |
| 290 | } |
| 291 | |
| 292 | nextHopsCount.add(entry.nextHopIp()); |
| 293 | } |
| 294 | |
| 295 | /*private synchronized Group deleteNextHop(IpPrefix prefix) { |
| 296 | IpAddress nextHopIp = prefixToNextHop.remove(prefix); |
| 297 | NextHop nextHop = nextHops.get(nextHopIp); |
| 298 | if (nextHop == null) { |
| 299 | log.warn("No next hop found when removing prefix {}", prefix); |
| 300 | return null; |
| 301 | } |
| 302 | |
| 303 | Group group = groupService.getGroup(deviceId, |
| 304 | new DefaultGroupKey(appKryo. |
| 305 | serialize(nextHop.group()))); |
| 306 | |
| 307 | // FIXME disabling group deletes for now until we verify the logic is OK |
| 308 | if (nextHopsCount.remove(nextHopIp, 1) <= 1) { |
| 309 | // There was one or less next hops, so there are now none |
| 310 | |
| 311 | log.debug("removing group for next hop {}", nextHop); |
| 312 | |
| 313 | nextHops.remove(nextHopIp); |
| 314 | |
| 315 | groupService.removeGroup(deviceId, |
| 316 | new DefaultGroupKey(appKryo.build().serialize(nextHop.group())), |
| 317 | appId); |
| 318 | } |
| 319 | |
| 320 | return group; |
| 321 | }*/ |
| 322 | |
| 323 | private void processIntfFilters(boolean install, Set<Interface> intfs) { |
| 324 | log.info("Processing {} router interfaces", intfs.size()); |
| 325 | for (Interface intf : intfs) { |
| 326 | if (!intf.connectPoint().deviceId().equals(deviceId)) { |
| 327 | // Ignore interfaces if they are not on the router switch |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | FilteringObjective.Builder fob = DefaultFilteringObjective.builder(); |
| 332 | fob.withKey(Criteria.matchInPort(intf.connectPoint().port())) |
| 333 | .addCondition(Criteria.matchEthDst(intf.mac())) |
| 334 | .addCondition(Criteria.matchVlanId(intf.vlan())); |
Jonathan Hart | 6344f57 | 2015-12-15 08:26:25 -0800 | [diff] [blame] | 335 | |
| 336 | fob.withPriority(PRIORITY_OFFSET); |
| 337 | |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 338 | fob.permit().fromApp(appId); |
| 339 | flowObjectiveService.filter( |
| 340 | deviceId, |
| 341 | fob.add(new ObjectiveContext() { |
| 342 | @Override |
| 343 | public void onSuccess(Objective objective) { |
| 344 | log.info("Successfully installed interface based " |
| 345 | + "filtering objectives for intf {}", intf); |
| 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void onError(Objective objective, |
| 350 | ObjectiveError error) { |
| 351 | log.error("Failed to install interface filters for intf {}: {}", |
| 352 | intf, error); |
| 353 | // TODO something more than just logging |
| 354 | } |
| 355 | })); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | private class InternalFibListener implements FibListener { |
| 360 | |
| 361 | @Override |
| 362 | public void update(Collection<FibUpdate> updates, |
| 363 | Collection<FibUpdate> withdraws) { |
Jonathan Hart | c22e847 | 2015-11-17 18:25:45 -0800 | [diff] [blame] | 364 | SingleSwitchFibInstaller.this.deleteFibEntry(withdraws); |
| 365 | SingleSwitchFibInstaller.this.updateFibEntry(updates); |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 369 | /** |
| 370 | * Listener for device events used to trigger driver setup when a device is |
| 371 | * (re)detected. |
| 372 | */ |
| 373 | private class InternalDeviceListener implements DeviceListener { |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 374 | @Override |
| 375 | public void event(DeviceEvent event) { |
| 376 | switch (event.type()) { |
| 377 | case DEVICE_ADDED: |
| 378 | case DEVICE_AVAILABILITY_CHANGED: |
| 379 | if (deviceService.isAvailable(event.subject().id())) { |
| 380 | log.info("Device connected {}", event.subject().id()); |
| 381 | if (event.subject().id().equals(deviceId)) { |
| 382 | processIntfFilters(true, interfaceService.getInterfaces()); |
| 383 | } |
| 384 | } |
| 385 | break; |
| 386 | // TODO other cases |
| 387 | case DEVICE_UPDATED: |
| 388 | case DEVICE_REMOVED: |
| 389 | case DEVICE_SUSPENDED: |
| 390 | case PORT_ADDED: |
| 391 | case PORT_UPDATED: |
| 392 | case PORT_REMOVED: |
| 393 | default: |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | } |
Jonathan Hart | b3fa42c | 2016-01-13 09:50:43 -0800 | [diff] [blame^] | 398 | |
| 399 | /** |
| 400 | * Listener for network config events. |
| 401 | */ |
| 402 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 403 | @Override |
| 404 | public void event(NetworkConfigEvent event) { |
| 405 | if (event.subject().equals(RoutingService.ROUTER_CONFIG_CLASS)) { |
| 406 | switch (event.type()) { |
| 407 | case CONFIG_ADDED: |
| 408 | case CONFIG_UPDATED: |
| 409 | updateConfig(); |
| 410 | break; |
| 411 | case CONFIG_REGISTERED: |
| 412 | case CONFIG_UNREGISTERED: |
| 413 | case CONFIG_REMOVED: |
| 414 | default: |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
Jonathan Hart | df20709 | 2015-12-10 11:19:25 -0800 | [diff] [blame] | 420 | } |