blob: b8800af9d02398a742e75a94eb438c8f03f735dd [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
pierventrebd6eefa2021-05-14 22:06:50 +0200206 // changes. Link should be already there because was up before. If not,
207 // we should not update the store. This could be a LINK DOWN happening
208 // after a DEVICE DOWN event.
209 if (isSeenLink(link)) {
210 updateSeenLink(link, false);
211 } else {
212 // Exiting here may not be needed since other checks later should fail
213 log.warn("received a link down for the link {} which is not in the store", link);
214 }
Saurav Das6430f412018-01-25 09:49:01 -0800215 // handle edge-ports for dual-homed hosts
216 if (srManager.mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700217 updateHostPorts(link, false);
Saurav Das6430f412018-01-25 09:49:01 -0800218 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800219
220 // device availability check helps to ensure that multiple link-removed
221 // events are actually treated as a single switch removed event.
222 // purgeSeenLink is necessary so we do rerouting (instead of rehashing)
223 // when switch comes back.
224 if (link.src().elementId() instanceof DeviceId
225 && !srManager.deviceService.isAvailable(link.src().deviceId())) {
226 purgeSeenLink(link);
227 return;
228 }
229 if (link.dst().elementId() instanceof DeviceId
230 && !srManager.deviceService.isAvailable(link.dst().deviceId())) {
231 purgeSeenLink(link);
232 return;
233 }
234
Saurav Dasdc7f2752018-03-18 21:28:15 -0700235 // process link only if it is bidirectional
236 if (!isBidirectionalLinkDown(link)) {
237 log.debug("Link not bidirectional.. waiting for other direction " +
238 "src {} --> dst {} ", link.dst(), link.src());
239 return;
240 }
241 log.info("processing bidi links {} <--> {} DOWN", link.src(), link.dst());
Saurav Dase6c448a2018-01-18 12:07:33 -0800242
Saurav Dasdc7f2752018-03-18 21:28:15 -0700243 for (Link ulink : getBidiComponentLinks(link)) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700244 log.info("-- Starting optimized route-path processing for component "
Saurav Dasdc7f2752018-03-18 21:28:15 -0700245 + "unidirectional link {} --> {} DOWN", ulink.src(), ulink.dst());
246 srManager.defaultRoutingHandler
247 .populateRoutingRulesForLinkStatusChange(ulink, null, null, true);
248
249 // attempt rehashing for parallel links
250 DefaultGroupHandler groupHandler = srManager.groupHandlerMap
251 .get(ulink.src().deviceId());
252 if (groupHandler != null) {
253 if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())
254 && isParallelLink(ulink)) {
255 log.debug("* retrying hash for parallel link removed:{}", ulink);
256 groupHandler.retryHash(ulink, true, false);
257 } else {
258 log.debug("Not attempting retry-hash for link removed: {} .. {}",
259 ulink,
260 (srManager.mastershipService.isLocalMaster(ulink
261 .src().deviceId())) ? "not parallel"
262 : "not master");
263 }
264 // ensure local stores are updated after all rerouting or rehashing
265 groupHandler.portDownForLink(ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800266 } else {
Saurav Dasdc7f2752018-03-18 21:28:15 -0700267 log.warn("group handler not found for dev:{} when removing link: {}",
268 ulink.src().deviceId(), ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800269 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800270 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800271 }
272
273 /**
274 * Checks validity of link. Examples of invalid links include
275 * indirect-links, links between ports on the same switch, and more.
276 *
277 * @param link the link to be processed
278 * @return true if valid link
279 */
Pier Luigid8a15162018-02-15 16:33:08 +0100280 boolean isLinkValid(Link link) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800281 if (link.type() != Link.Type.DIRECT) {
282 // NOTE: A DIRECT link might be transiently marked as INDIRECT
283 // if BDDP is received before LLDP. We can safely ignore that
284 // until the LLDP is received and the link is marked as DIRECT.
285 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
286 link.src(), link.dst(), link.type());
287 return false;
288 }
289 DeviceId srcId = link.src().deviceId();
290 DeviceId dstId = link.dst().deviceId();
291 if (srcId.equals(dstId)) {
292 log.warn("Links between ports on the same switch are not "
293 + "allowed .. ignoring link {}", link);
294 return false;
295 }
296 DeviceConfiguration devConfig = srManager.deviceConfiguration;
Saurav Dase321cff2018-02-09 17:26:45 -0800297 if (devConfig == null) {
298 log.warn("Cannot check validity of link without device config");
299 return true;
300 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800301 try {
Saurav Das97241862018-02-14 14:14:54 -0800302 /*if (!devConfig.isEdgeDevice(srcId)
Saurav Dase6c448a2018-01-18 12:07:33 -0800303 && !devConfig.isEdgeDevice(dstId)) {
304 // ignore links between spines
305 // XXX revisit when handling multi-stage fabrics
306 log.warn("Links between spines not allowed...ignoring "
307 + "link {}", link);
308 return false;
Saurav Das97241862018-02-14 14:14:54 -0800309 }*/
Saurav Dase6c448a2018-01-18 12:07:33 -0800310 if (devConfig.isEdgeDevice(srcId)
311 && devConfig.isEdgeDevice(dstId)) {
312 // ignore links between leaves if they are not pair-links
313 // XXX revisit if removing pair-link config or allowing more than
314 // one pair-link
315 if (devConfig.getPairDeviceId(srcId).equals(dstId)
316 && devConfig.getPairLocalPort(srcId)
317 .equals(link.src().port())
318 && devConfig.getPairLocalPort(dstId)
319 .equals(link.dst().port())) {
320 // found pair link - allow it
321 return true;
322 } else {
323 log.warn("Links between leaves other than pair-links are "
324 + "not allowed...ignoring link {}", link);
325 return false;
326 }
327 }
328 } catch (DeviceConfigNotFoundException e) {
329 // We still want to count the links in seenLinks even though there
330 // is no config. So we let it return true
331 log.warn("Could not check validity of link {} as subtending devices "
332 + "are not yet configured", link);
333 }
334 return true;
335 }
336
337 /**
338 * Administratively enables or disables edge ports if the link that was
Saurav Dasec683dc2018-04-27 18:42:30 -0700339 * added or removed was the only uplink port from an edge device. Edge ports
340 * that belong to dual-homed hosts are always processed. In addition,
341 * single-homed host ports are optionally processed depending on the
342 * singleHomedDown property.
Saurav Dase6c448a2018-01-18 12:07:33 -0800343 *
344 * @param link the link to be processed
345 * @param added true if link was added, false if link was removed
346 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700347 private void updateHostPorts(Link link, boolean added) {
pier3a60ee92019-12-20 22:12:57 +0100348 DeviceConfiguration devConfig = srManager.deviceConfiguration;
Saurav Dase6c448a2018-01-18 12:07:33 -0800349 if (added) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700350 try {
351 if (!devConfig.isEdgeDevice(link.src().deviceId())
352 || devConfig.isEdgeDevice(link.dst().deviceId())) {
353 return;
354 }
355 } catch (DeviceConfigNotFoundException e) {
356 log.warn("Unable to determine if link is a valid uplink"
357 + e.getMessage());
358 }
359 // re-enable previously disabled ports on this edge-device if any
Saurav Dase6c448a2018-01-18 12:07:33 -0800360 Set<PortNumber> p = downedPortStore.remove(link.src().deviceId());
361 if (p != null) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700362 log.warn("Link src {} --> dst {} added is an edge-device uplink, "
363 + "enabling dual homed ports if any: {}", link.src().deviceId(),
364 link.dst().deviceId(), (p.isEmpty()) ? "no ports" : p);
Saurav Dase6c448a2018-01-18 12:07:33 -0800365 p.forEach(pnum -> srManager.deviceAdminService
366 .changePortState(link.src().deviceId(), pnum, true));
367 }
368 } else {
pier3a60ee92019-12-20 22:12:57 +0100369 // If the device does not have a pair device - skip
370 DeviceId dev = link.src().deviceId();
371 if (getPairDeviceIdOrNull(dev) == null) {
372 log.info("Device {} does not have pair device " +
373 "not disabling access port", dev);
374 return;
375 }
376 // Verify if last uplink
Saurav Dasdebcf882018-04-06 20:16:01 -0700377 if (!lastUplink(link)) {
378 return;
379 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800380 // find dual homed hosts on this dev to disable
Saurav Dasec683dc2018-04-27 18:42:30 -0700381 Set<PortNumber> dp = srManager.hostHandler
382 .getDualHomedHostPorts(dev);
383 log.warn("Link src {} --> dst {} removed was the last uplink, "
384 + "disabling dual homed ports: {}", dev,
385 link.dst().deviceId(), (dp.isEmpty()) ? "no ports" : dp);
386 dp.forEach(pnum -> srManager.deviceAdminService
387 .changePortState(dev, pnum, false));
388 if (srManager.singleHomedDown) {
389 // get all configured ports and down them if they haven't already
390 // been downed
391 srManager.deviceService.getPorts(dev).stream()
392 .filter(p -> p.isEnabled() && !dp.contains(p.number()))
393 .filter(p -> srManager.interfaceService
394 .isConfigured(new ConnectPoint(dev, p.number())))
395 .filter(p -> !srManager.deviceConfiguration
396 .isPairLocalPort(dev, p.number()))
397 .forEach(p -> {
398 log.warn("Last uplink gone src {} -> dst {} .. removing "
399 + "configured port {}", p.number());
400 srManager.deviceAdminService
401 .changePortState(dev, p.number(), false);
402 dp.add(p.number());
403 });
404 }
405 if (!dp.isEmpty()) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800406 // update global store
Saurav Dasec683dc2018-04-27 18:42:30 -0700407 Set<PortNumber> p = downedPortStore.get(dev);
Saurav Dase6c448a2018-01-18 12:07:33 -0800408 if (p == null) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700409 p = dp;
Saurav Dase6c448a2018-01-18 12:07:33 -0800410 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -0700411 p.addAll(dp);
Saurav Dase6c448a2018-01-18 12:07:33 -0800412 }
413 downedPortStore.put(link.src().deviceId(), p);
414 }
415 }
416 }
417
418 /**
Saurav Dasdebcf882018-04-06 20:16:01 -0700419 * Returns true if given link was the last active uplink from src-device of
Saurav Dase6c448a2018-01-18 12:07:33 -0800420 * link. An uplink is defined as a unidirectional link with src as
421 * edgeRouter and dst as non-edgeRouter.
422 *
423 * @param link
Saurav Dasdebcf882018-04-06 20:16:01 -0700424 * @return true if given link was the last uplink from the src device
Saurav Dase6c448a2018-01-18 12:07:33 -0800425 */
Saurav Dasdebcf882018-04-06 20:16:01 -0700426 private boolean lastUplink(Link link) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800427 DeviceConfiguration devConfig = srManager.deviceConfiguration;
428 try {
Saurav Das6430f412018-01-25 09:49:01 -0800429 if (!devConfig.isEdgeDevice(link.src().deviceId())
430 || devConfig.isEdgeDevice(link.dst().deviceId())) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800431 return false;
432 }
Saurav Das6430f412018-01-25 09:49:01 -0800433 // note that using linkservice here would cause race conditions as
434 // more links can show up while the app is still processing the first one
435 Set<Link> devLinks = seenLinks.entrySet().stream()
436 .filter(entry -> entry.getKey().src().deviceId()
437 .equals(link.src().deviceId()))
438 .filter(entry -> entry.getValue())
439 .filter(entry -> !entry.getKey().equals(link))
440 .map(entry -> entry.getKey())
441 .collect(Collectors.toSet());
442
Saurav Dase6c448a2018-01-18 12:07:33 -0800443 for (Link l : devLinks) {
Saurav Das6430f412018-01-25 09:49:01 -0800444 if (devConfig.isEdgeDevice(l.dst().deviceId())) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800445 continue;
446 }
Saurav Dasdebcf882018-04-06 20:16:01 -0700447 log.debug("Found another active uplink {}", l);
Saurav Das6430f412018-01-25 09:49:01 -0800448 return false;
Saurav Dase6c448a2018-01-18 12:07:33 -0800449 }
Saurav Dasdebcf882018-04-06 20:16:01 -0700450 log.debug("No active uplink found");
Saurav Das6430f412018-01-25 09:49:01 -0800451 return true;
Saurav Dase6c448a2018-01-18 12:07:33 -0800452 } catch (DeviceConfigNotFoundException e) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700453 log.warn("Unable to determine if link was the last uplink"
Saurav Dase6c448a2018-01-18 12:07:33 -0800454 + e.getMessage());
455 }
456 return false;
457 }
458
459 /**
460 * Returns true if this controller instance has seen this link before. The
461 * link may not be currently up, but as long as the link had been seen
462 * before this method will return true. The one exception is when the link
463 * was indeed seen before, but this controller instance was forced to forget
464 * it by a call to purgeSeenLink method.
465 *
466 * @param link the infrastructure link being queried
467 * @return true if this controller instance has seen this link before
468 */
469 boolean isSeenLink(Link link) {
470 return seenLinks.containsKey(link);
471 }
472
473 /**
474 * Updates the seen link store. Updates can be for links that are currently
475 * available or not.
476 *
477 * @param link the link to update in the seen-link local store
478 * @param up the status of the link, true if up, false if down
479 */
480 void updateSeenLink(Link link, boolean up) {
481 seenLinks.put(link, up);
482 }
483
484 /**
485 * Returns the status of a seen-link (up or down). If the link has not been
486 * seen-before, a null object is returned.
487 *
488 * @param link the infrastructure link being queried
489 * @return null if the link was not seen-before; true if the seen-link is
490 * up; false if the seen-link is down
491 */
492 private Boolean isSeenLinkUp(Link link) {
493 return seenLinks.get(link);
494 }
495
496 /**
497 * Makes this controller instance forget a previously seen before link.
498 *
499 * @param link the infrastructure link to purge
500 */
501 private void purgeSeenLink(Link link) {
502 seenLinks.remove(link);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700503 seenBefore.remove(link);
Saurav Dase6c448a2018-01-18 12:07:33 -0800504 }
505
506 /**
507 * Returns the status of a link as parallel link. A parallel link is defined
508 * as a link which has common src and dst switches as another seen-link that
509 * is currently enabled. It is not necessary for the link being queried to
510 * be a seen-link.
511 *
512 * @param link the infrastructure link being queried
513 * @return true if a seen-link exists that is up, and shares the same src
514 * and dst switches as the link being queried
515 */
516 private boolean isParallelLink(Link link) {
517 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
518 Link seenLink = seen.getKey();
519 if (seenLink.equals(link)) {
520 continue;
521 }
522 if (seenLink.src().deviceId().equals(link.src().deviceId())
523 && seenLink.dst().deviceId().equals(link.dst().deviceId())
524 && seen.getValue()) {
525 return true;
526 }
527 }
528 return false;
529 }
530
531 /**
Saurav Dasdc7f2752018-03-18 21:28:15 -0700532 * Returns true if the link being queried is a bidirectional link that is
533 * up. A bidi-link is defined as a component unidirectional link, whose
534 * reverse link - ie. the component unidirectional link in the reverse
535 * direction - has been seen-before and is up. It is NOT necessary for the
536 * link being queried to be a previously seen-link.
Saurav Dase6c448a2018-01-18 12:07:33 -0800537 *
Saurav Dasdc7f2752018-03-18 21:28:15 -0700538 * @param link the infrastructure (unidirectional) link being queried
Saurav Dase6c448a2018-01-18 12:07:33 -0800539 * @return true if another unidirectional link exists in the reverse
540 * direction, has been seen-before and is up
541 */
Saurav Dasdc7f2752018-03-18 21:28:15 -0700542 boolean isBidirectionalLinkUp(Link link) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700543 // cannot call linkService as link may be gone
544 Link reverseLink = getReverseLink(link);
Saurav Dase6c448a2018-01-18 12:07:33 -0800545 if (reverseLink == null) {
546 return false;
547 }
548 Boolean result = isSeenLinkUp(reverseLink);
549 if (result == null) {
550 return false;
551 }
552 return result.booleanValue();
553 }
554
555 /**
Saurav Dasdc7f2752018-03-18 21:28:15 -0700556 * Returns true if the link being queried is a bidirectional link that is
557 * down. A bidi-link is defined as a component unidirectional link, whose
558 * reverse link - i.e the component unidirectional link in the reverse
559 * direction - has been seen before and is down. It is necessary for the
560 * reverse-link to have been previously seen.
561 *
562 * @param link the infrastructure (unidirectional) link being queried
563 * @return true if another unidirectional link exists in the reverse
564 * direction, has been seen-before and is down
565 */
566 boolean isBidirectionalLinkDown(Link link) {
567 // cannot call linkService as link may be gone
568 Link reverseLink = getReverseLink(link);
569 if (reverseLink == null) {
570 log.warn("Query for bidi-link down but reverse-link not found "
571 + "for link {}", link);
572 return false;
573 }
574 Boolean result = seenLinks.get(reverseLink);
575 if (result == null) {
576 return false;
577 }
578 // if reverse link is seen UP (true), then its not bidi yet
579 return !result.booleanValue();
580 }
581
582 /**
583 * Returns the link in the reverse direction from the given link, by
584 * consulting the seen-links store.
585 *
586 * @param link the given link
587 * @return the reverse link or null
588 */
589 Link getReverseLink(Link link) {
590 return seenLinks.keySet().stream()
591 .filter(l -> l.src().equals(link.dst()) && l.dst().equals(link.src()))
592 .findAny()
593 .orElse(null);
594 }
595
596 /**
597 * Returns the component unidirectional links of a declared bidirectional
598 * link, by consulting the seen-links store. Caller is responsible for
599 * previously verifying bidirectionality. Returned list may be empty if
600 * errors are encountered.
601 *
602 * @param link the declared bidirectional link
603 * @return list of component unidirectional links
604 */
605 List<Link> getBidiComponentLinks(Link link) {
606 Link reverseLink = getReverseLink(link);
607 List<Link> componentLinks;
608 if (reverseLink == null) { // really should not happen if link is bidi
609 log.error("cannot find reverse link for given link: {} ... is it "
610 + "bi-directional?", link);
611 componentLinks = ImmutableList.of();
612 } else {
613 componentLinks = ImmutableList.of(reverseLink, link);
614 }
615 return componentLinks;
616 }
617
618 /**
Saurav Dase6c448a2018-01-18 12:07:33 -0800619 * Determines if the given link should be avoided in routing calculations by
620 * policy or design.
621 *
622 * @param link the infrastructure link being queried
623 * @return true if link should be avoided
624 */
625 boolean avoidLink(Link link) {
626 // XXX currently only avoids all pair-links. In the future can be
627 // extended to avoid any generic link
628 DeviceId src = link.src().deviceId();
629 PortNumber srcPort = link.src().port();
630 DeviceConfiguration devConfig = srManager.deviceConfiguration;
631 if (devConfig == null || !devConfig.isConfigured(src)) {
632 log.warn("Device {} not configured..cannot avoid link {}", src,
633 link);
634 return false;
635 }
636 DeviceId pairDev;
637 PortNumber pairLocalPort, pairRemotePort = null;
638 try {
639 pairDev = devConfig.getPairDeviceId(src);
640 pairLocalPort = devConfig.getPairLocalPort(src);
641 if (pairDev != null) {
642 pairRemotePort = devConfig
643 .getPairLocalPort(pairDev);
644 }
645 } catch (DeviceConfigNotFoundException e) {
646 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
647 src, link);
648 return false;
649 }
650
651 return srcPort.equals(pairLocalPort)
652 && link.dst().deviceId().equals(pairDev)
653 && link.dst().port().equals(pairRemotePort);
654 }
655
656 /**
657 * Cleans up internal LinkHandler stores.
658 *
659 * @param device the device that has been removed
660 */
661 void processDeviceRemoved(Device device) {
662 seenLinks.keySet()
663 .removeIf(key -> key.src().deviceId().equals(device.id())
664 || key.dst().deviceId().equals(device.id()));
665 }
666
667 /**
668 * Administratively disables the host location switchport if the edge device
Saurav Dasec683dc2018-04-27 18:42:30 -0700669 * has no viable uplinks. The caller needs to determine if such behavior is
670 * desired for the single or dual-homed host.
Saurav Dase6c448a2018-01-18 12:07:33 -0800671 *
Saurav Dasec683dc2018-04-27 18:42:30 -0700672 * @param loc the host location
Saurav Dase6c448a2018-01-18 12:07:33 -0800673 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700674 void checkUplinksForHost(HostLocation loc) {
pier3a60ee92019-12-20 22:12:57 +0100675 // If the device does not have a pair device - return
676 if (getPairDeviceIdOrNull(loc.deviceId()) == null) {
677 log.info("Device {} does not have pair device " +
678 "not disabling access port", loc.deviceId());
679 return;
680 }
681 // Verify link validity
Saurav Dase6c448a2018-01-18 12:07:33 -0800682 try {
683 for (Link l : srManager.linkService.getDeviceLinks(loc.deviceId())) {
684 if (srManager.deviceConfiguration.isEdgeDevice(l.dst().deviceId())
685 || l.state() == Link.State.INACTIVE) {
686 continue;
687 }
688 // found valid uplink - so, nothing to do
689 return;
690 }
691 } catch (DeviceConfigNotFoundException e) {
692 log.warn("Could not check for valid uplinks due to missing device"
693 + "config " + e.getMessage());
694 return;
695 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700696 log.warn("Host location {} has no valid uplinks disabling port", loc);
Saurav Dase6c448a2018-01-18 12:07:33 -0800697 srManager.deviceAdminService.changePortState(loc.deviceId(), loc.port(),
698 false);
699 Set<PortNumber> p = downedPortStore.get(loc.deviceId());
700 if (p == null) {
701 p = Sets.newHashSet(loc.port());
702 } else {
703 p.add(loc.port());
704 }
705 downedPortStore.put(loc.deviceId(), p);
706 }
707
pier3a60ee92019-12-20 22:12:57 +0100708 private DeviceId getPairDeviceIdOrNull(DeviceId deviceId) {
709 DeviceId pairDev;
710 try {
711 pairDev = srManager.deviceConfiguration.getPairDeviceId(deviceId);
712 } catch (DeviceConfigNotFoundException e) {
713 pairDev = null;
714 }
715 return pairDev;
716 }
717
Saurav Das6430f412018-01-25 09:49:01 -0800718 ImmutableMap<Link, Boolean> getSeenLinks() {
719 return ImmutableMap.copyOf(seenLinks);
720 }
721
722 ImmutableMap<DeviceId, Set<PortNumber>> getDownedPorts() {
723 return ImmutableMap.copyOf(downedPortStore.entrySet());
724 }
725
Saurav Dasdc7f2752018-03-18 21:28:15 -0700726 /**
727 * Returns all links that egress from given device that are UP in the
728 * seenLinks store. The returned links are also confirmed to be
729 * bidirectional.
730 *
731 * @param deviceId the device identifier
732 * @return set of egress links from the device
733 */
734 Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
735 return seenLinks.keySet().stream()
736 .filter(link -> link.src().deviceId().equals(deviceId))
737 .filter(link -> seenLinks.get(link))
738 .filter(link -> isBidirectionalLinkUp(link))
739 .collect(Collectors.toSet());
740 }
741
Saurav Dase6c448a2018-01-18 12:07:33 -0800742}