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