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 | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 16 | package org.onosproject.routing.impl; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 17 | |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Collection; |
| 21 | import java.util.Collections; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.LinkedList; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | import java.util.concurrent.BlockingQueue; |
| 28 | import java.util.concurrent.ConcurrentHashMap; |
| 29 | import java.util.concurrent.ExecutorService; |
| 30 | import java.util.concurrent.Executors; |
| 31 | import java.util.concurrent.LinkedBlockingQueue; |
| 32 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 33 | import org.apache.felix.scr.annotations.Activate; |
| 34 | import org.apache.felix.scr.annotations.Component; |
| 35 | import org.apache.felix.scr.annotations.Deactivate; |
| 36 | import org.apache.felix.scr.annotations.Reference; |
| 37 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 38 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 39 | import org.onlab.packet.IpAddress; |
| 40 | import org.onlab.packet.IpPrefix; |
| 41 | import org.onlab.packet.MacAddress; |
| 42 | import org.onosproject.net.Host; |
| 43 | import org.onosproject.net.host.HostEvent; |
| 44 | import org.onosproject.net.host.HostListener; |
| 45 | import org.onosproject.net.host.HostService; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 46 | import org.onosproject.routing.BgpService; |
| 47 | import org.onosproject.routing.FibEntry; |
| 48 | import org.onosproject.routing.FibListener; |
| 49 | import org.onosproject.routing.FibUpdate; |
| 50 | import org.onosproject.routing.RouteEntry; |
| 51 | import org.onosproject.routing.RouteListener; |
| 52 | import org.onosproject.routing.RouteUpdate; |
| 53 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 57 | import com.google.common.collect.HashMultimap; |
| 58 | import com.google.common.collect.Multimaps; |
| 59 | import com.google.common.collect.SetMultimap; |
| 60 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 61 | import com.googlecode.concurrenttrees.common.KeyValuePair; |
| 62 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 63 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 64 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 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) { |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 167 | log.error("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) { |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 184 | log.error("Interrupted while taking from updates queue", e); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 185 | interrupted = true; |
| 186 | } catch (Exception e) { |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 187 | log.error("exception", e); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 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 | */ |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 202 | @Override |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 203 | public Collection<RouteEntry> getRoutes4() { |
| 204 | Iterator<KeyValuePair<RouteEntry>> it = |
| 205 | ribTable4.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 206 | |
| 207 | List<RouteEntry> routes = new LinkedList<>(); |
| 208 | |
| 209 | while (it.hasNext()) { |
| 210 | KeyValuePair<RouteEntry> entry = it.next(); |
| 211 | routes.add(entry.getValue()); |
| 212 | } |
| 213 | |
| 214 | return routes; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Gets all IPv6 routes from the RIB. |
| 219 | * |
| 220 | * @return all IPv6 routes from the RIB |
| 221 | */ |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 222 | @Override |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 223 | public Collection<RouteEntry> getRoutes6() { |
| 224 | Iterator<KeyValuePair<RouteEntry>> it = |
| 225 | ribTable6.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 226 | |
| 227 | List<RouteEntry> routes = new LinkedList<>(); |
| 228 | |
| 229 | while (it.hasNext()) { |
| 230 | KeyValuePair<RouteEntry> entry = it.next(); |
| 231 | routes.add(entry.getValue()); |
| 232 | } |
| 233 | |
| 234 | return routes; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Finds a route in the RIB for a prefix. The prefix can be either IPv4 or |
| 239 | * IPv6. |
| 240 | * |
| 241 | * @param prefix the prefix to use |
| 242 | * @return the route if found, otherwise null |
| 243 | */ |
| 244 | RouteEntry findRibRoute(IpPrefix prefix) { |
| 245 | String binaryString = RouteEntry.createBinaryString(prefix); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 246 | if (prefix.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 247 | // IPv4 |
| 248 | return ribTable4.getValueForExactKey(binaryString); |
| 249 | } |
| 250 | // IPv6 |
| 251 | return ribTable6.getValueForExactKey(binaryString); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Adds a route to the RIB. The route can be either IPv4 or IPv6. |
| 256 | * |
| 257 | * @param routeEntry the route entry to use |
| 258 | */ |
| 259 | void addRibRoute(RouteEntry routeEntry) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 260 | if (routeEntry.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 261 | // IPv4 |
| 262 | ribTable4.put(RouteEntry.createBinaryString(routeEntry.prefix()), |
| 263 | routeEntry); |
| 264 | } else { |
| 265 | // IPv6 |
| 266 | ribTable6.put(RouteEntry.createBinaryString(routeEntry.prefix()), |
| 267 | routeEntry); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Removes a route for a prefix from the RIB. The prefix can be either IPv4 |
| 273 | * or IPv6. |
| 274 | * |
| 275 | * @param prefix the prefix to use |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 276 | * @return true if the route was found and removed, otherwise false |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 277 | */ |
| 278 | boolean removeRibRoute(IpPrefix prefix) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 279 | if (prefix.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 280 | // IPv4 |
| 281 | return ribTable4.remove(RouteEntry.createBinaryString(prefix)); |
| 282 | } |
| 283 | // IPv6 |
| 284 | return ribTable6.remove(RouteEntry.createBinaryString(prefix)); |
| 285 | } |
| 286 | |
| 287 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 288 | * Processes route updates. |
| 289 | * |
| 290 | * @param routeUpdates the route updates to process |
| 291 | */ |
| 292 | void processRouteUpdates(Collection<RouteUpdate> routeUpdates) { |
| 293 | synchronized (this) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 294 | Collection<IpPrefix> withdrawPrefixes = new LinkedList<>(); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 295 | Collection<FibUpdate> fibUpdates = new LinkedList<>(); |
| 296 | Collection<FibUpdate> fibWithdraws = new LinkedList<>(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 297 | |
| 298 | for (RouteUpdate update : routeUpdates) { |
| 299 | switch (update.type()) { |
| 300 | case UPDATE: |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 301 | FibEntry fib = processRouteAdd(update.routeEntry(), |
| 302 | withdrawPrefixes); |
| 303 | if (fib != null) { |
| 304 | fibUpdates.add(new FibUpdate(FibUpdate.Type.UPDATE, fib)); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 305 | } |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 306 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 307 | break; |
| 308 | case DELETE: |
| 309 | processRouteDelete(update.routeEntry(), withdrawPrefixes); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 310 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 311 | break; |
| 312 | default: |
| 313 | log.error("Unknown update Type: {}", update.type()); |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 318 | withdrawPrefixes.forEach(p -> fibWithdraws.add(new FibUpdate( |
| 319 | FibUpdate.Type.DELETE, new FibEntry(p, null, null)))); |
| 320 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 321 | if (!fibUpdates.isEmpty() || !fibWithdraws.isEmpty()) { |
| 322 | fibComponent.update(fibUpdates, fibWithdraws); |
| 323 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 328 | * Processes adding a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 329 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 330 | * The route entry is added to the radix tree. If there was an existing |
| 331 | * next hop for this prefix, but the next hop was different, then the |
| 332 | * old route entry is deleted. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 333 | * </p> |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 334 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 335 | * NOTE: Currently, we don't handle routes if the next hop is within the |
| 336 | * SDN domain. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 337 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 338 | * |
| 339 | * @param routeEntry the route entry to add |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 340 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 341 | * intents will be withdrawn |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 342 | * @return the corresponding FIB entry change, or null |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 343 | */ |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 344 | private FibEntry processRouteAdd( |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 345 | RouteEntry routeEntry, |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 346 | Collection<IpPrefix> withdrawPrefixes) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 347 | log.debug("Processing route add: {}", routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 348 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 349 | // Find the old next-hop if we are updating an old route entry |
| 350 | IpAddress oldNextHop = null; |
| 351 | RouteEntry oldRouteEntry = findRibRoute(routeEntry.prefix()); |
| 352 | if (oldRouteEntry != null) { |
| 353 | oldNextHop = oldRouteEntry.nextHop(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 356 | // Add the new route to the RIB |
| 357 | addRibRoute(routeEntry); |
| 358 | |
| 359 | if (oldNextHop != null) { |
| 360 | if (oldNextHop.equals(routeEntry.nextHop())) { |
| 361 | return null; // No change |
| 362 | } |
| 363 | // |
| 364 | // Update an existing nexthop for the prefix. |
| 365 | // We need to remove the old flows for this prefix from the |
| 366 | // switches before the new flows are added. |
| 367 | // |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 368 | withdrawPrefixes.add(routeEntry.prefix()); |
| 369 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 370 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 371 | if (routeEntry.nextHop().isZero()) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 372 | // Route originated by SDN domain |
| 373 | // We don't handle these at the moment |
| 374 | log.debug("Own route {} to {}", |
| 375 | routeEntry.prefix(), routeEntry.nextHop()); |
| 376 | return null; |
| 377 | } |
| 378 | |
| 379 | // |
| 380 | // Find the MAC address of next hop router for this route entry. |
| 381 | // If the MAC address can not be found in ARP cache, then this prefix |
| 382 | // will be put in routesWaitingOnArp queue. Otherwise, generate |
| 383 | // a new route intent. |
| 384 | // |
| 385 | |
| 386 | // Monitor the IP address for updates of the MAC address |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 387 | hostService.startMonitoringIp(routeEntry.nextHop()); |
| 388 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 389 | // Check if we know the MAC address of the next hop |
| 390 | MacAddress nextHopMacAddress = ip2Mac.get(routeEntry.nextHop()); |
| 391 | if (nextHopMacAddress == null) { |
| 392 | Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop()); |
| 393 | if (!hosts.isEmpty()) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 394 | nextHopMacAddress = hosts.iterator().next().mac(); |
| 395 | } |
| 396 | if (nextHopMacAddress != null) { |
| 397 | ip2Mac.put(routeEntry.nextHop(), nextHopMacAddress); |
| 398 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 399 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 400 | if (nextHopMacAddress == null) { |
| 401 | routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 402 | return null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 403 | } |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 404 | return new FibEntry(routeEntry.prefix(), routeEntry.nextHop(), |
| 405 | nextHopMacAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 409 | * Processes the deletion of a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 410 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 411 | * The prefix for the routing entry is removed from radix tree. |
| 412 | * If the operation is successful, the prefix is added to the collection |
| 413 | * of prefixes whose intents that will be withdrawn. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 414 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 415 | * |
| 416 | * @param routeEntry the route entry to delete |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 417 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 418 | * intents will be withdrawn |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 419 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 420 | private void processRouteDelete(RouteEntry routeEntry, |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 421 | Collection<IpPrefix> withdrawPrefixes) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 422 | log.debug("Processing route delete: {}", routeEntry); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 423 | boolean isRemoved = removeRibRoute(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 424 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 425 | if (isRemoved) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 426 | // |
| 427 | // Only withdraw intents if an entry was actually removed from the |
| 428 | // tree. If no entry was removed, the <prefix, nexthop> wasn't |
| 429 | // there so it's probably already been removed and we don't |
| 430 | // need to do anything. |
| 431 | // |
| 432 | withdrawPrefixes.add(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 433 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 434 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 435 | routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 439 | * Signals the Router that the MAC to IP mapping has potentially been |
| 440 | * updated. This has the effect of updating the MAC address for any |
| 441 | * installed prefixes if it has changed, as well as installing any pending |
| 442 | * prefixes that were waiting for MAC resolution. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 443 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 444 | * @param ipAddress the IP address that an event was received for |
| 445 | * @param macAddress the most recently known MAC address for the IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 446 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 447 | private void updateMac(IpAddress ipAddress, MacAddress macAddress) { |
| 448 | log.debug("Received updated MAC info: {} => {}", ipAddress, |
| 449 | macAddress); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 450 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 451 | // |
| 452 | // We synchronize on "this" to prevent changes to the Radix tree |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 453 | // while we're pushing intents. If the tree changes, the |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 454 | // tree and the intents could get out of sync. |
| 455 | // |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 456 | synchronized (this) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 457 | Collection<FibUpdate> submitFibEntries = new LinkedList<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 458 | |
| 459 | Set<RouteEntry> routesToPush = |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 460 | routesWaitingOnArp.removeAll(ipAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 461 | |
| 462 | for (RouteEntry routeEntry : routesToPush) { |
| 463 | // These will always be adds |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 464 | RouteEntry foundRouteEntry = findRibRoute(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 465 | if (foundRouteEntry != null && |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 466 | foundRouteEntry.nextHop().equals(routeEntry.nextHop())) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 467 | // We only push FIB updates if the prefix is still in the |
| 468 | // radix tree and the next hop is the same as our entry. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 469 | // The prefix could have been removed while we were waiting |
| 470 | // for the ARP, or the next hop could have changed. |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 471 | submitFibEntries.add(new FibUpdate(FibUpdate.Type.UPDATE, |
| 472 | new FibEntry(routeEntry.prefix(), |
| 473 | ipAddress, macAddress))); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 474 | } else { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 475 | log.debug("{} has been revoked before the MAC was resolved", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 476 | routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 479 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 480 | if (!submitFibEntries.isEmpty()) { |
| 481 | fibComponent.update(submitFibEntries, Collections.emptyList()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | ip2Mac.put(ipAddress, macAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 489 | * Listener for host events. |
| 490 | */ |
| 491 | class InternalHostListener implements HostListener { |
| 492 | @Override |
| 493 | public void event(HostEvent event) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 494 | log.debug("Received HostEvent {}", event); |
| 495 | |
| 496 | Host host = event.subject(); |
| 497 | switch (event.type()) { |
| 498 | case HOST_ADDED: |
| 499 | // FALLTHROUGH |
| 500 | case HOST_UPDATED: |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 501 | for (IpAddress ipAddress : host.ipAddresses()) { |
| 502 | updateMac(ipAddress, host.mac()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 503 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 504 | break; |
| 505 | case HOST_REMOVED: |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 506 | for (IpAddress ipAddress : host.ipAddresses()) { |
| 507 | ip2Mac.remove(ipAddress); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 508 | } |
| 509 | break; |
| 510 | default: |
| 511 | break; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | } |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 515 | |
| 516 | /** |
| 517 | * Listener for route events. |
| 518 | */ |
| 519 | private class InternalRouteListener implements RouteListener { |
| 520 | @Override |
| 521 | public void update(Collection<RouteUpdate> routeUpdates) { |
| 522 | Router.this.update(routeUpdates); |
| 523 | } |
| 524 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 525 | } |