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