sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Address; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Prefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 23 | import org.onlab.packet.IpPrefix; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.DeviceId; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Link; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 28 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 29 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 33 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 34 | import static org.onlab.util.Tools.groupedThreads; |
| 35 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.HashSet; |
| 39 | import java.util.Set; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 40 | import java.util.concurrent.ScheduledExecutorService; |
| 41 | import java.util.concurrent.TimeUnit; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 42 | import java.util.concurrent.locks.Lock; |
| 43 | import java.util.concurrent.locks.ReentrantLock; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Default routing handler that is responsible for route computing and |
| 49 | * routing rule population. |
| 50 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 51 | public class DefaultRoutingHandler { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 52 | private static final int MAX_RETRY_ATTEMPTS = 25; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 53 | private static final String ECMPSPG_MISSING = "ECMP shortest path graph not found"; |
| 54 | private static Logger log = LoggerFactory.getLogger(DefaultRoutingHandler.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 55 | |
| 56 | private SegmentRoutingManager srManager; |
| 57 | private RoutingRulePopulator rulePopulator; |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 58 | private HashMap<DeviceId, EcmpShortestPathGraph> currentEcmpSpgMap; |
| 59 | private HashMap<DeviceId, EcmpShortestPathGraph> updatedEcmpSpgMap; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 60 | private DeviceConfiguration config; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 61 | private final Lock statusLock = new ReentrantLock(); |
| 62 | private volatile Status populationStatus; |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 63 | private ScheduledExecutorService executorService |
| 64 | = newScheduledThreadPool(1, groupedThreads("RoutingHandler", "retry-%d", log)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Represents the default routing population status. |
| 68 | */ |
| 69 | public enum Status { |
| 70 | // population process is not started yet. |
| 71 | IDLE, |
| 72 | |
| 73 | // population process started. |
| 74 | STARTED, |
| 75 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 76 | // population process was aborted due to errors, mostly for groups not |
| 77 | // found. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 78 | ABORTED, |
| 79 | |
| 80 | // population process was finished successfully. |
| 81 | SUCCEEDED |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Creates a DefaultRoutingHandler object. |
| 86 | * |
| 87 | * @param srManager SegmentRoutingManager object |
| 88 | */ |
| 89 | public DefaultRoutingHandler(SegmentRoutingManager srManager) { |
| 90 | this.srManager = srManager; |
| 91 | this.rulePopulator = checkNotNull(srManager.routingRulePopulator); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 92 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 93 | this.populationStatus = Status.IDLE; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 94 | this.currentEcmpSpgMap = Maps.newHashMap(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Populates all routing rules to all connected routers, including default |
| 99 | * routing rules, adjacency rules, and policy rules if any. |
| 100 | * |
| 101 | * @return true if it succeeds in populating all rules, otherwise false |
| 102 | */ |
| 103 | public boolean populateAllRoutingRules() { |
| 104 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 105 | statusLock.lock(); |
| 106 | try { |
| 107 | populationStatus = Status.STARTED; |
| 108 | rulePopulator.resetCounter(); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 109 | log.info("Starting to populate segment-routing rules"); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 110 | log.debug("populateAllRoutingRules: populationStatus is STARTED"); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 111 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 112 | for (Device sw : srManager.deviceService.getDevices()) { |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 113 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 114 | log.debug("populateAllRoutingRules: skipping device {}...we are not master", |
| 115 | sw.id()); |
| 116 | continue; |
| 117 | } |
| 118 | |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 119 | EcmpShortestPathGraph ecmpSpg = new EcmpShortestPathGraph(sw.id(), srManager); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 120 | if (!populateEcmpRoutingRules(sw.id(), ecmpSpg, ImmutableSet.of())) { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 121 | log.debug("populateAllRoutingRules: populationStatus is ABORTED"); |
| 122 | populationStatus = Status.ABORTED; |
| 123 | log.debug("Abort routing rule population"); |
| 124 | return false; |
| 125 | } |
| 126 | currentEcmpSpgMap.put(sw.id(), ecmpSpg); |
| 127 | |
| 128 | // TODO: Set adjacency routing rule for all switches |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 129 | } |
| 130 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 131 | log.debug("populateAllRoutingRules: populationStatus is SUCCEEDED"); |
| 132 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 133 | log.info("Completed routing rule population. Total # of rules pushed : {}", |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 134 | rulePopulator.getCounter()); |
| 135 | return true; |
| 136 | } finally { |
| 137 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 138 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 139 | } |
| 140 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Populates the routing rules according to the route changes due to the link |
| 143 | * failure or link add. It computes the routes changed due to the link changes and |
| 144 | * repopulates the rules only for the routes. |
| 145 | * |
| 146 | * @param linkFail link failed, null for link added |
| 147 | * @return true if it succeeds to populate all rules, false otherwise |
| 148 | */ |
| 149 | public boolean populateRoutingRulesForLinkStatusChange(Link linkFail) { |
| 150 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 151 | statusLock.lock(); |
| 152 | try { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 153 | |
| 154 | if (populationStatus == Status.STARTED) { |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 155 | log.warn("Previous rule population is not finished."); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 159 | // Take the snapshots of the links |
| 160 | updatedEcmpSpgMap = new HashMap<>(); |
| 161 | for (Device sw : srManager.deviceService.getDevices()) { |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 162 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 163 | continue; |
| 164 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 165 | EcmpShortestPathGraph ecmpSpgUpdated = |
| 166 | new EcmpShortestPathGraph(sw.id(), srManager); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 167 | updatedEcmpSpgMap.put(sw.id(), ecmpSpgUpdated); |
| 168 | } |
| 169 | |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 170 | log.info("Starts rule population from link change"); |
| 171 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 172 | Set<ArrayList<DeviceId>> routeChanges; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 173 | log.trace("populateRoutingRulesForLinkStatusChange: " |
| 174 | + "populationStatus is STARTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 175 | populationStatus = Status.STARTED; |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 176 | // optimized re-routing |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 177 | if (linkFail == null) { |
| 178 | // Compare all routes of existing ECMP SPG with the new ones |
| 179 | routeChanges = computeRouteChange(); |
| 180 | } else { |
| 181 | // Compare existing ECMP SPG only with the link removed |
| 182 | routeChanges = computeDamagedRoutes(linkFail); |
| 183 | } |
| 184 | |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 185 | // null routeChanges indicates that full re-routing is required |
| 186 | if (routeChanges == null) { |
| 187 | return populateAllRoutingRules(); |
| 188 | } |
| 189 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 190 | if (routeChanges.isEmpty()) { |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 191 | log.info("No route changes for the link status change"); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 192 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 193 | populationStatus = Status.SUCCEEDED; |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | if (repopulateRoutingRulesForRoutes(routeChanges)) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 198 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 199 | populationStatus = Status.SUCCEEDED; |
| 200 | log.info("Complete to repopulate the rules. # of rules populated : {}", |
| 201 | rulePopulator.getCounter()); |
| 202 | return true; |
| 203 | } else { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 204 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is ABORTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 205 | populationStatus = Status.ABORTED; |
| 206 | log.warn("Failed to repopulate the rules."); |
| 207 | return false; |
| 208 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 209 | } finally { |
| 210 | statusLock.unlock(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | private boolean repopulateRoutingRulesForRoutes(Set<ArrayList<DeviceId>> routes) { |
| 215 | rulePopulator.resetCounter(); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 216 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> routesBydevice = |
| 217 | new HashMap<>(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 218 | for (ArrayList<DeviceId> link: routes) { |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 219 | // When only the source device is defined, reinstall routes to all other devices |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 220 | if (link.size() == 1) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 221 | log.trace("repopulateRoutingRulesForRoutes: running ECMP graph for device {}", link.get(0)); |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 222 | EcmpShortestPathGraph ecmpSpg = new EcmpShortestPathGraph(link.get(0), srManager); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 223 | if (populateEcmpRoutingRules(link.get(0), ecmpSpg, ImmutableSet.of())) { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 224 | log.debug("Populating flow rules from all to dest:{} is successful", |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 225 | link.get(0)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 226 | currentEcmpSpgMap.put(link.get(0), ecmpSpg); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 227 | } else { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 228 | log.warn("Failed to populate the flow rules from all to dest:{}", link.get(0)); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 229 | return false; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 230 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 231 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 232 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 233 | routesBydevice.get(link.get(1)); |
| 234 | if (deviceRoutes == null) { |
| 235 | deviceRoutes = new ArrayList<>(); |
| 236 | routesBydevice.put(link.get(1), deviceRoutes); |
| 237 | } |
| 238 | deviceRoutes.add(link); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | for (DeviceId impactedDevice : routesBydevice.keySet()) { |
| 243 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 244 | routesBydevice.get(impactedDevice); |
| 245 | for (ArrayList<DeviceId> link: deviceRoutes) { |
| 246 | log.debug("repopulate RoutingRules For Routes {} -> {}", |
| 247 | link.get(0), link.get(1)); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 248 | DeviceId src = link.get(0); |
| 249 | DeviceId dst = link.get(1); |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 250 | EcmpShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(dst); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 251 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 252 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 253 | for (Integer itrIdx : switchVia.keySet()) { |
| 254 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 255 | switchVia.get(itrIdx); |
| 256 | for (DeviceId targetSw : swViaMap.keySet()) { |
| 257 | if (!targetSw.equals(src)) { |
| 258 | continue; |
| 259 | } |
| 260 | Set<DeviceId> nextHops = new HashSet<>(); |
| 261 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 262 | if (via.isEmpty()) { |
| 263 | nextHops.add(dst); |
| 264 | } else { |
| 265 | nextHops.add(via.get(0)); |
| 266 | } |
| 267 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 268 | if (!populateEcmpRoutingRulePartial(targetSw, dst, |
| 269 | nextHops, ImmutableSet.of())) { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 270 | return false; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 271 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 272 | log.debug("Populating flow rules from {} to {} is successful", |
| 273 | targetSw, dst); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 274 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 275 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 276 | //currentEcmpSpgMap.put(dst, ecmpSpg); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 277 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 278 | //Only if all the flows for all impacted routes to a |
| 279 | //specific target are pushed successfully, update the |
| 280 | //ECMP graph for that target. (Or else the next event |
| 281 | //would not see any changes in the ECMP graphs) |
| 282 | currentEcmpSpgMap.put(impactedDevice, |
| 283 | updatedEcmpSpgMap.get(impactedDevice)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Computes set of affected ECMP routes due to failed link. Assumes |
| 290 | * previous ecmp shortest-path graph exists for a switch in order to compute |
| 291 | * affected routes. If such a graph does not exist, the method returns null. |
| 292 | * |
| 293 | * @param linkFail the failed link |
| 294 | * @return the set of affected routes which may be empty if no routes were |
| 295 | * affected, or null if no previous ecmp spg was found for comparison |
| 296 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 297 | private Set<ArrayList<DeviceId>> computeDamagedRoutes(Link linkFail) { |
| 298 | |
| 299 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 300 | |
| 301 | for (Device sw : srManager.deviceService.getDevices()) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 302 | log.debug("Computing the impacted routes for device {} due to link fail", |
| 303 | sw.id()); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 304 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 305 | log.debug("No mastership for {} .. skipping route optimization", |
| 306 | sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 307 | continue; |
| 308 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 309 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 310 | if (ecmpSpg == null) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 311 | log.warn("No existing ECMP graph for switch {}. Aborting optimized" |
| 312 | + " rerouting and opting for full-reroute", sw.id()); |
| 313 | return null; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 314 | } |
| 315 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 316 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 317 | for (Integer itrIdx : switchVia.keySet()) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 318 | log.trace("Iterindex# {}", itrIdx); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 319 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 320 | switchVia.get(itrIdx); |
| 321 | for (DeviceId targetSw : swViaMap.keySet()) { |
| 322 | DeviceId destSw = sw.id(); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 323 | if (log.isTraceEnabled()) { |
| 324 | log.trace("TargetSwitch {} --> RootSwitch {}", targetSw, destSw); |
| 325 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 326 | log.trace(" Via:"); |
| 327 | via.forEach(e -> { log.trace(" {}", e); }); |
| 328 | } |
| 329 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 330 | Set<ArrayList<DeviceId>> subLinks = |
| 331 | computeLinks(targetSw, destSw, swViaMap); |
| 332 | for (ArrayList<DeviceId> alink: subLinks) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 333 | if ((alink.get(0).equals(linkFail.src().deviceId()) && |
| 334 | alink.get(1).equals(linkFail.dst().deviceId())) |
| 335 | || |
| 336 | (alink.get(0).equals(linkFail.dst().deviceId()) && |
| 337 | alink.get(1).equals(linkFail.src().deviceId()))) { |
| 338 | log.debug("Impacted route:{}->{}", targetSw, destSw); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 339 | ArrayList<DeviceId> aRoute = new ArrayList<>(); |
| 340 | aRoute.add(targetSw); |
| 341 | aRoute.add(destSw); |
| 342 | routes.add(aRoute); |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 348 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | return routes; |
| 352 | } |
| 353 | |
| 354 | private Set<ArrayList<DeviceId>> computeRouteChange() { |
| 355 | |
| 356 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 357 | |
| 358 | for (Device sw : srManager.deviceService.getDevices()) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 359 | log.debug("Computing the impacted routes for device {}", sw.id()); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 360 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 361 | log.debug("No mastership for {} ... skipping route optimization", |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 362 | sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 363 | continue; |
| 364 | } |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 365 | if (log.isTraceEnabled()) { |
| 366 | log.trace("link of {} - ", sw.id()); |
| 367 | for (Link link: srManager.linkService.getDeviceLinks(sw.id())) { |
| 368 | log.trace("{} -> {} ", link.src().deviceId(), link.dst().deviceId()); |
| 369 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 370 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 371 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 372 | if (ecmpSpg == null) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 373 | log.debug("No existing ECMP graph for device {}", sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 374 | ArrayList<DeviceId> route = new ArrayList<>(); |
| 375 | route.add(sw.id()); |
| 376 | routes.add(route); |
| 377 | continue; |
| 378 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 379 | EcmpShortestPathGraph newEcmpSpg = updatedEcmpSpgMap.get(sw.id()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 380 | //currentEcmpSpgMap.put(sw.id(), newEcmpSpg); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 381 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 382 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 383 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchViaUpdated = |
| 384 | newEcmpSpg.getAllLearnedSwitchesAndVia(); |
| 385 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 386 | for (Integer itrIdx : switchViaUpdated.keySet()) { |
| 387 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMapUpdated = |
| 388 | switchViaUpdated.get(itrIdx); |
| 389 | for (DeviceId srcSw : swViaMapUpdated.keySet()) { |
| 390 | ArrayList<ArrayList<DeviceId>> viaUpdated = swViaMapUpdated.get(srcSw); |
| 391 | ArrayList<ArrayList<DeviceId>> via = getVia(switchVia, srcSw); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 392 | if ((via == null) || !viaUpdated.equals(via)) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 393 | log.debug("Impacted route:{} -> {}", srcSw, sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 394 | ArrayList<DeviceId> route = new ArrayList<>(); |
| 395 | route.add(srcSw); |
| 396 | route.add(sw.id()); |
| 397 | routes.add(route); |
| 398 | } |
| 399 | } |
| 400 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 401 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 402 | |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 403 | if (log.isTraceEnabled()) { |
| 404 | for (ArrayList<DeviceId> link: routes) { |
| 405 | log.trace("Route changes - "); |
| 406 | if (link.size() == 1) { |
| 407 | log.trace(" : all -> {}", link.get(0)); |
| 408 | } else { |
| 409 | log.trace(" : {} -> {}", link.get(0), link.get(1)); |
| 410 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 411 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 412 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 413 | return routes; |
| 414 | } |
| 415 | |
| 416 | private ArrayList<ArrayList<DeviceId>> getVia(HashMap<Integer, HashMap<DeviceId, |
| 417 | ArrayList<ArrayList<DeviceId>>>> switchVia, DeviceId srcSw) { |
| 418 | for (Integer itrIdx : switchVia.keySet()) { |
| 419 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 420 | switchVia.get(itrIdx); |
| 421 | if (swViaMap.get(srcSw) == null) { |
| 422 | continue; |
| 423 | } else { |
| 424 | return swViaMap.get(srcSw); |
| 425 | } |
| 426 | } |
| 427 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 428 | return null; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | private Set<ArrayList<DeviceId>> computeLinks(DeviceId src, |
| 432 | DeviceId dst, |
| 433 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> viaMap) { |
| 434 | Set<ArrayList<DeviceId>> subLinks = Sets.newHashSet(); |
| 435 | for (ArrayList<DeviceId> via : viaMap.get(src)) { |
| 436 | DeviceId linkSrc = src; |
| 437 | DeviceId linkDst = dst; |
| 438 | for (DeviceId viaDevice: via) { |
| 439 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 440 | linkDst = viaDevice; |
| 441 | link.add(linkSrc); |
| 442 | link.add(linkDst); |
| 443 | subLinks.add(link); |
| 444 | linkSrc = viaDevice; |
| 445 | } |
| 446 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 447 | link.add(linkSrc); |
| 448 | link.add(dst); |
| 449 | subLinks.add(link); |
| 450 | } |
| 451 | |
| 452 | return subLinks; |
| 453 | } |
| 454 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 455 | /** |
| 456 | * Populate ECMP rules for subnets from all switches to destination. |
| 457 | * |
| 458 | * @param destSw Device ID of destination switch |
| 459 | * @param ecmpSPG ECMP shortest path graph |
| 460 | * @param subnets Subnets to be populated. If empty, populate all configured subnets. |
| 461 | * @return true if succeed |
| 462 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 463 | private boolean populateEcmpRoutingRules(DeviceId destSw, |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 464 | EcmpShortestPathGraph ecmpSPG, |
| 465 | Set<Ip4Prefix> subnets) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 466 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 467 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = ecmpSPG |
| 468 | .getAllLearnedSwitchesAndVia(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 469 | for (Integer itrIdx : switchVia.keySet()) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 470 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = switchVia |
| 471 | .get(itrIdx); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 472 | for (DeviceId targetSw : swViaMap.keySet()) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 473 | Set<DeviceId> nextHops = new HashSet<>(); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 474 | log.debug("** Iter: {} root: {} target: {}", itrIdx, destSw, targetSw); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 475 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 476 | if (via.isEmpty()) { |
| 477 | nextHops.add(destSw); |
| 478 | } else { |
| 479 | nextHops.add(via.get(0)); |
| 480 | } |
| 481 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 482 | if (!populateEcmpRoutingRulePartial(targetSw, destSw, nextHops, subnets)) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 483 | return false; |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return true; |
| 489 | } |
| 490 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 491 | /** |
| 492 | * Populate ECMP rules for subnets from target to destination via nexthops. |
| 493 | * |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 494 | * @param targetSw Device ID of target switch in which rules will be programmed |
| 495 | * @param destSw Device ID of final destination switch to which the rules will forward |
| 496 | * @param nextHops List of next hops via which destSw will be reached |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 497 | * @param subnets Subnets to be populated. If empty, populate all configured subnets. |
| 498 | * @return true if succeed |
| 499 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 500 | private boolean populateEcmpRoutingRulePartial(DeviceId targetSw, |
| 501 | DeviceId destSw, |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 502 | Set<DeviceId> nextHops, |
| 503 | Set<Ip4Prefix> subnets) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 504 | boolean result; |
| 505 | |
| 506 | if (nextHops.isEmpty()) { |
| 507 | nextHops.add(destSw); |
| 508 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 509 | // If both target switch and dest switch are edge routers, then set IP |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 510 | // rule for both subnet and router IP. |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 511 | boolean targetIsEdge; |
| 512 | boolean destIsEdge; |
| 513 | Ip4Address destRouterIp; |
| 514 | |
| 515 | try { |
| 516 | targetIsEdge = config.isEdgeDevice(targetSw); |
| 517 | destIsEdge = config.isEdgeDevice(destSw); |
| 518 | destRouterIp = config.getRouterIp(destSw); |
| 519 | } catch (DeviceConfigNotFoundException e) { |
| 520 | log.warn(e.getMessage() + " Aborting populateEcmpRoutingRulePartial."); |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | if (targetIsEdge && destIsEdge) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 525 | subnets = (subnets != null && !subnets.isEmpty()) ? subnets : config.getSubnets(destSw); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 526 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for subnets {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 527 | targetSw, destSw, subnets); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 528 | result = rulePopulator.populateIpRuleForSubnet(targetSw, subnets, |
| 529 | destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 530 | if (!result) { |
| 531 | return false; |
| 532 | } |
| 533 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 534 | Ip4Address routerIp = destRouterIp; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 535 | IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 536 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for router IP {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 537 | targetSw, destSw, routerIpPrefix); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 538 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 539 | if (!result) { |
| 540 | return false; |
| 541 | } |
| 542 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 543 | } else if (targetIsEdge) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 544 | // If the target switch is an edge router, then set IP rules for the router IP. |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 545 | Ip4Address routerIp = destRouterIp; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 546 | IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 547 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for router IP {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 548 | targetSw, destSw, routerIpPrefix); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 549 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 550 | if (!result) { |
| 551 | return false; |
| 552 | } |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 553 | } |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 554 | // Populates MPLS rules to all routers |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 555 | log.debug("* populateEcmpRoutingRulePartial in device{} towards {} for all MPLS rules", |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 556 | targetSw, destSw); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 557 | result = rulePopulator.populateMplsRule(targetSw, destSw, nextHops); |
| 558 | if (!result) { |
| 559 | return false; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 560 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 561 | return true; |
| 562 | } |
| 563 | |
| 564 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 565 | * Populates filtering rules for permitting Router DstMac and VLAN. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 566 | * |
| 567 | * @param deviceId Switch ID to set the rules |
| 568 | */ |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 569 | public void populatePortAddressingRules(DeviceId deviceId) { |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 570 | rulePopulator.populateRouterIpPunts(deviceId); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 571 | |
| 572 | // Although device is added, sometimes device store does not have the |
| 573 | // ports for this device yet. It results in missing filtering rules in the |
| 574 | // switch. We will attempt it a few times. If it still does not work, |
| 575 | // user can manually repopulate using CLI command sr-reroute-network |
| 576 | boolean success = rulePopulator.populateRouterMacVlanFilters(deviceId); |
| 577 | if (!success) { |
| 578 | executorService.schedule(new RetryFilters(deviceId), 200, TimeUnit.MILLISECONDS); |
| 579 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 583 | * Start the flow rule population process if it was never started. The |
| 584 | * process finishes successfully when all flow rules are set and stops with |
| 585 | * ABORTED status when any groups required for flows is not set yet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 586 | */ |
| 587 | public void startPopulationProcess() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 588 | statusLock.lock(); |
| 589 | try { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 590 | if (populationStatus == Status.IDLE |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 591 | || populationStatus == Status.SUCCEEDED |
| 592 | || populationStatus == Status.ABORTED) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 593 | populationStatus = Status.STARTED; |
| 594 | populateAllRoutingRules(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 595 | } else { |
| 596 | log.warn("Not initiating startPopulationProcess as populationStatus is {}", |
| 597 | populationStatus); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 598 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 599 | } finally { |
| 600 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * Resume the flow rule population process if it was aborted for any reason. |
| 606 | * Mostly the process is aborted when the groups required are not set yet. |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 607 | * XXX is this called? |
| 608 | * |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 609 | */ |
| 610 | public void resumePopulationProcess() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 611 | statusLock.lock(); |
| 612 | try { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 613 | if (populationStatus == Status.ABORTED) { |
| 614 | populationStatus = Status.STARTED; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 615 | // TODO: we need to restart from the point aborted instead of |
| 616 | // restarting. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 617 | populateAllRoutingRules(); |
| 618 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 619 | } finally { |
| 620 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 623 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 624 | /** |
| 625 | * Populate rules of given subnet at given location. |
| 626 | * |
| 627 | * @param cp connect point of the subnet being added |
| 628 | * @param subnets subnet being added |
| 629 | * @return true if succeed |
| 630 | */ |
| 631 | protected boolean populateSubnet(ConnectPoint cp, Set<Ip4Prefix> subnets) { |
| 632 | statusLock.lock(); |
| 633 | try { |
| 634 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(cp.deviceId()); |
| 635 | if (ecmpSpg == null) { |
| 636 | log.warn("Fail to populating subnet {}: {}", subnets, ECMPSPG_MISSING); |
| 637 | return false; |
| 638 | } |
| 639 | return populateEcmpRoutingRules(cp.deviceId(), ecmpSpg, subnets); |
| 640 | } finally { |
| 641 | statusLock.unlock(); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Revoke rules of given subnet at given location. |
| 647 | * |
| 648 | * @param subnets subnet being removed |
| 649 | * @return true if succeed |
| 650 | */ |
| 651 | protected boolean revokeSubnet(Set<Ip4Prefix> subnets) { |
| 652 | statusLock.lock(); |
| 653 | try { |
| 654 | return srManager.routingRulePopulator.revokeIpRuleForSubnet(subnets); |
| 655 | } finally { |
| 656 | statusLock.unlock(); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | protected void purgeEcmpGraph(DeviceId deviceId) { |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 661 | currentEcmpSpgMap.remove(deviceId); |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 662 | if (updatedEcmpSpgMap != null) { |
| 663 | updatedEcmpSpgMap.remove(deviceId); |
| 664 | } |
Pier Ventre | 6d59389 | 2016-09-13 21:33:40 -0700 | [diff] [blame] | 665 | this.populateRoutingRulesForLinkStatusChange(null); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 666 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 667 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 668 | private final class RetryFilters implements Runnable { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 669 | int attempts = MAX_RETRY_ATTEMPTS; |
| 670 | DeviceId devId; |
| 671 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 672 | private RetryFilters(DeviceId deviceId) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 673 | devId = deviceId; |
| 674 | } |
| 675 | |
| 676 | @Override |
| 677 | public void run() { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 678 | log.info("RETRY FILTER ATTEMPT# {} for dev:{}", |
| 679 | MAX_RETRY_ATTEMPTS - attempts, devId); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 680 | boolean success = rulePopulator.populateRouterMacVlanFilters(devId); |
| 681 | if (!success && --attempts > 0) { |
| 682 | executorService.schedule(this, 200, TimeUnit.MILLISECONDS); |
| 683 | } else if (attempts == 0) { |
| 684 | log.error("Unable to populate MacVlan filters in dev:{}", devId); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | } |
| 689 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 690 | } |