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