Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip; |
| 17 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 19 | import java.util.HashSet; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 23 | import java.util.Set; |
| 24 | import java.util.concurrent.BlockingQueue; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 25 | import java.util.concurrent.ExecutorService; |
| 26 | import java.util.concurrent.Executors; |
| 27 | import java.util.concurrent.LinkedBlockingQueue; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 28 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 29 | import org.onlab.onos.core.ApplicationId; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 30 | import org.onlab.onos.net.ConnectPoint; |
| 31 | import org.onlab.onos.net.Host; |
| 32 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 33 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onlab.onos.net.flow.TrafficSelector; |
| 35 | import org.onlab.onos.net.flow.TrafficTreatment; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.host.HostEvent; |
| 37 | import org.onlab.onos.net.host.HostListener; |
| 38 | import org.onlab.onos.net.host.HostService; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 40 | import org.onlab.onos.sdnip.config.BgpPeer; |
| 41 | import org.onlab.onos.sdnip.config.Interface; |
| 42 | import org.onlab.onos.sdnip.config.SdnIpConfigService; |
| 43 | import org.onlab.packet.Ethernet; |
| 44 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 45 | import org.onlab.packet.Ip4Address; |
| 46 | import org.onlab.packet.Ip4Prefix; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 47 | import org.onlab.packet.MacAddress; |
| 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 51 | import com.google.common.collect.HashMultimap; |
| 52 | import com.google.common.collect.Multimaps; |
| 53 | import com.google.common.collect.SetMultimap; |
| 54 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 55 | import com.googlecode.concurrenttrees.common.KeyValuePair; |
| 56 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 57 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 58 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 59 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 60 | /** |
| 61 | * This class processes BGP route update, translates each update into a intent |
| 62 | * and submits the intent. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 63 | */ |
| 64 | public class Router implements RouteListener { |
| 65 | |
| 66 | private static final Logger log = LoggerFactory.getLogger(Router.class); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 67 | // For routes announced by local BGP daemon in SDN network, |
| 68 | // the next hop will be 0.0.0.0. |
| 69 | private static final Ip4Address LOCAL_NEXT_HOP = |
| 70 | Ip4Address.valueOf("0.0.0.0"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 71 | |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 72 | // Store all route updates in a radix tree. |
| 73 | // The key in this tree is the binary string of prefix of the route. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 74 | private InvertedRadixTree<RouteEntry> bgpRoutes; |
| 75 | |
| 76 | // Stores all incoming route updates in a queue. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 77 | private final BlockingQueue<RouteUpdate> routeUpdates; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 78 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 79 | // The Ip4Address is the next hop address of each route update. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 80 | private final SetMultimap<Ip4Address, RouteEntry> routesWaitingOnArp; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 81 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 82 | private final ApplicationId appId; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 83 | private final IntentSynchronizer intentSynchronizer; |
| 84 | private final HostService hostService; |
| 85 | private final SdnIpConfigService configService; |
| 86 | private final InterfaceService interfaceService; |
| 87 | private final ExecutorService bgpUpdatesExecutor; |
| 88 | private final HostListener hostListener; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Class constructor. |
| 92 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 93 | * @param appId the application ID |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 94 | * @param intentSynchronizer the intent synchronizer |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 95 | * @param configService the configuration service |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 96 | * @param interfaceService the interface service |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 97 | * @param hostService the host service |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 98 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 99 | public Router(ApplicationId appId, IntentSynchronizer intentSynchronizer, |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 100 | SdnIpConfigService configService, |
| 101 | InterfaceService interfaceService, |
| 102 | HostService hostService) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 103 | this.appId = appId; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 104 | this.intentSynchronizer = intentSynchronizer; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 105 | this.configService = configService; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 106 | this.interfaceService = interfaceService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 107 | this.hostService = hostService; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 108 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 109 | this.hostListener = new InternalHostListener(); |
| 110 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 111 | bgpRoutes = new ConcurrentInvertedRadixTree<>( |
| 112 | new DefaultByteArrayNodeFactory()); |
| 113 | routeUpdates = new LinkedBlockingQueue<>(); |
| 114 | routesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 115 | HashMultimap.<Ip4Address, RouteEntry>create()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 116 | |
| 117 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 118 | new ThreadFactoryBuilder() |
| 119 | .setNameFormat("sdnip-bgp-updates-%d").build()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 123 | * Starts the router. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 124 | */ |
| 125 | public void start() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 126 | this.hostService.addListener(hostListener); |
| 127 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 128 | bgpUpdatesExecutor.execute(new Runnable() { |
| 129 | @Override |
| 130 | public void run() { |
| 131 | doUpdatesThread(); |
| 132 | } |
| 133 | }); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 136 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 137 | * Stops the router. |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 138 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 139 | public void stop() { |
| 140 | this.hostService.removeListener(hostListener); |
| 141 | |
| 142 | // Stop the thread(s) |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 143 | bgpUpdatesExecutor.shutdownNow(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 144 | |
| 145 | synchronized (this) { |
| 146 | // Cleanup all local state |
| 147 | bgpRoutes = new ConcurrentInvertedRadixTree<>( |
| 148 | new DefaultByteArrayNodeFactory()); |
| 149 | routeUpdates.clear(); |
| 150 | routesWaitingOnArp.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 151 | } |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public void update(RouteUpdate routeUpdate) { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 156 | log.debug("Received new route update: {}", routeUpdate); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 157 | |
| 158 | try { |
| 159 | routeUpdates.put(routeUpdate); |
| 160 | } catch (InterruptedException e) { |
| 161 | log.debug("Interrupted while putting on routeUpdates queue", e); |
| 162 | Thread.currentThread().interrupt(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 167 | * Thread for handling route updates. |
| 168 | */ |
| 169 | private void doUpdatesThread() { |
| 170 | boolean interrupted = false; |
| 171 | try { |
| 172 | while (!interrupted) { |
| 173 | try { |
| 174 | RouteUpdate update = routeUpdates.take(); |
| 175 | switch (update.type()) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 176 | case UPDATE: |
| 177 | processRouteAdd(update.routeEntry()); |
| 178 | break; |
| 179 | case DELETE: |
| 180 | processRouteDelete(update.routeEntry()); |
| 181 | break; |
| 182 | default: |
| 183 | log.error("Unknown update Type: {}", update.type()); |
| 184 | break; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 185 | } |
| 186 | } catch (InterruptedException e) { |
| 187 | log.debug("Interrupted while taking from updates queue", e); |
| 188 | interrupted = true; |
| 189 | } catch (Exception e) { |
| 190 | log.debug("exception", e); |
| 191 | } |
| 192 | } |
| 193 | } finally { |
| 194 | if (interrupted) { |
| 195 | Thread.currentThread().interrupt(); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 201 | * Processes adding a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 202 | * <p> |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 203 | * Put new route entry into the radix tree. If there was an existing |
| 204 | * next hop for this prefix, but the next hop was different, then execute |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 205 | * deleting old route entry. If the next hop is the SDN domain, we do not |
| 206 | * handle it at the moment. Otherwise, execute adding a route. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 207 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 208 | * |
| 209 | * @param routeEntry the route entry to add |
| 210 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 211 | void processRouteAdd(RouteEntry routeEntry) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 212 | synchronized (this) { |
| 213 | log.debug("Processing route add: {}", routeEntry); |
| 214 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 215 | Ip4Prefix prefix = routeEntry.prefix(); |
| 216 | Ip4Address nextHop = null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 217 | RouteEntry foundRouteEntry = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 218 | bgpRoutes.put(RouteEntry.createBinaryString(prefix), |
| 219 | routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 220 | if (foundRouteEntry != null) { |
| 221 | nextHop = foundRouteEntry.nextHop(); |
| 222 | } |
| 223 | |
| 224 | if (nextHop != null && !nextHop.equals(routeEntry.nextHop())) { |
| 225 | // There was an existing nexthop for this prefix. This update |
| 226 | // supersedes that, so we need to remove the old flows for this |
| 227 | // prefix from the switches |
| 228 | executeRouteDelete(routeEntry); |
| 229 | } |
| 230 | if (nextHop != null && nextHop.equals(routeEntry.nextHop())) { |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (routeEntry.nextHop().equals(LOCAL_NEXT_HOP)) { |
| 235 | // Route originated by SDN domain |
| 236 | // We don't handle these at the moment |
| 237 | log.debug("Own route {} to {}", |
| 238 | routeEntry.prefix(), routeEntry.nextHop()); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | executeRouteAdd(routeEntry); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Executes adding a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 248 | * <p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 249 | * Find out the egress Interface and MAC address of next hop router for |
| 250 | * this route entry. If the MAC address can not be found in ARP cache, |
| 251 | * then this prefix will be put in routesWaitingOnArp queue. Otherwise, |
| 252 | * new route intent will be created and installed. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 253 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 254 | * |
| 255 | * @param routeEntry the route entry to add |
| 256 | */ |
| 257 | private void executeRouteAdd(RouteEntry routeEntry) { |
| 258 | log.debug("Executing route add: {}", routeEntry); |
| 259 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 260 | // Monitor the IP address so we'll get notified of updates to the MAC |
| 261 | // address. |
| 262 | hostService.startMonitoringIp(routeEntry.nextHop()); |
| 263 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 264 | // See if we know the MAC address of the next hop |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 265 | MacAddress nextHopMacAddress = null; |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 266 | Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 267 | if (!hosts.isEmpty()) { |
| 268 | // TODO how to handle if multiple hosts are returned? |
| 269 | nextHopMacAddress = hosts.iterator().next().mac(); |
| 270 | } |
| 271 | |
| 272 | if (nextHopMacAddress == null) { |
| 273 | routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
| 277 | addRouteIntentToNextHop(routeEntry.prefix(), |
| 278 | routeEntry.nextHop(), |
| 279 | nextHopMacAddress); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Adds a route intent given a prefix and a next hop IP address. This |
| 284 | * method will find the egress interface for the intent. |
| 285 | * |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 286 | * @param prefix IP prefix of the route to add |
| 287 | * @param nextHopIpAddress IP address of the next hop |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 288 | * @param nextHopMacAddress MAC address of the next hop |
| 289 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 290 | private void addRouteIntentToNextHop(Ip4Prefix prefix, |
| 291 | Ip4Address nextHopIpAddress, |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 292 | MacAddress nextHopMacAddress) { |
| 293 | |
| 294 | // Find the attachment point (egress interface) of the next hop |
| 295 | Interface egressInterface; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 296 | if (configService.getBgpPeers().containsKey(nextHopIpAddress)) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 297 | // Route to a peer |
| 298 | log.debug("Route to peer {}", nextHopIpAddress); |
| 299 | BgpPeer peer = |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 300 | configService.getBgpPeers().get(nextHopIpAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 301 | egressInterface = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 302 | interfaceService.getInterface(peer.connectPoint()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 303 | } else { |
| 304 | // Route to non-peer |
| 305 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 306 | egressInterface = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 307 | interfaceService.getMatchingInterface(nextHopIpAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 308 | if (egressInterface == null) { |
| 309 | log.warn("No outgoing interface found for {}", |
| 310 | nextHopIpAddress); |
| 311 | return; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | doAddRouteIntent(prefix, egressInterface, nextHopMacAddress); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Installs a route intent for a prefix. |
| 320 | * <p/> |
| 321 | * Intent will match dst IP prefix and rewrite dst MAC address at all other |
| 322 | * border switches, then forward packets according to dst MAC address. |
| 323 | * |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 324 | * @param prefix IP prefix from route |
| 325 | * @param egressInterface egress Interface connected to next hop router |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 326 | * @param nextHopMacAddress MAC address of next hop router |
| 327 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 328 | private void doAddRouteIntent(Ip4Prefix prefix, Interface egressInterface, |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 329 | MacAddress nextHopMacAddress) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 330 | log.debug("Adding intent for prefix {}, next hop mac {}", |
| 331 | prefix, nextHopMacAddress); |
| 332 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 333 | Set<ConnectPoint> ingressPorts = new HashSet<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 334 | ConnectPoint egressPort = egressInterface.connectPoint(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 335 | |
| 336 | for (Interface intf : interfaceService.getInterfaces()) { |
Jonathan Hart | 2e3eef3 | 2014-11-12 11:05:40 -0800 | [diff] [blame] | 337 | if (!intf.connectPoint().equals(egressInterface.connectPoint())) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 338 | ConnectPoint srcPort = intf.connectPoint(); |
| 339 | ingressPorts.add(srcPort); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Match the destination IP prefix at the first hop |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 344 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 345 | .matchEthType(Ethernet.TYPE_IPV4) |
| 346 | .matchIPDst(prefix) |
| 347 | .build(); |
| 348 | |
| 349 | // Rewrite the destination MAC address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 350 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 351 | .setEthDst(nextHopMacAddress) |
| 352 | .build(); |
| 353 | |
| 354 | MultiPointToSinglePointIntent intent = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 355 | new MultiPointToSinglePointIntent(appId, selector, treatment, |
| 356 | ingressPorts, egressPort); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 357 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 358 | intentSynchronizer.submitRouteIntent(prefix, intent); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Executes deleting a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 363 | * <p> |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 364 | * Removes prefix from radix tree, and if successful, then try to delete |
| 365 | * the related intent. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 366 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 367 | * |
| 368 | * @param routeEntry the route entry to delete |
| 369 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 370 | void processRouteDelete(RouteEntry routeEntry) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 371 | synchronized (this) { |
| 372 | log.debug("Processing route delete: {}", routeEntry); |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 373 | Ip4Prefix prefix = routeEntry.prefix(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 374 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 375 | if (bgpRoutes.remove(RouteEntry.createBinaryString(prefix))) { |
| 376 | // |
| 377 | // Only delete flows if an entry was actually removed from the |
| 378 | // tree. If no entry was removed, the <prefix, nexthop> wasn't |
| 379 | // there so it's probably already been removed and we don't |
| 380 | // need to do anything. |
| 381 | // |
| 382 | executeRouteDelete(routeEntry); |
| 383 | } |
| 384 | |
| 385 | routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry); |
| 386 | // TODO cancel the request in the ARP manager as well |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Executed deleting a route entry. |
| 392 | * |
| 393 | * @param routeEntry the route entry to delete |
| 394 | */ |
| 395 | private void executeRouteDelete(RouteEntry routeEntry) { |
| 396 | log.debug("Executing route delete: {}", routeEntry); |
| 397 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 398 | intentSynchronizer.withdrawRouteIntent(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 402 | * Signals the Router that the MAC to IP mapping has potentially been |
| 403 | * updated. This has the effect of updating the MAC address for any |
| 404 | * installed prefixes if it has changed, as well as installing any pending |
| 405 | * prefixes that were waiting for MAC resolution. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 406 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 407 | * @param ipAddress the IP address that an event was received for |
| 408 | * @param macAddress the most recently known MAC address for the IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 409 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 410 | private void updateMac(Ip4Address ipAddress, MacAddress macAddress) { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 411 | log.debug("Received updated MAC info: {} => {}", ipAddress, macAddress); |
| 412 | |
| 413 | // TODO here we should check whether the next hop for any of our |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 414 | // installed prefixes has changed, not just prefixes pending |
| 415 | // installation. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 416 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 417 | // We synchronize on this to prevent changes to the radix tree |
| 418 | // while we're pushing intents. If the tree changes, the |
| 419 | // tree and intents could get out of sync. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 420 | synchronized (this) { |
| 421 | |
| 422 | Set<RouteEntry> routesToPush = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 423 | routesWaitingOnArp.removeAll(ipAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 424 | |
| 425 | for (RouteEntry routeEntry : routesToPush) { |
| 426 | // These will always be adds |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 427 | Ip4Prefix prefix = routeEntry.prefix(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 428 | String binaryString = RouteEntry.createBinaryString(prefix); |
| 429 | RouteEntry foundRouteEntry = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 430 | bgpRoutes.getValueForExactKey(binaryString); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 431 | if (foundRouteEntry != null && |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 432 | foundRouteEntry.nextHop().equals(routeEntry.nextHop())) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 433 | // We only push prefix flows if the prefix is still in the |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 434 | // radix tree and the next hop is the same as our |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 435 | // update. |
| 436 | // The prefix could have been removed while we were waiting |
| 437 | // for the ARP, or the next hop could have changed. |
| 438 | addRouteIntentToNextHop(prefix, ipAddress, macAddress); |
| 439 | } else { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 440 | log.debug("{} has been revoked before the MAC was resolved", |
| 441 | routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Gets the SDN-IP routes. |
| 449 | * |
| 450 | * @return the SDN-IP routes |
| 451 | */ |
| 452 | public Collection<RouteEntry> getRoutes() { |
| 453 | Iterator<KeyValuePair<RouteEntry>> it = |
| 454 | bgpRoutes.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 455 | |
| 456 | List<RouteEntry> routes = new LinkedList<>(); |
| 457 | |
| 458 | while (it.hasNext()) { |
| 459 | KeyValuePair<RouteEntry> entry = it.next(); |
| 460 | routes.add(entry.getValue()); |
| 461 | } |
| 462 | |
| 463 | return routes; |
| 464 | } |
| 465 | |
| 466 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 467 | * Listener for host events. |
| 468 | */ |
| 469 | class InternalHostListener implements HostListener { |
| 470 | @Override |
| 471 | public void event(HostEvent event) { |
| 472 | if (event.type() == HostEvent.Type.HOST_ADDED || |
| 473 | event.type() == HostEvent.Type.HOST_UPDATED) { |
| 474 | Host host = event.subject(); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 475 | for (IpAddress ip : host.ipAddresses()) { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 476 | Ip4Address ip4Address = ip.getIp4Address(); |
| 477 | if (ip4Address == null) { |
| 478 | // TODO: For now we support only IPv4 |
| 479 | continue; |
| 480 | } |
| 481 | updateMac(ip4Address, host.mac()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |