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