blob: edf5b3f3a55d931f17088909357dc6f812730d8f [file] [log] [blame]
Saurav Dase6c448a2018-01-18 12:07:33 -08001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
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
7 *
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.
15 */
16
17package org.onosproject.segmentrouting;
18
Saurav Dasdc7f2752018-03-18 21:28:15 -070019import java.util.List;
Saurav Dase6c448a2018-01-18 12:07:33 -080020import java.util.Map;
21import java.util.Map.Entry;
22import java.util.Set;
23import java.util.concurrent.ConcurrentHashMap;
Saurav Das6430f412018-01-25 09:49:01 -080024import java.util.stream.Collectors;
25
Saurav Dasec683dc2018-04-27 18:42:30 -070026import org.onosproject.net.ConnectPoint;
Saurav Dase6c448a2018-01-18 12:07:33 -080027import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.HostLocation;
30import org.onosproject.net.Link;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.link.LinkService;
33import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
Saurav Dase6c448a2018-01-18 12:07:33 -080034import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
35import org.onosproject.store.service.EventuallyConsistentMap;
36import org.onosproject.store.service.EventuallyConsistentMapBuilder;
37import org.onosproject.store.service.WallClockTimestamp;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
Saurav Dasdc7f2752018-03-18 21:28:15 -070041import com.google.common.collect.ImmutableList;
Saurav Das6430f412018-01-25 09:49:01 -080042import com.google.common.collect.ImmutableMap;
Saurav Dase6c448a2018-01-18 12:07:33 -080043import com.google.common.collect.Sets;
44
Saurav Dasdc7f2752018-03-18 21:28:15 -070045
Saurav Dase6c448a2018-01-18 12:07:33 -080046public class LinkHandler {
47 private static final Logger log = LoggerFactory.getLogger(LinkHandler.class);
48 protected final SegmentRoutingManager srManager;
Saurav Dase6c448a2018-01-18 12:07:33 -080049
50 // Local store for all links seen and their present status, used for
Saurav Das6430f412018-01-25 09:49:01 -080051 // optimized routing. The existence of the link in the keys is enough to know
Saurav Dase6c448a2018-01-18 12:07:33 -080052 // if the link has been "seen-before" by this instance of the controller.
53 // The boolean value indicates if the link is currently up or not.
Saurav Das6430f412018-01-25 09:49:01 -080054 // Currently the optimized routing logic depends on "forgetting" a link
55 // when a switch goes down, but "remembering" it when only the link goes down.
Saurav Dase6c448a2018-01-18 12:07:33 -080056 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
Saurav Dasdc7f2752018-03-18 21:28:15 -070057 private Set<Link> seenBefore = Sets.newConcurrentHashSet();
Saurav Dase6c448a2018-01-18 12:07:33 -080058 private EventuallyConsistentMap<DeviceId, Set<PortNumber>> downedPortStore = null;
59
60 /**
61 * Constructs the LinkHandler.
62 *
63 * @param srManager Segment Routing manager
64 */
65 LinkHandler(SegmentRoutingManager srManager) {
66 this.srManager = srManager;
Saurav Dase6c448a2018-01-18 12:07:33 -080067 log.debug("Creating EC map downedportstore");
68 EventuallyConsistentMapBuilder<DeviceId, Set<PortNumber>> downedPortsMapBuilder
69 = srManager.storageService.eventuallyConsistentMapBuilder();
70 downedPortStore = downedPortsMapBuilder.withName("downedportstore")
71 .withSerializer(srManager.createSerializer())
72 .withTimestampProvider((k, v) -> new WallClockTimestamp())
73 .build();
74 log.trace("Current size {}", downedPortStore.size());
75 }
76
77 /**
78 * Constructs the LinkHandler for unit-testing.
79 *
80 * @param srManager SegmentRoutingManager
81 * @param linkService LinkService
82 */
83 LinkHandler(SegmentRoutingManager srManager, LinkService linkService) {
84 this.srManager = srManager;
Saurav Dase6c448a2018-01-18 12:07:33 -080085 }
86
87 /**
Saurav Dase321cff2018-02-09 17:26:45 -080088 * Initialize LinkHandler.
89 */
Charles Chanc12c3d02018-03-09 15:53:44 -080090 void init() {
Saurav Dase321cff2018-02-09 17:26:45 -080091 log.info("Loading stored links");
Charles Chanc12c3d02018-03-09 15:53:44 -080092 srManager.linkService.getActiveLinks().forEach(this::processLinkAdded);
Saurav Dase321cff2018-02-09 17:26:45 -080093 }
94
95 /**
Saurav Dase6c448a2018-01-18 12:07:33 -080096 * Preprocessing of added link before being sent for route-path handling.
97 * Also performs post processing of link.
98 *
99 * @param link the link to be processed
100 */
101 void processLinkAdded(Link link) {
102 log.info("** LINK ADDED {}", link.toString());
103 if (!isLinkValid(link)) {
104 return;
105 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800106 // Irrespective of whether the local is a MASTER or not for this device,
Saurav Das6430f412018-01-25 09:49:01 -0800107 // create group handler instance and push default TTP flow rules if needed,
Saurav Dase6c448a2018-01-18 12:07:33 -0800108 // as in a multi-instance setup, instances can initiate groups for any
Saurav Das97241862018-02-14 14:14:54 -0800109 // device. Also update local groupHandler stores.
Saurav Dase6c448a2018-01-18 12:07:33 -0800110 DefaultGroupHandler groupHandler = srManager.groupHandlerMap
Saurav Das97241862018-02-14 14:14:54 -0800111 .get(link.src().deviceId());
Saurav Dasdebcf882018-04-06 20:16:01 -0700112 if (groupHandler == null) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800113 Device device = srManager.deviceService.getDevice(link.src().deviceId());
114 if (device != null) {
Saurav Das97241862018-02-14 14:14:54 -0800115 log.warn("processLinkAdded: Link Added notification without "
116 + "Device Added event, still handling it");
Saurav Dase6c448a2018-01-18 12:07:33 -0800117 srManager.processDeviceAdded(device);
Saurav Dase6c448a2018-01-18 12:07:33 -0800118 }
119 }
Saurav Dasdc7f2752018-03-18 21:28:15 -0700120
121 if (isSeenLink(link)) {
122 // temp store retains previous state, just before the state is updated in
123 // seenLinks; previous state is necessary when processing the
124 // linkupdate in defaultRoutingHandler
125 seenBefore.add(link);
126 }
127 updateSeenLink(link, true);
128
Saurav Das97241862018-02-14 14:14:54 -0800129 if (srManager.deviceConfiguration == null ||
130 !srManager.deviceConfiguration.isConfigured(link.src().deviceId())) {
Saurav Das97241862018-02-14 14:14:54 -0800131 log.warn("Source device of this link is not configured.. "
132 + "not processing further");
133 return;
134 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800135
Saurav Dasdc7f2752018-03-18 21:28:15 -0700136 // process link only if it is bidirectional
137 if (!isBidirectionalLinkUp(link)) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800138 log.debug("Link not bidirectional.. waiting for other direction " +
Saurav Dasdc7f2752018-03-18 21:28:15 -0700139 "src {} --> dst {} ", link.dst(), link.src());
140 return;
141 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800142
Saurav Dasdebcf882018-04-06 20:16:01 -0700143 log.info("processing bidi links {} <--> {} UP", link.src(), link.dst());
144 // update groupHandler internal port state for both directions
145 List<Link> ulinks = getBidiComponentLinks(link);
146 for (Link ulink : ulinks) {
147 DefaultGroupHandler gh = srManager.groupHandlerMap
148 .get(ulink.src().deviceId());
149 if (gh != null) {
150 gh.portUpForLink(ulink);
151 }
152 }
153
154 for (Link ulink : ulinks) {
155 log.info("-- Starting optimized route-path processing for component "
Saurav Dasdc7f2752018-03-18 21:28:15 -0700156 + "unidirectional link {} --> {} UP", ulink.src(), ulink.dst());
157 srManager.defaultRoutingHandler
158 .populateRoutingRulesForLinkStatusChange(null, ulink, null,
159 seenBefore.contains(ulink));
Saurav Dase6c448a2018-01-18 12:07:33 -0800160
Saurav Dasdc7f2752018-03-18 21:28:15 -0700161 if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())) {
162 // handle edge-ports for dual-homed hosts
Saurav Dasec683dc2018-04-27 18:42:30 -0700163 updateHostPorts(ulink, true);
Saurav Dase6c448a2018-01-18 12:07:33 -0800164
Saurav Dasdc7f2752018-03-18 21:28:15 -0700165 // It's possible that linkUp causes no route-path change as ECMP graph does
166 // not change if the link is a parallel link (same src-dst as
167 // another link). However we still need to update ECMP hash
168 // groups to include new buckets for the link that has come up.
Saurav Dasdebcf882018-04-06 20:16:01 -0700169 DefaultGroupHandler gh = srManager.groupHandlerMap
170 .get(ulink.src().deviceId());
171 if (gh != null) {
Saurav Dasdc7f2752018-03-18 21:28:15 -0700172 if (!seenBefore.contains(ulink) && isParallelLink(ulink)) {
173 // if link seen first time, we need to ensure hash-groups have
174 // all ports
175 log.debug("Attempting retryHash for paralled first-time link {}",
176 ulink);
Saurav Dasdebcf882018-04-06 20:16:01 -0700177 gh.retryHash(ulink, false, true);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700178 } else {
179 // seen before-link
180 if (isParallelLink(ulink)) {
181 log.debug("Attempting retryHash for paralled seen-before "
182 + "link {}", ulink);
Saurav Dasdebcf882018-04-06 20:16:01 -0700183 gh.retryHash(ulink, false, false);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700184 }
Ray Milkey43969b92018-01-24 10:41:14 -0800185 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800186 }
187 }
Saurav Dasdc7f2752018-03-18 21:28:15 -0700188 //clean up temp state
189 seenBefore.remove(ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800190 }
Saurav Dasdc7f2752018-03-18 21:28:15 -0700191
Saurav Dase6c448a2018-01-18 12:07:33 -0800192 }
193
194 /**
195 * Preprocessing of removed link before being sent for route-path handling.
196 * Also performs post processing of link.
197 *
198 * @param link the link to be processed
199 */
200 void processLinkRemoved(Link link) {
201 log.info("** LINK REMOVED {}", link.toString());
202 if (!isLinkValid(link)) {
203 return;
204 }
205 // when removing links, update seen links first, before doing route-path
206 // changes
207 updateSeenLink(link, false);
Saurav Das6430f412018-01-25 09:49:01 -0800208 // handle edge-ports for dual-homed hosts
209 if (srManager.mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700210 updateHostPorts(link, false);
Saurav Das6430f412018-01-25 09:49:01 -0800211 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800212
213 // device availability check helps to ensure that multiple link-removed
214 // events are actually treated as a single switch removed event.
215 // purgeSeenLink is necessary so we do rerouting (instead of rehashing)
216 // when switch comes back.
217 if (link.src().elementId() instanceof DeviceId
218 && !srManager.deviceService.isAvailable(link.src().deviceId())) {
219 purgeSeenLink(link);
220 return;
221 }
222 if (link.dst().elementId() instanceof DeviceId
223 && !srManager.deviceService.isAvailable(link.dst().deviceId())) {
224 purgeSeenLink(link);
225 return;
226 }
227
Saurav Dasdc7f2752018-03-18 21:28:15 -0700228 // process link only if it is bidirectional
229 if (!isBidirectionalLinkDown(link)) {
230 log.debug("Link not bidirectional.. waiting for other direction " +
231 "src {} --> dst {} ", link.dst(), link.src());
232 return;
233 }
234 log.info("processing bidi links {} <--> {} DOWN", link.src(), link.dst());
Saurav Dase6c448a2018-01-18 12:07:33 -0800235
Saurav Dasdc7f2752018-03-18 21:28:15 -0700236 for (Link ulink : getBidiComponentLinks(link)) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700237 log.info("-- Starting optimized route-path processing for component "
Saurav Dasdc7f2752018-03-18 21:28:15 -0700238 + "unidirectional link {} --> {} DOWN", ulink.src(), ulink.dst());
239 srManager.defaultRoutingHandler
240 .populateRoutingRulesForLinkStatusChange(ulink, null, null, true);
241
242 // attempt rehashing for parallel links
243 DefaultGroupHandler groupHandler = srManager.groupHandlerMap
244 .get(ulink.src().deviceId());
245 if (groupHandler != null) {
246 if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())
247 && isParallelLink(ulink)) {
248 log.debug("* retrying hash for parallel link removed:{}", ulink);
249 groupHandler.retryHash(ulink, true, false);
250 } else {
251 log.debug("Not attempting retry-hash for link removed: {} .. {}",
252 ulink,
253 (srManager.mastershipService.isLocalMaster(ulink
254 .src().deviceId())) ? "not parallel"
255 : "not master");
256 }
257 // ensure local stores are updated after all rerouting or rehashing
258 groupHandler.portDownForLink(ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800259 } else {
Saurav Dasdc7f2752018-03-18 21:28:15 -0700260 log.warn("group handler not found for dev:{} when removing link: {}",
261 ulink.src().deviceId(), ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800262 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800263 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800264 }
265
266 /**
267 * Checks validity of link. Examples of invalid links include
268 * indirect-links, links between ports on the same switch, and more.
269 *
270 * @param link the link to be processed
271 * @return true if valid link
272 */
Pier Luigid8a15162018-02-15 16:33:08 +0100273 boolean isLinkValid(Link link) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800274 if (link.type() != Link.Type.DIRECT) {
275 // NOTE: A DIRECT link might be transiently marked as INDIRECT
276 // if BDDP is received before LLDP. We can safely ignore that
277 // until the LLDP is received and the link is marked as DIRECT.
278 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
279 link.src(), link.dst(), link.type());
280 return false;
281 }
282 DeviceId srcId = link.src().deviceId();
283 DeviceId dstId = link.dst().deviceId();
284 if (srcId.equals(dstId)) {
285 log.warn("Links between ports on the same switch are not "
286 + "allowed .. ignoring link {}", link);
287 return false;
288 }
289 DeviceConfiguration devConfig = srManager.deviceConfiguration;
Saurav Dase321cff2018-02-09 17:26:45 -0800290 if (devConfig == null) {
291 log.warn("Cannot check validity of link without device config");
292 return true;
293 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800294 try {
Saurav Das97241862018-02-14 14:14:54 -0800295 /*if (!devConfig.isEdgeDevice(srcId)
Saurav Dase6c448a2018-01-18 12:07:33 -0800296 && !devConfig.isEdgeDevice(dstId)) {
297 // ignore links between spines
298 // XXX revisit when handling multi-stage fabrics
299 log.warn("Links between spines not allowed...ignoring "
300 + "link {}", link);
301 return false;
Saurav Das97241862018-02-14 14:14:54 -0800302 }*/
Saurav Dase6c448a2018-01-18 12:07:33 -0800303 if (devConfig.isEdgeDevice(srcId)
304 && devConfig.isEdgeDevice(dstId)) {
305 // ignore links between leaves if they are not pair-links
306 // XXX revisit if removing pair-link config or allowing more than
307 // one pair-link
308 if (devConfig.getPairDeviceId(srcId).equals(dstId)
309 && devConfig.getPairLocalPort(srcId)
310 .equals(link.src().port())
311 && devConfig.getPairLocalPort(dstId)
312 .equals(link.dst().port())) {
313 // found pair link - allow it
314 return true;
315 } else {
316 log.warn("Links between leaves other than pair-links are "
317 + "not allowed...ignoring link {}", link);
318 return false;
319 }
320 }
321 } catch (DeviceConfigNotFoundException e) {
322 // We still want to count the links in seenLinks even though there
323 // is no config. So we let it return true
324 log.warn("Could not check validity of link {} as subtending devices "
325 + "are not yet configured", link);
326 }
327 return true;
328 }
329
330 /**
331 * Administratively enables or disables edge ports if the link that was
Saurav Dasec683dc2018-04-27 18:42:30 -0700332 * added or removed was the only uplink port from an edge device. Edge ports
333 * that belong to dual-homed hosts are always processed. In addition,
334 * single-homed host ports are optionally processed depending on the
335 * singleHomedDown property.
Saurav Dase6c448a2018-01-18 12:07:33 -0800336 *
337 * @param link the link to be processed
338 * @param added true if link was added, false if link was removed
339 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700340 private void updateHostPorts(Link link, boolean added) {
pier3a60ee92019-12-20 22:12:57 +0100341 DeviceConfiguration devConfig = srManager.deviceConfiguration;
Saurav Dase6c448a2018-01-18 12:07:33 -0800342 if (added) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700343 try {
344 if (!devConfig.isEdgeDevice(link.src().deviceId())
345 || devConfig.isEdgeDevice(link.dst().deviceId())) {
346 return;
347 }
348 } catch (DeviceConfigNotFoundException e) {
349 log.warn("Unable to determine if link is a valid uplink"
350 + e.getMessage());
351 }
352 // re-enable previously disabled ports on this edge-device if any
Saurav Dase6c448a2018-01-18 12:07:33 -0800353 Set<PortNumber> p = downedPortStore.remove(link.src().deviceId());
354 if (p != null) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700355 log.warn("Link src {} --> dst {} added is an edge-device uplink, "
356 + "enabling dual homed ports if any: {}", link.src().deviceId(),
357 link.dst().deviceId(), (p.isEmpty()) ? "no ports" : p);
Saurav Dase6c448a2018-01-18 12:07:33 -0800358 p.forEach(pnum -> srManager.deviceAdminService
359 .changePortState(link.src().deviceId(), pnum, true));
360 }
361 } else {
pier3a60ee92019-12-20 22:12:57 +0100362 // If the device does not have a pair device - skip
363 DeviceId dev = link.src().deviceId();
364 if (getPairDeviceIdOrNull(dev) == null) {
365 log.info("Device {} does not have pair device " +
366 "not disabling access port", dev);
367 return;
368 }
369 // Verify if last uplink
Saurav Dasdebcf882018-04-06 20:16:01 -0700370 if (!lastUplink(link)) {
371 return;
372 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800373 // find dual homed hosts on this dev to disable
Saurav Dasec683dc2018-04-27 18:42:30 -0700374 Set<PortNumber> dp = srManager.hostHandler
375 .getDualHomedHostPorts(dev);
376 log.warn("Link src {} --> dst {} removed was the last uplink, "
377 + "disabling dual homed ports: {}", dev,
378 link.dst().deviceId(), (dp.isEmpty()) ? "no ports" : dp);
379 dp.forEach(pnum -> srManager.deviceAdminService
380 .changePortState(dev, pnum, false));
381 if (srManager.singleHomedDown) {
382 // get all configured ports and down them if they haven't already
383 // been downed
384 srManager.deviceService.getPorts(dev).stream()
385 .filter(p -> p.isEnabled() && !dp.contains(p.number()))
386 .filter(p -> srManager.interfaceService
387 .isConfigured(new ConnectPoint(dev, p.number())))
388 .filter(p -> !srManager.deviceConfiguration
389 .isPairLocalPort(dev, p.number()))
390 .forEach(p -> {
391 log.warn("Last uplink gone src {} -> dst {} .. removing "
392 + "configured port {}", p.number());
393 srManager.deviceAdminService
394 .changePortState(dev, p.number(), false);
395 dp.add(p.number());
396 });
397 }
398 if (!dp.isEmpty()) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800399 // update global store
Saurav Dasec683dc2018-04-27 18:42:30 -0700400 Set<PortNumber> p = downedPortStore.get(dev);
Saurav Dase6c448a2018-01-18 12:07:33 -0800401 if (p == null) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700402 p = dp;
Saurav Dase6c448a2018-01-18 12:07:33 -0800403 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -0700404 p.addAll(dp);
Saurav Dase6c448a2018-01-18 12:07:33 -0800405 }
406 downedPortStore.put(link.src().deviceId(), p);
407 }
408 }
409 }
410
411 /**
Saurav Dasdebcf882018-04-06 20:16:01 -0700412 * Returns true if given link was the last active uplink from src-device of
Saurav Dase6c448a2018-01-18 12:07:33 -0800413 * link. An uplink is defined as a unidirectional link with src as
414 * edgeRouter and dst as non-edgeRouter.
415 *
416 * @param link
Saurav Dasdebcf882018-04-06 20:16:01 -0700417 * @return true if given link was the last uplink from the src device
Saurav Dase6c448a2018-01-18 12:07:33 -0800418 */
Saurav Dasdebcf882018-04-06 20:16:01 -0700419 private boolean lastUplink(Link link) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800420 DeviceConfiguration devConfig = srManager.deviceConfiguration;
421 try {
Saurav Das6430f412018-01-25 09:49:01 -0800422 if (!devConfig.isEdgeDevice(link.src().deviceId())
423 || devConfig.isEdgeDevice(link.dst().deviceId())) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800424 return false;
425 }
Saurav Das6430f412018-01-25 09:49:01 -0800426 // note that using linkservice here would cause race conditions as
427 // more links can show up while the app is still processing the first one
428 Set<Link> devLinks = seenLinks.entrySet().stream()
429 .filter(entry -> entry.getKey().src().deviceId()
430 .equals(link.src().deviceId()))
431 .filter(entry -> entry.getValue())
432 .filter(entry -> !entry.getKey().equals(link))
433 .map(entry -> entry.getKey())
434 .collect(Collectors.toSet());
435
Saurav Dase6c448a2018-01-18 12:07:33 -0800436 for (Link l : devLinks) {
Saurav Das6430f412018-01-25 09:49:01 -0800437 if (devConfig.isEdgeDevice(l.dst().deviceId())) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800438 continue;
439 }
Saurav Dasdebcf882018-04-06 20:16:01 -0700440 log.debug("Found another active uplink {}", l);
Saurav Das6430f412018-01-25 09:49:01 -0800441 return false;
Saurav Dase6c448a2018-01-18 12:07:33 -0800442 }
Saurav Dasdebcf882018-04-06 20:16:01 -0700443 log.debug("No active uplink found");
Saurav Das6430f412018-01-25 09:49:01 -0800444 return true;
Saurav Dase6c448a2018-01-18 12:07:33 -0800445 } catch (DeviceConfigNotFoundException e) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700446 log.warn("Unable to determine if link was the last uplink"
Saurav Dase6c448a2018-01-18 12:07:33 -0800447 + e.getMessage());
448 }
449 return false;
450 }
451
452 /**
453 * Returns true if this controller instance has seen this link before. The
454 * link may not be currently up, but as long as the link had been seen
455 * before this method will return true. The one exception is when the link
456 * was indeed seen before, but this controller instance was forced to forget
457 * it by a call to purgeSeenLink method.
458 *
459 * @param link the infrastructure link being queried
460 * @return true if this controller instance has seen this link before
461 */
462 boolean isSeenLink(Link link) {
463 return seenLinks.containsKey(link);
464 }
465
466 /**
467 * Updates the seen link store. Updates can be for links that are currently
468 * available or not.
469 *
470 * @param link the link to update in the seen-link local store
471 * @param up the status of the link, true if up, false if down
472 */
473 void updateSeenLink(Link link, boolean up) {
474 seenLinks.put(link, up);
475 }
476
477 /**
478 * Returns the status of a seen-link (up or down). If the link has not been
479 * seen-before, a null object is returned.
480 *
481 * @param link the infrastructure link being queried
482 * @return null if the link was not seen-before; true if the seen-link is
483 * up; false if the seen-link is down
484 */
485 private Boolean isSeenLinkUp(Link link) {
486 return seenLinks.get(link);
487 }
488
489 /**
490 * Makes this controller instance forget a previously seen before link.
491 *
492 * @param link the infrastructure link to purge
493 */
494 private void purgeSeenLink(Link link) {
495 seenLinks.remove(link);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700496 seenBefore.remove(link);
Saurav Dase6c448a2018-01-18 12:07:33 -0800497 }
498
499 /**
500 * Returns the status of a link as parallel link. A parallel link is defined
501 * as a link which has common src and dst switches as another seen-link that
502 * is currently enabled. It is not necessary for the link being queried to
503 * be a seen-link.
504 *
505 * @param link the infrastructure link being queried
506 * @return true if a seen-link exists that is up, and shares the same src
507 * and dst switches as the link being queried
508 */
509 private boolean isParallelLink(Link link) {
510 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
511 Link seenLink = seen.getKey();
512 if (seenLink.equals(link)) {
513 continue;
514 }
515 if (seenLink.src().deviceId().equals(link.src().deviceId())
516 && seenLink.dst().deviceId().equals(link.dst().deviceId())
517 && seen.getValue()) {
518 return true;
519 }
520 }
521 return false;
522 }
523
524 /**
Saurav Dasdc7f2752018-03-18 21:28:15 -0700525 * Returns true if the link being queried is a bidirectional link that is
526 * up. A bidi-link is defined as a component unidirectional link, whose
527 * reverse link - ie. the component unidirectional link in the reverse
528 * direction - has been seen-before and is up. It is NOT necessary for the
529 * link being queried to be a previously seen-link.
Saurav Dase6c448a2018-01-18 12:07:33 -0800530 *
Saurav Dasdc7f2752018-03-18 21:28:15 -0700531 * @param link the infrastructure (unidirectional) link being queried
Saurav Dase6c448a2018-01-18 12:07:33 -0800532 * @return true if another unidirectional link exists in the reverse
533 * direction, has been seen-before and is up
534 */
Saurav Dasdc7f2752018-03-18 21:28:15 -0700535 boolean isBidirectionalLinkUp(Link link) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700536 // cannot call linkService as link may be gone
537 Link reverseLink = getReverseLink(link);
Saurav Dase6c448a2018-01-18 12:07:33 -0800538 if (reverseLink == null) {
539 return false;
540 }
541 Boolean result = isSeenLinkUp(reverseLink);
542 if (result == null) {
543 return false;
544 }
545 return result.booleanValue();
546 }
547
548 /**
Saurav Dasdc7f2752018-03-18 21:28:15 -0700549 * Returns true if the link being queried is a bidirectional link that is
550 * down. A bidi-link is defined as a component unidirectional link, whose
551 * reverse link - i.e the component unidirectional link in the reverse
552 * direction - has been seen before and is down. It is necessary for the
553 * reverse-link to have been previously seen.
554 *
555 * @param link the infrastructure (unidirectional) link being queried
556 * @return true if another unidirectional link exists in the reverse
557 * direction, has been seen-before and is down
558 */
559 boolean isBidirectionalLinkDown(Link link) {
560 // cannot call linkService as link may be gone
561 Link reverseLink = getReverseLink(link);
562 if (reverseLink == null) {
563 log.warn("Query for bidi-link down but reverse-link not found "
564 + "for link {}", link);
565 return false;
566 }
567 Boolean result = seenLinks.get(reverseLink);
568 if (result == null) {
569 return false;
570 }
571 // if reverse link is seen UP (true), then its not bidi yet
572 return !result.booleanValue();
573 }
574
575 /**
576 * Returns the link in the reverse direction from the given link, by
577 * consulting the seen-links store.
578 *
579 * @param link the given link
580 * @return the reverse link or null
581 */
582 Link getReverseLink(Link link) {
583 return seenLinks.keySet().stream()
584 .filter(l -> l.src().equals(link.dst()) && l.dst().equals(link.src()))
585 .findAny()
586 .orElse(null);
587 }
588
589 /**
590 * Returns the component unidirectional links of a declared bidirectional
591 * link, by consulting the seen-links store. Caller is responsible for
592 * previously verifying bidirectionality. Returned list may be empty if
593 * errors are encountered.
594 *
595 * @param link the declared bidirectional link
596 * @return list of component unidirectional links
597 */
598 List<Link> getBidiComponentLinks(Link link) {
599 Link reverseLink = getReverseLink(link);
600 List<Link> componentLinks;
601 if (reverseLink == null) { // really should not happen if link is bidi
602 log.error("cannot find reverse link for given link: {} ... is it "
603 + "bi-directional?", link);
604 componentLinks = ImmutableList.of();
605 } else {
606 componentLinks = ImmutableList.of(reverseLink, link);
607 }
608 return componentLinks;
609 }
610
611 /**
Saurav Dase6c448a2018-01-18 12:07:33 -0800612 * Determines if the given link should be avoided in routing calculations by
613 * policy or design.
614 *
615 * @param link the infrastructure link being queried
616 * @return true if link should be avoided
617 */
618 boolean avoidLink(Link link) {
619 // XXX currently only avoids all pair-links. In the future can be
620 // extended to avoid any generic link
621 DeviceId src = link.src().deviceId();
622 PortNumber srcPort = link.src().port();
623 DeviceConfiguration devConfig = srManager.deviceConfiguration;
624 if (devConfig == null || !devConfig.isConfigured(src)) {
625 log.warn("Device {} not configured..cannot avoid link {}", src,
626 link);
627 return false;
628 }
629 DeviceId pairDev;
630 PortNumber pairLocalPort, pairRemotePort = null;
631 try {
632 pairDev = devConfig.getPairDeviceId(src);
633 pairLocalPort = devConfig.getPairLocalPort(src);
634 if (pairDev != null) {
635 pairRemotePort = devConfig
636 .getPairLocalPort(pairDev);
637 }
638 } catch (DeviceConfigNotFoundException e) {
639 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
640 src, link);
641 return false;
642 }
643
644 return srcPort.equals(pairLocalPort)
645 && link.dst().deviceId().equals(pairDev)
646 && link.dst().port().equals(pairRemotePort);
647 }
648
649 /**
650 * Cleans up internal LinkHandler stores.
651 *
652 * @param device the device that has been removed
653 */
654 void processDeviceRemoved(Device device) {
655 seenLinks.keySet()
656 .removeIf(key -> key.src().deviceId().equals(device.id())
657 || key.dst().deviceId().equals(device.id()));
658 }
659
660 /**
661 * Administratively disables the host location switchport if the edge device
Saurav Dasec683dc2018-04-27 18:42:30 -0700662 * has no viable uplinks. The caller needs to determine if such behavior is
663 * desired for the single or dual-homed host.
Saurav Dase6c448a2018-01-18 12:07:33 -0800664 *
Saurav Dasec683dc2018-04-27 18:42:30 -0700665 * @param loc the host location
Saurav Dase6c448a2018-01-18 12:07:33 -0800666 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700667 void checkUplinksForHost(HostLocation loc) {
pier3a60ee92019-12-20 22:12:57 +0100668 // If the device does not have a pair device - return
669 if (getPairDeviceIdOrNull(loc.deviceId()) == null) {
670 log.info("Device {} does not have pair device " +
671 "not disabling access port", loc.deviceId());
672 return;
673 }
674 // Verify link validity
Saurav Dase6c448a2018-01-18 12:07:33 -0800675 try {
676 for (Link l : srManager.linkService.getDeviceLinks(loc.deviceId())) {
677 if (srManager.deviceConfiguration.isEdgeDevice(l.dst().deviceId())
678 || l.state() == Link.State.INACTIVE) {
679 continue;
680 }
681 // found valid uplink - so, nothing to do
682 return;
683 }
684 } catch (DeviceConfigNotFoundException e) {
685 log.warn("Could not check for valid uplinks due to missing device"
686 + "config " + e.getMessage());
687 return;
688 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700689 log.warn("Host location {} has no valid uplinks disabling port", loc);
Saurav Dase6c448a2018-01-18 12:07:33 -0800690 srManager.deviceAdminService.changePortState(loc.deviceId(), loc.port(),
691 false);
692 Set<PortNumber> p = downedPortStore.get(loc.deviceId());
693 if (p == null) {
694 p = Sets.newHashSet(loc.port());
695 } else {
696 p.add(loc.port());
697 }
698 downedPortStore.put(loc.deviceId(), p);
699 }
700
pier3a60ee92019-12-20 22:12:57 +0100701 private DeviceId getPairDeviceIdOrNull(DeviceId deviceId) {
702 DeviceId pairDev;
703 try {
704 pairDev = srManager.deviceConfiguration.getPairDeviceId(deviceId);
705 } catch (DeviceConfigNotFoundException e) {
706 pairDev = null;
707 }
708 return pairDev;
709 }
710
Saurav Das6430f412018-01-25 09:49:01 -0800711 ImmutableMap<Link, Boolean> getSeenLinks() {
712 return ImmutableMap.copyOf(seenLinks);
713 }
714
715 ImmutableMap<DeviceId, Set<PortNumber>> getDownedPorts() {
716 return ImmutableMap.copyOf(downedPortStore.entrySet());
717 }
718
Saurav Dasdc7f2752018-03-18 21:28:15 -0700719 /**
720 * Returns all links that egress from given device that are UP in the
721 * seenLinks store. The returned links are also confirmed to be
722 * bidirectional.
723 *
724 * @param deviceId the device identifier
725 * @return set of egress links from the device
726 */
727 Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
728 return seenLinks.keySet().stream()
729 .filter(link -> link.src().deviceId().equals(deviceId))
730 .filter(link -> seenLinks.get(link))
731 .filter(link -> isBidirectionalLinkUp(link))
732 .collect(Collectors.toSet());
733 }
734
Saurav Dase6c448a2018-01-18 12:07:33 -0800735}