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