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