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