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 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
| 19 | import com.google.common.collect.Sets; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 20 | import org.onlab.packet.Ip4Address; |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ip4Prefix; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 22 | import org.onlab.packet.IpPrefix; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Link; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 26 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 27 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.HashMap; |
| 33 | import java.util.HashSet; |
| 34 | import java.util.Set; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 35 | import java.util.concurrent.locks.Lock; |
| 36 | import java.util.concurrent.locks.ReentrantLock; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Default routing handler that is responsible for route computing and |
| 42 | * routing rule population. |
| 43 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 44 | public class DefaultRoutingHandler { |
| 45 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 46 | private static Logger log = LoggerFactory |
| 47 | .getLogger(DefaultRoutingHandler.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 48 | |
| 49 | private SegmentRoutingManager srManager; |
| 50 | private RoutingRulePopulator rulePopulator; |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 51 | private HashMap<DeviceId, EcmpShortestPathGraph> currentEcmpSpgMap; |
| 52 | private HashMap<DeviceId, EcmpShortestPathGraph> updatedEcmpSpgMap; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 53 | private DeviceConfiguration config; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 54 | private final Lock statusLock = new ReentrantLock(); |
| 55 | private volatile Status populationStatus; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Represents the default routing population status. |
| 59 | */ |
| 60 | public enum Status { |
| 61 | // population process is not started yet. |
| 62 | IDLE, |
| 63 | |
| 64 | // population process started. |
| 65 | STARTED, |
| 66 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 67 | // population process was aborted due to errors, mostly for groups not |
| 68 | // found. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 69 | ABORTED, |
| 70 | |
| 71 | // population process was finished successfully. |
| 72 | SUCCEEDED |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Creates a DefaultRoutingHandler object. |
| 77 | * |
| 78 | * @param srManager SegmentRoutingManager object |
| 79 | */ |
| 80 | public DefaultRoutingHandler(SegmentRoutingManager srManager) { |
| 81 | this.srManager = srManager; |
| 82 | this.rulePopulator = checkNotNull(srManager.routingRulePopulator); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 83 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 84 | this.populationStatus = Status.IDLE; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 85 | this.currentEcmpSpgMap = Maps.newHashMap(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Populates all routing rules to all connected routers, including default |
| 90 | * routing rules, adjacency rules, and policy rules if any. |
| 91 | * |
| 92 | * @return true if it succeeds in populating all rules, otherwise false |
| 93 | */ |
| 94 | public boolean populateAllRoutingRules() { |
| 95 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 96 | statusLock.lock(); |
| 97 | try { |
| 98 | populationStatus = Status.STARTED; |
| 99 | rulePopulator.resetCounter(); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 100 | log.info("Starting to populate segment-routing rules"); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 101 | log.debug("populateAllRoutingRules: populationStatus is STARTED"); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 102 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 103 | for (Device sw : srManager.deviceService.getDevices()) { |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 104 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 105 | log.debug("populateAllRoutingRules: skipping device {}...we are not master", |
| 106 | sw.id()); |
| 107 | continue; |
| 108 | } |
| 109 | |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 110 | EcmpShortestPathGraph ecmpSpg = new EcmpShortestPathGraph(sw.id(), srManager); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 111 | if (!populateEcmpRoutingRules(sw.id(), ecmpSpg)) { |
| 112 | log.debug("populateAllRoutingRules: populationStatus is ABORTED"); |
| 113 | populationStatus = Status.ABORTED; |
| 114 | log.debug("Abort routing rule population"); |
| 115 | return false; |
| 116 | } |
| 117 | currentEcmpSpgMap.put(sw.id(), ecmpSpg); |
| 118 | |
| 119 | // TODO: Set adjacency routing rule for all switches |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 120 | } |
| 121 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 122 | log.debug("populateAllRoutingRules: populationStatus is SUCCEEDED"); |
| 123 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 124 | log.info("Completed routing rule population. Total # of rules pushed : {}", |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 125 | rulePopulator.getCounter()); |
| 126 | return true; |
| 127 | } finally { |
| 128 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 129 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 130 | } |
| 131 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Populates the routing rules according to the route changes due to the link |
| 134 | * failure or link add. It computes the routes changed due to the link changes and |
| 135 | * repopulates the rules only for the routes. |
| 136 | * |
| 137 | * @param linkFail link failed, null for link added |
| 138 | * @return true if it succeeds to populate all rules, false otherwise |
| 139 | */ |
| 140 | public boolean populateRoutingRulesForLinkStatusChange(Link linkFail) { |
| 141 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 142 | statusLock.lock(); |
| 143 | try { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 144 | |
| 145 | if (populationStatus == Status.STARTED) { |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 146 | log.warn("Previous rule population is not finished."); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 150 | // Take the snapshots of the links |
| 151 | updatedEcmpSpgMap = new HashMap<>(); |
| 152 | for (Device sw : srManager.deviceService.getDevices()) { |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 153 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 154 | continue; |
| 155 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 156 | EcmpShortestPathGraph ecmpSpgUpdated = |
| 157 | new EcmpShortestPathGraph(sw.id(), srManager); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 158 | updatedEcmpSpgMap.put(sw.id(), ecmpSpgUpdated); |
| 159 | } |
| 160 | |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 161 | log.info("Starts rule population from link change"); |
| 162 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 163 | Set<ArrayList<DeviceId>> routeChanges; |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 164 | log.trace("populateRoutingRulesForLinkStatusChange: " |
| 165 | + "populationStatus is STARTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 166 | populationStatus = Status.STARTED; |
| 167 | if (linkFail == null) { |
| 168 | // Compare all routes of existing ECMP SPG with the new ones |
| 169 | routeChanges = computeRouteChange(); |
| 170 | } else { |
| 171 | // Compare existing ECMP SPG only with the link removed |
| 172 | routeChanges = computeDamagedRoutes(linkFail); |
| 173 | } |
| 174 | |
| 175 | if (routeChanges.isEmpty()) { |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 176 | log.info("No route changes for the link status change"); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 177 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 178 | populationStatus = Status.SUCCEEDED; |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | if (repopulateRoutingRulesForRoutes(routeChanges)) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 183 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 184 | populationStatus = Status.SUCCEEDED; |
| 185 | log.info("Complete to repopulate the rules. # of rules populated : {}", |
| 186 | rulePopulator.getCounter()); |
| 187 | return true; |
| 188 | } else { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 189 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is ABORTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 190 | populationStatus = Status.ABORTED; |
| 191 | log.warn("Failed to repopulate the rules."); |
| 192 | return false; |
| 193 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 194 | } finally { |
| 195 | statusLock.unlock(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | private boolean repopulateRoutingRulesForRoutes(Set<ArrayList<DeviceId>> routes) { |
| 200 | rulePopulator.resetCounter(); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 201 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> routesBydevice = |
| 202 | new HashMap<>(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 203 | for (ArrayList<DeviceId> link: routes) { |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 204 | // When only the source device is defined, reinstall routes to all other devices |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 205 | if (link.size() == 1) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 206 | log.trace("repopulateRoutingRulesForRoutes: running ECMP graph for device {}", link.get(0)); |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 207 | EcmpShortestPathGraph ecmpSpg = new EcmpShortestPathGraph(link.get(0), srManager); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 208 | if (populateEcmpRoutingRules(link.get(0), ecmpSpg)) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 209 | log.debug("Populating flow rules from {} to all is successful", |
| 210 | link.get(0)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 211 | currentEcmpSpgMap.put(link.get(0), ecmpSpg); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 212 | } else { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 213 | log.warn("Failed to populate the flow rules from {} to all", link.get(0)); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 214 | return false; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 215 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 216 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 217 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 218 | routesBydevice.get(link.get(1)); |
| 219 | if (deviceRoutes == null) { |
| 220 | deviceRoutes = new ArrayList<>(); |
| 221 | routesBydevice.put(link.get(1), deviceRoutes); |
| 222 | } |
| 223 | deviceRoutes.add(link); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | for (DeviceId impactedDevice : routesBydevice.keySet()) { |
| 228 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 229 | routesBydevice.get(impactedDevice); |
| 230 | for (ArrayList<DeviceId> link: deviceRoutes) { |
| 231 | log.debug("repopulate RoutingRules For Routes {} -> {}", |
| 232 | link.get(0), link.get(1)); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 233 | DeviceId src = link.get(0); |
| 234 | DeviceId dst = link.get(1); |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 235 | EcmpShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(dst); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 236 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 237 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 238 | for (Integer itrIdx : switchVia.keySet()) { |
| 239 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 240 | switchVia.get(itrIdx); |
| 241 | for (DeviceId targetSw : swViaMap.keySet()) { |
| 242 | if (!targetSw.equals(src)) { |
| 243 | continue; |
| 244 | } |
| 245 | Set<DeviceId> nextHops = new HashSet<>(); |
| 246 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 247 | if (via.isEmpty()) { |
| 248 | nextHops.add(dst); |
| 249 | } else { |
| 250 | nextHops.add(via.get(0)); |
| 251 | } |
| 252 | } |
| 253 | if (!populateEcmpRoutingRulePartial(targetSw, dst, nextHops)) { |
| 254 | return false; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 255 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 256 | log.debug("Populating flow rules from {} to {} is successful", |
| 257 | targetSw, dst); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 258 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 259 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 260 | //currentEcmpSpgMap.put(dst, ecmpSpg); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 261 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 262 | //Only if all the flows for all impacted routes to a |
| 263 | //specific target are pushed successfully, update the |
| 264 | //ECMP graph for that target. (Or else the next event |
| 265 | //would not see any changes in the ECMP graphs) |
| 266 | currentEcmpSpgMap.put(impactedDevice, |
| 267 | updatedEcmpSpgMap.get(impactedDevice)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 268 | } |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | private Set<ArrayList<DeviceId>> computeDamagedRoutes(Link linkFail) { |
| 273 | |
| 274 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 275 | |
| 276 | for (Device sw : srManager.deviceService.getDevices()) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 277 | log.debug("Computing the impacted routes for device {} due to link fail", |
| 278 | sw.id()); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 279 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 280 | continue; |
| 281 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 282 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 283 | if (ecmpSpg == null) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 284 | log.error("No existing ECMP graph for switch {}", sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 285 | continue; |
| 286 | } |
| 287 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 288 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 289 | for (Integer itrIdx : switchVia.keySet()) { |
| 290 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 291 | switchVia.get(itrIdx); |
| 292 | for (DeviceId targetSw : swViaMap.keySet()) { |
| 293 | DeviceId destSw = sw.id(); |
| 294 | Set<ArrayList<DeviceId>> subLinks = |
| 295 | computeLinks(targetSw, destSw, swViaMap); |
| 296 | for (ArrayList<DeviceId> alink: subLinks) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 297 | if ((alink.get(0).equals(linkFail.src().deviceId()) && |
| 298 | alink.get(1).equals(linkFail.dst().deviceId())) |
| 299 | || |
| 300 | (alink.get(0).equals(linkFail.dst().deviceId()) && |
| 301 | alink.get(1).equals(linkFail.src().deviceId()))) { |
| 302 | log.debug("Impacted route:{}->{}", targetSw, destSw); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 303 | ArrayList<DeviceId> aRoute = new ArrayList<>(); |
| 304 | aRoute.add(targetSw); |
| 305 | aRoute.add(destSw); |
| 306 | routes.add(aRoute); |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 312 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | return routes; |
| 316 | } |
| 317 | |
| 318 | private Set<ArrayList<DeviceId>> computeRouteChange() { |
| 319 | |
| 320 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 321 | |
| 322 | for (Device sw : srManager.deviceService.getDevices()) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 323 | log.debug("Computing the impacted routes for device {}", |
| 324 | sw.id()); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 325 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 326 | log.debug("No mastership for {} and skip route optimization", |
| 327 | sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 328 | continue; |
| 329 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 330 | |
| 331 | log.trace("link of {} - ", sw.id()); |
| 332 | for (Link link: srManager.linkService.getDeviceLinks(sw.id())) { |
| 333 | log.trace("{} -> {} ", link.src().deviceId(), link.dst().deviceId()); |
| 334 | } |
| 335 | |
| 336 | log.debug("Checking route change for switch {}", sw.id()); |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 337 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 338 | if (ecmpSpg == null) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 339 | log.debug("No existing ECMP graph for device {}", sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 340 | ArrayList<DeviceId> route = new ArrayList<>(); |
| 341 | route.add(sw.id()); |
| 342 | routes.add(route); |
| 343 | continue; |
| 344 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 345 | EcmpShortestPathGraph newEcmpSpg = updatedEcmpSpgMap.get(sw.id()); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 346 | //currentEcmpSpgMap.put(sw.id(), newEcmpSpg); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 347 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 348 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 349 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchViaUpdated = |
| 350 | newEcmpSpg.getAllLearnedSwitchesAndVia(); |
| 351 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 352 | for (Integer itrIdx : switchViaUpdated.keySet()) { |
| 353 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMapUpdated = |
| 354 | switchViaUpdated.get(itrIdx); |
| 355 | for (DeviceId srcSw : swViaMapUpdated.keySet()) { |
| 356 | ArrayList<ArrayList<DeviceId>> viaUpdated = swViaMapUpdated.get(srcSw); |
| 357 | ArrayList<ArrayList<DeviceId>> via = getVia(switchVia, srcSw); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 358 | if ((via == null) || !viaUpdated.equals(via)) { |
| 359 | log.debug("Impacted route:{}->{}", srcSw, sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 360 | ArrayList<DeviceId> route = new ArrayList<>(); |
| 361 | route.add(srcSw); |
| 362 | route.add(sw.id()); |
| 363 | routes.add(route); |
| 364 | } |
| 365 | } |
| 366 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 367 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 368 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 369 | for (ArrayList<DeviceId> link: routes) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 370 | log.trace("Route changes - "); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 371 | if (link.size() == 1) { |
| 372 | log.trace(" : {} - all", link.get(0)); |
| 373 | } else { |
| 374 | log.trace(" : {} - {}", link.get(0), link.get(1)); |
| 375 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | return routes; |
| 379 | } |
| 380 | |
| 381 | private ArrayList<ArrayList<DeviceId>> getVia(HashMap<Integer, HashMap<DeviceId, |
| 382 | ArrayList<ArrayList<DeviceId>>>> switchVia, DeviceId srcSw) { |
| 383 | for (Integer itrIdx : switchVia.keySet()) { |
| 384 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 385 | switchVia.get(itrIdx); |
| 386 | if (swViaMap.get(srcSw) == null) { |
| 387 | continue; |
| 388 | } else { |
| 389 | return swViaMap.get(srcSw); |
| 390 | } |
| 391 | } |
| 392 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 393 | return null; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | private Set<ArrayList<DeviceId>> computeLinks(DeviceId src, |
| 397 | DeviceId dst, |
| 398 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> viaMap) { |
| 399 | Set<ArrayList<DeviceId>> subLinks = Sets.newHashSet(); |
| 400 | for (ArrayList<DeviceId> via : viaMap.get(src)) { |
| 401 | DeviceId linkSrc = src; |
| 402 | DeviceId linkDst = dst; |
| 403 | for (DeviceId viaDevice: via) { |
| 404 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 405 | linkDst = viaDevice; |
| 406 | link.add(linkSrc); |
| 407 | link.add(linkDst); |
| 408 | subLinks.add(link); |
| 409 | linkSrc = viaDevice; |
| 410 | } |
| 411 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 412 | link.add(linkSrc); |
| 413 | link.add(dst); |
| 414 | subLinks.add(link); |
| 415 | } |
| 416 | |
| 417 | return subLinks; |
| 418 | } |
| 419 | |
| 420 | private boolean populateEcmpRoutingRules(DeviceId destSw, |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 421 | EcmpShortestPathGraph ecmpSPG) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 422 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 423 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = ecmpSPG |
| 424 | .getAllLearnedSwitchesAndVia(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 425 | for (Integer itrIdx : switchVia.keySet()) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 426 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = switchVia |
| 427 | .get(itrIdx); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 428 | for (DeviceId targetSw : swViaMap.keySet()) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 429 | Set<DeviceId> nextHops = new HashSet<>(); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 430 | log.debug("** Iter: {} root: {} target: {}", itrIdx, destSw, targetSw); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 431 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 432 | if (via.isEmpty()) { |
| 433 | nextHops.add(destSw); |
| 434 | } else { |
| 435 | nextHops.add(via.get(0)); |
| 436 | } |
| 437 | } |
| 438 | if (!populateEcmpRoutingRulePartial(targetSw, destSw, nextHops)) { |
| 439 | return false; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return true; |
| 445 | } |
| 446 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 447 | private boolean populateEcmpRoutingRulePartial(DeviceId targetSw, |
| 448 | DeviceId destSw, |
| 449 | Set<DeviceId> nextHops) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 450 | boolean result; |
| 451 | |
| 452 | if (nextHops.isEmpty()) { |
| 453 | nextHops.add(destSw); |
| 454 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 455 | // If both target switch and dest switch are edge routers, then set IP |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 456 | // rule for both subnet and router IP. |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 457 | boolean targetIsEdge; |
| 458 | boolean destIsEdge; |
| 459 | Ip4Address destRouterIp; |
| 460 | |
| 461 | try { |
| 462 | targetIsEdge = config.isEdgeDevice(targetSw); |
| 463 | destIsEdge = config.isEdgeDevice(destSw); |
| 464 | destRouterIp = config.getRouterIp(destSw); |
| 465 | } catch (DeviceConfigNotFoundException e) { |
| 466 | log.warn(e.getMessage() + " Aborting populateEcmpRoutingRulePartial."); |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | if (targetIsEdge && destIsEdge) { |
Charles Chan | c6ad775 | 2015-10-29 14:58:10 -0700 | [diff] [blame] | 471 | Set<Ip4Prefix> subnets = config.getSubnets(destSw); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 472 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for subnets {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 473 | targetSw, destSw, subnets); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 474 | result = rulePopulator.populateIpRuleForSubnet(targetSw, |
| 475 | subnets, |
| 476 | destSw, |
| 477 | nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 478 | if (!result) { |
| 479 | return false; |
| 480 | } |
| 481 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 482 | Ip4Address routerIp = destRouterIp; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 483 | IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 484 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for router IP {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 485 | targetSw, destSw, routerIpPrefix); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 486 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 487 | if (!result) { |
| 488 | return false; |
| 489 | } |
| 490 | |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 491 | } else if (targetIsEdge) { |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 492 | // If the target switch is an edge router, then set IP rules for the router IP. |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 493 | Ip4Address routerIp = destRouterIp; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 494 | IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 495 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} for router IP {}", |
Saurav Das | 4c35fc4 | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 496 | targetSw, destSw, routerIpPrefix); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 497 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 498 | if (!result) { |
| 499 | return false; |
| 500 | } |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 501 | } |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 502 | // Populates MPLS rules to all routers |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 503 | log.debug("* populateEcmpRoutingRulePartial in device{} towards {} for all MPLS rules", |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 504 | targetSw, destSw); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 505 | result = rulePopulator.populateMplsRule(targetSw, destSw, nextHops); |
| 506 | if (!result) { |
| 507 | return false; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 508 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 509 | return true; |
| 510 | } |
| 511 | |
| 512 | /** |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 513 | * Populates filtering rules for permitting Router DstMac and VLAN. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 514 | * |
| 515 | * @param deviceId Switch ID to set the rules |
| 516 | */ |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 517 | public void populatePortAddressingRules(DeviceId deviceId) { |
| 518 | rulePopulator.populateRouterMacVlanFilters(deviceId); |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 519 | rulePopulator.populateXConnectVlanFilters(deviceId); |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 520 | rulePopulator.populateRouterIpPunts(deviceId); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 524 | * Start the flow rule population process if it was never started. The |
| 525 | * process finishes successfully when all flow rules are set and stops with |
| 526 | * ABORTED status when any groups required for flows is not set yet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 527 | */ |
| 528 | public void startPopulationProcess() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 529 | statusLock.lock(); |
| 530 | try { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 531 | if (populationStatus == Status.IDLE |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 532 | || populationStatus == Status.SUCCEEDED |
| 533 | || populationStatus == Status.ABORTED) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 534 | populationStatus = Status.STARTED; |
| 535 | populateAllRoutingRules(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 536 | } else { |
| 537 | log.warn("Not initiating startPopulationProcess as populationStatus is {}", |
| 538 | populationStatus); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 539 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 540 | } finally { |
| 541 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Resume the flow rule population process if it was aborted for any reason. |
| 547 | * 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] | 548 | * XXX is this called? |
| 549 | * |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 550 | */ |
| 551 | public void resumePopulationProcess() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 552 | statusLock.lock(); |
| 553 | try { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 554 | if (populationStatus == Status.ABORTED) { |
| 555 | populationStatus = Status.STARTED; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 556 | // TODO: we need to restart from the point aborted instead of |
| 557 | // restarting. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 558 | populateAllRoutingRules(); |
| 559 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 560 | } finally { |
| 561 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 564 | |
| 565 | public void purgeEcmpGraph(DeviceId deviceId) { |
| 566 | currentEcmpSpgMap.remove(deviceId); |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 567 | if (updatedEcmpSpgMap != null) { |
| 568 | updatedEcmpSpgMap.remove(deviceId); |
| 569 | } |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 570 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 571 | } |