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