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