blob: 2193c953612dd1c4d744f112b01c33555e933496 [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;
34import org.onosproject.segmentrouting.config.DeviceConfiguration;
35import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
36import org.onosproject.store.service.EventuallyConsistentMap;
37import org.onosproject.store.service.EventuallyConsistentMapBuilder;
38import org.onosproject.store.service.WallClockTimestamp;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
Saurav Dasdc7f2752018-03-18 21:28:15 -070042import com.google.common.collect.ImmutableList;
Saurav Das6430f412018-01-25 09:49:01 -080043import com.google.common.collect.ImmutableMap;
Saurav Dase6c448a2018-01-18 12:07:33 -080044import com.google.common.collect.Sets;
45
Saurav Dasdc7f2752018-03-18 21:28:15 -070046
Saurav Dase6c448a2018-01-18 12:07:33 -080047public class LinkHandler {
48 private static final Logger log = LoggerFactory.getLogger(LinkHandler.class);
49 protected final SegmentRoutingManager srManager;
Saurav Dase6c448a2018-01-18 12:07:33 -080050
51 // Local store for all links seen and their present status, used for
Saurav Das6430f412018-01-25 09:49:01 -080052 // optimized routing. The existence of the link in the keys is enough to know
Saurav Dase6c448a2018-01-18 12:07:33 -080053 // if the link has been "seen-before" by this instance of the controller.
54 // The boolean value indicates if the link is currently up or not.
Saurav Das6430f412018-01-25 09:49:01 -080055 // Currently the optimized routing logic depends on "forgetting" a link
56 // when a switch goes down, but "remembering" it when only the link goes down.
Saurav Dase6c448a2018-01-18 12:07:33 -080057 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
Saurav Dasdc7f2752018-03-18 21:28:15 -070058 private Set<Link> seenBefore = Sets.newConcurrentHashSet();
Saurav Dase6c448a2018-01-18 12:07:33 -080059 private EventuallyConsistentMap<DeviceId, Set<PortNumber>> downedPortStore = null;
60
61 /**
62 * Constructs the LinkHandler.
63 *
64 * @param srManager Segment Routing manager
65 */
66 LinkHandler(SegmentRoutingManager srManager) {
67 this.srManager = srManager;
Saurav Dase6c448a2018-01-18 12:07:33 -080068 log.debug("Creating EC map downedportstore");
69 EventuallyConsistentMapBuilder<DeviceId, Set<PortNumber>> downedPortsMapBuilder
70 = srManager.storageService.eventuallyConsistentMapBuilder();
71 downedPortStore = downedPortsMapBuilder.withName("downedportstore")
72 .withSerializer(srManager.createSerializer())
73 .withTimestampProvider((k, v) -> new WallClockTimestamp())
74 .build();
75 log.trace("Current size {}", downedPortStore.size());
76 }
77
78 /**
79 * Constructs the LinkHandler for unit-testing.
80 *
81 * @param srManager SegmentRoutingManager
82 * @param linkService LinkService
83 */
84 LinkHandler(SegmentRoutingManager srManager, LinkService linkService) {
85 this.srManager = srManager;
Saurav Dase6c448a2018-01-18 12:07:33 -080086 }
87
88 /**
Saurav Dase321cff2018-02-09 17:26:45 -080089 * Initialize LinkHandler.
90 */
Charles Chanc12c3d02018-03-09 15:53:44 -080091 void init() {
Saurav Dase321cff2018-02-09 17:26:45 -080092 log.info("Loading stored links");
Charles Chanc12c3d02018-03-09 15:53:44 -080093 srManager.linkService.getActiveLinks().forEach(this::processLinkAdded);
Saurav Dase321cff2018-02-09 17:26:45 -080094 }
95
96 /**
Saurav Dase6c448a2018-01-18 12:07:33 -080097 * Preprocessing of added link before being sent for route-path handling.
98 * Also performs post processing of link.
99 *
100 * @param link the link to be processed
101 */
102 void processLinkAdded(Link link) {
103 log.info("** LINK ADDED {}", link.toString());
104 if (!isLinkValid(link)) {
105 return;
106 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800107 // Irrespective of whether the local is a MASTER or not for this device,
Saurav Das6430f412018-01-25 09:49:01 -0800108 // create group handler instance and push default TTP flow rules if needed,
Saurav Dase6c448a2018-01-18 12:07:33 -0800109 // as in a multi-instance setup, instances can initiate groups for any
Saurav Das97241862018-02-14 14:14:54 -0800110 // device. Also update local groupHandler stores.
Saurav Dase6c448a2018-01-18 12:07:33 -0800111 DefaultGroupHandler groupHandler = srManager.groupHandlerMap
Saurav Das97241862018-02-14 14:14:54 -0800112 .get(link.src().deviceId());
Saurav Dasdebcf882018-04-06 20:16:01 -0700113 if (groupHandler == null) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800114 Device device = srManager.deviceService.getDevice(link.src().deviceId());
115 if (device != null) {
Saurav Das97241862018-02-14 14:14:54 -0800116 log.warn("processLinkAdded: Link Added notification without "
117 + "Device Added event, still handling it");
Saurav Dase6c448a2018-01-18 12:07:33 -0800118 srManager.processDeviceAdded(device);
Saurav Dase6c448a2018-01-18 12:07:33 -0800119 }
120 }
Saurav Dasdc7f2752018-03-18 21:28:15 -0700121
122 if (isSeenLink(link)) {
123 // temp store retains previous state, just before the state is updated in
124 // seenLinks; previous state is necessary when processing the
125 // linkupdate in defaultRoutingHandler
126 seenBefore.add(link);
127 }
128 updateSeenLink(link, true);
129
Saurav Das97241862018-02-14 14:14:54 -0800130 if (srManager.deviceConfiguration == null ||
131 !srManager.deviceConfiguration.isConfigured(link.src().deviceId())) {
Saurav Das97241862018-02-14 14:14:54 -0800132 log.warn("Source device of this link is not configured.. "
133 + "not processing further");
134 return;
135 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800136
Saurav Dasdc7f2752018-03-18 21:28:15 -0700137 // process link only if it is bidirectional
138 if (!isBidirectionalLinkUp(link)) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800139 log.debug("Link not bidirectional.. waiting for other direction " +
Saurav Dasdc7f2752018-03-18 21:28:15 -0700140 "src {} --> dst {} ", link.dst(), link.src());
141 return;
142 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800143
Saurav Dasdebcf882018-04-06 20:16:01 -0700144 log.info("processing bidi links {} <--> {} UP", link.src(), link.dst());
145 // update groupHandler internal port state for both directions
146 List<Link> ulinks = getBidiComponentLinks(link);
147 for (Link ulink : ulinks) {
148 DefaultGroupHandler gh = srManager.groupHandlerMap
149 .get(ulink.src().deviceId());
150 if (gh != null) {
151 gh.portUpForLink(ulink);
152 }
153 }
154
155 for (Link ulink : ulinks) {
156 log.info("-- Starting optimized route-path processing for component "
Saurav Dasdc7f2752018-03-18 21:28:15 -0700157 + "unidirectional link {} --> {} UP", ulink.src(), ulink.dst());
158 srManager.defaultRoutingHandler
159 .populateRoutingRulesForLinkStatusChange(null, ulink, null,
160 seenBefore.contains(ulink));
Saurav Dase6c448a2018-01-18 12:07:33 -0800161
Saurav Dasdc7f2752018-03-18 21:28:15 -0700162 if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())) {
163 // handle edge-ports for dual-homed hosts
Saurav Dasec683dc2018-04-27 18:42:30 -0700164 updateHostPorts(ulink, true);
Saurav Dase6c448a2018-01-18 12:07:33 -0800165
Saurav Dasdc7f2752018-03-18 21:28:15 -0700166 // It's possible that linkUp causes no route-path change as ECMP graph does
167 // not change if the link is a parallel link (same src-dst as
168 // another link). However we still need to update ECMP hash
169 // groups to include new buckets for the link that has come up.
Saurav Dasdebcf882018-04-06 20:16:01 -0700170 DefaultGroupHandler gh = srManager.groupHandlerMap
171 .get(ulink.src().deviceId());
172 if (gh != null) {
Saurav Dasdc7f2752018-03-18 21:28:15 -0700173 if (!seenBefore.contains(ulink) && isParallelLink(ulink)) {
174 // if link seen first time, we need to ensure hash-groups have
175 // all ports
176 log.debug("Attempting retryHash for paralled first-time link {}",
177 ulink);
Saurav Dasdebcf882018-04-06 20:16:01 -0700178 gh.retryHash(ulink, false, true);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700179 } else {
180 // seen before-link
181 if (isParallelLink(ulink)) {
182 log.debug("Attempting retryHash for paralled seen-before "
183 + "link {}", ulink);
Saurav Dasdebcf882018-04-06 20:16:01 -0700184 gh.retryHash(ulink, false, false);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700185 }
Ray Milkey43969b92018-01-24 10:41:14 -0800186 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800187 }
188 }
Saurav Dasdc7f2752018-03-18 21:28:15 -0700189 //clean up temp state
190 seenBefore.remove(ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800191 }
Saurav Dasdc7f2752018-03-18 21:28:15 -0700192
Saurav Dase6c448a2018-01-18 12:07:33 -0800193 }
194
195 /**
196 * Preprocessing of removed link before being sent for route-path handling.
197 * Also performs post processing of link.
198 *
199 * @param link the link to be processed
200 */
201 void processLinkRemoved(Link link) {
202 log.info("** LINK REMOVED {}", link.toString());
203 if (!isLinkValid(link)) {
204 return;
205 }
206 // when removing links, update seen links first, before doing route-path
207 // changes
208 updateSeenLink(link, false);
Saurav Das6430f412018-01-25 09:49:01 -0800209 // handle edge-ports for dual-homed hosts
210 if (srManager.mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700211 updateHostPorts(link, false);
Saurav Das6430f412018-01-25 09:49:01 -0800212 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800213
214 // device availability check helps to ensure that multiple link-removed
215 // events are actually treated as a single switch removed event.
216 // purgeSeenLink is necessary so we do rerouting (instead of rehashing)
217 // when switch comes back.
218 if (link.src().elementId() instanceof DeviceId
219 && !srManager.deviceService.isAvailable(link.src().deviceId())) {
220 purgeSeenLink(link);
221 return;
222 }
223 if (link.dst().elementId() instanceof DeviceId
224 && !srManager.deviceService.isAvailable(link.dst().deviceId())) {
225 purgeSeenLink(link);
226 return;
227 }
228
Saurav Dasdc7f2752018-03-18 21:28:15 -0700229 // process link only if it is bidirectional
230 if (!isBidirectionalLinkDown(link)) {
231 log.debug("Link not bidirectional.. waiting for other direction " +
232 "src {} --> dst {} ", link.dst(), link.src());
233 return;
234 }
235 log.info("processing bidi links {} <--> {} DOWN", link.src(), link.dst());
Saurav Dase6c448a2018-01-18 12:07:33 -0800236
Saurav Dasdc7f2752018-03-18 21:28:15 -0700237 for (Link ulink : getBidiComponentLinks(link)) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700238 log.info("-- Starting optimized route-path processing for component "
Saurav Dasdc7f2752018-03-18 21:28:15 -0700239 + "unidirectional link {} --> {} DOWN", ulink.src(), ulink.dst());
240 srManager.defaultRoutingHandler
241 .populateRoutingRulesForLinkStatusChange(ulink, null, null, true);
242
243 // attempt rehashing for parallel links
244 DefaultGroupHandler groupHandler = srManager.groupHandlerMap
245 .get(ulink.src().deviceId());
246 if (groupHandler != null) {
247 if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())
248 && isParallelLink(ulink)) {
249 log.debug("* retrying hash for parallel link removed:{}", ulink);
250 groupHandler.retryHash(ulink, true, false);
251 } else {
252 log.debug("Not attempting retry-hash for link removed: {} .. {}",
253 ulink,
254 (srManager.mastershipService.isLocalMaster(ulink
255 .src().deviceId())) ? "not parallel"
256 : "not master");
257 }
258 // ensure local stores are updated after all rerouting or rehashing
259 groupHandler.portDownForLink(ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800260 } else {
Saurav Dasdc7f2752018-03-18 21:28:15 -0700261 log.warn("group handler not found for dev:{} when removing link: {}",
262 ulink.src().deviceId(), ulink);
Saurav Dase6c448a2018-01-18 12:07:33 -0800263 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800264 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800265 }
266
267 /**
268 * Checks validity of link. Examples of invalid links include
269 * indirect-links, links between ports on the same switch, and more.
270 *
271 * @param link the link to be processed
272 * @return true if valid link
273 */
Pier Luigid8a15162018-02-15 16:33:08 +0100274 boolean isLinkValid(Link link) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800275 if (link.type() != Link.Type.DIRECT) {
276 // NOTE: A DIRECT link might be transiently marked as INDIRECT
277 // if BDDP is received before LLDP. We can safely ignore that
278 // until the LLDP is received and the link is marked as DIRECT.
279 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
280 link.src(), link.dst(), link.type());
281 return false;
282 }
283 DeviceId srcId = link.src().deviceId();
284 DeviceId dstId = link.dst().deviceId();
285 if (srcId.equals(dstId)) {
286 log.warn("Links between ports on the same switch are not "
287 + "allowed .. ignoring link {}", link);
288 return false;
289 }
290 DeviceConfiguration devConfig = srManager.deviceConfiguration;
Saurav Dase321cff2018-02-09 17:26:45 -0800291 if (devConfig == null) {
292 log.warn("Cannot check validity of link without device config");
293 return true;
294 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800295 try {
Saurav Das97241862018-02-14 14:14:54 -0800296 /*if (!devConfig.isEdgeDevice(srcId)
Saurav Dase6c448a2018-01-18 12:07:33 -0800297 && !devConfig.isEdgeDevice(dstId)) {
298 // ignore links between spines
299 // XXX revisit when handling multi-stage fabrics
300 log.warn("Links between spines not allowed...ignoring "
301 + "link {}", link);
302 return false;
Saurav Das97241862018-02-14 14:14:54 -0800303 }*/
Saurav Dase6c448a2018-01-18 12:07:33 -0800304 if (devConfig.isEdgeDevice(srcId)
305 && devConfig.isEdgeDevice(dstId)) {
306 // ignore links between leaves if they are not pair-links
307 // XXX revisit if removing pair-link config or allowing more than
308 // one pair-link
309 if (devConfig.getPairDeviceId(srcId).equals(dstId)
310 && devConfig.getPairLocalPort(srcId)
311 .equals(link.src().port())
312 && devConfig.getPairLocalPort(dstId)
313 .equals(link.dst().port())) {
314 // found pair link - allow it
315 return true;
316 } else {
317 log.warn("Links between leaves other than pair-links are "
318 + "not allowed...ignoring link {}", link);
319 return false;
320 }
321 }
322 } catch (DeviceConfigNotFoundException e) {
323 // We still want to count the links in seenLinks even though there
324 // is no config. So we let it return true
325 log.warn("Could not check validity of link {} as subtending devices "
326 + "are not yet configured", link);
327 }
328 return true;
329 }
330
331 /**
332 * Administratively enables or disables edge ports if the link that was
Saurav Dasec683dc2018-04-27 18:42:30 -0700333 * added or removed was the only uplink port from an edge device. Edge ports
334 * that belong to dual-homed hosts are always processed. In addition,
335 * single-homed host ports are optionally processed depending on the
336 * singleHomedDown property.
Saurav Dase6c448a2018-01-18 12:07:33 -0800337 *
338 * @param link the link to be processed
339 * @param added true if link was added, false if link was removed
340 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700341 private void updateHostPorts(Link link, boolean added) {
pier3a60ee92019-12-20 22:12:57 +0100342 DeviceConfiguration devConfig = srManager.deviceConfiguration;
Saurav Dase6c448a2018-01-18 12:07:33 -0800343 if (added) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700344 try {
345 if (!devConfig.isEdgeDevice(link.src().deviceId())
346 || devConfig.isEdgeDevice(link.dst().deviceId())) {
347 return;
348 }
349 } catch (DeviceConfigNotFoundException e) {
350 log.warn("Unable to determine if link is a valid uplink"
351 + e.getMessage());
352 }
353 // re-enable previously disabled ports on this edge-device if any
Saurav Dase6c448a2018-01-18 12:07:33 -0800354 Set<PortNumber> p = downedPortStore.remove(link.src().deviceId());
355 if (p != null) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700356 log.warn("Link src {} --> dst {} added is an edge-device uplink, "
357 + "enabling dual homed ports if any: {}", link.src().deviceId(),
358 link.dst().deviceId(), (p.isEmpty()) ? "no ports" : p);
Saurav Dase6c448a2018-01-18 12:07:33 -0800359 p.forEach(pnum -> srManager.deviceAdminService
360 .changePortState(link.src().deviceId(), pnum, true));
361 }
362 } else {
pier3a60ee92019-12-20 22:12:57 +0100363 // If the device does not have a pair device - skip
364 DeviceId dev = link.src().deviceId();
365 if (getPairDeviceIdOrNull(dev) == null) {
366 log.info("Device {} does not have pair device " +
367 "not disabling access port", dev);
368 return;
369 }
370 // Verify if last uplink
Saurav Dasdebcf882018-04-06 20:16:01 -0700371 if (!lastUplink(link)) {
372 return;
373 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800374 // find dual homed hosts on this dev to disable
Saurav Dasec683dc2018-04-27 18:42:30 -0700375 Set<PortNumber> dp = srManager.hostHandler
376 .getDualHomedHostPorts(dev);
377 log.warn("Link src {} --> dst {} removed was the last uplink, "
378 + "disabling dual homed ports: {}", dev,
379 link.dst().deviceId(), (dp.isEmpty()) ? "no ports" : dp);
380 dp.forEach(pnum -> srManager.deviceAdminService
381 .changePortState(dev, pnum, false));
382 if (srManager.singleHomedDown) {
383 // get all configured ports and down them if they haven't already
384 // been downed
385 srManager.deviceService.getPorts(dev).stream()
386 .filter(p -> p.isEnabled() && !dp.contains(p.number()))
387 .filter(p -> srManager.interfaceService
388 .isConfigured(new ConnectPoint(dev, p.number())))
389 .filter(p -> !srManager.deviceConfiguration
390 .isPairLocalPort(dev, p.number()))
391 .forEach(p -> {
392 log.warn("Last uplink gone src {} -> dst {} .. removing "
393 + "configured port {}", p.number());
394 srManager.deviceAdminService
395 .changePortState(dev, p.number(), false);
396 dp.add(p.number());
397 });
398 }
399 if (!dp.isEmpty()) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800400 // update global store
Saurav Dasec683dc2018-04-27 18:42:30 -0700401 Set<PortNumber> p = downedPortStore.get(dev);
Saurav Dase6c448a2018-01-18 12:07:33 -0800402 if (p == null) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700403 p = dp;
Saurav Dase6c448a2018-01-18 12:07:33 -0800404 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -0700405 p.addAll(dp);
Saurav Dase6c448a2018-01-18 12:07:33 -0800406 }
407 downedPortStore.put(link.src().deviceId(), p);
408 }
409 }
410 }
411
412 /**
Saurav Dasdebcf882018-04-06 20:16:01 -0700413 * Returns true if given link was the last active uplink from src-device of
Saurav Dase6c448a2018-01-18 12:07:33 -0800414 * link. An uplink is defined as a unidirectional link with src as
415 * edgeRouter and dst as non-edgeRouter.
416 *
417 * @param link
Saurav Dasdebcf882018-04-06 20:16:01 -0700418 * @return true if given link was the last uplink from the src device
Saurav Dase6c448a2018-01-18 12:07:33 -0800419 */
Saurav Dasdebcf882018-04-06 20:16:01 -0700420 private boolean lastUplink(Link link) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800421 DeviceConfiguration devConfig = srManager.deviceConfiguration;
422 try {
Saurav Das6430f412018-01-25 09:49:01 -0800423 if (!devConfig.isEdgeDevice(link.src().deviceId())
424 || devConfig.isEdgeDevice(link.dst().deviceId())) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800425 return false;
426 }
Saurav Das6430f412018-01-25 09:49:01 -0800427 // note that using linkservice here would cause race conditions as
428 // more links can show up while the app is still processing the first one
429 Set<Link> devLinks = seenLinks.entrySet().stream()
430 .filter(entry -> entry.getKey().src().deviceId()
431 .equals(link.src().deviceId()))
432 .filter(entry -> entry.getValue())
433 .filter(entry -> !entry.getKey().equals(link))
434 .map(entry -> entry.getKey())
435 .collect(Collectors.toSet());
436
Saurav Dase6c448a2018-01-18 12:07:33 -0800437 for (Link l : devLinks) {
Saurav Das6430f412018-01-25 09:49:01 -0800438 if (devConfig.isEdgeDevice(l.dst().deviceId())) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800439 continue;
440 }
Saurav Dasdebcf882018-04-06 20:16:01 -0700441 log.debug("Found another active uplink {}", l);
Saurav Das6430f412018-01-25 09:49:01 -0800442 return false;
Saurav Dase6c448a2018-01-18 12:07:33 -0800443 }
Saurav Dasdebcf882018-04-06 20:16:01 -0700444 log.debug("No active uplink found");
Saurav Das6430f412018-01-25 09:49:01 -0800445 return true;
Saurav Dase6c448a2018-01-18 12:07:33 -0800446 } catch (DeviceConfigNotFoundException e) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700447 log.warn("Unable to determine if link was the last uplink"
Saurav Dase6c448a2018-01-18 12:07:33 -0800448 + e.getMessage());
449 }
450 return false;
451 }
452
453 /**
454 * Returns true if this controller instance has seen this link before. The
455 * link may not be currently up, but as long as the link had been seen
456 * before this method will return true. The one exception is when the link
457 * was indeed seen before, but this controller instance was forced to forget
458 * it by a call to purgeSeenLink method.
459 *
460 * @param link the infrastructure link being queried
461 * @return true if this controller instance has seen this link before
462 */
463 boolean isSeenLink(Link link) {
464 return seenLinks.containsKey(link);
465 }
466
467 /**
468 * Updates the seen link store. Updates can be for links that are currently
469 * available or not.
470 *
471 * @param link the link to update in the seen-link local store
472 * @param up the status of the link, true if up, false if down
473 */
474 void updateSeenLink(Link link, boolean up) {
475 seenLinks.put(link, up);
476 }
477
478 /**
479 * Returns the status of a seen-link (up or down). If the link has not been
480 * seen-before, a null object is returned.
481 *
482 * @param link the infrastructure link being queried
483 * @return null if the link was not seen-before; true if the seen-link is
484 * up; false if the seen-link is down
485 */
486 private Boolean isSeenLinkUp(Link link) {
487 return seenLinks.get(link);
488 }
489
490 /**
491 * Makes this controller instance forget a previously seen before link.
492 *
493 * @param link the infrastructure link to purge
494 */
495 private void purgeSeenLink(Link link) {
496 seenLinks.remove(link);
Saurav Dasdc7f2752018-03-18 21:28:15 -0700497 seenBefore.remove(link);
Saurav Dase6c448a2018-01-18 12:07:33 -0800498 }
499
500 /**
501 * Returns the status of a link as parallel link. A parallel link is defined
502 * as a link which has common src and dst switches as another seen-link that
503 * is currently enabled. It is not necessary for the link being queried to
504 * be a seen-link.
505 *
506 * @param link the infrastructure link being queried
507 * @return true if a seen-link exists that is up, and shares the same src
508 * and dst switches as the link being queried
509 */
510 private boolean isParallelLink(Link link) {
511 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
512 Link seenLink = seen.getKey();
513 if (seenLink.equals(link)) {
514 continue;
515 }
516 if (seenLink.src().deviceId().equals(link.src().deviceId())
517 && seenLink.dst().deviceId().equals(link.dst().deviceId())
518 && seen.getValue()) {
519 return true;
520 }
521 }
522 return false;
523 }
524
525 /**
Saurav Dasdc7f2752018-03-18 21:28:15 -0700526 * Returns true if the link being queried is a bidirectional link that is
527 * up. A bidi-link is defined as a component unidirectional link, whose
528 * reverse link - ie. the component unidirectional link in the reverse
529 * direction - has been seen-before and is up. It is NOT necessary for the
530 * link being queried to be a previously seen-link.
Saurav Dase6c448a2018-01-18 12:07:33 -0800531 *
Saurav Dasdc7f2752018-03-18 21:28:15 -0700532 * @param link the infrastructure (unidirectional) link being queried
Saurav Dase6c448a2018-01-18 12:07:33 -0800533 * @return true if another unidirectional link exists in the reverse
534 * direction, has been seen-before and is up
535 */
Saurav Dasdc7f2752018-03-18 21:28:15 -0700536 boolean isBidirectionalLinkUp(Link link) {
Saurav Dasdebcf882018-04-06 20:16:01 -0700537 // cannot call linkService as link may be gone
538 Link reverseLink = getReverseLink(link);
Saurav Dase6c448a2018-01-18 12:07:33 -0800539 if (reverseLink == null) {
540 return false;
541 }
542 Boolean result = isSeenLinkUp(reverseLink);
543 if (result == null) {
544 return false;
545 }
546 return result.booleanValue();
547 }
548
549 /**
Saurav Dasdc7f2752018-03-18 21:28:15 -0700550 * Returns true if the link being queried is a bidirectional link that is
551 * down. A bidi-link is defined as a component unidirectional link, whose
552 * reverse link - i.e the component unidirectional link in the reverse
553 * direction - has been seen before and is down. It is necessary for the
554 * reverse-link to have been previously seen.
555 *
556 * @param link the infrastructure (unidirectional) link being queried
557 * @return true if another unidirectional link exists in the reverse
558 * direction, has been seen-before and is down
559 */
560 boolean isBidirectionalLinkDown(Link link) {
561 // cannot call linkService as link may be gone
562 Link reverseLink = getReverseLink(link);
563 if (reverseLink == null) {
564 log.warn("Query for bidi-link down but reverse-link not found "
565 + "for link {}", link);
566 return false;
567 }
568 Boolean result = seenLinks.get(reverseLink);
569 if (result == null) {
570 return false;
571 }
572 // if reverse link is seen UP (true), then its not bidi yet
573 return !result.booleanValue();
574 }
575
576 /**
577 * Returns the link in the reverse direction from the given link, by
578 * consulting the seen-links store.
579 *
580 * @param link the given link
581 * @return the reverse link or null
582 */
583 Link getReverseLink(Link link) {
584 return seenLinks.keySet().stream()
585 .filter(l -> l.src().equals(link.dst()) && l.dst().equals(link.src()))
586 .findAny()
587 .orElse(null);
588 }
589
590 /**
591 * Returns the component unidirectional links of a declared bidirectional
592 * link, by consulting the seen-links store. Caller is responsible for
593 * previously verifying bidirectionality. Returned list may be empty if
594 * errors are encountered.
595 *
596 * @param link the declared bidirectional link
597 * @return list of component unidirectional links
598 */
599 List<Link> getBidiComponentLinks(Link link) {
600 Link reverseLink = getReverseLink(link);
601 List<Link> componentLinks;
602 if (reverseLink == null) { // really should not happen if link is bidi
603 log.error("cannot find reverse link for given link: {} ... is it "
604 + "bi-directional?", link);
605 componentLinks = ImmutableList.of();
606 } else {
607 componentLinks = ImmutableList.of(reverseLink, link);
608 }
609 return componentLinks;
610 }
611
612 /**
Saurav Dase6c448a2018-01-18 12:07:33 -0800613 * Determines if the given link should be avoided in routing calculations by
614 * policy or design.
615 *
616 * @param link the infrastructure link being queried
617 * @return true if link should be avoided
618 */
619 boolean avoidLink(Link link) {
620 // XXX currently only avoids all pair-links. In the future can be
621 // extended to avoid any generic link
622 DeviceId src = link.src().deviceId();
623 PortNumber srcPort = link.src().port();
624 DeviceConfiguration devConfig = srManager.deviceConfiguration;
625 if (devConfig == null || !devConfig.isConfigured(src)) {
626 log.warn("Device {} not configured..cannot avoid link {}", src,
627 link);
628 return false;
629 }
630 DeviceId pairDev;
631 PortNumber pairLocalPort, pairRemotePort = null;
632 try {
633 pairDev = devConfig.getPairDeviceId(src);
634 pairLocalPort = devConfig.getPairLocalPort(src);
635 if (pairDev != null) {
636 pairRemotePort = devConfig
637 .getPairLocalPort(pairDev);
638 }
639 } catch (DeviceConfigNotFoundException e) {
640 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
641 src, link);
642 return false;
643 }
644
645 return srcPort.equals(pairLocalPort)
646 && link.dst().deviceId().equals(pairDev)
647 && link.dst().port().equals(pairRemotePort);
648 }
649
650 /**
651 * Cleans up internal LinkHandler stores.
652 *
653 * @param device the device that has been removed
654 */
655 void processDeviceRemoved(Device device) {
656 seenLinks.keySet()
657 .removeIf(key -> key.src().deviceId().equals(device.id())
658 || key.dst().deviceId().equals(device.id()));
659 }
660
661 /**
662 * Administratively disables the host location switchport if the edge device
Saurav Dasec683dc2018-04-27 18:42:30 -0700663 * has no viable uplinks. The caller needs to determine if such behavior is
664 * desired for the single or dual-homed host.
Saurav Dase6c448a2018-01-18 12:07:33 -0800665 *
Saurav Dasec683dc2018-04-27 18:42:30 -0700666 * @param loc the host location
Saurav Dase6c448a2018-01-18 12:07:33 -0800667 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700668 void checkUplinksForHost(HostLocation loc) {
pier3a60ee92019-12-20 22:12:57 +0100669 // If the device does not have a pair device - return
670 if (getPairDeviceIdOrNull(loc.deviceId()) == null) {
671 log.info("Device {} does not have pair device " +
672 "not disabling access port", loc.deviceId());
673 return;
674 }
675 // Verify link validity
Saurav Dase6c448a2018-01-18 12:07:33 -0800676 try {
677 for (Link l : srManager.linkService.getDeviceLinks(loc.deviceId())) {
678 if (srManager.deviceConfiguration.isEdgeDevice(l.dst().deviceId())
679 || l.state() == Link.State.INACTIVE) {
680 continue;
681 }
682 // found valid uplink - so, nothing to do
683 return;
684 }
685 } catch (DeviceConfigNotFoundException e) {
686 log.warn("Could not check for valid uplinks due to missing device"
687 + "config " + e.getMessage());
688 return;
689 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700690 log.warn("Host location {} has no valid uplinks disabling port", loc);
Saurav Dase6c448a2018-01-18 12:07:33 -0800691 srManager.deviceAdminService.changePortState(loc.deviceId(), loc.port(),
692 false);
693 Set<PortNumber> p = downedPortStore.get(loc.deviceId());
694 if (p == null) {
695 p = Sets.newHashSet(loc.port());
696 } else {
697 p.add(loc.port());
698 }
699 downedPortStore.put(loc.deviceId(), p);
700 }
701
pier3a60ee92019-12-20 22:12:57 +0100702 private DeviceId getPairDeviceIdOrNull(DeviceId deviceId) {
703 DeviceId pairDev;
704 try {
705 pairDev = srManager.deviceConfiguration.getPairDeviceId(deviceId);
706 } catch (DeviceConfigNotFoundException e) {
707 pairDev = null;
708 }
709 return pairDev;
710 }
711
Saurav Das6430f412018-01-25 09:49:01 -0800712 ImmutableMap<Link, Boolean> getSeenLinks() {
713 return ImmutableMap.copyOf(seenLinks);
714 }
715
716 ImmutableMap<DeviceId, Set<PortNumber>> getDownedPorts() {
717 return ImmutableMap.copyOf(downedPortStore.entrySet());
718 }
719
Saurav Dasdc7f2752018-03-18 21:28:15 -0700720 /**
721 * Returns all links that egress from given device that are UP in the
722 * seenLinks store. The returned links are also confirmed to be
723 * bidirectional.
724 *
725 * @param deviceId the device identifier
726 * @return set of egress links from the device
727 */
728 Set<Link> getDeviceEgressLinks(DeviceId deviceId) {
729 return seenLinks.keySet().stream()
730 .filter(link -> link.src().deviceId().equals(deviceId))
731 .filter(link -> seenLinks.get(link))
732 .filter(link -> isBidirectionalLinkUp(link))
733 .collect(Collectors.toSet());
734 }
735
Saurav Dase6c448a2018-01-18 12:07:33 -0800736}