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(); |
| 187 | for (ArrayList<DeviceId> link: routes) { |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 188 | // When only the source device is defined, reinstall routes to all other devices |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 189 | if (link.size() == 1) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 190 | log.trace("repopulateRoutingRulesForRoutes: running ECMP graph for device {}", link.get(0)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 191 | ECMPShortestPathGraph ecmpSpg = new ECMPShortestPathGraph(link.get(0), srManager); |
| 192 | if (populateEcmpRoutingRules(link.get(0), ecmpSpg)) { |
| 193 | currentEcmpSpgMap.put(link.get(0), ecmpSpg); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 194 | } else { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 195 | 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] | 196 | return false; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 197 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 198 | } else { |
| 199 | DeviceId src = link.get(0); |
| 200 | DeviceId dst = link.get(1); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 201 | log.trace("repopulateRoutingRulesForRoutes: running ECMP graph for device {}", dst); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 202 | ECMPShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(dst); |
| 203 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 204 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 205 | for (Integer itrIdx : switchVia.keySet()) { |
| 206 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 207 | switchVia.get(itrIdx); |
| 208 | for (DeviceId targetSw : swViaMap.keySet()) { |
| 209 | if (!targetSw.equals(src)) { |
| 210 | continue; |
| 211 | } |
| 212 | Set<DeviceId> nextHops = new HashSet<>(); |
| 213 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 214 | if (via.isEmpty()) { |
| 215 | nextHops.add(dst); |
| 216 | } else { |
| 217 | nextHops.add(via.get(0)); |
| 218 | } |
| 219 | } |
| 220 | if (!populateEcmpRoutingRulePartial(targetSw, dst, nextHops)) { |
| 221 | return false; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 224 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 225 | currentEcmpSpgMap.put(dst, ecmpSpg); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | private Set<ArrayList<DeviceId>> computeDamagedRoutes(Link linkFail) { |
| 232 | |
| 233 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 234 | |
| 235 | for (Device sw : srManager.deviceService.getDevices()) { |
| 236 | if (srManager.mastershipService. |
| 237 | getLocalRole(sw.id()) != MastershipRole.MASTER) { |
| 238 | continue; |
| 239 | } |
| 240 | ECMPShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(sw.id()); |
| 241 | if (ecmpSpg == null) { |
| 242 | log.error("No existing ECMP path for switch {}", sw.id()); |
| 243 | continue; |
| 244 | } |
| 245 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 246 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 247 | for (Integer itrIdx : switchVia.keySet()) { |
| 248 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 249 | switchVia.get(itrIdx); |
| 250 | for (DeviceId targetSw : swViaMap.keySet()) { |
| 251 | DeviceId destSw = sw.id(); |
| 252 | Set<ArrayList<DeviceId>> subLinks = |
| 253 | computeLinks(targetSw, destSw, swViaMap); |
| 254 | for (ArrayList<DeviceId> alink: subLinks) { |
| 255 | if (alink.get(0).equals(linkFail.src().deviceId()) && |
| 256 | alink.get(1).equals(linkFail.dst().deviceId())) { |
| 257 | ArrayList<DeviceId> aRoute = new ArrayList<>(); |
| 258 | aRoute.add(targetSw); |
| 259 | aRoute.add(destSw); |
| 260 | routes.add(aRoute); |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 266 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | return routes; |
| 270 | } |
| 271 | |
| 272 | private Set<ArrayList<DeviceId>> computeRouteChange() { |
| 273 | |
| 274 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 275 | |
| 276 | for (Device sw : srManager.deviceService.getDevices()) { |
| 277 | if (srManager.mastershipService. |
| 278 | getLocalRole(sw.id()) != MastershipRole.MASTER) { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 279 | log.warn("No mastership for {} and skip route optimization"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 280 | continue; |
| 281 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 282 | |
| 283 | log.trace("link of {} - ", sw.id()); |
| 284 | for (Link link: srManager.linkService.getDeviceLinks(sw.id())) { |
| 285 | log.trace("{} -> {} ", link.src().deviceId(), link.dst().deviceId()); |
| 286 | } |
| 287 | |
| 288 | log.debug("Checking route change for switch {}", sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 289 | ECMPShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(sw.id()); |
| 290 | if (ecmpSpg == null) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 291 | log.debug("No existing ECMP graph for device {}", sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 292 | ArrayList<DeviceId> route = new ArrayList<>(); |
| 293 | route.add(sw.id()); |
| 294 | routes.add(route); |
| 295 | continue; |
| 296 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 297 | ECMPShortestPathGraph newEcmpSpg = updatedEcmpSpgMap.get(sw.id()); |
| 298 | currentEcmpSpgMap.put(sw.id(), newEcmpSpg); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 299 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 300 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 301 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchViaUpdated = |
| 302 | newEcmpSpg.getAllLearnedSwitchesAndVia(); |
| 303 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 304 | for (Integer itrIdx : switchViaUpdated.keySet()) { |
| 305 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMapUpdated = |
| 306 | switchViaUpdated.get(itrIdx); |
| 307 | for (DeviceId srcSw : swViaMapUpdated.keySet()) { |
| 308 | ArrayList<ArrayList<DeviceId>> viaUpdated = swViaMapUpdated.get(srcSw); |
| 309 | ArrayList<ArrayList<DeviceId>> via = getVia(switchVia, srcSw); |
| 310 | if (via.isEmpty() || !viaUpdated.equals(via)) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 311 | ArrayList<DeviceId> route = new ArrayList<>(); |
| 312 | route.add(srcSw); |
| 313 | route.add(sw.id()); |
| 314 | routes.add(route); |
| 315 | } |
| 316 | } |
| 317 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 318 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 319 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 320 | for (ArrayList<DeviceId> link: routes) { |
| 321 | log.trace("Link changes - "); |
| 322 | if (link.size() == 1) { |
| 323 | log.trace(" : {} - all", link.get(0)); |
| 324 | } else { |
| 325 | log.trace(" : {} - {}", link.get(0), link.get(1)); |
| 326 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return routes; |
| 330 | } |
| 331 | |
| 332 | private ArrayList<ArrayList<DeviceId>> getVia(HashMap<Integer, HashMap<DeviceId, |
| 333 | ArrayList<ArrayList<DeviceId>>>> switchVia, DeviceId srcSw) { |
| 334 | for (Integer itrIdx : switchVia.keySet()) { |
| 335 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 336 | switchVia.get(itrIdx); |
| 337 | if (swViaMap.get(srcSw) == null) { |
| 338 | continue; |
| 339 | } else { |
| 340 | return swViaMap.get(srcSw); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | return new ArrayList<>(); |
| 345 | } |
| 346 | |
| 347 | private Set<ArrayList<DeviceId>> computeLinks(DeviceId src, |
| 348 | DeviceId dst, |
| 349 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> viaMap) { |
| 350 | Set<ArrayList<DeviceId>> subLinks = Sets.newHashSet(); |
| 351 | for (ArrayList<DeviceId> via : viaMap.get(src)) { |
| 352 | DeviceId linkSrc = src; |
| 353 | DeviceId linkDst = dst; |
| 354 | for (DeviceId viaDevice: via) { |
| 355 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 356 | linkDst = viaDevice; |
| 357 | link.add(linkSrc); |
| 358 | link.add(linkDst); |
| 359 | subLinks.add(link); |
| 360 | linkSrc = viaDevice; |
| 361 | } |
| 362 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 363 | link.add(linkSrc); |
| 364 | link.add(dst); |
| 365 | subLinks.add(link); |
| 366 | } |
| 367 | |
| 368 | return subLinks; |
| 369 | } |
| 370 | |
| 371 | private boolean populateEcmpRoutingRules(DeviceId destSw, |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 372 | ECMPShortestPathGraph ecmpSPG) { |
| 373 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 374 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = ecmpSPG |
| 375 | .getAllLearnedSwitchesAndVia(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 376 | for (Integer itrIdx : switchVia.keySet()) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 377 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = switchVia |
| 378 | .get(itrIdx); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 379 | for (DeviceId targetSw : swViaMap.keySet()) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 380 | Set<DeviceId> nextHops = new HashSet<>(); |
| 381 | |
| 382 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 383 | if (via.isEmpty()) { |
| 384 | nextHops.add(destSw); |
| 385 | } else { |
| 386 | nextHops.add(via.get(0)); |
| 387 | } |
| 388 | } |
| 389 | if (!populateEcmpRoutingRulePartial(targetSw, destSw, nextHops)) { |
| 390 | return false; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 398 | private boolean populateEcmpRoutingRulePartial(DeviceId targetSw, |
| 399 | DeviceId destSw, |
| 400 | Set<DeviceId> nextHops) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 401 | boolean result; |
| 402 | |
| 403 | if (nextHops.isEmpty()) { |
| 404 | nextHops.add(destSw); |
| 405 | } |
| 406 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 407 | // If both target switch and dest switch are edge routers, then set IP |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 408 | // rule for both subnet and router IP. |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 409 | if (config.isEdgeDevice(targetSw) && config.isEdgeDevice(destSw)) { |
| 410 | List<Ip4Prefix> subnets = config.getSubnets(destSw); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 411 | log.debug("populateEcmpRoutingRulePartial in device {} towards {} for subnets {}", |
| 412 | targetSw, destSw, subnets); |
Srikanth Vavilapalli | 37a461b | 2015-04-07 15:12:32 -0700 | [diff] [blame] | 413 | result = rulePopulator.populateIpRuleForSubnet(targetSw, |
| 414 | subnets, |
| 415 | destSw, |
| 416 | nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 417 | if (!result) { |
| 418 | return false; |
| 419 | } |
| 420 | |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 421 | Ip4Address routerIp = config.getRouterIp(destSw); |
| 422 | IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 423 | log.debug("populateEcmpRoutingRulePartial in device {} towards {} for router IP {}", |
| 424 | targetSw, destSw, routerIpPrefix); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 425 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 426 | if (!result) { |
| 427 | return false; |
| 428 | } |
| 429 | |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 430 | // 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] | 431 | } else if (config.isEdgeDevice(targetSw)) { |
| 432 | Ip4Address routerIp = config.getRouterIp(destSw); |
| 433 | IpPrefix routerIpPrefix = IpPrefix.valueOf(routerIp, IpPrefix.MAX_INET_MASK_LENGTH); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 434 | log.debug("populateEcmpRoutingRulePartial in device {} towards {} for router IP {}", |
| 435 | targetSw, destSw, routerIpPrefix); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 436 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 437 | if (!result) { |
| 438 | return false; |
| 439 | } |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 440 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 441 | |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 442 | // Populates MPLS rules to all routers |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 443 | log.debug("populateEcmpRoutingRulePartial in device{} towards {} for all MPLS rules", |
| 444 | targetSw, destSw); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 445 | result = rulePopulator.populateMplsRule(targetSw, destSw, nextHops); |
| 446 | if (!result) { |
| 447 | return false; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 454 | * Populates table miss entries for all tables, and pipeline rules for VLAN |
| 455 | * and TACM tables. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 456 | * |
| 457 | * @param deviceId Switch ID to set the rules |
| 458 | */ |
| 459 | public void populateTtpRules(DeviceId deviceId) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 460 | rulePopulator.populateTableVlan(deviceId); |
| 461 | rulePopulator.populateTableTMac(deviceId); |
| 462 | } |
| 463 | |
| 464 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 465 | * Start the flow rule population process if it was never started. The |
| 466 | * process finishes successfully when all flow rules are set and stops with |
| 467 | * ABORTED status when any groups required for flows is not set yet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 468 | */ |
| 469 | public void startPopulationProcess() { |
| 470 | synchronized (populationStatus) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 471 | if (populationStatus == Status.IDLE |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 472 | || populationStatus == Status.SUCCEEDED |
| 473 | || populationStatus == Status.ABORTED) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 474 | populationStatus = Status.STARTED; |
| 475 | populateAllRoutingRules(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 476 | } else { |
| 477 | log.warn("Not initiating startPopulationProcess as populationStatus is {}", |
| 478 | populationStatus); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Resume the flow rule population process if it was aborted for any reason. |
| 485 | * Mostly the process is aborted when the groups required are not set yet. |
| 486 | */ |
| 487 | public void resumePopulationProcess() { |
| 488 | synchronized (populationStatus) { |
| 489 | if (populationStatus == Status.ABORTED) { |
| 490 | populationStatus = Status.STARTED; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 491 | // TODO: we need to restart from the point aborted instead of |
| 492 | // restarting. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 493 | populateAllRoutingRules(); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |