sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 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 | package org.onosproject.segmentrouting; |
| 17 | |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableMap.Builder; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 26 | import org.onlab.packet.Ip6Address; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IpPrefix; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 28 | import org.onosproject.cluster.NodeId; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.DeviceId; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 32 | import org.onosproject.net.Link; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 33 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 34 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 35 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.HashMap; |
| 41 | import java.util.HashSet; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 42 | import java.util.Iterator; |
| 43 | import java.util.Map; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 44 | import java.util.Objects; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 45 | import java.util.Set; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 46 | import java.util.concurrent.ScheduledExecutorService; |
| 47 | import java.util.concurrent.TimeUnit; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 48 | import java.util.concurrent.locks.Lock; |
| 49 | import java.util.concurrent.locks.ReentrantLock; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 50 | |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 51 | import static com.google.common.base.MoreObjects.toStringHelper; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 52 | import static com.google.common.base.Preconditions.checkNotNull; |
| 53 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 54 | import static org.onlab.util.Tools.groupedThreads; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 55 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Default routing handler that is responsible for route computing and |
| 58 | * routing rule population. |
| 59 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 60 | public class DefaultRoutingHandler { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 61 | private static final int MAX_CONSTANT_RETRY_ATTEMPTS = 5; |
| 62 | private static final int RETRY_INTERVAL_MS = 250; |
| 63 | private static final int RETRY_INTERVAL_SCALE = 1; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 64 | private static Logger log = LoggerFactory.getLogger(DefaultRoutingHandler.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 65 | |
| 66 | private SegmentRoutingManager srManager; |
| 67 | private RoutingRulePopulator rulePopulator; |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 68 | private HashMap<DeviceId, EcmpShortestPathGraph> currentEcmpSpgMap; |
| 69 | private HashMap<DeviceId, EcmpShortestPathGraph> updatedEcmpSpgMap; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 70 | private DeviceConfiguration config; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 71 | private final Lock statusLock = new ReentrantLock(); |
| 72 | private volatile Status populationStatus; |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 73 | private ScheduledExecutorService executorService |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 74 | = newScheduledThreadPool(1, groupedThreads("retryftr", "retry-%d", log)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Represents the default routing population status. |
| 78 | */ |
| 79 | public enum Status { |
| 80 | // population process is not started yet. |
| 81 | IDLE, |
| 82 | |
| 83 | // population process started. |
| 84 | STARTED, |
| 85 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 86 | // population process was aborted due to errors, mostly for groups not |
| 87 | // found. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 88 | ABORTED, |
| 89 | |
| 90 | // population process was finished successfully. |
| 91 | SUCCEEDED |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Creates a DefaultRoutingHandler object. |
| 96 | * |
| 97 | * @param srManager SegmentRoutingManager object |
| 98 | */ |
| 99 | public DefaultRoutingHandler(SegmentRoutingManager srManager) { |
| 100 | this.srManager = srManager; |
| 101 | this.rulePopulator = checkNotNull(srManager.routingRulePopulator); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 102 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 103 | this.populationStatus = Status.IDLE; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 104 | this.currentEcmpSpgMap = Maps.newHashMap(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 108 | * Returns an immutable copy of the current ECMP shortest-path graph as |
| 109 | * computed by this controller instance. |
| 110 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 111 | * @return immutable copy of the current ECMP graph |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 112 | */ |
| 113 | public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEmcpSpgMap() { |
| 114 | Builder<DeviceId, EcmpShortestPathGraph> builder = ImmutableMap.builder(); |
| 115 | currentEcmpSpgMap.entrySet().forEach(entry -> { |
| 116 | if (entry.getValue() != null) { |
| 117 | builder.put(entry.getKey(), entry.getValue()); |
| 118 | } |
| 119 | }); |
| 120 | return builder.build(); |
| 121 | } |
| 122 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 123 | ////////////////////////////////////// |
| 124 | // Route path handling |
| 125 | ////////////////////////////////////// |
| 126 | |
| 127 | /* The following three methods represent the three major ways in routing |
| 128 | * is triggered in the network |
| 129 | * a) due to configuration change |
| 130 | * b) due to route-added event |
| 131 | * c) due to change in the topology |
| 132 | */ |
| 133 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 134 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 135 | * Populates all routing rules to all switches. Typically triggered at |
| 136 | * startup or after a configuration event. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 137 | */ |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 138 | public void populateAllRoutingRules() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 139 | statusLock.lock(); |
| 140 | try { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 141 | if (populationStatus == Status.STARTED) { |
| 142 | log.warn("Previous rule population is not finished. Cannot" |
| 143 | + " proceed with populateAllRoutingRules"); |
| 144 | return; |
| 145 | } |
| 146 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 147 | populationStatus = Status.STARTED; |
| 148 | rulePopulator.resetCounter(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 149 | log.info("Starting to populate all routing rules"); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 150 | log.debug("populateAllRoutingRules: populationStatus is STARTED"); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 151 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 152 | // take a snapshot of the topology |
| 153 | updatedEcmpSpgMap = new HashMap<>(); |
| 154 | Set<EdgePair> edgePairs = new HashSet<>(); |
| 155 | Set<ArrayList<DeviceId>> routeChanges = new HashSet<>(); |
| 156 | for (Device dstSw : srManager.deviceService.getDevices()) { |
| 157 | EcmpShortestPathGraph ecmpSpgUpdated = |
| 158 | new EcmpShortestPathGraph(dstSw.id(), srManager); |
| 159 | updatedEcmpSpgMap.put(dstSw.id(), ecmpSpgUpdated); |
| 160 | DeviceId pairDev = getPairDev(dstSw.id()); |
| 161 | if (pairDev != null) { |
| 162 | // pairDev may not be available yet, but we still need to add |
| 163 | ecmpSpgUpdated = new EcmpShortestPathGraph(pairDev, srManager); |
| 164 | updatedEcmpSpgMap.put(pairDev, ecmpSpgUpdated); |
| 165 | edgePairs.add(new EdgePair(dstSw.id(), pairDev)); |
| 166 | } |
| 167 | DeviceId ret = shouldHandleRouting(dstSw.id()); |
| 168 | if (ret == null) { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 169 | continue; |
| 170 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 171 | Set<DeviceId> devsToProcess = Sets.newHashSet(dstSw.id(), ret); |
| 172 | // To do a full reroute, assume all routes have changed |
| 173 | for (DeviceId dev : devsToProcess) { |
| 174 | for (Device targetSw : srManager.deviceService.getDevices()) { |
| 175 | if (targetSw.id().equals(dev)) { |
| 176 | continue; |
| 177 | } |
| 178 | routeChanges.add(Lists.newArrayList(targetSw.id(), dev)); |
| 179 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 180 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 181 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 182 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 183 | if (!redoRouting(routeChanges, edgePairs, null)) { |
| 184 | log.debug("populateAllRoutingRules: populationStatus is ABORTED"); |
| 185 | populationStatus = Status.ABORTED; |
| 186 | log.warn("Failed to repopulate all routing rules."); |
| 187 | return; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 188 | } |
| 189 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 190 | log.debug("populateAllRoutingRules: populationStatus is SUCCEEDED"); |
| 191 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 192 | log.info("Completed all routing rule population. Total # of rules pushed : {}", |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 193 | rulePopulator.getCounter()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 194 | return; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 195 | } finally { |
| 196 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 197 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 198 | } |
| 199 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 200 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 201 | * Populate rules from all other edge devices to the connect-point(s) |
| 202 | * specified for the given subnets. |
| 203 | * |
| 204 | * @param cpts connect point(s) of the subnets being added |
| 205 | * @param subnets subnets being added |
| 206 | */ //XXX refactor |
| 207 | protected void populateSubnet(Set<ConnectPoint> cpts, Set<IpPrefix> subnets) { |
| 208 | statusLock.lock(); |
| 209 | try { |
| 210 | if (populationStatus == Status.STARTED) { |
| 211 | log.warn("Previous rule population is not finished. Cannot" |
| 212 | + " proceed with routing rules for added routes"); |
| 213 | return; |
| 214 | } |
| 215 | populationStatus = Status.STARTED; |
| 216 | rulePopulator.resetCounter(); |
| 217 | log.info("Starting to populate routing rules for added routes"); |
| 218 | // Take snapshots of the topology |
| 219 | updatedEcmpSpgMap = new HashMap<>(); |
| 220 | Set<EdgePair> edgePairs = new HashSet<>(); |
| 221 | Set<ArrayList<DeviceId>> routeChanges = new HashSet<>(); |
| 222 | boolean handleRouting = false; |
| 223 | |
| 224 | if (cpts.size() == 2) { |
| 225 | // ensure connect points are edge-pairs |
| 226 | Iterator<ConnectPoint> iter = cpts.iterator(); |
| 227 | DeviceId dev1 = iter.next().deviceId(); |
| 228 | DeviceId pairDev = getPairDev(dev1); |
| 229 | if (iter.next().deviceId().equals(pairDev)) { |
| 230 | edgePairs.add(new EdgePair(dev1, pairDev)); |
| 231 | } else { |
| 232 | log.warn("Connectpoints {} for subnets {} not on " |
| 233 | + "pair-devices.. aborting populateSubnet", cpts, subnets); |
| 234 | populationStatus = Status.ABORTED; |
| 235 | return; |
| 236 | } |
| 237 | for (ConnectPoint cp : cpts) { |
| 238 | EcmpShortestPathGraph ecmpSpgUpdated = |
| 239 | new EcmpShortestPathGraph(cp.deviceId(), srManager); |
| 240 | updatedEcmpSpgMap.put(cp.deviceId(), ecmpSpgUpdated); |
| 241 | DeviceId retId = shouldHandleRouting(cp.deviceId()); |
| 242 | if (retId == null) { |
| 243 | continue; |
| 244 | } |
| 245 | handleRouting = true; |
| 246 | } |
| 247 | } else { |
| 248 | // single connect point |
| 249 | DeviceId dstSw = cpts.iterator().next().deviceId(); |
| 250 | EcmpShortestPathGraph ecmpSpgUpdated = |
| 251 | new EcmpShortestPathGraph(dstSw, srManager); |
| 252 | updatedEcmpSpgMap.put(dstSw, ecmpSpgUpdated); |
| 253 | if (srManager.mastershipService.isLocalMaster(dstSw)) { |
| 254 | handleRouting = true; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (!handleRouting) { |
| 259 | log.debug("This instance is not handling ecmp routing to the " |
| 260 | + "connectPoint(s) {}", cpts); |
| 261 | populationStatus = Status.ABORTED; |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | // if it gets here, this instance should handle routing for the |
| 266 | // connectpoint(s). Assume all route-paths have to be updated to |
| 267 | // the connectpoint(s) with the following exceptions |
| 268 | // 1. if target is non-edge no need for routing rules |
| 269 | // 2. if target is one of the connectpoints |
| 270 | for (ConnectPoint cp : cpts) { |
| 271 | DeviceId dstSw = cp.deviceId(); |
| 272 | for (Device targetSw : srManager.deviceService.getDevices()) { |
| 273 | boolean isEdge = false; |
| 274 | try { |
| 275 | isEdge = config.isEdgeDevice(targetSw.id()); |
| 276 | } catch (DeviceConfigNotFoundException e) { |
| 277 | log.warn(e.getMessage() + "aborting populateSubnet"); |
| 278 | populationStatus = Status.ABORTED; |
| 279 | return; |
| 280 | } |
| 281 | if (dstSw.equals(targetSw.id()) || !isEdge || |
| 282 | (cpts.size() == 2 && |
| 283 | targetSw.id().equals(getPairDev(dstSw)))) { |
| 284 | continue; |
| 285 | } |
| 286 | routeChanges.add(Lists.newArrayList(targetSw.id(), dstSw)); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (!redoRouting(routeChanges, edgePairs, subnets)) { |
| 291 | log.debug("populateSubnet: populationStatus is ABORTED"); |
| 292 | populationStatus = Status.ABORTED; |
| 293 | log.warn("Failed to repopulate the rules for subnet."); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | log.debug("populateSubnet: populationStatus is SUCCEEDED"); |
| 298 | populationStatus = Status.SUCCEEDED; |
| 299 | log.info("Completed subnet population. Total # of rules pushed : {}", |
| 300 | rulePopulator.getCounter()); |
| 301 | return; |
| 302 | |
| 303 | } finally { |
| 304 | statusLock.unlock(); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 309 | * Populates the routing rules or makes hash group changes according to the |
| 310 | * route-path changes due to link failure, switch failure or link up. This |
| 311 | * method should only be called for one of these three possible event-types. |
| 312 | * Note that when a switch goes away, all of its links fail as well, |
| 313 | * but this is handled as a single switch removal event. |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 314 | * |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 315 | * @param linkDown the single failed link, or null for other conditions |
| 316 | * such as link-up or a removed switch |
| 317 | * @param linkUp the single link up, or null for other conditions such as |
| 318 | * link-down or a removed switch |
| 319 | * @param switchDown the removed switch, or null for other conditions such as |
| 320 | * link-down or link-up |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 321 | */ // refactor |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 322 | public void populateRoutingRulesForLinkStatusChange(Link linkDown, |
| 323 | Link linkUp, |
| 324 | DeviceId switchDown) { |
| 325 | if ((linkDown != null && (linkUp != null || switchDown != null)) || |
| 326 | (linkUp != null && (linkDown != null || switchDown != null)) || |
| 327 | (switchDown != null && (linkUp != null || linkDown != null))) { |
| 328 | log.warn("Only one event can be handled for link status change .. aborting"); |
| 329 | return; |
| 330 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 331 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 332 | statusLock.lock(); |
| 333 | try { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 334 | |
| 335 | if (populationStatus == Status.STARTED) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 336 | log.warn("Previous rule population is not finished. Cannot" |
| 337 | + " proceeed with routingRules for Link Status change"); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 338 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 341 | // Take snapshots of the topology |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 342 | updatedEcmpSpgMap = new HashMap<>(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 343 | Set<EdgePair> edgePairs = new HashSet<>(); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 344 | for (Device sw : srManager.deviceService.getDevices()) { |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 345 | EcmpShortestPathGraph ecmpSpgUpdated = |
| 346 | new EcmpShortestPathGraph(sw.id(), srManager); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 347 | updatedEcmpSpgMap.put(sw.id(), ecmpSpgUpdated); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 348 | DeviceId pairDev = getPairDev(sw.id()); |
| 349 | if (pairDev != null) { |
| 350 | // pairDev may not be available yet, but we still need to add |
| 351 | ecmpSpgUpdated = new EcmpShortestPathGraph(pairDev, srManager); |
| 352 | updatedEcmpSpgMap.put(pairDev, ecmpSpgUpdated); |
| 353 | edgePairs.add(new EdgePair(sw.id(), pairDev)); |
| 354 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 357 | log.info("Starting to populate routing rules from link status change"); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 358 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 359 | Set<ArrayList<DeviceId>> routeChanges; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 360 | log.debug("populateRoutingRulesForLinkStatusChange: " |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 361 | + "populationStatus is STARTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 362 | populationStatus = Status.STARTED; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 363 | rulePopulator.resetCounter(); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 364 | // try optimized re-routing |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 365 | if (linkDown == null) { |
| 366 | // either a linkUp or a switchDown - compute all route changes by |
| 367 | // comparing all routes of existing ECMP SPG to new ECMP SPG |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 368 | routeChanges = computeRouteChange(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 369 | |
| 370 | if (routeChanges != null) { |
| 371 | // deal with linkUp of a seen-before link |
| 372 | if (linkUp != null && srManager.isSeenLink(linkUp)) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 373 | if (!srManager.isBidirectional(linkUp)) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 374 | log.warn("Not a bidirectional link yet .. not " |
| 375 | + "processing link {}", linkUp); |
| 376 | srManager.updateSeenLink(linkUp, true); |
| 377 | populationStatus = Status.ABORTED; |
| 378 | return; |
| 379 | } |
| 380 | // link previously seen before |
| 381 | // do hash-bucket changes instead of a re-route |
| 382 | processHashGroupChange(routeChanges, false, null); |
| 383 | // clear out routesChanges so a re-route is not attempted |
| 384 | routeChanges = ImmutableSet.of(); |
| 385 | } |
| 386 | |
| 387 | //deal with switchDown |
| 388 | if (switchDown != null) { |
| 389 | processHashGroupChange(routeChanges, true, switchDown); |
| 390 | // clear out routesChanges so a re-route is not attempted |
| 391 | routeChanges = ImmutableSet.of(); |
| 392 | } |
| 393 | |
| 394 | // for a linkUp of a never-seen-before link |
| 395 | // let it fall through to a reroute of the routeChanges |
| 396 | |
| 397 | } |
| 398 | |
| 399 | // now that we are past the check for a previously seen link |
| 400 | // it is safe to update the store for the linkUp |
| 401 | if (linkUp != null) { |
| 402 | srManager.updateSeenLink(linkUp, true); |
| 403 | } |
| 404 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 405 | } else { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 406 | // link has gone down |
| 407 | // Compare existing ECMP SPG only with the link that went down |
| 408 | routeChanges = computeDamagedRoutes(linkDown); |
| 409 | if (routeChanges != null) { |
| 410 | processHashGroupChange(routeChanges, true, null); |
| 411 | // clear out routesChanges so a re-route is not attempted |
| 412 | routeChanges = ImmutableSet.of(); |
| 413 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 416 | // do full re-routing if optimized routing returns null routeChanges |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 417 | if (routeChanges == null) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 418 | log.info("Optimized routing failed... opting for full reroute"); |
| 419 | populationStatus = Status.ABORTED; |
| 420 | statusLock.unlock(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 421 | populateAllRoutingRules(); |
| 422 | return; |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 423 | } |
| 424 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 425 | if (routeChanges.isEmpty()) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 426 | log.info("No re-route attempted for the link status change"); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 427 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 428 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 429 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 432 | // reroute of routeChanges |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 433 | if (redoRouting(routeChanges, edgePairs, null)) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 434 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 435 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 436 | log.info("Completed repopulation of rules for link-status change." |
| 437 | + " # of rules populated : {}", rulePopulator.getCounter()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 438 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 439 | } else { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 440 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is ABORTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 441 | populationStatus = Status.ABORTED; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 442 | log.warn("Failed to repopulate the rules for link status change."); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 443 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 444 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 445 | } finally { |
| 446 | statusLock.unlock(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 450 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 451 | * Processes a set a route-path changes by reprogramming routing rules and |
| 452 | * creating new hash-groups or editing them if necessary. This method also |
| 453 | * determines the next-hops for the route-path from the src-switch (target) |
| 454 | * of the path towards the dst-switch of the path. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 455 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 456 | * @param routeChanges a set of route-path changes, where each route-path is |
| 457 | * a list with its first element the src-switch (target) |
| 458 | * of the path, and the second element the dst-switch of |
| 459 | * the path. |
| 460 | * @param edgePairs a set of edge-switches that are paired by configuration |
| 461 | * @param subnets a set of prefixes that need to be populated in the routing |
| 462 | * table of the target switch in the route-path. Can be null, |
| 463 | * in which case all the prefixes belonging to the dst-switch |
| 464 | * will be populated in the target switch |
| 465 | * @return true if successful in repopulating all routes |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 466 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 467 | private boolean redoRouting(Set<ArrayList<DeviceId>> routeChanges, |
| 468 | Set<EdgePair> edgePairs, Set<IpPrefix> subnets) { |
| 469 | // first make every entry two-elements |
| 470 | Set<ArrayList<DeviceId>> changedRoutes = new HashSet<>(); |
| 471 | for (ArrayList<DeviceId> route : routeChanges) { |
| 472 | if (route.size() == 1) { |
| 473 | DeviceId dstSw = route.get(0); |
| 474 | EcmpShortestPathGraph ec = updatedEcmpSpgMap.get(dstSw); |
| 475 | if (ec == null) { |
| 476 | log.warn("No graph found for {} .. aborting redoRouting", dstSw); |
| 477 | return false; |
| 478 | } |
| 479 | ec.getAllLearnedSwitchesAndVia().keySet().forEach(key -> { |
| 480 | ec.getAllLearnedSwitchesAndVia().get(key).keySet().forEach(target -> { |
| 481 | changedRoutes.add(Lists.newArrayList(target, dstSw)); |
| 482 | }); |
| 483 | }); |
| 484 | } else { |
| 485 | DeviceId targetSw = route.get(0); |
| 486 | DeviceId dstSw = route.get(1); |
| 487 | changedRoutes.add(Lists.newArrayList(targetSw, dstSw)); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // now process changedRoutes according to edgePairs |
| 492 | if (!redoRoutingEdgePairs(edgePairs, subnets, changedRoutes)) { |
| 493 | return false; //abort routing and fail fast |
| 494 | } |
| 495 | |
| 496 | // whatever is left in changedRoutes is now processed for individual dsts. |
| 497 | if (!redoRoutingIndividualDests(subnets, changedRoutes)) { |
| 498 | return false; //abort routing and fail fast |
| 499 | } |
| 500 | |
| 501 | //XXX should we do hashgroupchanges here? |
| 502 | |
| 503 | // update ecmpSPG for all edge-pairs |
| 504 | for (EdgePair ep : edgePairs) { |
| 505 | currentEcmpSpgMap.put(ep.dev1, updatedEcmpSpgMap.get(ep.dev1)); |
| 506 | currentEcmpSpgMap.put(ep.dev2, updatedEcmpSpgMap.get(ep.dev2)); |
| 507 | log.debug("Updating ECMPspg for edge-pair:{}-{}", ep.dev1, ep.dev2); |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Programs targetSw in the changedRoutes for given prefixes reachable by |
| 514 | * an edgePair. If no prefixes are given, the method will use configured |
| 515 | * subnets/prefixes. If some configured subnets belong only to a specific |
| 516 | * destination in the edgePair, then the target switch will be programmed |
| 517 | * only to that destination. |
| 518 | * |
| 519 | * @param edgePairs set of edge-pairs for which target will be programmed |
| 520 | * @param subnets a set of prefixes that need to be populated in the routing |
| 521 | * table of the target switch in the changedRoutes. Can be null, |
| 522 | * in which case all the configured prefixes belonging to the |
| 523 | * paired switches will be populated in the target switch |
| 524 | * @param changedRoutes a set of route-path changes, where each route-path is |
| 525 | * a list with its first element the src-switch (target) |
| 526 | * of the path, and the second element the dst-switch of |
| 527 | * the path. |
| 528 | * @return true if successful |
| 529 | */ |
| 530 | private boolean redoRoutingEdgePairs(Set<EdgePair> edgePairs, |
| 531 | Set<IpPrefix> subnets, |
| 532 | Set<ArrayList<DeviceId>> changedRoutes) { |
| 533 | for (EdgePair ep : edgePairs) { |
| 534 | // temp store for a target's changedRoutes to this edge-pair |
| 535 | Map<DeviceId, Set<ArrayList<DeviceId>>> targetRoutes = new HashMap<>(); |
| 536 | Iterator<ArrayList<DeviceId>> i = changedRoutes.iterator(); |
| 537 | while (i.hasNext()) { |
| 538 | ArrayList<DeviceId> route = i.next(); |
| 539 | DeviceId dstSw = route.get(1); |
| 540 | if (ep.includes(dstSw)) { |
| 541 | // routeChange for edge pair found |
| 542 | // sort by target iff target is edge and remove from changedRoutes |
| 543 | DeviceId targetSw = route.get(0); |
| 544 | try { |
| 545 | if (!srManager.deviceConfiguration.isEdgeDevice(targetSw)) { |
| 546 | continue; |
| 547 | } |
| 548 | } catch (DeviceConfigNotFoundException e) { |
| 549 | log.warn(e.getMessage() + "aborting redoRouting"); |
| 550 | return false; |
| 551 | } |
| 552 | // route is from another edge to this edge-pair |
| 553 | if (targetRoutes.containsKey(targetSw)) { |
| 554 | targetRoutes.get(targetSw).add(route); |
| 555 | } else { |
| 556 | Set<ArrayList<DeviceId>> temp = new HashSet<>(); |
| 557 | temp.add(route); |
| 558 | targetRoutes.put(targetSw, temp); |
| 559 | } |
| 560 | i.remove(); |
| 561 | } |
| 562 | } |
| 563 | // so now for this edgepair we have a per target set of routechanges |
| 564 | // process target->edgePair route |
| 565 | for (Map.Entry<DeviceId, Set<ArrayList<DeviceId>>> entry : |
| 566 | targetRoutes.entrySet()) { |
| 567 | log.debug("* redoRoutingDstPair Target:{} -> edge-pair {}", |
| 568 | entry.getKey(), ep); |
| 569 | DeviceId targetSw = entry.getKey(); |
| 570 | Map<DeviceId, Set<DeviceId>> perDstNextHops = new HashMap<>(); |
| 571 | entry.getValue().forEach(route -> { |
| 572 | Set<DeviceId> nhops = getNextHops(route.get(0), route.get(1)); |
| 573 | log.debug("route: target {} -> dst {} found with next-hops {}", |
| 574 | route.get(0), route.get(1), nhops); |
| 575 | perDstNextHops.put(route.get(1), nhops); |
| 576 | }); |
| 577 | Set<IpPrefix> ipDev1 = (subnets == null) ? config.getSubnets(ep.dev1) |
| 578 | : subnets; |
| 579 | Set<IpPrefix> ipDev2 = (subnets == null) ? config.getSubnets(ep.dev2) |
| 580 | : subnets; |
| 581 | ipDev1 = (ipDev1 == null) ? Sets.newHashSet() : ipDev1; |
| 582 | ipDev2 = (ipDev2 == null) ? Sets.newHashSet() : ipDev2; |
| 583 | // handle routing to subnets common to edge-pair |
| 584 | // only if the targetSw is not part of the edge-pair |
| 585 | if (!ep.includes(targetSw)) { |
| 586 | if (!populateEcmpRoutingRulePartial( |
| 587 | targetSw, |
| 588 | ep.dev1, ep.dev2, |
| 589 | perDstNextHops, |
| 590 | Sets.intersection(ipDev1, ipDev2))) { |
| 591 | return false; // abort everything and fail fast |
| 592 | } |
| 593 | } |
| 594 | // handle routing to subnets that only belong to dev1 |
| 595 | Set<IpPrefix> onlyDev1Subnets = Sets.difference(ipDev1, ipDev2); |
| 596 | if (!onlyDev1Subnets.isEmpty() && perDstNextHops.get(ep.dev1) != null) { |
| 597 | Map<DeviceId, Set<DeviceId>> onlyDev1NextHops = new HashMap<>(); |
| 598 | onlyDev1NextHops.put(ep.dev1, perDstNextHops.get(ep.dev1)); |
| 599 | if (!populateEcmpRoutingRulePartial( |
| 600 | targetSw, |
| 601 | ep.dev1, null, |
| 602 | onlyDev1NextHops, |
| 603 | onlyDev1Subnets)) { |
| 604 | return false; // abort everything and fail fast |
| 605 | } |
| 606 | } |
| 607 | // handle routing to subnets that only belong to dev2 |
| 608 | Set<IpPrefix> onlyDev2Subnets = Sets.difference(ipDev2, ipDev1); |
| 609 | if (!onlyDev2Subnets.isEmpty() && perDstNextHops.get(ep.dev2) != null) { |
| 610 | Map<DeviceId, Set<DeviceId>> onlyDev2NextHops = new HashMap<>(); |
| 611 | onlyDev2NextHops.put(ep.dev2, perDstNextHops.get(ep.dev2)); |
| 612 | if (!populateEcmpRoutingRulePartial( |
| 613 | targetSw, |
| 614 | ep.dev2, null, |
| 615 | onlyDev2NextHops, |
| 616 | onlyDev2Subnets)) { |
| 617 | return false; // abort everything and fail fast |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | // if it gets here it has succeeded for all targets to this edge-pair |
| 622 | } |
| 623 | return true; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Programs targetSw in the changedRoutes for given prefixes reachable by |
| 628 | * a destination switch that is not part of an edge-pair. |
| 629 | * If no prefixes are given, the method will use configured subnets/prefixes. |
| 630 | * |
| 631 | * @param subnets a set of prefixes that need to be populated in the routing |
| 632 | * table of the target switch in the changedRoutes. Can be null, |
| 633 | * in which case all the configured prefixes belonging to the |
| 634 | * paired switches will be populated in the target switch |
| 635 | * @param changedRoutes a set of route-path changes, where each route-path is |
| 636 | * a list with its first element the src-switch (target) |
| 637 | * of the path, and the second element the dst-switch of |
| 638 | * the path. |
| 639 | * @return true if successful |
| 640 | */ |
| 641 | private boolean redoRoutingIndividualDests(Set<IpPrefix> subnets, |
| 642 | Set<ArrayList<DeviceId>> changedRoutes) { |
| 643 | // aggregate route-path changes for each dst device |
| 644 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> routesBydevice = |
| 645 | new HashMap<>(); |
| 646 | for (ArrayList<DeviceId> route: changedRoutes) { |
| 647 | DeviceId dstSw = route.get(1); |
| 648 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 649 | routesBydevice.get(dstSw); |
| 650 | if (deviceRoutes == null) { |
| 651 | deviceRoutes = new ArrayList<>(); |
| 652 | routesBydevice.put(dstSw, deviceRoutes); |
| 653 | } |
| 654 | deviceRoutes.add(route); |
| 655 | } |
| 656 | for (DeviceId impactedDstDevice : routesBydevice.keySet()) { |
| 657 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 658 | routesBydevice.get(impactedDstDevice); |
| 659 | for (ArrayList<DeviceId> route: deviceRoutes) { |
| 660 | log.debug("* redoRoutingIndiDst Target: {} -> dst: {}", |
| 661 | route.get(0), route.get(1)); |
| 662 | DeviceId targetSw = route.get(0); |
| 663 | DeviceId dstSw = route.get(1); // same as impactedDstDevice |
| 664 | Set<DeviceId> nextHops = getNextHops(targetSw, dstSw); |
| 665 | Map<DeviceId, Set<DeviceId>> nhops = new HashMap<>(); |
| 666 | nhops.put(dstSw, nextHops); |
| 667 | if (!populateEcmpRoutingRulePartial(targetSw, dstSw, null, nhops, |
| 668 | (subnets == null) ? Sets.newHashSet() : subnets)) { |
| 669 | return false; // abort routing and fail fast |
| 670 | } |
| 671 | log.debug("Populating flow rules from target: {} to dst: {}" |
| 672 | + " is successful", targetSw, dstSw); |
| 673 | } |
| 674 | //Only if all the flows for all impacted routes to a |
| 675 | //specific target are pushed successfully, update the |
| 676 | //ECMP graph for that target. Or else the next event |
| 677 | //would not see any changes in the ECMP graphs. |
| 678 | //In another case, the target switch has gone away, so |
| 679 | //routes can't be installed. In that case, the current map |
| 680 | //is updated here, without any flows being pushed. |
| 681 | currentEcmpSpgMap.put(impactedDstDevice, |
| 682 | updatedEcmpSpgMap.get(impactedDstDevice)); |
| 683 | log.debug("Updating ECMPspg for impacted dev:{}", impactedDstDevice); |
| 684 | } |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Populate ECMP rules for subnets from target to destination via nexthops. |
| 690 | * |
| 691 | * @param targetSw Device ID of target switch in which rules will be programmed |
| 692 | * @param destSw1 Device ID of final destination switch to which the rules will forward |
| 693 | * @param destSw2 Device ID of paired destination switch to which the rules will forward |
| 694 | * A null deviceId indicates packets should only be sent to destSw1 |
| 695 | * @param nextHops Map indication a list of next hops per destSw |
| 696 | * @param subnets Subnets to be populated. If empty, populate all configured subnets. |
| 697 | * @return true if it succeeds in populating rules |
| 698 | */ // refactor |
| 699 | private boolean populateEcmpRoutingRulePartial(DeviceId targetSw, |
| 700 | DeviceId destSw1, |
| 701 | DeviceId destSw2, |
| 702 | Map<DeviceId, Set<DeviceId>> nextHops, |
| 703 | Set<IpPrefix> subnets) { |
| 704 | boolean result; |
| 705 | // If both target switch and dest switch are edge routers, then set IP |
| 706 | // rule for both subnet and router IP. |
| 707 | boolean targetIsEdge; |
| 708 | boolean dest1IsEdge; |
| 709 | Ip4Address dest1RouterIpv4, dest2RouterIpv4 = null; |
| 710 | Ip6Address dest1RouterIpv6, dest2RouterIpv6 = null; |
| 711 | |
| 712 | try { |
| 713 | targetIsEdge = config.isEdgeDevice(targetSw); |
| 714 | dest1IsEdge = config.isEdgeDevice(destSw1); |
| 715 | dest1RouterIpv4 = config.getRouterIpv4(destSw1); |
| 716 | dest1RouterIpv6 = config.getRouterIpv6(destSw1); |
| 717 | if (destSw2 != null) { |
| 718 | dest2RouterIpv4 = config.getRouterIpv4(destSw2); |
| 719 | dest2RouterIpv6 = config.getRouterIpv6(destSw2); |
| 720 | } |
| 721 | } catch (DeviceConfigNotFoundException e) { |
| 722 | log.warn(e.getMessage() + " Aborting populateEcmpRoutingRulePartial."); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 723 | return false; |
| 724 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 725 | |
| 726 | if (targetIsEdge && dest1IsEdge) { |
| 727 | subnets = (subnets != null && !subnets.isEmpty()) |
| 728 | ? Sets.newHashSet(subnets) |
| 729 | : Sets.newHashSet(config.getSubnets(destSw1)); |
| 730 | // XXX - Rethink this |
| 731 | /*subnets.add(dest1RouterIpv4.toIpPrefix()); |
| 732 | if (dest1RouterIpv6 != null) { |
| 733 | subnets.add(dest1RouterIpv6.toIpPrefix()); |
| 734 | } |
| 735 | if (destSw2 != null && dest2RouterIpv4 != null) { |
| 736 | subnets.add(dest2RouterIpv4.toIpPrefix()); |
| 737 | if (dest2RouterIpv6 != null) { |
| 738 | subnets.add(dest2RouterIpv6.toIpPrefix()); |
| 739 | } |
| 740 | }*/ |
| 741 | log.debug(". populateEcmpRoutingRulePartial in device {} towards {} {} " |
| 742 | + "for subnets {}", targetSw, destSw1, |
| 743 | (destSw2 != null) ? ("& " + destSw2) : "", |
| 744 | subnets); |
| 745 | result = rulePopulator.populateIpRuleForSubnet(targetSw, subnets, |
| 746 | destSw1, destSw2, |
| 747 | nextHops); |
| 748 | if (!result) { |
| 749 | return false; |
| 750 | } |
| 751 | /* XXX rethink this |
| 752 | IpPrefix routerIpPrefix = destRouterIpv4.toIpPrefix(); |
| 753 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} " |
| 754 | + "for router IP {}", targetSw, destSw, routerIpPrefix); |
| 755 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, |
| 756 | destSw, nextHops); |
| 757 | if (!result) { |
| 758 | return false; |
| 759 | } |
| 760 | // If present we deal with IPv6 loopback. |
| 761 | if (destRouterIpv6 != null) { |
| 762 | routerIpPrefix = destRouterIpv6.toIpPrefix(); |
| 763 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {}" |
| 764 | + " for v6 router IP {}", targetSw, destSw, routerIpPrefix); |
| 765 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, |
| 766 | destSw, nextHops); |
| 767 | if (!result) { |
| 768 | return false; |
| 769 | } |
| 770 | }*/ |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 771 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 772 | |
| 773 | if (!targetIsEdge && dest1IsEdge) { |
| 774 | // MPLS rules in all non-edge target devices. These rules are for |
| 775 | // individual destinations, even if the dsts are part of edge-pairs. |
| 776 | log.debug(". populateEcmpRoutingRulePartial in device{} towards {} for " |
| 777 | + "all MPLS rules", targetSw, destSw1); |
| 778 | result = rulePopulator.populateMplsRule(targetSw, destSw1, |
| 779 | nextHops.get(destSw1), |
| 780 | dest1RouterIpv4); |
| 781 | if (!result) { |
| 782 | return false; |
| 783 | } |
| 784 | if (dest1RouterIpv6 != null) { |
| 785 | result = rulePopulator.populateMplsRule(targetSw, destSw1, |
| 786 | nextHops.get(destSw1), |
| 787 | dest1RouterIpv6); |
| 788 | if (!result) { |
| 789 | return false; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | // To save on ECMP groups |
| 795 | // avoid MPLS rules in non-edge-devices to non-edge-devices |
| 796 | // avoid MPLS transit rules in edge-devices |
| 797 | // avoid loopback IP rules in edge-devices to non-edge-devices |
| 798 | return true; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Processes a set a route-path changes by editing hash groups. |
| 803 | * |
| 804 | * @param routeChanges a set of route-path changes, where each route-path is |
| 805 | * a list with its first element the src-switch of the path |
| 806 | * and the second element the dst-switch of the path. |
| 807 | * @param linkOrSwitchFailed true if the route changes are for a failed |
| 808 | * switch or linkDown event |
| 809 | * @param failedSwitch the switchId if the route changes are for a failed switch, |
| 810 | * otherwise null |
| 811 | */ |
| 812 | private void processHashGroupChange(Set<ArrayList<DeviceId>> routeChanges, |
| 813 | boolean linkOrSwitchFailed, |
| 814 | DeviceId failedSwitch) { |
| 815 | for (ArrayList<DeviceId> route : routeChanges) { |
| 816 | DeviceId targetSw = route.get(0); |
| 817 | boolean success; |
| 818 | DeviceId dstSw = null; |
| 819 | if (route.size() > 1) { |
| 820 | dstSw = route.get(1); |
| 821 | } |
| 822 | |
| 823 | if (linkOrSwitchFailed) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 824 | fixHashGroupsForRoute(route, true); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 825 | // it's possible that we cannot fix hash groups for a route |
| 826 | // if the target switch has failed. Nevertheless the ecmp graph |
| 827 | // for the impacted switch must still be updated. |
| 828 | if (failedSwitch != null && targetSw.equals(failedSwitch) |
| 829 | && dstSw != null) { |
| 830 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 831 | currentEcmpSpgMap.remove(targetSw); |
| 832 | log.debug("Updating ECMPspg for dst:{} removing failed " |
| 833 | + "target:{}", dstSw, targetSw); |
| 834 | return; |
| 835 | } |
| 836 | //linkfailed - update both sides |
| 837 | currentEcmpSpgMap.put(targetSw, updatedEcmpSpgMap.get(targetSw)); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 838 | if (dstSw != null) { |
| 839 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 840 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 841 | log.debug("Updating ECMPspg for dst:{} and target:{}", dstSw, targetSw); |
| 842 | } else { |
| 843 | success = fixHashGroupsForRoute(route, false); |
| 844 | if (success) { |
| 845 | currentEcmpSpgMap.put(targetSw, updatedEcmpSpgMap.get(targetSw)); |
| 846 | if (dstSw != null) { |
| 847 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 848 | } |
| 849 | log.debug("Updating ECMPspg for target:{} and dst:{}", |
| 850 | targetSw, dstSw); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * Edits hash groups in the src-switch (targetSw) of a route-path by |
| 858 | * calling the groupHandler to either add or remove buckets in an existing |
| 859 | * hash group. |
| 860 | * |
| 861 | * @param route a single list representing a route-path where the first element |
| 862 | * is the src-switch (targetSw) of the route-path and the |
| 863 | * second element is the dst-switch |
| 864 | * @param revoke true if buckets in the hash-groups need to be removed; |
| 865 | * false if buckets in the hash-groups need to be added |
| 866 | * @return true if the hash group editing is successful |
| 867 | */ |
| 868 | private boolean fixHashGroupsForRoute(ArrayList<DeviceId> route, |
| 869 | boolean revoke) { |
| 870 | DeviceId targetSw = route.get(0); |
| 871 | if (route.size() < 2) { |
| 872 | log.warn("Cannot fixHashGroupsForRoute - no dstSw in route {}", route); |
| 873 | return false; |
| 874 | } |
| 875 | DeviceId destSw = route.get(1); |
| 876 | log.debug("Processing fixHashGroupsForRoute: Target {} -> Dest {}", |
| 877 | targetSw, destSw); |
| 878 | boolean targetIsEdge = false; |
| 879 | try { |
| 880 | targetIsEdge = srManager.deviceConfiguration.isEdgeDevice(targetSw); |
| 881 | } catch (DeviceConfigNotFoundException e) { |
| 882 | log.warn(e.getMessage() + "Cannot determine if targetIsEdge {}.. " |
| 883 | + "continuing fixHash", targetSw); |
| 884 | } |
| 885 | |
| 886 | // figure out the new next hops at the targetSw towards the destSw |
| 887 | Set<DeviceId> nextHops = new HashSet<>(); |
| 888 | EcmpShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(destSw); |
| 889 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 890 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 891 | for (Integer itrIdx : switchVia.keySet()) { |
| 892 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 893 | switchVia.get(itrIdx); |
| 894 | for (DeviceId target : swViaMap.keySet()) { |
| 895 | if (target.equals(targetSw)) { |
| 896 | // found the iteration where targetSw is reached- get nextHops |
| 897 | if (!targetIsEdge && itrIdx > 1) { |
| 898 | // optimization for spines to not use other leaves to get |
| 899 | // to a leaf to avoid loops |
| 900 | log.debug("Avoiding {} hop path for non-edge targetSw:{}" |
| 901 | + " --> dstSw:{}", itrIdx, targetSw, destSw); |
| 902 | break; |
| 903 | } |
| 904 | for (ArrayList<DeviceId> via : swViaMap.get(target)) { |
| 905 | if (via.isEmpty()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 906 | // the dstSw is the next-hop from the targetSw |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 907 | nextHops.add(destSw); |
| 908 | } else { |
| 909 | // first elem is next-hop in each ECMP path |
| 910 | nextHops.add(via.get(0)); |
| 911 | } |
| 912 | } |
| 913 | break; |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | // call group handler to change hash group at targetSw |
| 919 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw); |
| 920 | if (grpHandler == null) { |
| 921 | log.warn("Cannot find grouphandler for dev:{} .. aborting" |
| 922 | + " {} hash group buckets for route:{} ", targetSw, |
| 923 | (revoke) ? "revoke" : "repopulate", route); |
| 924 | return false; |
| 925 | } |
| 926 | log.debug("{} hash-groups buckets For Route {} -> {} to next-hops {}", |
| 927 | (revoke) ? "revoke" : "repopulating", |
| 928 | targetSw, destSw, nextHops); |
| 929 | return (revoke) ? grpHandler.fixHashGroups(targetSw, nextHops, |
| 930 | destSw, true) |
| 931 | : grpHandler.fixHashGroups(targetSw, nextHops, |
| 932 | destSw, false); |
| 933 | } |
| 934 | |
| 935 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 936 | * Start the flow rule population process if it was never started. The |
| 937 | * process finishes successfully when all flow rules are set and stops with |
| 938 | * ABORTED status when any groups required for flows is not set yet. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 939 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 940 | public void startPopulationProcess() { |
| 941 | statusLock.lock(); |
| 942 | try { |
| 943 | if (populationStatus == Status.IDLE |
| 944 | || populationStatus == Status.SUCCEEDED |
| 945 | || populationStatus == Status.ABORTED) { |
| 946 | populateAllRoutingRules(); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 947 | } else { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 948 | log.warn("Not initiating startPopulationProcess as populationStatus is {}", |
| 949 | populationStatus); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 950 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 951 | } finally { |
| 952 | statusLock.unlock(); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 953 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 956 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 957 | * Revoke rules of given subnet in all edge switches. |
| 958 | * |
| 959 | * @param subnets subnet being removed |
| 960 | * @return true if succeed |
| 961 | */ |
| 962 | protected boolean revokeSubnet(Set<IpPrefix> subnets) { |
| 963 | statusLock.lock(); |
| 964 | try { |
| 965 | return srManager.routingRulePopulator.revokeIpRuleForSubnet(subnets); |
| 966 | } finally { |
| 967 | statusLock.unlock(); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * Remove ECMP graph entry for the given device. Typically called when |
| 973 | * device is no longer available. |
| 974 | * |
| 975 | * @param deviceId the device for which graphs need to be purged |
| 976 | */ |
| 977 | protected void purgeEcmpGraph(DeviceId deviceId) { |
| 978 | currentEcmpSpgMap.remove(deviceId); |
| 979 | if (updatedEcmpSpgMap != null) { |
| 980 | updatedEcmpSpgMap.remove(deviceId); |
| 981 | } |
| 982 | } |
| 983 | |
| 984 | ////////////////////////////////////// |
| 985 | // Routing helper methods and classes |
| 986 | ////////////////////////////////////// |
| 987 | |
| 988 | /** |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 989 | * Computes set of affected routes due to failed link. Assumes |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 990 | * previous ecmp shortest-path graph exists for a switch in order to compute |
| 991 | * affected routes. If such a graph does not exist, the method returns null. |
| 992 | * |
| 993 | * @param linkFail the failed link |
| 994 | * @return the set of affected routes which may be empty if no routes were |
| 995 | * affected, or null if no previous ecmp spg was found for comparison |
| 996 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 997 | private Set<ArrayList<DeviceId>> computeDamagedRoutes(Link linkFail) { |
| 998 | |
| 999 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 1000 | |
| 1001 | for (Device sw : srManager.deviceService.getDevices()) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1002 | log.debug("Computing the impacted routes for device {} due to link fail", |
| 1003 | sw.id()); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 1004 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1005 | log.debug("No mastership for {} .. skipping route optimization", |
| 1006 | sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1007 | continue; |
| 1008 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 1009 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1010 | if (ecmpSpg == null) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1011 | log.warn("No existing ECMP graph for switch {}. Aborting optimized" |
| 1012 | + " rerouting and opting for full-reroute", sw.id()); |
| 1013 | return null; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1014 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1015 | if (log.isDebugEnabled()) { |
| 1016 | log.debug("Root switch: {}", sw.id()); |
| 1017 | log.debug(" Current/Existing SPG: {}", ecmpSpg); |
| 1018 | log.debug(" New/Updated SPG: {}", updatedEcmpSpgMap.get(sw.id())); |
| 1019 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1020 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 1021 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 1022 | for (Integer itrIdx : switchVia.keySet()) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1023 | log.trace("Current/Exiting SPG Iterindex# {}", itrIdx); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1024 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 1025 | switchVia.get(itrIdx); |
| 1026 | for (DeviceId targetSw : swViaMap.keySet()) { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1027 | DeviceId rootSw = sw.id(); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1028 | if (log.isTraceEnabled()) { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1029 | log.trace("TargetSwitch {} --> RootSwitch {}", targetSw, rootSw); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1030 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 1031 | log.trace(" Via:"); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1032 | via.forEach(e -> log.trace(" {}", e)); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1035 | Set<ArrayList<DeviceId>> subLinks = |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1036 | computeLinks(targetSw, rootSw, swViaMap); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1037 | for (ArrayList<DeviceId> alink: subLinks) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1038 | if ((alink.get(0).equals(linkFail.src().deviceId()) && |
| 1039 | alink.get(1).equals(linkFail.dst().deviceId())) |
| 1040 | || |
| 1041 | (alink.get(0).equals(linkFail.dst().deviceId()) && |
| 1042 | alink.get(1).equals(linkFail.src().deviceId()))) { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1043 | log.debug("Impacted route:{}->{}", targetSw, rootSw); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1044 | ArrayList<DeviceId> aRoute = new ArrayList<>(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1045 | aRoute.add(targetSw); // switch with rules to populate |
| 1046 | aRoute.add(rootSw); // towards this destination |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1047 | routes.add(aRoute); |
| 1048 | break; |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 1053 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | return routes; |
| 1057 | } |
| 1058 | |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1059 | /** |
| 1060 | * Computes set of affected routes due to new links or failed switches. |
| 1061 | * |
| 1062 | * @return the set of affected routes which may be empty if no routes were |
| 1063 | * affected |
| 1064 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1065 | private Set<ArrayList<DeviceId>> computeRouteChange() { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1066 | ImmutableSet.Builder<ArrayList<DeviceId>> changedRtBldr = |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1067 | ImmutableSet.builder(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1068 | |
| 1069 | for (Device sw : srManager.deviceService.getDevices()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1070 | log.debug("Computing the impacted routes for device {}", sw.id()); |
| 1071 | DeviceId retId = shouldHandleRouting(sw.id()); |
| 1072 | if (retId == null) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1075 | Set<DeviceId> devicesToProcess = Sets.newHashSet(retId, sw.id()); |
| 1076 | for (DeviceId rootSw : devicesToProcess) { |
| 1077 | if (log.isTraceEnabled()) { |
| 1078 | log.trace("Device links for dev: {}", rootSw); |
| 1079 | for (Link link: srManager.linkService.getDeviceLinks(rootSw)) { |
| 1080 | log.trace("{} -> {} ", link.src().deviceId(), |
| 1081 | link.dst().deviceId()); |
| 1082 | } |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1083 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1084 | EcmpShortestPathGraph currEcmpSpg = currentEcmpSpgMap.get(rootSw); |
| 1085 | if (currEcmpSpg == null) { |
| 1086 | log.debug("No existing ECMP graph for device {}.. adding self as " |
| 1087 | + "changed route", rootSw); |
| 1088 | changedRtBldr.add(Lists.newArrayList(rootSw)); |
| 1089 | continue; |
| 1090 | } |
| 1091 | EcmpShortestPathGraph newEcmpSpg = updatedEcmpSpgMap.get(rootSw); |
| 1092 | if (log.isDebugEnabled()) { |
| 1093 | log.debug("Root switch: {}", rootSw); |
| 1094 | log.debug(" Current/Existing SPG: {}", currEcmpSpg); |
| 1095 | log.debug(" New/Updated SPG: {}", newEcmpSpg); |
| 1096 | } |
| 1097 | // first use the updated/new map to compare to current/existing map |
| 1098 | // as new links may have come up |
| 1099 | changedRtBldr.addAll(compareGraphs(newEcmpSpg, currEcmpSpg, rootSw)); |
| 1100 | // then use the current/existing map to compare to updated/new map |
| 1101 | // as switch may have been removed |
| 1102 | changedRtBldr.addAll(compareGraphs(currEcmpSpg, newEcmpSpg, rootSw)); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 1103 | } |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1104 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1105 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1106 | Set<ArrayList<DeviceId>> changedRoutes = changedRtBldr.build(); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1107 | for (ArrayList<DeviceId> route: changedRoutes) { |
| 1108 | log.debug("Route changes Target -> Root"); |
| 1109 | if (route.size() == 1) { |
| 1110 | log.debug(" : all -> {}", route.get(0)); |
| 1111 | } else { |
| 1112 | log.debug(" : {} -> {}", route.get(0), route.get(1)); |
| 1113 | } |
| 1114 | } |
| 1115 | return changedRoutes; |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * For the root switch, searches all the target nodes reachable in the base |
| 1120 | * graph, and compares paths to the ones in the comp graph. |
| 1121 | * |
| 1122 | * @param base the graph that is indexed for all reachable target nodes |
| 1123 | * from the root node |
| 1124 | * @param comp the graph that the base graph is compared to |
| 1125 | * @param rootSw both ecmp graphs are calculated for the root node |
| 1126 | * @return all the routes that have changed in the base graph |
| 1127 | */ |
| 1128 | private Set<ArrayList<DeviceId>> compareGraphs(EcmpShortestPathGraph base, |
| 1129 | EcmpShortestPathGraph comp, |
| 1130 | DeviceId rootSw) { |
| 1131 | ImmutableSet.Builder<ArrayList<DeviceId>> changedRoutesBuilder = |
| 1132 | ImmutableSet.builder(); |
| 1133 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> baseMap = |
| 1134 | base.getAllLearnedSwitchesAndVia(); |
| 1135 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> compMap = |
| 1136 | comp.getAllLearnedSwitchesAndVia(); |
| 1137 | for (Integer itrIdx : baseMap.keySet()) { |
| 1138 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> baseViaMap = |
| 1139 | baseMap.get(itrIdx); |
| 1140 | for (DeviceId targetSw : baseViaMap.keySet()) { |
| 1141 | ArrayList<ArrayList<DeviceId>> basePath = baseViaMap.get(targetSw); |
| 1142 | ArrayList<ArrayList<DeviceId>> compPath = getVia(compMap, targetSw); |
| 1143 | if ((compPath == null) || !basePath.equals(compPath)) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1144 | log.trace("Impacted route:{} -> {}", targetSw, rootSw); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1145 | ArrayList<DeviceId> route = new ArrayList<>(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1146 | route.add(targetSw); // switch with rules to populate |
| 1147 | route.add(rootSw); // towards this destination |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1148 | changedRoutesBuilder.add(route); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 1151 | } |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1152 | return changedRoutesBuilder.build(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1155 | /** |
| 1156 | * Returns the ECMP paths traversed to reach the target switch. |
| 1157 | * |
| 1158 | * @param switchVia a per-iteration view of the ECMP graph for a root switch |
| 1159 | * @param targetSw the switch to reach from the root switch |
| 1160 | * @return the nodes traversed on ECMP paths to the target switch |
| 1161 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1162 | private ArrayList<ArrayList<DeviceId>> getVia(HashMap<Integer, HashMap<DeviceId, |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1163 | ArrayList<ArrayList<DeviceId>>>> switchVia, DeviceId targetSw) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1164 | for (Integer itrIdx : switchVia.keySet()) { |
| 1165 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 1166 | switchVia.get(itrIdx); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1167 | if (swViaMap.get(targetSw) == null) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1168 | continue; |
| 1169 | } else { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1170 | return swViaMap.get(targetSw); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1174 | return null; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1177 | /** |
| 1178 | * Utility method to break down a path from src to dst device into a collection |
| 1179 | * of links. |
| 1180 | * |
| 1181 | * @param src src device of the path |
| 1182 | * @param dst dst device of the path |
| 1183 | * @param viaMap path taken from src to dst device |
| 1184 | * @return collection of links in the path |
| 1185 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1186 | private Set<ArrayList<DeviceId>> computeLinks(DeviceId src, |
| 1187 | DeviceId dst, |
| 1188 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> viaMap) { |
| 1189 | Set<ArrayList<DeviceId>> subLinks = Sets.newHashSet(); |
| 1190 | for (ArrayList<DeviceId> via : viaMap.get(src)) { |
| 1191 | DeviceId linkSrc = src; |
| 1192 | DeviceId linkDst = dst; |
| 1193 | for (DeviceId viaDevice: via) { |
| 1194 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 1195 | linkDst = viaDevice; |
| 1196 | link.add(linkSrc); |
| 1197 | link.add(linkDst); |
| 1198 | subLinks.add(link); |
| 1199 | linkSrc = viaDevice; |
| 1200 | } |
| 1201 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 1202 | link.add(linkSrc); |
| 1203 | link.add(dst); |
| 1204 | subLinks.add(link); |
| 1205 | } |
| 1206 | |
| 1207 | return subLinks; |
| 1208 | } |
| 1209 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1210 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1211 | * Determines whether this controller instance should handle routing for the |
| 1212 | * given {@code deviceId}, based on mastership and pairDeviceId if one exists. |
| 1213 | * Returns null if this instance should not handle routing for given {@code deviceId}. |
| 1214 | * Otherwise the returned value could be the given deviceId itself, or the |
| 1215 | * deviceId for the paired edge device. In the latter case, this instance |
| 1216 | * should handle routing for both the given device and the paired device. |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1217 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1218 | * @param deviceId device identifier to consider for routing |
| 1219 | * @return null or deviceId which could be the same as the given deviceId |
| 1220 | * or the deviceId of a paired edge device |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1221 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1222 | private DeviceId shouldHandleRouting(DeviceId deviceId) { |
| 1223 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 1224 | log.debug("Not master for dev:{} .. skipping routing, may get handled " |
| 1225 | + "elsewhere as part of paired devices", deviceId); |
| 1226 | return null; |
| 1227 | } |
| 1228 | NodeId myNode = srManager.mastershipService.getMasterFor(deviceId); |
| 1229 | DeviceId pairDev = getPairDev(deviceId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1230 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1231 | if (pairDev != null) { |
| 1232 | if (!srManager.deviceService.isAvailable(pairDev)) { |
| 1233 | log.warn("pairedDev {} not available .. routing this dev:{} " |
| 1234 | + "without mastership check", |
| 1235 | pairDev, deviceId); |
| 1236 | return pairDev; // handle both temporarily |
| 1237 | } |
| 1238 | NodeId pairMasterNode = srManager.mastershipService.getMasterFor(pairDev); |
| 1239 | if (myNode.compareTo(pairMasterNode) <= 0) { |
| 1240 | log.debug("Handling routing for both dev:{} pair-dev:{}; myNode: {}" |
| 1241 | + " pairMaster:{} compare:{}", deviceId, pairDev, |
| 1242 | myNode, pairMasterNode, |
| 1243 | myNode.compareTo(pairMasterNode)); |
| 1244 | return pairDev; // handle both |
| 1245 | } else { |
| 1246 | log.debug("PairDev node: {} should handle routing for dev:{} and " |
| 1247 | + "pair-dev:{}", pairMasterNode, deviceId, pairDev); |
| 1248 | return null; // handle neither |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1249 | } |
| 1250 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1251 | return deviceId; // not paired, just handle given device |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1254 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1255 | * Returns the configured paired DeviceId for the given Device, or null |
| 1256 | * if no such paired device has been configured. |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1257 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1258 | * @param deviceId |
| 1259 | * @return configured pair deviceId or null |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1260 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1261 | private DeviceId getPairDev(DeviceId deviceId) { |
| 1262 | DeviceId pairDev; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1263 | try { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1264 | pairDev = srManager.deviceConfiguration.getPairDeviceId(deviceId); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1265 | } catch (DeviceConfigNotFoundException e) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1266 | log.warn(e.getMessage() + " .. cannot continue routing for dev: {}"); |
| 1267 | return null; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 1268 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1269 | return pairDev; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1273 | * Returns the set of deviceIds which are the next hops from the targetSw |
| 1274 | * to the dstSw according to the latest ECMP spg. |
| 1275 | * |
| 1276 | * @param targetSw the switch for which the next-hops are desired |
| 1277 | * @param dstSw the switch to which the next-hops lead to from the targetSw |
| 1278 | * @return set of next hop deviceIds, could be empty if no next hops are found |
| 1279 | */ |
| 1280 | private Set<DeviceId> getNextHops(DeviceId targetSw, DeviceId dstSw) { |
| 1281 | boolean targetIsEdge = false; |
| 1282 | try { |
| 1283 | targetIsEdge = srManager.deviceConfiguration.isEdgeDevice(targetSw); |
| 1284 | } catch (DeviceConfigNotFoundException e) { |
| 1285 | log.warn(e.getMessage() + "Cannot determine if targetIsEdge {}.. " |
| 1286 | + "continuing to getNextHops", targetSw); |
| 1287 | } |
| 1288 | |
| 1289 | EcmpShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(dstSw); |
| 1290 | if (ecmpSpg == null) { |
| 1291 | log.debug("No ecmpSpg found for dstSw: {}", dstSw); |
| 1292 | return ImmutableSet.of(); |
| 1293 | } |
| 1294 | HashMap<Integer, |
| 1295 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 1296 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 1297 | for (Integer itrIdx : switchVia.keySet()) { |
| 1298 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 1299 | switchVia.get(itrIdx); |
| 1300 | for (DeviceId target : swViaMap.keySet()) { |
| 1301 | if (!target.equals(targetSw)) { |
| 1302 | continue; |
| 1303 | } |
| 1304 | if (!targetIsEdge && itrIdx > 1) { |
| 1305 | // optimization for spines to not use other leaves to get |
| 1306 | // to a leaf to avoid loops |
| 1307 | log.debug("Avoiding {} hop path for non-edge targetSw:{}" |
| 1308 | + " --> dstSw:{}", itrIdx, targetSw, dstSw); |
| 1309 | break; |
| 1310 | } |
| 1311 | Set<DeviceId> nextHops = new HashSet<>(); |
| 1312 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 1313 | if (via.isEmpty()) { |
| 1314 | // the dstSw is the next-hop from the targetSw |
| 1315 | nextHops.add(dstSw); |
| 1316 | } else { |
| 1317 | // first elem is next-hop in each ECMP path |
| 1318 | nextHops.add(via.get(0)); |
| 1319 | } |
| 1320 | } |
| 1321 | return nextHops; |
| 1322 | } |
| 1323 | } |
| 1324 | return ImmutableSet.of(); //no next-hops found |
| 1325 | } |
| 1326 | |
| 1327 | /** |
| 1328 | * Represents two devices that are paired by configuration. An EdgePair for |
| 1329 | * (dev1, dev2) is the same as as EdgePair for (dev2, dev1) |
| 1330 | */ |
| 1331 | protected final class EdgePair { |
| 1332 | DeviceId dev1; |
| 1333 | DeviceId dev2; |
| 1334 | |
| 1335 | EdgePair(DeviceId dev1, DeviceId dev2) { |
| 1336 | this.dev1 = dev1; |
| 1337 | this.dev2 = dev2; |
| 1338 | } |
| 1339 | |
| 1340 | boolean includes(DeviceId dev) { |
| 1341 | return dev1.equals(dev) || dev2.equals(dev); |
| 1342 | } |
| 1343 | |
| 1344 | @Override |
| 1345 | public boolean equals(Object o) { |
| 1346 | if (this == o) { |
| 1347 | return true; |
| 1348 | } |
| 1349 | if (!(o instanceof EdgePair)) { |
| 1350 | return false; |
| 1351 | } |
| 1352 | EdgePair that = (EdgePair) o; |
| 1353 | return ((this.dev1.equals(that.dev1) && this.dev2.equals(that.dev2)) || |
| 1354 | (this.dev1.equals(that.dev2) && this.dev2.equals(that.dev1))); |
| 1355 | } |
| 1356 | |
| 1357 | @Override |
| 1358 | public int hashCode() { |
| 1359 | if (dev1.toString().compareTo(dev2.toString()) <= 0) { |
| 1360 | return Objects.hash(dev1, dev2); |
| 1361 | } else { |
| 1362 | return Objects.hash(dev2, dev1); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | @Override |
| 1367 | public String toString() { |
| 1368 | return toStringHelper(this) |
| 1369 | .add("Dev1", dev1) |
| 1370 | .add("Dev2", dev2) |
| 1371 | .toString(); |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | ////////////////////////////////////// |
| 1376 | // Filtering rule creation |
| 1377 | ////////////////////////////////////// |
| 1378 | |
| 1379 | /** |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1380 | * Populates filtering rules for port, and punting rules |
| 1381 | * for gateway IPs, loopback IPs and arp/ndp traffic. |
| 1382 | * Should only be called by the master instance for this device/port. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1383 | * |
| 1384 | * @param deviceId Switch ID to set the rules |
| 1385 | */ |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1386 | public void populatePortAddressingRules(DeviceId deviceId) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1387 | // Although device is added, sometimes device store does not have the |
| 1388 | // ports for this device yet. It results in missing filtering rules in the |
| 1389 | // switch. We will attempt it a few times. If it still does not work, |
| 1390 | // user can manually repopulate using CLI command sr-reroute-network |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 1391 | PortFilterInfo firstRun = rulePopulator.populateVlanMacFilters(deviceId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1392 | if (firstRun == null) { |
| 1393 | firstRun = new PortFilterInfo(0, 0, 0); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1394 | } |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1395 | executorService.schedule(new RetryFilters(deviceId, firstRun), |
| 1396 | RETRY_INTERVAL_MS, TimeUnit.MILLISECONDS); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /** |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1400 | * Utility class used to temporarily store information about the ports on a |
| 1401 | * device processed for filtering objectives. |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1402 | */ |
| 1403 | public final class PortFilterInfo { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1404 | int disabledPorts = 0, errorPorts = 0, filteredPorts = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1405 | |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1406 | public PortFilterInfo(int disabledPorts, int errorPorts, |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1407 | int filteredPorts) { |
| 1408 | this.disabledPorts = disabledPorts; |
| 1409 | this.filteredPorts = filteredPorts; |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1410 | this.errorPorts = errorPorts; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | @Override |
| 1414 | public int hashCode() { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1415 | return Objects.hash(disabledPorts, filteredPorts, errorPorts); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | @Override |
| 1419 | public boolean equals(Object obj) { |
| 1420 | if (this == obj) { |
| 1421 | return true; |
| 1422 | } |
| 1423 | if ((obj == null) || (!(obj instanceof PortFilterInfo))) { |
| 1424 | return false; |
| 1425 | } |
| 1426 | PortFilterInfo other = (PortFilterInfo) obj; |
| 1427 | return ((disabledPorts == other.disabledPorts) && |
| 1428 | (filteredPorts == other.filteredPorts) && |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1429 | (errorPorts == other.errorPorts)); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | @Override |
| 1433 | public String toString() { |
| 1434 | MoreObjects.ToStringHelper helper = toStringHelper(this) |
| 1435 | .add("disabledPorts", disabledPorts) |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1436 | .add("errorPorts", errorPorts) |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1437 | .add("filteredPorts", filteredPorts); |
| 1438 | return helper.toString(); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /** |
| 1443 | * RetryFilters populates filtering objectives for a device and keeps retrying |
| 1444 | * till the number of ports filtered are constant for a predefined number |
| 1445 | * of attempts. |
| 1446 | */ |
| 1447 | protected final class RetryFilters implements Runnable { |
| 1448 | int constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; |
| 1449 | DeviceId devId; |
| 1450 | int counter; |
| 1451 | PortFilterInfo prevRun; |
| 1452 | |
| 1453 | private RetryFilters(DeviceId deviceId, PortFilterInfo previousRun) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1454 | devId = deviceId; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1455 | prevRun = previousRun; |
| 1456 | counter = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | @Override |
| 1460 | public void run() { |
Charles Chan | 077314e | 2017-06-22 14:27:17 -0700 | [diff] [blame] | 1461 | log.debug("RETRY FILTER ATTEMPT {} ** dev:{}", ++counter, devId); |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 1462 | PortFilterInfo thisRun = rulePopulator.populateVlanMacFilters(devId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1463 | boolean sameResult = prevRun.equals(thisRun); |
| 1464 | log.debug("dev:{} prevRun:{} thisRun:{} sameResult:{}", devId, prevRun, |
| 1465 | thisRun, sameResult); |
| 1466 | if (thisRun == null || !sameResult || (sameResult && --constantAttempts > 0)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1467 | // exponentially increasing intervals for retries |
| 1468 | executorService.schedule(this, |
| 1469 | RETRY_INTERVAL_MS * (int) Math.pow(counter, RETRY_INTERVAL_SCALE), |
| 1470 | TimeUnit.MILLISECONDS); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1471 | if (!sameResult) { |
| 1472 | constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; //reset |
| 1473 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1474 | } |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1475 | prevRun = (thisRun == null) ? prevRun : thisRun; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1476 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1479 | } |