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