sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.segmentrouting; |
| 17 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableMap.Builder; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.Sets; |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 24 | |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 25 | import org.onlab.packet.EthType; |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame] | 26 | import com.google.common.collect.Streams; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ip6Address; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 29 | import org.onlab.packet.IpPrefix; |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 32 | import org.onlab.util.PredictableExecutor; |
| 33 | import org.onlab.util.PredictableExecutor.PickyCallable; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 34 | import org.onosproject.cluster.NodeId; |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 35 | import org.onosproject.mastership.MastershipEvent; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.net.DeviceId; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 39 | import org.onosproject.net.Link; |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 40 | import org.onosproject.net.PortNumber; |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.Objective; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 42 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 43 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 44 | import org.onosproject.store.serializers.KryoNamespaces; |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 45 | import org.onosproject.store.service.ConsistentMultimap; |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 46 | import org.onosproject.store.service.Serializer; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
| 49 | |
Yuta HIGUCHI | c9d9347 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 50 | import java.time.Instant; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 51 | import java.util.ArrayList; |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 52 | import java.util.Collections; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 53 | import java.util.HashMap; |
| 54 | import java.util.HashSet; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 55 | import java.util.Iterator; |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 56 | import java.util.List; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 57 | import java.util.Map; |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 58 | import java.util.Map.Entry; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 59 | import java.util.Objects; |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 60 | import java.util.Optional; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 61 | import java.util.Set; |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 62 | import java.util.concurrent.CompletableFuture; |
| 63 | import java.util.concurrent.ExecutionException; |
| 64 | import java.util.concurrent.ExecutorService; |
| 65 | import java.util.concurrent.Future; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 66 | import java.util.concurrent.ScheduledExecutorService; |
| 67 | import java.util.concurrent.TimeUnit; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 68 | import java.util.concurrent.locks.Lock; |
| 69 | import java.util.concurrent.locks.ReentrantLock; |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame] | 70 | import java.util.stream.Collectors; |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 71 | import java.util.stream.Stream; |
| 72 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 73 | import static com.google.common.base.Preconditions.checkNotNull; |
| 74 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 75 | import static org.onlab.util.Tools.groupedThreads; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 76 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Default routing handler that is responsible for route computing and |
| 79 | * routing rule population. |
| 80 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 81 | public class DefaultRoutingHandler { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 82 | private static final int MAX_CONSTANT_RETRY_ATTEMPTS = 5; |
Ray Milkey | 092e9e2 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 83 | private static final long RETRY_INTERVAL_MS = 250L; |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 84 | private static final int RETRY_INTERVAL_SCALE = 1; |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 85 | private static final long STABLITY_THRESHOLD = 10; //secs |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 86 | private static final long MASTER_CHANGE_DELAY = 1000; // ms |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 87 | private static final long PURGE_DELAY = 1000; // ms |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 88 | private static Logger log = LoggerFactory.getLogger(DefaultRoutingHandler.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 89 | |
| 90 | private SegmentRoutingManager srManager; |
| 91 | private RoutingRulePopulator rulePopulator; |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 92 | private HashMap<DeviceId, EcmpShortestPathGraph> currentEcmpSpgMap; |
| 93 | private HashMap<DeviceId, EcmpShortestPathGraph> updatedEcmpSpgMap; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 94 | private DeviceConfiguration config; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 95 | private final Lock statusLock = new ReentrantLock(); |
| 96 | private volatile Status populationStatus; |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 97 | private ScheduledExecutorService executorService |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 98 | = newScheduledThreadPool(1, groupedThreads("retryftr", "retry-%d", log)); |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 99 | private ScheduledExecutorService executorServiceMstChg |
| 100 | = newScheduledThreadPool(1, groupedThreads("masterChg", "mstch-%d", log)); |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 101 | private ScheduledExecutorService executorServiceFRR |
| 102 | = newScheduledThreadPool(1, groupedThreads("fullRR", "fullRR-%d", log)); |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 103 | // Route populators - 0 will leverage available processors |
| 104 | private static final int DEFAULT_THREADS = 0; |
| 105 | private ExecutorService routePopulators; |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 106 | |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 107 | private Instant lastRoutingChange = Instant.EPOCH; |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 108 | private Instant lastFullReroute = Instant.EPOCH; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 109 | |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 110 | // Distributed store to keep track of ONOS instance that should program the |
| 111 | // device pair. There should be only one instance (the king) that programs the same pair. |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 112 | Map<Set<DeviceId>, NodeId> shouldProgram; |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 113 | Map<DeviceId, Boolean> shouldProgramCache; |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 114 | |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 115 | // Distributed routes store to keep track of the routes already seen |
| 116 | // destination device is the key and target sw is the value |
| 117 | ConsistentMultimap<DeviceId, DeviceId> seenBeforeRoutes; |
| 118 | |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 119 | // Local store to keep track of all devices that this instance was responsible |
| 120 | // for programming in the last run. Helps to determine if mastership changed |
| 121 | // during a run - only relevant for programming as a result of topo change. |
| 122 | Set<DeviceId> lastProgrammed; |
| 123 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Represents the default routing population status. |
| 126 | */ |
| 127 | public enum Status { |
| 128 | // population process is not started yet. |
| 129 | IDLE, |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 130 | // population process started. |
| 131 | STARTED, |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 132 | // population process was aborted due to errors, mostly for groups not found. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 133 | ABORTED, |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 134 | // population process was finished successfully. |
| 135 | SUCCEEDED |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Creates a DefaultRoutingHandler object. |
| 140 | * |
| 141 | * @param srManager SegmentRoutingManager object |
| 142 | */ |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 143 | DefaultRoutingHandler(SegmentRoutingManager srManager) { |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 144 | this.shouldProgram = srManager.storageService.<Set<DeviceId>, NodeId>consistentMapBuilder() |
| 145 | .withName("sr-should-program") |
| 146 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 147 | .withRelaxedReadConsistency() |
| 148 | .build().asJavaMap(); |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 149 | this.seenBeforeRoutes = srManager.storageService.<DeviceId, DeviceId>consistentMultimapBuilder() |
| 150 | .withName("programmed-routes") |
| 151 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 152 | .withRelaxedReadConsistency() |
| 153 | .build(); |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 154 | this.shouldProgramCache = Maps.newConcurrentMap(); |
| 155 | update(srManager); |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 156 | this.routePopulators = new PredictableExecutor(DEFAULT_THREADS, |
| 157 | groupedThreads("onos/sr", "r-populator-%d", log)); |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Updates a DefaultRoutingHandler object. |
| 162 | * |
| 163 | * @param srManager SegmentRoutingManager object |
| 164 | */ |
| 165 | void update(SegmentRoutingManager srManager) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 166 | this.srManager = srManager; |
| 167 | this.rulePopulator = checkNotNull(srManager.routingRulePopulator); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 168 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 169 | this.populationStatus = Status.IDLE; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 170 | this.currentEcmpSpgMap = Maps.newHashMap(); |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 171 | this.lastProgrammed = Sets.newConcurrentHashSet(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 175 | * Returns an immutable copy of the current ECMP shortest-path graph as |
| 176 | * computed by this controller instance. |
| 177 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 178 | * @return immutable copy of the current ECMP graph |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 179 | */ |
| 180 | public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEmcpSpgMap() { |
| 181 | Builder<DeviceId, EcmpShortestPathGraph> builder = ImmutableMap.builder(); |
| 182 | currentEcmpSpgMap.entrySet().forEach(entry -> { |
| 183 | if (entry.getValue() != null) { |
| 184 | builder.put(entry.getKey(), entry.getValue()); |
| 185 | } |
| 186 | }); |
| 187 | return builder.build(); |
| 188 | } |
| 189 | |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Acquires the lock used when making routing changes. |
| 192 | */ |
| 193 | public void acquireRoutingLock() { |
| 194 | statusLock.lock(); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Releases the lock used when making routing changes. |
| 199 | */ |
| 200 | public void releaseRoutingLock() { |
| 201 | statusLock.unlock(); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Determines if routing in the network has been stable in the last |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 206 | * STABILITY_THRESHOLD seconds, by comparing the current time to the last |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 207 | * routing change timestamp. |
| 208 | * |
| 209 | * @return true if stable |
| 210 | */ |
| 211 | public boolean isRoutingStable() { |
Yuta HIGUCHI | c9d9347 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 212 | long last = (long) (lastRoutingChange.toEpochMilli() / 1000.0); |
| 213 | long now = (long) (Instant.now().toEpochMilli() / 1000.0); |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 214 | log.trace("Routing stable since {}s", now - last); |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 215 | return (now - last) > STABLITY_THRESHOLD; |
| 216 | } |
| 217 | |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Gracefully shuts down the defaultRoutingHandler. Typically called when |
| 220 | * the app is deactivated |
| 221 | */ |
| 222 | public void shutdown() { |
| 223 | executorService.shutdown(); |
| 224 | executorServiceMstChg.shutdown(); |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 225 | executorServiceFRR.shutdown(); |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 226 | routePopulators.shutdown(); |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 227 | } |
Saurav Das | fbe7457 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 228 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 229 | ////////////////////////////////////// |
| 230 | // Route path handling |
| 231 | ////////////////////////////////////// |
| 232 | |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 233 | /* The following three methods represent the three major ways in which |
| 234 | * route-path handling is triggered in the network |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 235 | * a) due to configuration change |
| 236 | * b) due to route-added event |
| 237 | * c) due to change in the topology |
| 238 | */ |
| 239 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 240 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 241 | * Populates all routing rules to all switches. Typically triggered at |
| 242 | * startup or after a configuration event. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 243 | */ |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 244 | public void populateAllRoutingRules() { |
Yuta HIGUCHI | c9d9347 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 245 | lastRoutingChange = Instant.now(); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 246 | statusLock.lock(); |
| 247 | try { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 248 | if (populationStatus == Status.STARTED) { |
| 249 | log.warn("Previous rule population is not finished. Cannot" |
| 250 | + " proceed with populateAllRoutingRules"); |
| 251 | return; |
| 252 | } |
| 253 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 254 | populationStatus = Status.STARTED; |
| 255 | rulePopulator.resetCounter(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 256 | log.info("Starting to populate all routing rules"); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 257 | log.debug("populateAllRoutingRules: populationStatus is STARTED"); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 258 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 259 | // take a snapshot of the topology |
| 260 | updatedEcmpSpgMap = new HashMap<>(); |
| 261 | Set<EdgePair> edgePairs = new HashSet<>(); |
| 262 | Set<ArrayList<DeviceId>> routeChanges = new HashSet<>(); |
Jonathan Hart | 61e24e1 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 263 | for (DeviceId dstSw : srManager.deviceConfiguration.getRouters()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 264 | EcmpShortestPathGraph ecmpSpgUpdated = |
Jonathan Hart | 61e24e1 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 265 | new EcmpShortestPathGraph(dstSw, srManager); |
| 266 | updatedEcmpSpgMap.put(dstSw, ecmpSpgUpdated); |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 267 | Optional<DeviceId> pairDev = srManager.getPairDeviceId(dstSw); |
| 268 | if (pairDev.isPresent()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 269 | // pairDev may not be available yet, but we still need to add |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 270 | ecmpSpgUpdated = new EcmpShortestPathGraph(pairDev.get(), srManager); |
| 271 | updatedEcmpSpgMap.put(pairDev.get(), ecmpSpgUpdated); |
| 272 | edgePairs.add(new EdgePair(dstSw, pairDev.get())); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 273 | } |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 274 | |
| 275 | if (!shouldProgram(dstSw)) { |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 276 | lastProgrammed.remove(dstSw); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 277 | continue; |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 278 | } else { |
| 279 | lastProgrammed.add(dstSw); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 280 | } |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 281 | // To do a full reroute, assume all route-paths have changed |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 282 | for (DeviceId dev : deviceAndItsPair(dstSw)) { |
Jonathan Hart | 61e24e1 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 283 | for (DeviceId targetSw : srManager.deviceConfiguration.getRouters()) { |
| 284 | if (targetSw.equals(dev)) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 285 | continue; |
| 286 | } |
Jonathan Hart | 61e24e1 | 2017-11-30 18:23:42 -0800 | [diff] [blame] | 287 | routeChanges.add(Lists.newArrayList(targetSw, dev)); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 288 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 289 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 290 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 291 | |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 292 | log.debug("seenBeforeRoutes size {}", seenBeforeRoutes.size()); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 293 | if (!redoRouting(routeChanges, edgePairs, null)) { |
| 294 | log.debug("populateAllRoutingRules: populationStatus is ABORTED"); |
| 295 | populationStatus = Status.ABORTED; |
| 296 | log.warn("Failed to repopulate all routing rules."); |
| 297 | return; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 298 | } |
| 299 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 300 | log.debug("populateAllRoutingRules: populationStatus is SUCCEEDED"); |
| 301 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 302 | log.info("Completed all routing rule population. Total # of rules pushed : {}", |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 303 | rulePopulator.getCounter()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 304 | return; |
pier | debd15c | 2019-04-19 20:55:53 +0200 | [diff] [blame] | 305 | } catch (Exception e) { |
| 306 | log.error("populateAllRoutingRules thrown an exception: {}", |
| 307 | e.getMessage(), e); |
| 308 | populationStatus = Status.ABORTED; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 309 | } finally { |
| 310 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 311 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 312 | } |
| 313 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 314 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 315 | * Populate rules from all other edge devices to the connect-point(s) |
| 316 | * specified for the given subnets. |
| 317 | * |
| 318 | * @param cpts connect point(s) of the subnets being added |
| 319 | * @param subnets subnets being added |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 320 | */ |
| 321 | // XXX refactor |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 322 | protected void populateSubnet(Set<ConnectPoint> cpts, Set<IpPrefix> subnets) { |
Charles Chan | 6db55b9 | 2017-09-11 15:21:57 -0700 | [diff] [blame] | 323 | if (cpts == null || cpts.size() < 1 || cpts.size() > 2) { |
| 324 | log.warn("Skipping populateSubnet due to illegal size of connect points. {}", cpts); |
| 325 | return; |
| 326 | } |
| 327 | |
Yuta HIGUCHI | c9d9347 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 328 | lastRoutingChange = Instant.now(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 329 | statusLock.lock(); |
| 330 | try { |
| 331 | if (populationStatus == Status.STARTED) { |
| 332 | log.warn("Previous rule population is not finished. Cannot" |
| 333 | + " proceed with routing rules for added routes"); |
| 334 | return; |
| 335 | } |
| 336 | populationStatus = Status.STARTED; |
| 337 | rulePopulator.resetCounter(); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 338 | log.info("Starting to populate routing rules for added routes, subnets={}, cpts={}", |
| 339 | subnets, cpts); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 340 | // In principle an update to a subnet/prefix should not require a |
| 341 | // new ECMPspg calculation as it is not a topology event. As a |
| 342 | // result, we use the current/existing ECMPspg in the updated map |
| 343 | // used by the redoRouting method. |
Saurav Das | 6de6ffd | 2018-02-09 09:15:03 -0800 | [diff] [blame] | 344 | if (updatedEcmpSpgMap == null) { |
| 345 | updatedEcmpSpgMap = new HashMap<>(); |
| 346 | } |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 347 | currentEcmpSpgMap.entrySet().forEach(entry -> { |
| 348 | updatedEcmpSpgMap.put(entry.getKey(), entry.getValue()); |
Saurav Das | e321cff | 2018-02-09 17:26:45 -0800 | [diff] [blame] | 349 | if (log.isTraceEnabled()) { |
| 350 | log.trace("Root switch: {}", entry.getKey()); |
| 351 | log.trace(" Current/Existing SPG: {}", entry.getValue()); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 352 | } |
| 353 | }); |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 354 | log.debug("seenBeforeRoutes size {}", seenBeforeRoutes.size()); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 355 | Set<EdgePair> edgePairs = new HashSet<>(); |
| 356 | Set<ArrayList<DeviceId>> routeChanges = new HashSet<>(); |
| 357 | boolean handleRouting = false; |
| 358 | |
| 359 | if (cpts.size() == 2) { |
| 360 | // ensure connect points are edge-pairs |
| 361 | Iterator<ConnectPoint> iter = cpts.iterator(); |
| 362 | DeviceId dev1 = iter.next().deviceId(); |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 363 | Optional<DeviceId> pairDev = srManager.getPairDeviceId(dev1); |
| 364 | if (pairDev.isPresent() && iter.next().deviceId().equals(pairDev.get())) { |
| 365 | edgePairs.add(new EdgePair(dev1, pairDev.get())); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 366 | } else { |
| 367 | log.warn("Connectpoints {} for subnets {} not on " |
| 368 | + "pair-devices.. aborting populateSubnet", cpts, subnets); |
| 369 | populationStatus = Status.ABORTED; |
| 370 | return; |
| 371 | } |
| 372 | for (ConnectPoint cp : cpts) { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 373 | if (updatedEcmpSpgMap.get(cp.deviceId()) == null) { |
| 374 | EcmpShortestPathGraph ecmpSpgUpdated = |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 375 | new EcmpShortestPathGraph(cp.deviceId(), srManager); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 376 | updatedEcmpSpgMap.put(cp.deviceId(), ecmpSpgUpdated); |
| 377 | log.warn("populateSubnet: no updated graph for dev:{}" |
| 378 | + " ... creating", cp.deviceId()); |
| 379 | } |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 380 | if (!shouldProgram(cp.deviceId())) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 381 | continue; |
| 382 | } |
| 383 | handleRouting = true; |
| 384 | } |
| 385 | } else { |
| 386 | // single connect point |
| 387 | DeviceId dstSw = cpts.iterator().next().deviceId(); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 388 | if (updatedEcmpSpgMap.get(dstSw) == null) { |
| 389 | EcmpShortestPathGraph ecmpSpgUpdated = |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 390 | new EcmpShortestPathGraph(dstSw, srManager); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 391 | updatedEcmpSpgMap.put(dstSw, ecmpSpgUpdated); |
| 392 | log.warn("populateSubnet: no updated graph for dev:{}" |
| 393 | + " ... creating", dstSw); |
| 394 | } |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 395 | handleRouting = shouldProgram(dstSw); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | if (!handleRouting) { |
| 399 | log.debug("This instance is not handling ecmp routing to the " |
| 400 | + "connectPoint(s) {}", cpts); |
| 401 | populationStatus = Status.ABORTED; |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | // if it gets here, this instance should handle routing for the |
| 406 | // connectpoint(s). Assume all route-paths have to be updated to |
| 407 | // the connectpoint(s) with the following exceptions |
| 408 | // 1. if target is non-edge no need for routing rules |
| 409 | // 2. if target is one of the connectpoints |
| 410 | for (ConnectPoint cp : cpts) { |
| 411 | DeviceId dstSw = cp.deviceId(); |
| 412 | for (Device targetSw : srManager.deviceService.getDevices()) { |
| 413 | boolean isEdge = false; |
| 414 | try { |
| 415 | isEdge = config.isEdgeDevice(targetSw.id()); |
| 416 | } catch (DeviceConfigNotFoundException e) { |
Charles Chan | eaf3c9b | 2018-02-16 17:20:54 -0800 | [diff] [blame] | 417 | log.warn(e.getMessage() + "aborting populateSubnet on targetSw {}", targetSw.id()); |
| 418 | continue; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 419 | } |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 420 | Optional<DeviceId> pairDev = srManager.getPairDeviceId(dstSw); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 421 | if (dstSw.equals(targetSw.id()) || !isEdge || |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 422 | (cpts.size() == 2 && pairDev.isPresent() && targetSw.id().equals(pairDev.get()))) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 423 | continue; |
| 424 | } |
| 425 | routeChanges.add(Lists.newArrayList(targetSw.id(), dstSw)); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (!redoRouting(routeChanges, edgePairs, subnets)) { |
| 430 | log.debug("populateSubnet: populationStatus is ABORTED"); |
| 431 | populationStatus = Status.ABORTED; |
| 432 | log.warn("Failed to repopulate the rules for subnet."); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | log.debug("populateSubnet: populationStatus is SUCCEEDED"); |
| 437 | populationStatus = Status.SUCCEEDED; |
| 438 | log.info("Completed subnet population. Total # of rules pushed : {}", |
| 439 | rulePopulator.getCounter()); |
| 440 | return; |
| 441 | |
pier | debd15c | 2019-04-19 20:55:53 +0200 | [diff] [blame] | 442 | } catch (Exception e) { |
| 443 | log.error("populateSubnet thrown an exception: {}", |
| 444 | e.getMessage(), e); |
| 445 | populationStatus = Status.ABORTED; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 446 | } finally { |
| 447 | statusLock.unlock(); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 452 | * Populates the routing rules or makes hash group changes according to the |
| 453 | * route-path changes due to link failure, switch failure or link up. This |
| 454 | * method should only be called for one of these three possible event-types. |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 455 | * Note that when a switch goes away, all of its links fail as well, but |
| 456 | * this is handled as a single switch removal event. |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 457 | * |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 458 | * @param linkDown the single failed link, or null for other conditions such |
| 459 | * as link-up or a removed switch |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 460 | * @param linkUp the single link up, or null for other conditions such as |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 461 | * link-down or a removed switch |
| 462 | * @param switchDown the removed switch, or null for other conditions such |
| 463 | * as link-down or link-up |
| 464 | * @param seenBefore true if this event is for a linkUp or linkDown for a |
| 465 | * seen link |
| 466 | */ |
| 467 | // TODO This method should be refactored into three separated methods |
Charles Chan | 9d2dd55 | 2018-06-19 20:56:33 -0700 | [diff] [blame] | 468 | public void populateRoutingRulesForLinkStatusChange(Link linkDown, Link linkUp, |
| 469 | DeviceId switchDown, boolean seenBefore) { |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 470 | if (Stream.of(linkDown, linkUp, switchDown).filter(Objects::nonNull) |
| 471 | .count() != 1) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 472 | log.warn("Only one event can be handled for link status change .. aborting"); |
| 473 | return; |
| 474 | } |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 475 | |
Yuta HIGUCHI | c9d9347 | 2017-08-18 23:16:35 -0700 | [diff] [blame] | 476 | lastRoutingChange = Instant.now(); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 477 | statusLock.lock(); |
| 478 | try { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 479 | |
| 480 | if (populationStatus == Status.STARTED) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 481 | log.warn("Previous rule population is not finished. Cannot" |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 482 | + " proceeed with routingRules for Topology change"); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 483 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 486 | // Take snapshots of the topology |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 487 | updatedEcmpSpgMap = new HashMap<>(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 488 | Set<EdgePair> edgePairs = new HashSet<>(); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 489 | for (Device sw : srManager.deviceService.getDevices()) { |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 490 | EcmpShortestPathGraph ecmpSpgUpdated = |
| 491 | new EcmpShortestPathGraph(sw.id(), srManager); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 492 | updatedEcmpSpgMap.put(sw.id(), ecmpSpgUpdated); |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 493 | Optional<DeviceId> pairDev = srManager.getPairDeviceId(sw.id()); |
| 494 | if (pairDev.isPresent()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 495 | // pairDev may not be available yet, but we still need to add |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 496 | ecmpSpgUpdated = new EcmpShortestPathGraph(pairDev.get(), srManager); |
| 497 | updatedEcmpSpgMap.put(pairDev.get(), ecmpSpgUpdated); |
| 498 | edgePairs.add(new EdgePair(sw.id(), pairDev.get())); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 499 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 502 | log.info("Starting to populate routing rules from Topology change"); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 503 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 504 | Set<ArrayList<DeviceId>> routeChanges; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 505 | log.debug("populateRoutingRulesForLinkStatusChange: " |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 506 | + "populationStatus is STARTED"); |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 507 | log.debug("seenBeforeRoutes size {}", seenBeforeRoutes.size()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 508 | populationStatus = Status.STARTED; |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 509 | rulePopulator.resetCounter(); //XXX maybe useful to have a rehash ctr |
| 510 | boolean hashGroupsChanged = false; |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 511 | // try optimized re-routing |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 512 | if (linkDown == null) { |
| 513 | // either a linkUp or a switchDown - compute all route changes by |
| 514 | // comparing all routes of existing ECMP SPG to new ECMP SPG |
Saurav Das | cea556f | 2018-03-05 14:37:16 -0800 | [diff] [blame] | 515 | routeChanges = computeRouteChange(switchDown); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 516 | |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 517 | // deal with linkUp |
| 518 | if (linkUp != null) { |
| 519 | // deal with linkUp of a seen-before link |
| 520 | if (seenBefore) { |
| 521 | // link previously seen before |
| 522 | // do hash-bucket changes instead of a re-route |
| 523 | processHashGroupChangeForLinkUp(routeChanges); |
| 524 | // clear out routesChanges so a re-route is not attempted |
| 525 | routeChanges = ImmutableSet.of(); |
| 526 | hashGroupsChanged = true; |
| 527 | } else { |
| 528 | // do hash-bucket changes first, method will return changed routes; |
| 529 | // for each route not changed it will perform a reroute |
| 530 | Set<ArrayList<DeviceId>> changedRoutes = processHashGroupChangeForLinkUp(routeChanges); |
| 531 | Set<ArrayList<DeviceId>> routeChangesTemp = getExpandedRoutes(routeChanges); |
| 532 | changedRoutes.forEach(routeChangesTemp::remove); |
| 533 | // if routesChanges is empty a re-route is not attempted |
| 534 | routeChanges = routeChangesTemp; |
| 535 | for (ArrayList<DeviceId> route : routeChanges) { |
| 536 | log.debug("remaining routes Target -> Root"); |
| 537 | if (route.size() == 1) { |
| 538 | log.debug(" : all -> {}", route.get(0)); |
| 539 | } else { |
| 540 | log.debug(" : {} -> {}", route.get(0), route.get(1)); |
| 541 | } |
| 542 | } |
| 543 | // Mark hash groups as changed |
| 544 | if (!changedRoutes.isEmpty()) { |
| 545 | hashGroupsChanged = true; |
| 546 | } |
| 547 | } |
| 548 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 551 | //deal with switchDown |
| 552 | if (switchDown != null) { |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 553 | processHashGroupChangeForFailure(routeChanges, switchDown); |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 554 | // clear out routesChanges so a re-route is not attempted |
| 555 | routeChanges = ImmutableSet.of(); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 556 | hashGroupsChanged = true; |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 557 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 558 | } else { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 559 | // link has gone down |
| 560 | // Compare existing ECMP SPG only with the link that went down |
| 561 | routeChanges = computeDamagedRoutes(linkDown); |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 562 | processHashGroupChangeForFailure(routeChanges, null); |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 563 | // clear out routesChanges so a re-route is not attempted |
| 564 | routeChanges = ImmutableSet.of(); |
| 565 | hashGroupsChanged = true; |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 566 | } |
| 567 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 568 | if (routeChanges.isEmpty()) { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 569 | if (hashGroupsChanged) { |
| 570 | log.info("Hash-groups changed for link status change"); |
| 571 | } else { |
| 572 | log.info("No re-route or re-hash attempted for the link" |
| 573 | + " status change"); |
| 574 | updatedEcmpSpgMap.keySet().forEach(devId -> { |
| 575 | currentEcmpSpgMap.put(devId, updatedEcmpSpgMap.get(devId)); |
| 576 | log.debug("Updating ECMPspg for remaining dev:{}", devId); |
| 577 | }); |
| 578 | } |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 579 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 580 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 581 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 582 | } |
| 583 | |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 584 | if (hashGroupsChanged) { |
| 585 | log.debug("Hash-groups changed for link status change"); |
| 586 | } |
| 587 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 588 | // reroute of routeChanges |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 589 | if (redoRouting(routeChanges, edgePairs, null)) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 590 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 591 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 592 | log.info("Completed repopulation of rules for link-status change." |
| 593 | + " # of rules populated : {}", rulePopulator.getCounter()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 594 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 595 | } else { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 596 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is ABORTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 597 | populationStatus = Status.ABORTED; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 598 | log.warn("Failed to repopulate the rules for link status change."); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 599 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 600 | } |
pier | debd15c | 2019-04-19 20:55:53 +0200 | [diff] [blame] | 601 | } catch (Exception e) { |
| 602 | log.error("populateRoutingRulesForLinkStatusChange thrown an exception: {}", |
| 603 | e.getMessage(), e); |
| 604 | populationStatus = Status.ABORTED; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 605 | } finally { |
| 606 | statusLock.unlock(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 610 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 611 | * Processes a set a route-path changes by reprogramming routing rules and |
| 612 | * creating new hash-groups or editing them if necessary. This method also |
| 613 | * determines the next-hops for the route-path from the src-switch (target) |
| 614 | * of the path towards the dst-switch of the path. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 615 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 616 | * @param routeChanges a set of route-path changes, where each route-path is |
| 617 | * a list with its first element the src-switch (target) |
| 618 | * of the path, and the second element the dst-switch of |
| 619 | * the path. |
| 620 | * @param edgePairs a set of edge-switches that are paired by configuration |
| 621 | * @param subnets a set of prefixes that need to be populated in the routing |
| 622 | * table of the target switch in the route-path. Can be null, |
| 623 | * in which case all the prefixes belonging to the dst-switch |
| 624 | * will be populated in the target switch |
| 625 | * @return true if successful in repopulating all routes |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 626 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 627 | private boolean redoRouting(Set<ArrayList<DeviceId>> routeChanges, |
| 628 | Set<EdgePair> edgePairs, Set<IpPrefix> subnets) { |
| 629 | // first make every entry two-elements |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 630 | Set<ArrayList<DeviceId>> changedRoutes = getExpandedRoutes(routeChanges); |
| 631 | // no valid routes - fail fast |
| 632 | if (changedRoutes.isEmpty()) { |
| 633 | return false; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 634 | } |
| 635 | |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 636 | // Temporary stores the changed routes |
| 637 | Set<ArrayList<DeviceId>> tempRoutes = ImmutableSet.copyOf(changedRoutes); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 638 | // now process changedRoutes according to edgePairs |
| 639 | if (!redoRoutingEdgePairs(edgePairs, subnets, changedRoutes)) { |
| 640 | return false; //abort routing and fail fast |
| 641 | } |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 642 | // Calculate the programmed routes pointing to the pairs |
| 643 | Set<ArrayList<DeviceId>> programmedPairRoutes = Sets.difference(tempRoutes, changedRoutes); |
| 644 | log.debug("Evaluating programmed pair routes"); |
| 645 | storeSeenBeforeRoutes(programmedPairRoutes); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 646 | |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 647 | // Temporary stores the left routes |
| 648 | tempRoutes = ImmutableSet.copyOf(changedRoutes); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 649 | // whatever is left in changedRoutes is now processed for individual dsts. |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 650 | Set<DeviceId> updatedDevices = Sets.newHashSet(); |
| 651 | if (!redoRoutingIndividualDests(subnets, changedRoutes, |
| 652 | updatedDevices)) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 653 | return false; //abort routing and fail fast |
| 654 | } |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 655 | // Calculate the individual programmed routes |
| 656 | Set<ArrayList<DeviceId>> programmedIndividualRoutes = Sets.difference(tempRoutes, changedRoutes); |
| 657 | log.debug("Evaluating individual programmed routes"); |
| 658 | storeSeenBeforeRoutes(programmedIndividualRoutes); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 659 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 660 | // update ecmpSPG for all edge-pairs |
| 661 | for (EdgePair ep : edgePairs) { |
| 662 | currentEcmpSpgMap.put(ep.dev1, updatedEcmpSpgMap.get(ep.dev1)); |
| 663 | currentEcmpSpgMap.put(ep.dev2, updatedEcmpSpgMap.get(ep.dev2)); |
| 664 | log.debug("Updating ECMPspg for edge-pair:{}-{}", ep.dev1, ep.dev2); |
| 665 | } |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 666 | |
| 667 | // here is where we update all devices not touched by this instance |
| 668 | updatedEcmpSpgMap.keySet().stream() |
| 669 | .filter(devId -> !edgePairs.stream().anyMatch(ep -> ep.includes(devId))) |
| 670 | .filter(devId -> !updatedDevices.contains(devId)) |
| 671 | .forEach(devId -> { |
| 672 | currentEcmpSpgMap.put(devId, updatedEcmpSpgMap.get(devId)); |
| 673 | log.debug("Updating ECMPspg for remaining dev:{}", devId); |
| 674 | }); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 675 | return true; |
| 676 | } |
| 677 | |
| 678 | /** |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 679 | * Stores the routes seen before. Routes are two-elements arrays. |
| 680 | * @param seenRoutes seen before routes |
| 681 | */ |
| 682 | private void storeSeenBeforeRoutes(Set<ArrayList<DeviceId>> seenRoutes) { |
| 683 | Set<DeviceId> nextHops; |
| 684 | for (ArrayList<DeviceId> route : seenRoutes) { |
| 685 | log.debug("Route {} -> {} has been programmed", route.get(0), route.get(1)); |
| 686 | nextHops = getNextHops(route.get(0), route.get(1)); |
| 687 | // No valid next hops - cannot be considered a programmed route |
| 688 | if (nextHops.isEmpty()) { |
| 689 | log.debug("Could not find next hop from target:{} --> dst {} " |
| 690 | + "skipping this route", route.get(0), route.get(1)); |
| 691 | continue; |
| 692 | } |
| 693 | // Already present - do not add again |
| 694 | if (seenBeforeRoutes.containsEntry(route.get(1), route.get(0))) { |
| 695 | log.debug("Route from target:{} --> dst {} " + |
| 696 | "already present, skipping this route", route.get(0), route.get(1)); |
| 697 | continue; |
| 698 | } |
| 699 | seenBeforeRoutes.put(route.get(1), route.get(0)); |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 704 | * Programs targetSw in the changedRoutes for given prefixes reachable by |
| 705 | * an edgePair. If no prefixes are given, the method will use configured |
| 706 | * subnets/prefixes. If some configured subnets belong only to a specific |
| 707 | * destination in the edgePair, then the target switch will be programmed |
| 708 | * only to that destination. |
| 709 | * |
| 710 | * @param edgePairs set of edge-pairs for which target will be programmed |
| 711 | * @param subnets a set of prefixes that need to be populated in the routing |
| 712 | * table of the target switch in the changedRoutes. Can be null, |
| 713 | * in which case all the configured prefixes belonging to the |
| 714 | * paired switches will be populated in the target switch |
| 715 | * @param changedRoutes a set of route-path changes, where each route-path is |
| 716 | * a list with its first element the src-switch (target) |
| 717 | * of the path, and the second element the dst-switch of |
| 718 | * the path. |
| 719 | * @return true if successful |
| 720 | */ |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 721 | private boolean redoRoutingEdgePairs(Set<EdgePair> edgePairs, Set<IpPrefix> subnets, |
| 722 | Set<ArrayList<DeviceId>> changedRoutes) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 723 | for (EdgePair ep : edgePairs) { |
| 724 | // temp store for a target's changedRoutes to this edge-pair |
| 725 | Map<DeviceId, Set<ArrayList<DeviceId>>> targetRoutes = new HashMap<>(); |
| 726 | Iterator<ArrayList<DeviceId>> i = changedRoutes.iterator(); |
| 727 | while (i.hasNext()) { |
| 728 | ArrayList<DeviceId> route = i.next(); |
| 729 | DeviceId dstSw = route.get(1); |
| 730 | if (ep.includes(dstSw)) { |
| 731 | // routeChange for edge pair found |
| 732 | // sort by target iff target is edge and remove from changedRoutes |
| 733 | DeviceId targetSw = route.get(0); |
| 734 | try { |
| 735 | if (!srManager.deviceConfiguration.isEdgeDevice(targetSw)) { |
| 736 | continue; |
| 737 | } |
| 738 | } catch (DeviceConfigNotFoundException e) { |
| 739 | log.warn(e.getMessage() + "aborting redoRouting"); |
| 740 | return false; |
| 741 | } |
| 742 | // route is from another edge to this edge-pair |
| 743 | if (targetRoutes.containsKey(targetSw)) { |
| 744 | targetRoutes.get(targetSw).add(route); |
| 745 | } else { |
| 746 | Set<ArrayList<DeviceId>> temp = new HashSet<>(); |
| 747 | temp.add(route); |
| 748 | targetRoutes.put(targetSw, temp); |
| 749 | } |
| 750 | i.remove(); |
| 751 | } |
| 752 | } |
| 753 | // so now for this edgepair we have a per target set of routechanges |
| 754 | // process target->edgePair route |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 755 | List<Future<Boolean>> futures = Lists.newArrayList(); |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 756 | for (Entry<DeviceId, Set<ArrayList<DeviceId>>> entry : |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 757 | targetRoutes.entrySet()) { |
| 758 | log.debug("* redoRoutingDstPair Target:{} -> edge-pair {}", |
| 759 | entry.getKey(), ep); |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 760 | futures.add(routePopulators.submit(new RedoRoutingEdgePair(entry.getKey(), entry.getValue(), |
| 761 | subnets, ep))); |
| 762 | } |
| 763 | if (!checkJobs(futures)) { |
| 764 | return false; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 765 | } |
| 766 | // if it gets here it has succeeded for all targets to this edge-pair |
| 767 | } |
| 768 | return true; |
| 769 | } |
| 770 | |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 771 | private final class RedoRoutingEdgePair implements PickyCallable<Boolean> { |
| 772 | private DeviceId targetSw; |
| 773 | private Set<ArrayList<DeviceId>> routes; |
| 774 | private Set<IpPrefix> subnets; |
| 775 | private EdgePair ep; |
| 776 | |
| 777 | /** |
| 778 | * Builds a RedoRoutingEdgePair task which provides a result. |
| 779 | * |
| 780 | * @param targetSw the target switch |
| 781 | * @param routes the changed routes |
| 782 | * @param subnets the subnets |
| 783 | * @param ep the edge pair |
| 784 | */ |
| 785 | RedoRoutingEdgePair(DeviceId targetSw, Set<ArrayList<DeviceId>> routes, |
| 786 | Set<IpPrefix> subnets, EdgePair ep) { |
| 787 | this.targetSw = targetSw; |
| 788 | this.routes = routes; |
| 789 | this.subnets = subnets; |
| 790 | this.ep = ep; |
| 791 | } |
| 792 | |
| 793 | @Override |
| 794 | public Boolean call() throws Exception { |
| 795 | return redoRoutingEdgePair(); |
| 796 | } |
| 797 | |
| 798 | @Override |
| 799 | public int hint() { |
| 800 | return targetSw.hashCode(); |
| 801 | } |
| 802 | |
| 803 | private boolean redoRoutingEdgePair() { |
| 804 | Map<DeviceId, Set<DeviceId>> perDstNextHops = new HashMap<>(); |
| 805 | routes.forEach(route -> { |
| 806 | Set<DeviceId> nhops = getNextHops(route.get(0), route.get(1)); |
| 807 | log.debug("route: target {} -> dst {} found with next-hops {}", |
| 808 | route.get(0), route.get(1), nhops); |
| 809 | perDstNextHops.put(route.get(1), nhops); |
| 810 | }); |
| 811 | |
| 812 | List<Set<IpPrefix>> batchedSubnetDev1, batchedSubnetDev2; |
| 813 | if (subnets != null) { |
| 814 | batchedSubnetDev1 = Lists.<Set<IpPrefix>>newArrayList(Sets.newHashSet(subnets)); |
| 815 | batchedSubnetDev2 = Lists.<Set<IpPrefix>>newArrayList(Sets.newHashSet(subnets)); |
| 816 | } else { |
| 817 | batchedSubnetDev1 = config.getBatchedSubnets(ep.dev1); |
| 818 | batchedSubnetDev2 = config.getBatchedSubnets(ep.dev2); |
| 819 | } |
| 820 | List<Set<IpPrefix>> batchedSubnetBoth = Streams |
| 821 | .zip(batchedSubnetDev1.stream(), batchedSubnetDev2.stream(), (a, b) -> Sets.intersection(a, b)) |
| 822 | .filter(set -> !set.isEmpty()) |
| 823 | .collect(Collectors.toList()); |
| 824 | List<Set<IpPrefix>> batchedSubnetDev1Only = Streams |
| 825 | .zip(batchedSubnetDev1.stream(), batchedSubnetDev2.stream(), (a, b) -> Sets.difference(a, b)) |
| 826 | .filter(set -> !set.isEmpty()) |
| 827 | .collect(Collectors.toList()); |
| 828 | List<Set<IpPrefix>> batchedSubnetDev2Only = Streams |
| 829 | .zip(batchedSubnetDev1.stream(), batchedSubnetDev2.stream(), (a, b) -> Sets.difference(b, a)) |
| 830 | .filter(set -> !set.isEmpty()) |
| 831 | .collect(Collectors.toList()); |
| 832 | |
| 833 | Set<DeviceId> nhDev1 = perDstNextHops.get(ep.dev1); |
| 834 | Set<DeviceId> nhDev2 = perDstNextHops.get(ep.dev2); |
| 835 | |
| 836 | // handle routing to subnets common to edge-pair |
| 837 | // only if the targetSw is not part of the edge-pair and there |
| 838 | // exists a next hop to at least one of the devices in the edge-pair |
| 839 | if (!ep.includes(targetSw) |
| 840 | && ((nhDev1 != null && !nhDev1.isEmpty()) || (nhDev2 != null && !nhDev2.isEmpty()))) { |
| 841 | log.trace("getSubnets on both {} and {}: {}", ep.dev1, ep.dev2, batchedSubnetBoth); |
| 842 | for (Set<IpPrefix> prefixes : batchedSubnetBoth) { |
| 843 | if (!populateEcmpRoutingRulePartial(targetSw, ep.dev1, ep.dev2, |
| 844 | perDstNextHops, prefixes)) { |
| 845 | return false; // abort everything and fail fast |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | } |
| 850 | // handle routing to subnets that only belong to dev1 only if |
| 851 | // a next-hop exists from the target to dev1 |
| 852 | if (!batchedSubnetDev1Only.isEmpty() && |
| 853 | batchedSubnetDev1Only.stream().anyMatch(subnet -> !subnet.isEmpty()) && |
| 854 | nhDev1 != null && !nhDev1.isEmpty()) { |
| 855 | Map<DeviceId, Set<DeviceId>> onlyDev1NextHops = new HashMap<>(); |
| 856 | onlyDev1NextHops.put(ep.dev1, nhDev1); |
| 857 | log.trace("getSubnets on {} only: {}", ep.dev1, batchedSubnetDev1Only); |
| 858 | for (Set<IpPrefix> prefixes : batchedSubnetDev1Only) { |
| 859 | if (!populateEcmpRoutingRulePartial(targetSw, ep.dev1, null, |
| 860 | onlyDev1NextHops, prefixes)) { |
| 861 | return false; // abort everything and fail fast |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | // handle routing to subnets that only belong to dev2 only if |
| 866 | // a next-hop exists from the target to dev2 |
| 867 | if (!batchedSubnetDev2Only.isEmpty() && |
| 868 | batchedSubnetDev2Only.stream().anyMatch(subnet -> !subnet.isEmpty()) && |
| 869 | nhDev2 != null && !nhDev2.isEmpty()) { |
| 870 | Map<DeviceId, Set<DeviceId>> onlyDev2NextHops = new HashMap<>(); |
| 871 | onlyDev2NextHops.put(ep.dev2, nhDev2); |
| 872 | log.trace("getSubnets on {} only: {}", ep.dev2, batchedSubnetDev2Only); |
| 873 | for (Set<IpPrefix> prefixes : batchedSubnetDev2Only) { |
| 874 | if (!populateEcmpRoutingRulePartial(targetSw, ep.dev2, null, |
| 875 | onlyDev2NextHops, prefixes)) { |
| 876 | return false; // abort everything and fail fast |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | return true; |
| 881 | } |
| 882 | } |
| 883 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 884 | /** |
| 885 | * Programs targetSw in the changedRoutes for given prefixes reachable by |
| 886 | * a destination switch that is not part of an edge-pair. |
| 887 | * If no prefixes are given, the method will use configured subnets/prefixes. |
| 888 | * |
| 889 | * @param subnets a set of prefixes that need to be populated in the routing |
| 890 | * table of the target switch in the changedRoutes. Can be null, |
| 891 | * in which case all the configured prefixes belonging to the |
| 892 | * paired switches will be populated in the target switch |
| 893 | * @param changedRoutes a set of route-path changes, where each route-path is |
| 894 | * a list with its first element the src-switch (target) |
| 895 | * of the path, and the second element the dst-switch of |
| 896 | * the path. |
| 897 | * @return true if successful |
| 898 | */ |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 899 | private boolean redoRoutingIndividualDests(Set<IpPrefix> subnets, Set<ArrayList<DeviceId>> changedRoutes, |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 900 | Set<DeviceId> updatedDevices) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 901 | // aggregate route-path changes for each dst device |
| 902 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> routesBydevice = |
| 903 | new HashMap<>(); |
| 904 | for (ArrayList<DeviceId> route: changedRoutes) { |
| 905 | DeviceId dstSw = route.get(1); |
| 906 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 907 | routesBydevice.get(dstSw); |
| 908 | if (deviceRoutes == null) { |
| 909 | deviceRoutes = new ArrayList<>(); |
| 910 | routesBydevice.put(dstSw, deviceRoutes); |
| 911 | } |
| 912 | deviceRoutes.add(route); |
| 913 | } |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 914 | // iterate over the impacted devices |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 915 | for (DeviceId impactedDstDevice : routesBydevice.keySet()) { |
| 916 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 917 | routesBydevice.get(impactedDstDevice); |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 918 | List<Future<Boolean>> futures = Lists.newArrayList(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 919 | for (ArrayList<DeviceId> route: deviceRoutes) { |
| 920 | log.debug("* redoRoutingIndiDst Target: {} -> dst: {}", |
| 921 | route.get(0), route.get(1)); |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 922 | futures.add(routePopulators.submit(new RedoRoutingIndividualDest(subnets, route))); |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 923 | changedRoutes.remove(route); |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 924 | } |
| 925 | // check the execution of each job |
| 926 | if (!checkJobs(futures)) { |
| 927 | return false; |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 928 | } |
| 929 | //Only if all the flows for all impacted routes to a |
| 930 | //specific target are pushed successfully, update the |
| 931 | //ECMP graph for that target. Or else the next event |
| 932 | //would not see any changes in the ECMP graphs. |
| 933 | //In another case, the target switch has gone away, so |
| 934 | //routes can't be installed. In that case, the current map |
| 935 | //is updated here, without any flows being pushed. |
| 936 | currentEcmpSpgMap.put(impactedDstDevice, |
| 937 | updatedEcmpSpgMap.get(impactedDstDevice)); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 938 | updatedDevices.add(impactedDstDevice); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 939 | log.debug("Updating ECMPspg for impacted dev:{}", impactedDstDevice); |
| 940 | } |
| 941 | return true; |
| 942 | } |
| 943 | |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 944 | private final class RedoRoutingIndividualDest implements PickyCallable<Boolean> { |
| 945 | private DeviceId targetSw; |
| 946 | private ArrayList<DeviceId> route; |
| 947 | private Set<IpPrefix> subnets; |
| 948 | |
| 949 | /** |
| 950 | * Builds a RedoRoutingIndividualDest task, which provides a result. |
| 951 | * |
| 952 | * @param subnets a set of prefixes |
| 953 | * @param route a route-path change |
| 954 | */ |
| 955 | RedoRoutingIndividualDest(Set<IpPrefix> subnets, ArrayList<DeviceId> route) { |
| 956 | this.targetSw = route.get(0); |
| 957 | this.route = route; |
| 958 | this.subnets = subnets; |
| 959 | } |
| 960 | |
| 961 | @Override |
| 962 | public Boolean call() throws Exception { |
| 963 | DeviceId dstSw = route.get(1); // same as impactedDstDevice |
| 964 | Set<DeviceId> nextHops = getNextHops(targetSw, dstSw); |
| 965 | if (nextHops.isEmpty()) { |
| 966 | log.debug("Could not find next hop from target:{} --> dst {} " |
| 967 | + "skipping this route", targetSw, dstSw); |
| 968 | return true; |
| 969 | } |
| 970 | Map<DeviceId, Set<DeviceId>> nhops = new HashMap<>(); |
| 971 | nhops.put(dstSw, nextHops); |
| 972 | if (!populateEcmpRoutingRulePartial(targetSw, dstSw, null, nhops, |
| 973 | (subnets == null) ? Sets.newHashSet() : subnets)) { |
| 974 | return false; // abort routing and fail fast |
| 975 | } |
| 976 | log.debug("Populating flow rules from target: {} to dst: {}" |
| 977 | + " is successful", targetSw, dstSw); |
| 978 | return true; |
| 979 | } |
| 980 | |
| 981 | @Override |
| 982 | public int hint() { |
| 983 | return targetSw.hashCode(); |
| 984 | } |
| 985 | } |
| 986 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 987 | /** |
| 988 | * Populate ECMP rules for subnets from target to destination via nexthops. |
| 989 | * |
| 990 | * @param targetSw Device ID of target switch in which rules will be programmed |
| 991 | * @param destSw1 Device ID of final destination switch to which the rules will forward |
| 992 | * @param destSw2 Device ID of paired destination switch to which the rules will forward |
| 993 | * A null deviceId indicates packets should only be sent to destSw1 |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 994 | * @param nextHops Map of a set of next hops per destSw |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 995 | * @param subnets Subnets to be populated. If empty, populate all configured subnets. |
| 996 | * @return true if it succeeds in populating rules |
| 997 | */ // refactor |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 998 | private boolean populateEcmpRoutingRulePartial(DeviceId targetSw, DeviceId destSw1, DeviceId destSw2, |
| 999 | Map<DeviceId, Set<DeviceId>> nextHops, Set<IpPrefix> subnets) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1000 | boolean result; |
| 1001 | // If both target switch and dest switch are edge routers, then set IP |
| 1002 | // rule for both subnet and router IP. |
| 1003 | boolean targetIsEdge; |
| 1004 | boolean dest1IsEdge; |
| 1005 | Ip4Address dest1RouterIpv4, dest2RouterIpv4 = null; |
| 1006 | Ip6Address dest1RouterIpv6, dest2RouterIpv6 = null; |
| 1007 | |
| 1008 | try { |
| 1009 | targetIsEdge = config.isEdgeDevice(targetSw); |
| 1010 | dest1IsEdge = config.isEdgeDevice(destSw1); |
| 1011 | dest1RouterIpv4 = config.getRouterIpv4(destSw1); |
| 1012 | dest1RouterIpv6 = config.getRouterIpv6(destSw1); |
| 1013 | if (destSw2 != null) { |
| 1014 | dest2RouterIpv4 = config.getRouterIpv4(destSw2); |
| 1015 | dest2RouterIpv6 = config.getRouterIpv6(destSw2); |
| 1016 | } |
| 1017 | } catch (DeviceConfigNotFoundException e) { |
| 1018 | log.warn(e.getMessage() + " Aborting populateEcmpRoutingRulePartial."); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1019 | return false; |
| 1020 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1021 | |
| 1022 | if (targetIsEdge && dest1IsEdge) { |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame] | 1023 | List<Set<IpPrefix>> batchedSubnets; |
| 1024 | if (subnets != null && !subnets.isEmpty()) { |
| 1025 | batchedSubnets = Lists.<Set<IpPrefix>>newArrayList(Sets.newHashSet(subnets)); |
| 1026 | } else { |
| 1027 | batchedSubnets = config.getBatchedSubnets(destSw1); |
| 1028 | } |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1029 | // XXX - Rethink this - ignoring routerIPs in all other switches |
| 1030 | // even edge to edge switches |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1031 | /*subnets.add(dest1RouterIpv4.toIpPrefix()); |
| 1032 | if (dest1RouterIpv6 != null) { |
| 1033 | subnets.add(dest1RouterIpv6.toIpPrefix()); |
| 1034 | } |
| 1035 | if (destSw2 != null && dest2RouterIpv4 != null) { |
| 1036 | subnets.add(dest2RouterIpv4.toIpPrefix()); |
| 1037 | if (dest2RouterIpv6 != null) { |
| 1038 | subnets.add(dest2RouterIpv6.toIpPrefix()); |
| 1039 | } |
| 1040 | }*/ |
Charles Chan | 19b7003 | 2019-04-17 14:20:26 -0700 | [diff] [blame] | 1041 | log.trace("getSubnets on {}: {}", destSw1, batchedSubnets); |
| 1042 | for (Set<IpPrefix> prefixes : batchedSubnets) { |
| 1043 | log.debug(". populateEcmpRoutingRulePartial in device {} towards {} {} " |
| 1044 | + "for subnets {}", targetSw, destSw1, |
| 1045 | (destSw2 != null) ? ("& " + destSw2) : "", |
| 1046 | prefixes); |
| 1047 | if (!rulePopulator.populateIpRuleForSubnet(targetSw, prefixes, destSw1, destSw2, nextHops)) { |
| 1048 | return false; |
| 1049 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1050 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1051 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1052 | |
| 1053 | if (!targetIsEdge && dest1IsEdge) { |
| 1054 | // MPLS rules in all non-edge target devices. These rules are for |
| 1055 | // individual destinations, even if the dsts are part of edge-pairs. |
| 1056 | log.debug(". populateEcmpRoutingRulePartial in device{} towards {} for " |
| 1057 | + "all MPLS rules", targetSw, destSw1); |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 1058 | result = rulePopulator.populateMplsRule(targetSw, destSw1, nextHops.get(destSw1), dest1RouterIpv4); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1059 | if (!result) { |
| 1060 | return false; |
| 1061 | } |
| 1062 | if (dest1RouterIpv6 != null) { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1063 | int v4sid = 0, v6sid = 0; |
| 1064 | try { |
| 1065 | v4sid = config.getIPv4SegmentId(destSw1); |
| 1066 | v6sid = config.getIPv6SegmentId(destSw1); |
| 1067 | } catch (DeviceConfigNotFoundException e) { |
| 1068 | log.warn(e.getMessage()); |
| 1069 | } |
| 1070 | if (v4sid != v6sid) { |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 1071 | result = rulePopulator.populateMplsRule(targetSw, destSw1, nextHops.get(destSw1), |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 1072 | dest1RouterIpv6); |
| 1073 | if (!result) { |
| 1074 | return false; |
| 1075 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
Andreas Pantelopoulos | fc4bc2a | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 1080 | if (!targetIsEdge && !dest1IsEdge) { |
| 1081 | // MPLS rules for inter-connected spines |
| 1082 | // can be merged with above if, left it here for clarity |
| 1083 | log.debug(". populateEcmpRoutingRulePartial in device{} towards {} for " |
| 1084 | + "all MPLS rules", targetSw, destSw1); |
| 1085 | |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 1086 | result = rulePopulator.populateMplsRule(targetSw, destSw1, nextHops.get(destSw1), dest1RouterIpv4); |
Andreas Pantelopoulos | fc4bc2a | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 1087 | if (!result) { |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | if (dest1RouterIpv6 != null) { |
| 1092 | int v4sid = 0, v6sid = 0; |
| 1093 | try { |
| 1094 | v4sid = config.getIPv4SegmentId(destSw1); |
| 1095 | v6sid = config.getIPv6SegmentId(destSw1); |
| 1096 | } catch (DeviceConfigNotFoundException e) { |
| 1097 | log.warn(e.getMessage()); |
| 1098 | } |
| 1099 | if (v4sid != v6sid) { |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 1100 | result = rulePopulator.populateMplsRule(targetSw, destSw1, nextHops.get(destSw1), |
Andreas Pantelopoulos | fc4bc2a | 2018-03-12 16:30:20 -0700 | [diff] [blame] | 1101 | dest1RouterIpv6); |
| 1102 | if (!result) { |
| 1103 | return false; |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1109 | // To save on ECMP groups |
| 1110 | // avoid MPLS rules in non-edge-devices to non-edge-devices |
| 1111 | // avoid MPLS transit rules in edge-devices |
| 1112 | // avoid loopback IP rules in edge-devices to non-edge-devices |
| 1113 | return true; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /** |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1117 | * Processes a set a route-path changes due to a switch/link failure by editing hash groups. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1118 | * |
| 1119 | * @param routeChanges a set of route-path changes, where each route-path is |
| 1120 | * a list with its first element the src-switch of the path |
| 1121 | * and the second element the dst-switch of the path. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1122 | * @param failedSwitch the switchId if the route changes are for a failed switch, |
| 1123 | * otherwise null |
| 1124 | */ |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1125 | private void processHashGroupChangeForFailure(Set<ArrayList<DeviceId>> routeChanges, |
| 1126 | DeviceId failedSwitch) { |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1127 | // first, ensure each routeChanges entry has two elements |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1128 | Set<ArrayList<DeviceId>> changedRoutes = getAllExpandedRoutes(routeChanges); |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1129 | boolean someFailed = false; |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1130 | boolean success; |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1131 | Set<DeviceId> updatedDevices = Sets.newHashSet(); |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1132 | for (ArrayList<DeviceId> route : changedRoutes) { |
| 1133 | DeviceId targetSw = route.get(0); |
| 1134 | DeviceId dstSw = route.get(1); |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1135 | success = fixHashGroupsForRoute(route, true); |
| 1136 | // it's possible that we cannot fix hash groups for a route |
| 1137 | // if the target switch has failed. Nevertheless the ecmp graph |
| 1138 | // for the impacted switch must still be updated. |
| 1139 | if (!success && failedSwitch != null && targetSw.equals(failedSwitch)) { |
| 1140 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 1141 | currentEcmpSpgMap.remove(targetSw); |
| 1142 | log.debug("Updating ECMPspg for dst:{} removing failed switch " |
| 1143 | + "target:{}", dstSw, targetSw); |
| 1144 | updatedDevices.add(targetSw); |
| 1145 | updatedDevices.add(dstSw); |
| 1146 | continue; |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 1147 | |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1148 | } |
| 1149 | //linkfailed - update both sides |
| 1150 | if (success) { |
| 1151 | currentEcmpSpgMap.put(targetSw, updatedEcmpSpgMap.get(targetSw)); |
| 1152 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 1153 | log.debug("Updating ECMPspg for dst:{} and target:{} for linkdown" |
| 1154 | + " or switchdown", dstSw, targetSw); |
| 1155 | updatedDevices.add(targetSw); |
| 1156 | updatedDevices.add(dstSw); |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1157 | } else { |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1158 | someFailed = true; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1159 | } |
| 1160 | } |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1161 | if (!someFailed) { |
| 1162 | // here is where we update all devices not touched by this instance |
| 1163 | updatedEcmpSpgMap.keySet().stream() |
| 1164 | .filter(devId -> !updatedDevices.contains(devId)) |
| 1165 | .forEach(devId -> { |
| 1166 | currentEcmpSpgMap.put(devId, updatedEcmpSpgMap.get(devId)); |
| 1167 | log.debug("Updating ECMPspg for remaining dev:{}", devId); |
| 1168 | }); |
| 1169 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /** |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1173 | * Processes a set a route-path changes due to link up by editing hash groups. |
| 1174 | * |
| 1175 | * @param routeChanges a set of route-path changes, where each route-path is |
| 1176 | * a list with its first element the src-switch of the path |
| 1177 | * and the second element the dst-switch of the path. |
| 1178 | * @return set of changed routes |
| 1179 | */ |
| 1180 | private Set<ArrayList<DeviceId>> processHashGroupChangeForLinkUp(Set<ArrayList<DeviceId>> routeChanges) { |
| 1181 | // Stores changed routes |
| 1182 | Set<ArrayList<DeviceId>> doneRoutes = new HashSet<>(); |
| 1183 | // first, ensure each routeChanges entry has two elements |
| 1184 | Set<ArrayList<DeviceId>> changedRoutes = getAllExpandedRoutes(routeChanges); |
| 1185 | boolean someFailed = false; |
| 1186 | boolean success; |
| 1187 | Set<DeviceId> updatedDevices = Sets.newHashSet(); |
| 1188 | for (ArrayList<DeviceId> route : changedRoutes) { |
| 1189 | DeviceId targetSw = route.get(0); |
| 1190 | DeviceId dstSw = route.get(1); |
| 1191 | // linkup - fix (if possible) |
| 1192 | success = fixHashGroupsForRoute(route, false); |
| 1193 | if (success) { |
| 1194 | currentEcmpSpgMap.put(targetSw, updatedEcmpSpgMap.get(targetSw)); |
| 1195 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 1196 | log.debug("Updating ECMPspg for target:{} and dst:{} for linkup", |
| 1197 | targetSw, dstSw); |
| 1198 | updatedDevices.add(targetSw); |
| 1199 | updatedDevices.add(dstSw); |
| 1200 | doneRoutes.add(route); |
| 1201 | } else { |
| 1202 | someFailed = true; |
| 1203 | } |
| 1204 | |
| 1205 | } |
| 1206 | if (!someFailed) { |
| 1207 | // here is where we update all devices not touched by this instance |
| 1208 | updatedEcmpSpgMap.keySet().stream() |
| 1209 | .filter(devId -> !updatedDevices.contains(devId)) |
| 1210 | .forEach(devId -> { |
| 1211 | currentEcmpSpgMap.put(devId, updatedEcmpSpgMap.get(devId)); |
| 1212 | log.debug("Updating ECMPspg for remaining dev:{}", devId); |
| 1213 | }); |
| 1214 | } |
| 1215 | return doneRoutes; |
| 1216 | } |
| 1217 | |
| 1218 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1219 | * Edits hash groups in the src-switch (targetSw) of a route-path by |
| 1220 | * calling the groupHandler to either add or remove buckets in an existing |
| 1221 | * hash group. |
| 1222 | * |
| 1223 | * @param route a single list representing a route-path where the first element |
| 1224 | * is the src-switch (targetSw) of the route-path and the |
| 1225 | * second element is the dst-switch |
| 1226 | * @param revoke true if buckets in the hash-groups need to be removed; |
| 1227 | * false if buckets in the hash-groups need to be added |
| 1228 | * @return true if the hash group editing is successful |
| 1229 | */ |
| 1230 | private boolean fixHashGroupsForRoute(ArrayList<DeviceId> route, |
| 1231 | boolean revoke) { |
| 1232 | DeviceId targetSw = route.get(0); |
| 1233 | if (route.size() < 2) { |
| 1234 | log.warn("Cannot fixHashGroupsForRoute - no dstSw in route {}", route); |
| 1235 | return false; |
| 1236 | } |
| 1237 | DeviceId destSw = route.get(1); |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 1238 | if (!seenBeforeRoutes.containsEntry(destSw, targetSw)) { |
| 1239 | log.warn("Cannot fixHashGroupsForRoute {} -> {} has not been programmed before", |
| 1240 | targetSw, destSw); |
| 1241 | return false; |
| 1242 | } |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1243 | log.debug("* processing fixHashGroupsForRoute: Target {} -> Dest {}", |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1244 | targetSw, destSw); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1245 | // figure out the new next hops at the targetSw towards the destSw |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1246 | Set<DeviceId> nextHops = getNextHops(targetSw, destSw); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1247 | // call group handler to change hash group at targetSw |
| 1248 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw); |
| 1249 | if (grpHandler == null) { |
| 1250 | log.warn("Cannot find grouphandler for dev:{} .. aborting" |
| 1251 | + " {} hash group buckets for route:{} ", targetSw, |
| 1252 | (revoke) ? "revoke" : "repopulate", route); |
| 1253 | return false; |
| 1254 | } |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1255 | log.debug("{} hash-groups buckets For Route {} -> {} to new next-hops {}", |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1256 | (revoke) ? "revoke" : "repopulating", |
| 1257 | targetSw, destSw, nextHops); |
| 1258 | return (revoke) ? grpHandler.fixHashGroups(targetSw, nextHops, |
| 1259 | destSw, true) |
| 1260 | : grpHandler.fixHashGroups(targetSw, nextHops, |
| 1261 | destSw, false); |
| 1262 | } |
| 1263 | |
| 1264 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1265 | * Start the flow rule population process if it was never started. The |
| 1266 | * process finishes successfully when all flow rules are set and stops with |
| 1267 | * ABORTED status when any groups required for flows is not set yet. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1268 | */ |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1269 | public void startPopulationProcess() { |
| 1270 | statusLock.lock(); |
| 1271 | try { |
| 1272 | if (populationStatus == Status.IDLE |
| 1273 | || populationStatus == Status.SUCCEEDED |
| 1274 | || populationStatus == Status.ABORTED) { |
| 1275 | populateAllRoutingRules(); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 1276 | } else { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1277 | log.warn("Not initiating startPopulationProcess as populationStatus is {}", |
| 1278 | populationStatus); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1279 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1280 | } finally { |
| 1281 | statusLock.unlock(); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1282 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1285 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1286 | * Revoke rules of given subnet in all edge switches. |
| 1287 | * |
| 1288 | * @param subnets subnet being removed |
| 1289 | * @return true if succeed |
| 1290 | */ |
| 1291 | protected boolean revokeSubnet(Set<IpPrefix> subnets) { |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 1292 | DeviceId targetSw; |
| 1293 | List<Future<Boolean>> futures = Lists.newArrayList(); |
| 1294 | for (Device sw : srManager.deviceService.getAvailableDevices()) { |
| 1295 | targetSw = sw.id(); |
| 1296 | if (shouldProgram(targetSw)) { |
| 1297 | futures.add(routePopulators.submit(new RevokeSubnet(targetSw, subnets))); |
| 1298 | } else { |
| 1299 | futures.add(CompletableFuture.completedFuture(true)); |
| 1300 | } |
| 1301 | } |
| 1302 | // check the execution of each job |
| 1303 | return checkJobs(futures); |
| 1304 | } |
| 1305 | |
Shibu Vijayakumar | 5e26f8c | 2020-01-07 11:45:09 +0000 | [diff] [blame] | 1306 | /** |
| 1307 | * Revoke rules of given subnets in the given switches. |
| 1308 | * |
| 1309 | * @param targetSwitches switched from which subnets to be removed |
| 1310 | * @param subnets subnet bring removed |
| 1311 | * @return true if succeed |
| 1312 | */ |
| 1313 | protected boolean revokeSubnet(Set<DeviceId> targetSwitches, Set<IpPrefix> subnets) { |
| 1314 | List<Future<Boolean>> futures = Lists.newArrayList(); |
| 1315 | for (DeviceId targetSw : targetSwitches) { |
| 1316 | if (shouldProgram(targetSw)) { |
| 1317 | futures.add(routePopulators.submit(new RevokeSubnet(targetSw, subnets))); |
| 1318 | } else { |
| 1319 | futures.add(CompletableFuture.completedFuture(true)); |
| 1320 | } |
| 1321 | } |
| 1322 | // check the execution of each job |
| 1323 | return checkJobs(futures); |
| 1324 | } |
| 1325 | |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 1326 | private final class RevokeSubnet implements PickyCallable<Boolean> { |
| 1327 | private DeviceId targetSw; |
| 1328 | private Set<IpPrefix> subnets; |
| 1329 | |
| 1330 | /** |
| 1331 | * Builds a RevokeSubnet task, which provides a result. |
| 1332 | * |
| 1333 | * @param subnets a set of prefixes |
| 1334 | * @param targetSw target switch |
| 1335 | */ |
| 1336 | RevokeSubnet(DeviceId targetSw, Set<IpPrefix> subnets) { |
| 1337 | this.targetSw = targetSw; |
| 1338 | this.subnets = subnets; |
| 1339 | } |
| 1340 | |
| 1341 | @Override |
| 1342 | public Boolean call() throws Exception { |
| 1343 | return srManager.routingRulePopulator.revokeIpRuleForSubnet(targetSw, subnets); |
| 1344 | } |
| 1345 | |
| 1346 | @Override |
| 1347 | public int hint() { |
| 1348 | return targetSw.hashCode(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | /** |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 1353 | * Populates IP rules for a route that has direct connection to the switch |
| 1354 | * if the current instance is the master of the switch. |
| 1355 | * |
| 1356 | * @param deviceId device ID of the device that next hop attaches to |
| 1357 | * @param prefix IP prefix of the route |
| 1358 | * @param hostMac MAC address of the next hop |
| 1359 | * @param hostVlanId Vlan ID of the nexthop |
| 1360 | * @param outPort port where the next hop attaches to |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1361 | * @param directHost host is of type direct or indirect |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1362 | * @return future that includes the flow objective if succeeded, null if otherwise |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 1363 | */ |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1364 | CompletableFuture<Objective> populateRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, |
| 1365 | VlanId hostVlanId, PortNumber outPort, boolean directHost) { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1366 | if (shouldProgram(deviceId)) { |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1367 | return srManager.routingRulePopulator.populateRoute(deviceId, prefix, |
| 1368 | hostMac, hostVlanId, outPort, directHost); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 1369 | } |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1370 | return CompletableFuture.completedFuture(null); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | /** |
| 1374 | * Removes IP rules for a route when the next hop is gone. |
| 1375 | * if the current instance is the master of the switch. |
| 1376 | * |
| 1377 | * @param deviceId device ID of the device that next hop attaches to |
| 1378 | * @param prefix IP prefix of the route |
| 1379 | * @param hostMac MAC address of the next hop |
| 1380 | * @param hostVlanId Vlan ID of the nexthop |
| 1381 | * @param outPort port that next hop attaches to |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1382 | * @param directHost host is of type direct or indirect |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1383 | * @return future that carries the flow objective if succeeded, null if otherwise |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 1384 | */ |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1385 | CompletableFuture<Objective> revokeRoute(DeviceId deviceId, IpPrefix prefix, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 1386 | MacAddress hostMac, VlanId hostVlanId, PortNumber outPort, boolean directHost) { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1387 | if (shouldProgram(deviceId)) { |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1388 | return srManager.routingRulePopulator.revokeRoute(deviceId, prefix, hostMac, hostVlanId, |
| 1389 | outPort, directHost); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 1390 | } |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1391 | return CompletableFuture.completedFuture(null); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1394 | CompletableFuture<Objective> populateBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1395 | if (shouldProgram(deviceId)) { |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1396 | return srManager.routingRulePopulator.populateBridging(deviceId, port, mac, vlanId); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1397 | } |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1398 | return CompletableFuture.completedFuture(null); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1399 | } |
| 1400 | |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1401 | CompletableFuture<Objective> revokeBridging(DeviceId deviceId, PortNumber port, MacAddress mac, VlanId vlanId) { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1402 | if (shouldProgram(deviceId)) { |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1403 | return srManager.routingRulePopulator.revokeBridging(deviceId, port, mac, vlanId); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1404 | } |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 1405 | return CompletableFuture.completedFuture(null); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 1408 | CompletableFuture<Objective> updateBridging(DeviceId deviceId, PortNumber portNum, MacAddress hostMac, |
| 1409 | VlanId vlanId, boolean popVlan, boolean install) { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1410 | if (shouldProgram(deviceId)) { |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 1411 | return srManager.routingRulePopulator.updateBridging(deviceId, portNum, hostMac, vlanId, popVlan, install); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1412 | } |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 1413 | return CompletableFuture.completedFuture(null); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 1416 | CompletableFuture<Objective> updateFwdObj(DeviceId deviceId, PortNumber portNumber, IpPrefix prefix, |
| 1417 | MacAddress hostMac, VlanId vlanId, boolean popVlan, boolean install) { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1418 | if (shouldProgram(deviceId)) { |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 1419 | return srManager.routingRulePopulator.updateFwdObj(deviceId, portNumber, prefix, hostMac, |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1420 | vlanId, popVlan, install); |
| 1421 | } |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 1422 | return CompletableFuture.completedFuture(null); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 1425 | /** |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1426 | * Populates IP rules for a route when the next hop is double-tagged. |
| 1427 | * |
| 1428 | * @param deviceId device ID that next hop attaches to |
| 1429 | * @param prefix IP prefix of the route |
| 1430 | * @param hostMac MAC address of the next hop |
| 1431 | * @param innerVlan Inner Vlan ID of the next hop |
| 1432 | * @param outerVlan Outer Vlan ID of the next hop |
| 1433 | * @param outerTpid Outer TPID of the next hop |
| 1434 | * @param outPort port that the next hop attaches to |
| 1435 | */ |
| 1436 | void populateDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, VlanId innerVlan, |
| 1437 | VlanId outerVlan, EthType outerTpid, PortNumber outPort) { |
| 1438 | if (srManager.mastershipService.isLocalMaster(deviceId)) { |
Charles Chan | 61c086d | 2019-07-26 17:46:15 -0700 | [diff] [blame] | 1439 | srManager.routingRulePopulator.populateDoubleTaggedRoute( |
| 1440 | deviceId, prefix, hostMac, innerVlan, outerVlan, outerTpid, outPort); |
| 1441 | srManager.routingRulePopulator.processDoubleTaggedFilter( |
| 1442 | deviceId, outPort, outerVlan, innerVlan, true); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | /** |
| 1447 | * Revokes IP rules for a route when the next hop is double-tagged. |
| 1448 | * |
| 1449 | * @param deviceId device ID that next hop attaches to |
| 1450 | * @param prefix IP prefix of the route |
| 1451 | * @param hostMac MAC address of the next hop |
| 1452 | * @param innerVlan Inner Vlan ID of the next hop |
| 1453 | * @param outerVlan Outer Vlan ID of the next hop |
| 1454 | * @param outerTpid Outer TPID of the next hop |
| 1455 | * @param outPort port that the next hop attaches to |
| 1456 | */ |
| 1457 | void revokeDoubleTaggedRoute(DeviceId deviceId, IpPrefix prefix, MacAddress hostMac, VlanId innerVlan, |
| 1458 | VlanId outerVlan, EthType outerTpid, PortNumber outPort) { |
| 1459 | // Revoke route either if this node have the mastership (when device is available) or |
| 1460 | // if this node is the leader (even when device is unavailable) |
| 1461 | if (!srManager.mastershipService.isLocalMaster(deviceId)) { |
| 1462 | if (srManager.deviceService.isAvailable(deviceId)) { |
| 1463 | // Master node will revoke specified rule. |
| 1464 | log.debug("This node is not a master for {}, stop revoking route.", deviceId); |
| 1465 | return; |
| 1466 | } |
| 1467 | |
| 1468 | // isLocalMaster will return false when the device is unavailable. |
| 1469 | // Verify if this node is the leader in that case. |
| 1470 | NodeId leader = srManager.leadershipService.runForLeadership( |
| 1471 | deviceId.toString()).leaderNodeId(); |
| 1472 | if (!srManager.clusterService.getLocalNode().id().equals(leader)) { |
| 1473 | // Leader node will revoke specified rule. |
| 1474 | log.debug("This node is not a master for {}, stop revoking route.", deviceId); |
| 1475 | return; |
| 1476 | } |
| 1477 | } |
| 1478 | |
Charles Chan | 61c086d | 2019-07-26 17:46:15 -0700 | [diff] [blame] | 1479 | srManager.routingRulePopulator.revokeDoubleTaggedRoute(deviceId, prefix, hostMac, |
| 1480 | innerVlan, outerVlan, outerTpid, outPort); |
| 1481 | srManager.routingRulePopulator.processDoubleTaggedFilter(deviceId, outPort, outerVlan, innerVlan, false); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 1484 | /** |
| 1485 | * Purges seen before routes for a given device. |
| 1486 | * @param deviceId the device id |
| 1487 | */ |
| 1488 | void purgeSeenBeforeRoutes(DeviceId deviceId) { |
| 1489 | log.debug("Purging seen before routes having as target {}", deviceId); |
| 1490 | Set<Entry<DeviceId, DeviceId>> routesToPurge = seenBeforeRoutes.stream() |
| 1491 | .filter(entry -> entry.getValue().equals(deviceId)) |
| 1492 | .collect(Collectors.toSet()); |
| 1493 | routesToPurge.forEach(entry -> seenBeforeRoutes.remove(entry.getKey(), entry.getValue())); |
| 1494 | } |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 1495 | |
| 1496 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1497 | * Remove ECMP graph entry for the given device. Typically called when |
| 1498 | * device is no longer available. |
| 1499 | * |
| 1500 | * @param deviceId the device for which graphs need to be purged |
| 1501 | */ |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 1502 | void purgeEcmpGraph(DeviceId deviceId) { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1503 | statusLock.lock(); |
| 1504 | try { |
Saurav Das | 6430f41 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1505 | if (populationStatus == Status.STARTED) { |
| 1506 | log.warn("Previous rule population is not finished. Cannot" |
| 1507 | + " proceeed with purgeEcmpGraph for {}", deviceId); |
| 1508 | return; |
| 1509 | } |
| 1510 | log.debug("Updating ECMPspg for unavailable dev:{}", deviceId); |
| 1511 | currentEcmpSpgMap.remove(deviceId); |
| 1512 | if (updatedEcmpSpgMap != null) { |
| 1513 | updatedEcmpSpgMap.remove(deviceId); |
| 1514 | } |
| 1515 | } finally { |
| 1516 | statusLock.unlock(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1517 | } |
| 1518 | } |
| 1519 | |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1520 | /** |
| 1521 | * Attempts a full reroute of route-paths if topology has changed relatively |
| 1522 | * close to a mastership change event. Does not do a reroute if mastership |
| 1523 | * change is due to reasons other than a ONOS cluster event - for example a |
| 1524 | * call to balance-masters, or a switch up/down event. |
| 1525 | * |
| 1526 | * @param devId the device identifier for which mastership has changed |
| 1527 | * @param me the mastership event |
| 1528 | */ |
| 1529 | void checkFullRerouteForMasterChange(DeviceId devId, MastershipEvent me) { |
| 1530 | // give small delay to absorb mastership events that are caused by |
| 1531 | // device that has disconnected from cluster |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 1532 | executorServiceMstChg.schedule(new MasterChange(devId, me), |
| 1533 | MASTER_CHANGE_DELAY, TimeUnit.MILLISECONDS); |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | protected final class MasterChange implements Runnable { |
| 1537 | private DeviceId devId; |
| 1538 | private MastershipEvent me; |
| 1539 | private static final long CLUSTER_EVENT_THRESHOLD = 4500; // ms |
| 1540 | private static final long DEVICE_EVENT_THRESHOLD = 2000; // ms |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 1541 | private static final long EDGE_PORT_EVENT_THRESHOLD = 10000; //ms |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1542 | private static final long FULL_REROUTE_THRESHOLD = 10000; // ms |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1543 | |
| 1544 | MasterChange(DeviceId devId, MastershipEvent me) { |
| 1545 | this.devId = devId; |
| 1546 | this.me = me; |
| 1547 | } |
| 1548 | |
| 1549 | @Override |
| 1550 | public void run() { |
| 1551 | long lce = srManager.clusterListener.timeSinceLastClusterEvent(); |
| 1552 | boolean clusterEvent = lce < CLUSTER_EVENT_THRESHOLD; |
| 1553 | |
| 1554 | // ignore event for lost switch if cluster event hasn't happened - |
| 1555 | // device down event will handle it |
| 1556 | if ((me.roleInfo().master() == null |
| 1557 | || !srManager.deviceService.isAvailable(devId)) |
| 1558 | && !clusterEvent) { |
| 1559 | log.debug("Full reroute not required for lost device: {}/{} " |
| 1560 | + "clusterEvent/timeSince: {}/{}", |
| 1561 | devId, me.roleInfo(), clusterEvent, lce); |
| 1562 | return; |
| 1563 | } |
| 1564 | |
| 1565 | long update = srManager.deviceService.getLastUpdatedInstant(devId); |
| 1566 | long lde = Instant.now().toEpochMilli() - update; |
| 1567 | boolean deviceEvent = lde < DEVICE_EVENT_THRESHOLD; |
| 1568 | |
| 1569 | // ignore event for recently connected switch if cluster event hasn't |
| 1570 | // happened - link up events will handle it |
| 1571 | if (srManager.deviceService.isAvailable(devId) && deviceEvent |
| 1572 | && !clusterEvent) { |
| 1573 | log.debug("Full reroute not required for recently available" |
| 1574 | + " device: {}/{} deviceEvent/timeSince: {}/{} " |
| 1575 | + "clusterEvent/timeSince: {}/{}", |
| 1576 | devId, me.roleInfo(), deviceEvent, lde, clusterEvent, lce); |
| 1577 | return; |
| 1578 | } |
| 1579 | |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 1580 | long lepe = Instant.now().toEpochMilli() |
| 1581 | - srManager.lastEdgePortEvent.toEpochMilli(); |
| 1582 | boolean edgePortEvent = lepe < EDGE_PORT_EVENT_THRESHOLD; |
| 1583 | |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1584 | // if it gets here, then mastership change is likely due to onos |
| 1585 | // instance failure, or network partition in onos cluster |
| 1586 | // normally a mastership change like this does not require re-programming |
| 1587 | // but if topology changes happen at the same time then we may miss events |
| 1588 | if (!isRoutingStable() && clusterEvent) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 1589 | log.warn("Mastership changed for dev: {}/{} while programming route-paths " |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1590 | + "due to clusterEvent {} ms ago .. attempting full reroute", |
| 1591 | devId, me.roleInfo(), lce); |
| 1592 | if (srManager.mastershipService.isLocalMaster(devId)) { |
| 1593 | // old master could have died when populating filters |
| 1594 | populatePortAddressingRules(devId); |
| 1595 | } |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1596 | // old master could have died when creating groups |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1597 | // XXX right now we have no fine-grained way to only make changes |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1598 | // for the route paths affected by this device. Thus we do a |
| 1599 | // full reroute after purging all hash groups. We also try to do |
| 1600 | // it only once, irrespective of the number of devices |
| 1601 | // that changed mastership when their master instance died. |
| 1602 | long lfrr = Instant.now().toEpochMilli() - lastFullReroute.toEpochMilli(); |
| 1603 | boolean doFullReroute = lfrr > FULL_REROUTE_THRESHOLD; |
| 1604 | if (doFullReroute) { |
| 1605 | lastFullReroute = Instant.now(); |
| 1606 | for (Device dev : srManager.deviceService.getDevices()) { |
| 1607 | if (shouldProgram(dev.id())) { |
| 1608 | srManager.purgeHashedNextObjectiveStore(dev.id()); |
pier | f331a49 | 2020-01-07 15:39:39 +0100 | [diff] [blame] | 1609 | seenBeforeRoutes.removeAll(dev.id()); |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | // give small delay to ensure entire store is purged |
| 1613 | executorServiceFRR.schedule(new FullRerouteAfterPurge(), |
| 1614 | PURGE_DELAY, |
| 1615 | TimeUnit.MILLISECONDS); |
| 1616 | } else { |
| 1617 | log.warn("Full reroute attempted {} ms ago .. skipping", lfrr); |
| 1618 | } |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 1619 | |
| 1620 | } else if (edgePortEvent && clusterEvent) { |
| 1621 | log.warn("Mastership changed for dev: {}/{} due to clusterEvent {} ms ago " |
| 1622 | + "while edge-port event happened {} ms ago " |
| 1623 | + " .. reprogramming all edge-ports", |
| 1624 | devId, me.roleInfo(), lce, lepe); |
| 1625 | if (shouldProgram(devId)) { |
| 1626 | srManager.deviceService.getPorts(devId).stream() |
| 1627 | .filter(p -> srManager.interfaceService |
| 1628 | .isConfigured(new ConnectPoint(devId, p.number()))) |
| 1629 | .forEach(p -> srManager.processPortUpdated(devId, p)); |
| 1630 | } |
| 1631 | |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1632 | } else { |
| 1633 | log.debug("Stable route-paths .. full reroute not attempted for " |
| 1634 | + "mastership change {}/{} deviceEvent/timeSince: {}/{} " |
| 1635 | + "clusterEvent/timeSince: {}/{}", devId, me.roleInfo(), |
| 1636 | deviceEvent, lde, clusterEvent, lce); |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1641 | /** |
| 1642 | * Performs a full reroute of routing rules in all the switches. Assumes |
| 1643 | * caller has purged hash groups from the nextObjective store, otherwise |
| 1644 | * re-uses ones available in the store. |
| 1645 | */ |
| 1646 | protected final class FullRerouteAfterPurge implements Runnable { |
| 1647 | @Override |
| 1648 | public void run() { |
| 1649 | populateAllRoutingRules(); |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1654 | ////////////////////////////////////// |
| 1655 | // Routing helper methods and classes |
| 1656 | ////////////////////////////////////// |
| 1657 | |
| 1658 | /** |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1659 | * Computes set of affected routes due to failed link. Assumes previous ecmp |
| 1660 | * shortest-path graph exists for a switch in order to compute affected |
| 1661 | * routes. If such a graph does not exist, the method returns null. |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1662 | * |
| 1663 | * @param linkFail the failed link |
| 1664 | * @return the set of affected routes which may be empty if no routes were |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1665 | * affected |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1666 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1667 | private Set<ArrayList<DeviceId>> computeDamagedRoutes(Link linkFail) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1668 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 1669 | |
| 1670 | for (Device sw : srManager.deviceService.getDevices()) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1671 | log.debug("Computing the impacted routes for device {} due to link fail", |
| 1672 | sw.id()); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1673 | if (!shouldProgram(sw.id())) { |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1674 | lastProgrammed.remove(sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1675 | continue; |
| 1676 | } |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1677 | for (DeviceId rootSw : deviceAndItsPair(sw.id())) { |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1678 | // check for mastership change since last run |
| 1679 | if (!lastProgrammed.contains(sw.id())) { |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1680 | log.warn("New responsibility for this node to program dev:{}" |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1681 | + " ... nuking current ECMPspg", sw.id()); |
| 1682 | currentEcmpSpgMap.remove(sw.id()); |
| 1683 | } |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1684 | lastProgrammed.add(sw.id()); |
| 1685 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1686 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(rootSw); |
| 1687 | if (ecmpSpg == null) { |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1688 | log.warn("No existing ECMP graph for switch {}. Assuming " |
| 1689 | + "all route-paths have changed towards it.", rootSw); |
| 1690 | for (DeviceId targetSw : srManager.deviceConfiguration.getRouters()) { |
| 1691 | if (targetSw.equals(rootSw)) { |
| 1692 | continue; |
| 1693 | } |
| 1694 | routes.add(Lists.newArrayList(targetSw, rootSw)); |
| 1695 | log.debug("Impacted route:{}->{}", targetSw, rootSw); |
| 1696 | } |
| 1697 | continue; |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1698 | } |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1699 | |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1700 | if (log.isDebugEnabled()) { |
| 1701 | log.debug("Root switch: {}", rootSw); |
| 1702 | log.debug(" Current/Existing SPG: {}", ecmpSpg); |
| 1703 | log.debug(" New/Updated SPG: {}", updatedEcmpSpgMap.get(rootSw)); |
| 1704 | } |
| 1705 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> |
| 1706 | switchVia = ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 1707 | // figure out if the broken link affected any route-paths in this graph |
| 1708 | for (Integer itrIdx : switchVia.keySet()) { |
| 1709 | log.trace("Current/Exiting SPG Iterindex# {}", itrIdx); |
| 1710 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 1711 | switchVia.get(itrIdx); |
| 1712 | for (DeviceId targetSw : swViaMap.keySet()) { |
| 1713 | log.trace("TargetSwitch {} --> RootSwitch {}", |
| 1714 | targetSw, rootSw); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1715 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 1716 | log.trace(" Via:"); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1717 | via.forEach(e -> log.trace(" {}", e)); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1718 | } |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1719 | Set<ArrayList<DeviceId>> subLinks = |
| 1720 | computeLinks(targetSw, rootSw, swViaMap); |
| 1721 | for (ArrayList<DeviceId> alink: subLinks) { |
| 1722 | if ((alink.get(0).equals(linkFail.src().deviceId()) && |
| 1723 | alink.get(1).equals(linkFail.dst().deviceId())) |
| 1724 | || |
| 1725 | (alink.get(0).equals(linkFail.dst().deviceId()) && |
| 1726 | alink.get(1).equals(linkFail.src().deviceId()))) { |
| 1727 | log.debug("Impacted route:{}->{}", targetSw, rootSw); |
| 1728 | ArrayList<DeviceId> aRoute = new ArrayList<>(); |
| 1729 | aRoute.add(targetSw); // switch with rules to populate |
| 1730 | aRoute.add(rootSw); // towards this destination |
| 1731 | routes.add(aRoute); |
| 1732 | break; |
| 1733 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1734 | } |
| 1735 | } |
| 1736 | } |
Saurav Das | fe0b05e | 2017-08-14 16:44:43 -0700 | [diff] [blame] | 1737 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1738 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 1739 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1740 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1741 | return routes; |
| 1742 | } |
| 1743 | |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1744 | /** |
| 1745 | * Computes set of affected routes due to new links or failed switches. |
| 1746 | * |
Saurav Das | dc7f275 | 2018-03-18 21:28:15 -0700 | [diff] [blame] | 1747 | * @param failedSwitch deviceId of failed switch if any |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1748 | * @return the set of affected routes which may be empty if no routes were |
| 1749 | * affected |
| 1750 | */ |
Saurav Das | cea556f | 2018-03-05 14:37:16 -0800 | [diff] [blame] | 1751 | private Set<ArrayList<DeviceId>> computeRouteChange(DeviceId failedSwitch) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1752 | ImmutableSet.Builder<ArrayList<DeviceId>> changedRtBldr = |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1753 | ImmutableSet.builder(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1754 | |
| 1755 | for (Device sw : srManager.deviceService.getDevices()) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1756 | log.debug("Computing the impacted routes for device {}", sw.id()); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1757 | if (!shouldProgram(sw.id())) { |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1758 | lastProgrammed.remove(sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1759 | continue; |
| 1760 | } |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1761 | for (DeviceId rootSw : deviceAndItsPair(sw.id())) { |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1762 | if (log.isTraceEnabled()) { |
| 1763 | log.trace("Device links for dev: {}", rootSw); |
| 1764 | for (Link link: srManager.linkService.getDeviceLinks(rootSw)) { |
| 1765 | log.trace("{} -> {} ", link.src().deviceId(), |
| 1766 | link.dst().deviceId()); |
| 1767 | } |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 1768 | } |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1769 | // check for mastership change since last run |
| 1770 | if (!lastProgrammed.contains(sw.id())) { |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1771 | log.warn("New responsibility for this node to program dev:{}" |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1772 | + " ... nuking current ECMPspg", sw.id()); |
| 1773 | currentEcmpSpgMap.remove(sw.id()); |
| 1774 | } |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1775 | lastProgrammed.add(sw.id()); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1776 | EcmpShortestPathGraph currEcmpSpg = currentEcmpSpgMap.get(rootSw); |
| 1777 | if (currEcmpSpg == null) { |
| 1778 | log.debug("No existing ECMP graph for device {}.. adding self as " |
| 1779 | + "changed route", rootSw); |
| 1780 | changedRtBldr.add(Lists.newArrayList(rootSw)); |
| 1781 | continue; |
| 1782 | } |
| 1783 | EcmpShortestPathGraph newEcmpSpg = updatedEcmpSpgMap.get(rootSw); |
Saurav Das | debcf88 | 2018-04-06 20:16:01 -0700 | [diff] [blame] | 1784 | if (newEcmpSpg == null) { |
| 1785 | log.warn("Cannot find updated ECMP graph for dev:{}", rootSw); |
| 1786 | continue; |
| 1787 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1788 | if (log.isDebugEnabled()) { |
| 1789 | log.debug("Root switch: {}", rootSw); |
| 1790 | log.debug(" Current/Existing SPG: {}", currEcmpSpg); |
| 1791 | log.debug(" New/Updated SPG: {}", newEcmpSpg); |
| 1792 | } |
| 1793 | // first use the updated/new map to compare to current/existing map |
| 1794 | // as new links may have come up |
| 1795 | changedRtBldr.addAll(compareGraphs(newEcmpSpg, currEcmpSpg, rootSw)); |
| 1796 | // then use the current/existing map to compare to updated/new map |
| 1797 | // as switch may have been removed |
| 1798 | changedRtBldr.addAll(compareGraphs(currEcmpSpg, newEcmpSpg, rootSw)); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 1799 | } |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1800 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1801 | |
Saurav Das | cea556f | 2018-03-05 14:37:16 -0800 | [diff] [blame] | 1802 | // handle clearing state for a failed switch in case the switch does |
| 1803 | // not have a pair, or the pair is not available |
| 1804 | if (failedSwitch != null) { |
Charles Chan | 6dbcd25 | 2018-04-02 11:46:38 -0700 | [diff] [blame] | 1805 | Optional<DeviceId> pairDev = srManager.getPairDeviceId(failedSwitch); |
| 1806 | if (!pairDev.isPresent() || !srManager.deviceService.isAvailable(pairDev.get())) { |
Saurav Das | cea556f | 2018-03-05 14:37:16 -0800 | [diff] [blame] | 1807 | log.debug("Proxy Route changes to downed Sw:{}", failedSwitch); |
| 1808 | srManager.deviceService.getDevices().forEach(dev -> { |
| 1809 | if (!dev.id().equals(failedSwitch) && |
| 1810 | srManager.mastershipService.isLocalMaster(dev.id())) { |
| 1811 | log.debug(" : {}", dev.id()); |
| 1812 | changedRtBldr.add(Lists.newArrayList(dev.id(), failedSwitch)); |
| 1813 | } |
| 1814 | }); |
| 1815 | } |
| 1816 | } |
| 1817 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1818 | Set<ArrayList<DeviceId>> changedRoutes = changedRtBldr.build(); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1819 | for (ArrayList<DeviceId> route: changedRoutes) { |
| 1820 | log.debug("Route changes Target -> Root"); |
| 1821 | if (route.size() == 1) { |
| 1822 | log.debug(" : all -> {}", route.get(0)); |
| 1823 | } else { |
| 1824 | log.debug(" : {} -> {}", route.get(0), route.get(1)); |
| 1825 | } |
| 1826 | } |
| 1827 | return changedRoutes; |
| 1828 | } |
| 1829 | |
pier | 572d4a9 | 2019-04-25 18:51:51 +0200 | [diff] [blame] | 1830 | // Utility method to expands the route changes in two elements array using |
| 1831 | // the ECMP graph. Caller represents all to dst switch routes with an |
| 1832 | // array containing only the dst switch. |
| 1833 | private Set<ArrayList<DeviceId>> getExpandedRoutes(Set<ArrayList<DeviceId>> routeChanges) { |
| 1834 | Set<ArrayList<DeviceId>> changedRoutes = new HashSet<>(); |
| 1835 | // Ensure each routeChanges entry has two elements |
| 1836 | for (ArrayList<DeviceId> route : routeChanges) { |
| 1837 | if (route.size() == 1) { |
| 1838 | DeviceId dstSw = route.get(0); |
| 1839 | EcmpShortestPathGraph ec = updatedEcmpSpgMap.get(dstSw); |
| 1840 | if (ec == null) { |
| 1841 | log.warn("No graph found for {} .. aborting redoRouting", dstSw); |
| 1842 | return Collections.emptySet(); |
| 1843 | } |
| 1844 | ec.getAllLearnedSwitchesAndVia().keySet().forEach(key -> { |
| 1845 | ec.getAllLearnedSwitchesAndVia().get(key).keySet().forEach(target -> { |
| 1846 | changedRoutes.add(Lists.newArrayList(target, dstSw)); |
| 1847 | }); |
| 1848 | }); |
| 1849 | } else { |
| 1850 | DeviceId targetSw = route.get(0); |
| 1851 | DeviceId dstSw = route.get(1); |
| 1852 | changedRoutes.add(Lists.newArrayList(targetSw, dstSw)); |
| 1853 | } |
| 1854 | } |
| 1855 | return changedRoutes; |
| 1856 | } |
| 1857 | |
| 1858 | // Utility method to expands the route changes in two elements array using |
| 1859 | // the available devices. Caller represents all to dst switch routes with an |
| 1860 | // array containing only the dst switch. |
| 1861 | private Set<ArrayList<DeviceId>> getAllExpandedRoutes(Set<ArrayList<DeviceId>> routeChanges) { |
| 1862 | Set<ArrayList<DeviceId>> changedRoutes = new HashSet<>(); |
| 1863 | // Ensure each routeChanges entry has two elements |
| 1864 | for (ArrayList<DeviceId> route : routeChanges) { |
| 1865 | if (route.size() == 1) { |
| 1866 | // route-path changes are from everyone else to this switch |
| 1867 | DeviceId dstSw = route.get(0); |
| 1868 | srManager.deviceService.getAvailableDevices().forEach(sw -> { |
| 1869 | if (!sw.id().equals(dstSw)) { |
| 1870 | changedRoutes.add(Lists.newArrayList(sw.id(), dstSw)); |
| 1871 | } |
| 1872 | }); |
| 1873 | } else { |
| 1874 | changedRoutes.add(route); |
| 1875 | } |
| 1876 | } |
| 1877 | return changedRoutes; |
| 1878 | } |
| 1879 | |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1880 | /** |
| 1881 | * For the root switch, searches all the target nodes reachable in the base |
| 1882 | * graph, and compares paths to the ones in the comp graph. |
| 1883 | * |
| 1884 | * @param base the graph that is indexed for all reachable target nodes |
| 1885 | * from the root node |
| 1886 | * @param comp the graph that the base graph is compared to |
| 1887 | * @param rootSw both ecmp graphs are calculated for the root node |
| 1888 | * @return all the routes that have changed in the base graph |
| 1889 | */ |
| 1890 | private Set<ArrayList<DeviceId>> compareGraphs(EcmpShortestPathGraph base, |
| 1891 | EcmpShortestPathGraph comp, |
| 1892 | DeviceId rootSw) { |
| 1893 | ImmutableSet.Builder<ArrayList<DeviceId>> changedRoutesBuilder = |
| 1894 | ImmutableSet.builder(); |
| 1895 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> baseMap = |
| 1896 | base.getAllLearnedSwitchesAndVia(); |
| 1897 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> compMap = |
| 1898 | comp.getAllLearnedSwitchesAndVia(); |
| 1899 | for (Integer itrIdx : baseMap.keySet()) { |
| 1900 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> baseViaMap = |
| 1901 | baseMap.get(itrIdx); |
| 1902 | for (DeviceId targetSw : baseViaMap.keySet()) { |
| 1903 | ArrayList<ArrayList<DeviceId>> basePath = baseViaMap.get(targetSw); |
| 1904 | ArrayList<ArrayList<DeviceId>> compPath = getVia(compMap, targetSw); |
| 1905 | if ((compPath == null) || !basePath.equals(compPath)) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 1906 | log.trace("Impacted route:{} -> {}", targetSw, rootSw); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1907 | ArrayList<DeviceId> route = new ArrayList<>(); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1908 | route.add(targetSw); // switch with rules to populate |
| 1909 | route.add(rootSw); // towards this destination |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1910 | changedRoutesBuilder.add(route); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1911 | } |
| 1912 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 1913 | } |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1914 | return changedRoutesBuilder.build(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1917 | /** |
| 1918 | * Returns the ECMP paths traversed to reach the target switch. |
| 1919 | * |
| 1920 | * @param switchVia a per-iteration view of the ECMP graph for a root switch |
| 1921 | * @param targetSw the switch to reach from the root switch |
| 1922 | * @return the nodes traversed on ECMP paths to the target switch |
| 1923 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1924 | private ArrayList<ArrayList<DeviceId>> getVia(HashMap<Integer, HashMap<DeviceId, |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1925 | ArrayList<ArrayList<DeviceId>>>> switchVia, DeviceId targetSw) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1926 | for (Integer itrIdx : switchVia.keySet()) { |
| 1927 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 1928 | switchVia.get(itrIdx); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1929 | if (swViaMap.get(targetSw) == null) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1930 | continue; |
| 1931 | } else { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 1932 | return swViaMap.get(targetSw); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 1936 | return null; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1939 | /** |
| 1940 | * Utility method to break down a path from src to dst device into a collection |
| 1941 | * of links. |
| 1942 | * |
| 1943 | * @param src src device of the path |
| 1944 | * @param dst dst device of the path |
| 1945 | * @param viaMap path taken from src to dst device |
| 1946 | * @return collection of links in the path |
| 1947 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 1948 | private Set<ArrayList<DeviceId>> computeLinks(DeviceId src, |
| 1949 | DeviceId dst, |
| 1950 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> viaMap) { |
| 1951 | Set<ArrayList<DeviceId>> subLinks = Sets.newHashSet(); |
| 1952 | for (ArrayList<DeviceId> via : viaMap.get(src)) { |
| 1953 | DeviceId linkSrc = src; |
| 1954 | DeviceId linkDst = dst; |
| 1955 | for (DeviceId viaDevice: via) { |
| 1956 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 1957 | linkDst = viaDevice; |
| 1958 | link.add(linkSrc); |
| 1959 | link.add(linkDst); |
| 1960 | subLinks.add(link); |
| 1961 | linkSrc = viaDevice; |
| 1962 | } |
| 1963 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 1964 | link.add(linkSrc); |
| 1965 | link.add(dst); |
| 1966 | subLinks.add(link); |
| 1967 | } |
| 1968 | |
| 1969 | return subLinks; |
| 1970 | } |
| 1971 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1972 | /** |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1973 | * Determines whether this controller instance should program the |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1974 | * given {@code deviceId}, based on mastership and pairDeviceId if one exists. |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1975 | * <p> |
| 1976 | * Once an instance is elected, it will be the only instance responsible for programming |
| 1977 | * both devices in the pair until it goes down. |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1978 | * |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 1979 | * @param deviceId device identifier to consider for routing |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1980 | * @return true if current instance should handle the routing for given device |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 1981 | */ |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1982 | boolean shouldProgram(DeviceId deviceId) { |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 1983 | Boolean cached = shouldProgramCache.get(deviceId); |
| 1984 | if (cached != null) { |
Saurav Das | 00e553b | 2018-04-21 17:19:48 -0700 | [diff] [blame] | 1985 | log.debug("shouldProgram dev:{} cached:{}", deviceId, cached); |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 1986 | return cached; |
| 1987 | } |
| 1988 | |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1989 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(deviceId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1990 | |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1991 | NodeId currentNodeId = srManager.clusterService.getLocalNode().id(); |
| 1992 | NodeId masterNodeId = srManager.mastershipService.getMasterFor(deviceId); |
| 1993 | Optional<NodeId> pairMasterNodeId = pairDeviceId.map(srManager.mastershipService::getMasterFor); |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1994 | log.debug("Evaluate shouldProgram {}/pair={}. currentNodeId={}, master={}, pairMaster={}", |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 1995 | deviceId, pairDeviceId, currentNodeId, masterNodeId, pairMasterNodeId); |
| 1996 | |
| 1997 | // No pair device configured. Only handle when current instance is the master of the device |
| 1998 | if (!pairDeviceId.isPresent()) { |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 1999 | log.debug("No pair device. currentNodeId={}, master={}", currentNodeId, masterNodeId); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 2000 | return currentNodeId.equals(masterNodeId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 2001 | } |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 2002 | |
| 2003 | // Should not handle if current instance is not the master of either switch |
| 2004 | if (!currentNodeId.equals(masterNodeId) && |
| 2005 | !(pairMasterNodeId.isPresent() && currentNodeId.equals(pairMasterNodeId.get()))) { |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 2006 | log.debug("Current nodeId {} is neither the master of target device {} nor pair device {}", |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 2007 | currentNodeId, deviceId, pairDeviceId); |
| 2008 | return false; |
| 2009 | } |
| 2010 | |
| 2011 | Set<DeviceId> key = Sets.newHashSet(deviceId, pairDeviceId.get()); |
| 2012 | |
| 2013 | NodeId king = shouldProgram.compute(key, ((k, v) -> { |
| 2014 | if (v == null) { |
| 2015 | // There is no value in the map. Elect a node |
| 2016 | return elect(Lists.newArrayList(masterNodeId, pairMasterNodeId.orElse(null))); |
| 2017 | } else { |
| 2018 | if (v.equals(masterNodeId) || v.equals(pairMasterNodeId.orElse(null))) { |
| 2019 | // Use the node in the map if it is still alive and is a master of any of the two switches |
| 2020 | return v; |
| 2021 | } else { |
| 2022 | // Previously elected node is no longer the master of either switch. Re-elect a node. |
| 2023 | return elect(Lists.newArrayList(masterNodeId, pairMasterNodeId.orElse(null))); |
| 2024 | } |
| 2025 | } |
| 2026 | })); |
| 2027 | |
| 2028 | if (king != null) { |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 2029 | log.debug("{} is king, should handle routing for {}/pair={}", king, deviceId, pairDeviceId); |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 2030 | shouldProgramCache.put(deviceId, king.equals(currentNodeId)); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 2031 | return king.equals(currentNodeId); |
| 2032 | } else { |
| 2033 | log.error("Fail to elect a king for {}/pair={}. Abort.", deviceId, pairDeviceId); |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 2034 | shouldProgramCache.remove(deviceId); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 2035 | return false; |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | /** |
| 2040 | * Elects a node who should take responsibility of programming devices. |
| 2041 | * @param nodeIds list of candidate node ID |
| 2042 | * |
| 2043 | * @return NodeId of the node that gets elected, or null if none of the node can be elected |
| 2044 | */ |
| 2045 | private NodeId elect(List<NodeId> nodeIds) { |
| 2046 | // Remove all null elements. This could happen when some device has no master |
| 2047 | nodeIds.removeAll(Collections.singleton(null)); |
| 2048 | nodeIds.sort(null); |
| 2049 | return nodeIds.size() == 0 ? null : nodeIds.get(0); |
| 2050 | } |
| 2051 | |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 2052 | void invalidateShouldProgramCache(DeviceId deviceId) { |
| 2053 | shouldProgramCache.remove(deviceId); |
| 2054 | } |
| 2055 | |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 2056 | /** |
| 2057 | * Returns a set of device ID, containing given device and its pair device if exist. |
| 2058 | * |
| 2059 | * @param deviceId Device ID |
| 2060 | * @return a set of device ID, containing given device and its pair device if exist. |
| 2061 | */ |
| 2062 | private Set<DeviceId> deviceAndItsPair(DeviceId deviceId) { |
| 2063 | Set<DeviceId> ret = Sets.newHashSet(deviceId); |
| 2064 | srManager.getPairDeviceId(deviceId).ifPresent(ret::add); |
| 2065 | return ret; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 2068 | /** |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 2069 | * Returns the set of deviceIds which are the next hops from the targetSw |
| 2070 | * to the dstSw according to the latest ECMP spg. |
| 2071 | * |
| 2072 | * @param targetSw the switch for which the next-hops are desired |
| 2073 | * @param dstSw the switch to which the next-hops lead to from the targetSw |
| 2074 | * @return set of next hop deviceIds, could be empty if no next hops are found |
| 2075 | */ |
| 2076 | private Set<DeviceId> getNextHops(DeviceId targetSw, DeviceId dstSw) { |
| 2077 | boolean targetIsEdge = false; |
| 2078 | try { |
| 2079 | targetIsEdge = srManager.deviceConfiguration.isEdgeDevice(targetSw); |
| 2080 | } catch (DeviceConfigNotFoundException e) { |
| 2081 | log.warn(e.getMessage() + "Cannot determine if targetIsEdge {}.. " |
| 2082 | + "continuing to getNextHops", targetSw); |
| 2083 | } |
| 2084 | |
| 2085 | EcmpShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(dstSw); |
| 2086 | if (ecmpSpg == null) { |
| 2087 | log.debug("No ecmpSpg found for dstSw: {}", dstSw); |
| 2088 | return ImmutableSet.of(); |
| 2089 | } |
| 2090 | HashMap<Integer, |
| 2091 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 2092 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 2093 | for (Integer itrIdx : switchVia.keySet()) { |
| 2094 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 2095 | switchVia.get(itrIdx); |
| 2096 | for (DeviceId target : swViaMap.keySet()) { |
| 2097 | if (!target.equals(targetSw)) { |
| 2098 | continue; |
| 2099 | } |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 2100 | // optimization for spines to not use leaves to get |
| 2101 | // to a spine or other leaves. Also leaves should not use other |
| 2102 | // leaves to get to the destination |
| 2103 | if ((!targetIsEdge && itrIdx > 1) || targetIsEdge) { |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 2104 | boolean pathdevIsEdge = false; |
| 2105 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 2106 | log.debug("Evaluating next-hop in path: {}", via); |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 2107 | for (DeviceId pathdev : via) { |
| 2108 | try { |
| 2109 | pathdevIsEdge = srManager.deviceConfiguration |
| 2110 | .isEdgeDevice(pathdev); |
| 2111 | } catch (DeviceConfigNotFoundException e) { |
| 2112 | log.warn(e.getMessage()); |
| 2113 | } |
| 2114 | if (pathdevIsEdge) { |
Saurav Das | 68e1b6a | 2018-06-11 17:02:31 -0700 | [diff] [blame] | 2115 | log.debug("Avoiding {} hop path for targetSw:{}" |
Saurav Das | 9724186 | 2018-02-14 14:14:54 -0800 | [diff] [blame] | 2116 | + " --> dstSw:{} which goes through an edge" |
| 2117 | + " device {} in path {}", itrIdx, |
| 2118 | targetSw, dstSw, pathdev, via); |
| 2119 | return ImmutableSet.of(); |
| 2120 | } |
| 2121 | } |
| 2122 | } |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 2123 | } |
| 2124 | Set<DeviceId> nextHops = new HashSet<>(); |
| 2125 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 2126 | if (via.isEmpty()) { |
| 2127 | // the dstSw is the next-hop from the targetSw |
| 2128 | nextHops.add(dstSw); |
| 2129 | } else { |
| 2130 | // first elem is next-hop in each ECMP path |
| 2131 | nextHops.add(via.get(0)); |
| 2132 | } |
| 2133 | } |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 2134 | log.debug("target {} --> dst: {} has next-hops:{}", targetSw, |
| 2135 | dstSw, nextHops); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 2136 | return nextHops; |
| 2137 | } |
| 2138 | } |
Saurav Das | 4936839 | 2018-04-23 18:42:12 -0700 | [diff] [blame] | 2139 | log.debug("No next hops found for target:{} --> dst: {}", targetSw, dstSw); |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 2140 | return ImmutableSet.of(); //no next-hops found |
| 2141 | } |
| 2142 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 2143 | ////////////////////////////////////// |
| 2144 | // Filtering rule creation |
| 2145 | ////////////////////////////////////// |
| 2146 | |
| 2147 | /** |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 2148 | * Populates filtering rules for port, and punting rules |
| 2149 | * for gateway IPs, loopback IPs and arp/ndp traffic. |
| 2150 | * Should only be called by the master instance for this device/port. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 2151 | * |
| 2152 | * @param deviceId Switch ID to set the rules |
| 2153 | */ |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 2154 | void populatePortAddressingRules(DeviceId deviceId) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 2155 | // Although device is added, sometimes device store does not have the |
| 2156 | // ports for this device yet. It results in missing filtering rules in the |
| 2157 | // switch. We will attempt it a few times. If it still does not work, |
| 2158 | // user can manually repopulate using CLI command sr-reroute-network |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 2159 | PortFilterInfo firstRun = rulePopulator.populateVlanMacFilters(deviceId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 2160 | if (firstRun == null) { |
| 2161 | firstRun = new PortFilterInfo(0, 0, 0); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 2162 | } |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 2163 | executorService.schedule(new RetryFilters(deviceId, firstRun), |
| 2164 | RETRY_INTERVAL_MS, TimeUnit.MILLISECONDS); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | /** |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 2168 | * RetryFilters populates filtering objectives for a device and keeps retrying |
| 2169 | * till the number of ports filtered are constant for a predefined number |
| 2170 | * of attempts. |
| 2171 | */ |
| 2172 | protected final class RetryFilters implements Runnable { |
| 2173 | int constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; |
| 2174 | DeviceId devId; |
| 2175 | int counter; |
| 2176 | PortFilterInfo prevRun; |
| 2177 | |
| 2178 | private RetryFilters(DeviceId deviceId, PortFilterInfo previousRun) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 2179 | devId = deviceId; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 2180 | prevRun = previousRun; |
| 2181 | counter = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | @Override |
| 2185 | public void run() { |
Charles Chan | 077314e | 2017-06-22 14:27:17 -0700 | [diff] [blame] | 2186 | log.debug("RETRY FILTER ATTEMPT {} ** dev:{}", ++counter, devId); |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 2187 | PortFilterInfo thisRun = rulePopulator.populateVlanMacFilters(devId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 2188 | boolean sameResult = prevRun.equals(thisRun); |
| 2189 | log.debug("dev:{} prevRun:{} thisRun:{} sameResult:{}", devId, prevRun, |
| 2190 | thisRun, sameResult); |
Ray Milkey | 614352e | 2018-02-26 09:36:31 -0800 | [diff] [blame] | 2191 | if (thisRun == null || !sameResult || (--constantAttempts > 0)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 2192 | // exponentially increasing intervals for retries |
| 2193 | executorService.schedule(this, |
| 2194 | RETRY_INTERVAL_MS * (int) Math.pow(counter, RETRY_INTERVAL_SCALE), |
| 2195 | TimeUnit.MILLISECONDS); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 2196 | if (!sameResult) { |
| 2197 | constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; //reset |
| 2198 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 2199 | } |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 2200 | prevRun = (thisRun == null) ? prevRun : thisRun; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 2201 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 2202 | } |
pier | a994119 | 2019-04-24 16:12:47 +0200 | [diff] [blame] | 2203 | |
| 2204 | // Check jobs completion. It returns false if one of the job fails |
| 2205 | // and cancel the remaining |
| 2206 | private boolean checkJobs(List<Future<Boolean>> futures) { |
| 2207 | boolean completed = true; |
| 2208 | for (Future<Boolean> future : futures) { |
| 2209 | try { |
| 2210 | if (completed) { |
| 2211 | if (!future.get()) { |
| 2212 | completed = false; |
| 2213 | } |
| 2214 | } else { |
| 2215 | future.cancel(true); |
| 2216 | } |
| 2217 | } catch (InterruptedException | ExecutionException e) { |
| 2218 | completed = false; |
| 2219 | } |
| 2220 | } |
| 2221 | return completed; |
| 2222 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 2223 | } |