blob: 2190627c4c99be3902f4e5e31f83a4d5c7f892d2 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-04-01 13:05:26 -07003 *
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 */
16package org.onosproject.segmentrouting;
17
Saurav Dasc88d4662017-05-15 15:34:25 -070018import com.google.common.collect.ImmutableMap;
19import com.google.common.collect.ImmutableMap.Builder;
Charles Chan93e71ba2016-04-29 14:38:22 -070020import com.google.common.collect.ImmutableSet;
Saurav Das4e3224f2016-11-29 14:27:25 -080021import com.google.common.collect.Lists;
sangho20eff1d2015-04-13 15:15:58 -070022import com.google.common.collect.Maps;
23import com.google.common.collect.Sets;
Saurav Dasceccf242017-08-03 18:30:35 -070024
Jonghwan Hyuna76bf032018-04-09 09:40:50 -070025import org.onlab.packet.EthType;
Charles Chan022d6672019-04-17 14:20:26 -070026import com.google.common.collect.Streams;
sangho666cd6d2015-04-14 16:27:13 -070027import org.onlab.packet.Ip4Address;
Pier Ventree0ae7a32016-11-23 09:57:42 -080028import org.onlab.packet.Ip6Address;
sanghob35a6192015-04-01 13:05:26 -070029import org.onlab.packet.IpPrefix;
Charles Chan2fde6d42017-08-23 14:46:43 -070030import org.onlab.packet.MacAddress;
31import org.onlab.packet.VlanId;
pier8b4ba992019-04-24 16:12:47 +020032import org.onlab.util.PredictableExecutor;
33import org.onlab.util.PredictableExecutor.PickyCallable;
Saurav Das7bcbe702017-06-13 15:35:54 -070034import org.onosproject.cluster.NodeId;
Saurav Dasc6dc1772018-04-21 17:19:48 -070035import org.onosproject.mastership.MastershipEvent;
Charles Chan93e71ba2016-04-29 14:38:22 -070036import org.onosproject.net.ConnectPoint;
sanghob35a6192015-04-01 13:05:26 -070037import org.onosproject.net.Device;
38import org.onosproject.net.DeviceId;
sangho20eff1d2015-04-13 15:15:58 -070039import org.onosproject.net.Link;
Charles Chan2fde6d42017-08-23 14:46:43 -070040import org.onosproject.net.PortNumber;
Charles Chan0b4e6182015-11-03 10:42:14 -080041import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
42import org.onosproject.segmentrouting.config.DeviceConfiguration;
Saurav Dasc88d4662017-05-15 15:34:25 -070043import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Jonghwan Hyuna76bf032018-04-09 09:40:50 -070044import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Charles Chan2ff1bac2018-03-29 16:03:41 -070045import org.onosproject.store.serializers.KryoNamespaces;
pier7398a5b2020-01-07 15:39:39 +010046import org.onosproject.store.service.ConsistentMultimap;
Charles Chan2ff1bac2018-03-29 16:03:41 -070047import org.onosproject.store.service.Serializer;
sanghob35a6192015-04-01 13:05:26 -070048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
50
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070051import java.time.Instant;
sanghob35a6192015-04-01 13:05:26 -070052import java.util.ArrayList;
Charles Chan2ff1bac2018-03-29 16:03:41 -070053import java.util.Collections;
sanghob35a6192015-04-01 13:05:26 -070054import java.util.HashMap;
55import java.util.HashSet;
Saurav Das7bcbe702017-06-13 15:35:54 -070056import java.util.Iterator;
Charles Chan2ff1bac2018-03-29 16:03:41 -070057import java.util.List;
Saurav Das7bcbe702017-06-13 15:35:54 -070058import java.util.Map;
pier7398a5b2020-01-07 15:39:39 +010059import java.util.Map.Entry;
Saurav Dasd2fded02016-12-02 15:43:47 -080060import java.util.Objects;
Charles Chanba6c5752018-04-02 11:46:38 -070061import java.util.Optional;
sanghob35a6192015-04-01 13:05:26 -070062import java.util.Set;
pier8b4ba992019-04-24 16:12:47 +020063import java.util.concurrent.CompletableFuture;
64import java.util.concurrent.ExecutionException;
65import java.util.concurrent.ExecutorService;
66import java.util.concurrent.Future;
Saurav Das59232cf2016-04-27 18:35:50 -070067import java.util.concurrent.ScheduledExecutorService;
68import java.util.concurrent.TimeUnit;
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +090069import java.util.concurrent.locks.Lock;
70import java.util.concurrent.locks.ReentrantLock;
Charles Chan022d6672019-04-17 14:20:26 -070071import java.util.stream.Collectors;
Saurav Das604ab3a2018-03-18 21:28:15 -070072import java.util.stream.Stream;
73
Pier Ventree0ae7a32016-11-23 09:57:42 -080074import static com.google.common.base.Preconditions.checkNotNull;
75import static java.util.concurrent.Executors.newScheduledThreadPool;
76import static org.onlab.util.Tools.groupedThreads;
sanghob35a6192015-04-01 13:05:26 -070077
Charles Chane849c192016-01-11 18:28:54 -080078/**
79 * Default routing handler that is responsible for route computing and
80 * routing rule population.
81 */
sanghob35a6192015-04-01 13:05:26 -070082public class DefaultRoutingHandler {
Saurav Das018605f2017-02-18 14:05:44 -080083 private static final int MAX_CONSTANT_RETRY_ATTEMPTS = 5;
Ray Milkey3717e602018-02-01 13:49:47 -080084 private static final long RETRY_INTERVAL_MS = 250L;
Saurav Das018605f2017-02-18 14:05:44 -080085 private static final int RETRY_INTERVAL_SCALE = 1;
Saurav Dasceccf242017-08-03 18:30:35 -070086 private static final long STABLITY_THRESHOLD = 10; //secs
Saurav Dasc6dc1772018-04-21 17:19:48 -070087 private static final long MASTER_CHANGE_DELAY = 1000; // ms
Saurav Dasf1027d42018-06-11 17:02:31 -070088 private static final long PURGE_DELAY = 1000; // ms
Charles Chan93e71ba2016-04-29 14:38:22 -070089 private static Logger log = LoggerFactory.getLogger(DefaultRoutingHandler.class);
sanghob35a6192015-04-01 13:05:26 -070090
91 private SegmentRoutingManager srManager;
92 private RoutingRulePopulator rulePopulator;
Shashikanth VH013a7bc2015-12-11 01:32:44 +053093 private HashMap<DeviceId, EcmpShortestPathGraph> currentEcmpSpgMap;
94 private HashMap<DeviceId, EcmpShortestPathGraph> updatedEcmpSpgMap;
sangho666cd6d2015-04-14 16:27:13 -070095 private DeviceConfiguration config;
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +090096 private final Lock statusLock = new ReentrantLock();
97 private volatile Status populationStatus;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -070098 private ScheduledExecutorService executorService
Saurav Dasd2fded02016-12-02 15:43:47 -080099 = newScheduledThreadPool(1, groupedThreads("retryftr", "retry-%d", log));
Saurav Das60ca8d52018-04-23 18:42:12 -0700100 private ScheduledExecutorService executorServiceMstChg
101 = newScheduledThreadPool(1, groupedThreads("masterChg", "mstch-%d", log));
Saurav Dasf1027d42018-06-11 17:02:31 -0700102 private ScheduledExecutorService executorServiceFRR
103 = newScheduledThreadPool(1, groupedThreads("fullRR", "fullRR-%d", log));
pier8b4ba992019-04-24 16:12:47 +0200104 // Route populators - 0 will leverage available processors
105 private static final int DEFAULT_THREADS = 0;
106 private ExecutorService routePopulators;
Saurav Das60ca8d52018-04-23 18:42:12 -0700107
Saurav Dasc6dc1772018-04-21 17:19:48 -0700108 private Instant lastRoutingChange = Instant.EPOCH;
Saurav Dasf1027d42018-06-11 17:02:31 -0700109 private Instant lastFullReroute = Instant.EPOCH;
sanghob35a6192015-04-01 13:05:26 -0700110
Saurav Dasc6dc1772018-04-21 17:19:48 -0700111 // Distributed store to keep track of ONOS instance that should program the
112 // device pair. There should be only one instance (the king) that programs the same pair.
Charles Chan2ff1bac2018-03-29 16:03:41 -0700113 Map<Set<DeviceId>, NodeId> shouldProgram;
Charles Chana8487b02018-04-18 18:41:05 -0700114 Map<DeviceId, Boolean> shouldProgramCache;
Charles Chan2ff1bac2018-03-29 16:03:41 -0700115
pier7398a5b2020-01-07 15:39:39 +0100116 // Distributed routes store to keep track of the routes already seen
117 // destination device is the key and target sw is the value
118 ConsistentMultimap<DeviceId, DeviceId> seenBeforeRoutes;
119
Saurav Dasc6dc1772018-04-21 17:19:48 -0700120 // Local store to keep track of all devices that this instance was responsible
121 // for programming in the last run. Helps to determine if mastership changed
122 // during a run - only relevant for programming as a result of topo change.
123 Set<DeviceId> lastProgrammed;
124
sanghob35a6192015-04-01 13:05:26 -0700125 /**
126 * Represents the default routing population status.
127 */
128 public enum Status {
129 // population process is not started yet.
130 IDLE,
sanghob35a6192015-04-01 13:05:26 -0700131 // population process started.
132 STARTED,
pier8b4ba992019-04-24 16:12:47 +0200133 // population process was aborted due to errors, mostly for groups not found.
sanghob35a6192015-04-01 13:05:26 -0700134 ABORTED,
sanghob35a6192015-04-01 13:05:26 -0700135 // population process was finished successfully.
136 SUCCEEDED
137 }
138
139 /**
140 * Creates a DefaultRoutingHandler object.
141 *
142 * @param srManager SegmentRoutingManager object
143 */
Charles Chan2ff1bac2018-03-29 16:03:41 -0700144 DefaultRoutingHandler(SegmentRoutingManager srManager) {
Charles Chana8487b02018-04-18 18:41:05 -0700145 this.shouldProgram = srManager.storageService.<Set<DeviceId>, NodeId>consistentMapBuilder()
146 .withName("sr-should-program")
147 .withSerializer(Serializer.using(KryoNamespaces.API))
148 .withRelaxedReadConsistency()
149 .build().asJavaMap();
pier7398a5b2020-01-07 15:39:39 +0100150 this.seenBeforeRoutes = srManager.storageService.<DeviceId, DeviceId>consistentMultimapBuilder()
151 .withName("programmed-routes")
152 .withSerializer(Serializer.using(KryoNamespaces.API))
153 .withRelaxedReadConsistency()
154 .build();
Charles Chana8487b02018-04-18 18:41:05 -0700155 this.shouldProgramCache = Maps.newConcurrentMap();
156 update(srManager);
pier8b4ba992019-04-24 16:12:47 +0200157 this.routePopulators = new PredictableExecutor(DEFAULT_THREADS,
158 groupedThreads("onos/sr", "r-populator-%d", log));
Charles Chana8487b02018-04-18 18:41:05 -0700159 }
160
161 /**
162 * Updates a DefaultRoutingHandler object.
163 *
164 * @param srManager SegmentRoutingManager object
165 */
166 void update(SegmentRoutingManager srManager) {
sanghob35a6192015-04-01 13:05:26 -0700167 this.srManager = srManager;
168 this.rulePopulator = checkNotNull(srManager.routingRulePopulator);
sangho666cd6d2015-04-14 16:27:13 -0700169 this.config = checkNotNull(srManager.deviceConfiguration);
sanghob35a6192015-04-01 13:05:26 -0700170 this.populationStatus = Status.IDLE;
sangho20eff1d2015-04-13 15:15:58 -0700171 this.currentEcmpSpgMap = Maps.newHashMap();
Saurav Dasc6dc1772018-04-21 17:19:48 -0700172 this.lastProgrammed = Sets.newConcurrentHashSet();
sanghob35a6192015-04-01 13:05:26 -0700173 }
174
175 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700176 * Returns an immutable copy of the current ECMP shortest-path graph as
177 * computed by this controller instance.
178 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700179 * @return immutable copy of the current ECMP graph
Saurav Dasc88d4662017-05-15 15:34:25 -0700180 */
181 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEmcpSpgMap() {
182 Builder<DeviceId, EcmpShortestPathGraph> builder = ImmutableMap.builder();
183 currentEcmpSpgMap.entrySet().forEach(entry -> {
184 if (entry.getValue() != null) {
185 builder.put(entry.getKey(), entry.getValue());
186 }
187 });
188 return builder.build();
189 }
190
Saurav Dasceccf242017-08-03 18:30:35 -0700191 /**
192 * Acquires the lock used when making routing changes.
193 */
194 public void acquireRoutingLock() {
195 statusLock.lock();
196 }
197
198 /**
199 * Releases the lock used when making routing changes.
200 */
201 public void releaseRoutingLock() {
202 statusLock.unlock();
203 }
204
205 /**
206 * Determines if routing in the network has been stable in the last
207 * STABLITY_THRESHOLD seconds, by comparing the current time to the last
208 * routing change timestamp.
209 *
210 * @return true if stable
211 */
212 public boolean isRoutingStable() {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700213 long last = (long) (lastRoutingChange.toEpochMilli() / 1000.0);
214 long now = (long) (Instant.now().toEpochMilli() / 1000.0);
Saurav Das9df5b7c2017-08-14 16:44:43 -0700215 log.trace("Routing stable since {}s", now - last);
Saurav Dasceccf242017-08-03 18:30:35 -0700216 return (now - last) > STABLITY_THRESHOLD;
217 }
218
Saurav Das60ca8d52018-04-23 18:42:12 -0700219 /**
220 * Gracefully shuts down the defaultRoutingHandler. Typically called when
221 * the app is deactivated
222 */
223 public void shutdown() {
224 executorService.shutdown();
225 executorServiceMstChg.shutdown();
Saurav Dasf1027d42018-06-11 17:02:31 -0700226 executorServiceFRR.shutdown();
pier8b4ba992019-04-24 16:12:47 +0200227 routePopulators.shutdown();
Saurav Das60ca8d52018-04-23 18:42:12 -0700228 }
Saurav Dasceccf242017-08-03 18:30:35 -0700229
Saurav Das7bcbe702017-06-13 15:35:54 -0700230 //////////////////////////////////////
231 // Route path handling
232 //////////////////////////////////////
233
Saurav Das45f48152018-01-18 12:07:33 -0800234 /* The following three methods represent the three major ways in which
235 * route-path handling is triggered in the network
Saurav Das7bcbe702017-06-13 15:35:54 -0700236 * a) due to configuration change
237 * b) due to route-added event
238 * c) due to change in the topology
239 */
240
Saurav Dasc88d4662017-05-15 15:34:25 -0700241 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700242 * Populates all routing rules to all switches. Typically triggered at
243 * startup or after a configuration event.
sanghob35a6192015-04-01 13:05:26 -0700244 */
Saurav Dasc88d4662017-05-15 15:34:25 -0700245 public void populateAllRoutingRules() {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700246 lastRoutingChange = Instant.now();
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900247 statusLock.lock();
248 try {
Saurav Das7bcbe702017-06-13 15:35:54 -0700249 if (populationStatus == Status.STARTED) {
250 log.warn("Previous rule population is not finished. Cannot"
251 + " proceed with populateAllRoutingRules");
252 return;
253 }
254
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900255 populationStatus = Status.STARTED;
256 rulePopulator.resetCounter();
Saurav Das7bcbe702017-06-13 15:35:54 -0700257 log.info("Starting to populate all routing rules");
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900258 log.debug("populateAllRoutingRules: populationStatus is STARTED");
sanghob35a6192015-04-01 13:05:26 -0700259
Saurav Das7bcbe702017-06-13 15:35:54 -0700260 // take a snapshot of the topology
261 updatedEcmpSpgMap = new HashMap<>();
262 Set<EdgePair> edgePairs = new HashSet<>();
263 Set<ArrayList<DeviceId>> routeChanges = new HashSet<>();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800264 for (DeviceId dstSw : srManager.deviceConfiguration.getRouters()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700265 EcmpShortestPathGraph ecmpSpgUpdated =
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800266 new EcmpShortestPathGraph(dstSw, srManager);
267 updatedEcmpSpgMap.put(dstSw, ecmpSpgUpdated);
Charles Chanba6c5752018-04-02 11:46:38 -0700268 Optional<DeviceId> pairDev = srManager.getPairDeviceId(dstSw);
269 if (pairDev.isPresent()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700270 // pairDev may not be available yet, but we still need to add
Charles Chanba6c5752018-04-02 11:46:38 -0700271 ecmpSpgUpdated = new EcmpShortestPathGraph(pairDev.get(), srManager);
272 updatedEcmpSpgMap.put(pairDev.get(), ecmpSpgUpdated);
273 edgePairs.add(new EdgePair(dstSw, pairDev.get()));
Saurav Das7bcbe702017-06-13 15:35:54 -0700274 }
Charles Chan2ff1bac2018-03-29 16:03:41 -0700275
276 if (!shouldProgram(dstSw)) {
Saurav Dasc6dc1772018-04-21 17:19:48 -0700277 lastProgrammed.remove(dstSw);
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900278 continue;
Saurav Dasc6dc1772018-04-21 17:19:48 -0700279 } else {
280 lastProgrammed.add(dstSw);
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900281 }
Saurav Dasc6dc1772018-04-21 17:19:48 -0700282 // To do a full reroute, assume all route-paths have changed
Charles Chan2ff1bac2018-03-29 16:03:41 -0700283 for (DeviceId dev : deviceAndItsPair(dstSw)) {
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800284 for (DeviceId targetSw : srManager.deviceConfiguration.getRouters()) {
285 if (targetSw.equals(dev)) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700286 continue;
287 }
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800288 routeChanges.add(Lists.newArrayList(targetSw, dev));
Saurav Das7bcbe702017-06-13 15:35:54 -0700289 }
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900290 }
Saurav Das7bcbe702017-06-13 15:35:54 -0700291 }
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900292
pier7398a5b2020-01-07 15:39:39 +0100293 log.debug("seenBeforeRoutes size {}", seenBeforeRoutes.size());
Saurav Das7bcbe702017-06-13 15:35:54 -0700294 if (!redoRouting(routeChanges, edgePairs, null)) {
295 log.debug("populateAllRoutingRules: populationStatus is ABORTED");
296 populationStatus = Status.ABORTED;
297 log.warn("Failed to repopulate all routing rules.");
298 return;
sanghob35a6192015-04-01 13:05:26 -0700299 }
300
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900301 log.debug("populateAllRoutingRules: populationStatus is SUCCEEDED");
302 populationStatus = Status.SUCCEEDED;
Saurav Das7bcbe702017-06-13 15:35:54 -0700303 log.info("Completed all routing rule population. Total # of rules pushed : {}",
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900304 rulePopulator.getCounter());
Saurav Dasc88d4662017-05-15 15:34:25 -0700305 return;
pier4bc3fa92019-04-19 20:55:53 +0200306 } catch (Exception e) {
307 log.error("populateAllRoutingRules thrown an exception: {}",
308 e.getMessage(), e);
309 populationStatus = Status.ABORTED;
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900310 } finally {
311 statusLock.unlock();
sanghob35a6192015-04-01 13:05:26 -0700312 }
sanghob35a6192015-04-01 13:05:26 -0700313 }
314
sangho20eff1d2015-04-13 15:15:58 -0700315 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700316 * Populate rules from all other edge devices to the connect-point(s)
317 * specified for the given subnets.
318 *
319 * @param cpts connect point(s) of the subnets being added
320 * @param subnets subnets being added
Charles Chan2fde6d42017-08-23 14:46:43 -0700321 */
322 // XXX refactor
Saurav Das7bcbe702017-06-13 15:35:54 -0700323 protected void populateSubnet(Set<ConnectPoint> cpts, Set<IpPrefix> subnets) {
Charles Chan71e64f12017-09-11 15:21:57 -0700324 if (cpts == null || cpts.size() < 1 || cpts.size() > 2) {
325 log.warn("Skipping populateSubnet due to illegal size of connect points. {}", cpts);
326 return;
327 }
328
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700329 lastRoutingChange = Instant.now();
Saurav Das7bcbe702017-06-13 15:35:54 -0700330 statusLock.lock();
331 try {
332 if (populationStatus == Status.STARTED) {
333 log.warn("Previous rule population is not finished. Cannot"
334 + " proceed with routing rules for added routes");
335 return;
336 }
337 populationStatus = Status.STARTED;
338 rulePopulator.resetCounter();
Charles Chan2fde6d42017-08-23 14:46:43 -0700339 log.info("Starting to populate routing rules for added routes, subnets={}, cpts={}",
340 subnets, cpts);
Saurav Dasc568c342018-01-25 09:49:01 -0800341 // In principle an update to a subnet/prefix should not require a
342 // new ECMPspg calculation as it is not a topology event. As a
343 // result, we use the current/existing ECMPspg in the updated map
344 // used by the redoRouting method.
Saurav Das15a81782018-02-09 09:15:03 -0800345 if (updatedEcmpSpgMap == null) {
346 updatedEcmpSpgMap = new HashMap<>();
347 }
Saurav Dasc568c342018-01-25 09:49:01 -0800348 currentEcmpSpgMap.entrySet().forEach(entry -> {
349 updatedEcmpSpgMap.put(entry.getKey(), entry.getValue());
Saurav Dase7f51012018-02-09 17:26:45 -0800350 if (log.isTraceEnabled()) {
351 log.trace("Root switch: {}", entry.getKey());
352 log.trace(" Current/Existing SPG: {}", entry.getValue());
Saurav Dasc568c342018-01-25 09:49:01 -0800353 }
354 });
pier7398a5b2020-01-07 15:39:39 +0100355 log.debug("seenBeforeRoutes size {}", seenBeforeRoutes.size());
Saurav Das7bcbe702017-06-13 15:35:54 -0700356 Set<EdgePair> edgePairs = new HashSet<>();
357 Set<ArrayList<DeviceId>> routeChanges = new HashSet<>();
358 boolean handleRouting = false;
359
360 if (cpts.size() == 2) {
361 // ensure connect points are edge-pairs
362 Iterator<ConnectPoint> iter = cpts.iterator();
363 DeviceId dev1 = iter.next().deviceId();
Charles Chanba6c5752018-04-02 11:46:38 -0700364 Optional<DeviceId> pairDev = srManager.getPairDeviceId(dev1);
365 if (pairDev.isPresent() && iter.next().deviceId().equals(pairDev.get())) {
366 edgePairs.add(new EdgePair(dev1, pairDev.get()));
Saurav Das7bcbe702017-06-13 15:35:54 -0700367 } else {
368 log.warn("Connectpoints {} for subnets {} not on "
369 + "pair-devices.. aborting populateSubnet", cpts, subnets);
370 populationStatus = Status.ABORTED;
371 return;
372 }
373 for (ConnectPoint cp : cpts) {
Saurav Dasc568c342018-01-25 09:49:01 -0800374 if (updatedEcmpSpgMap.get(cp.deviceId()) == null) {
375 EcmpShortestPathGraph ecmpSpgUpdated =
Saurav Das7bcbe702017-06-13 15:35:54 -0700376 new EcmpShortestPathGraph(cp.deviceId(), srManager);
Saurav Dasc568c342018-01-25 09:49:01 -0800377 updatedEcmpSpgMap.put(cp.deviceId(), ecmpSpgUpdated);
378 log.warn("populateSubnet: no updated graph for dev:{}"
379 + " ... creating", cp.deviceId());
380 }
Charles Chan2ff1bac2018-03-29 16:03:41 -0700381 if (!shouldProgram(cp.deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700382 continue;
383 }
384 handleRouting = true;
385 }
386 } else {
387 // single connect point
388 DeviceId dstSw = cpts.iterator().next().deviceId();
Saurav Dasc568c342018-01-25 09:49:01 -0800389 if (updatedEcmpSpgMap.get(dstSw) == null) {
390 EcmpShortestPathGraph ecmpSpgUpdated =
Saurav Das7bcbe702017-06-13 15:35:54 -0700391 new EcmpShortestPathGraph(dstSw, srManager);
Saurav Dasc568c342018-01-25 09:49:01 -0800392 updatedEcmpSpgMap.put(dstSw, ecmpSpgUpdated);
393 log.warn("populateSubnet: no updated graph for dev:{}"
394 + " ... creating", dstSw);
395 }
Charles Chan2ff1bac2018-03-29 16:03:41 -0700396 handleRouting = shouldProgram(dstSw);
Saurav Das7bcbe702017-06-13 15:35:54 -0700397 }
398
399 if (!handleRouting) {
400 log.debug("This instance is not handling ecmp routing to the "
401 + "connectPoint(s) {}", cpts);
402 populationStatus = Status.ABORTED;
403 return;
404 }
405
406 // if it gets here, this instance should handle routing for the
407 // connectpoint(s). Assume all route-paths have to be updated to
408 // the connectpoint(s) with the following exceptions
409 // 1. if target is non-edge no need for routing rules
410 // 2. if target is one of the connectpoints
411 for (ConnectPoint cp : cpts) {
412 DeviceId dstSw = cp.deviceId();
413 for (Device targetSw : srManager.deviceService.getDevices()) {
414 boolean isEdge = false;
415 try {
416 isEdge = config.isEdgeDevice(targetSw.id());
417 } catch (DeviceConfigNotFoundException e) {
Charles Chan92726132018-02-16 17:20:54 -0800418 log.warn(e.getMessage() + "aborting populateSubnet on targetSw {}", targetSw.id());
419 continue;
Saurav Das7bcbe702017-06-13 15:35:54 -0700420 }
Charles Chanba6c5752018-04-02 11:46:38 -0700421 Optional<DeviceId> pairDev = srManager.getPairDeviceId(dstSw);
Saurav Das7bcbe702017-06-13 15:35:54 -0700422 if (dstSw.equals(targetSw.id()) || !isEdge ||
Charles Chanba6c5752018-04-02 11:46:38 -0700423 (cpts.size() == 2 && pairDev.isPresent() && targetSw.id().equals(pairDev.get()))) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700424 continue;
425 }
426 routeChanges.add(Lists.newArrayList(targetSw.id(), dstSw));
427 }
428 }
429
430 if (!redoRouting(routeChanges, edgePairs, subnets)) {
431 log.debug("populateSubnet: populationStatus is ABORTED");
432 populationStatus = Status.ABORTED;
433 log.warn("Failed to repopulate the rules for subnet.");
434 return;
435 }
436
437 log.debug("populateSubnet: populationStatus is SUCCEEDED");
438 populationStatus = Status.SUCCEEDED;
439 log.info("Completed subnet population. Total # of rules pushed : {}",
440 rulePopulator.getCounter());
441 return;
442
pier4bc3fa92019-04-19 20:55:53 +0200443 } catch (Exception e) {
444 log.error("populateSubnet thrown an exception: {}",
445 e.getMessage(), e);
446 populationStatus = Status.ABORTED;
Saurav Das7bcbe702017-06-13 15:35:54 -0700447 } finally {
448 statusLock.unlock();
449 }
450 }
451
452 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700453 * Populates the routing rules or makes hash group changes according to the
454 * route-path changes due to link failure, switch failure or link up. This
455 * method should only be called for one of these three possible event-types.
Saurav Das604ab3a2018-03-18 21:28:15 -0700456 * Note that when a switch goes away, all of its links fail as well, but
457 * this is handled as a single switch removal event.
sangho20eff1d2015-04-13 15:15:58 -0700458 *
Saurav Das604ab3a2018-03-18 21:28:15 -0700459 * @param linkDown the single failed link, or null for other conditions such
460 * as link-up or a removed switch
Saurav Dasc88d4662017-05-15 15:34:25 -0700461 * @param linkUp the single link up, or null for other conditions such as
Saurav Das604ab3a2018-03-18 21:28:15 -0700462 * link-down or a removed switch
463 * @param switchDown the removed switch, or null for other conditions such
464 * as link-down or link-up
465 * @param seenBefore true if this event is for a linkUp or linkDown for a
466 * seen link
467 */
468 // TODO This method should be refactored into three separated methods
Charles Chan15281332018-06-19 20:56:33 -0700469 public void populateRoutingRulesForLinkStatusChange(Link linkDown, Link linkUp,
470 DeviceId switchDown, boolean seenBefore) {
Saurav Das604ab3a2018-03-18 21:28:15 -0700471 if (Stream.of(linkDown, linkUp, switchDown).filter(Objects::nonNull)
472 .count() != 1) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700473 log.warn("Only one event can be handled for link status change .. aborting");
474 return;
475 }
Saurav Das604ab3a2018-03-18 21:28:15 -0700476
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700477 lastRoutingChange = Instant.now();
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900478 statusLock.lock();
479 try {
sangho20eff1d2015-04-13 15:15:58 -0700480
481 if (populationStatus == Status.STARTED) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700482 log.warn("Previous rule population is not finished. Cannot"
Saurav Dasc568c342018-01-25 09:49:01 -0800483 + " proceeed with routingRules for Topology change");
Saurav Dasc88d4662017-05-15 15:34:25 -0700484 return;
sangho20eff1d2015-04-13 15:15:58 -0700485 }
486
Saurav Das7bcbe702017-06-13 15:35:54 -0700487 // Take snapshots of the topology
sangho45b009c2015-05-07 13:30:57 -0700488 updatedEcmpSpgMap = new HashMap<>();
Saurav Das7bcbe702017-06-13 15:35:54 -0700489 Set<EdgePair> edgePairs = new HashSet<>();
sangho45b009c2015-05-07 13:30:57 -0700490 for (Device sw : srManager.deviceService.getDevices()) {
Shashikanth VH013a7bc2015-12-11 01:32:44 +0530491 EcmpShortestPathGraph ecmpSpgUpdated =
492 new EcmpShortestPathGraph(sw.id(), srManager);
sangho45b009c2015-05-07 13:30:57 -0700493 updatedEcmpSpgMap.put(sw.id(), ecmpSpgUpdated);
Charles Chanba6c5752018-04-02 11:46:38 -0700494 Optional<DeviceId> pairDev = srManager.getPairDeviceId(sw.id());
495 if (pairDev.isPresent()) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700496 // pairDev may not be available yet, but we still need to add
Charles Chanba6c5752018-04-02 11:46:38 -0700497 ecmpSpgUpdated = new EcmpShortestPathGraph(pairDev.get(), srManager);
498 updatedEcmpSpgMap.put(pairDev.get(), ecmpSpgUpdated);
499 edgePairs.add(new EdgePair(sw.id(), pairDev.get()));
Saurav Das7bcbe702017-06-13 15:35:54 -0700500 }
sangho45b009c2015-05-07 13:30:57 -0700501 }
502
Saurav Dasc568c342018-01-25 09:49:01 -0800503 log.info("Starting to populate routing rules from Topology change");
sangho52abe3a2015-05-05 14:13:34 -0700504
sangho20eff1d2015-04-13 15:15:58 -0700505 Set<ArrayList<DeviceId>> routeChanges;
Saurav Dasc88d4662017-05-15 15:34:25 -0700506 log.debug("populateRoutingRulesForLinkStatusChange: "
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700507 + "populationStatus is STARTED");
pier7398a5b2020-01-07 15:39:39 +0100508 log.debug("seenBeforeRoutes size {}", seenBeforeRoutes.size());
sangho20eff1d2015-04-13 15:15:58 -0700509 populationStatus = Status.STARTED;
Saurav Dasc568c342018-01-25 09:49:01 -0800510 rulePopulator.resetCounter(); //XXX maybe useful to have a rehash ctr
511 boolean hashGroupsChanged = false;
Saurav Das4e3224f2016-11-29 14:27:25 -0800512 // try optimized re-routing
Saurav Dasc88d4662017-05-15 15:34:25 -0700513 if (linkDown == null) {
514 // either a linkUp or a switchDown - compute all route changes by
515 // comparing all routes of existing ECMP SPG to new ECMP SPG
Saurav Dase0d4c872018-03-05 14:37:16 -0800516 routeChanges = computeRouteChange(switchDown);
Saurav Dasc88d4662017-05-15 15:34:25 -0700517
pier1c2ca732019-04-25 18:51:51 +0200518 // deal with linkUp
519 if (linkUp != null) {
520 // deal with linkUp of a seen-before link
521 if (seenBefore) {
522 // link previously seen before
523 // do hash-bucket changes instead of a re-route
524 processHashGroupChangeForLinkUp(routeChanges);
525 // clear out routesChanges so a re-route is not attempted
526 routeChanges = ImmutableSet.of();
527 hashGroupsChanged = true;
528 } else {
529 // do hash-bucket changes first, method will return changed routes;
530 // for each route not changed it will perform a reroute
531 Set<ArrayList<DeviceId>> changedRoutes = processHashGroupChangeForLinkUp(routeChanges);
532 Set<ArrayList<DeviceId>> routeChangesTemp = getExpandedRoutes(routeChanges);
533 changedRoutes.forEach(routeChangesTemp::remove);
534 // if routesChanges is empty a re-route is not attempted
535 routeChanges = routeChangesTemp;
536 for (ArrayList<DeviceId> route : routeChanges) {
537 log.debug("remaining routes Target -> Root");
538 if (route.size() == 1) {
539 log.debug(" : all -> {}", route.get(0));
540 } else {
541 log.debug(" : {} -> {}", route.get(0), route.get(1));
542 }
543 }
544 // Mark hash groups as changed
545 if (!changedRoutes.isEmpty()) {
546 hashGroupsChanged = true;
547 }
548 }
549
Saurav Dasc88d4662017-05-15 15:34:25 -0700550 }
551
Saurav Das9df5b7c2017-08-14 16:44:43 -0700552 //deal with switchDown
553 if (switchDown != null) {
pier1c2ca732019-04-25 18:51:51 +0200554 processHashGroupChangeForFailure(routeChanges, switchDown);
Saurav Das9df5b7c2017-08-14 16:44:43 -0700555 // clear out routesChanges so a re-route is not attempted
556 routeChanges = ImmutableSet.of();
Saurav Dasc568c342018-01-25 09:49:01 -0800557 hashGroupsChanged = true;
Saurav Das9df5b7c2017-08-14 16:44:43 -0700558 }
sangho20eff1d2015-04-13 15:15:58 -0700559 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700560 // link has gone down
561 // Compare existing ECMP SPG only with the link that went down
562 routeChanges = computeDamagedRoutes(linkDown);
pier1c2ca732019-04-25 18:51:51 +0200563 processHashGroupChangeForFailure(routeChanges, null);
Saurav Dasf1027d42018-06-11 17:02:31 -0700564 // clear out routesChanges so a re-route is not attempted
565 routeChanges = ImmutableSet.of();
566 hashGroupsChanged = true;
Saurav Dasb5c236e2016-06-07 10:08:06 -0700567 }
568
sangho20eff1d2015-04-13 15:15:58 -0700569 if (routeChanges.isEmpty()) {
Saurav Dasc568c342018-01-25 09:49:01 -0800570 if (hashGroupsChanged) {
571 log.info("Hash-groups changed for link status change");
572 } else {
573 log.info("No re-route or re-hash attempted for the link"
574 + " status change");
575 updatedEcmpSpgMap.keySet().forEach(devId -> {
576 currentEcmpSpgMap.put(devId, updatedEcmpSpgMap.get(devId));
577 log.debug("Updating ECMPspg for remaining dev:{}", devId);
578 });
579 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700580 log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED");
sangho20eff1d2015-04-13 15:15:58 -0700581 populationStatus = Status.SUCCEEDED;
Saurav Dasc88d4662017-05-15 15:34:25 -0700582 return;
sangho20eff1d2015-04-13 15:15:58 -0700583 }
584
pier1c2ca732019-04-25 18:51:51 +0200585 if (hashGroupsChanged) {
586 log.debug("Hash-groups changed for link status change");
587 }
588
Saurav Dasc88d4662017-05-15 15:34:25 -0700589 // reroute of routeChanges
Saurav Das7bcbe702017-06-13 15:35:54 -0700590 if (redoRouting(routeChanges, edgePairs, null)) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700591 log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED");
sangho20eff1d2015-04-13 15:15:58 -0700592 populationStatus = Status.SUCCEEDED;
Saurav Das7bcbe702017-06-13 15:35:54 -0700593 log.info("Completed repopulation of rules for link-status change."
594 + " # of rules populated : {}", rulePopulator.getCounter());
Saurav Dasc88d4662017-05-15 15:34:25 -0700595 return;
sangho20eff1d2015-04-13 15:15:58 -0700596 } else {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700597 log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is ABORTED");
sangho20eff1d2015-04-13 15:15:58 -0700598 populationStatus = Status.ABORTED;
Saurav Das7bcbe702017-06-13 15:35:54 -0700599 log.warn("Failed to repopulate the rules for link status change.");
Saurav Dasc88d4662017-05-15 15:34:25 -0700600 return;
sangho20eff1d2015-04-13 15:15:58 -0700601 }
pier4bc3fa92019-04-19 20:55:53 +0200602 } catch (Exception e) {
603 log.error("populateRoutingRulesForLinkStatusChange thrown an exception: {}",
604 e.getMessage(), e);
605 populationStatus = Status.ABORTED;
HIGUCHI Yuta84a25fc2015-09-08 16:16:31 +0900606 } finally {
607 statusLock.unlock();
sangho20eff1d2015-04-13 15:15:58 -0700608 }
609 }
610
Saurav Dasc88d4662017-05-15 15:34:25 -0700611 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700612 * Processes a set a route-path changes by reprogramming routing rules and
613 * creating new hash-groups or editing them if necessary. This method also
614 * determines the next-hops for the route-path from the src-switch (target)
615 * of the path towards the dst-switch of the path.
Saurav Dasc88d4662017-05-15 15:34:25 -0700616 *
Saurav Das7bcbe702017-06-13 15:35:54 -0700617 * @param routeChanges a set of route-path changes, where each route-path is
618 * a list with its first element the src-switch (target)
619 * of the path, and the second element the dst-switch of
620 * the path.
621 * @param edgePairs a set of edge-switches that are paired by configuration
622 * @param subnets a set of prefixes that need to be populated in the routing
623 * table of the target switch in the route-path. Can be null,
624 * in which case all the prefixes belonging to the dst-switch
625 * will be populated in the target switch
626 * @return true if successful in repopulating all routes
Saurav Dasc88d4662017-05-15 15:34:25 -0700627 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700628 private boolean redoRouting(Set<ArrayList<DeviceId>> routeChanges,
629 Set<EdgePair> edgePairs, Set<IpPrefix> subnets) {
630 // first make every entry two-elements
pier1c2ca732019-04-25 18:51:51 +0200631 Set<ArrayList<DeviceId>> changedRoutes = getExpandedRoutes(routeChanges);
632 // no valid routes - fail fast
633 if (changedRoutes.isEmpty()) {
634 return false;
Saurav Das7bcbe702017-06-13 15:35:54 -0700635 }
636
pier7398a5b2020-01-07 15:39:39 +0100637 // Temporary stores the changed routes
638 Set<ArrayList<DeviceId>> tempRoutes = ImmutableSet.copyOf(changedRoutes);
Saurav Das7bcbe702017-06-13 15:35:54 -0700639 // now process changedRoutes according to edgePairs
640 if (!redoRoutingEdgePairs(edgePairs, subnets, changedRoutes)) {
641 return false; //abort routing and fail fast
642 }
pier7398a5b2020-01-07 15:39:39 +0100643 // Calculate the programmed routes pointing to the pairs
644 Set<ArrayList<DeviceId>> programmedPairRoutes = Sets.difference(tempRoutes, changedRoutes);
645 log.debug("Evaluating programmed pair routes");
646 storeSeenBeforeRoutes(programmedPairRoutes);
Saurav Das7bcbe702017-06-13 15:35:54 -0700647
pier7398a5b2020-01-07 15:39:39 +0100648 // Temporary stores the left routes
649 tempRoutes = ImmutableSet.copyOf(changedRoutes);
Saurav Das7bcbe702017-06-13 15:35:54 -0700650 // whatever is left in changedRoutes is now processed for individual dsts.
Saurav Dasc568c342018-01-25 09:49:01 -0800651 Set<DeviceId> updatedDevices = Sets.newHashSet();
652 if (!redoRoutingIndividualDests(subnets, changedRoutes,
653 updatedDevices)) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700654 return false; //abort routing and fail fast
655 }
pier7398a5b2020-01-07 15:39:39 +0100656 // Calculate the individual programmed routes
657 Set<ArrayList<DeviceId>> programmedIndividualRoutes = Sets.difference(tempRoutes, changedRoutes);
658 log.debug("Evaluating individual programmed routes");
659 storeSeenBeforeRoutes(programmedIndividualRoutes);
Saurav Das7bcbe702017-06-13 15:35:54 -0700660
Saurav Das7bcbe702017-06-13 15:35:54 -0700661 // update ecmpSPG for all edge-pairs
662 for (EdgePair ep : edgePairs) {
663 currentEcmpSpgMap.put(ep.dev1, updatedEcmpSpgMap.get(ep.dev1));
664 currentEcmpSpgMap.put(ep.dev2, updatedEcmpSpgMap.get(ep.dev2));
665 log.debug("Updating ECMPspg for edge-pair:{}-{}", ep.dev1, ep.dev2);
666 }
Saurav Dasc568c342018-01-25 09:49:01 -0800667
668 // here is where we update all devices not touched by this instance
669 updatedEcmpSpgMap.keySet().stream()
670 .filter(devId -> !edgePairs.stream().anyMatch(ep -> ep.includes(devId)))
671 .filter(devId -> !updatedDevices.contains(devId))
672 .forEach(devId -> {
673 currentEcmpSpgMap.put(devId, updatedEcmpSpgMap.get(devId));
674 log.debug("Updating ECMPspg for remaining dev:{}", devId);
675 });
Saurav Das7bcbe702017-06-13 15:35:54 -0700676 return true;
677 }
678
679 /**
pier7398a5b2020-01-07 15:39:39 +0100680 * Stores the routes seen before. Routes are two-elements arrays.
681 * @param seenRoutes seen before routes
682 */
683 private void storeSeenBeforeRoutes(Set<ArrayList<DeviceId>> seenRoutes) {
684 Set<DeviceId> nextHops;
685 for (ArrayList<DeviceId> route : seenRoutes) {
686 log.debug("Route {} -> {} has been programmed", route.get(0), route.get(1));
687 nextHops = getNextHops(route.get(0), route.get(1));
688 // No valid next hops - cannot be considered a programmed route
689 if (nextHops.isEmpty()) {
690 log.debug("Could not find next hop from target:{} --> dst {} "
691 + "skipping this route", route.get(0), route.get(1));
692 continue;
693 }
694 // Already present - do not add again
695 if (seenBeforeRoutes.containsEntry(route.get(1), route.get(0))) {
696 log.debug("Route from target:{} --> dst {} " +
697 "already present, skipping this route", route.get(0), route.get(1));
698 continue;
699 }
700 seenBeforeRoutes.put(route.get(1), route.get(0));
701 }
702 }
703
704 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700705 * Programs targetSw in the changedRoutes for given prefixes reachable by
706 * an edgePair. If no prefixes are given, the method will use configured
707 * subnets/prefixes. If some configured subnets belong only to a specific
708 * destination in the edgePair, then the target switch will be programmed
709 * only to that destination.
710 *
711 * @param edgePairs set of edge-pairs for which target will be programmed
712 * @param subnets a set of prefixes that need to be populated in the routing
713 * table of the target switch in the changedRoutes. Can be null,
714 * in which case all the configured prefixes belonging to the
715 * paired switches will be populated in the target switch
716 * @param changedRoutes a set of route-path changes, where each route-path is
717 * a list with its first element the src-switch (target)
718 * of the path, and the second element the dst-switch of
719 * the path.
720 * @return true if successful
721 */
pier8b4ba992019-04-24 16:12:47 +0200722 private boolean redoRoutingEdgePairs(Set<EdgePair> edgePairs, Set<IpPrefix> subnets,
723 Set<ArrayList<DeviceId>> changedRoutes) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700724 for (EdgePair ep : edgePairs) {
725 // temp store for a target's changedRoutes to this edge-pair
726 Map<DeviceId, Set<ArrayList<DeviceId>>> targetRoutes = new HashMap<>();
727 Iterator<ArrayList<DeviceId>> i = changedRoutes.iterator();
728 while (i.hasNext()) {
729 ArrayList<DeviceId> route = i.next();
730 DeviceId dstSw = route.get(1);
731 if (ep.includes(dstSw)) {
732 // routeChange for edge pair found
733 // sort by target iff target is edge and remove from changedRoutes
734 DeviceId targetSw = route.get(0);
735 try {
736 if (!srManager.deviceConfiguration.isEdgeDevice(targetSw)) {
737 continue;
738 }
739 } catch (DeviceConfigNotFoundException e) {
740 log.warn(e.getMessage() + "aborting redoRouting");
741 return false;
742 }
743 // route is from another edge to this edge-pair
744 if (targetRoutes.containsKey(targetSw)) {
745 targetRoutes.get(targetSw).add(route);
746 } else {
747 Set<ArrayList<DeviceId>> temp = new HashSet<>();
748 temp.add(route);
749 targetRoutes.put(targetSw, temp);
750 }
751 i.remove();
752 }
753 }
754 // so now for this edgepair we have a per target set of routechanges
755 // process target->edgePair route
pier8b4ba992019-04-24 16:12:47 +0200756 List<Future<Boolean>> futures = Lists.newArrayList();
pier7398a5b2020-01-07 15:39:39 +0100757 for (Entry<DeviceId, Set<ArrayList<DeviceId>>> entry :
Saurav Das7bcbe702017-06-13 15:35:54 -0700758 targetRoutes.entrySet()) {
759 log.debug("* redoRoutingDstPair Target:{} -> edge-pair {}",
760 entry.getKey(), ep);
pier8b4ba992019-04-24 16:12:47 +0200761 futures.add(routePopulators.submit(new RedoRoutingEdgePair(entry.getKey(), entry.getValue(),
762 subnets, ep)));
763 }
764 if (!checkJobs(futures)) {
765 return false;
Saurav Das7bcbe702017-06-13 15:35:54 -0700766 }
767 // if it gets here it has succeeded for all targets to this edge-pair
768 }
769 return true;
770 }
771
pier8b4ba992019-04-24 16:12:47 +0200772 private final class RedoRoutingEdgePair implements PickyCallable<Boolean> {
773 private DeviceId targetSw;
774 private Set<ArrayList<DeviceId>> routes;
775 private Set<IpPrefix> subnets;
776 private EdgePair ep;
777
778 /**
779 * Builds a RedoRoutingEdgePair task which provides a result.
780 *
781 * @param targetSw the target switch
782 * @param routes the changed routes
783 * @param subnets the subnets
784 * @param ep the edge pair
785 */
786 RedoRoutingEdgePair(DeviceId targetSw, Set<ArrayList<DeviceId>> routes,
787 Set<IpPrefix> subnets, EdgePair ep) {
788 this.targetSw = targetSw;
789 this.routes = routes;
790 this.subnets = subnets;
791 this.ep = ep;
792 }
793
794 @Override
795 public Boolean call() throws Exception {
796 return redoRoutingEdgePair();
797 }
798
799 @Override
800 public int hint() {
801 return targetSw.hashCode();
802 }
803
804 private boolean redoRoutingEdgePair() {
805 Map<DeviceId, Set<DeviceId>> perDstNextHops = new HashMap<>();
806 routes.forEach(route -> {
807 Set<DeviceId> nhops = getNextHops(route.get(0), route.get(1));
808 log.debug("route: target {} -> dst {} found with next-hops {}",
809 route.get(0), route.get(1), nhops);
810 perDstNextHops.put(route.get(1), nhops);
811 });
812
813 List<Set<IpPrefix>> batchedSubnetDev1, batchedSubnetDev2;
814 if (subnets != null) {
815 batchedSubnetDev1 = Lists.<Set<IpPrefix>>newArrayList(Sets.newHashSet(subnets));
816 batchedSubnetDev2 = Lists.<Set<IpPrefix>>newArrayList(Sets.newHashSet(subnets));
817 } else {
818 batchedSubnetDev1 = config.getBatchedSubnets(ep.dev1);
819 batchedSubnetDev2 = config.getBatchedSubnets(ep.dev2);
820 }
821 List<Set<IpPrefix>> batchedSubnetBoth = Streams
822 .zip(batchedSubnetDev1.stream(), batchedSubnetDev2.stream(), (a, b) -> Sets.intersection(a, b))
823 .filter(set -> !set.isEmpty())
824 .collect(Collectors.toList());
825 List<Set<IpPrefix>> batchedSubnetDev1Only = Streams
826 .zip(batchedSubnetDev1.stream(), batchedSubnetDev2.stream(), (a, b) -> Sets.difference(a, b))
827 .filter(set -> !set.isEmpty())
828 .collect(Collectors.toList());
829 List<Set<IpPrefix>> batchedSubnetDev2Only = Streams
830 .zip(batchedSubnetDev1.stream(), batchedSubnetDev2.stream(), (a, b) -> Sets.difference(b, a))
831 .filter(set -> !set.isEmpty())
832 .collect(Collectors.toList());
833
834 Set<DeviceId> nhDev1 = perDstNextHops.get(ep.dev1);
835 Set<DeviceId> nhDev2 = perDstNextHops.get(ep.dev2);
836
837 // handle routing to subnets common to edge-pair
838 // only if the targetSw is not part of the edge-pair and there
839 // exists a next hop to at least one of the devices in the edge-pair
840 if (!ep.includes(targetSw)
841 && ((nhDev1 != null && !nhDev1.isEmpty()) || (nhDev2 != null && !nhDev2.isEmpty()))) {
842 log.trace("getSubnets on both {} and {}: {}", ep.dev1, ep.dev2, batchedSubnetBoth);
843 for (Set<IpPrefix> prefixes : batchedSubnetBoth) {
844 if (!populateEcmpRoutingRulePartial(targetSw, ep.dev1, ep.dev2,
845 perDstNextHops, prefixes)) {
846 return false; // abort everything and fail fast
847 }
848 }
849
850 }
851 // handle routing to subnets that only belong to dev1 only if
852 // a next-hop exists from the target to dev1
853 if (!batchedSubnetDev1Only.isEmpty() &&
854 batchedSubnetDev1Only.stream().anyMatch(subnet -> !subnet.isEmpty()) &&
855 nhDev1 != null && !nhDev1.isEmpty()) {
856 Map<DeviceId, Set<DeviceId>> onlyDev1NextHops = new HashMap<>();
857 onlyDev1NextHops.put(ep.dev1, nhDev1);
858 log.trace("getSubnets on {} only: {}", ep.dev1, batchedSubnetDev1Only);
859 for (Set<IpPrefix> prefixes : batchedSubnetDev1Only) {
860 if (!populateEcmpRoutingRulePartial(targetSw, ep.dev1, null,
861 onlyDev1NextHops, prefixes)) {
862 return false; // abort everything and fail fast
863 }
864 }
865 }
866 // handle routing to subnets that only belong to dev2 only if
867 // a next-hop exists from the target to dev2
868 if (!batchedSubnetDev2Only.isEmpty() &&
869 batchedSubnetDev2Only.stream().anyMatch(subnet -> !subnet.isEmpty()) &&
870 nhDev2 != null && !nhDev2.isEmpty()) {
871 Map<DeviceId, Set<DeviceId>> onlyDev2NextHops = new HashMap<>();
872 onlyDev2NextHops.put(ep.dev2, nhDev2);
873 log.trace("getSubnets on {} only: {}", ep.dev2, batchedSubnetDev2Only);
874 for (Set<IpPrefix> prefixes : batchedSubnetDev2Only) {
875 if (!populateEcmpRoutingRulePartial(targetSw, ep.dev2, null,
876 onlyDev2NextHops, prefixes)) {
877 return false; // abort everything and fail fast
878 }
879 }
880 }
881 return true;
882 }
883 }
884
Saurav Das7bcbe702017-06-13 15:35:54 -0700885 /**
886 * Programs targetSw in the changedRoutes for given prefixes reachable by
887 * a destination switch that is not part of an edge-pair.
888 * If no prefixes are given, the method will use configured subnets/prefixes.
889 *
890 * @param subnets a set of prefixes that need to be populated in the routing
891 * table of the target switch in the changedRoutes. Can be null,
892 * in which case all the configured prefixes belonging to the
893 * paired switches will be populated in the target switch
894 * @param changedRoutes a set of route-path changes, where each route-path is
895 * a list with its first element the src-switch (target)
896 * of the path, and the second element the dst-switch of
897 * the path.
898 * @return true if successful
899 */
pier8b4ba992019-04-24 16:12:47 +0200900 private boolean redoRoutingIndividualDests(Set<IpPrefix> subnets, Set<ArrayList<DeviceId>> changedRoutes,
Saurav Dasc568c342018-01-25 09:49:01 -0800901 Set<DeviceId> updatedDevices) {
Saurav Das7bcbe702017-06-13 15:35:54 -0700902 // aggregate route-path changes for each dst device
903 HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> routesBydevice =
904 new HashMap<>();
905 for (ArrayList<DeviceId> route: changedRoutes) {
906 DeviceId dstSw = route.get(1);
907 ArrayList<ArrayList<DeviceId>> deviceRoutes =
908 routesBydevice.get(dstSw);
909 if (deviceRoutes == null) {
910 deviceRoutes = new ArrayList<>();
911 routesBydevice.put(dstSw, deviceRoutes);
912 }
913 deviceRoutes.add(route);
914 }
pier8b4ba992019-04-24 16:12:47 +0200915 // iterate over the impacted devices
Saurav Das7bcbe702017-06-13 15:35:54 -0700916 for (DeviceId impactedDstDevice : routesBydevice.keySet()) {
917 ArrayList<ArrayList<DeviceId>> deviceRoutes =
918 routesBydevice.get(impactedDstDevice);
pier8b4ba992019-04-24 16:12:47 +0200919 List<Future<Boolean>> futures = Lists.newArrayList();
Saurav Das7bcbe702017-06-13 15:35:54 -0700920 for (ArrayList<DeviceId> route: deviceRoutes) {
921 log.debug("* redoRoutingIndiDst Target: {} -> dst: {}",
922 route.get(0), route.get(1));
pier8b4ba992019-04-24 16:12:47 +0200923 futures.add(routePopulators.submit(new RedoRoutingIndividualDest(subnets, route)));
pier7398a5b2020-01-07 15:39:39 +0100924 changedRoutes.remove(route);
pier8b4ba992019-04-24 16:12:47 +0200925 }
926 // check the execution of each job
927 if (!checkJobs(futures)) {
928 return false;
Saurav Das7bcbe702017-06-13 15:35:54 -0700929 }
930 //Only if all the flows for all impacted routes to a
931 //specific target are pushed successfully, update the
932 //ECMP graph for that target. Or else the next event
933 //would not see any changes in the ECMP graphs.
934 //In another case, the target switch has gone away, so
935 //routes can't be installed. In that case, the current map
936 //is updated here, without any flows being pushed.
937 currentEcmpSpgMap.put(impactedDstDevice,
938 updatedEcmpSpgMap.get(impactedDstDevice));
Saurav Dasc568c342018-01-25 09:49:01 -0800939 updatedDevices.add(impactedDstDevice);
Saurav Das7bcbe702017-06-13 15:35:54 -0700940 log.debug("Updating ECMPspg for impacted dev:{}", impactedDstDevice);
941 }
942 return true;
943 }
944
pier8b4ba992019-04-24 16:12:47 +0200945 private final class RedoRoutingIndividualDest implements PickyCallable<Boolean> {
946 private DeviceId targetSw;
947 private ArrayList<DeviceId> route;
948 private Set<IpPrefix> subnets;
949
950 /**
951 * Builds a RedoRoutingIndividualDest task, which provides a result.
952 *
953 * @param subnets a set of prefixes
954 * @param route a route-path change
955 */
956 RedoRoutingIndividualDest(Set<IpPrefix> subnets, ArrayList<DeviceId> route) {
957 this.targetSw = route.get(0);
958 this.route = route;
959 this.subnets = subnets;
960 }
961
962 @Override
963 public Boolean call() throws Exception {
964 DeviceId dstSw = route.get(1); // same as impactedDstDevice
965 Set<DeviceId> nextHops = getNextHops(targetSw, dstSw);
966 if (nextHops.isEmpty()) {
967 log.debug("Could not find next hop from target:{} --> dst {} "
968 + "skipping this route", targetSw, dstSw);
969 return true;
970 }
971 Map<DeviceId, Set<DeviceId>> nhops = new HashMap<>();
972 nhops.put(dstSw, nextHops);
973 if (!populateEcmpRoutingRulePartial(targetSw, dstSw, null, nhops,
974 (subnets == null) ? Sets.newHashSet() : subnets)) {
975 return false; // abort routing and fail fast
976 }
977 log.debug("Populating flow rules from target: {} to dst: {}"
978 + " is successful", targetSw, dstSw);
979 return true;
980 }
981
982 @Override
983 public int hint() {
984 return targetSw.hashCode();
985 }
986 }
987
Saurav Das7bcbe702017-06-13 15:35:54 -0700988 /**
989 * Populate ECMP rules for subnets from target to destination via nexthops.
990 *
991 * @param targetSw Device ID of target switch in which rules will be programmed
992 * @param destSw1 Device ID of final destination switch to which the rules will forward
993 * @param destSw2 Device ID of paired destination switch to which the rules will forward
994 * A null deviceId indicates packets should only be sent to destSw1
Saurav Dasa4020382018-02-14 14:14:54 -0800995 * @param nextHops Map of a set of next hops per destSw
Saurav Das7bcbe702017-06-13 15:35:54 -0700996 * @param subnets Subnets to be populated. If empty, populate all configured subnets.
997 * @return true if it succeeds in populating rules
998 */ // refactor
pier8b4ba992019-04-24 16:12:47 +0200999 private boolean populateEcmpRoutingRulePartial(DeviceId targetSw, DeviceId destSw1, DeviceId destSw2,
1000 Map<DeviceId, Set<DeviceId>> nextHops, Set<IpPrefix> subnets) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001001 boolean result;
1002 // If both target switch and dest switch are edge routers, then set IP
1003 // rule for both subnet and router IP.
1004 boolean targetIsEdge;
1005 boolean dest1IsEdge;
1006 Ip4Address dest1RouterIpv4, dest2RouterIpv4 = null;
1007 Ip6Address dest1RouterIpv6, dest2RouterIpv6 = null;
1008
1009 try {
1010 targetIsEdge = config.isEdgeDevice(targetSw);
1011 dest1IsEdge = config.isEdgeDevice(destSw1);
1012 dest1RouterIpv4 = config.getRouterIpv4(destSw1);
1013 dest1RouterIpv6 = config.getRouterIpv6(destSw1);
1014 if (destSw2 != null) {
1015 dest2RouterIpv4 = config.getRouterIpv4(destSw2);
1016 dest2RouterIpv6 = config.getRouterIpv6(destSw2);
1017 }
1018 } catch (DeviceConfigNotFoundException e) {
1019 log.warn(e.getMessage() + " Aborting populateEcmpRoutingRulePartial.");
Saurav Dasc88d4662017-05-15 15:34:25 -07001020 return false;
1021 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001022
1023 if (targetIsEdge && dest1IsEdge) {
Charles Chan022d6672019-04-17 14:20:26 -07001024 List<Set<IpPrefix>> batchedSubnets;
1025 if (subnets != null && !subnets.isEmpty()) {
1026 batchedSubnets = Lists.<Set<IpPrefix>>newArrayList(Sets.newHashSet(subnets));
1027 } else {
1028 batchedSubnets = config.getBatchedSubnets(destSw1);
1029 }
Saurav Dasa4020382018-02-14 14:14:54 -08001030 // XXX - Rethink this - ignoring routerIPs in all other switches
1031 // even edge to edge switches
Saurav Das7bcbe702017-06-13 15:35:54 -07001032 /*subnets.add(dest1RouterIpv4.toIpPrefix());
1033 if (dest1RouterIpv6 != null) {
1034 subnets.add(dest1RouterIpv6.toIpPrefix());
1035 }
1036 if (destSw2 != null && dest2RouterIpv4 != null) {
1037 subnets.add(dest2RouterIpv4.toIpPrefix());
1038 if (dest2RouterIpv6 != null) {
1039 subnets.add(dest2RouterIpv6.toIpPrefix());
1040 }
1041 }*/
Charles Chan022d6672019-04-17 14:20:26 -07001042 log.trace("getSubnets on {}: {}", destSw1, batchedSubnets);
1043 for (Set<IpPrefix> prefixes : batchedSubnets) {
1044 log.debug(". populateEcmpRoutingRulePartial in device {} towards {} {} "
1045 + "for subnets {}", targetSw, destSw1,
1046 (destSw2 != null) ? ("& " + destSw2) : "",
1047 prefixes);
1048 if (!rulePopulator.populateIpRuleForSubnet(targetSw, prefixes, destSw1, destSw2, nextHops)) {
1049 return false;
1050 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001051 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001052 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001053
1054 if (!targetIsEdge && dest1IsEdge) {
1055 // MPLS rules in all non-edge target devices. These rules are for
1056 // individual destinations, even if the dsts are part of edge-pairs.
1057 log.debug(". populateEcmpRoutingRulePartial in device{} towards {} for "
1058 + "all MPLS rules", targetSw, destSw1);
pier8b4ba992019-04-24 16:12:47 +02001059 result = rulePopulator.populateMplsRule(targetSw, destSw1, nextHops.get(destSw1), dest1RouterIpv4);
Saurav Das7bcbe702017-06-13 15:35:54 -07001060 if (!result) {
1061 return false;
1062 }
1063 if (dest1RouterIpv6 != null) {
Saurav Dasa4020382018-02-14 14:14:54 -08001064 int v4sid = 0, v6sid = 0;
1065 try {
1066 v4sid = config.getIPv4SegmentId(destSw1);
1067 v6sid = config.getIPv6SegmentId(destSw1);
1068 } catch (DeviceConfigNotFoundException e) {
1069 log.warn(e.getMessage());
1070 }
1071 if (v4sid != v6sid) {
pier8b4ba992019-04-24 16:12:47 +02001072 result = rulePopulator.populateMplsRule(targetSw, destSw1, nextHops.get(destSw1),
Saurav Dasa4020382018-02-14 14:14:54 -08001073 dest1RouterIpv6);
1074 if (!result) {
1075 return false;
1076 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001077 }
1078 }
1079 }
1080
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -07001081 if (!targetIsEdge && !dest1IsEdge) {
1082 // MPLS rules for inter-connected spines
1083 // can be merged with above if, left it here for clarity
1084 log.debug(". populateEcmpRoutingRulePartial in device{} towards {} for "
1085 + "all MPLS rules", targetSw, destSw1);
1086
pier8b4ba992019-04-24 16:12:47 +02001087 result = rulePopulator.populateMplsRule(targetSw, destSw1, nextHops.get(destSw1), dest1RouterIpv4);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -07001088 if (!result) {
1089 return false;
1090 }
1091
1092 if (dest1RouterIpv6 != null) {
1093 int v4sid = 0, v6sid = 0;
1094 try {
1095 v4sid = config.getIPv4SegmentId(destSw1);
1096 v6sid = config.getIPv6SegmentId(destSw1);
1097 } catch (DeviceConfigNotFoundException e) {
1098 log.warn(e.getMessage());
1099 }
1100 if (v4sid != v6sid) {
pier8b4ba992019-04-24 16:12:47 +02001101 result = rulePopulator.populateMplsRule(targetSw, destSw1, nextHops.get(destSw1),
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -07001102 dest1RouterIpv6);
1103 if (!result) {
1104 return false;
1105 }
1106 }
1107 }
1108 }
1109
Saurav Das7bcbe702017-06-13 15:35:54 -07001110 // To save on ECMP groups
1111 // avoid MPLS rules in non-edge-devices to non-edge-devices
1112 // avoid MPLS transit rules in edge-devices
1113 // avoid loopback IP rules in edge-devices to non-edge-devices
1114 return true;
Saurav Dasc88d4662017-05-15 15:34:25 -07001115 }
1116
1117 /**
pier1c2ca732019-04-25 18:51:51 +02001118 * Processes a set a route-path changes due to a switch/link failure by editing hash groups.
Saurav Dasc88d4662017-05-15 15:34:25 -07001119 *
1120 * @param routeChanges a set of route-path changes, where each route-path is
1121 * a list with its first element the src-switch of the path
1122 * and the second element the dst-switch of the path.
Saurav Dasc88d4662017-05-15 15:34:25 -07001123 * @param failedSwitch the switchId if the route changes are for a failed switch,
1124 * otherwise null
1125 */
pier1c2ca732019-04-25 18:51:51 +02001126 private void processHashGroupChangeForFailure(Set<ArrayList<DeviceId>> routeChanges,
1127 DeviceId failedSwitch) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001128 // first, ensure each routeChanges entry has two elements
pier1c2ca732019-04-25 18:51:51 +02001129 Set<ArrayList<DeviceId>> changedRoutes = getAllExpandedRoutes(routeChanges);
Saurav Dasc568c342018-01-25 09:49:01 -08001130 boolean someFailed = false;
pier1c2ca732019-04-25 18:51:51 +02001131 boolean success;
Saurav Dasc568c342018-01-25 09:49:01 -08001132 Set<DeviceId> updatedDevices = Sets.newHashSet();
Saurav Das9df5b7c2017-08-14 16:44:43 -07001133 for (ArrayList<DeviceId> route : changedRoutes) {
1134 DeviceId targetSw = route.get(0);
1135 DeviceId dstSw = route.get(1);
pier1c2ca732019-04-25 18:51:51 +02001136 success = fixHashGroupsForRoute(route, true);
1137 // it's possible that we cannot fix hash groups for a route
1138 // if the target switch has failed. Nevertheless the ecmp graph
1139 // for the impacted switch must still be updated.
1140 if (!success && failedSwitch != null && targetSw.equals(failedSwitch)) {
1141 currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw));
1142 currentEcmpSpgMap.remove(targetSw);
1143 log.debug("Updating ECMPspg for dst:{} removing failed switch "
1144 + "target:{}", dstSw, targetSw);
1145 updatedDevices.add(targetSw);
1146 updatedDevices.add(dstSw);
1147 continue;
pier7398a5b2020-01-07 15:39:39 +01001148
pier1c2ca732019-04-25 18:51:51 +02001149 }
1150 //linkfailed - update both sides
1151 if (success) {
1152 currentEcmpSpgMap.put(targetSw, updatedEcmpSpgMap.get(targetSw));
1153 currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw));
1154 log.debug("Updating ECMPspg for dst:{} and target:{} for linkdown"
1155 + " or switchdown", dstSw, targetSw);
1156 updatedDevices.add(targetSw);
1157 updatedDevices.add(dstSw);
Saurav Das9df5b7c2017-08-14 16:44:43 -07001158 } else {
pier1c2ca732019-04-25 18:51:51 +02001159 someFailed = true;
Saurav Dasc88d4662017-05-15 15:34:25 -07001160 }
1161 }
Saurav Dasc568c342018-01-25 09:49:01 -08001162 if (!someFailed) {
1163 // here is where we update all devices not touched by this instance
1164 updatedEcmpSpgMap.keySet().stream()
1165 .filter(devId -> !updatedDevices.contains(devId))
1166 .forEach(devId -> {
1167 currentEcmpSpgMap.put(devId, updatedEcmpSpgMap.get(devId));
1168 log.debug("Updating ECMPspg for remaining dev:{}", devId);
1169 });
1170 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001171 }
1172
1173 /**
pier1c2ca732019-04-25 18:51:51 +02001174 * Processes a set a route-path changes due to link up by editing hash groups.
1175 *
1176 * @param routeChanges a set of route-path changes, where each route-path is
1177 * a list with its first element the src-switch of the path
1178 * and the second element the dst-switch of the path.
1179 * @return set of changed routes
1180 */
1181 private Set<ArrayList<DeviceId>> processHashGroupChangeForLinkUp(Set<ArrayList<DeviceId>> routeChanges) {
1182 // Stores changed routes
1183 Set<ArrayList<DeviceId>> doneRoutes = new HashSet<>();
1184 // first, ensure each routeChanges entry has two elements
1185 Set<ArrayList<DeviceId>> changedRoutes = getAllExpandedRoutes(routeChanges);
1186 boolean someFailed = false;
1187 boolean success;
1188 Set<DeviceId> updatedDevices = Sets.newHashSet();
1189 for (ArrayList<DeviceId> route : changedRoutes) {
1190 DeviceId targetSw = route.get(0);
1191 DeviceId dstSw = route.get(1);
1192 // linkup - fix (if possible)
1193 success = fixHashGroupsForRoute(route, false);
1194 if (success) {
1195 currentEcmpSpgMap.put(targetSw, updatedEcmpSpgMap.get(targetSw));
1196 currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw));
1197 log.debug("Updating ECMPspg for target:{} and dst:{} for linkup",
1198 targetSw, dstSw);
1199 updatedDevices.add(targetSw);
1200 updatedDevices.add(dstSw);
1201 doneRoutes.add(route);
1202 } else {
1203 someFailed = true;
1204 }
1205
1206 }
1207 if (!someFailed) {
1208 // here is where we update all devices not touched by this instance
1209 updatedEcmpSpgMap.keySet().stream()
1210 .filter(devId -> !updatedDevices.contains(devId))
1211 .forEach(devId -> {
1212 currentEcmpSpgMap.put(devId, updatedEcmpSpgMap.get(devId));
1213 log.debug("Updating ECMPspg for remaining dev:{}", devId);
1214 });
1215 }
1216 return doneRoutes;
1217 }
1218
1219 /**
Saurav Dasc88d4662017-05-15 15:34:25 -07001220 * Edits hash groups in the src-switch (targetSw) of a route-path by
1221 * calling the groupHandler to either add or remove buckets in an existing
1222 * hash group.
1223 *
1224 * @param route a single list representing a route-path where the first element
1225 * is the src-switch (targetSw) of the route-path and the
1226 * second element is the dst-switch
1227 * @param revoke true if buckets in the hash-groups need to be removed;
1228 * false if buckets in the hash-groups need to be added
1229 * @return true if the hash group editing is successful
1230 */
1231 private boolean fixHashGroupsForRoute(ArrayList<DeviceId> route,
1232 boolean revoke) {
1233 DeviceId targetSw = route.get(0);
1234 if (route.size() < 2) {
1235 log.warn("Cannot fixHashGroupsForRoute - no dstSw in route {}", route);
1236 return false;
1237 }
1238 DeviceId destSw = route.get(1);
pier7398a5b2020-01-07 15:39:39 +01001239 if (!seenBeforeRoutes.containsEntry(destSw, targetSw)) {
1240 log.warn("Cannot fixHashGroupsForRoute {} -> {} has not been programmed before",
1241 targetSw, destSw);
1242 return false;
1243 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001244 log.debug("* processing fixHashGroupsForRoute: Target {} -> Dest {}",
Saurav Dasc88d4662017-05-15 15:34:25 -07001245 targetSw, destSw);
Saurav Dasc88d4662017-05-15 15:34:25 -07001246 // figure out the new next hops at the targetSw towards the destSw
Saurav Das9df5b7c2017-08-14 16:44:43 -07001247 Set<DeviceId> nextHops = getNextHops(targetSw, destSw);
Saurav Dasc88d4662017-05-15 15:34:25 -07001248 // call group handler to change hash group at targetSw
1249 DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw);
1250 if (grpHandler == null) {
1251 log.warn("Cannot find grouphandler for dev:{} .. aborting"
1252 + " {} hash group buckets for route:{} ", targetSw,
1253 (revoke) ? "revoke" : "repopulate", route);
1254 return false;
1255 }
Saurav Dasf1027d42018-06-11 17:02:31 -07001256 log.debug("{} hash-groups buckets For Route {} -> {} to new next-hops {}",
Saurav Dasc88d4662017-05-15 15:34:25 -07001257 (revoke) ? "revoke" : "repopulating",
1258 targetSw, destSw, nextHops);
1259 return (revoke) ? grpHandler.fixHashGroups(targetSw, nextHops,
1260 destSw, true)
1261 : grpHandler.fixHashGroups(targetSw, nextHops,
1262 destSw, false);
1263 }
1264
1265 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001266 * Start the flow rule population process if it was never started. The
1267 * process finishes successfully when all flow rules are set and stops with
1268 * ABORTED status when any groups required for flows is not set yet.
Saurav Dasc88d4662017-05-15 15:34:25 -07001269 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001270 public void startPopulationProcess() {
1271 statusLock.lock();
1272 try {
1273 if (populationStatus == Status.IDLE
1274 || populationStatus == Status.SUCCEEDED
1275 || populationStatus == Status.ABORTED) {
1276 populateAllRoutingRules();
sangho45b009c2015-05-07 13:30:57 -07001277 } else {
Saurav Das7bcbe702017-06-13 15:35:54 -07001278 log.warn("Not initiating startPopulationProcess as populationStatus is {}",
1279 populationStatus);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001280 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001281 } finally {
1282 statusLock.unlock();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001283 }
sangho20eff1d2015-04-13 15:15:58 -07001284 }
1285
Saurav Dasb5c236e2016-06-07 10:08:06 -07001286 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001287 * Revoke rules of given subnet in all edge switches.
1288 *
1289 * @param subnets subnet being removed
1290 * @return true if succeed
1291 */
1292 protected boolean revokeSubnet(Set<IpPrefix> subnets) {
pier8b4ba992019-04-24 16:12:47 +02001293 DeviceId targetSw;
1294 List<Future<Boolean>> futures = Lists.newArrayList();
1295 for (Device sw : srManager.deviceService.getAvailableDevices()) {
1296 targetSw = sw.id();
1297 if (shouldProgram(targetSw)) {
1298 futures.add(routePopulators.submit(new RevokeSubnet(targetSw, subnets)));
1299 } else {
1300 futures.add(CompletableFuture.completedFuture(true));
1301 }
1302 }
1303 // check the execution of each job
1304 return checkJobs(futures);
1305 }
1306
1307 private final class RevokeSubnet implements PickyCallable<Boolean> {
1308 private DeviceId targetSw;
1309 private Set<IpPrefix> subnets;
1310
1311 /**
1312 * Builds a RevokeSubnet task, which provides a result.
1313 *
1314 * @param subnets a set of prefixes
1315 * @param targetSw target switch
1316 */
1317 RevokeSubnet(DeviceId targetSw, Set<IpPrefix> subnets) {
1318 this.targetSw = targetSw;
1319 this.subnets = subnets;
1320 }
1321
1322 @Override
1323 public Boolean call() throws Exception {
1324 return srManager.routingRulePopulator.revokeIpRuleForSubnet(targetSw, subnets);
1325 }
1326
1327 @Override
1328 public int hint() {
1329 return targetSw.hashCode();
Saurav Das7bcbe702017-06-13 15:35:54 -07001330 }
1331 }
1332
1333 /**
Charles Chan2fde6d42017-08-23 14:46:43 -07001334 * Populates IP rules for a route that has direct connection to the switch
1335 * if the current instance is the master of the switch.
1336 *
1337 * @param deviceId device ID of the device that next hop attaches to
1338 * @param prefix IP prefix of the route
1339 * @param hostMac MAC address of the next hop
1340 * @param hostVlanId Vlan ID of the nexthop
1341 * @param outPort port where the next hop attaches to
Ruchi Sahota5d800282019-01-28 01:08:18 +00001342 * @param directHost host is of type direct or indirect
Charles Chan2fde6d42017-08-23 14:46:43 -07001343 */
1344 void populateRoute(DeviceId deviceId, IpPrefix prefix,
Ruchi Sahota5d800282019-01-28 01:08:18 +00001345 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) {
Charles Chan2ff1bac2018-03-29 16:03:41 -07001346 if (shouldProgram(deviceId)) {
Ruchi Sahota5d800282019-01-28 01:08:18 +00001347 srManager.routingRulePopulator.populateRoute(deviceId, prefix, hostMac, hostVlanId, outPort, directHost);
Charles Chan2fde6d42017-08-23 14:46:43 -07001348 }
1349 }
1350
1351 /**
1352 * Removes IP rules for a route when the next hop is gone.
1353 * if the current instance is the master of the switch.
1354 *
1355 * @param deviceId device ID of the device that next hop attaches to
1356 * @param prefix IP prefix of the route
1357 * @param hostMac MAC address of the next hop
1358 * @param hostVlanId Vlan ID of the nexthop
1359 * @param outPort port that next hop attaches to
Ruchi Sahota5d800282019-01-28 01:08:18 +00001360 * @param directHost host is of type direct or indirect
Charles Chan2fde6d42017-08-23 14:46:43 -07001361 */
1362 void revokeRoute(DeviceId deviceId, IpPrefix prefix,
Ruchi Sahota5d800282019-01-28 01:08:18 +00001363 MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) {
Charles Chan2ff1bac2018-03-29 16:03:41 -07001364 if (shouldProgram(deviceId)) {
Ruchi Sahota5d800282019-01-28 01:08:18 +00001365 srManager.routingRulePopulator.revokeRoute(deviceId, prefix, hostMac, hostVlanId, outPort, directHost);
Charles Chan2fde6d42017-08-23 14:46:43 -07001366 }
1367 }
1368
Charles Chan2ff1bac2018-03-29 16:03:41 -07001369 void populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
1370 if (shouldProgram(deviceId)) {
1371 srManager.routingRulePopulator.populateBridging(deviceId, port, mac, vlanId);
1372 }
1373 }
1374
1375 void revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) {
1376 if (shouldProgram(deviceId)) {
1377 srManager.routingRulePopulator.revokeBridging(deviceId, port, mac, vlanId);
1378 }
1379 }
1380
1381 void updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac,
1382 VlanId vlanId, boolean popVlan, boolean install) {
1383 if (shouldProgram(deviceId)) {
1384 srManager.routingRulePopulator.updateBridging(deviceId, portNum, hostMac, vlanId, popVlan, install);
1385 }
1386 }
1387
1388 void updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, MacAddress hostMac,
1389 VlanId vlanId, boolean popVlan, boolean install) {
1390 if (shouldProgram(deviceId)) {
1391 srManager.routingRulePopulator.updateFwdObj(deviceId, portNumber, prefix, hostMac,
1392 vlanId, popVlan, install);
1393 }
1394 }
1395
Charles Chan2fde6d42017-08-23 14:46:43 -07001396 /**
pier7398a5b2020-01-07 15:39:39 +01001397 * Purges seen before routes for a given device.
1398 * @param deviceId the device id
1399 */
1400 void purgeSeenBeforeRoutes(DeviceId deviceId) {
1401 log.debug("Purging seen before routes having as target {}", deviceId);
1402 Set<Entry<DeviceId, DeviceId>> routesToPurge = seenBeforeRoutes.stream()
1403 .filter(entry -> entry.getValue().equals(deviceId))
1404 .collect(Collectors.toSet());
1405 routesToPurge.forEach(entry -> seenBeforeRoutes.remove(entry.getKey(), entry.getValue()));
1406 }
1407
1408 /**
Jonghwan Hyuna76bf032018-04-09 09:40:50 -07001409 * Populates IP rules for a route when the next hop is double-tagged.
1410 *
1411 * @param deviceId device ID that next hop attaches to
1412 * @param prefix IP prefix of the route
1413 * @param hostMac MAC address of the next hop
1414 * @param innerVlan Inner Vlan ID of the next hop
1415 * @param outerVlan Outer Vlan ID of the next hop
1416 * @param outerTpid Outer TPID of the next hop
1417 * @param outPort port that the next hop attaches to
1418 */
1419 void populateDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, VlanId innerVlan,
1420 VlanId outerVlan, EthType outerTpid, PortNumber outPort) {
1421 if (srManager.mastershipService.isLocalMaster(deviceId)) {
1422 VlanId dummyVlan = srManager.allocateDummyVlanId(
1423 new ConnectPoint(deviceId, outPort), prefix.address());
1424 if (!dummyVlan.equals(VlanId.NONE)) {
1425 srManager.routingRulePopulator.populateDoubleTaggedRoute(
1426 deviceId, prefix, hostMac, dummyVlan, innerVlan, outerVlan, outerTpid, outPort);
1427 srManager.routingRulePopulator.processDoubleTaggedFilter(
1428 deviceId, outPort, outerVlan, innerVlan, true);
1429 } else {
1430 log.error("Failed to allocate dummy VLAN ID for host {} at {}/{}",
1431 prefix.address(), deviceId, outPort);
1432 }
1433 }
1434 }
1435
1436 /**
1437 * Revokes IP rules for a route when the next hop is double-tagged.
1438 *
1439 * @param deviceId device ID that next hop attaches to
1440 * @param prefix IP prefix of the route
1441 * @param hostMac MAC address of the next hop
1442 * @param innerVlan Inner Vlan ID of the next hop
1443 * @param outerVlan Outer Vlan ID of the next hop
1444 * @param outerTpid Outer TPID of the next hop
1445 * @param outPort port that the next hop attaches to
1446 */
1447 void revokeDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, VlanId innerVlan,
1448 VlanId outerVlan, EthType outerTpid, PortNumber outPort) {
1449 // Revoke route either if this node have the mastership (when device is available) or
1450 // if this node is the leader (even when device is unavailable)
1451 if (!srManager.mastershipService.isLocalMaster(deviceId)) {
1452 if (srManager.deviceService.isAvailable(deviceId)) {
1453 // Master node will revoke specified rule.
1454 log.debug("This node is not a master for {}, stop revoking route.", deviceId);
1455 return;
1456 }
1457
1458 // isLocalMaster will return false when the device is unavailable.
1459 // Verify if this node is the leader in that case.
1460 NodeId leader = srManager.leadershipService.runForLeadership(
1461 deviceId.toString()).leaderNodeId();
1462 if (!srManager.clusterService.getLocalNode().id().equals(leader)) {
1463 // Leader node will revoke specified rule.
1464 log.debug("This node is not a master for {}, stop revoking route.", deviceId);
1465 return;
1466 }
1467 }
1468
1469 VlanId dummyVlan = srManager.dummyVlanIdStore().get(new DummyVlanIdStoreKey(
1470 new ConnectPoint(deviceId, outPort), prefix.address()));
1471 if (dummyVlan == null) {
1472 log.error("Failed to get dummyVlanId for host {} at {}/{}.",
1473 prefix.address(), deviceId, outPort);
1474 } else {
1475 srManager.routingRulePopulator.revokeDoubleTaggedRoute(
1476 deviceId, prefix, hostMac, dummyVlan, innerVlan, outerVlan, outerTpid, outPort);
1477 srManager.routingRulePopulator.processDoubleTaggedFilter(
1478 deviceId, outPort, outerVlan, innerVlan, false);
1479 }
1480 }
1481
1482
1483 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001484 * Remove ECMP graph entry for the given device. Typically called when
1485 * device is no longer available.
1486 *
1487 * @param deviceId the device for which graphs need to be purged
1488 */
Charles Chana8487b02018-04-18 18:41:05 -07001489 void purgeEcmpGraph(DeviceId deviceId) {
Saurav Dasc568c342018-01-25 09:49:01 -08001490 statusLock.lock();
1491 try {
Saurav Dasc568c342018-01-25 09:49:01 -08001492 if (populationStatus == Status.STARTED) {
1493 log.warn("Previous rule population is not finished. Cannot"
1494 + " proceeed with purgeEcmpGraph for {}", deviceId);
1495 return;
1496 }
1497 log.debug("Updating ECMPspg for unavailable dev:{}", deviceId);
1498 currentEcmpSpgMap.remove(deviceId);
1499 if (updatedEcmpSpgMap != null) {
1500 updatedEcmpSpgMap.remove(deviceId);
1501 }
1502 } finally {
1503 statusLock.unlock();
Saurav Das7bcbe702017-06-13 15:35:54 -07001504 }
1505 }
1506
Saurav Dasc6dc1772018-04-21 17:19:48 -07001507 /**
1508 * Attempts a full reroute of route-paths if topology has changed relatively
1509 * close to a mastership change event. Does not do a reroute if mastership
1510 * change is due to reasons other than a ONOS cluster event - for example a
1511 * call to balance-masters, or a switch up/down event.
1512 *
1513 * @param devId the device identifier for which mastership has changed
1514 * @param me the mastership event
1515 */
1516 void checkFullRerouteForMasterChange(DeviceId devId, MastershipEvent me) {
1517 // give small delay to absorb mastership events that are caused by
1518 // device that has disconnected from cluster
Saurav Das60ca8d52018-04-23 18:42:12 -07001519 executorServiceMstChg.schedule(new MasterChange(devId, me),
1520 MASTER_CHANGE_DELAY, TimeUnit.MILLISECONDS);
Saurav Dasc6dc1772018-04-21 17:19:48 -07001521 }
1522
1523 protected final class MasterChange implements Runnable {
1524 private DeviceId devId;
1525 private MastershipEvent me;
1526 private static final long CLUSTER_EVENT_THRESHOLD = 4500; // ms
1527 private static final long DEVICE_EVENT_THRESHOLD = 2000; // ms
Saurav Das41b49a92018-04-27 18:42:30 -07001528 private static final long EDGE_PORT_EVENT_THRESHOLD = 10000; //ms
Saurav Dasf1027d42018-06-11 17:02:31 -07001529 private static final long FULL_REROUTE_THRESHOLD = 10000; // ms
Saurav Dasc6dc1772018-04-21 17:19:48 -07001530
1531 MasterChange(DeviceId devId, MastershipEvent me) {
1532 this.devId = devId;
1533 this.me = me;
1534 }
1535
1536 @Override
1537 public void run() {
1538 long lce = srManager.clusterListener.timeSinceLastClusterEvent();
1539 boolean clusterEvent = lce < CLUSTER_EVENT_THRESHOLD;
1540
1541 // ignore event for lost switch if cluster event hasn't happened -
1542 // device down event will handle it
1543 if ((me.roleInfo().master() == null
1544 || !srManager.deviceService.isAvailable(devId))
1545 && !clusterEvent) {
1546 log.debug("Full reroute not required for lost device: {}/{} "
1547 + "clusterEvent/timeSince: {}/{}",
1548 devId, me.roleInfo(), clusterEvent, lce);
1549 return;
1550 }
1551
1552 long update = srManager.deviceService.getLastUpdatedInstant(devId);
1553 long lde = Instant.now().toEpochMilli() - update;
1554 boolean deviceEvent = lde < DEVICE_EVENT_THRESHOLD;
1555
1556 // ignore event for recently connected switch if cluster event hasn't
1557 // happened - link up events will handle it
1558 if (srManager.deviceService.isAvailable(devId) && deviceEvent
1559 && !clusterEvent) {
1560 log.debug("Full reroute not required for recently available"
1561 + " device: {}/{} deviceEvent/timeSince: {}/{} "
1562 + "clusterEvent/timeSince: {}/{}",
1563 devId, me.roleInfo(), deviceEvent, lde, clusterEvent, lce);
1564 return;
1565 }
1566
Saurav Das41b49a92018-04-27 18:42:30 -07001567 long lepe = Instant.now().toEpochMilli()
1568 - srManager.lastEdgePortEvent.toEpochMilli();
1569 boolean edgePortEvent = lepe < EDGE_PORT_EVENT_THRESHOLD;
1570
Saurav Dasc6dc1772018-04-21 17:19:48 -07001571 // if it gets here, then mastership change is likely due to onos
1572 // instance failure, or network partition in onos cluster
1573 // normally a mastership change like this does not require re-programming
1574 // but if topology changes happen at the same time then we may miss events
1575 if (!isRoutingStable() && clusterEvent) {
Saurav Das41b49a92018-04-27 18:42:30 -07001576 log.warn("Mastership changed for dev: {}/{} while programming route-paths "
Saurav Dasc6dc1772018-04-21 17:19:48 -07001577 + "due to clusterEvent {} ms ago .. attempting full reroute",
1578 devId, me.roleInfo(), lce);
1579 if (srManager.mastershipService.isLocalMaster(devId)) {
1580 // old master could have died when populating filters
1581 populatePortAddressingRules(devId);
1582 }
Saurav Dasf1027d42018-06-11 17:02:31 -07001583 // old master could have died when creating groups
Saurav Dasc6dc1772018-04-21 17:19:48 -07001584 // XXX right now we have no fine-grained way to only make changes
Saurav Dasf1027d42018-06-11 17:02:31 -07001585 // for the route paths affected by this device. Thus we do a
1586 // full reroute after purging all hash groups. We also try to do
1587 // it only once, irrespective of the number of devices
1588 // that changed mastership when their master instance died.
1589 long lfrr = Instant.now().toEpochMilli() - lastFullReroute.toEpochMilli();
1590 boolean doFullReroute = lfrr > FULL_REROUTE_THRESHOLD;
1591 if (doFullReroute) {
1592 lastFullReroute = Instant.now();
1593 for (Device dev : srManager.deviceService.getDevices()) {
1594 if (shouldProgram(dev.id())) {
1595 srManager.purgeHashedNextObjectiveStore(dev.id());
pier7398a5b2020-01-07 15:39:39 +01001596 seenBeforeRoutes.removeAll(dev.id());
Saurav Dasf1027d42018-06-11 17:02:31 -07001597 }
1598 }
1599 // give small delay to ensure entire store is purged
1600 executorServiceFRR.schedule(new FullRerouteAfterPurge(),
1601 PURGE_DELAY,
1602 TimeUnit.MILLISECONDS);
1603 } else {
1604 log.warn("Full reroute attempted {} ms ago .. skipping", lfrr);
1605 }
Saurav Das41b49a92018-04-27 18:42:30 -07001606
1607 } else if (edgePortEvent && clusterEvent) {
1608 log.warn("Mastership changed for dev: {}/{} due to clusterEvent {} ms ago "
1609 + "while edge-port event happened {} ms ago "
1610 + " .. reprogramming all edge-ports",
1611 devId, me.roleInfo(), lce, lepe);
1612 if (shouldProgram(devId)) {
1613 srManager.deviceService.getPorts(devId).stream()
1614 .filter(p -> srManager.interfaceService
1615 .isConfigured(new ConnectPoint(devId, p.number())))
1616 .forEach(p -> srManager.processPortUpdated(devId, p));
1617 }
1618
Saurav Dasc6dc1772018-04-21 17:19:48 -07001619 } else {
1620 log.debug("Stable route-paths .. full reroute not attempted for "
1621 + "mastership change {}/{} deviceEvent/timeSince: {}/{} "
1622 + "clusterEvent/timeSince: {}/{}", devId, me.roleInfo(),
1623 deviceEvent, lde, clusterEvent, lce);
1624 }
1625 }
1626 }
1627
Saurav Dasf1027d42018-06-11 17:02:31 -07001628 /**
1629 * Performs a full reroute of routing rules in all the switches. Assumes
1630 * caller has purged hash groups from the nextObjective store, otherwise
1631 * re-uses ones available in the store.
1632 */
1633 protected final class FullRerouteAfterPurge implements Runnable {
1634 @Override
1635 public void run() {
1636 populateAllRoutingRules();
1637 }
1638 }
1639
1640
Saurav Das7bcbe702017-06-13 15:35:54 -07001641 //////////////////////////////////////
1642 // Routing helper methods and classes
1643 //////////////////////////////////////
1644
1645 /**
Saurav Dasf1027d42018-06-11 17:02:31 -07001646 * Computes set of affected routes due to failed link. Assumes previous ecmp
1647 * shortest-path graph exists for a switch in order to compute affected
1648 * routes. If such a graph does not exist, the method returns null.
Saurav Dasb5c236e2016-06-07 10:08:06 -07001649 *
1650 * @param linkFail the failed link
1651 * @return the set of affected routes which may be empty if no routes were
Saurav Dasf1027d42018-06-11 17:02:31 -07001652 * affected
Saurav Dasb5c236e2016-06-07 10:08:06 -07001653 */
sangho20eff1d2015-04-13 15:15:58 -07001654 private Set<ArrayList<DeviceId>> computeDamagedRoutes(Link linkFail) {
sangho20eff1d2015-04-13 15:15:58 -07001655 Set<ArrayList<DeviceId>> routes = new HashSet<>();
1656
1657 for (Device sw : srManager.deviceService.getDevices()) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001658 log.debug("Computing the impacted routes for device {} due to link fail",
1659 sw.id());
Charles Chan2ff1bac2018-03-29 16:03:41 -07001660 if (!shouldProgram(sw.id())) {
Saurav Dasc6dc1772018-04-21 17:19:48 -07001661 lastProgrammed.remove(sw.id());
sangho20eff1d2015-04-13 15:15:58 -07001662 continue;
1663 }
Charles Chan2ff1bac2018-03-29 16:03:41 -07001664 for (DeviceId rootSw : deviceAndItsPair(sw.id())) {
Saurav Dasc6dc1772018-04-21 17:19:48 -07001665 // check for mastership change since last run
1666 if (!lastProgrammed.contains(sw.id())) {
Saurav Dasf1027d42018-06-11 17:02:31 -07001667 log.warn("New responsibility for this node to program dev:{}"
Saurav Dasc6dc1772018-04-21 17:19:48 -07001668 + " ... nuking current ECMPspg", sw.id());
1669 currentEcmpSpgMap.remove(sw.id());
1670 }
Saurav Dasf1027d42018-06-11 17:02:31 -07001671 lastProgrammed.add(sw.id());
1672
Saurav Das9df5b7c2017-08-14 16:44:43 -07001673 EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(rootSw);
1674 if (ecmpSpg == null) {
Saurav Dasf1027d42018-06-11 17:02:31 -07001675 log.warn("No existing ECMP graph for switch {}. Assuming "
1676 + "all route-paths have changed towards it.", rootSw);
1677 for (DeviceId targetSw : srManager.deviceConfiguration.getRouters()) {
1678 if (targetSw.equals(rootSw)) {
1679 continue;
1680 }
1681 routes.add(Lists.newArrayList(targetSw, rootSw));
1682 log.debug("Impacted route:{}->{}", targetSw, rootSw);
1683 }
1684 continue;
Saurav Das9df5b7c2017-08-14 16:44:43 -07001685 }
Saurav Dasf1027d42018-06-11 17:02:31 -07001686
Saurav Das9df5b7c2017-08-14 16:44:43 -07001687 if (log.isDebugEnabled()) {
1688 log.debug("Root switch: {}", rootSw);
1689 log.debug(" Current/Existing SPG: {}", ecmpSpg);
1690 log.debug(" New/Updated SPG: {}", updatedEcmpSpgMap.get(rootSw));
1691 }
1692 HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>>
1693 switchVia = ecmpSpg.getAllLearnedSwitchesAndVia();
1694 // figure out if the broken link affected any route-paths in this graph
1695 for (Integer itrIdx : switchVia.keySet()) {
1696 log.trace("Current/Exiting SPG Iterindex# {}", itrIdx);
1697 HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap =
1698 switchVia.get(itrIdx);
1699 for (DeviceId targetSw : swViaMap.keySet()) {
1700 log.trace("TargetSwitch {} --> RootSwitch {}",
1701 targetSw, rootSw);
Saurav Dasb5c236e2016-06-07 10:08:06 -07001702 for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) {
1703 log.trace(" Via:");
Pier Ventree0ae7a32016-11-23 09:57:42 -08001704 via.forEach(e -> log.trace(" {}", e));
Saurav Dasb5c236e2016-06-07 10:08:06 -07001705 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001706 Set<ArrayList<DeviceId>> subLinks =
1707 computeLinks(targetSw, rootSw, swViaMap);
1708 for (ArrayList<DeviceId> alink: subLinks) {
1709 if ((alink.get(0).equals(linkFail.src().deviceId()) &&
1710 alink.get(1).equals(linkFail.dst().deviceId()))
1711 ||
1712 (alink.get(0).equals(linkFail.dst().deviceId()) &&
1713 alink.get(1).equals(linkFail.src().deviceId()))) {
1714 log.debug("Impacted route:{}->{}", targetSw, rootSw);
1715 ArrayList<DeviceId> aRoute = new ArrayList<>();
1716 aRoute.add(targetSw); // switch with rules to populate
1717 aRoute.add(rootSw); // towards this destination
1718 routes.add(aRoute);
1719 break;
1720 }
sangho20eff1d2015-04-13 15:15:58 -07001721 }
1722 }
1723 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001724
sangho20eff1d2015-04-13 15:15:58 -07001725 }
sangho45b009c2015-05-07 13:30:57 -07001726
sangho20eff1d2015-04-13 15:15:58 -07001727 }
sangho20eff1d2015-04-13 15:15:58 -07001728 return routes;
1729 }
1730
Saurav Das4e3224f2016-11-29 14:27:25 -08001731 /**
1732 * Computes set of affected routes due to new links or failed switches.
1733 *
Saurav Das604ab3a2018-03-18 21:28:15 -07001734 * @param failedSwitch deviceId of failed switch if any
Saurav Das4e3224f2016-11-29 14:27:25 -08001735 * @return the set of affected routes which may be empty if no routes were
1736 * affected
1737 */
Saurav Dase0d4c872018-03-05 14:37:16 -08001738 private Set<ArrayList<DeviceId>> computeRouteChange(DeviceId failedSwitch) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001739 ImmutableSet.Builder<ArrayList<DeviceId>> changedRtBldr =
Saurav Das4e3224f2016-11-29 14:27:25 -08001740 ImmutableSet.builder();
sangho20eff1d2015-04-13 15:15:58 -07001741
1742 for (Device sw : srManager.deviceService.getDevices()) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001743 log.debug("Computing the impacted routes for device {}", sw.id());
Charles Chan2ff1bac2018-03-29 16:03:41 -07001744 if (!shouldProgram(sw.id())) {
Saurav Dasc6dc1772018-04-21 17:19:48 -07001745 lastProgrammed.remove(sw.id());
sangho20eff1d2015-04-13 15:15:58 -07001746 continue;
1747 }
Charles Chan2ff1bac2018-03-29 16:03:41 -07001748 for (DeviceId rootSw : deviceAndItsPair(sw.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001749 if (log.isTraceEnabled()) {
1750 log.trace("Device links for dev: {}", rootSw);
1751 for (Link link: srManager.linkService.getDeviceLinks(rootSw)) {
1752 log.trace("{} -> {} ", link.src().deviceId(),
1753 link.dst().deviceId());
1754 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001755 }
Saurav Dasc6dc1772018-04-21 17:19:48 -07001756 // check for mastership change since last run
1757 if (!lastProgrammed.contains(sw.id())) {
Saurav Dasf1027d42018-06-11 17:02:31 -07001758 log.warn("New responsibility for this node to program dev:{}"
Saurav Dasc6dc1772018-04-21 17:19:48 -07001759 + " ... nuking current ECMPspg", sw.id());
1760 currentEcmpSpgMap.remove(sw.id());
1761 }
Saurav Dasf1027d42018-06-11 17:02:31 -07001762 lastProgrammed.add(sw.id());
Saurav Das7bcbe702017-06-13 15:35:54 -07001763 EcmpShortestPathGraph currEcmpSpg = currentEcmpSpgMap.get(rootSw);
1764 if (currEcmpSpg == null) {
1765 log.debug("No existing ECMP graph for device {}.. adding self as "
1766 + "changed route", rootSw);
1767 changedRtBldr.add(Lists.newArrayList(rootSw));
1768 continue;
1769 }
1770 EcmpShortestPathGraph newEcmpSpg = updatedEcmpSpgMap.get(rootSw);
Saurav Das5a356042018-04-06 20:16:01 -07001771 if (newEcmpSpg == null) {
1772 log.warn("Cannot find updated ECMP graph for dev:{}", rootSw);
1773 continue;
1774 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001775 if (log.isDebugEnabled()) {
1776 log.debug("Root switch: {}", rootSw);
1777 log.debug(" Current/Existing SPG: {}", currEcmpSpg);
1778 log.debug(" New/Updated SPG: {}", newEcmpSpg);
1779 }
1780 // first use the updated/new map to compare to current/existing map
1781 // as new links may have come up
1782 changedRtBldr.addAll(compareGraphs(newEcmpSpg, currEcmpSpg, rootSw));
1783 // then use the current/existing map to compare to updated/new map
1784 // as switch may have been removed
1785 changedRtBldr.addAll(compareGraphs(currEcmpSpg, newEcmpSpg, rootSw));
sangho45b009c2015-05-07 13:30:57 -07001786 }
Saurav Das4e3224f2016-11-29 14:27:25 -08001787 }
sangho20eff1d2015-04-13 15:15:58 -07001788
Saurav Dase0d4c872018-03-05 14:37:16 -08001789 // handle clearing state for a failed switch in case the switch does
1790 // not have a pair, or the pair is not available
1791 if (failedSwitch != null) {
Charles Chanba6c5752018-04-02 11:46:38 -07001792 Optional<DeviceId> pairDev = srManager.getPairDeviceId(failedSwitch);
1793 if (!pairDev.isPresent() || !srManager.deviceService.isAvailable(pairDev.get())) {
Saurav Dase0d4c872018-03-05 14:37:16 -08001794 log.debug("Proxy Route changes to downed Sw:{}", failedSwitch);
1795 srManager.deviceService.getDevices().forEach(dev -> {
1796 if (!dev.id().equals(failedSwitch) &&
1797 srManager.mastershipService.isLocalMaster(dev.id())) {
1798 log.debug(" : {}", dev.id());
1799 changedRtBldr.add(Lists.newArrayList(dev.id(), failedSwitch));
1800 }
1801 });
1802 }
1803 }
1804
Saurav Das7bcbe702017-06-13 15:35:54 -07001805 Set<ArrayList<DeviceId>> changedRoutes = changedRtBldr.build();
Saurav Das4e3224f2016-11-29 14:27:25 -08001806 for (ArrayList<DeviceId> route: changedRoutes) {
1807 log.debug("Route changes Target -> Root");
1808 if (route.size() == 1) {
1809 log.debug(" : all -> {}", route.get(0));
1810 } else {
1811 log.debug(" : {} -> {}", route.get(0), route.get(1));
1812 }
1813 }
1814 return changedRoutes;
1815 }
1816
pier1c2ca732019-04-25 18:51:51 +02001817 // Utility method to expands the route changes in two elements array using
1818 // the ECMP graph. Caller represents all to dst switch routes with an
1819 // array containing only the dst switch.
1820 private Set<ArrayList<DeviceId>> getExpandedRoutes(Set<ArrayList<DeviceId>> routeChanges) {
1821 Set<ArrayList<DeviceId>> changedRoutes = new HashSet<>();
1822 // Ensure each routeChanges entry has two elements
1823 for (ArrayList<DeviceId> route : routeChanges) {
1824 if (route.size() == 1) {
1825 DeviceId dstSw = route.get(0);
1826 EcmpShortestPathGraph ec = updatedEcmpSpgMap.get(dstSw);
1827 if (ec == null) {
1828 log.warn("No graph found for {} .. aborting redoRouting", dstSw);
1829 return Collections.emptySet();
1830 }
1831 ec.getAllLearnedSwitchesAndVia().keySet().forEach(key -> {
1832 ec.getAllLearnedSwitchesAndVia().get(key).keySet().forEach(target -> {
1833 changedRoutes.add(Lists.newArrayList(target, dstSw));
1834 });
1835 });
1836 } else {
1837 DeviceId targetSw = route.get(0);
1838 DeviceId dstSw = route.get(1);
1839 changedRoutes.add(Lists.newArrayList(targetSw, dstSw));
1840 }
1841 }
1842 return changedRoutes;
1843 }
1844
1845 // Utility method to expands the route changes in two elements array using
1846 // the available devices. Caller represents all to dst switch routes with an
1847 // array containing only the dst switch.
1848 private Set<ArrayList<DeviceId>> getAllExpandedRoutes(Set<ArrayList<DeviceId>> routeChanges) {
1849 Set<ArrayList<DeviceId>> changedRoutes = new HashSet<>();
1850 // Ensure each routeChanges entry has two elements
1851 for (ArrayList<DeviceId> route : routeChanges) {
1852 if (route.size() == 1) {
1853 // route-path changes are from everyone else to this switch
1854 DeviceId dstSw = route.get(0);
1855 srManager.deviceService.getAvailableDevices().forEach(sw -> {
1856 if (!sw.id().equals(dstSw)) {
1857 changedRoutes.add(Lists.newArrayList(sw.id(), dstSw));
1858 }
1859 });
1860 } else {
1861 changedRoutes.add(route);
1862 }
1863 }
1864 return changedRoutes;
1865 }
1866
Saurav Das4e3224f2016-11-29 14:27:25 -08001867 /**
1868 * For the root switch, searches all the target nodes reachable in the base
1869 * graph, and compares paths to the ones in the comp graph.
1870 *
1871 * @param base the graph that is indexed for all reachable target nodes
1872 * from the root node
1873 * @param comp the graph that the base graph is compared to
1874 * @param rootSw both ecmp graphs are calculated for the root node
1875 * @return all the routes that have changed in the base graph
1876 */
1877 private Set<ArrayList<DeviceId>> compareGraphs(EcmpShortestPathGraph base,
1878 EcmpShortestPathGraph comp,
1879 DeviceId rootSw) {
1880 ImmutableSet.Builder<ArrayList<DeviceId>> changedRoutesBuilder =
1881 ImmutableSet.builder();
1882 HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> baseMap =
1883 base.getAllLearnedSwitchesAndVia();
1884 HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> compMap =
1885 comp.getAllLearnedSwitchesAndVia();
1886 for (Integer itrIdx : baseMap.keySet()) {
1887 HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> baseViaMap =
1888 baseMap.get(itrIdx);
1889 for (DeviceId targetSw : baseViaMap.keySet()) {
1890 ArrayList<ArrayList<DeviceId>> basePath = baseViaMap.get(targetSw);
1891 ArrayList<ArrayList<DeviceId>> compPath = getVia(compMap, targetSw);
1892 if ((compPath == null) || !basePath.equals(compPath)) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001893 log.trace("Impacted route:{} -> {}", targetSw, rootSw);
Saurav Das4e3224f2016-11-29 14:27:25 -08001894 ArrayList<DeviceId> route = new ArrayList<>();
Saurav Das7bcbe702017-06-13 15:35:54 -07001895 route.add(targetSw); // switch with rules to populate
1896 route.add(rootSw); // towards this destination
Saurav Das4e3224f2016-11-29 14:27:25 -08001897 changedRoutesBuilder.add(route);
sangho20eff1d2015-04-13 15:15:58 -07001898 }
1899 }
sangho45b009c2015-05-07 13:30:57 -07001900 }
Saurav Das4e3224f2016-11-29 14:27:25 -08001901 return changedRoutesBuilder.build();
sangho20eff1d2015-04-13 15:15:58 -07001902 }
1903
Saurav Das7bcbe702017-06-13 15:35:54 -07001904 /**
1905 * Returns the ECMP paths traversed to reach the target switch.
1906 *
1907 * @param switchVia a per-iteration view of the ECMP graph for a root switch
1908 * @param targetSw the switch to reach from the root switch
1909 * @return the nodes traversed on ECMP paths to the target switch
1910 */
sangho20eff1d2015-04-13 15:15:58 -07001911 private ArrayList<ArrayList<DeviceId>> getVia(HashMap<Integer, HashMap<DeviceId,
Saurav Das4e3224f2016-11-29 14:27:25 -08001912 ArrayList<ArrayList<DeviceId>>>> switchVia, DeviceId targetSw) {
sangho20eff1d2015-04-13 15:15:58 -07001913 for (Integer itrIdx : switchVia.keySet()) {
1914 HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap =
1915 switchVia.get(itrIdx);
Saurav Das4e3224f2016-11-29 14:27:25 -08001916 if (swViaMap.get(targetSw) == null) {
sangho20eff1d2015-04-13 15:15:58 -07001917 continue;
1918 } else {
Saurav Das4e3224f2016-11-29 14:27:25 -08001919 return swViaMap.get(targetSw);
sangho20eff1d2015-04-13 15:15:58 -07001920 }
1921 }
1922
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001923 return null;
sangho20eff1d2015-04-13 15:15:58 -07001924 }
1925
Saurav Das7bcbe702017-06-13 15:35:54 -07001926 /**
1927 * Utility method to break down a path from src to dst device into a collection
1928 * of links.
1929 *
1930 * @param src src device of the path
1931 * @param dst dst device of the path
1932 * @param viaMap path taken from src to dst device
1933 * @return collection of links in the path
1934 */
sangho20eff1d2015-04-13 15:15:58 -07001935 private Set<ArrayList<DeviceId>> computeLinks(DeviceId src,
1936 DeviceId dst,
1937 HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> viaMap) {
1938 Set<ArrayList<DeviceId>> subLinks = Sets.newHashSet();
1939 for (ArrayList<DeviceId> via : viaMap.get(src)) {
1940 DeviceId linkSrc = src;
1941 DeviceId linkDst = dst;
1942 for (DeviceId viaDevice: via) {
1943 ArrayList<DeviceId> link = new ArrayList<>();
1944 linkDst = viaDevice;
1945 link.add(linkSrc);
1946 link.add(linkDst);
1947 subLinks.add(link);
1948 linkSrc = viaDevice;
1949 }
1950 ArrayList<DeviceId> link = new ArrayList<>();
1951 link.add(linkSrc);
1952 link.add(dst);
1953 subLinks.add(link);
1954 }
1955
1956 return subLinks;
1957 }
1958
Charles Chan93e71ba2016-04-29 14:38:22 -07001959 /**
Charles Chan2ff1bac2018-03-29 16:03:41 -07001960 * Determines whether this controller instance should program the
Saurav Das7bcbe702017-06-13 15:35:54 -07001961 * given {@code deviceId}, based on mastership and pairDeviceId if one exists.
Charles Chan2ff1bac2018-03-29 16:03:41 -07001962 * <p>
1963 * Once an instance is elected, it will be the only instance responsible for programming
1964 * both devices in the pair until it goes down.
Charles Chan93e71ba2016-04-29 14:38:22 -07001965 *
Saurav Das7bcbe702017-06-13 15:35:54 -07001966 * @param deviceId device identifier to consider for routing
Charles Chan2ff1bac2018-03-29 16:03:41 -07001967 * @return true if current instance should handle the routing for given device
Charles Chan93e71ba2016-04-29 14:38:22 -07001968 */
Charles Chan2ff1bac2018-03-29 16:03:41 -07001969 boolean shouldProgram(DeviceId deviceId) {
Charles Chana8487b02018-04-18 18:41:05 -07001970 Boolean cached = shouldProgramCache.get(deviceId);
1971 if (cached != null) {
Saurav Dasc6dc1772018-04-21 17:19:48 -07001972 log.debug("shouldProgram dev:{} cached:{}", deviceId, cached);
Charles Chana8487b02018-04-18 18:41:05 -07001973 return cached;
1974 }
1975
Charles Chan2ff1bac2018-03-29 16:03:41 -07001976 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001977
Charles Chan2ff1bac2018-03-29 16:03:41 -07001978 NodeId currentNodeId = srManager.clusterService.getLocalNode().id();
1979 NodeId masterNodeId = srManager.mastershipService.getMasterFor(deviceId);
1980 Optional<NodeId> pairMasterNodeId = pairDeviceId.map(srManager.mastershipService::getMasterFor);
Saurav Dasf1027d42018-06-11 17:02:31 -07001981 log.debug("Evaluate shouldProgram {}/pair={}. currentNodeId={}, master={}, pairMaster={}",
Charles Chan2ff1bac2018-03-29 16:03:41 -07001982 deviceId, pairDeviceId, currentNodeId, masterNodeId, pairMasterNodeId);
1983
1984 // No pair device configured. Only handle when current instance is the master of the device
1985 if (!pairDeviceId.isPresent()) {
Saurav Dasf1027d42018-06-11 17:02:31 -07001986 log.debug("No pair device. currentNodeId={}, master={}", currentNodeId, masterNodeId);
Charles Chan2ff1bac2018-03-29 16:03:41 -07001987 return currentNodeId.equals(masterNodeId);
sanghob35a6192015-04-01 13:05:26 -07001988 }
Charles Chan2ff1bac2018-03-29 16:03:41 -07001989
1990 // Should not handle if current instance is not the master of either switch
1991 if (!currentNodeId.equals(masterNodeId) &&
1992 !(pairMasterNodeId.isPresent() && currentNodeId.equals(pairMasterNodeId.get()))) {
Saurav Dasf1027d42018-06-11 17:02:31 -07001993 log.debug("Current nodeId {} is neither the master of target device {} nor pair device {}",
Charles Chan2ff1bac2018-03-29 16:03:41 -07001994 currentNodeId, deviceId, pairDeviceId);
1995 return false;
1996 }
1997
1998 Set<DeviceId> key = Sets.newHashSet(deviceId, pairDeviceId.get());
1999
2000 NodeId king = shouldProgram.compute(key, ((k, v) -> {
2001 if (v == null) {
2002 // There is no value in the map. Elect a node
2003 return elect(Lists.newArrayList(masterNodeId, pairMasterNodeId.orElse(null)));
2004 } else {
2005 if (v.equals(masterNodeId) || v.equals(pairMasterNodeId.orElse(null))) {
2006 // Use the node in the map if it is still alive and is a master of any of the two switches
2007 return v;
2008 } else {
2009 // Previously elected node is no longer the master of either switch. Re-elect a node.
2010 return elect(Lists.newArrayList(masterNodeId, pairMasterNodeId.orElse(null)));
2011 }
2012 }
2013 }));
2014
2015 if (king != null) {
Saurav Dasf1027d42018-06-11 17:02:31 -07002016 log.debug("{} is king, should handle routing for {}/pair={}", king, deviceId, pairDeviceId);
Charles Chana8487b02018-04-18 18:41:05 -07002017 shouldProgramCache.put(deviceId, king.equals(currentNodeId));
Charles Chan2ff1bac2018-03-29 16:03:41 -07002018 return king.equals(currentNodeId);
2019 } else {
2020 log.error("Fail to elect a king for {}/pair={}. Abort.", deviceId, pairDeviceId);
Charles Chana8487b02018-04-18 18:41:05 -07002021 shouldProgramCache.remove(deviceId);
Charles Chan2ff1bac2018-03-29 16:03:41 -07002022 return false;
2023 }
2024 }
2025
2026 /**
2027 * Elects a node who should take responsibility of programming devices.
2028 * @param nodeIds list of candidate node ID
2029 *
2030 * @return NodeId of the node that gets elected, or null if none of the node can be elected
2031 */
2032 private NodeId elect(List<NodeId> nodeIds) {
2033 // Remove all null elements. This could happen when some device has no master
2034 nodeIds.removeAll(Collections.singleton(null));
2035 nodeIds.sort(null);
2036 return nodeIds.size() == 0 ? null : nodeIds.get(0);
2037 }
2038
Charles Chana8487b02018-04-18 18:41:05 -07002039 void invalidateShouldProgramCache(DeviceId deviceId) {
2040 shouldProgramCache.remove(deviceId);
2041 }
2042
Charles Chan2ff1bac2018-03-29 16:03:41 -07002043 /**
2044 * Returns a set of device ID, containing given device and its pair device if exist.
2045 *
2046 * @param deviceId Device ID
2047 * @return a set of device ID, containing given device and its pair device if exist.
2048 */
2049 private Set<DeviceId> deviceAndItsPair(DeviceId deviceId) {
2050 Set<DeviceId> ret = Sets.newHashSet(deviceId);
2051 srManager.getPairDeviceId(deviceId).ifPresent(ret::add);
2052 return ret;
sanghob35a6192015-04-01 13:05:26 -07002053 }
2054
Charles Chan93e71ba2016-04-29 14:38:22 -07002055 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07002056 * Returns the set of deviceIds which are the next hops from the targetSw
2057 * to the dstSw according to the latest ECMP spg.
2058 *
2059 * @param targetSw the switch for which the next-hops are desired
2060 * @param dstSw the switch to which the next-hops lead to from the targetSw
2061 * @return set of next hop deviceIds, could be empty if no next hops are found
2062 */
2063 private Set<DeviceId> getNextHops(DeviceId targetSw, DeviceId dstSw) {
2064 boolean targetIsEdge = false;
2065 try {
2066 targetIsEdge = srManager.deviceConfiguration.isEdgeDevice(targetSw);
2067 } catch (DeviceConfigNotFoundException e) {
2068 log.warn(e.getMessage() + "Cannot determine if targetIsEdge {}.. "
2069 + "continuing to getNextHops", targetSw);
2070 }
2071
2072 EcmpShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(dstSw);
2073 if (ecmpSpg == null) {
2074 log.debug("No ecmpSpg found for dstSw: {}", dstSw);
2075 return ImmutableSet.of();
2076 }
2077 HashMap<Integer,
2078 HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia =
2079 ecmpSpg.getAllLearnedSwitchesAndVia();
2080 for (Integer itrIdx : switchVia.keySet()) {
2081 HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap =
2082 switchVia.get(itrIdx);
2083 for (DeviceId target : swViaMap.keySet()) {
2084 if (!target.equals(targetSw)) {
2085 continue;
2086 }
Saurav Das60ca8d52018-04-23 18:42:12 -07002087 // optimization for spines to not use leaves to get
2088 // to a spine or other leaves. Also leaves should not use other
2089 // leaves to get to the destination
2090 if ((!targetIsEdge && itrIdx > 1) || targetIsEdge) {
Saurav Dasa4020382018-02-14 14:14:54 -08002091 boolean pathdevIsEdge = false;
2092 for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) {
Saurav Das60ca8d52018-04-23 18:42:12 -07002093 log.debug("Evaluating next-hop in path: {}", via);
Saurav Dasa4020382018-02-14 14:14:54 -08002094 for (DeviceId pathdev : via) {
2095 try {
2096 pathdevIsEdge = srManager.deviceConfiguration
2097 .isEdgeDevice(pathdev);
2098 } catch (DeviceConfigNotFoundException e) {
2099 log.warn(e.getMessage());
2100 }
2101 if (pathdevIsEdge) {
Saurav Dasf1027d42018-06-11 17:02:31 -07002102 log.debug("Avoiding {} hop path for targetSw:{}"
Saurav Dasa4020382018-02-14 14:14:54 -08002103 + " --> dstSw:{} which goes through an edge"
2104 + " device {} in path {}", itrIdx,
2105 targetSw, dstSw, pathdev, via);
2106 return ImmutableSet.of();
2107 }
2108 }
2109 }
Saurav Das7bcbe702017-06-13 15:35:54 -07002110 }
2111 Set<DeviceId> nextHops = new HashSet<>();
2112 for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) {
2113 if (via.isEmpty()) {
2114 // the dstSw is the next-hop from the targetSw
2115 nextHops.add(dstSw);
2116 } else {
2117 // first elem is next-hop in each ECMP path
2118 nextHops.add(via.get(0));
2119 }
2120 }
Saurav Das60ca8d52018-04-23 18:42:12 -07002121 log.debug("target {} --> dst: {} has next-hops:{}", targetSw,
2122 dstSw, nextHops);
Saurav Das7bcbe702017-06-13 15:35:54 -07002123 return nextHops;
2124 }
2125 }
Saurav Das60ca8d52018-04-23 18:42:12 -07002126 log.debug("No next hops found for target:{} --> dst: {}", targetSw, dstSw);
Saurav Das7bcbe702017-06-13 15:35:54 -07002127 return ImmutableSet.of(); //no next-hops found
2128 }
2129
Saurav Das7bcbe702017-06-13 15:35:54 -07002130 //////////////////////////////////////
2131 // Filtering rule creation
2132 //////////////////////////////////////
2133
2134 /**
Saurav Das018605f2017-02-18 14:05:44 -08002135 * Populates filtering rules for port, and punting rules
2136 * for gateway IPs, loopback IPs and arp/ndp traffic.
2137 * Should only be called by the master instance for this device/port.
sanghob35a6192015-04-01 13:05:26 -07002138 *
2139 * @param deviceId Switch ID to set the rules
2140 */
Charles Chana8487b02018-04-18 18:41:05 -07002141 void populatePortAddressingRules(DeviceId deviceId) {
Saurav Das59232cf2016-04-27 18:35:50 -07002142 // Although device is added, sometimes device store does not have the
2143 // ports for this device yet. It results in missing filtering rules in the
2144 // switch. We will attempt it a few times. If it still does not work,
2145 // user can manually repopulate using CLI command sr-reroute-network
Charles Chanf6ec1532017-02-08 16:10:40 -08002146 PortFilterInfo firstRun = rulePopulator.populateVlanMacFilters(deviceId);
Saurav Dasd2fded02016-12-02 15:43:47 -08002147 if (firstRun == null) {
2148 firstRun = new PortFilterInfo(0, 0, 0);
Saurav Das59232cf2016-04-27 18:35:50 -07002149 }
Saurav Dasd2fded02016-12-02 15:43:47 -08002150 executorService.schedule(new RetryFilters(deviceId, firstRun),
2151 RETRY_INTERVAL_MS, TimeUnit.MILLISECONDS);
sanghob35a6192015-04-01 13:05:26 -07002152 }
2153
2154 /**
Saurav Dasd2fded02016-12-02 15:43:47 -08002155 * RetryFilters populates filtering objectives for a device and keeps retrying
2156 * till the number of ports filtered are constant for a predefined number
2157 * of attempts.
2158 */
2159 protected final class RetryFilters implements Runnable {
2160 int constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS;
2161 DeviceId devId;
2162 int counter;
2163 PortFilterInfo prevRun;
2164
2165 private RetryFilters(DeviceId deviceId, PortFilterInfo previousRun) {
Saurav Das59232cf2016-04-27 18:35:50 -07002166 devId = deviceId;
Saurav Dasd2fded02016-12-02 15:43:47 -08002167 prevRun = previousRun;
2168 counter = 0;
Saurav Das59232cf2016-04-27 18:35:50 -07002169 }
2170
2171 @Override
2172 public void run() {
Charles Chan7f9737b2017-06-22 14:27:17 -07002173 log.debug("RETRY FILTER ATTEMPT {} ** dev:{}", ++counter, devId);
Charles Chanf6ec1532017-02-08 16:10:40 -08002174 PortFilterInfo thisRun = rulePopulator.populateVlanMacFilters(devId);
Saurav Dasd2fded02016-12-02 15:43:47 -08002175 boolean sameResult = prevRun.equals(thisRun);
2176 log.debug("dev:{} prevRun:{} thisRun:{} sameResult:{}", devId, prevRun,
2177 thisRun, sameResult);
Ray Milkeyc6c9b172018-02-26 09:36:31 -08002178 if (thisRun == null || !sameResult || (--constantAttempts > 0)) {
Saurav Das018605f2017-02-18 14:05:44 -08002179 // exponentially increasing intervals for retries
2180 executorService.schedule(this,
2181 RETRY_INTERVAL_MS * (int) Math.pow(counter, RETRY_INTERVAL_SCALE),
2182 TimeUnit.MILLISECONDS);
Saurav Dasd2fded02016-12-02 15:43:47 -08002183 if (!sameResult) {
2184 constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; //reset
2185 }
Saurav Das59232cf2016-04-27 18:35:50 -07002186 }
Saurav Dasd2fded02016-12-02 15:43:47 -08002187 prevRun = (thisRun == null) ? prevRun : thisRun;
Saurav Das59232cf2016-04-27 18:35:50 -07002188 }
Saurav Das59232cf2016-04-27 18:35:50 -07002189 }
pier8b4ba992019-04-24 16:12:47 +02002190
2191 // Check jobs completion. It returns false if one of the job fails
2192 // and cancel the remaining
2193 private boolean checkJobs(List<Future<Boolean>> futures) {
2194 boolean completed = true;
2195 for (Future<Boolean> future : futures) {
2196 try {
2197 if (completed) {
2198 if (!future.get()) {
2199 completed = false;
2200 }
2201 } else {
2202 future.cancel(true);
2203 }
2204 } catch (InterruptedException | ExecutionException e) {
2205 completed = false;
2206 }
2207 }
2208 return completed;
2209 }
sanghob35a6192015-04-01 13:05:26 -07002210}