blob: ddbc5eeda5ebb348dae9ca0f36f1927b1a345243 [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 Hart2da1e602015-02-18 19:09:24 -080016package org.onosproject.routing.impl;
Jonathan Hart335ef462014-10-16 08:20:46 -070017
Jonathan Hart552e31f2015-02-06 11:11:59 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.Multimaps;
20import com.google.common.collect.SetMultimap;
21import com.google.common.util.concurrent.ThreadFactoryBuilder;
22import com.googlecode.concurrenttrees.common.KeyValuePair;
23import com.googlecode.concurrenttrees.radix.node.concrete.DefaultByteArrayNodeFactory;
24import com.googlecode.concurrenttrees.radixinverted.ConcurrentInvertedRadixTree;
25import com.googlecode.concurrenttrees.radixinverted.InvertedRadixTree;
Jonathan Hart41349e92015-02-09 14:14:02 -080026import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.apache.felix.scr.annotations.Service;
Jonathan Hart552e31f2015-02-06 11:11:59 -080032import org.onlab.packet.Ip4Address;
33import org.onlab.packet.IpAddress;
34import org.onlab.packet.IpPrefix;
35import org.onlab.packet.MacAddress;
36import org.onosproject.net.Host;
37import org.onosproject.net.host.HostEvent;
38import org.onosproject.net.host.HostListener;
39import org.onosproject.net.host.HostService;
Jonathan Hart2da1e602015-02-18 19:09:24 -080040import org.onosproject.routing.BgpService;
41import org.onosproject.routing.FibEntry;
42import org.onosproject.routing.FibListener;
43import org.onosproject.routing.FibUpdate;
44import org.onosproject.routing.RouteEntry;
45import org.onosproject.routing.RouteListener;
46import org.onosproject.routing.RouteUpdate;
47import org.onosproject.routing.RoutingService;
Jonathan Hart552e31f2015-02-06 11:11:59 -080048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Pingping3855f312014-10-22 12:50:37 -070051import java.util.Collection;
Jonathan Hart552e31f2015-02-06 11:11:59 -080052import java.util.Collections;
Pingping3855f312014-10-22 12:50:37 -070053import java.util.Iterator;
54import java.util.LinkedList;
55import java.util.List;
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080056import java.util.Map;
Pingping3855f312014-10-22 12:50:37 -070057import java.util.Set;
58import java.util.concurrent.BlockingQueue;
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080059import java.util.concurrent.ConcurrentHashMap;
Pingping3855f312014-10-22 12:50:37 -070060import java.util.concurrent.ExecutorService;
61import java.util.concurrent.Executors;
62import java.util.concurrent.LinkedBlockingQueue;
Pingping3855f312014-10-22 12:50:37 -070063
Jonathan Hart41349e92015-02-09 14:14:02 -080064import static com.google.common.base.Preconditions.checkNotNull;
65
Jonathan Hart335ef462014-10-16 08:20:46 -070066/**
Jonathan Hart552e31f2015-02-06 11:11:59 -080067 * This class processes route updates and maintains a Routing Information Base
68 * (RIB). After route updates have been processed and next hops have been
69 * resolved, FIB updates are sent to any listening FIB components.
Jonathan Hart335ef462014-10-16 08:20:46 -070070 */
Jonathan Hart41349e92015-02-09 14:14:02 -080071@Component(immediate = true)
72@Service
73public class Router implements RoutingService {
Jonathan Hart335ef462014-10-16 08:20:46 -070074
75 private static final Logger log = LoggerFactory.getLogger(Router.class);
76
Jonathan Hart552e31f2015-02-06 11:11:59 -080077 // Route entries are stored in a radix tree.
Jonathan Hart0b04bed2014-10-16 16:39:19 -070078 // The key in this tree is the binary string of prefix of the route.
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080079 private InvertedRadixTree<RouteEntry> ribTable4;
80 private InvertedRadixTree<RouteEntry> ribTable6;
Jonathan Hart335ef462014-10-16 08:20:46 -070081
82 // Stores all incoming route updates in a queue.
Jonathan Hart41349e92015-02-09 14:14:02 -080083 private final BlockingQueue<Collection<RouteUpdate>> routeUpdatesQueue
84 = new LinkedBlockingQueue<>();
Jonathan Hart335ef462014-10-16 08:20:46 -070085
Jonathan Hart552e31f2015-02-06 11:11:59 -080086 // Next-hop IP address to route entry mapping for next hops pending MAC resolution
Jonathan Hart41349e92015-02-09 14:14:02 -080087 private SetMultimap<IpAddress, RouteEntry> routesWaitingOnArp;
Jonathan Hart335ef462014-10-16 08:20:46 -070088
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080089 // The IPv4 address to MAC address mapping
Jonathan Hart41349e92015-02-09 14:14:02 -080090 private final Map<IpAddress, MacAddress> ip2Mac = new ConcurrentHashMap<>();
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -080091
Jonathan Hart41349e92015-02-09 14:14:02 -080092 private FibListener fibComponent;
Jonathan Hart335ef462014-10-16 08:20:46 -070093
Jonathan Hart41349e92015-02-09 14:14:02 -080094 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected HostService hostService;
Jonathan Hart335ef462014-10-16 08:20:46 -070096
Jonathan Hart41349e92015-02-09 14:14:02 -080097 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected BgpService bgpService;
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -080099
Jonathan Hart41349e92015-02-09 14:14:02 -0800100 private ExecutorService bgpUpdatesExecutor;
101 private final HostListener hostListener = new InternalHostListener();
102
103 @Activate
104 public void activate() {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800105 ribTable4 = new ConcurrentInvertedRadixTree<>(
106 new DefaultByteArrayNodeFactory());
107 ribTable6 = new ConcurrentInvertedRadixTree<>(
Jonathan Hart335ef462014-10-16 08:20:46 -0700108 new DefaultByteArrayNodeFactory());
Jonathan Hart335ef462014-10-16 08:20:46 -0700109 routesWaitingOnArp = Multimaps.synchronizedSetMultimap(
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800110 HashMultimap.<IpAddress, RouteEntry>create());
Jonathan Hart335ef462014-10-16 08:20:46 -0700111
112 bgpUpdatesExecutor = Executors.newSingleThreadExecutor(
Pavlin Radoslavov8b752442014-11-18 14:34:37 -0800113 new ThreadFactoryBuilder()
114 .setNameFormat("sdnip-bgp-updates-%d").build());
Jonathan Hart335ef462014-10-16 08:20:46 -0700115 }
116
Jonathan Hart41349e92015-02-09 14:14:02 -0800117 @Deactivate
118 public void deactivate() {
119 log.debug("Stopped");
120 }
121
122 @Override
123 public void start(FibListener listener) {
124 this.fibComponent = checkNotNull(listener);
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800125 this.hostService.addListener(hostListener);
126
Jonathan Hartd24fafb2015-02-09 17:55:32 -0800127 bgpService.start(new InternalRouteListener());
Jonathan Hart41349e92015-02-09 14:14:02 -0800128
Jonathan Hart335ef462014-10-16 08:20:46 -0700129 bgpUpdatesExecutor.execute(new Runnable() {
130 @Override
131 public void run() {
132 doUpdatesThread();
133 }
134 });
Jonathan Hart335ef462014-10-16 08:20:46 -0700135 }
136
Jonathan Hart41349e92015-02-09 14:14:02 -0800137 @Override
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800138 public void stop() {
Jonathan Hart41349e92015-02-09 14:14:02 -0800139 bgpService.stop();
140
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800141 this.hostService.removeListener(hostListener);
142
143 // Stop the thread(s)
Jonathan Hart739c8352014-10-29 17:49:26 -0700144 bgpUpdatesExecutor.shutdownNow();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800145
146 synchronized (this) {
147 // Cleanup all local state
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800148 ribTable4 = new ConcurrentInvertedRadixTree<>(
149 new DefaultByteArrayNodeFactory());
150 ribTable6 = new ConcurrentInvertedRadixTree<>(
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800151 new DefaultByteArrayNodeFactory());
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800152 routeUpdatesQueue.clear();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800153 routesWaitingOnArp.clear();
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800154 ip2Mac.clear();
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800155 }
Jonathan Hart739c8352014-10-29 17:49:26 -0700156 }
157
Jonathan Hart41349e92015-02-09 14:14:02 -0800158 /**
159 * Entry point for route updates.
160 *
161 * @param routeUpdates collection of route updates to process
162 */
163 private 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 Radoslavov3a0a52e2015-01-06 17:41:37 -0800198 * Gets all IPv4 routes from the RIB.
199 *
200 * @return all IPv4 routes from the RIB
201 */
202 public Collection<RouteEntry> getRoutes4() {
203 Iterator<KeyValuePair<RouteEntry>> it =
204 ribTable4.getKeyValuePairsForKeysStartingWith("").iterator();
205
206 List<RouteEntry> routes = new LinkedList<>();
207
208 while (it.hasNext()) {
209 KeyValuePair<RouteEntry> entry = it.next();
210 routes.add(entry.getValue());
211 }
212
213 return routes;
214 }
215
216 /**
217 * Gets all IPv6 routes from the RIB.
218 *
219 * @return all IPv6 routes from the RIB
220 */
221 public Collection<RouteEntry> getRoutes6() {
222 Iterator<KeyValuePair<RouteEntry>> it =
223 ribTable6.getKeyValuePairsForKeysStartingWith("").iterator();
224
225 List<RouteEntry> routes = new LinkedList<>();
226
227 while (it.hasNext()) {
228 KeyValuePair<RouteEntry> entry = it.next();
229 routes.add(entry.getValue());
230 }
231
232 return routes;
233 }
234
235 /**
236 * Finds a route in the RIB for a prefix. The prefix can be either IPv4 or
237 * IPv6.
238 *
239 * @param prefix the prefix to use
240 * @return the route if found, otherwise null
241 */
242 RouteEntry findRibRoute(IpPrefix prefix) {
243 String binaryString = RouteEntry.createBinaryString(prefix);
244 if (prefix.version() == Ip4Address.VERSION) {
245 // IPv4
246 return ribTable4.getValueForExactKey(binaryString);
247 }
248 // IPv6
249 return ribTable6.getValueForExactKey(binaryString);
250 }
251
252 /**
253 * Adds a route to the RIB. The route can be either IPv4 or IPv6.
254 *
255 * @param routeEntry the route entry to use
256 */
257 void addRibRoute(RouteEntry routeEntry) {
258 if (routeEntry.prefix().version() == Ip4Address.VERSION) {
259 // IPv4
260 ribTable4.put(RouteEntry.createBinaryString(routeEntry.prefix()),
261 routeEntry);
262 } else {
263 // IPv6
264 ribTable6.put(RouteEntry.createBinaryString(routeEntry.prefix()),
265 routeEntry);
266 }
267 }
268
269 /**
270 * Removes a route for a prefix from the RIB. The prefix can be either IPv4
271 * or IPv6.
272 *
273 * @param prefix the prefix to use
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800274 * @return true if the route was found and removed, otherwise false
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800275 */
276 boolean removeRibRoute(IpPrefix prefix) {
277 if (prefix.version() == Ip4Address.VERSION) {
278 // IPv4
279 return ribTable4.remove(RouteEntry.createBinaryString(prefix));
280 }
281 // IPv6
282 return ribTable6.remove(RouteEntry.createBinaryString(prefix));
283 }
284
285 /**
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800286 * Processes route updates.
287 *
288 * @param routeUpdates the route updates to process
289 */
290 void processRouteUpdates(Collection<RouteUpdate> routeUpdates) {
291 synchronized (this) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800292 Collection<IpPrefix> withdrawPrefixes = new LinkedList<>();
Jonathan Hart552e31f2015-02-06 11:11:59 -0800293 Collection<FibUpdate> fibUpdates = new LinkedList<>();
294 Collection<FibUpdate> fibWithdraws = new LinkedList<>();
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800295
296 for (RouteUpdate update : routeUpdates) {
297 switch (update.type()) {
298 case UPDATE:
Jonathan Hart552e31f2015-02-06 11:11:59 -0800299 FibEntry fib = processRouteAdd(update.routeEntry(),
300 withdrawPrefixes);
301 if (fib != null) {
302 fibUpdates.add(new FibUpdate(FibUpdate.Type.UPDATE, fib));
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800303 }
Jonathan Hart552e31f2015-02-06 11:11:59 -0800304
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800305 break;
306 case DELETE:
307 processRouteDelete(update.routeEntry(), withdrawPrefixes);
Jonathan Hart552e31f2015-02-06 11:11:59 -0800308
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800309 break;
310 default:
311 log.error("Unknown update Type: {}", update.type());
312 break;
313 }
314 }
315
Jonathan Hart552e31f2015-02-06 11:11:59 -0800316 withdrawPrefixes.forEach(p -> fibWithdraws.add(new FibUpdate(
317 FibUpdate.Type.DELETE, new FibEntry(p, null, null))));
318
Jonathan Hart41349e92015-02-09 14:14:02 -0800319 if (!fibUpdates.isEmpty() || !fibWithdraws.isEmpty()) {
320 fibComponent.update(fibUpdates, fibWithdraws);
321 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800322 }
323 }
324
325 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700326 * Processes adding a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700327 * <p>
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800328 * The route entry is added to the radix tree. If there was an existing
329 * next hop for this prefix, but the next hop was different, then the
330 * old route entry is deleted.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700331 * </p>
Thomas Vachuska4b420772014-10-30 16:46:17 -0700332 * <p>
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800333 * NOTE: Currently, we don't handle routes if the next hop is within the
334 * SDN domain.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700335 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700336 *
337 * @param routeEntry the route entry to add
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800338 * @param withdrawPrefixes the collection of accumulated prefixes whose
339 * intents will be withdrawn
Jonathan Hart552e31f2015-02-06 11:11:59 -0800340 * @return the corresponding FIB entry change, or null
Jonathan Hart335ef462014-10-16 08:20:46 -0700341 */
Jonathan Hart552e31f2015-02-06 11:11:59 -0800342 private FibEntry processRouteAdd(
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800343 RouteEntry routeEntry,
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800344 Collection<IpPrefix> withdrawPrefixes) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800345 log.debug("Processing route add: {}", routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700346
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800347 // Find the old next-hop if we are updating an old route entry
348 IpAddress oldNextHop = null;
349 RouteEntry oldRouteEntry = findRibRoute(routeEntry.prefix());
350 if (oldRouteEntry != null) {
351 oldNextHop = oldRouteEntry.nextHop();
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800352 }
353
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800354 // Add the new route to the RIB
355 addRibRoute(routeEntry);
356
357 if (oldNextHop != null) {
358 if (oldNextHop.equals(routeEntry.nextHop())) {
359 return null; // No change
360 }
361 //
362 // Update an existing nexthop for the prefix.
363 // We need to remove the old flows for this prefix from the
364 // switches before the new flows are added.
365 //
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800366 withdrawPrefixes.add(routeEntry.prefix());
367 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800368
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800369 if (routeEntry.nextHop().isZero()) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800370 // Route originated by SDN domain
371 // We don't handle these at the moment
372 log.debug("Own route {} to {}",
373 routeEntry.prefix(), routeEntry.nextHop());
374 return null;
375 }
376
377 //
378 // Find the MAC address of next hop router for this route entry.
379 // If the MAC address can not be found in ARP cache, then this prefix
380 // will be put in routesWaitingOnArp queue. Otherwise, generate
381 // a new route intent.
382 //
383
384 // Monitor the IP address for updates of the MAC address
Jonathan Hart31582d12014-10-22 13:52:41 -0700385 hostService.startMonitoringIp(routeEntry.nextHop());
386
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800387 // Check if we know the MAC address of the next hop
388 MacAddress nextHopMacAddress = ip2Mac.get(routeEntry.nextHop());
389 if (nextHopMacAddress == null) {
390 Set<Host> hosts = hostService.getHostsByIp(routeEntry.nextHop());
391 if (!hosts.isEmpty()) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800392 nextHopMacAddress = hosts.iterator().next().mac();
393 }
394 if (nextHopMacAddress != null) {
395 ip2Mac.put(routeEntry.nextHop(), nextHopMacAddress);
396 }
Jonathan Hart335ef462014-10-16 08:20:46 -0700397 }
Jonathan Hart335ef462014-10-16 08:20:46 -0700398 if (nextHopMacAddress == null) {
399 routesWaitingOnArp.put(routeEntry.nextHop(), routeEntry);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800400 return null;
Jonathan Hart335ef462014-10-16 08:20:46 -0700401 }
Jonathan Hart552e31f2015-02-06 11:11:59 -0800402 return new FibEntry(routeEntry.prefix(), routeEntry.nextHop(),
403 nextHopMacAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700404 }
405
406 /**
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800407 * Processes the deletion of a route entry.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700408 * <p>
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800409 * The prefix for the routing entry is removed from radix tree.
410 * If the operation is successful, the prefix is added to the collection
411 * of prefixes whose intents that will be withdrawn.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700412 * </p>
Jonathan Hart335ef462014-10-16 08:20:46 -0700413 *
414 * @param routeEntry the route entry to delete
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800415 * @param withdrawPrefixes the collection of accumulated prefixes whose
416 * intents will be withdrawn
Jonathan Hart335ef462014-10-16 08:20:46 -0700417 */
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800418 private void processRouteDelete(RouteEntry routeEntry,
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800419 Collection<IpPrefix> withdrawPrefixes) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800420 log.debug("Processing route delete: {}", routeEntry);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800421 boolean isRemoved = removeRibRoute(routeEntry.prefix());
Jonathan Hart335ef462014-10-16 08:20:46 -0700422
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800423 if (isRemoved) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800424 //
425 // Only withdraw intents if an entry was actually removed from the
426 // tree. If no entry was removed, the <prefix, nexthop> wasn't
427 // there so it's probably already been removed and we don't
428 // need to do anything.
429 //
430 withdrawPrefixes.add(routeEntry.prefix());
Jonathan Hart335ef462014-10-16 08:20:46 -0700431 }
Jonathan Hart335ef462014-10-16 08:20:46 -0700432
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800433 routesWaitingOnArp.remove(routeEntry.nextHop(), routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700434 }
435
436 /**
Jonathan Hart31582d12014-10-22 13:52:41 -0700437 * Signals the Router that the MAC to IP mapping has potentially been
438 * updated. This has the effect of updating the MAC address for any
439 * installed prefixes if it has changed, as well as installing any pending
440 * prefixes that were waiting for MAC resolution.
Jonathan Hart335ef462014-10-16 08:20:46 -0700441 *
Jonathan Hart31582d12014-10-22 13:52:41 -0700442 * @param ipAddress the IP address that an event was received for
443 * @param macAddress the most recently known MAC address for the IP address
Jonathan Hart335ef462014-10-16 08:20:46 -0700444 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800445 private void updateMac(IpAddress ipAddress, MacAddress macAddress) {
446 log.debug("Received updated MAC info: {} => {}", ipAddress,
447 macAddress);
Jonathan Hart31582d12014-10-22 13:52:41 -0700448
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800449 //
450 // We synchronize on "this" to prevent changes to the Radix tree
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700451 // while we're pushing intents. If the tree changes, the
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800452 // tree and the intents could get out of sync.
453 //
Jonathan Hart335ef462014-10-16 08:20:46 -0700454 synchronized (this) {
Jonathan Hart552e31f2015-02-06 11:11:59 -0800455 Collection<FibUpdate> submitFibEntries = new LinkedList<>();
Jonathan Hart335ef462014-10-16 08:20:46 -0700456
457 Set<RouteEntry> routesToPush =
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800458 routesWaitingOnArp.removeAll(ipAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700459
460 for (RouteEntry routeEntry : routesToPush) {
461 // These will always be adds
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800462 RouteEntry foundRouteEntry = findRibRoute(routeEntry.prefix());
Jonathan Hart335ef462014-10-16 08:20:46 -0700463 if (foundRouteEntry != null &&
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800464 foundRouteEntry.nextHop().equals(routeEntry.nextHop())) {
Jonathan Hart552e31f2015-02-06 11:11:59 -0800465 // We only push FIB updates if the prefix is still in the
466 // radix tree and the next hop is the same as our entry.
Jonathan Hart335ef462014-10-16 08:20:46 -0700467 // The prefix could have been removed while we were waiting
468 // for the ARP, or the next hop could have changed.
Jonathan Hart552e31f2015-02-06 11:11:59 -0800469 submitFibEntries.add(new FibUpdate(FibUpdate.Type.UPDATE,
470 new FibEntry(routeEntry.prefix(),
471 ipAddress, macAddress)));
Jonathan Hart335ef462014-10-16 08:20:46 -0700472 } else {
Jonathan Hart31582d12014-10-22 13:52:41 -0700473 log.debug("{} has been revoked before the MAC was resolved",
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800474 routeEntry);
Jonathan Hart335ef462014-10-16 08:20:46 -0700475 }
476 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800477
Jonathan Hart552e31f2015-02-06 11:11:59 -0800478 if (!submitFibEntries.isEmpty()) {
479 fibComponent.update(submitFibEntries, Collections.emptyList());
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800480 }
481
482 ip2Mac.put(ipAddress, macAddress);
Jonathan Hart335ef462014-10-16 08:20:46 -0700483 }
484 }
485
486 /**
Jonathan Hart335ef462014-10-16 08:20:46 -0700487 * Listener for host events.
488 */
489 class InternalHostListener implements HostListener {
490 @Override
491 public void event(HostEvent event) {
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800492 log.debug("Received HostEvent {}", event);
493
494 Host host = event.subject();
495 switch (event.type()) {
496 case HOST_ADDED:
497 // FALLTHROUGH
498 case HOST_UPDATED:
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800499 for (IpAddress ipAddress : host.ipAddresses()) {
500 updateMac(ipAddress, host.mac());
Jonathan Hart335ef462014-10-16 08:20:46 -0700501 }
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800502 break;
503 case HOST_REMOVED:
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800504 for (IpAddress ipAddress : host.ipAddresses()) {
505 ip2Mac.remove(ipAddress);
Pavlin Radoslavov248c2ae2014-12-02 09:51:25 -0800506 }
507 break;
508 default:
509 break;
Jonathan Hart335ef462014-10-16 08:20:46 -0700510 }
511 }
512 }
Jonathan Hart41349e92015-02-09 14:14:02 -0800513
514 /**
515 * Listener for route events.
516 */
517 private class InternalRouteListener implements RouteListener {
518 @Override
519 public void update(Collection<RouteUpdate> routeUpdates) {
520 Router.this.update(routeUpdates);
521 }
522 }
Jonathan Hart335ef462014-10-16 08:20:46 -0700523}