Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 19 | import java.util.List; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 20 | import java.util.Map; |
| 21 | import java.util.Map.Entry; |
| 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 24 | import java.util.stream.Collectors; |
| 25 | |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.HostLocation; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.link.LinkService; |
| 33 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 34 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
| 35 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 36 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 37 | import org.onosproject.store.service.WallClockTimestamp; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 41 | import com.google.common.collect.ImmutableList; |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 42 | import com.google.common.collect.ImmutableMap; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 43 | import com.google.common.collect.Sets; |
| 44 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 45 | |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 46 | public class LinkHandler { |
| 47 | private static final Logger log = LoggerFactory.getLogger(LinkHandler.class); |
| 48 | protected final SegmentRoutingManager srManager; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 49 | |
| 50 | // Local store for all links seen and their present status, used for |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 51 | // optimized routing. The existence of the link in the keys is enough to know |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 52 | // 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 Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 54 | // 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 56 | private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>(); |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 57 | private Set<Link> seenBefore = Sets.newConcurrentHashSet(); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 58 | 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 67 | 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
Saurav Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 88 | * Initialize LinkHandler. |
| 89 | */ |
Charles Chan | c12c3d0 | 2018-03-09 15:53:44 -0800 | [diff] [blame] | 90 | void init() { |
Saurav Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 91 | log.info("Loading stored links"); |
Charles Chan | c12c3d0 | 2018-03-09 15:53:44 -0800 | [diff] [blame] | 92 | srManager.linkService.getActiveLinks().forEach(this::processLinkAdded); |
Saurav Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 96 | * 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 106 | // Irrespective of whether the local is a MASTER or not for this device, |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 107 | // create group handler instance and push default TTP flow rules if needed, |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 108 | // as in a multi-instance setup, instances can initiate groups for any |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 109 | // device. Also update local groupHandler stores. |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 110 | DefaultGroupHandler groupHandler = srManager.groupHandlerMap |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 111 | .get(link.src().deviceId()); |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 112 | if (groupHandler == null) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 113 | Device device = srManager.deviceService.getDevice(link.src().deviceId()); |
| 114 | if (device != null) { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 115 | log.warn("processLinkAdded: Link Added notification without " |
| 116 | + "Device Added event, still handling it"); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 117 | srManager.processDeviceAdded(device); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 120 | |
| 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 Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 129 | if (srManager.deviceConfiguration == null || |
| 130 | !srManager.deviceConfiguration.isConfigured(link.src().deviceId())) { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 131 | log.warn("Source device of this link is not configured.. " |
| 132 | + "not processing further"); |
| 133 | return; |
| 134 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 135 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 136 | // process link only if it is bidirectional |
| 137 | if (!isBidirectionalLinkUp(link)) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 138 | log.debug("Link not bidirectional.. waiting for other direction " + |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 139 | "src {} --> dst {} ", link.dst(), link.src()); |
| 140 | return; |
| 141 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 142 | |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 143 | 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 Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 156 | + "unidirectional link {} --> {} UP", ulink.src(), ulink.dst()); |
| 157 | srManager.defaultRoutingHandler |
| 158 | .populateRoutingRulesForLinkStatusChange(null, ulink, null, |
| 159 | seenBefore.contains(ulink)); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 160 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 161 | if (srManager.mastershipService.isLocalMaster(ulink.src().deviceId())) { |
| 162 | // handle edge-ports for dual-homed hosts |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 163 | updateHostPorts(ulink, true); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 164 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 165 | // 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 Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 169 | DefaultGroupHandler gh = srManager.groupHandlerMap |
| 170 | .get(ulink.src().deviceId()); |
| 171 | if (gh != null) { |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 172 | 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 Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 177 | gh.retryHash(ulink, false, true); |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 178 | } else { |
| 179 | // seen before-link |
| 180 | if (isParallelLink(ulink)) { |
| 181 | log.debug("Attempting retryHash for paralled seen-before " |
| 182 | + "link {}", ulink); |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 183 | gh.retryHash(ulink, false, false); |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 184 | } |
Ray Milkey | 43969b9 | 2018-01-24 10:41:14 -0800 | [diff] [blame] | 185 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 188 | //clean up temp state |
| 189 | seenBefore.remove(ulink); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 190 | } |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 191 | |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 192 | } |
| 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 |
pierventre | bd6eefa | 2021-05-14 22:06:50 +0200 | [diff] [blame] | 206 | // 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 Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 215 | // handle edge-ports for dual-homed hosts |
| 216 | if (srManager.mastershipService.isLocalMaster(link.src().deviceId())) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 217 | updateHostPorts(link, false); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 218 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 219 | |
| 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 Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 235 | // 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 242 | |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 243 | for (Link ulink : getBidiComponentLinks(link)) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 244 | log.info("-- Starting optimized route-path processing for component " |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 245 | + "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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 266 | } else { |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 267 | log.warn("group handler not found for dev:{} when removing link: {}", |
| 268 | ulink.src().deviceId(), ulink); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 269 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 270 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 271 | } |
| 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 Luigi | d8a1516 | 2018-02-15 16:33:08 +0100 | [diff] [blame] | 280 | boolean isLinkValid(Link link) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 281 | 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 Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 297 | if (devConfig == null) { |
| 298 | log.warn("Cannot check validity of link without device config"); |
| 299 | return true; |
| 300 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 301 | try { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 302 | /*if (!devConfig.isEdgeDevice(srcId) |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 303 | && !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 Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 309 | }*/ |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 310 | 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 Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 339 | * 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 343 | * |
| 344 | * @param link the link to be processed |
| 345 | * @param added true if link was added, false if link was removed |
| 346 | */ |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 347 | private void updateHostPorts(Link link, boolean added) { |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 348 | DeviceConfiguration devConfig = srManager.deviceConfiguration; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 349 | if (added) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 350 | 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 360 | Set<PortNumber> p = downedPortStore.remove(link.src().deviceId()); |
| 361 | if (p != null) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 362 | 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 365 | p.forEach(pnum -> srManager.deviceAdminService |
| 366 | .changePortState(link.src().deviceId(), pnum, true)); |
| 367 | } |
| 368 | } else { |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 369 | // 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 Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 377 | if (!lastUplink(link)) { |
| 378 | return; |
| 379 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 380 | // find dual homed hosts on this dev to disable |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 381 | 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 406 | // update global store |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 407 | Set<PortNumber> p = downedPortStore.get(dev); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 408 | if (p == null) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 409 | p = dp; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 410 | } else { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 411 | p.addAll(dp); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 412 | } |
| 413 | downedPortStore.put(link.src().deviceId(), p); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 419 | * Returns true if given link was the last active uplink from src-device of |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 420 | * link. An uplink is defined as a unidirectional link with src as |
| 421 | * edgeRouter and dst as non-edgeRouter. |
| 422 | * |
| 423 | * @param link |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 424 | * @return true if given link was the last uplink from the src device |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 425 | */ |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 426 | private boolean lastUplink(Link link) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 427 | DeviceConfiguration devConfig = srManager.deviceConfiguration; |
| 428 | try { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 429 | if (!devConfig.isEdgeDevice(link.src().deviceId()) |
| 430 | || devConfig.isEdgeDevice(link.dst().deviceId())) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 431 | return false; |
| 432 | } |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 433 | // 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 443 | for (Link l : devLinks) { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 444 | if (devConfig.isEdgeDevice(l.dst().deviceId())) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 445 | continue; |
| 446 | } |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 447 | log.debug("Found another active uplink {}", l); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 448 | return false; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 449 | } |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 450 | log.debug("No active uplink found"); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 451 | return true; |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 452 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 453 | log.warn("Unable to determine if link was the last uplink" |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 454 | + 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 Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 503 | seenBefore.remove(link); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 504 | } |
| 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 Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 532 | * 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 537 | * |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 538 | * @param link the infrastructure (unidirectional) link being queried |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 539 | * @return true if another unidirectional link exists in the reverse |
| 540 | * direction, has been seen-before and is up |
| 541 | */ |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 542 | boolean isBidirectionalLinkUp(Link link) { |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 543 | // cannot call linkService as link may be gone |
| 544 | Link reverseLink = getReverseLink(link); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 545 | 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 Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 556 | * 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 619 | * 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 Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 669 | * has no viable uplinks. The caller needs to determine if such behavior is |
| 670 | * desired for the single or dual-homed host. |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 671 | * |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 672 | * @param loc the host location |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 673 | */ |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 674 | void checkUplinksForHost(HostLocation loc) { |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 675 | // 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 682 | 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 Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 696 | log.warn("Host location {} has no valid uplinks disabling port", loc); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 697 | 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 | |
pier | 3a60ee9 | 2019-12-20 22:12:57 +0100 | [diff] [blame] | 708 | 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 Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 718 | 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 Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 726 | /** |
| 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 Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 742 | } |