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