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 | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 55 | private static final int MAX_CONSTANT_RETRY_ATTEMPTS = 5; |
| 56 | private static final int RETRY_INTERVAL_MS = 250; |
| 57 | private static final int RETRY_INTERVAL_SCALE = 1; |
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:"); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 338 | via.forEach(e -> log.trace(" {}", e)); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 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, |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 508 | Set<IpPrefix> 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, |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 546 | Set<IpPrefix> 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; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 556 | Ip4Address destRouterIpv4; |
| 557 | Ip6Address destRouterIpv6; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 558 | |
| 559 | try { |
| 560 | targetIsEdge = config.isEdgeDevice(targetSw); |
| 561 | destIsEdge = config.isEdgeDevice(destSw); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 562 | destRouterIpv4 = config.getRouterIpv4(destSw); |
| 563 | destRouterIpv6 = config.getRouterIpv6(destSw); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 564 | } catch (DeviceConfigNotFoundException e) { |
| 565 | log.warn(e.getMessage() + " Aborting populateEcmpRoutingRulePartial."); |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | if (targetIsEdge && destIsEdge) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 570 | subnets = (subnets != null && !subnets.isEmpty()) ? subnets : config.getSubnets(destSw); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 571 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for subnets {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 572 | targetSw, destSw, subnets); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 573 | result = rulePopulator.populateIpRuleForSubnet(targetSw, subnets, |
| 574 | destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 575 | if (!result) { |
| 576 | return false; |
| 577 | } |
| 578 | |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 579 | IpPrefix routerIpPrefix = destRouterIpv4.toIpPrefix(); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 580 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for router IP {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 581 | targetSw, destSw, routerIpPrefix); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 582 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 583 | if (!result) { |
| 584 | return false; |
| 585 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 586 | /* |
| 587 | * If present we deal with IPv6 loopback. |
| 588 | */ |
| 589 | if (destRouterIpv6 != null) { |
| 590 | routerIpPrefix = destRouterIpv6.toIpPrefix(); |
| 591 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for v6 router IP {}", |
| 592 | targetSw, destSw, routerIpPrefix); |
| 593 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
| 594 | if (!result) { |
| 595 | return false; |
| 596 | } |
| 597 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 598 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 599 | } else if (targetIsEdge) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 600 | // 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] | 601 | IpPrefix routerIpPrefix = destRouterIpv4.toIpPrefix(); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 602 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for router IP {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 603 | targetSw, destSw, routerIpPrefix); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 604 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 605 | if (!result) { |
| 606 | return false; |
| 607 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 608 | if (destRouterIpv6 != null) { |
| 609 | routerIpPrefix = destRouterIpv6.toIpPrefix(); |
| 610 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for v6 router IP {}", |
| 611 | targetSw, destSw, routerIpPrefix); |
| 612 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
| 613 | if (!result) { |
| 614 | return false; |
| 615 | } |
| 616 | } |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 617 | } |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 618 | // Populates MPLS rules to all routers |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 619 | log.debug("* populateEcmpRoutingRulePartial in device{} towards {} for all MPLS rules", |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 620 | targetSw, destSw); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 621 | result = rulePopulator.populateMplsRule(targetSw, destSw, nextHops, destRouterIpv4); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 622 | if (!result) { |
| 623 | return false; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 624 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 625 | /* |
| 626 | * If present we will populate the MPLS rules for the IPv6 sid. |
| 627 | */ |
| 628 | if (destRouterIpv6 != null) { |
| 629 | result = rulePopulator.populateMplsRule(targetSw, destSw, nextHops, destRouterIpv6); |
| 630 | if (!result) { |
| 631 | return false; |
| 632 | } |
| 633 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 634 | return true; |
| 635 | } |
| 636 | |
| 637 | /** |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 638 | * Populates filtering rules for port, and punting rules |
| 639 | * for gateway IPs, loopback IPs and arp/ndp traffic. |
| 640 | * Should only be called by the master instance for this device/port. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 641 | * |
| 642 | * @param deviceId Switch ID to set the rules |
| 643 | */ |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 644 | public void populatePortAddressingRules(DeviceId deviceId) { |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 645 | rulePopulator.populateIpPunts(deviceId); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 646 | rulePopulator.populateArpNdpPunts(deviceId); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 647 | |
| 648 | // Although device is added, sometimes device store does not have the |
| 649 | // ports for this device yet. It results in missing filtering rules in the |
| 650 | // switch. We will attempt it a few times. If it still does not work, |
| 651 | // user can manually repopulate using CLI command sr-reroute-network |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 652 | PortFilterInfo firstRun = rulePopulator.populateVlanMacFilters(deviceId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 653 | if (firstRun == null) { |
| 654 | firstRun = new PortFilterInfo(0, 0, 0); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 655 | } |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 656 | executorService.schedule(new RetryFilters(deviceId, firstRun), |
| 657 | RETRY_INTERVAL_MS, TimeUnit.MILLISECONDS); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 661 | * Start the flow rule population process if it was never started. The |
| 662 | * process finishes successfully when all flow rules are set and stops with |
| 663 | * ABORTED status when any groups required for flows is not set yet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 664 | */ |
| 665 | public void startPopulationProcess() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 666 | statusLock.lock(); |
| 667 | try { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 668 | if (populationStatus == Status.IDLE |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 669 | || populationStatus == Status.SUCCEEDED |
| 670 | || populationStatus == Status.ABORTED) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 671 | populationStatus = Status.STARTED; |
| 672 | populateAllRoutingRules(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 673 | } else { |
| 674 | log.warn("Not initiating startPopulationProcess as populationStatus is {}", |
| 675 | populationStatus); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 676 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 677 | } finally { |
| 678 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Resume the flow rule population process if it was aborted for any reason. |
| 684 | * 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] | 685 | * XXX is this called? |
| 686 | * |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 687 | */ |
| 688 | public void resumePopulationProcess() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 689 | statusLock.lock(); |
| 690 | try { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 691 | if (populationStatus == Status.ABORTED) { |
| 692 | populationStatus = Status.STARTED; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 693 | // TODO: we need to restart from the point aborted instead of |
| 694 | // restarting. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 695 | populateAllRoutingRules(); |
| 696 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 697 | } finally { |
| 698 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 699 | } |
| 700 | } |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 701 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 702 | /** |
| 703 | * Populate rules of given subnet at given location. |
| 704 | * |
| 705 | * @param cp connect point of the subnet being added |
| 706 | * @param subnets subnet being added |
| 707 | * @return true if succeed |
| 708 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 709 | protected boolean populateSubnet(ConnectPoint cp, Set<IpPrefix> subnets) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 710 | statusLock.lock(); |
| 711 | try { |
| 712 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(cp.deviceId()); |
| 713 | if (ecmpSpg == null) { |
| 714 | log.warn("Fail to populating subnet {}: {}", subnets, ECMPSPG_MISSING); |
| 715 | return false; |
| 716 | } |
| 717 | return populateEcmpRoutingRules(cp.deviceId(), ecmpSpg, subnets); |
| 718 | } finally { |
| 719 | statusLock.unlock(); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Revoke rules of given subnet at given location. |
| 725 | * |
| 726 | * @param subnets subnet being removed |
| 727 | * @return true if succeed |
| 728 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 729 | protected boolean revokeSubnet(Set<IpPrefix> subnets) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 730 | statusLock.lock(); |
| 731 | try { |
| 732 | return srManager.routingRulePopulator.revokeIpRuleForSubnet(subnets); |
| 733 | } finally { |
| 734 | statusLock.unlock(); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | protected void purgeEcmpGraph(DeviceId deviceId) { |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 739 | currentEcmpSpgMap.remove(deviceId); |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 740 | if (updatedEcmpSpgMap != null) { |
| 741 | updatedEcmpSpgMap.remove(deviceId); |
| 742 | } |
Pier Ventre | 6d59389 | 2016-09-13 21:33:40 -0700 | [diff] [blame] | 743 | this.populateRoutingRulesForLinkStatusChange(null); |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 744 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 745 | |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 746 | /** |
| 747 | * Utility class used to temporarily store information about the ports on a |
| 748 | * device processed for filtering objectives. |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 749 | */ |
| 750 | public final class PortFilterInfo { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 751 | int disabledPorts = 0, errorPorts = 0, filteredPorts = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 752 | |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 753 | public PortFilterInfo(int disabledPorts, int errorPorts, |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 754 | int filteredPorts) { |
| 755 | this.disabledPorts = disabledPorts; |
| 756 | this.filteredPorts = filteredPorts; |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 757 | this.errorPorts = errorPorts; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | @Override |
| 761 | public int hashCode() { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 762 | return Objects.hash(disabledPorts, filteredPorts, errorPorts); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | @Override |
| 766 | public boolean equals(Object obj) { |
| 767 | if (this == obj) { |
| 768 | return true; |
| 769 | } |
| 770 | if ((obj == null) || (!(obj instanceof PortFilterInfo))) { |
| 771 | return false; |
| 772 | } |
| 773 | PortFilterInfo other = (PortFilterInfo) obj; |
| 774 | return ((disabledPorts == other.disabledPorts) && |
| 775 | (filteredPorts == other.filteredPorts) && |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 776 | (errorPorts == other.errorPorts)); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public String toString() { |
| 781 | MoreObjects.ToStringHelper helper = toStringHelper(this) |
| 782 | .add("disabledPorts", disabledPorts) |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 783 | .add("errorPorts", errorPorts) |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 784 | .add("filteredPorts", filteredPorts); |
| 785 | return helper.toString(); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * RetryFilters populates filtering objectives for a device and keeps retrying |
| 791 | * till the number of ports filtered are constant for a predefined number |
| 792 | * of attempts. |
| 793 | */ |
| 794 | protected final class RetryFilters implements Runnable { |
| 795 | int constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; |
| 796 | DeviceId devId; |
| 797 | int counter; |
| 798 | PortFilterInfo prevRun; |
| 799 | |
| 800 | private RetryFilters(DeviceId deviceId, PortFilterInfo previousRun) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 801 | devId = deviceId; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 802 | prevRun = previousRun; |
| 803 | counter = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | @Override |
| 807 | public void run() { |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 808 | log.info("RETRY FILTER ATTEMPT {} ** dev:{}", ++counter, devId); |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 809 | PortFilterInfo thisRun = rulePopulator.populateVlanMacFilters(devId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 810 | boolean sameResult = prevRun.equals(thisRun); |
| 811 | log.debug("dev:{} prevRun:{} thisRun:{} sameResult:{}", devId, prevRun, |
| 812 | thisRun, sameResult); |
| 813 | if (thisRun == null || !sameResult || (sameResult && --constantAttempts > 0)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 814 | // exponentially increasing intervals for retries |
| 815 | executorService.schedule(this, |
| 816 | RETRY_INTERVAL_MS * (int) Math.pow(counter, RETRY_INTERVAL_SCALE), |
| 817 | TimeUnit.MILLISECONDS); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 818 | if (!sameResult) { |
| 819 | constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; //reset |
| 820 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 821 | } |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 822 | prevRun = (thisRun == null) ? prevRun : thisRun; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 823 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 824 | } |
| 825 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 826 | } |