blob: 62ac1c0faa20cb23ba90bf2e3f7db36cc973fc54 [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());
pierventre6e184b92021-05-28 12:21:19 +0200157 // Performs the seenBefore optimization iff we have seen before both links in that case
158 // we have programmed the group (unless there were major issues in the system)
Saurav Dasdc7f2752018-03-18 21:28:15 -0700159 srManager.defaultRoutingHandler
160 .populateRoutingRulesForLinkStatusChange(null, ulink, null,
pierventre6e184b92021-05-28 12:21:19 +0200161 (seenBefore.contains(ulink) && seenBefore.contains(getReverseLink(ulink))));
Saurav Dase6c448a2018-01-18 12:07:33 -0800162
Saurav Dasdc7f2752018-03-18 21:28:15 -0700163 if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())) {
164 // handle edge-ports for dual-homed hosts
Saurav Dasec683dc2018-04-27 18:42:30 -0700165 updateHostPorts(ulink, true);
Saurav Dase6c448a2018-01-18 12:07:33 -0800166
Saurav Dasdc7f2752018-03-18 21:28:15 -0700167 // It's possible that linkUp causes no route-path change as ECMP graph does
168 // not change if the link is a parallel link (same src-dst as
169 // another link). However we still need to update ECMP hash
170 // groups to include new buckets for the link that has come up.
Saurav Dasdebcf882018-04-06 20:16:01 -0700171 DefaultGroupHandler gh = srManager.groupHandlerMap
172 .get(ulink.src().deviceId());
173 if (gh != null) {
Saurav Dasdc7f2752018-03-18 21:28:15 -0700174 if (!seenBefore.contains(ulink) && isParallelLink(ulink)) {
175 // if link seen first time, we need to ensure hash-groups have
176 // all ports
177 log.debug("Attempting retryHash for paralled first-time link {}",
178 ulink);
Saurav Dasdebcf882018-04-06 20:16:01 -0700179 gh.retryHash(ulink, false, true);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700180 } else {
181 // seen before-link
182 if (isParallelLink(ulink)) {
183 log.debug("Attempting retryHash for paralled seen-before "
184 + "link {}", ulink);
Saurav Dasdebcf882018-04-06 20:16:01 -0700185 gh.retryHash(ulink, false, false);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700186 }
Ray Milkey43969b92018-01-24 10:41:14 -0800187 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800188 }
189 }
Saurav Dasdc7f2752018-03-18 21:28:15 -0700190 //clean up temp state
191 seenBefore.remove(ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800192 }
Saurav Dasdc7f2752018-03-18 21:28:15 -0700193
Saurav Dase6c448a2018-01-18 12:07:33 -0800194 }
195
196 /**
197 * Preprocessing of removed link before being sent for route-path handling.
198 * Also performs post processing of link.
199 *
200 * @param link the link to be processed
201 */
202 void processLinkRemoved(Link link) {
203 log.info("** LINK REMOVED {}", link.toString());
204 if (!isLinkValid(link)) {
205 return;
206 }
207 // when removing links, update seen links first, before doing route-path
pierventrebd6eefa2021-05-14 22:06:50 +0200208 // changes. Link should be already there because was up before. If not,
209 // we should not update the store. This could be a LINK DOWN happening
210 // after a DEVICE DOWN event.
211 if (isSeenLink(link)) {
212 updateSeenLink(link, false);
213 } else {
214 // Exiting here may not be needed since other checks later should fail
215 log.warn("received a link down for the link {} which is not in the store", link);
216 }
Saurav Das6430f412018-01-25 09:49:01 -0800217 // handle edge-ports for dual-homed hosts
218 if (srManager.mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700219 updateHostPorts(link, false);
Saurav Das6430f412018-01-25 09:49:01 -0800220 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800221
222 // device availability check helps to ensure that multiple link-removed
223 // events are actually treated as a single switch removed event.
224 // purgeSeenLink is necessary so we do rerouting (instead of rehashing)
225 // when switch comes back.
226 if (link.src().elementId() instanceof DeviceId
227 && !srManager.deviceService.isAvailable(link.src().deviceId())) {
228 purgeSeenLink(link);
229 return;
230 }
231 if (link.dst().elementId() instanceof DeviceId
232 && !srManager.deviceService.isAvailable(link.dst().deviceId())) {
233 purgeSeenLink(link);
234 return;
235 }
236
Saurav Dasdc7f2752018-03-18 21:28:15 -0700237 // process link only if it is bidirectional
238 if (!isBidirectionalLinkDown(link)) {
239 log.debug("Link not bidirectional.. waiting for other direction " +
240 "src {} --> dst {} ", link.dst(), link.src());
241 return;
242 }
243 log.info("processing bidi links {} <--> {} DOWN", link.src(), link.dst());
Saurav Dase6c448a2018-01-18 12:07:33 -0800244
Saurav Dasdc7f2752018-03-18 21:28:15 -0700245 for (Link ulink : getBidiComponentLinks(link)) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700246 log.info("-- Starting optimized route-path processing for component "
Saurav Dasdc7f2752018-03-18 21:28:15 -0700247 + "unidirectional link {} --> {} DOWN", ulink.src(), ulink.dst());
248 srManager.defaultRoutingHandler
249 .populateRoutingRulesForLinkStatusChange(ulink, null, null, true);
250
251 // attempt rehashing for parallel links
252 DefaultGroupHandler groupHandler = srManager.groupHandlerMap
253 .get(ulink.src().deviceId());
254 if (groupHandler != null) {
255 if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())
256 && isParallelLink(ulink)) {
257 log.debug("* retrying hash for parallel link removed:{}", ulink);
258 groupHandler.retryHash(ulink, true, false);
259 } else {
260 log.debug("Not attempting retry-hash for link removed: {} .. {}",
261 ulink,
262 (srManager.mastershipService.isLocalMaster(ulink
263 .src().deviceId())) ? "not parallel"
264 : "not master");
265 }
266 // ensure local stores are updated after all rerouting or rehashing
267 groupHandler.portDownForLink(ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800268 } else {
Saurav Dasdc7f2752018-03-18 21:28:15 -0700269 log.warn("group handler not found for dev:{} when removing link: {}",
270 ulink.src().deviceId(), ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800271 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800272 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800273 }
274
275 /**
276 * Checks validity of link. Examples of invalid links include
277 * indirect-links, links between ports on the same switch, and more.
278 *
279 * @param link the link to be processed
280 * @return true if valid link
281 */
Pier Luigid8a15162018-02-15 16:33:08 +0100282 boolean isLinkValid(Link link) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800283 if (link.type() != Link.Type.DIRECT) {
284 // NOTE: A DIRECT link might be transiently marked as INDIRECT
285 // if BDDP is received before LLDP. We can safely ignore that
286 // until the LLDP is received and the link is marked as DIRECT.
287 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
288 link.src(), link.dst(), link.type());
289 return false;
290 }
291 DeviceId srcId = link.src().deviceId();
292 DeviceId dstId = link.dst().deviceId();
293 if (srcId.equals(dstId)) {
294 log.warn("Links between ports on the same switch are not "
295 + "allowed .. ignoring link {}", link);
296 return false;
297 }
298 DeviceConfiguration devConfig = srManager.deviceConfiguration;
Saurav Dase321cff2018-02-09 17:26:45 -0800299 if (devConfig == null) {
300 log.warn("Cannot check validity of link without device config");
301 return true;
302 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800303 try {
Saurav Das97241862018-02-14 14:14:54 -0800304 /*if (!devConfig.isEdgeDevice(srcId)
Saurav Dase6c448a2018-01-18 12:07:33 -0800305 && !devConfig.isEdgeDevice(dstId)) {
306 // ignore links between spines
307 // XXX revisit when handling multi-stage fabrics
308 log.warn("Links between spines not allowed...ignoring "
309 + "link {}", link);
310 return false;
Saurav Das97241862018-02-14 14:14:54 -0800311 }*/
Saurav Dase6c448a2018-01-18 12:07:33 -0800312 if (devConfig.isEdgeDevice(srcId)
313 && devConfig.isEdgeDevice(dstId)) {
314 // ignore links between leaves if they are not pair-links
315 // XXX revisit if removing pair-link config or allowing more than
316 // one pair-link
317 if (devConfig.getPairDeviceId(srcId).equals(dstId)
318 && devConfig.getPairLocalPort(srcId)
319 .equals(link.src().port())
320 && devConfig.getPairLocalPort(dstId)
321 .equals(link.dst().port())) {
322 // found pair link - allow it
323 return true;
324 } else {
325 log.warn("Links between leaves other than pair-links are "
326 + "not allowed...ignoring link {}", link);
327 return false;
328 }
329 }
330 } catch (DeviceConfigNotFoundException e) {
331 // We still want to count the links in seenLinks even though there
332 // is no config. So we let it return true
333 log.warn("Could not check validity of link {} as subtending devices "
334 + "are not yet configured", link);
335 }
336 return true;
337 }
338
339 /**
340 * Administratively enables or disables edge ports if the link that was
Saurav Dasec683dc2018-04-27 18:42:30 -0700341 * added or removed was the only uplink port from an edge device. Edge ports
342 * that belong to dual-homed hosts are always processed. In addition,
343 * single-homed host ports are optionally processed depending on the
344 * singleHomedDown property.
Saurav Dase6c448a2018-01-18 12:07:33 -0800345 *
346 * @param link the link to be processed
347 * @param added true if link was added, false if link was removed
348 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700349 private void updateHostPorts(Link link, boolean added) {
pier3a60ee92019-12-20 22:12:57 +0100350 DeviceConfiguration devConfig = srManager.deviceConfiguration;
Saurav Dase6c448a2018-01-18 12:07:33 -0800351 if (added) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700352 try {
353 if (!devConfig.isEdgeDevice(link.src().deviceId())
354 || devConfig.isEdgeDevice(link.dst().deviceId())) {
355 return;
356 }
357 } catch (DeviceConfigNotFoundException e) {
358 log.warn("Unable to determine if link is a valid uplink"
359 + e.getMessage());
360 }
361 // re-enable previously disabled ports on this edge-device if any
Saurav Dase6c448a2018-01-18 12:07:33 -0800362 Set<PortNumber> p = downedPortStore.remove(link.src().deviceId());
363 if (p != null) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700364 log.warn("Link src {} --> dst {} added is an edge-device uplink, "
365 + "enabling dual homed ports if any: {}", link.src().deviceId(),
366 link.dst().deviceId(), (p.isEmpty()) ? "no ports" : p);
Saurav Dase6c448a2018-01-18 12:07:33 -0800367 p.forEach(pnum -> srManager.deviceAdminService
368 .changePortState(link.src().deviceId(), pnum, true));
369 }
370 } else {
pier3a60ee92019-12-20 22:12:57 +0100371 // If the device does not have a pair device - skip
372 DeviceId dev = link.src().deviceId();
373 if (getPairDeviceIdOrNull(dev) == null) {
374 log.info("Device {} does not have pair device " +
375 "not disabling access port", dev);
376 return;
377 }
378 // Verify if last uplink
Saurav Dasdebcf882018-04-06 20:16:01 -0700379 if (!lastUplink(link)) {
380 return;
381 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800382 // find dual homed hosts on this dev to disable
Saurav Dasec683dc2018-04-27 18:42:30 -0700383 Set<PortNumber> dp = srManager.hostHandler
384 .getDualHomedHostPorts(dev);
385 log.warn("Link src {} --> dst {} removed was the last uplink, "
386 + "disabling dual homed ports: {}", dev,
387 link.dst().deviceId(), (dp.isEmpty()) ? "no ports" : dp);
388 dp.forEach(pnum -> srManager.deviceAdminService
389 .changePortState(dev, pnum, false));
390 if (srManager.singleHomedDown) {
391 // get all configured ports and down them if they haven't already
392 // been downed
393 srManager.deviceService.getPorts(dev).stream()
394 .filter(p -> p.isEnabled() && !dp.contains(p.number()))
395 .filter(p -> srManager.interfaceService
396 .isConfigured(new ConnectPoint(dev, p.number())))
397 .filter(p -> !srManager.deviceConfiguration
398 .isPairLocalPort(dev, p.number()))
399 .forEach(p -> {
400 log.warn("Last uplink gone src {} -> dst {} .. removing "
401 + "configured port {}", p.number());
402 srManager.deviceAdminService
403 .changePortState(dev, p.number(), false);
404 dp.add(p.number());
405 });
406 }
407 if (!dp.isEmpty()) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800408 // update global store
Saurav Dasec683dc2018-04-27 18:42:30 -0700409 Set<PortNumber> p = downedPortStore.get(dev);
Saurav Dase6c448a2018-01-18 12:07:33 -0800410 if (p == null) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700411 p = dp;
Saurav Dase6c448a2018-01-18 12:07:33 -0800412 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -0700413 p.addAll(dp);
Saurav Dase6c448a2018-01-18 12:07:33 -0800414 }
415 downedPortStore.put(link.src().deviceId(), p);
416 }
417 }
418 }
419
420 /**
Saurav Dasdebcf882018-04-06 20:16:01 -0700421 * Returns true if given link was the last active uplink from src-device of
Saurav Dase6c448a2018-01-18 12:07:33 -0800422 * link. An uplink is defined as a unidirectional link with src as
423 * edgeRouter and dst as non-edgeRouter.
424 *
425 * @param link
Saurav Dasdebcf882018-04-06 20:16:01 -0700426 * @return true if given link was the last uplink from the src device
Saurav Dase6c448a2018-01-18 12:07:33 -0800427 */
Saurav Dasdebcf882018-04-06 20:16:01 -0700428 private boolean lastUplink(Link link) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800429 DeviceConfiguration devConfig = srManager.deviceConfiguration;
430 try {
Saurav Das6430f412018-01-25 09:49:01 -0800431 if (!devConfig.isEdgeDevice(link.src().deviceId())
432 || devConfig.isEdgeDevice(link.dst().deviceId())) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800433 return false;
434 }
Saurav Das6430f412018-01-25 09:49:01 -0800435 // note that using linkservice here would cause race conditions as
436 // more links can show up while the app is still processing the first one
437 Set<Link> devLinks = seenLinks.entrySet().stream()
438 .filter(entry -> entry.getKey().src().deviceId()
439 .equals(link.src().deviceId()))
440 .filter(entry -> entry.getValue())
441 .filter(entry -> !entry.getKey().equals(link))
442 .map(entry -> entry.getKey())
443 .collect(Collectors.toSet());
444
Saurav Dase6c448a2018-01-18 12:07:33 -0800445 for (Link l : devLinks) {
Saurav Das6430f412018-01-25 09:49:01 -0800446 if (devConfig.isEdgeDevice(l.dst().deviceId())) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800447 continue;
448 }
Saurav Dasdebcf882018-04-06 20:16:01 -0700449 log.debug("Found another active uplink {}", l);
Saurav Das6430f412018-01-25 09:49:01 -0800450 return false;
Saurav Dase6c448a2018-01-18 12:07:33 -0800451 }
Saurav Dasdebcf882018-04-06 20:16:01 -0700452 log.debug("No active uplink found");
Saurav Das6430f412018-01-25 09:49:01 -0800453 return true;
Saurav Dase6c448a2018-01-18 12:07:33 -0800454 } catch (DeviceConfigNotFoundException e) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700455 log.warn("Unable to determine if link was the last uplink"
Saurav Dase6c448a2018-01-18 12:07:33 -0800456 + e.getMessage());
457 }
458 return false;
459 }
460
461 /**
462 * Returns true if this controller instance has seen this link before. The
463 * link may not be currently up, but as long as the link had been seen
464 * before this method will return true. The one exception is when the link
465 * was indeed seen before, but this controller instance was forced to forget
466 * it by a call to purgeSeenLink method.
467 *
468 * @param link the infrastructure link being queried
469 * @return true if this controller instance has seen this link before
470 */
471 boolean isSeenLink(Link link) {
472 return seenLinks.containsKey(link);
473 }
474
475 /**
476 * Updates the seen link store. Updates can be for links that are currently
477 * available or not.
478 *
479 * @param link the link to update in the seen-link local store
480 * @param up the status of the link, true if up, false if down
481 */
482 void updateSeenLink(Link link, boolean up) {
483 seenLinks.put(link, up);
484 }
485
486 /**
487 * Returns the status of a seen-link (up or down). If the link has not been
488 * seen-before, a null object is returned.
489 *
490 * @param link the infrastructure link being queried
491 * @return null if the link was not seen-before; true if the seen-link is
492 * up; false if the seen-link is down
493 */
494 private Boolean isSeenLinkUp(Link link) {
495 return seenLinks.get(link);
496 }
497
498 /**
499 * Makes this controller instance forget a previously seen before link.
500 *
501 * @param link the infrastructure link to purge
502 */
503 private void purgeSeenLink(Link link) {
504 seenLinks.remove(link);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700505 seenBefore.remove(link);
Saurav Dase6c448a2018-01-18 12:07:33 -0800506 }
507
508 /**
509 * Returns the status of a link as parallel link. A parallel link is defined
510 * as a link which has common src and dst switches as another seen-link that
511 * is currently enabled. It is not necessary for the link being queried to
512 * be a seen-link.
513 *
514 * @param link the infrastructure link being queried
515 * @return true if a seen-link exists that is up, and shares the same src
516 * and dst switches as the link being queried
517 */
518 private boolean isParallelLink(Link link) {
519 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
520 Link seenLink = seen.getKey();
521 if (seenLink.equals(link)) {
522 continue;
523 }
524 if (seenLink.src().deviceId().equals(link.src().deviceId())
525 && seenLink.dst().deviceId().equals(link.dst().deviceId())
526 && seen.getValue()) {
527 return true;
528 }
529 }
530 return false;
531 }
532
533 /**
Saurav Dasdc7f2752018-03-18 21:28:15 -0700534 * Returns true if the link being queried is a bidirectional link that is
535 * up. A bidi-link is defined as a component unidirectional link, whose
536 * reverse link - ie. the component unidirectional link in the reverse
537 * direction - has been seen-before and is up. It is NOT necessary for the
538 * link being queried to be a previously seen-link.
Saurav Dase6c448a2018-01-18 12:07:33 -0800539 *
Saurav Dasdc7f2752018-03-18 21:28:15 -0700540 * @param link the infrastructure (unidirectional) link being queried
Saurav Dase6c448a2018-01-18 12:07:33 -0800541 * @return true if another unidirectional link exists in the reverse
542 * direction, has been seen-before and is up
543 */
Saurav Dasdc7f2752018-03-18 21:28:15 -0700544 boolean isBidirectionalLinkUp(Link link) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700545 // cannot call linkService as link may be gone
546 Link reverseLink = getReverseLink(link);
Saurav Dase6c448a2018-01-18 12:07:33 -0800547 if (reverseLink == null) {
548 return false;
549 }
550 Boolean result = isSeenLinkUp(reverseLink);
551 if (result == null) {
552 return false;
553 }
554 return result.booleanValue();
555 }
556
557 /**
Saurav Dasdc7f2752018-03-18 21:28:15 -0700558 * Returns true if the link being queried is a bidirectional link that is
559 * down. A bidi-link is defined as a component unidirectional link, whose
560 * reverse link - i.e the component unidirectional link in the reverse
561 * direction - has been seen before and is down. It is necessary for the
562 * reverse-link to have been previously seen.
563 *
564 * @param link the infrastructure (unidirectional) link being queried
565 * @return true if another unidirectional link exists in the reverse
566 * direction, has been seen-before and is down
567 */
568 boolean isBidirectionalLinkDown(Link link) {
569 // cannot call linkService as link may be gone
570 Link reverseLink = getReverseLink(link);
571 if (reverseLink == null) {
572 log.warn("Query for bidi-link down but reverse-link not found "
573 + "for link {}", link);
574 return false;
575 }
576 Boolean result = seenLinks.get(reverseLink);
577 if (result == null) {
578 return false;
579 }
580 // if reverse link is seen UP (true), then its not bidi yet
581 return !result.booleanValue();
582 }
583
584 /**
585 * Returns the link in the reverse direction from the given link, by
586 * consulting the seen-links store.
587 *
588 * @param link the given link
589 * @return the reverse link or null
590 */
591 Link getReverseLink(Link link) {
592 return seenLinks.keySet().stream()
593 .filter(l -> l.src().equals(link.dst()) && l.dst().equals(link.src()))
594 .findAny()
595 .orElse(null);
596 }
597
598 /**
599 * Returns the component unidirectional links of a declared bidirectional
600 * link, by consulting the seen-links store. Caller is responsible for
601 * previously verifying bidirectionality. Returned list may be empty if
602 * errors are encountered.
603 *
604 * @param link the declared bidirectional link
605 * @return list of component unidirectional links
606 */
607 List<Link> getBidiComponentLinks(Link link) {
608 Link reverseLink = getReverseLink(link);
609 List<Link> componentLinks;
610 if (reverseLink == null) { // really should not happen if link is bidi
611 log.error("cannot find reverse link for given link: {} ... is it "
612 + "bi-directional?", link);
613 componentLinks = ImmutableList.of();
614 } else {
615 componentLinks = ImmutableList.of(reverseLink, link);
616 }
617 return componentLinks;
618 }
619
620 /**
Saurav Dase6c448a2018-01-18 12:07:33 -0800621 * Determines if the given link should be avoided in routing calculations by
622 * policy or design.
623 *
624 * @param link the infrastructure link being queried
625 * @return true if link should be avoided
626 */
627 boolean avoidLink(Link link) {
628 // XXX currently only avoids all pair-links. In the future can be
629 // extended to avoid any generic link
630 DeviceId src = link.src().deviceId();
631 PortNumber srcPort = link.src().port();
632 DeviceConfiguration devConfig = srManager.deviceConfiguration;
633 if (devConfig == null || !devConfig.isConfigured(src)) {
634 log.warn("Device {} not configured..cannot avoid link {}", src,
635 link);
636 return false;
637 }
638 DeviceId pairDev;
639 PortNumber pairLocalPort, pairRemotePort = null;
640 try {
641 pairDev = devConfig.getPairDeviceId(src);
642 pairLocalPort = devConfig.getPairLocalPort(src);
643 if (pairDev != null) {
644 pairRemotePort = devConfig
645 .getPairLocalPort(pairDev);
646 }
647 } catch (DeviceConfigNotFoundException e) {
648 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
649 src, link);
650 return false;
651 }
652
653 return srcPort.equals(pairLocalPort)
654 && link.dst().deviceId().equals(pairDev)
655 && link.dst().port().equals(pairRemotePort);
656 }
657
658 /**
659 * Cleans up internal LinkHandler stores.
660 *
661 * @param device the device that has been removed
662 */
663 void processDeviceRemoved(Device device) {
664 seenLinks.keySet()
665 .removeIf(key -> key.src().deviceId().equals(device.id())
666 || key.dst().deviceId().equals(device.id()));
667 }
668
669 /**
670 * Administratively disables the host location switchport if the edge device
Saurav Dasec683dc2018-04-27 18:42:30 -0700671 * has no viable uplinks. The caller needs to determine if such behavior is
672 * desired for the single or dual-homed host.
Saurav Dase6c448a2018-01-18 12:07:33 -0800673 *
Saurav Dasec683dc2018-04-27 18:42:30 -0700674 * @param loc the host location
Saurav Dase6c448a2018-01-18 12:07:33 -0800675 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700676 void checkUplinksForHost(HostLocation loc) {
pier3a60ee92019-12-20 22:12:57 +0100677 // If the device does not have a pair device - return
678 if (getPairDeviceIdOrNull(loc.deviceId()) == null) {
679 log.info("Device {} does not have pair device " +
680 "not disabling access port", loc.deviceId());
681 return;
682 }
683 // Verify link validity
Saurav Dase6c448a2018-01-18 12:07:33 -0800684 try {
685 for (Link l : srManager.linkService.getDeviceLinks(loc.deviceId())) {
686 if (srManager.deviceConfiguration.isEdgeDevice(l.dst().deviceId())
687 || l.state() == Link.State.INACTIVE) {
688 continue;
689 }
690 // found valid uplink - so, nothing to do
691 return;
692 }
693 } catch (DeviceConfigNotFoundException e) {
694 log.warn("Could not check for valid uplinks due to missing device"
695 + "config " + e.getMessage());
696 return;
697 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700698 log.warn("Host location {} has no valid uplinks disabling port", loc);
Saurav Dase6c448a2018-01-18 12:07:33 -0800699 srManager.deviceAdminService.changePortState(loc.deviceId(), loc.port(),
700 false);
701 Set<PortNumber> p = downedPortStore.get(loc.deviceId());
702 if (p == null) {
703 p = Sets.newHashSet(loc.port());
704 } else {
705 p.add(loc.port());
706 }
707 downedPortStore.put(loc.deviceId(), p);
708 }
709
pier3a60ee92019-12-20 22:12:57 +0100710 private DeviceId getPairDeviceIdOrNull(DeviceId deviceId) {
711 DeviceId pairDev;
712 try {
713 pairDev = srManager.deviceConfiguration.getPairDeviceId(deviceId);
714 } catch (DeviceConfigNotFoundException e) {
715 pairDev = null;
716 }
717 return pairDev;
718 }
719
Saurav Das6430f412018-01-25 09:49:01 -0800720 ImmutableMap<Link, Boolean> getSeenLinks() {
721 return ImmutableMap.copyOf(seenLinks);
722 }
723
724 ImmutableMap<DeviceId, Set<PortNumber>> getDownedPorts() {
725 return ImmutableMap.copyOf(downedPortStore.entrySet());
726 }
727
Saurav Dasdc7f2752018-03-18 21:28:15 -0700728 /**
729 * Returns all links that egress from given device that are UP in the
730 * seenLinks store. The returned links are also confirmed to be
731 * bidirectional.
732 *
733 * @param deviceId the device identifier
734 * @return set of egress links from the device
735 */
736 Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
737 return seenLinks.keySet().stream()
738 .filter(link -> link.src().deviceId().equals(deviceId))
739 .filter(link -> seenLinks.get(link))
740 .filter(link -> isBidirectionalLinkUp(link))
741 .collect(Collectors.toSet());
742 }
743
Saurav Dase6c448a2018-01-18 12:07:33 -0800744}