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; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 38 | import org.onosproject.net.ConnectPoint; |
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; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 47 | import org.onosproject.routing.IntentRequestListener; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 48 | import org.onosproject.routing.RouteEntry; |
| 49 | import org.onosproject.routing.RouteListener; |
| 50 | import org.onosproject.routing.RouteUpdate; |
| 51 | import org.onosproject.routing.RoutingService; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 52 | import org.onosproject.routing.config.Interface; |
| 53 | import org.onosproject.routing.config.RoutingConfigurationService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame^] | 57 | import java.util.Collection; |
| 58 | import java.util.Collections; |
| 59 | import java.util.Iterator; |
| 60 | import java.util.LinkedList; |
| 61 | import java.util.List; |
| 62 | import java.util.Map; |
| 63 | import java.util.Set; |
| 64 | import java.util.concurrent.BlockingQueue; |
| 65 | import java.util.concurrent.ConcurrentHashMap; |
| 66 | import java.util.concurrent.ExecutorService; |
| 67 | import java.util.concurrent.Executors; |
| 68 | import java.util.concurrent.LinkedBlockingQueue; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 69 | |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame^] | 70 | import static com.google.common.base.Preconditions.checkNotNull; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 71 | import static org.onosproject.routing.RouteEntry.createBinaryString; |
| 72 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 73 | /** |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 74 | * This class processes route updates and maintains a Routing Information Base |
| 75 | * (RIB). After route updates have been processed and next hops have been |
| 76 | * resolved, FIB updates are sent to any listening FIB components. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 77 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 78 | @Component(immediate = true) |
| 79 | @Service |
| 80 | public class Router implements RoutingService { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 81 | |
| 82 | private static final Logger log = LoggerFactory.getLogger(Router.class); |
| 83 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 84 | // Route entries are stored in a radix tree. |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 85 | // 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] | 86 | private InvertedRadixTree<RouteEntry> ribTable4; |
| 87 | private InvertedRadixTree<RouteEntry> ribTable6; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 88 | |
| 89 | // Stores all incoming route updates in a queue. |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 90 | private final BlockingQueue<Collection<RouteUpdate>> routeUpdatesQueue = |
| 91 | new LinkedBlockingQueue<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 92 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 93 | // Next-hop IP address to route entry mapping for next hops pending MAC |
| 94 | // resolution |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 95 | private SetMultimap<IpAddress, RouteEntry> routesWaitingOnArp; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 96 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 97 | // The IPv4 address to MAC address mapping |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 98 | private final Map<IpAddress, MacAddress> ip2Mac = new ConcurrentHashMap<>(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 99 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 100 | private FibListener fibComponent; |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 101 | private IntentRequestListener intentRequestListener; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 102 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame^] | 104 | protected CoreService coreService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 107 | protected HostService hostService; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 108 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected BgpService bgpService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 111 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 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); |
| 143 | |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void addIntentRequestListener(IntentRequestListener intentRequestListener) { |
| 148 | this.intentRequestListener = checkNotNull(intentRequestListener); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void start() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 153 | this.hostService.addListener(hostListener); |
| 154 | |
Jonathan Hart | d24fafb | 2015-02-09 17:55:32 -0800 | [diff] [blame] | 155 | bgpService.start(new InternalRouteListener()); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 156 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 157 | bgpUpdatesExecutor.execute(new Runnable() { |
| 158 | @Override |
| 159 | public void run() { |
| 160 | doUpdatesThread(); |
| 161 | } |
| 162 | }); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 165 | @Override |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 166 | public void stop() { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 167 | bgpService.stop(); |
| 168 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 169 | this.hostService.removeListener(hostListener); |
| 170 | |
| 171 | // Stop the thread(s) |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 172 | bgpUpdatesExecutor.shutdownNow(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 173 | |
| 174 | synchronized (this) { |
| 175 | // Cleanup all local state |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 176 | ribTable4 = new ConcurrentInvertedRadixTree<>( |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 177 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 178 | ribTable6 = new ConcurrentInvertedRadixTree<>( |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 179 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 180 | routeUpdatesQueue.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 181 | routesWaitingOnArp.clear(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 182 | ip2Mac.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 183 | } |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 186 | /** |
| 187 | * Entry point for route updates. |
| 188 | * |
| 189 | * @param routeUpdates collection of route updates to process |
| 190 | */ |
| 191 | private void update(Collection<RouteUpdate> routeUpdates) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 192 | try { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 193 | routeUpdatesQueue.put(routeUpdates); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 194 | } catch (InterruptedException e) { |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 195 | log.error("Interrupted while putting on routeUpdatesQueue", e); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 196 | Thread.currentThread().interrupt(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 201 | * Thread for handling route updates. |
| 202 | */ |
| 203 | private void doUpdatesThread() { |
| 204 | boolean interrupted = false; |
| 205 | try { |
| 206 | while (!interrupted) { |
| 207 | try { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 208 | Collection<RouteUpdate> routeUpdates = |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 209 | routeUpdatesQueue.take(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 210 | processRouteUpdates(routeUpdates); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 211 | } catch (InterruptedException e) { |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 212 | log.error("Interrupted while taking from updates queue", e); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 213 | interrupted = true; |
| 214 | } catch (Exception e) { |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 215 | log.error("exception", e); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | } finally { |
| 219 | if (interrupted) { |
| 220 | Thread.currentThread().interrupt(); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 226 | * Gets all IPv4 routes from the RIB. |
| 227 | * |
| 228 | * @return all IPv4 routes from the RIB |
| 229 | */ |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 230 | @Override |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 231 | public Collection<RouteEntry> getRoutes4() { |
| 232 | Iterator<KeyValuePair<RouteEntry>> it = |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 233 | ribTable4.getKeyValuePairsForKeysStartingWith("").iterator(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 234 | |
| 235 | List<RouteEntry> routes = new LinkedList<>(); |
| 236 | |
| 237 | while (it.hasNext()) { |
| 238 | KeyValuePair<RouteEntry> entry = it.next(); |
| 239 | routes.add(entry.getValue()); |
| 240 | } |
| 241 | |
| 242 | return routes; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Gets all IPv6 routes from the RIB. |
| 247 | * |
| 248 | * @return all IPv6 routes from the RIB |
| 249 | */ |
Srikanth Vavilapalli | 89ad377 | 2015-03-25 18:00:38 -0700 | [diff] [blame] | 250 | @Override |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 251 | public Collection<RouteEntry> getRoutes6() { |
| 252 | Iterator<KeyValuePair<RouteEntry>> it = |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 253 | ribTable6.getKeyValuePairsForKeysStartingWith("").iterator(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 254 | |
| 255 | List<RouteEntry> routes = new LinkedList<>(); |
| 256 | |
| 257 | while (it.hasNext()) { |
| 258 | KeyValuePair<RouteEntry> entry = it.next(); |
| 259 | routes.add(entry.getValue()); |
| 260 | } |
| 261 | |
| 262 | return routes; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Finds a route in the RIB for a prefix. The prefix can be either IPv4 or |
| 267 | * IPv6. |
| 268 | * |
| 269 | * @param prefix the prefix to use |
| 270 | * @return the route if found, otherwise null |
| 271 | */ |
| 272 | RouteEntry findRibRoute(IpPrefix prefix) { |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 273 | String binaryString = createBinaryString(prefix); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 274 | if (prefix.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 275 | // IPv4 |
| 276 | return ribTable4.getValueForExactKey(binaryString); |
| 277 | } |
| 278 | // IPv6 |
| 279 | return ribTable6.getValueForExactKey(binaryString); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Adds a route to the RIB. The route can be either IPv4 or IPv6. |
| 284 | * |
| 285 | * @param routeEntry the route entry to use |
| 286 | */ |
| 287 | void addRibRoute(RouteEntry routeEntry) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 288 | if (routeEntry.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 289 | // IPv4 |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 290 | ribTable4.put(createBinaryString(routeEntry.prefix()), |
| 291 | routeEntry); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 292 | } else { |
| 293 | // IPv6 |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 294 | ribTable6.put(createBinaryString(routeEntry.prefix()), |
| 295 | routeEntry); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Removes a route for a prefix from the RIB. The prefix can be either IPv4 |
| 301 | * or IPv6. |
| 302 | * |
| 303 | * @param prefix the prefix to use |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 304 | * @return true if the route was found and removed, otherwise false |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 305 | */ |
| 306 | boolean removeRibRoute(IpPrefix prefix) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 307 | if (prefix.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 308 | // IPv4 |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 309 | return ribTable4.remove(createBinaryString(prefix)); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 310 | } |
| 311 | // IPv6 |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 312 | return ribTable6.remove(createBinaryString(prefix)); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 316 | * Processes route updates. |
| 317 | * |
| 318 | * @param routeUpdates the route updates to process |
| 319 | */ |
| 320 | void processRouteUpdates(Collection<RouteUpdate> routeUpdates) { |
| 321 | synchronized (this) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 322 | Collection<IpPrefix> withdrawPrefixes = new LinkedList<>(); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 323 | Collection<FibUpdate> fibUpdates = new LinkedList<>(); |
| 324 | Collection<FibUpdate> fibWithdraws = new LinkedList<>(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 325 | |
| 326 | for (RouteUpdate update : routeUpdates) { |
| 327 | switch (update.type()) { |
| 328 | case UPDATE: |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 329 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 330 | FibEntry fib = processRouteAdd(update.routeEntry(), |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 331 | withdrawPrefixes); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 332 | if (fib != null) { |
| 333 | fibUpdates.add(new FibUpdate(FibUpdate.Type.UPDATE, fib)); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 334 | } |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 335 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 336 | break; |
| 337 | case DELETE: |
| 338 | processRouteDelete(update.routeEntry(), withdrawPrefixes); |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 339 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 340 | break; |
| 341 | default: |
| 342 | log.error("Unknown update Type: {}", update.type()); |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 347 | withdrawPrefixes.forEach(p -> fibWithdraws.add(new FibUpdate( |
| 348 | FibUpdate.Type.DELETE, new FibEntry(p, null, null)))); |
| 349 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 350 | if (!fibUpdates.isEmpty() || !fibWithdraws.isEmpty()) { |
| 351 | fibComponent.update(fibUpdates, fibWithdraws); |
| 352 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
| 355 | |
| 356 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 357 | * Processes adding a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 358 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 359 | * The route entry is added to the radix tree. If there was an existing |
| 360 | * next hop for this prefix, but the next hop was different, then the |
| 361 | * old route entry is deleted. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 362 | * </p> |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 363 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 364 | * NOTE: Currently, we don't handle routes if the next hop is within the |
| 365 | * SDN domain. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 366 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 367 | * |
| 368 | * @param routeEntry the route entry to add |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 369 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 370 | * intents will be withdrawn |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 371 | * @return the corresponding FIB entry change, or null |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 372 | */ |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 373 | private FibEntry processRouteAdd(RouteEntry routeEntry, |
| 374 | Collection<IpPrefix> withdrawPrefixes) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 375 | log.debug("Processing route add: {}", routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 376 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 377 | // Find the old next-hop if we are updating an old route entry |
| 378 | IpAddress oldNextHop = null; |
| 379 | RouteEntry oldRouteEntry = findRibRoute(routeEntry.prefix()); |
| 380 | if (oldRouteEntry != null) { |
| 381 | oldNextHop = oldRouteEntry.nextHop(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 384 | // Add the new route to the RIB |
| 385 | addRibRoute(routeEntry); |
| 386 | |
| 387 | if (oldNextHop != null) { |
| 388 | if (oldNextHop.equals(routeEntry.nextHop())) { |
| 389 | return null; // No change |
| 390 | } |
| 391 | // |
| 392 | // Update an existing nexthop for the prefix. |
| 393 | // We need to remove the old flows for this prefix from the |
| 394 | // switches before the new flows are added. |
| 395 | // |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 396 | withdrawPrefixes.add(routeEntry.prefix()); |
| 397 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 398 | |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 399 | if (routingConfigurationService.isIpPrefixLocal(routeEntry.prefix())) { |
| 400 | // Route originated by local SDN domain |
| 401 | // We don't handle these here, reactive routing APP will handle |
| 402 | // these |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 403 | log.debug("Own route {} to {}", |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 404 | routeEntry.prefix(), routeEntry.nextHop()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 405 | return null; |
| 406 | } |
| 407 | |
| 408 | // |
| 409 | // Find the MAC address of next hop router for this route entry. |
| 410 | // If the MAC address can not be found in ARP cache, then this prefix |
| 411 | // will be put in routesWaitingOnArp queue. Otherwise, generate |
| 412 | // a new route intent. |
| 413 | // |
| 414 | |
| 415 | // Monitor the IP address for updates of the MAC address |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 416 | hostService.startMonitoringIp(routeEntry.nextHop()); |
| 417 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 418 | // Check if we know the MAC address of the next hop |
| 419 | MacAddress nextHopMacAddress = ip2Mac.get(routeEntry.nextHop()); |
| 420 | if (nextHopMacAddress == null) { |
| 421 | Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop()); |
| 422 | if (!hosts.isEmpty()) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 423 | nextHopMacAddress = hosts.iterator().next().mac(); |
| 424 | } |
| 425 | if (nextHopMacAddress != null) { |
| 426 | ip2Mac.put(routeEntry.nextHop(), nextHopMacAddress); |
| 427 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 428 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 429 | if (nextHopMacAddress == null) { |
| 430 | routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 431 | return null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 432 | } |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 433 | return new FibEntry(routeEntry.prefix(), routeEntry.nextHop(), |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 434 | nextHopMacAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 438 | * Processes the deletion of a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 439 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 440 | * The prefix for the routing entry is removed from radix tree. |
| 441 | * If the operation is successful, the prefix is added to the collection |
| 442 | * of prefixes whose intents that will be withdrawn. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 443 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 444 | * |
| 445 | * @param routeEntry the route entry to delete |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 446 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 447 | * intents will be withdrawn |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 448 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 449 | private void processRouteDelete(RouteEntry routeEntry, |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 450 | Collection<IpPrefix> withdrawPrefixes) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 451 | log.debug("Processing route delete: {}", routeEntry); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 452 | boolean isRemoved = removeRibRoute(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 453 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 454 | if (isRemoved) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 455 | // |
| 456 | // Only withdraw intents if an entry was actually removed from the |
| 457 | // tree. If no entry was removed, the <prefix, nexthop> wasn't |
| 458 | // there so it's probably already been removed and we don't |
| 459 | // need to do anything. |
| 460 | // |
| 461 | withdrawPrefixes.add(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 462 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 463 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 464 | routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 468 | * Signals the Router that the MAC to IP mapping has potentially been |
| 469 | * updated. This has the effect of updating the MAC address for any |
| 470 | * installed prefixes if it has changed, as well as installing any pending |
| 471 | * prefixes that were waiting for MAC resolution. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 472 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 473 | * @param ipAddress the IP address that an event was received for |
| 474 | * @param macAddress the most recently known MAC address for the IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 475 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 476 | private void updateMac(IpAddress ipAddress, MacAddress macAddress) { |
| 477 | log.debug("Received updated MAC info: {} => {}", ipAddress, |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 478 | macAddress); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 479 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 480 | // |
| 481 | // We synchronize on "this" to prevent changes to the Radix tree |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 482 | // while we're pushing intents. If the tree changes, the |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 483 | // tree and the intents could get out of sync. |
| 484 | // |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 485 | synchronized (this) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 486 | Collection<FibUpdate> submitFibEntries = new LinkedList<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 487 | |
| 488 | Set<RouteEntry> routesToPush = |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 489 | routesWaitingOnArp.removeAll(ipAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 490 | |
| 491 | for (RouteEntry routeEntry : routesToPush) { |
| 492 | // These will always be adds |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 493 | RouteEntry foundRouteEntry = findRibRoute(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 494 | if (foundRouteEntry != null && |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 495 | foundRouteEntry.nextHop().equals(routeEntry.nextHop())) { |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 496 | // We only push FIB updates if the prefix is still in the |
| 497 | // radix tree and the next hop is the same as our entry. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 498 | // The prefix could have been removed while we were waiting |
| 499 | // for the ARP, or the next hop could have changed. |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 500 | submitFibEntries.add(new FibUpdate(FibUpdate.Type.UPDATE, |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 501 | new FibEntry(routeEntry.prefix(), |
| 502 | ipAddress, macAddress))); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 503 | } else { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 504 | log.debug("{} has been revoked before the MAC was resolved", |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 505 | routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 508 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 509 | if (!submitFibEntries.isEmpty()) { |
| 510 | fibComponent.update(submitFibEntries, Collections.emptyList()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | ip2Mac.put(ipAddress, macAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 518 | * Listener for host events. |
| 519 | */ |
| 520 | class InternalHostListener implements HostListener { |
| 521 | @Override |
| 522 | public void event(HostEvent event) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 523 | log.debug("Received HostEvent {}", event); |
| 524 | |
| 525 | Host host = event.subject(); |
| 526 | switch (event.type()) { |
| 527 | case HOST_ADDED: |
| 528 | // FALLTHROUGH |
| 529 | case HOST_UPDATED: |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 530 | for (IpAddress ipAddress : host.ipAddresses()) { |
| 531 | updateMac(ipAddress, host.mac()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 532 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 533 | break; |
| 534 | case HOST_REMOVED: |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 535 | for (IpAddress ipAddress : host.ipAddresses()) { |
| 536 | ip2Mac.remove(ipAddress); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 537 | } |
| 538 | break; |
| 539 | default: |
| 540 | break; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | } |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 544 | |
| 545 | /** |
| 546 | * Listener for route events. |
| 547 | */ |
| 548 | private class InternalRouteListener implements RouteListener { |
| 549 | @Override |
| 550 | public void update(Collection<RouteUpdate> routeUpdates) { |
| 551 | Router.this.update(routeUpdates); |
| 552 | } |
| 553 | } |
Pingping Lin | e28ae4c | 2015-03-13 11:37:03 -0700 | [diff] [blame] | 554 | |
| 555 | @Override |
| 556 | public LocationType getLocationType(IpAddress ipAddress) { |
| 557 | if (routingConfigurationService.isIpAddressLocal(ipAddress)) { |
| 558 | return LocationType.LOCAL; |
| 559 | } else if (getLongestMatchableRouteEntry(ipAddress) != null) { |
| 560 | return LocationType.INTERNET; |
| 561 | } else { |
| 562 | return LocationType.NO_ROUTE; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | @Override |
| 567 | public RouteEntry getLongestMatchableRouteEntry(IpAddress ipAddress) { |
| 568 | RouteEntry routeEntry = null; |
| 569 | Iterable<RouteEntry> routeEntries; |
| 570 | |
| 571 | if (ipAddress.isIp4()) { |
| 572 | routeEntries = ribTable4.getValuesForKeysPrefixing( |
| 573 | createBinaryString( |
| 574 | IpPrefix.valueOf(ipAddress, Ip4Address.BIT_LENGTH))); |
| 575 | } else { |
| 576 | routeEntries = ribTable6.getValuesForKeysPrefixing( |
| 577 | createBinaryString( |
| 578 | IpPrefix.valueOf(ipAddress, Ip6Address.BIT_LENGTH))); |
| 579 | } |
| 580 | if (routeEntries == null) { |
| 581 | return null; |
| 582 | } |
| 583 | Iterator<RouteEntry> it = routeEntries.iterator(); |
| 584 | while (it.hasNext()) { |
| 585 | routeEntry = it.next(); |
| 586 | } |
| 587 | return routeEntry; |
| 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public ConnectPoint getEgressConnectPoint(IpAddress dstIpAddress) { |
| 592 | LocationType type = getLocationType(dstIpAddress); |
| 593 | if (type == LocationType.LOCAL) { |
| 594 | Set<Host> hosts = hostService.getHostsByIp(dstIpAddress); |
| 595 | if (!hosts.isEmpty()) { |
| 596 | return hosts.iterator().next().location(); |
| 597 | } else { |
| 598 | hostService.startMonitoringIp(dstIpAddress); |
| 599 | return null; |
| 600 | } |
| 601 | } else if (type == LocationType.INTERNET) { |
| 602 | IpAddress nextHopIpAddress = null; |
| 603 | RouteEntry routeEntry = getLongestMatchableRouteEntry(dstIpAddress); |
| 604 | if (routeEntry != null) { |
| 605 | nextHopIpAddress = routeEntry.nextHop(); |
| 606 | Interface it = routingConfigurationService |
| 607 | .getMatchingInterface(nextHopIpAddress); |
| 608 | if (it != null) { |
| 609 | return it.connectPoint(); |
| 610 | } else { |
| 611 | return null; |
| 612 | } |
| 613 | } else { |
| 614 | return null; |
| 615 | } |
| 616 | } else { |
| 617 | return null; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public void packetReactiveProcessor(IpAddress dstIpAddress, |
| 623 | IpAddress srcIpAddress, |
| 624 | ConnectPoint srcConnectPoint, |
| 625 | MacAddress srcMacAddress) { |
| 626 | checkNotNull(dstIpAddress); |
| 627 | checkNotNull(srcIpAddress); |
| 628 | checkNotNull(srcConnectPoint); |
| 629 | checkNotNull(srcMacAddress); |
| 630 | |
| 631 | // |
| 632 | // Step1: Try to update the existing intent first if it exists. |
| 633 | // |
| 634 | IpPrefix ipPrefix = null; |
| 635 | if (routingConfigurationService.isIpAddressLocal(dstIpAddress)) { |
| 636 | if (dstIpAddress.isIp4()) { |
| 637 | ipPrefix = IpPrefix.valueOf(dstIpAddress, |
| 638 | Ip4Address.BIT_LENGTH); |
| 639 | } else { |
| 640 | ipPrefix = IpPrefix.valueOf(dstIpAddress, |
| 641 | Ip6Address.BIT_LENGTH); |
| 642 | } |
| 643 | } else { |
| 644 | // Get IP prefix from BGP route table |
| 645 | RouteEntry routeEntry = getLongestMatchableRouteEntry(dstIpAddress); |
| 646 | if (routeEntry != null) { |
| 647 | ipPrefix = routeEntry.prefix(); |
| 648 | } |
| 649 | } |
| 650 | if (ipPrefix != null |
| 651 | && intentRequestListener.mp2pIntentExists(ipPrefix)) { |
| 652 | intentRequestListener.updateExistingMp2pIntent(ipPrefix, |
| 653 | srcConnectPoint); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | // |
| 658 | // Step2: There is no existing intent for the destination IP address. |
| 659 | // Check whether it is necessary to create a new one. If necessary then |
| 660 | // create a new one. |
| 661 | // |
| 662 | TrafficType trafficType = |
| 663 | trafficTypeClassifier(srcConnectPoint, dstIpAddress); |
| 664 | |
| 665 | switch (trafficType) { |
| 666 | case HOST_TO_INTERNET: |
| 667 | // If the destination IP address is outside the local SDN network. |
| 668 | // The Step 1 has already handled it. We do not need to do anything here. |
| 669 | break; |
| 670 | case INTERNET_TO_HOST: |
| 671 | intentRequestListener.setUpConnectivityInternetToHost(dstIpAddress); |
| 672 | break; |
| 673 | case HOST_TO_HOST: |
| 674 | intentRequestListener.setUpConnectivityHostToHost(dstIpAddress, |
| 675 | srcIpAddress, srcMacAddress, srcConnectPoint); |
| 676 | break; |
| 677 | case INTERNET_TO_INTERNET: |
| 678 | log.trace("This is transit traffic, " |
| 679 | + "the intent should be preinstalled already"); |
| 680 | break; |
| 681 | case DROP: |
| 682 | // TODO here should setUpDropPaccketIntent(...); |
| 683 | // We need a new type of intent here. |
| 684 | break; |
| 685 | case UNKNOWN: |
| 686 | log.trace("This is unknown traffic, so we do nothing"); |
| 687 | break; |
| 688 | default: |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Classifies the traffic and return the traffic type. |
| 695 | * |
| 696 | * @param srcConnectPoint the connect point where the packet comes from |
| 697 | * @param dstIp the destination IP address in packet |
| 698 | * @return the traffic type which this packet belongs to |
| 699 | */ |
| 700 | private TrafficType trafficTypeClassifier(ConnectPoint srcConnectPoint, |
| 701 | IpAddress dstIp) { |
| 702 | LocationType dstIpLocationType = getLocationType(dstIp); |
| 703 | Interface srcInterface = |
| 704 | routingConfigurationService.getInterface(srcConnectPoint); |
| 705 | |
| 706 | switch (dstIpLocationType) { |
| 707 | case INTERNET: |
| 708 | if (srcInterface == null) { |
| 709 | return TrafficType.HOST_TO_INTERNET; |
| 710 | } else { |
| 711 | return TrafficType.INTERNET_TO_INTERNET; |
| 712 | } |
| 713 | case LOCAL: |
| 714 | if (srcInterface == null) { |
| 715 | return TrafficType.HOST_TO_HOST; |
| 716 | } else { |
| 717 | // TODO Currently we only consider local public prefixes. |
| 718 | // In the future, we will consider the local private prefixes. |
| 719 | // If dstIpLocationType is a local private, we should return |
| 720 | // TrafficType.DROP. |
| 721 | return TrafficType.INTERNET_TO_HOST; |
| 722 | } |
| 723 | case NO_ROUTE: |
| 724 | return TrafficType.DROP; |
| 725 | default: |
| 726 | return TrafficType.UNKNOWN; |
| 727 | } |
| 728 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 729 | } |