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 | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 16 | package org.onosproject.routing; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.Multimaps; |
| 20 | import com.google.common.collect.SetMultimap; |
| 21 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 22 | import com.googlecode.concurrenttrees.common.KeyValuePair; |
| 23 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 24 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 25 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 32 | import org.onlab.packet.Ip4Address; |
| 33 | import org.onlab.packet.IpAddress; |
| 34 | import org.onlab.packet.IpPrefix; |
| 35 | import org.onlab.packet.MacAddress; |
| 36 | import org.onosproject.net.Host; |
| 37 | import org.onosproject.net.host.HostEvent; |
| 38 | import org.onosproject.net.host.HostListener; |
| 39 | import org.onosproject.net.host.HostService; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 40 | import org.onosproject.routingapi.BgpService; |
| 41 | import org.onosproject.routingapi.FibEntry; |
| 42 | import org.onosproject.routingapi.FibListener; |
| 43 | import org.onosproject.routingapi.FibUpdate; |
| 44 | import org.onosproject.routingapi.RouteEntry; |
| 45 | import org.onosproject.routingapi.RouteListener; |
| 46 | import org.onosproject.routingapi.RouteUpdate; |
| 47 | import org.onosproject.routingapi.RoutingService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
| 50 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 51 | import java.util.Collection; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 52 | import java.util.Collections; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 53 | import java.util.Iterator; |
| 54 | import java.util.LinkedList; |
| 55 | import java.util.List; |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 56 | import java.util.Map; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 57 | import java.util.Set; |
| 58 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 59 | import java.util.concurrent.ConcurrentHashMap; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 60 | import java.util.concurrent.ExecutorService; |
| 61 | import java.util.concurrent.Executors; |
| 62 | import java.util.concurrent.LinkedBlockingQueue; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 63 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 64 | import static com.google.common.base.Preconditions.checkNotNull; |
| 65 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 66 | /** |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 67 | * This class processes route updates and maintains a Routing Information Base |
| 68 | * (RIB). After route updates have been processed and next hops have been |
| 69 | * resolved, FIB updates are sent to any listening FIB components. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 70 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 71 | @Component(immediate = true) |
| 72 | @Service |
| 73 | public class Router implements RoutingService { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 74 | |
| 75 | private static final Logger log = LoggerFactory.getLogger(Router.class); |
| 76 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 77 | // Route entries are stored in a radix tree. |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 78 | // The key in this tree is the binary string of prefix of the route. |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 79 | private InvertedRadixTree<RouteEntry> ribTable4; |
| 80 | private InvertedRadixTree<RouteEntry> ribTable6; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 81 | |
| 82 | // Stores all incoming route updates in a queue. |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 83 | private final BlockingQueue<Collection<RouteUpdate>> routeUpdatesQueue |
| 84 | = new LinkedBlockingQueue<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 85 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 86 | // Next-hop IP address to route entry mapping for next hops pending MAC resolution |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 87 | private SetMultimap<IpAddress, RouteEntry> routesWaitingOnArp; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 88 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 89 | // The IPv4 address to MAC address mapping |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 90 | private final Map<IpAddress, MacAddress> ip2Mac = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 91 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 92 | private FibListener fibComponent; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 93 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected HostService hostService; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 96 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected BgpService bgpService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 99 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 100 | private ExecutorService bgpUpdatesExecutor; |
| 101 | private final HostListener hostListener = new InternalHostListener(); |
| 102 | |
| 103 | @Activate |
| 104 | public void activate() { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 105 | ribTable4 = new ConcurrentInvertedRadixTree<>( |
| 106 | new DefaultByteArrayNodeFactory()); |
| 107 | ribTable6 = new ConcurrentInvertedRadixTree<>( |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 108 | new DefaultByteArrayNodeFactory()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 109 | routesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 110 | HashMultimap.<IpAddress, RouteEntry>create()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 111 | |
| 112 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 113 | new ThreadFactoryBuilder() |
| 114 | .setNameFormat("sdnip-bgp-updates-%d").build()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 117 | @Deactivate |
| 118 | public void deactivate() { |
| 119 | log.debug("Stopped"); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void start(FibListener listener) { |
| 124 | this.fibComponent = checkNotNull(listener); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 125 | this.hostService.addListener(hostListener); |
| 126 | |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 127 | bgpService.start(new InternalRouteListener()); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 128 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 129 | bgpUpdatesExecutor.execute(new Runnable() { |
| 130 | @Override |
| 131 | public void run() { |
| 132 | doUpdatesThread(); |
| 133 | } |
| 134 | }); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 137 | @Override |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 138 | public void stop() { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 139 | bgpService.stop(); |
| 140 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 141 | this.hostService.removeListener(hostListener); |
| 142 | |
| 143 | // Stop the thread(s) |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 144 | bgpUpdatesExecutor.shutdownNow(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 145 | |
| 146 | synchronized (this) { |
| 147 | // Cleanup all local state |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 148 | ribTable4 = new ConcurrentInvertedRadixTree<>( |
| 149 | new DefaultByteArrayNodeFactory()); |
| 150 | ribTable6 = new ConcurrentInvertedRadixTree<>( |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 151 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 152 | routeUpdatesQueue.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 153 | routesWaitingOnArp.clear(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 154 | ip2Mac.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 155 | } |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 158 | /** |
| 159 | * Entry point for route updates. |
| 160 | * |
| 161 | * @param routeUpdates collection of route updates to process |
| 162 | */ |
| 163 | private void update(Collection<RouteUpdate> routeUpdates) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 164 | try { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 165 | routeUpdatesQueue.put(routeUpdates); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 166 | } catch (InterruptedException e) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 167 | log.debug("Interrupted while putting on routeUpdatesQueue", e); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 168 | Thread.currentThread().interrupt(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 173 | * Thread for handling route updates. |
| 174 | */ |
| 175 | private void doUpdatesThread() { |
| 176 | boolean interrupted = false; |
| 177 | try { |
| 178 | while (!interrupted) { |
| 179 | try { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 180 | Collection<RouteUpdate> routeUpdates = |
| 181 | routeUpdatesQueue.take(); |
| 182 | processRouteUpdates(routeUpdates); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 183 | } catch (InterruptedException e) { |
| 184 | log.debug("Interrupted while taking from updates queue", e); |
| 185 | interrupted = true; |
| 186 | } catch (Exception e) { |
| 187 | log.debug("exception", e); |
| 188 | } |
| 189 | } |
| 190 | } finally { |
| 191 | if (interrupted) { |
| 192 | Thread.currentThread().interrupt(); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 198 | * Gets all IPv4 routes from the RIB. |
| 199 | * |
| 200 | * @return all IPv4 routes from the RIB |
| 201 | */ |
| 202 | public Collection<RouteEntry> getRoutes4() { |
| 203 | Iterator<KeyValuePair<RouteEntry>> it = |
| 204 | ribTable4.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 205 | |
| 206 | List<RouteEntry> routes = new LinkedList<>(); |
| 207 | |
| 208 | while (it.hasNext()) { |
| 209 | KeyValuePair<RouteEntry> entry = it.next(); |
| 210 | routes.add(entry.getValue()); |
| 211 | } |
| 212 | |
| 213 | return routes; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Gets all IPv6 routes from the RIB. |
| 218 | * |
| 219 | * @return all IPv6 routes from the RIB |
| 220 | */ |
| 221 | public Collection<RouteEntry> getRoutes6() { |
| 222 | Iterator<KeyValuePair<RouteEntry>> it = |
| 223 | ribTable6.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 224 | |
| 225 | List<RouteEntry> routes = new LinkedList<>(); |
| 226 | |
| 227 | while (it.hasNext()) { |
| 228 | KeyValuePair<RouteEntry> entry = it.next(); |
| 229 | routes.add(entry.getValue()); |
| 230 | } |
| 231 | |
| 232 | return routes; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Finds a route in the RIB for a prefix. The prefix can be either IPv4 or |
| 237 | * IPv6. |
| 238 | * |
| 239 | * @param prefix the prefix to use |
| 240 | * @return the route if found, otherwise null |
| 241 | */ |
| 242 | RouteEntry findRibRoute(IpPrefix prefix) { |
| 243 | String binaryString = RouteEntry.createBinaryString(prefix); |
| 244 | if (prefix.version() == Ip4Address.VERSION) { |
| 245 | // IPv4 |
| 246 | return ribTable4.getValueForExactKey(binaryString); |
| 247 | } |
| 248 | // IPv6 |
| 249 | return ribTable6.getValueForExactKey(binaryString); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Adds a route to the RIB. The route can be either IPv4 or IPv6. |
| 254 | * |
| 255 | * @param routeEntry the route entry to use |
| 256 | */ |
| 257 | void addRibRoute(RouteEntry routeEntry) { |
| 258 | if (routeEntry.prefix().version() == Ip4Address.VERSION) { |
| 259 | // IPv4 |
| 260 | ribTable4.put(RouteEntry.createBinaryString(routeEntry.prefix()), |
| 261 | routeEntry); |
| 262 | } else { |
| 263 | // IPv6 |
| 264 | ribTable6.put(RouteEntry.createBinaryString(routeEntry.prefix()), |
| 265 | routeEntry); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Removes a route for a prefix from the RIB. The prefix can be either IPv4 |
| 271 | * or IPv6. |
| 272 | * |
| 273 | * @param prefix the prefix to use |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 274 | * @return true if the route was found and removed, otherwise false |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 275 | */ |
| 276 | boolean removeRibRoute(IpPrefix prefix) { |
| 277 | if (prefix.version() == Ip4Address.VERSION) { |
| 278 | // IPv4 |
| 279 | return ribTable4.remove(RouteEntry.createBinaryString(prefix)); |
| 280 | } |
| 281 | // IPv6 |
| 282 | return ribTable6.remove(RouteEntry.createBinaryString(prefix)); |
| 283 | } |
| 284 | |
| 285 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 286 | * Processes route updates. |
| 287 | * |
| 288 | * @param routeUpdates the route updates to process |
| 289 | */ |
| 290 | void processRouteUpdates(Collection<RouteUpdate> routeUpdates) { |
| 291 | synchronized (this) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 292 | Collection<IpPrefix> withdrawPrefixes = new LinkedList<>(); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 293 | Collection<FibUpdate> fibUpdates = new LinkedList<>(); |
| 294 | Collection<FibUpdate> fibWithdraws = new LinkedList<>(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 295 | |
| 296 | for (RouteUpdate update : routeUpdates) { |
| 297 | switch (update.type()) { |
| 298 | case UPDATE: |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 299 | FibEntry fib = processRouteAdd(update.routeEntry(), |
| 300 | withdrawPrefixes); |
| 301 | if (fib != null) { |
| 302 | fibUpdates.add(new FibUpdate(FibUpdate.Type.UPDATE, fib)); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 303 | } |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 304 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 305 | break; |
| 306 | case DELETE: |
| 307 | processRouteDelete(update.routeEntry(), withdrawPrefixes); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 308 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 309 | break; |
| 310 | default: |
| 311 | log.error("Unknown update Type: {}", update.type()); |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 316 | withdrawPrefixes.forEach(p -> fibWithdraws.add(new FibUpdate( |
| 317 | FibUpdate.Type.DELETE, new FibEntry(p, null, null)))); |
| 318 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 319 | if (!fibUpdates.isEmpty() || !fibWithdraws.isEmpty()) { |
| 320 | fibComponent.update(fibUpdates, fibWithdraws); |
| 321 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 326 | * Processes adding a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 327 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 328 | * The route entry is added to the radix tree. If there was an existing |
| 329 | * next hop for this prefix, but the next hop was different, then the |
| 330 | * old route entry is deleted. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 331 | * </p> |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 332 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 333 | * NOTE: Currently, we don't handle routes if the next hop is within the |
| 334 | * SDN domain. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 335 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 336 | * |
| 337 | * @param routeEntry the route entry to add |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 338 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 339 | * intents will be withdrawn |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 340 | * @return the corresponding FIB entry change, or null |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 341 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 342 | private FibEntry processRouteAdd( |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 343 | RouteEntry routeEntry, |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 344 | Collection<IpPrefix> withdrawPrefixes) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 345 | log.debug("Processing route add: {}", routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 346 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 347 | // Find the old next-hop if we are updating an old route entry |
| 348 | IpAddress oldNextHop = null; |
| 349 | RouteEntry oldRouteEntry = findRibRoute(routeEntry.prefix()); |
| 350 | if (oldRouteEntry != null) { |
| 351 | oldNextHop = oldRouteEntry.nextHop(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 354 | // Add the new route to the RIB |
| 355 | addRibRoute(routeEntry); |
| 356 | |
| 357 | if (oldNextHop != null) { |
| 358 | if (oldNextHop.equals(routeEntry.nextHop())) { |
| 359 | return null; // No change |
| 360 | } |
| 361 | // |
| 362 | // Update an existing nexthop for the prefix. |
| 363 | // We need to remove the old flows for this prefix from the |
| 364 | // switches before the new flows are added. |
| 365 | // |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 366 | withdrawPrefixes.add(routeEntry.prefix()); |
| 367 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 368 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 369 | if (routeEntry.nextHop().isZero()) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 370 | // Route originated by SDN domain |
| 371 | // We don't handle these at the moment |
| 372 | log.debug("Own route {} to {}", |
| 373 | routeEntry.prefix(), routeEntry.nextHop()); |
| 374 | return null; |
| 375 | } |
| 376 | |
| 377 | // |
| 378 | // Find the MAC address of next hop router for this route entry. |
| 379 | // If the MAC address can not be found in ARP cache, then this prefix |
| 380 | // will be put in routesWaitingOnArp queue. Otherwise, generate |
| 381 | // a new route intent. |
| 382 | // |
| 383 | |
| 384 | // Monitor the IP address for updates of the MAC address |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 385 | hostService.startMonitoringIp(routeEntry.nextHop()); |
| 386 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 387 | // Check if we know the MAC address of the next hop |
| 388 | MacAddress nextHopMacAddress = ip2Mac.get(routeEntry.nextHop()); |
| 389 | if (nextHopMacAddress == null) { |
| 390 | Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop()); |
| 391 | if (!hosts.isEmpty()) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 392 | nextHopMacAddress = hosts.iterator().next().mac(); |
| 393 | } |
| 394 | if (nextHopMacAddress != null) { |
| 395 | ip2Mac.put(routeEntry.nextHop(), nextHopMacAddress); |
| 396 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 397 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 398 | if (nextHopMacAddress == null) { |
| 399 | routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 400 | return null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 401 | } |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 402 | return new FibEntry(routeEntry.prefix(), routeEntry.nextHop(), |
| 403 | nextHopMacAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 407 | * Processes the deletion of a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 408 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 409 | * The prefix for the routing entry is removed from radix tree. |
| 410 | * If the operation is successful, the prefix is added to the collection |
| 411 | * of prefixes whose intents that will be withdrawn. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 412 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 413 | * |
| 414 | * @param routeEntry the route entry to delete |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 415 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 416 | * intents will be withdrawn |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 417 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 418 | private void processRouteDelete(RouteEntry routeEntry, |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 419 | Collection<IpPrefix> withdrawPrefixes) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 420 | log.debug("Processing route delete: {}", routeEntry); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 421 | boolean isRemoved = removeRibRoute(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 422 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 423 | if (isRemoved) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 424 | // |
| 425 | // Only withdraw intents if an entry was actually removed from the |
| 426 | // tree. If no entry was removed, the <prefix, nexthop> wasn't |
| 427 | // there so it's probably already been removed and we don't |
| 428 | // need to do anything. |
| 429 | // |
| 430 | withdrawPrefixes.add(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 431 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 432 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 433 | routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /** |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 437 | * Signals the Router that the MAC to IP mapping has potentially been |
| 438 | * updated. This has the effect of updating the MAC address for any |
| 439 | * installed prefixes if it has changed, as well as installing any pending |
| 440 | * prefixes that were waiting for MAC resolution. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 441 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 442 | * @param ipAddress the IP address that an event was received for |
| 443 | * @param macAddress the most recently known MAC address for the IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 444 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 445 | private void updateMac(IpAddress ipAddress, MacAddress macAddress) { |
| 446 | log.debug("Received updated MAC info: {} => {}", ipAddress, |
| 447 | macAddress); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 448 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 449 | // |
| 450 | // We synchronize on "this" to prevent changes to the Radix tree |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 451 | // while we're pushing intents. If the tree changes, the |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 452 | // tree and the intents could get out of sync. |
| 453 | // |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 454 | synchronized (this) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 455 | Collection<FibUpdate> submitFibEntries = new LinkedList<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 456 | |
| 457 | Set<RouteEntry> routesToPush = |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 458 | routesWaitingOnArp.removeAll(ipAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 459 | |
| 460 | for (RouteEntry routeEntry : routesToPush) { |
| 461 | // These will always be adds |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 462 | RouteEntry foundRouteEntry = findRibRoute(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 463 | if (foundRouteEntry != null && |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 464 | foundRouteEntry.nextHop().equals(routeEntry.nextHop())) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 465 | // We only push FIB updates if the prefix is still in the |
| 466 | // radix tree and the next hop is the same as our entry. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 467 | // The prefix could have been removed while we were waiting |
| 468 | // for the ARP, or the next hop could have changed. |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 469 | submitFibEntries.add(new FibUpdate(FibUpdate.Type.UPDATE, |
| 470 | new FibEntry(routeEntry.prefix(), |
| 471 | ipAddress, macAddress))); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 472 | } else { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 473 | log.debug("{} has been revoked before the MAC was resolved", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 474 | routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 477 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 478 | if (!submitFibEntries.isEmpty()) { |
| 479 | fibComponent.update(submitFibEntries, Collections.emptyList()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | ip2Mac.put(ipAddress, macAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 487 | * Listener for host events. |
| 488 | */ |
| 489 | class InternalHostListener implements HostListener { |
| 490 | @Override |
| 491 | public void event(HostEvent event) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 492 | log.debug("Received HostEvent {}", event); |
| 493 | |
| 494 | Host host = event.subject(); |
| 495 | switch (event.type()) { |
| 496 | case HOST_ADDED: |
| 497 | // FALLTHROUGH |
| 498 | case HOST_UPDATED: |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 499 | for (IpAddress ipAddress : host.ipAddresses()) { |
| 500 | updateMac(ipAddress, host.mac()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 501 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 502 | break; |
| 503 | case HOST_REMOVED: |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 504 | for (IpAddress ipAddress : host.ipAddresses()) { |
| 505 | ip2Mac.remove(ipAddress); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 506 | } |
| 507 | break; |
| 508 | default: |
| 509 | break; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | } |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * Listener for route events. |
| 516 | */ |
| 517 | private class InternalRouteListener implements RouteListener { |
| 518 | @Override |
| 519 | public void update(Collection<RouteUpdate> routeUpdates) { |
| 520 | Router.this.update(routeUpdates); |
| 521 | } |
| 522 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 523 | } |