blob: c250fd13303f5ab84cd385d3c1fbe3736a5105b5 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart335ef462014-10-16 08:20:46 -070016package org.onlab.onos.sdnip;
17
Pingping3855f312014-10-22 12:50:37 -070018import java.util.Collection;
Pingping3855f312014-10-22 12:50:37 -070019import java.util.HashSet;
20import java.util.Iterator;
21import java.util.LinkedList;
22import java.util.List;
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080023import java.util.Map;
Pingping3855f312014-10-22 12:50:37 -070024import java.util.Set;
25import java.util.concurrent.BlockingQueue;
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080026import java.util.concurrent.ConcurrentHashMap;
Pingping3855f312014-10-22 12:50:37 -070027import java.util.concurrent.ExecutorService;
28import java.util.concurrent.Executors;
29import java.util.concurrent.LinkedBlockingQueue;
Pingping3855f312014-10-22 12:50:37 -070030
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080031import org.apache.commons.lang3.tuple.Pair;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070032import org.onlab.onos.core.ApplicationId;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070033import org.onlab.onos.net.ConnectPoint;
34import org.onlab.onos.net.Host;
35import org.onlab.onos.net.flow.DefaultTrafficSelector;
36import org.onlab.onos.net.flow.DefaultTrafficTreatment;
37import org.onlab.onos.net.flow.TrafficSelector;
38import org.onlab.onos.net.flow.TrafficTreatment;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070039import org.onlab.onos.net.host.HostEvent;
40import org.onlab.onos.net.host.HostListener;
41import org.onlab.onos.net.host.HostService;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070042import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
43import org.onlab.onos.sdnip.config.BgpPeer;
44import org.onlab.onos.sdnip.config.Interface;
Jonathan Hart9965d772014-12-02 10:28:34 -080045import org.onlab.onos.sdnip.config.SdnIpConfigurationService;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070046import org.onlab.packet.Ethernet;
47import org.onlab.packet.IpAddress;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080048import org.onlab.packet.Ip4Address;
49import org.onlab.packet.Ip4Prefix;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070050import org.onlab.packet.MacAddress;
51import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
53
Pingping3855f312014-10-22 12:50:37 -070054import com.google.common.collect.HashMultimap;
55import com.google.common.collect.Multimaps;
56import com.google.common.collect.SetMultimap;
57import com.google.common.util.concurrent.ThreadFactoryBuilder;
58import com.googlecode.concurrenttrees.common.KeyValuePair;
59import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
60import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
61import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
Jonathan Hart335ef462014-10-16 08:20:46 -070062
Jonathan Hart335ef462014-10-16 08:20:46 -070063/**
64 * This class processes BGP route update, translates each update into a intent
65 * and submits the intent.
Jonathan Hart335ef462014-10-16 08:20:46 -070066 */
67public class Router implements RouteListener {
68
69 private static final Logger log = LoggerFactory.getLogger(Router.class);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080070 // 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 Hart335ef462014-10-16 08:20:46 -070074
Jonathan Hart0b04bed2014-10-16 16:39:19 -070075 // Store all route updates in a radix tree.
76 // The key in this tree is the binary string of prefix of the route.
Jonathan Hart335ef462014-10-16 08:20:46 -070077 private InvertedRadixTree<RouteEntry> bgpRoutes;
78
79 // Stores all incoming route updates in a queue.
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080080 private final BlockingQueue<Collection<RouteUpdate>> routeUpdatesQueue;
Jonathan Hart335ef462014-10-16 08:20:46 -070081
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080082 // The Ip4Address is the next hop address of each route update.
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080083 private final SetMultimap<Ip4Address, RouteEntry> routesWaitingOnArp;
Jonathan Hart335ef462014-10-16 08:20:46 -070084
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080085 // The IPv4 address to MAC address mapping
86 private final Map<Ip4Address, MacAddress> ip2Mac;
87
Thomas Vachuskab97cf282014-10-20 23:31:12 -070088 private final ApplicationId appId;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080089 private final IntentSynchronizer intentSynchronizer;
90 private final HostService hostService;
Jonathan Hart9965d772014-12-02 10:28:34 -080091 private final SdnIpConfigurationService configService;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080092 private final InterfaceService interfaceService;
93 private final ExecutorService bgpUpdatesExecutor;
94 private final HostListener hostListener;
Jonathan Hart335ef462014-10-16 08:20:46 -070095
96 /**
97 * Class constructor.
98 *
Jonathan Hart31582d12014-10-22 13:52:41 -070099 * @param appId the application ID
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800100 * @param intentSynchronizer the intent synchronizer
Jonathan Hart31582d12014-10-22 13:52:41 -0700101 * @param configService the configuration service
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700102 * @param interfaceService the interface service
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800103 * @param hostService the host service
Jonathan Hart335ef462014-10-16 08:20:46 -0700104 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800105 public Router(ApplicationId appId, IntentSynchronizer intentSynchronizer,
Jonathan Hart9965d772014-12-02 10:28:34 -0800106 SdnIpConfigurationService configService,
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800107 InterfaceService interfaceService,
108 HostService hostService) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700109 this.appId = appId;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800110 this.intentSynchronizer = intentSynchronizer;
Jonathan Hart31582d12014-10-22 13:52:41 -0700111 this.configService = configService;
Jonathan Hart335ef462014-10-16 08:20:46 -0700112 this.interfaceService = interfaceService;
Pavlin Radoslavova7243cc2014-11-22 21:38:02 -0800113 this.hostService = hostService;
Jonathan Hart335ef462014-10-16 08:20:46 -0700114
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800115 this.hostListener = new InternalHostListener();
116
Jonathan Hart335ef462014-10-16 08:20:46 -0700117 bgpRoutes = new ConcurrentInvertedRadixTree<>(
118 new DefaultByteArrayNodeFactory());
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800119 routeUpdatesQueue = new LinkedBlockingQueue<>();
Jonathan Hart335ef462014-10-16 08:20:46 -0700120 routesWaitingOnArp = Multimaps.synchronizedSetMultimap(
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800121 HashMultimap.<Ip4Address, RouteEntry>create());
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800122 ip2Mac = new ConcurrentHashMap<>();
Jonathan Hart335ef462014-10-16 08:20:46 -0700123
124 bgpUpdatesExecutor = Executors.newSingleThreadExecutor(
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800125 new ThreadFactoryBuilder()
126 .setNameFormat("sdnip-bgp-updates-%d").build());
Jonathan Hart335ef462014-10-16 08:20:46 -0700127 }
128
129 /**
Jonathan Hart739c8352014-10-29 17:49:26 -0700130 * Starts the router.
Jonathan Hart335ef462014-10-16 08:20:46 -0700131 */
132 public void start() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800133 this.hostService.addListener(hostListener);
134
Jonathan Hart335ef462014-10-16 08:20:46 -0700135 bgpUpdatesExecutor.execute(new Runnable() {
136 @Override
137 public void run() {
138 doUpdatesThread();
139 }
140 });
Jonathan Hart335ef462014-10-16 08:20:46 -0700141 }
142
Jonathan Hart739c8352014-10-29 17:49:26 -0700143 /**
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800144 * Stops the router.
Jonathan Hart739c8352014-10-29 17:49:26 -0700145 */
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800146 public void stop() {
147 this.hostService.removeListener(hostListener);
148
149 // Stop the thread(s)
Jonathan Hart739c8352014-10-29 17:49:26 -0700150 bgpUpdatesExecutor.shutdownNow();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800151
152 synchronized (this) {
153 // Cleanup all local state
154 bgpRoutes = new ConcurrentInvertedRadixTree<>(
155 new DefaultByteArrayNodeFactory());
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800156 routeUpdatesQueue.clear();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800157 routesWaitingOnArp.clear();
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800158 ip2Mac.clear();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800159 }
Jonathan Hart739c8352014-10-29 17:49:26 -0700160 }
161
Jonathan Hart335ef462014-10-16 08:20:46 -0700162 @Override
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800163 public void update(Collection<RouteUpdate> routeUpdates) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700164 try {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800165 routeUpdatesQueue.put(routeUpdates);
Jonathan Hart335ef462014-10-16 08:20:46 -0700166 } catch (InterruptedException e) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800167 log.debug("Interrupted while putting on routeUpdatesQueue", e);
Jonathan Hart335ef462014-10-16 08:20:46 -0700168 Thread.currentThread().interrupt();
169 }
170 }
171
172 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700173 * Thread for handling route updates.
174 */
175 private void doUpdatesThread() {
176 boolean interrupted = false;
177 try {
178 while (!interrupted) {
179 try {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800180 Collection<RouteUpdate> routeUpdates =
181 routeUpdatesQueue.take();
182 processRouteUpdates(routeUpdates);
Jonathan Hart335ef462014-10-16 08:20:46 -0700183 } 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 Radoslavov248c2ae2014-12-02 09:51:25 -0800198 * 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 Hart335ef462014-10-16 08:20:46 -0700234 * Processes adding a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700235 * <p>
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800236 * 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 Vachuska4b420772014-10-30 16:46:17 -0700239 * </p>
Thomas Vachuska4b420772014-10-30 16:46:17 -0700240 * <p>
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800241 * NOTE: Currently, we don't handle routes if the next hop is within the
242 * SDN domain.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700243 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700244 *
245 * @param routeEntry the route entry to add
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800246 * @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 Hart335ef462014-10-16 08:20:46 -0700249 */
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800250 private MultiPointToSinglePointIntent processRouteAdd(
251 RouteEntry routeEntry,
252 Collection<Ip4Prefix> withdrawPrefixes) {
253 log.debug("Processing route add: {}", routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700254
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800255 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 Hart31582d12014-10-22 13:52:41 -0700290 hostService.startMonitoringIp(routeEntry.nextHop());
291
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800292 // 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 Hart335ef462014-10-16 08:20:46 -0700303 }
Jonathan Hart335ef462014-10-16 08:20:46 -0700304 if (nextHopMacAddress == null) {
305 routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800306 return null;
Jonathan Hart335ef462014-10-16 08:20:46 -0700307 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800308 return generateRouteIntent(routeEntry.prefix(), routeEntry.nextHop(),
309 nextHopMacAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700310 }
311
312 /**
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800313 * 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 Hart335ef462014-10-16 08:20:46 -0700319 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700320 * @param prefix IP prefix of the route to add
321 * @param nextHopIpAddress IP address of the next hop
Jonathan Hart335ef462014-10-16 08:20:46 -0700322 * @param nextHopMacAddress MAC address of the next hop
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800323 * @return the generated intent, or null if no intent should be submitted
Jonathan Hart335ef462014-10-16 08:20:46 -0700324 */
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800325 private MultiPointToSinglePointIntent generateRouteIntent(
326 Ip4Prefix prefix,
327 Ip4Address nextHopIpAddress,
328 MacAddress nextHopMacAddress) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700329
330 // Find the attachment point (egress interface) of the next hop
331 Interface egressInterface;
Jonathan Hart31582d12014-10-22 13:52:41 -0700332 if (configService.getBgpPeers().containsKey(nextHopIpAddress)) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700333 // Route to a peer
334 log.debug("Route to peer {}", nextHopIpAddress);
335 BgpPeer peer =
Jonathan Hart31582d12014-10-22 13:52:41 -0700336 configService.getBgpPeers().get(nextHopIpAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700337 egressInterface =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700338 interfaceService.getInterface(peer.connectPoint());
Jonathan Hart335ef462014-10-16 08:20:46 -0700339 } else {
340 // Route to non-peer
341 log.debug("Route to non-peer {}", nextHopIpAddress);
342 egressInterface =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700343 interfaceService.getMatchingInterface(nextHopIpAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700344 if (egressInterface == null) {
345 log.warn("No outgoing interface found for {}",
346 nextHopIpAddress);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800347 return null;
Jonathan Hart335ef462014-10-16 08:20:46 -0700348 }
349 }
350
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800351 //
352 // Generate the intent itself
353 //
Jonathan Hart335ef462014-10-16 08:20:46 -0700354 Set<ConnectPoint> ingressPorts = new HashSet<>();
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800355 ConnectPoint egressPort = egressInterface.connectPoint();
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800356 log.debug("Generating intent for prefix {}, next hop mac {}",
357 prefix, nextHopMacAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700358
359 for (Interface intf : interfaceService.getInterfaces()) {
Jonathan Hart2e3eef32014-11-12 11:05:40 -0800360 if (!intf.connectPoint().equals(egressInterface.connectPoint())) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700361 ConnectPoint srcPort = intf.connectPoint();
362 ingressPorts.add(srcPort);
363 }
364 }
365
366 // Match the destination IP prefix at the first hop
Jonathan Hart335ef462014-10-16 08:20:46 -0700367 TrafficSelector selector = DefaultTrafficSelector.builder()
368 .matchEthType(Ethernet.TYPE_IPV4)
369 .matchIPDst(prefix)
370 .build();
371
372 // Rewrite the destination MAC address
Jonathan Hart335ef462014-10-16 08:20:46 -0700373 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
374 .setEthDst(nextHopMacAddress)
375 .build();
376
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800377 return new MultiPointToSinglePointIntent(appId, selector, treatment,
378 ingressPorts, egressPort);
Jonathan Hart335ef462014-10-16 08:20:46 -0700379 }
380
381 /**
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800382 * Processes the deletion of a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700383 * <p>
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800384 * 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 Vachuska4b420772014-10-30 16:46:17 -0700387 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700388 *
389 * @param routeEntry the route entry to delete
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800390 * @param withdrawPrefixes the collection of accumulated prefixes whose
391 * intents will be withdrawn
Jonathan Hart335ef462014-10-16 08:20:46 -0700392 */
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800393 private void processRouteDelete(RouteEntry routeEntry,
394 Collection<Ip4Prefix> withdrawPrefixes) {
395 log.debug("Processing route delete: {}", routeEntry);
396 Ip4Prefix prefix = routeEntry.prefix();
Jonathan Hart335ef462014-10-16 08:20:46 -0700397
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800398 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 Hart335ef462014-10-16 08:20:46 -0700406 }
Jonathan Hart335ef462014-10-16 08:20:46 -0700407
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800408 routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry);
409 // TODO cancel the request in the ARP manager as well
Jonathan Hart335ef462014-10-16 08:20:46 -0700410 }
411
412 /**
Jonathan Hart31582d12014-10-22 13:52:41 -0700413 * 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 Hart335ef462014-10-16 08:20:46 -0700417 *
Jonathan Hart31582d12014-10-22 13:52:41 -0700418 * @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 Hart335ef462014-10-16 08:20:46 -0700420 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800421 private void updateMac(Ip4Address ipAddress, MacAddress macAddress) {
Jonathan Hart31582d12014-10-22 13:52:41 -0700422 log.debug("Received updated MAC info: {} => {}", ipAddress, macAddress);
423
424 // TODO here we should check whether the next hop for any of our
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800425 // installed prefixes has changed, not just prefixes pending
426 // installation.
Jonathan Hart335ef462014-10-16 08:20:46 -0700427
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700428 // 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 Hart335ef462014-10-16 08:20:46 -0700431 synchronized (this) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800432 Collection<Pair<Ip4Prefix, MultiPointToSinglePointIntent>>
433 submitIntents = new LinkedList<>();
434 MultiPointToSinglePointIntent intent;
Jonathan Hart335ef462014-10-16 08:20:46 -0700435
436 Set<RouteEntry> routesToPush =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700437 routesWaitingOnArp.removeAll(ipAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700438
439 for (RouteEntry routeEntry : routesToPush) {
440 // These will always be adds
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800441 Ip4Prefix prefix = routeEntry.prefix();
Jonathan Hart335ef462014-10-16 08:20:46 -0700442 String binaryString = RouteEntry.createBinaryString(prefix);
443 RouteEntry foundRouteEntry =
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700444 bgpRoutes.getValueForExactKey(binaryString);
Jonathan Hart335ef462014-10-16 08:20:46 -0700445 if (foundRouteEntry != null &&
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800446 foundRouteEntry.nextHop().equals(routeEntry.nextHop())) {
Jonathan Hart335ef462014-10-16 08:20:46 -0700447 // We only push prefix flows if the prefix is still in the
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700448 // radix tree and the next hop is the same as our
Jonathan Hart335ef462014-10-16 08:20:46 -0700449 // update.
450 // The prefix could have been removed while we were waiting
451 // for the ARP, or the next hop could have changed.
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800452 intent = generateRouteIntent(prefix, ipAddress,
453 macAddress);
454 if (intent != null) {
455 submitIntents.add(Pair.of(prefix, intent));
456 }
Jonathan Hart335ef462014-10-16 08:20:46 -0700457 } else {
Jonathan Hart31582d12014-10-22 13:52:41 -0700458 log.debug("{} has been revoked before the MAC was resolved",
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800459 routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700460 }
461 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800462
463 if (!submitIntents.isEmpty()) {
464 Collection<Ip4Prefix> withdrawPrefixes = new LinkedList<>();
465 intentSynchronizer.updateRouteIntents(submitIntents,
466 withdrawPrefixes);
467 }
468
469 ip2Mac.put(ipAddress, macAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700470 }
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 Hart335ef462014-10-16 08:20:46 -0700493 * Listener for host events.
494 */
495 class InternalHostListener implements HostListener {
496 @Override
497 public void event(HostEvent event) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800498 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 Radoslavov33f228a2014-10-27 19:33:16 -0700505 for (IpAddress ip : host.ipAddresses()) {
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800506 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 Hart335ef462014-10-16 08:20:46 -0700512 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800513 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 Hart335ef462014-10-16 08:20:46 -0700526 }
527 }
528 }
529}