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