Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip; |
| 17 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 19 | import java.util.HashSet; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 23 | import java.util.Map; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | import java.util.concurrent.BlockingQueue; |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 26 | import java.util.concurrent.ConcurrentHashMap; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 27 | import java.util.concurrent.ExecutorService; |
| 28 | import java.util.concurrent.Executors; |
| 29 | import java.util.concurrent.LinkedBlockingQueue; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 30 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 31 | import org.apache.commons.lang3.tuple.Pair; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 32 | import org.onlab.onos.core.ApplicationId; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.ConnectPoint; |
| 34 | import org.onlab.onos.net.Host; |
| 35 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 36 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 37 | import org.onlab.onos.net.flow.TrafficSelector; |
| 38 | import org.onlab.onos.net.flow.TrafficTreatment; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 39 | import org.onlab.onos.net.host.HostEvent; |
| 40 | import org.onlab.onos.net.host.HostListener; |
| 41 | import org.onlab.onos.net.host.HostService; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 42 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 43 | import org.onlab.onos.sdnip.config.BgpPeer; |
| 44 | import org.onlab.onos.sdnip.config.Interface; |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame^] | 45 | import org.onlab.onos.sdnip.config.SdnIpConfigurationService; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 46 | import org.onlab.packet.Ethernet; |
| 47 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 48 | import org.onlab.packet.Ip4Address; |
| 49 | import org.onlab.packet.Ip4Prefix; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 50 | import org.onlab.packet.MacAddress; |
| 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
| 53 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 54 | import com.google.common.collect.HashMultimap; |
| 55 | import com.google.common.collect.Multimaps; |
| 56 | import com.google.common.collect.SetMultimap; |
| 57 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 58 | import com.googlecode.concurrenttrees.common.KeyValuePair; |
| 59 | import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory; |
| 60 | import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree; |
| 61 | import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 62 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 63 | /** |
| 64 | * This class processes BGP route update, translates each update into a intent |
| 65 | * and submits the intent. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 66 | */ |
| 67 | public class Router implements RouteListener { |
| 68 | |
| 69 | private static final Logger log = LoggerFactory.getLogger(Router.class); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 70 | // For routes announced by local BGP daemon in SDN network, |
| 71 | // the next hop will be 0.0.0.0. |
| 72 | private static final Ip4Address LOCAL_NEXT_HOP = |
| 73 | Ip4Address.valueOf("0.0.0.0"); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 74 | |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 75 | // Store all route updates in a radix tree. |
| 76 | // The key in this tree is the binary string of prefix of the route. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 77 | private InvertedRadixTree<RouteEntry> bgpRoutes; |
| 78 | |
| 79 | // Stores all incoming route updates in a queue. |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 80 | private final BlockingQueue<Collection<RouteUpdate>> routeUpdatesQueue; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 81 | |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 82 | // The Ip4Address is the next hop address of each route update. |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 83 | private final SetMultimap<Ip4Address, RouteEntry> routesWaitingOnArp; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 84 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 85 | // The IPv4 address to MAC address mapping |
| 86 | private final Map<Ip4Address, MacAddress> ip2Mac; |
| 87 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 88 | private final ApplicationId appId; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 89 | private final IntentSynchronizer intentSynchronizer; |
| 90 | private final HostService hostService; |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame^] | 91 | private final SdnIpConfigurationService configService; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 92 | private final InterfaceService interfaceService; |
| 93 | private final ExecutorService bgpUpdatesExecutor; |
| 94 | private final HostListener hostListener; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Class constructor. |
| 98 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 99 | * @param appId the application ID |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 100 | * @param intentSynchronizer the intent synchronizer |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 101 | * @param configService the configuration service |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 102 | * @param interfaceService the interface service |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 103 | * @param hostService the host service |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 104 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 105 | public Router(ApplicationId appId, IntentSynchronizer intentSynchronizer, |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame^] | 106 | SdnIpConfigurationService configService, |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 107 | InterfaceService interfaceService, |
| 108 | HostService hostService) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 109 | this.appId = appId; |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 110 | this.intentSynchronizer = intentSynchronizer; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 111 | this.configService = configService; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 112 | this.interfaceService = interfaceService; |
Pavlin Radoslavov | a7243cc | 2014-11-22 21:38:02 -0800 | [diff] [blame] | 113 | this.hostService = hostService; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 114 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 115 | this.hostListener = new InternalHostListener(); |
| 116 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 117 | bgpRoutes = new ConcurrentInvertedRadixTree<>( |
| 118 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 119 | routeUpdatesQueue = new LinkedBlockingQueue<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 120 | routesWaitingOnArp = Multimaps.synchronizedSetMultimap( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 121 | HashMultimap.<Ip4Address, RouteEntry>create()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 122 | ip2Mac = new ConcurrentHashMap<>(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 123 | |
| 124 | bgpUpdatesExecutor = Executors.newSingleThreadExecutor( |
Pavlin Radoslavov | 8b75244 | 2014-11-18 14:34:37 -0800 | [diff] [blame] | 125 | new ThreadFactoryBuilder() |
| 126 | .setNameFormat("sdnip-bgp-updates-%d").build()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 130 | * Starts the router. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 131 | */ |
| 132 | public void start() { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 133 | this.hostService.addListener(hostListener); |
| 134 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 135 | bgpUpdatesExecutor.execute(new Runnable() { |
| 136 | @Override |
| 137 | public void run() { |
| 138 | doUpdatesThread(); |
| 139 | } |
| 140 | }); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 143 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 144 | * Stops the router. |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 145 | */ |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 146 | public void stop() { |
| 147 | this.hostService.removeListener(hostListener); |
| 148 | |
| 149 | // Stop the thread(s) |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 150 | bgpUpdatesExecutor.shutdownNow(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 151 | |
| 152 | synchronized (this) { |
| 153 | // Cleanup all local state |
| 154 | bgpRoutes = new ConcurrentInvertedRadixTree<>( |
| 155 | new DefaultByteArrayNodeFactory()); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 156 | routeUpdatesQueue.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 157 | routesWaitingOnArp.clear(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 158 | ip2Mac.clear(); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 159 | } |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 162 | @Override |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 163 | public void update(Collection<RouteUpdate> routeUpdates) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 164 | try { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 165 | routeUpdatesQueue.put(routeUpdates); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 166 | } catch (InterruptedException e) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 167 | log.debug("Interrupted while putting on routeUpdatesQueue", e); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 168 | Thread.currentThread().interrupt(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 173 | * Thread for handling route updates. |
| 174 | */ |
| 175 | private void doUpdatesThread() { |
| 176 | boolean interrupted = false; |
| 177 | try { |
| 178 | while (!interrupted) { |
| 179 | try { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 180 | Collection<RouteUpdate> routeUpdates = |
| 181 | routeUpdatesQueue.take(); |
| 182 | processRouteUpdates(routeUpdates); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 183 | } catch (InterruptedException e) { |
| 184 | log.debug("Interrupted while taking from updates queue", e); |
| 185 | interrupted = true; |
| 186 | } catch (Exception e) { |
| 187 | log.debug("exception", e); |
| 188 | } |
| 189 | } |
| 190 | } finally { |
| 191 | if (interrupted) { |
| 192 | Thread.currentThread().interrupt(); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 198 | * Processes route updates. |
| 199 | * |
| 200 | * @param routeUpdates the route updates to process |
| 201 | */ |
| 202 | void processRouteUpdates(Collection<RouteUpdate> routeUpdates) { |
| 203 | synchronized (this) { |
| 204 | Collection<Pair<Ip4Prefix, MultiPointToSinglePointIntent>> |
| 205 | submitIntents = new LinkedList<>(); |
| 206 | Collection<Ip4Prefix> withdrawPrefixes = new LinkedList<>(); |
| 207 | MultiPointToSinglePointIntent intent; |
| 208 | |
| 209 | for (RouteUpdate update : routeUpdates) { |
| 210 | switch (update.type()) { |
| 211 | case UPDATE: |
| 212 | intent = processRouteAdd(update.routeEntry(), |
| 213 | withdrawPrefixes); |
| 214 | if (intent != null) { |
| 215 | submitIntents.add(Pair.of(update.routeEntry().prefix(), |
| 216 | intent)); |
| 217 | } |
| 218 | break; |
| 219 | case DELETE: |
| 220 | processRouteDelete(update.routeEntry(), withdrawPrefixes); |
| 221 | break; |
| 222 | default: |
| 223 | log.error("Unknown update Type: {}", update.type()); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | intentSynchronizer.updateRouteIntents(submitIntents, |
| 229 | withdrawPrefixes); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 234 | * Processes adding a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 235 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 236 | * The route entry is added to the radix tree. If there was an existing |
| 237 | * next hop for this prefix, but the next hop was different, then the |
| 238 | * old route entry is deleted. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 239 | * </p> |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 240 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 241 | * NOTE: Currently, we don't handle routes if the next hop is within the |
| 242 | * SDN domain. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 243 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 244 | * |
| 245 | * @param routeEntry the route entry to add |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 246 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 247 | * intents will be withdrawn |
| 248 | * @return the corresponding intent that should be submitted, or null |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 249 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 250 | private MultiPointToSinglePointIntent processRouteAdd( |
| 251 | RouteEntry routeEntry, |
| 252 | Collection<Ip4Prefix> withdrawPrefixes) { |
| 253 | log.debug("Processing route add: {}", routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 254 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 255 | Ip4Prefix prefix = routeEntry.prefix(); |
| 256 | Ip4Address nextHop = null; |
| 257 | RouteEntry foundRouteEntry = |
| 258 | bgpRoutes.put(RouteEntry.createBinaryString(prefix), |
| 259 | routeEntry); |
| 260 | if (foundRouteEntry != null) { |
| 261 | nextHop = foundRouteEntry.nextHop(); |
| 262 | } |
| 263 | |
| 264 | if (nextHop != null && !nextHop.equals(routeEntry.nextHop())) { |
| 265 | // There was an existing nexthop for this prefix. This update |
| 266 | // supersedes that, so we need to remove the old flows for this |
| 267 | // prefix from the switches |
| 268 | withdrawPrefixes.add(routeEntry.prefix()); |
| 269 | } |
| 270 | if (nextHop != null && nextHop.equals(routeEntry.nextHop())) { |
| 271 | return null; |
| 272 | } |
| 273 | |
| 274 | if (routeEntry.nextHop().equals(LOCAL_NEXT_HOP)) { |
| 275 | // Route originated by SDN domain |
| 276 | // We don't handle these at the moment |
| 277 | log.debug("Own route {} to {}", |
| 278 | routeEntry.prefix(), routeEntry.nextHop()); |
| 279 | return null; |
| 280 | } |
| 281 | |
| 282 | // |
| 283 | // Find the MAC address of next hop router for this route entry. |
| 284 | // If the MAC address can not be found in ARP cache, then this prefix |
| 285 | // will be put in routesWaitingOnArp queue. Otherwise, generate |
| 286 | // a new route intent. |
| 287 | // |
| 288 | |
| 289 | // Monitor the IP address for updates of the MAC address |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 290 | hostService.startMonitoringIp(routeEntry.nextHop()); |
| 291 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 292 | // Check if we know the MAC address of the next hop |
| 293 | MacAddress nextHopMacAddress = ip2Mac.get(routeEntry.nextHop()); |
| 294 | if (nextHopMacAddress == null) { |
| 295 | Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop()); |
| 296 | if (!hosts.isEmpty()) { |
| 297 | // TODO how to handle if multiple hosts are returned? |
| 298 | nextHopMacAddress = hosts.iterator().next().mac(); |
| 299 | } |
| 300 | if (nextHopMacAddress != null) { |
| 301 | ip2Mac.put(routeEntry.nextHop(), nextHopMacAddress); |
| 302 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 303 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 304 | if (nextHopMacAddress == null) { |
| 305 | routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 306 | return null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 307 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 308 | return generateRouteIntent(routeEntry.prefix(), routeEntry.nextHop(), |
| 309 | nextHopMacAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 313 | * Generates a route intent for a prefix, the next hop IP address, and |
| 314 | * the next hop MAC address. |
| 315 | * <p/> |
| 316 | * This method will find the egress interface for the intent. |
| 317 | * Intent will match dst IP prefix and rewrite dst MAC address at all other |
| 318 | * border switches, then forward packets according to dst MAC address. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 319 | * |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 320 | * @param prefix IP prefix of the route to add |
| 321 | * @param nextHopIpAddress IP address of the next hop |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 322 | * @param nextHopMacAddress MAC address of the next hop |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 323 | * @return the generated intent, or null if no intent should be submitted |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 324 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 325 | private MultiPointToSinglePointIntent generateRouteIntent( |
| 326 | Ip4Prefix prefix, |
| 327 | Ip4Address nextHopIpAddress, |
| 328 | MacAddress nextHopMacAddress) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 329 | |
| 330 | // Find the attachment point (egress interface) of the next hop |
| 331 | Interface egressInterface; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 332 | if (configService.getBgpPeers().containsKey(nextHopIpAddress)) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 333 | // Route to a peer |
| 334 | log.debug("Route to peer {}", nextHopIpAddress); |
| 335 | BgpPeer peer = |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 336 | configService.getBgpPeers().get(nextHopIpAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 337 | egressInterface = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 338 | interfaceService.getInterface(peer.connectPoint()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 339 | } else { |
| 340 | // Route to non-peer |
| 341 | log.debug("Route to non-peer {}", nextHopIpAddress); |
| 342 | egressInterface = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 343 | interfaceService.getMatchingInterface(nextHopIpAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 344 | if (egressInterface == null) { |
| 345 | log.warn("No outgoing interface found for {}", |
| 346 | nextHopIpAddress); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 347 | return null; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 351 | // |
| 352 | // Generate the intent itself |
| 353 | // |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 354 | Set<ConnectPoint> ingressPorts = new HashSet<>(); |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 355 | ConnectPoint egressPort = egressInterface.connectPoint(); |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 356 | log.debug("Generating intent for prefix {}, next hop mac {}", |
| 357 | prefix, nextHopMacAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 358 | |
| 359 | for (Interface intf : interfaceService.getInterfaces()) { |
Jonathan Hart | 2e3eef3 | 2014-11-12 11:05:40 -0800 | [diff] [blame] | 360 | if (!intf.connectPoint().equals(egressInterface.connectPoint())) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 361 | ConnectPoint srcPort = intf.connectPoint(); |
| 362 | ingressPorts.add(srcPort); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // Match the destination IP prefix at the first hop |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 367 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 368 | .matchEthType(Ethernet.TYPE_IPV4) |
| 369 | .matchIPDst(prefix) |
| 370 | .build(); |
| 371 | |
| 372 | // Rewrite the destination MAC address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 373 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 374 | .setEthDst(nextHopMacAddress) |
| 375 | .build(); |
| 376 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 377 | return new MultiPointToSinglePointIntent(appId, selector, treatment, |
| 378 | ingressPorts, egressPort); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 382 | * Processes the deletion of a route entry. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 383 | * <p> |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 384 | * The prefix for the routing entry is removed from radix tree. |
| 385 | * If the operation is successful, the prefix is added to the collection |
| 386 | * of prefixes whose intents that will be withdrawn. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 387 | * </p> |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 388 | * |
| 389 | * @param routeEntry the route entry to delete |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 390 | * @param withdrawPrefixes the collection of accumulated prefixes whose |
| 391 | * intents will be withdrawn |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 392 | */ |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 393 | private void processRouteDelete(RouteEntry routeEntry, |
| 394 | Collection<Ip4Prefix> withdrawPrefixes) { |
| 395 | log.debug("Processing route delete: {}", routeEntry); |
| 396 | Ip4Prefix prefix = routeEntry.prefix(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 397 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 398 | if (bgpRoutes.remove(RouteEntry.createBinaryString(prefix))) { |
| 399 | // |
| 400 | // Only withdraw intents if an entry was actually removed from the |
| 401 | // tree. If no entry was removed, the <prefix, nexthop> wasn't |
| 402 | // there so it's probably already been removed and we don't |
| 403 | // need to do anything. |
| 404 | // |
| 405 | withdrawPrefixes.add(routeEntry.prefix()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 406 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 407 | |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 408 | routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry); |
| 409 | // TODO cancel the request in the ARP manager as well |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 413 | * Signals the Router that the MAC to IP mapping has potentially been |
| 414 | * updated. This has the effect of updating the MAC address for any |
| 415 | * installed prefixes if it has changed, as well as installing any pending |
| 416 | * prefixes that were waiting for MAC resolution. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 417 | * |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 418 | * @param ipAddress the IP address that an event was received for |
| 419 | * @param macAddress the most recently known MAC address for the IP address |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 420 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 421 | private void updateMac(Ip4Address ipAddress, MacAddress macAddress) { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 422 | log.debug("Received updated MAC info: {} => {}", ipAddress, macAddress); |
| 423 | |
| 424 | // TODO here we should check whether the next hop for any of our |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 425 | // installed prefixes has changed, not just prefixes pending |
| 426 | // installation. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 427 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 428 | // We synchronize on this to prevent changes to the radix tree |
| 429 | // while we're pushing intents. If the tree changes, the |
| 430 | // tree and intents could get out of sync. |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 431 | synchronized (this) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 432 | Collection<Pair<Ip4Prefix, MultiPointToSinglePointIntent>> |
| 433 | submitIntents = new LinkedList<>(); |
| 434 | MultiPointToSinglePointIntent intent; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 435 | |
| 436 | Set<RouteEntry> routesToPush = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 437 | routesWaitingOnArp.removeAll(ipAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 438 | |
| 439 | for (RouteEntry routeEntry : routesToPush) { |
| 440 | // These will always be adds |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 441 | Ip4Prefix prefix = routeEntry.prefix(); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 442 | String binaryString = RouteEntry.createBinaryString(prefix); |
| 443 | RouteEntry foundRouteEntry = |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 444 | bgpRoutes.getValueForExactKey(binaryString); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 445 | if (foundRouteEntry != null && |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 446 | foundRouteEntry.nextHop().equals(routeEntry.nextHop())) { |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 447 | // We only push prefix flows if the prefix is still in the |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 448 | // radix tree and the next hop is the same as our |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 449 | // update. |
| 450 | // The prefix could have been removed while we were waiting |
| 451 | // for the ARP, or the next hop could have changed. |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 452 | intent = generateRouteIntent(prefix, ipAddress, |
| 453 | macAddress); |
| 454 | if (intent != null) { |
| 455 | submitIntents.add(Pair.of(prefix, intent)); |
| 456 | } |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 457 | } else { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 458 | log.debug("{} has been revoked before the MAC was resolved", |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 459 | routeEntry); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 460 | } |
| 461 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 462 | |
| 463 | if (!submitIntents.isEmpty()) { |
| 464 | Collection<Ip4Prefix> withdrawPrefixes = new LinkedList<>(); |
| 465 | intentSynchronizer.updateRouteIntents(submitIntents, |
| 466 | withdrawPrefixes); |
| 467 | } |
| 468 | |
| 469 | ip2Mac.put(ipAddress, macAddress); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Gets the SDN-IP routes. |
| 475 | * |
| 476 | * @return the SDN-IP routes |
| 477 | */ |
| 478 | public Collection<RouteEntry> getRoutes() { |
| 479 | Iterator<KeyValuePair<RouteEntry>> it = |
| 480 | bgpRoutes.getKeyValuePairsForKeysStartingWith("").iterator(); |
| 481 | |
| 482 | List<RouteEntry> routes = new LinkedList<>(); |
| 483 | |
| 484 | while (it.hasNext()) { |
| 485 | KeyValuePair<RouteEntry> entry = it.next(); |
| 486 | routes.add(entry.getValue()); |
| 487 | } |
| 488 | |
| 489 | return routes; |
| 490 | } |
| 491 | |
| 492 | /** |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 493 | * Listener for host events. |
| 494 | */ |
| 495 | class InternalHostListener implements HostListener { |
| 496 | @Override |
| 497 | public void event(HostEvent event) { |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 498 | log.debug("Received HostEvent {}", event); |
| 499 | |
| 500 | Host host = event.subject(); |
| 501 | switch (event.type()) { |
| 502 | case HOST_ADDED: |
| 503 | // FALLTHROUGH |
| 504 | case HOST_UPDATED: |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 505 | for (IpAddress ip : host.ipAddresses()) { |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 506 | Ip4Address ip4Address = ip.getIp4Address(); |
| 507 | if (ip4Address == null) { |
| 508 | // TODO: For now we support only IPv4 |
| 509 | continue; |
| 510 | } |
| 511 | updateMac(ip4Address, host.mac()); |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 512 | } |
Pavlin Radoslavov | 248c2ae | 2014-12-02 09:51:25 -0800 | [diff] [blame] | 513 | break; |
| 514 | case HOST_REMOVED: |
| 515 | for (IpAddress ip : host.ipAddresses()) { |
| 516 | Ip4Address ip4Address = ip.getIp4Address(); |
| 517 | if (ip4Address == null) { |
| 518 | // TODO: For now we support only IPv4 |
| 519 | continue; |
| 520 | } |
| 521 | ip2Mac.remove(ip4Address); |
| 522 | } |
| 523 | break; |
| 524 | default: |
| 525 | break; |
Jonathan Hart | 335ef46 | 2014-10-16 08:20:46 -0700 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | } |
| 529 | } |