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