sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 43b5354 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 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 | |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableMap.Builder; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Maps; |
| 24 | import com.google.common.collect.Sets; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ip4Address; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 26 | import org.onlab.packet.Ip6Address; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 27 | import org.onlab.packet.IpPrefix; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 28 | import org.onosproject.net.ConnectPoint; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
| 30 | import org.onosproject.net.DeviceId; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 31 | import org.onosproject.net.Link; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 32 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
| 33 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 34 | import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.HashMap; |
| 40 | import java.util.HashSet; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 41 | import java.util.Objects; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 42 | import java.util.Set; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 43 | import java.util.concurrent.ScheduledExecutorService; |
| 44 | import java.util.concurrent.TimeUnit; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 45 | import java.util.concurrent.locks.Lock; |
| 46 | import java.util.concurrent.locks.ReentrantLock; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 47 | |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 48 | import static com.google.common.base.MoreObjects.toStringHelper; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 49 | import static com.google.common.base.Preconditions.checkNotNull; |
| 50 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
| 51 | import static org.onlab.util.Tools.groupedThreads; |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 52 | |
Charles Chan | b7f75ac | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Default routing handler that is responsible for route computing and |
| 55 | * routing rule population. |
| 56 | */ |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 57 | public class DefaultRoutingHandler { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 58 | private static final int MAX_CONSTANT_RETRY_ATTEMPTS = 5; |
| 59 | private static final int RETRY_INTERVAL_MS = 250; |
| 60 | private static final int RETRY_INTERVAL_SCALE = 1; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 61 | private static final String ECMPSPG_MISSING = "ECMP shortest path graph not found"; |
| 62 | private static Logger log = LoggerFactory.getLogger(DefaultRoutingHandler.class); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 63 | |
| 64 | private SegmentRoutingManager srManager; |
| 65 | private RoutingRulePopulator rulePopulator; |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 66 | private HashMap<DeviceId, EcmpShortestPathGraph> currentEcmpSpgMap; |
| 67 | private HashMap<DeviceId, EcmpShortestPathGraph> updatedEcmpSpgMap; |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 68 | private DeviceConfiguration config; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 69 | private final Lock statusLock = new ReentrantLock(); |
| 70 | private volatile Status populationStatus; |
Yuta HIGUCHI | ebee2f1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 71 | private ScheduledExecutorService executorService |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 72 | = newScheduledThreadPool(1, groupedThreads("retryftr", "retry-%d", log)); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Represents the default routing population status. |
| 76 | */ |
| 77 | public enum Status { |
| 78 | // population process is not started yet. |
| 79 | IDLE, |
| 80 | |
| 81 | // population process started. |
| 82 | STARTED, |
| 83 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 84 | // population process was aborted due to errors, mostly for groups not |
| 85 | // found. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 86 | ABORTED, |
| 87 | |
| 88 | // population process was finished successfully. |
| 89 | SUCCEEDED |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a DefaultRoutingHandler object. |
| 94 | * |
| 95 | * @param srManager SegmentRoutingManager object |
| 96 | */ |
| 97 | public DefaultRoutingHandler(SegmentRoutingManager srManager) { |
| 98 | this.srManager = srManager; |
| 99 | this.rulePopulator = checkNotNull(srManager.routingRulePopulator); |
sangho | 9b169e3 | 2015-04-14 16:27:13 -0700 | [diff] [blame] | 100 | this.config = checkNotNull(srManager.deviceConfiguration); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 101 | this.populationStatus = Status.IDLE; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 102 | this.currentEcmpSpgMap = Maps.newHashMap(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 106 | * Returns an immutable copy of the current ECMP shortest-path graph as |
| 107 | * computed by this controller instance. |
| 108 | * |
| 109 | * @return the current ECMP graph |
| 110 | */ |
| 111 | public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEmcpSpgMap() { |
| 112 | Builder<DeviceId, EcmpShortestPathGraph> builder = ImmutableMap.builder(); |
| 113 | currentEcmpSpgMap.entrySet().forEach(entry -> { |
| 114 | if (entry.getValue() != null) { |
| 115 | builder.put(entry.getKey(), entry.getValue()); |
| 116 | } |
| 117 | }); |
| 118 | return builder.build(); |
| 119 | } |
| 120 | |
| 121 | /** |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 122 | * Populates all routing rules to all connected routers, including default |
| 123 | * routing rules, adjacency rules, and policy rules if any. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 124 | */ |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 125 | public void populateAllRoutingRules() { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 126 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 127 | statusLock.lock(); |
| 128 | try { |
| 129 | populationStatus = Status.STARTED; |
| 130 | rulePopulator.resetCounter(); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 131 | log.info("Starting to populate segment-routing rules"); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 132 | log.debug("populateAllRoutingRules: populationStatus is STARTED"); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 133 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 134 | for (Device sw : srManager.deviceService.getDevices()) { |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 135 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 136 | log.debug("populateAllRoutingRules: skipping device {}..." |
| 137 | + "we are not master", sw.id()); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 138 | continue; |
| 139 | } |
| 140 | |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 141 | EcmpShortestPathGraph ecmpSpg = new EcmpShortestPathGraph(sw.id(), srManager); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 142 | if (!populateEcmpRoutingRules(sw.id(), ecmpSpg, ImmutableSet.of())) { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 143 | log.debug("populateAllRoutingRules: populationStatus is ABORTED"); |
| 144 | populationStatus = Status.ABORTED; |
| 145 | log.debug("Abort routing rule population"); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 146 | return; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 147 | } |
| 148 | currentEcmpSpgMap.put(sw.id(), ecmpSpg); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 149 | log.debug("Updating ECMPspg for sw:{}", sw.id()); |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 150 | |
| 151 | // TODO: Set adjacency routing rule for all switches |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 152 | } |
| 153 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 154 | log.debug("populateAllRoutingRules: populationStatus is SUCCEEDED"); |
| 155 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 156 | log.info("Completed routing rule population. Total # of rules pushed : {}", |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 157 | rulePopulator.getCounter()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 158 | return; |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 159 | } finally { |
| 160 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 161 | } |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 162 | } |
| 163 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 164 | /** |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 165 | * Populates the routing rules or makes hash group changes according to the |
| 166 | * route-path changes due to link failure, switch failure or link up. This |
| 167 | * method should only be called for one of these three possible event-types. |
| 168 | * Note that when a switch goes away, all of its links fail as well, |
| 169 | * but this is handled as a single switch removal event. |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 170 | * |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 171 | * @param linkDown the single failed link, or null for other conditions |
| 172 | * such as link-up or a removed switch |
| 173 | * @param linkUp the single link up, or null for other conditions such as |
| 174 | * link-down or a removed switch |
| 175 | * @param switchDown the removed switch, or null for other conditions such as |
| 176 | * link-down or link-up |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 177 | */ |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 178 | public void populateRoutingRulesForLinkStatusChange(Link linkDown, |
| 179 | Link linkUp, |
| 180 | DeviceId switchDown) { |
| 181 | if ((linkDown != null && (linkUp != null || switchDown != null)) || |
| 182 | (linkUp != null && (linkDown != null || switchDown != null)) || |
| 183 | (switchDown != null && (linkUp != null || linkDown != null))) { |
| 184 | log.warn("Only one event can be handled for link status change .. aborting"); |
| 185 | return; |
| 186 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 187 | |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 188 | statusLock.lock(); |
| 189 | try { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 190 | |
| 191 | if (populationStatus == Status.STARTED) { |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 192 | log.warn("Previous rule population is not finished."); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 193 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 194 | } |
| 195 | |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 196 | // Take the snapshots of the links |
| 197 | updatedEcmpSpgMap = new HashMap<>(); |
| 198 | for (Device sw : srManager.deviceService.getDevices()) { |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 199 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 200 | continue; |
| 201 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 202 | EcmpShortestPathGraph ecmpSpgUpdated = |
| 203 | new EcmpShortestPathGraph(sw.id(), srManager); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 204 | updatedEcmpSpgMap.put(sw.id(), ecmpSpgUpdated); |
| 205 | } |
| 206 | |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 207 | log.info("Starts rule population from link change"); |
| 208 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 209 | Set<ArrayList<DeviceId>> routeChanges; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 210 | log.debug("populateRoutingRulesForLinkStatusChange: " |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 211 | + "populationStatus is STARTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 212 | populationStatus = Status.STARTED; |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 213 | // try optimized re-routing |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 214 | if (linkDown == null) { |
| 215 | // either a linkUp or a switchDown - compute all route changes by |
| 216 | // comparing all routes of existing ECMP SPG to new ECMP SPG |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 217 | routeChanges = computeRouteChange(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 218 | |
| 219 | if (routeChanges != null) { |
| 220 | // deal with linkUp of a seen-before link |
| 221 | if (linkUp != null && srManager.isSeenLink(linkUp)) { |
| 222 | if (!isBidirectional(linkUp)) { |
| 223 | log.warn("Not a bidirectional link yet .. not " |
| 224 | + "processing link {}", linkUp); |
| 225 | srManager.updateSeenLink(linkUp, true); |
| 226 | populationStatus = Status.ABORTED; |
| 227 | return; |
| 228 | } |
| 229 | // link previously seen before |
| 230 | // do hash-bucket changes instead of a re-route |
| 231 | processHashGroupChange(routeChanges, false, null); |
| 232 | // clear out routesChanges so a re-route is not attempted |
| 233 | routeChanges = ImmutableSet.of(); |
| 234 | } |
| 235 | |
| 236 | //deal with switchDown |
| 237 | if (switchDown != null) { |
| 238 | processHashGroupChange(routeChanges, true, switchDown); |
| 239 | // clear out routesChanges so a re-route is not attempted |
| 240 | routeChanges = ImmutableSet.of(); |
| 241 | } |
| 242 | |
| 243 | // for a linkUp of a never-seen-before link |
| 244 | // let it fall through to a reroute of the routeChanges |
| 245 | |
| 246 | } |
| 247 | |
| 248 | // now that we are past the check for a previously seen link |
| 249 | // it is safe to update the store for the linkUp |
| 250 | if (linkUp != null) { |
| 251 | srManager.updateSeenLink(linkUp, true); |
| 252 | } |
| 253 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 254 | } else { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 255 | // link has gone down |
| 256 | // Compare existing ECMP SPG only with the link that went down |
| 257 | routeChanges = computeDamagedRoutes(linkDown); |
| 258 | if (routeChanges != null) { |
| 259 | processHashGroupChange(routeChanges, true, null); |
| 260 | // clear out routesChanges so a re-route is not attempted |
| 261 | routeChanges = ImmutableSet.of(); |
| 262 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 265 | // do full re-routing if optimized routing returns null routeChanges |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 266 | if (routeChanges == null) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 267 | log.info("Optimized routing failed... doing full re-route"); |
| 268 | populateAllRoutingRules(); |
| 269 | return; |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 270 | } |
| 271 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 272 | if (routeChanges.isEmpty()) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 273 | log.info("No re-route attempted for the link status change"); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 274 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 275 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 276 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 279 | // reroute of routeChanges |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 280 | if (repopulateRoutingRulesForRoutes(routeChanges)) { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 281 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is SUCCEEDED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 282 | populationStatus = Status.SUCCEEDED; |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 283 | log.info("Completed repopulation of rules. # of rules populated : {}", |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 284 | rulePopulator.getCounter()); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 285 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 286 | } else { |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 287 | log.debug("populateRoutingRulesForLinkStatusChange: populationStatus is ABORTED"); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 288 | populationStatus = Status.ABORTED; |
| 289 | log.warn("Failed to repopulate the rules."); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 290 | return; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 291 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 292 | } finally { |
| 293 | statusLock.unlock(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Returns true if the link being queried is a bidirectional link. A bidi |
| 299 | * link is defined as a link, whose reverse link - ie. the link in the reverse |
| 300 | * direction - has been seen-before and is up. |
| 301 | * |
| 302 | * @param link the infrastructure link being queried |
| 303 | * @return true if another unidirectional link exists in the reverse direction, |
| 304 | * has been seen-before and is up |
| 305 | */ |
| 306 | private boolean isBidirectional(Link link) { |
| 307 | Link reverseLink = srManager.linkService.getLink(link.dst(), link.src()); |
| 308 | if (reverseLink == null) { |
| 309 | return false; |
| 310 | } |
| 311 | Boolean result = srManager.isSeenLinkUp(reverseLink); |
| 312 | if (result == null) { |
| 313 | return false; |
| 314 | } |
| 315 | return result.booleanValue(); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Processes a set a route-path changes by editing hash groups. |
| 320 | * |
| 321 | * @param routeChanges a set of route-path changes, where each route-path is |
| 322 | * a list with its first element the src-switch of the path |
| 323 | * and the second element the dst-switch of the path. |
| 324 | * @param linkOrSwitchFailed true if the route changes are for a failed |
| 325 | * switch or linkDown event |
| 326 | * @param failedSwitch the switchId if the route changes are for a failed switch, |
| 327 | * otherwise null |
| 328 | */ |
| 329 | private void processHashGroupChange(Set<ArrayList<DeviceId>> routeChanges, |
| 330 | boolean linkOrSwitchFailed, |
| 331 | DeviceId failedSwitch) { |
| 332 | for (ArrayList<DeviceId> route : routeChanges) { |
| 333 | DeviceId targetSw = route.get(0); |
| 334 | boolean success; |
| 335 | DeviceId dstSw = null; |
| 336 | if (route.size() > 1) { |
| 337 | dstSw = route.get(1); |
| 338 | } |
| 339 | |
| 340 | if (linkOrSwitchFailed) { |
| 341 | success = fixHashGroupsForRoute(route, true); |
| 342 | // it's possible that we cannot fix hash groups for a route |
| 343 | // if the target switch has failed. Nevertheless the ecmp graph |
| 344 | // for the impacted switch must still be updated. |
| 345 | if (failedSwitch != null && targetSw.equals(failedSwitch) |
| 346 | && dstSw != null) { |
| 347 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 348 | currentEcmpSpgMap.remove(targetSw); |
| 349 | log.debug("Updating ECMPspg for dst:{} removing failed " |
| 350 | + "target:{}", dstSw, targetSw); |
| 351 | return; |
| 352 | } |
| 353 | //linkfailed - update both sides |
| 354 | currentEcmpSpgMap.put(targetSw, updatedEcmpSpgMap.get(targetSw)); |
| 355 | dstSw = route.get(1); |
| 356 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 357 | log.debug("Updating ECMPspg for dst:{} and target:{}", dstSw, targetSw); |
| 358 | } else { |
| 359 | success = fixHashGroupsForRoute(route, false); |
| 360 | if (success) { |
| 361 | currentEcmpSpgMap.put(targetSw, updatedEcmpSpgMap.get(targetSw)); |
| 362 | if (dstSw != null) { |
| 363 | currentEcmpSpgMap.put(dstSw, updatedEcmpSpgMap.get(dstSw)); |
| 364 | } |
| 365 | log.debug("Updating ECMPspg for target:{} and dst:{}", |
| 366 | targetSw, dstSw); |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Edits hash groups in the src-switch (targetSw) of a route-path by |
| 374 | * calling the groupHandler to either add or remove buckets in an existing |
| 375 | * hash group. |
| 376 | * |
| 377 | * @param route a single list representing a route-path where the first element |
| 378 | * is the src-switch (targetSw) of the route-path and the |
| 379 | * second element is the dst-switch |
| 380 | * @param revoke true if buckets in the hash-groups need to be removed; |
| 381 | * false if buckets in the hash-groups need to be added |
| 382 | * @return true if the hash group editing is successful |
| 383 | */ |
| 384 | private boolean fixHashGroupsForRoute(ArrayList<DeviceId> route, |
| 385 | boolean revoke) { |
| 386 | DeviceId targetSw = route.get(0); |
| 387 | if (route.size() < 2) { |
| 388 | log.warn("Cannot fixHashGroupsForRoute - no dstSw in route {}", route); |
| 389 | return false; |
| 390 | } |
| 391 | DeviceId destSw = route.get(1); |
| 392 | log.debug("Processing fixHashGroupsForRoute: Target {} -> Dest {}", |
| 393 | targetSw, destSw); |
| 394 | boolean targetIsEdge = false; |
| 395 | try { |
| 396 | targetIsEdge = srManager.deviceConfiguration.isEdgeDevice(targetSw); |
| 397 | } catch (DeviceConfigNotFoundException e) { |
| 398 | log.warn(e.getMessage() + "Cannot determine if targetIsEdge {}.. " |
| 399 | + "continuing fixHash", targetSw); |
| 400 | } |
| 401 | |
| 402 | // figure out the new next hops at the targetSw towards the destSw |
| 403 | Set<DeviceId> nextHops = new HashSet<>(); |
| 404 | EcmpShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(destSw); |
| 405 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 406 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 407 | for (Integer itrIdx : switchVia.keySet()) { |
| 408 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 409 | switchVia.get(itrIdx); |
| 410 | for (DeviceId target : swViaMap.keySet()) { |
| 411 | if (target.equals(targetSw)) { |
| 412 | // found the iteration where targetSw is reached- get nextHops |
| 413 | if (!targetIsEdge && itrIdx > 1) { |
| 414 | // optimization for spines to not use other leaves to get |
| 415 | // to a leaf to avoid loops |
| 416 | log.debug("Avoiding {} hop path for non-edge targetSw:{}" |
| 417 | + " --> dstSw:{}", itrIdx, targetSw, destSw); |
| 418 | break; |
| 419 | } |
| 420 | for (ArrayList<DeviceId> via : swViaMap.get(target)) { |
| 421 | if (via.isEmpty()) { |
| 422 | nextHops.add(destSw); |
| 423 | } else { |
| 424 | // first elem is next-hop in each ECMP path |
| 425 | nextHops.add(via.get(0)); |
| 426 | } |
| 427 | } |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // call group handler to change hash group at targetSw |
| 434 | DefaultGroupHandler grpHandler = srManager.getGroupHandler(targetSw); |
| 435 | if (grpHandler == null) { |
| 436 | log.warn("Cannot find grouphandler for dev:{} .. aborting" |
| 437 | + " {} hash group buckets for route:{} ", targetSw, |
| 438 | (revoke) ? "revoke" : "repopulate", route); |
| 439 | return false; |
| 440 | } |
| 441 | log.debug("{} hash-groups buckets For Route {} -> {} to next-hops {}", |
| 442 | (revoke) ? "revoke" : "repopulating", |
| 443 | targetSw, destSw, nextHops); |
| 444 | return (revoke) ? grpHandler.fixHashGroups(targetSw, nextHops, |
| 445 | destSw, true) |
| 446 | : grpHandler.fixHashGroups(targetSw, nextHops, |
| 447 | destSw, false); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Processes a set a route-path changes by reprogramming routing rules and |
| 452 | * creating new hash-groups if necessary. |
| 453 | * |
| 454 | * @param routeChanges a set of route-path changes, where each route-path is |
| 455 | * a list with its first element the src-switch of the path |
| 456 | * and the second element the dst-switch of the path. |
| 457 | * @return true if successful in repopulating routes |
| 458 | */ |
| 459 | private boolean repopulateRoutingRulesForRoutes(Set<ArrayList<DeviceId>> routeChanges) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 460 | rulePopulator.resetCounter(); |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 461 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> routesBydevice = |
| 462 | new HashMap<>(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 463 | for (ArrayList<DeviceId> link: routeChanges) { |
sangho | 2165d22 | 2015-05-01 09:38:25 -0700 | [diff] [blame] | 464 | // When only the source device is defined, reinstall routes to all other devices |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 465 | if (link.size() == 1) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 466 | log.debug("-- repopulateRoutingRulesForRoutes: running ECMP graph for device {}", link.get(0)); |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 467 | EcmpShortestPathGraph ecmpSpg = new EcmpShortestPathGraph(link.get(0), srManager); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 468 | if (populateEcmpRoutingRules(link.get(0), ecmpSpg, ImmutableSet.of())) { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 469 | log.debug("Populating flow rules from all to dest:{} is successful", |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 470 | link.get(0)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 471 | currentEcmpSpgMap.put(link.get(0), ecmpSpg); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 472 | log.debug("Updating ECMPspg for dest:{}", link.get(0)); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 473 | } else { |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 474 | log.warn("Failed to populate the flow rules from all to dest:{}", link.get(0)); |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 475 | return false; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 476 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 477 | } else { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 478 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 479 | routesBydevice.get(link.get(1)); |
| 480 | if (deviceRoutes == null) { |
| 481 | deviceRoutes = new ArrayList<>(); |
| 482 | routesBydevice.put(link.get(1), deviceRoutes); |
| 483 | } |
| 484 | deviceRoutes.add(link); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | for (DeviceId impactedDevice : routesBydevice.keySet()) { |
| 489 | ArrayList<ArrayList<DeviceId>> deviceRoutes = |
| 490 | routesBydevice.get(impactedDevice); |
| 491 | for (ArrayList<DeviceId> link: deviceRoutes) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 492 | log.debug("-- repopulateRoutingRulesForRoutes {} -> {}", |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 493 | link.get(0), link.get(1)); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 494 | DeviceId src = link.get(0); |
| 495 | DeviceId dst = link.get(1); |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 496 | EcmpShortestPathGraph ecmpSpg = updatedEcmpSpgMap.get(dst); |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 497 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 498 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 499 | for (Integer itrIdx : switchVia.keySet()) { |
| 500 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 501 | switchVia.get(itrIdx); |
| 502 | for (DeviceId targetSw : swViaMap.keySet()) { |
| 503 | if (!targetSw.equals(src)) { |
| 504 | continue; |
| 505 | } |
| 506 | Set<DeviceId> nextHops = new HashSet<>(); |
| 507 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 508 | // in this ECMP path to the targetSw, get the next hop |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 509 | if (via.isEmpty()) { |
| 510 | nextHops.add(dst); |
| 511 | } else { |
| 512 | nextHops.add(via.get(0)); |
| 513 | } |
| 514 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 515 | if (!populateEcmpRoutingRulePartial(targetSw, dst, |
| 516 | nextHops, ImmutableSet.of())) { |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 517 | return false; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 518 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 519 | log.debug("Populating flow rules from {} to {} is successful", |
| 520 | targetSw, dst); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 521 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 524 | //Only if all the flows for all impacted routes to a |
| 525 | //specific target are pushed successfully, update the |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 526 | //ECMP graph for that target. Or else the next event |
| 527 | //would not see any changes in the ECMP graphs. |
| 528 | //In another case, the target switch has gone away, so |
| 529 | //routes can't be installed. In that case, the current map |
| 530 | //is updated here, without any flows being pushed. |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 531 | currentEcmpSpgMap.put(impactedDevice, |
| 532 | updatedEcmpSpgMap.get(impactedDevice)); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 533 | log.debug("Updating ECMPspg for impacted dev:{}", impactedDevice); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 534 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 535 | |
| 536 | processHashGroupChange(routeChanges, false, null); |
| 537 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 538 | return true; |
| 539 | } |
| 540 | |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 541 | /** |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 542 | * Computes set of affected routes due to failed link. Assumes |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 543 | * previous ecmp shortest-path graph exists for a switch in order to compute |
| 544 | * affected routes. If such a graph does not exist, the method returns null. |
| 545 | * |
| 546 | * @param linkFail the failed link |
| 547 | * @return the set of affected routes which may be empty if no routes were |
| 548 | * affected, or null if no previous ecmp spg was found for comparison |
| 549 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 550 | private Set<ArrayList<DeviceId>> computeDamagedRoutes(Link linkFail) { |
| 551 | |
| 552 | Set<ArrayList<DeviceId>> routes = new HashSet<>(); |
| 553 | |
| 554 | for (Device sw : srManager.deviceService.getDevices()) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 555 | log.debug("Computing the impacted routes for device {} due to link fail", |
| 556 | sw.id()); |
Charles Chan | 7727767 | 2015-10-20 16:24:19 -0700 | [diff] [blame] | 557 | if (!srManager.mastershipService.isLocalMaster(sw.id())) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 558 | log.debug("No mastership for {} .. skipping route optimization", |
| 559 | sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 560 | continue; |
| 561 | } |
Shashikanth VH | 0637b16 | 2015-12-11 01:32:44 +0530 | [diff] [blame] | 562 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(sw.id()); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 563 | if (ecmpSpg == null) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 564 | log.warn("No existing ECMP graph for switch {}. Aborting optimized" |
| 565 | + " rerouting and opting for full-reroute", sw.id()); |
| 566 | return null; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 567 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 568 | if (log.isDebugEnabled()) { |
| 569 | log.debug("Root switch: {}", sw.id()); |
| 570 | log.debug(" Current/Existing SPG: {}", ecmpSpg); |
| 571 | log.debug(" New/Updated SPG: {}", updatedEcmpSpgMap.get(sw.id())); |
| 572 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 573 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = |
| 574 | ecmpSpg.getAllLearnedSwitchesAndVia(); |
| 575 | for (Integer itrIdx : switchVia.keySet()) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 576 | log.trace("Current/Exiting SPG Iterindex# {}", itrIdx); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 577 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 578 | switchVia.get(itrIdx); |
| 579 | for (DeviceId targetSw : swViaMap.keySet()) { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 580 | DeviceId rootSw = sw.id(); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 581 | if (log.isTraceEnabled()) { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 582 | log.trace("TargetSwitch {} --> RootSwitch {}", targetSw, rootSw); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 583 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 584 | log.trace(" Via:"); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 585 | via.forEach(e -> log.trace(" {}", e)); |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 588 | Set<ArrayList<DeviceId>> subLinks = |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 589 | computeLinks(targetSw, rootSw, swViaMap); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 590 | for (ArrayList<DeviceId> alink: subLinks) { |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 591 | if ((alink.get(0).equals(linkFail.src().deviceId()) && |
| 592 | alink.get(1).equals(linkFail.dst().deviceId())) |
| 593 | || |
| 594 | (alink.get(0).equals(linkFail.dst().deviceId()) && |
| 595 | alink.get(1).equals(linkFail.src().deviceId()))) { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 596 | log.debug("Impacted route:{}->{}", targetSw, rootSw); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 597 | ArrayList<DeviceId> aRoute = new ArrayList<>(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 598 | aRoute.add(targetSw); // switch with rules to populate |
| 599 | aRoute.add(rootSw); // towards this destination |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 600 | routes.add(aRoute); |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 606 | |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | return routes; |
| 610 | } |
| 611 | |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 612 | /** |
| 613 | * Computes set of affected routes due to new links or failed switches. |
| 614 | * |
| 615 | * @return the set of affected routes which may be empty if no routes were |
| 616 | * affected |
| 617 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 618 | private Set<ArrayList<DeviceId>> computeRouteChange() { |
| 619 | |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 620 | ImmutableSet.Builder<ArrayList<DeviceId>> changedRoutesBuilder = |
| 621 | ImmutableSet.builder(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 622 | |
| 623 | for (Device sw : srManager.deviceService.getDevices()) { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 624 | DeviceId rootSw = sw.id(); |
| 625 | log.debug("Computing the impacted routes for device {}", rootSw); |
| 626 | if (!srManager.mastershipService.isLocalMaster(rootSw)) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 627 | log.debug("No mastership for {} ... skipping route optimization", |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 628 | rootSw); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 629 | continue; |
| 630 | } |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 631 | if (log.isTraceEnabled()) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 632 | log.trace("Device links for dev: {}", rootSw); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 633 | for (Link link: srManager.linkService.getDeviceLinks(rootSw)) { |
Saurav Das | b149be1 | 2016-06-07 10:08:06 -0700 | [diff] [blame] | 634 | log.trace("{} -> {} ", link.src().deviceId(), link.dst().deviceId()); |
| 635 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 636 | } |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 637 | EcmpShortestPathGraph currEcmpSpg = currentEcmpSpgMap.get(rootSw); |
| 638 | if (currEcmpSpg == null) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 639 | log.debug("No existing ECMP graph for device {}.. adding self as " |
| 640 | + "changed route", rootSw); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 641 | changedRoutesBuilder.add(Lists.newArrayList(rootSw)); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 642 | continue; |
| 643 | } |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 644 | EcmpShortestPathGraph newEcmpSpg = updatedEcmpSpgMap.get(rootSw); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 645 | if (log.isDebugEnabled()) { |
| 646 | log.debug("Root switch: {}", rootSw); |
| 647 | log.debug(" Current/Existing SPG: {}", currEcmpSpg); |
| 648 | log.debug(" New/Updated SPG: {}", newEcmpSpg); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 649 | } |
| 650 | // first use the updated/new map to compare to current/existing map |
| 651 | // as new links may have come up |
| 652 | changedRoutesBuilder.addAll(compareGraphs(newEcmpSpg, currEcmpSpg, rootSw)); |
| 653 | // then use the current/existing map to compare to updated/new map |
| 654 | // as switch may have been removed |
| 655 | changedRoutesBuilder.addAll(compareGraphs(currEcmpSpg, newEcmpSpg, rootSw)); |
| 656 | } |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 657 | |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 658 | Set<ArrayList<DeviceId>> changedRoutes = changedRoutesBuilder.build(); |
| 659 | for (ArrayList<DeviceId> route: changedRoutes) { |
| 660 | log.debug("Route changes Target -> Root"); |
| 661 | if (route.size() == 1) { |
| 662 | log.debug(" : all -> {}", route.get(0)); |
| 663 | } else { |
| 664 | log.debug(" : {} -> {}", route.get(0), route.get(1)); |
| 665 | } |
| 666 | } |
| 667 | return changedRoutes; |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * For the root switch, searches all the target nodes reachable in the base |
| 672 | * graph, and compares paths to the ones in the comp graph. |
| 673 | * |
| 674 | * @param base the graph that is indexed for all reachable target nodes |
| 675 | * from the root node |
| 676 | * @param comp the graph that the base graph is compared to |
| 677 | * @param rootSw both ecmp graphs are calculated for the root node |
| 678 | * @return all the routes that have changed in the base graph |
| 679 | */ |
| 680 | private Set<ArrayList<DeviceId>> compareGraphs(EcmpShortestPathGraph base, |
| 681 | EcmpShortestPathGraph comp, |
| 682 | DeviceId rootSw) { |
| 683 | ImmutableSet.Builder<ArrayList<DeviceId>> changedRoutesBuilder = |
| 684 | ImmutableSet.builder(); |
| 685 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> baseMap = |
| 686 | base.getAllLearnedSwitchesAndVia(); |
| 687 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> compMap = |
| 688 | comp.getAllLearnedSwitchesAndVia(); |
| 689 | for (Integer itrIdx : baseMap.keySet()) { |
| 690 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> baseViaMap = |
| 691 | baseMap.get(itrIdx); |
| 692 | for (DeviceId targetSw : baseViaMap.keySet()) { |
| 693 | ArrayList<ArrayList<DeviceId>> basePath = baseViaMap.get(targetSw); |
| 694 | ArrayList<ArrayList<DeviceId>> compPath = getVia(compMap, targetSw); |
| 695 | if ((compPath == null) || !basePath.equals(compPath)) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 696 | log.trace("Impacted route:{} -> {}", targetSw, rootSw); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 697 | ArrayList<DeviceId> route = new ArrayList<>(); |
| 698 | route.add(targetSw); |
| 699 | route.add(rootSw); |
| 700 | changedRoutesBuilder.add(route); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
sangho | 28d0b6d | 2015-05-07 13:30:57 -0700 | [diff] [blame] | 703 | } |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 704 | return changedRoutesBuilder.build(); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | private ArrayList<ArrayList<DeviceId>> getVia(HashMap<Integer, HashMap<DeviceId, |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 708 | ArrayList<ArrayList<DeviceId>>>> switchVia, DeviceId targetSw) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 709 | for (Integer itrIdx : switchVia.keySet()) { |
| 710 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = |
| 711 | switchVia.get(itrIdx); |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 712 | if (swViaMap.get(targetSw) == null) { |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 713 | continue; |
| 714 | } else { |
Saurav Das | 1b391d5 | 2016-11-29 14:27:25 -0800 | [diff] [blame] | 715 | return swViaMap.get(targetSw); |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Srikanth Vavilapalli | 64d96c1 | 2015-05-14 20:22:47 -0700 | [diff] [blame] | 719 | return null; |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | private Set<ArrayList<DeviceId>> computeLinks(DeviceId src, |
| 723 | DeviceId dst, |
| 724 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> viaMap) { |
| 725 | Set<ArrayList<DeviceId>> subLinks = Sets.newHashSet(); |
| 726 | for (ArrayList<DeviceId> via : viaMap.get(src)) { |
| 727 | DeviceId linkSrc = src; |
| 728 | DeviceId linkDst = dst; |
| 729 | for (DeviceId viaDevice: via) { |
| 730 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 731 | linkDst = viaDevice; |
| 732 | link.add(linkSrc); |
| 733 | link.add(linkDst); |
| 734 | subLinks.add(link); |
| 735 | linkSrc = viaDevice; |
| 736 | } |
| 737 | ArrayList<DeviceId> link = new ArrayList<>(); |
| 738 | link.add(linkSrc); |
| 739 | link.add(dst); |
| 740 | subLinks.add(link); |
| 741 | } |
| 742 | |
| 743 | return subLinks; |
| 744 | } |
| 745 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 746 | /** |
| 747 | * Populate ECMP rules for subnets from all switches to destination. |
| 748 | * |
| 749 | * @param destSw Device ID of destination switch |
| 750 | * @param ecmpSPG ECMP shortest path graph |
| 751 | * @param subnets Subnets to be populated. If empty, populate all configured subnets. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 752 | * @return true if it succeeds in populating rules |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 753 | */ |
sangho | fb7c729 | 2015-04-13 15:15:58 -0700 | [diff] [blame] | 754 | private boolean populateEcmpRoutingRules(DeviceId destSw, |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 755 | EcmpShortestPathGraph ecmpSPG, |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 756 | Set<IpPrefix> subnets) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 757 | |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 758 | HashMap<Integer, HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>>> switchVia = ecmpSPG |
| 759 | .getAllLearnedSwitchesAndVia(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 760 | for (Integer itrIdx : switchVia.keySet()) { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 761 | HashMap<DeviceId, ArrayList<ArrayList<DeviceId>>> swViaMap = switchVia |
| 762 | .get(itrIdx); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 763 | for (DeviceId targetSw : swViaMap.keySet()) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 764 | Set<DeviceId> nextHops = new HashSet<>(); |
Saurav Das | 8897918 | 2015-10-19 14:37:36 -0700 | [diff] [blame] | 765 | log.debug("** Iter: {} root: {} target: {}", itrIdx, destSw, targetSw); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 766 | for (ArrayList<DeviceId> via : swViaMap.get(targetSw)) { |
| 767 | if (via.isEmpty()) { |
| 768 | nextHops.add(destSw); |
| 769 | } else { |
| 770 | nextHops.add(via.get(0)); |
| 771 | } |
| 772 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 773 | if (!populateEcmpRoutingRulePartial(targetSw, destSw, nextHops, subnets)) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 774 | return false; |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | return true; |
| 780 | } |
| 781 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 782 | /** |
| 783 | * Populate ECMP rules for subnets from target to destination via nexthops. |
| 784 | * |
Saurav Das | e0237a3 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 785 | * @param targetSw Device ID of target switch in which rules will be programmed |
| 786 | * @param destSw Device ID of final destination switch to which the rules will forward |
| 787 | * @param nextHops List of next hops via which destSw will be reached |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 788 | * @param subnets Subnets to be populated. If empty, populate all configured subnets. |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 789 | * @return true if it succees in populating rules |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 790 | */ |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 791 | private boolean populateEcmpRoutingRulePartial(DeviceId targetSw, |
| 792 | DeviceId destSw, |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 793 | Set<DeviceId> nextHops, |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 794 | Set<IpPrefix> subnets) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 795 | boolean result; |
| 796 | |
| 797 | if (nextHops.isEmpty()) { |
| 798 | nextHops.add(destSw); |
| 799 | } |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 800 | // If both target switch and dest switch are edge routers, then set IP |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 801 | // rule for both subnet and router IP. |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 802 | boolean targetIsEdge; |
| 803 | boolean destIsEdge; |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 804 | Ip4Address destRouterIpv4; |
| 805 | Ip6Address destRouterIpv6; |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 806 | |
| 807 | try { |
| 808 | targetIsEdge = config.isEdgeDevice(targetSw); |
| 809 | destIsEdge = config.isEdgeDevice(destSw); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 810 | destRouterIpv4 = config.getRouterIpv4(destSw); |
| 811 | destRouterIpv6 = config.getRouterIpv6(destSw); |
Charles Chan | 319d1a2 | 2015-11-03 10:42:14 -0800 | [diff] [blame] | 812 | } catch (DeviceConfigNotFoundException e) { |
| 813 | log.warn(e.getMessage() + " Aborting populateEcmpRoutingRulePartial."); |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | if (targetIsEdge && destIsEdge) { |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 818 | subnets = (subnets != null && !subnets.isEmpty()) ? subnets |
| 819 | : config.getSubnets(destSw); |
| 820 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} " |
| 821 | + "for subnets {}", targetSw, destSw, subnets); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 822 | result = rulePopulator.populateIpRuleForSubnet(targetSw, subnets, |
| 823 | destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 824 | if (!result) { |
| 825 | return false; |
| 826 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 827 | IpPrefix routerIpPrefix = destRouterIpv4.toIpPrefix(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 828 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {} " |
| 829 | + "for router IP {}", targetSw, destSw, routerIpPrefix); |
| 830 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, |
| 831 | destSw, nextHops); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 832 | if (!result) { |
| 833 | return false; |
| 834 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 835 | // If present we deal with IPv6 loopback. |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 836 | if (destRouterIpv6 != null) { |
| 837 | routerIpPrefix = destRouterIpv6.toIpPrefix(); |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 838 | log.debug("* populateEcmpRoutingRulePartial in device {} towards {}" |
| 839 | + " for v6 router IP {}", targetSw, destSw, routerIpPrefix); |
| 840 | result = rulePopulator.populateIpRuleForRouter(targetSw, routerIpPrefix, |
| 841 | destSw, nextHops); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 842 | if (!result) { |
| 843 | return false; |
| 844 | } |
| 845 | } |
sangho | df0153f | 2015-05-05 14:13:34 -0700 | [diff] [blame] | 846 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 847 | |
| 848 | if (!targetIsEdge && destIsEdge) { |
| 849 | // MPLS rules in all non-edge target devices |
| 850 | log.debug("* populateEcmpRoutingRulePartial in device{} towards {} for " |
| 851 | + "all MPLS rules", targetSw, destSw); |
| 852 | result = rulePopulator.populateMplsRule(targetSw, destSw, nextHops, |
| 853 | destRouterIpv4); |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 854 | if (!result) { |
| 855 | return false; |
| 856 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 857 | if (destRouterIpv6 != null) { |
| 858 | result = rulePopulator.populateMplsRule(targetSw, destSw, nextHops, |
| 859 | destRouterIpv6); |
| 860 | if (!result) { |
| 861 | return false; |
| 862 | } |
| 863 | } |
Pier Ventre | adb4ae6 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 864 | } |
Saurav Das | 62ae679 | 2017-05-15 15:34:25 -0700 | [diff] [blame] | 865 | |
| 866 | // To save on ECMP groups |
| 867 | // avoid MPLS rules in non-edge-devices to non-edge-devices |
| 868 | // avoid MPLS transit rules in edge-devices |
| 869 | // avoid loopback IP rules in edge-devices to non-edge-devices |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 870 | return true; |
| 871 | } |
| 872 | |
| 873 | /** |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 874 | * Populates filtering rules for port, and punting rules |
| 875 | * for gateway IPs, loopback IPs and arp/ndp traffic. |
| 876 | * Should only be called by the master instance for this device/port. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 877 | * |
| 878 | * @param deviceId Switch ID to set the rules |
| 879 | */ |
Saurav Das | 9f1c42e | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 880 | public void populatePortAddressingRules(DeviceId deviceId) { |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 881 | rulePopulator.populateIpPunts(deviceId); |
Pier Luigi | b9632ba | 2017-01-12 18:14:58 -0800 | [diff] [blame] | 882 | rulePopulator.populateArpNdpPunts(deviceId); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 883 | |
| 884 | // Although device is added, sometimes device store does not have the |
| 885 | // ports for this device yet. It results in missing filtering rules in the |
| 886 | // switch. We will attempt it a few times. If it still does not work, |
| 887 | // user can manually repopulate using CLI command sr-reroute-network |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 888 | PortFilterInfo firstRun = rulePopulator.populateVlanMacFilters(deviceId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 889 | if (firstRun == null) { |
| 890 | firstRun = new PortFilterInfo(0, 0, 0); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 891 | } |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 892 | executorService.schedule(new RetryFilters(deviceId, firstRun), |
| 893 | RETRY_INTERVAL_MS, TimeUnit.MILLISECONDS); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /** |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 897 | * Start the flow rule population process if it was never started. The |
| 898 | * process finishes successfully when all flow rules are set and stops with |
| 899 | * ABORTED status when any groups required for flows is not set yet. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 900 | */ |
| 901 | public void startPopulationProcess() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 902 | statusLock.lock(); |
| 903 | try { |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 904 | if (populationStatus == Status.IDLE |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 905 | || populationStatus == Status.SUCCEEDED |
| 906 | || populationStatus == Status.ABORTED) { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 907 | populationStatus = Status.STARTED; |
| 908 | populateAllRoutingRules(); |
Srikanth Vavilapalli | 7cd1671 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 909 | } else { |
| 910 | log.warn("Not initiating startPopulationProcess as populationStatus is {}", |
| 911 | populationStatus); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 912 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 913 | } finally { |
| 914 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * Resume the flow rule population process if it was aborted for any reason. |
| 920 | * 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] | 921 | * XXX is this called? |
| 922 | * |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 923 | */ |
| 924 | public void resumePopulationProcess() { |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 925 | statusLock.lock(); |
| 926 | try { |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 927 | if (populationStatus == Status.ABORTED) { |
| 928 | populationStatus = Status.STARTED; |
Srikanth Vavilapalli | 6450548 | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 929 | // TODO: we need to restart from the point aborted instead of |
| 930 | // restarting. |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 931 | populateAllRoutingRules(); |
| 932 | } |
HIGUCHI Yuta | 16d8fd5 | 2015-09-08 16:16:31 +0900 | [diff] [blame] | 933 | } finally { |
| 934 | statusLock.unlock(); |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 937 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 938 | /** |
| 939 | * Populate rules of given subnet at given location. |
| 940 | * |
| 941 | * @param cp connect point of the subnet being added |
| 942 | * @param subnets subnet being added |
| 943 | * @return true if succeed |
| 944 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 945 | protected boolean populateSubnet(ConnectPoint cp, Set<IpPrefix> subnets) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 946 | statusLock.lock(); |
| 947 | try { |
| 948 | EcmpShortestPathGraph ecmpSpg = currentEcmpSpgMap.get(cp.deviceId()); |
| 949 | if (ecmpSpg == null) { |
| 950 | log.warn("Fail to populating subnet {}: {}", subnets, ECMPSPG_MISSING); |
| 951 | return false; |
| 952 | } |
| 953 | return populateEcmpRoutingRules(cp.deviceId(), ecmpSpg, subnets); |
| 954 | } finally { |
| 955 | statusLock.unlock(); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | /** |
| 960 | * Revoke rules of given subnet at given location. |
| 961 | * |
| 962 | * @param subnets subnet being removed |
| 963 | * @return true if succeed |
| 964 | */ |
Pier Ventre | b6a7f34 | 2016-11-26 21:05:22 -0800 | [diff] [blame] | 965 | protected boolean revokeSubnet(Set<IpPrefix> subnets) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 966 | statusLock.lock(); |
| 967 | try { |
| 968 | return srManager.routingRulePopulator.revokeIpRuleForSubnet(subnets); |
| 969 | } finally { |
| 970 | statusLock.unlock(); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | protected void purgeEcmpGraph(DeviceId deviceId) { |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 975 | currentEcmpSpgMap.remove(deviceId); |
Saurav Das | 52d4ed7 | 2016-03-28 19:00:18 -0700 | [diff] [blame] | 976 | if (updatedEcmpSpgMap != null) { |
| 977 | updatedEcmpSpgMap.remove(deviceId); |
| 978 | } |
Saurav Das | c3604f1 | 2016-03-23 11:22:49 -0700 | [diff] [blame] | 979 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 980 | |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 981 | /** |
| 982 | * Utility class used to temporarily store information about the ports on a |
| 983 | * device processed for filtering objectives. |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 984 | */ |
| 985 | public final class PortFilterInfo { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 986 | int disabledPorts = 0, errorPorts = 0, filteredPorts = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 987 | |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 988 | public PortFilterInfo(int disabledPorts, int errorPorts, |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 989 | int filteredPorts) { |
| 990 | this.disabledPorts = disabledPorts; |
| 991 | this.filteredPorts = filteredPorts; |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 992 | this.errorPorts = errorPorts; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | @Override |
| 996 | public int hashCode() { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 997 | return Objects.hash(disabledPorts, filteredPorts, errorPorts); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | @Override |
| 1001 | public boolean equals(Object obj) { |
| 1002 | if (this == obj) { |
| 1003 | return true; |
| 1004 | } |
| 1005 | if ((obj == null) || (!(obj instanceof PortFilterInfo))) { |
| 1006 | return false; |
| 1007 | } |
| 1008 | PortFilterInfo other = (PortFilterInfo) obj; |
| 1009 | return ((disabledPorts == other.disabledPorts) && |
| 1010 | (filteredPorts == other.filteredPorts) && |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1011 | (errorPorts == other.errorPorts)); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | @Override |
| 1015 | public String toString() { |
| 1016 | MoreObjects.ToStringHelper helper = toStringHelper(this) |
| 1017 | .add("disabledPorts", disabledPorts) |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1018 | .add("errorPorts", errorPorts) |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1019 | .add("filteredPorts", filteredPorts); |
| 1020 | return helper.toString(); |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * RetryFilters populates filtering objectives for a device and keeps retrying |
| 1026 | * till the number of ports filtered are constant for a predefined number |
| 1027 | * of attempts. |
| 1028 | */ |
| 1029 | protected final class RetryFilters implements Runnable { |
| 1030 | int constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; |
| 1031 | DeviceId devId; |
| 1032 | int counter; |
| 1033 | PortFilterInfo prevRun; |
| 1034 | |
| 1035 | private RetryFilters(DeviceId deviceId, PortFilterInfo previousRun) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1036 | devId = deviceId; |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1037 | prevRun = previousRun; |
| 1038 | counter = 0; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | @Override |
| 1042 | public void run() { |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1043 | log.info("RETRY FILTER ATTEMPT {} ** dev:{}", ++counter, devId); |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 1044 | PortFilterInfo thisRun = rulePopulator.populateVlanMacFilters(devId); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1045 | boolean sameResult = prevRun.equals(thisRun); |
| 1046 | log.debug("dev:{} prevRun:{} thisRun:{} sameResult:{}", devId, prevRun, |
| 1047 | thisRun, sameResult); |
| 1048 | if (thisRun == null || !sameResult || (sameResult && --constantAttempts > 0)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 1049 | // exponentially increasing intervals for retries |
| 1050 | executorService.schedule(this, |
| 1051 | RETRY_INTERVAL_MS * (int) Math.pow(counter, RETRY_INTERVAL_SCALE), |
| 1052 | TimeUnit.MILLISECONDS); |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1053 | if (!sameResult) { |
| 1054 | constantAttempts = MAX_CONSTANT_RETRY_ATTEMPTS; //reset |
| 1055 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1056 | } |
Saurav Das | d1872b0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 1057 | prevRun = (thisRun == null) ? prevRun : thisRun; |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1058 | } |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
sangho | 80f11cb | 2015-04-01 13:05:26 -0700 | [diff] [blame] | 1061 | } |