Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
Saurav Das | 261c300 | 2017-06-13 15:35:54 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
| 20 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onlab.packet.MacAddress; |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 23 | import org.onlab.packet.VlanId; |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 25 | import org.onosproject.net.Host; |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 26 | import org.onosproject.net.HostLocation; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.host.HostEvent; |
Ray Milkey | a815431 | 2017-08-08 13:00:43 -0700 | [diff] [blame] | 29 | import org.onosproject.routeservice.ResolvedRoute; |
| 30 | import org.onosproject.routeservice.RouteEvent; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 32 | import org.onosproject.routeservice.RouteInfo; |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 36 | import java.util.List; |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 37 | import java.util.Collection; |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 38 | import java.util.Objects; |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 39 | import java.util.Optional; |
| 40 | import java.util.Set; |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 41 | import java.util.stream.Collectors; |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 42 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Handles RouteEvent and manages routing entries. |
| 45 | */ |
| 46 | public class RouteHandler { |
| 47 | private static final Logger log = LoggerFactory.getLogger(RouteHandler.class); |
| 48 | private final SegmentRoutingManager srManager; |
| 49 | |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 50 | RouteHandler(SegmentRoutingManager srManager) { |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 51 | this.srManager = srManager; |
| 52 | } |
| 53 | |
| 54 | protected void init(DeviceId deviceId) { |
Charles Chan | 7d20a4e | 2018-04-13 14:01:49 -0400 | [diff] [blame] | 55 | srManager.routeService.getRouteTables().stream() |
| 56 | .map(srManager.routeService::getRoutes) |
| 57 | .flatMap(Collection::stream) |
| 58 | .map(RouteInfo::allRoutes) |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 59 | .filter(allRoutes -> allRoutes.stream().anyMatch(resolvedRoute -> |
| 60 | srManager.nextHopLocations(resolvedRoute).stream() |
| 61 | .anyMatch(cp -> deviceId.equals(cp.deviceId())))) |
| 62 | .forEach(rr -> processRouteAddedInternal(rr, true)); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 65 | void processRouteAdded(RouteEvent event) { |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 66 | processRouteAddedInternal(event.alternatives(), false); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Internal logic that handles route addition. |
| 71 | * |
| 72 | * @param routes collection of routes to be processed |
| 73 | * @param populateRouteOnly true if we only want to populateRoute but not populateSubnet. |
| 74 | * Set it to true when initializing a device coming up. |
| 75 | * populateSubnet will be done when link comes up later so it is redundant. |
| 76 | * populateRoute still needs to be done for statically configured next hop hosts. |
| 77 | */ |
| 78 | private void processRouteAddedInternal(Collection<ResolvedRoute> routes, boolean populateRouteOnly) { |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 79 | if (!isReady()) { |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 80 | log.info("System is not ready. Skip adding route for {}", routes); |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 81 | return; |
| 82 | } |
| 83 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 84 | log.info("processRouteAddedInternal. routes={}", routes); |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 85 | |
Charles Chan | 482b642 | 2018-04-09 11:52:08 -0400 | [diff] [blame] | 86 | if (routes.size() > 2) { |
| 87 | log.info("Route {} has more than two next hops. Do not process route change", routes); |
| 88 | return; |
| 89 | } |
| 90 | |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 91 | ResolvedRoute rr = routes.stream().findFirst().orElse(null); |
| 92 | if (rr == null) { |
| 93 | log.warn("No resolved route found. Abort processRouteAddedInternal"); |
| 94 | } |
| 95 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 96 | Set<ConnectPoint> allLocations = Sets.newHashSet(); |
| 97 | Set<IpPrefix> allPrefixes = Sets.newHashSet(); |
| 98 | routes.forEach(route -> { |
| 99 | allLocations.addAll(srManager.nextHopLocations(route)); |
| 100 | allPrefixes.add(route.prefix()); |
| 101 | }); |
| 102 | log.debug("RouteAdded. populateSubnet {}, {}", allLocations, allPrefixes); |
| 103 | srManager.defaultRoutingHandler.populateSubnet(allLocations, allPrefixes); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 104 | |
Charles Chan | 12a8a84 | 2020-02-14 13:23:57 -0800 | [diff] [blame] | 105 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 106 | routes.forEach(route -> { |
| 107 | IpPrefix prefix = route.prefix(); |
| 108 | MacAddress nextHopMac = route.nextHopMac(); |
| 109 | VlanId nextHopVlan = route.nextHopVlan(); |
| 110 | Set<ConnectPoint> locations = srManager.nextHopLocations(route); |
| 111 | |
| 112 | locations.forEach(location -> { |
| 113 | log.debug("RouteAdded. addSubnet {}, {}", location, prefix); |
| 114 | srManager.deviceConfiguration.addSubnet(location, prefix); |
| 115 | log.debug("RouteAdded populateRoute {}, {}, {}, {}", location, prefix, nextHopMac, nextHopVlan); |
| 116 | srManager.defaultRoutingHandler.populateRoute(location.deviceId(), prefix, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 117 | nextHopMac, nextHopVlan, location.port(), false); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 118 | }); |
| 119 | }); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 122 | void processRouteUpdated(RouteEvent event) { |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 123 | processRouteUpdatedInternal(Sets.newHashSet(event.alternatives()), |
| 124 | Sets.newHashSet(event.prevAlternatives())); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void processAlternativeRoutesChanged(RouteEvent event) { |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 128 | processRouteUpdatedInternal(Sets.newHashSet(event.alternatives()), |
| 129 | Sets.newHashSet(event.prevAlternatives())); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private void processRouteUpdatedInternal(Set<ResolvedRoute> routes, Set<ResolvedRoute> oldRoutes) { |
| 133 | if (!isReady()) { |
| 134 | log.info("System is not ready. Skip updating route for {} -> {}", oldRoutes, routes); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | log.info("processRouteUpdatedInternal. routes={}, oldRoutes={}", routes, oldRoutes); |
| 139 | |
Charles Chan | 482b642 | 2018-04-09 11:52:08 -0400 | [diff] [blame] | 140 | if (routes.size() > 2) { |
| 141 | log.info("Route {} has more than two next hops. Do not process route change", routes); |
| 142 | return; |
| 143 | } |
| 144 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 145 | Set<ConnectPoint> allLocations = Sets.newHashSet(); |
| 146 | Set<IpPrefix> allPrefixes = Sets.newHashSet(); |
| 147 | routes.forEach(route -> { |
| 148 | allLocations.addAll(srManager.nextHopLocations(route)); |
| 149 | allPrefixes.add(route.prefix()); |
| 150 | }); |
Charles Chan | 482b642 | 2018-04-09 11:52:08 -0400 | [diff] [blame] | 151 | |
| 152 | // Just come back from an invalid next hop count |
| 153 | // Revoke subnet from all locations and reset oldRoutes such that system will be reprogrammed from scratch |
| 154 | if (oldRoutes.size() > 2) { |
| 155 | log.info("Revoke subnet {} and reset oldRoutes"); |
| 156 | srManager.defaultRoutingHandler.revokeSubnet(allPrefixes); |
| 157 | oldRoutes = Sets.newHashSet(); |
| 158 | } |
| 159 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 160 | log.debug("RouteUpdated. populateSubnet {}, {}", allLocations, allPrefixes); |
| 161 | srManager.defaultRoutingHandler.populateSubnet(allLocations, allPrefixes); |
| 162 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 163 | Set<ResolvedRoute> toBeRemoved = Sets.difference(oldRoutes, routes).immutableCopy(); |
| 164 | Set<ResolvedRoute> toBeAdded = Sets.difference(routes, oldRoutes).immutableCopy(); |
| 165 | |
| 166 | toBeRemoved.forEach(route -> { |
| 167 | srManager.nextHopLocations(route).forEach(oldLocation -> { |
Charles Chan | 06f626c | 2018-02-05 17:20:05 -0800 | [diff] [blame] | 168 | if (toBeAdded.stream().map(srManager::nextHopLocations) |
| 169 | .flatMap(Set::stream).map(ConnectPoint::deviceId) |
| 170 | .noneMatch(deviceId -> deviceId.equals(oldLocation.deviceId()))) { |
| 171 | IpPrefix prefix = route.prefix(); |
| 172 | log.debug("RouteUpdated. removeSubnet {}, {}", oldLocation, prefix); |
| 173 | srManager.deviceConfiguration.removeSubnet(oldLocation, prefix); |
| 174 | // We don't remove the flow on the old location in occasion of two next hops becoming one |
| 175 | // since the populateSubnet will point the old location to the new location via spine. |
| 176 | } |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 177 | }); |
| 178 | }); |
| 179 | |
| 180 | toBeAdded.forEach(route -> { |
| 181 | IpPrefix prefix = route.prefix(); |
| 182 | MacAddress nextHopMac = route.nextHopMac(); |
| 183 | VlanId nextHopVlan = route.nextHopVlan(); |
| 184 | Set<ConnectPoint> locations = srManager.nextHopLocations(route); |
| 185 | |
| 186 | locations.forEach(location -> { |
| 187 | log.debug("RouteUpdated. addSubnet {}, {}", location, prefix); |
| 188 | srManager.deviceConfiguration.addSubnet(location, prefix); |
| 189 | log.debug("RouteUpdated. populateRoute {}, {}, {}, {}", location, prefix, nextHopMac, nextHopVlan); |
| 190 | srManager.defaultRoutingHandler.populateRoute(location.deviceId(), prefix, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 191 | nextHopMac, nextHopVlan, location.port(), false); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 192 | }); |
| 193 | }); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 196 | void processRouteRemoved(RouteEvent event) { |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 197 | processRouteRemovedInternal(event.alternatives()); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 200 | private void processRouteRemovedInternal(Collection<ResolvedRoute> routes) { |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 201 | if (!isReady()) { |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 202 | log.info("System is not ready. Skip removing route for {}", routes); |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 206 | log.info("processRouteRemovedInternal. routes={}", routes); |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame] | 207 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 208 | Set<IpPrefix> allPrefixes = Sets.newHashSet(); |
| 209 | routes.forEach(route -> { |
| 210 | allPrefixes.add(route.prefix()); |
| 211 | }); |
| 212 | log.debug("RouteRemoved. revokeSubnet {}", allPrefixes); |
| 213 | srManager.defaultRoutingHandler.revokeSubnet(allPrefixes); |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 214 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 215 | routes.forEach(route -> { |
| 216 | IpPrefix prefix = route.prefix(); |
| 217 | MacAddress nextHopMac = route.nextHopMac(); |
| 218 | VlanId nextHopVlan = route.nextHopVlan(); |
| 219 | Set<ConnectPoint> locations = srManager.nextHopLocations(route); |
| 220 | |
| 221 | locations.forEach(location -> { |
| 222 | log.debug("RouteRemoved. removeSubnet {}, {}", location, prefix); |
| 223 | srManager.deviceConfiguration.removeSubnet(location, prefix); |
Charles Chan | c4d6888 | 2018-03-15 16:41:10 -0700 | [diff] [blame] | 224 | // We don't need to call revokeRoute again since revokeSubnet will remove the prefix |
| 225 | // from all devices, including the ones that next hop attaches to. |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 226 | |
| 227 | // Also remove redirection flows on the pair device if exists. |
| 228 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId()); |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 229 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId()); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 230 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) { |
| 231 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 232 | // when the host is untagged |
| 233 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(nextHopVlan); |
| 234 | |
| 235 | log.debug("RouteRemoved. revokeRoute {}, {}, {}, {}", location, prefix, nextHopMac, nextHopVlan); |
| 236 | srManager.defaultRoutingHandler.revokeRoute(pairDeviceId.get(), prefix, |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 237 | nextHopMac, vlanId, pairLocalPort.get(), false); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 238 | } |
| 239 | }); |
| 240 | }); |
| 241 | } |
| 242 | |
| 243 | void processHostMovedEvent(HostEvent event) { |
| 244 | log.info("processHostMovedEvent {}", event); |
| 245 | MacAddress hostMac = event.subject().mac(); |
| 246 | VlanId hostVlanId = event.subject().vlan(); |
Ruchi Sahota | 0786932 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 247 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 248 | Set<HostLocation> prevLocations = event.prevSubject().locations(); |
| 249 | Set<HostLocation> newLocations = event.subject().locations(); |
Ruchi Sahota | 0786932 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 250 | Set<ConnectPoint> connectPoints = newLocations.stream() |
| 251 | .map(l -> (ConnectPoint) l).collect(Collectors.toSet()); |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 252 | List<Set<IpPrefix>> batchedSubnets = |
| 253 | srManager.deviceConfiguration.getBatchedSubnets(event.subject().id()); |
Ruchi Sahota | 0786932 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 254 | Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId) |
| 255 | .collect(Collectors.toSet()); |
| 256 | |
| 257 | // Set of deviceIDs of the previous locations where the host was connected |
| 258 | // Used to determine if host moved to different connect points |
| 259 | // on same device or moved to a different device altogether |
| 260 | Set<DeviceId> oldDeviceIds = prevLocations.stream().map(HostLocation::deviceId) |
| 261 | .collect(Collectors.toSet()); |
| 262 | |
| 263 | // L3 Ucast bucket needs to be updated only once per host |
| 264 | // and only when the no. of routes with the host as next-hop is not zero |
| 265 | if (!batchedSubnets.isEmpty()) { |
| 266 | // For each new location, if NextObj exists for the host, update with new location .. |
| 267 | Sets.difference(newLocations, prevLocations).forEach(newLocation -> { |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 268 | // NOTE: that we use the nexthop vlanId to retrieve the nextId |
| 269 | // while the vlanId used to program the L3 unicast chain |
| 270 | // is derived from the port configuration. In case of |
| 271 | // a tagged interface we use host vlanId. Host vlan should |
| 272 | // be part of the tags configured for that port. See the |
| 273 | // code in DefaultGroupHandler.updateL3UcastGroupBucket |
| 274 | int nextId = srManager.getMacVlanNextObjectiveId(newLocation.deviceId(), |
| 275 | hostMac, hostVlanId, null, false); |
| 276 | if (nextId != -1) { |
| 277 | //Update the nextId group bucket |
| 278 | log.debug("HostMoved. NextId exists, update L3 Ucast Group Bucket {}, {}, {} --> {}", |
| 279 | newLocation, hostMac, hostVlanId, nextId); |
| 280 | srManager.updateMacVlanTreatment(newLocation.deviceId(), hostMac, hostVlanId, |
| 281 | newLocation.port(), nextId); |
| 282 | } else { |
| 283 | log.debug("HostMoved. NextId does not exist for this location {}, host {}/{}", |
| 284 | newLocation, hostMac, hostVlanId); |
| 285 | } |
Ruchi Sahota | 0786932 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 286 | }); |
| 287 | } |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 288 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 289 | batchedSubnets.forEach(subnets -> { |
| 290 | log.debug("HostMoved. populateSubnet {}, {}", newLocations, subnets); |
| 291 | srManager.defaultRoutingHandler.populateSubnet(connectPoints, subnets); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 292 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 293 | subnets.forEach(prefix -> { |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 294 | // For each old location |
| 295 | Sets.difference(prevLocations, newLocations).forEach(prevLocation -> { |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 296 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 297 | // Remove flows for unchanged IPs only when the host moves from a switch to another. |
| 298 | // Otherwise, do not remove and let the adding part update the old flow |
| 299 | if (newDeviceIds.contains(prevLocation.deviceId())) { |
| 300 | return; |
| 301 | } |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 302 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 303 | log.debug("HostMoved. removeSubnet {}, {}", prevLocation, prefix); |
| 304 | srManager.deviceConfiguration.removeSubnet(prevLocation, prefix); |
Charles Chan | 4dcd510 | 2019-02-28 15:40:57 -0800 | [diff] [blame] | 305 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 306 | // Do not remove flow from a device if the route is still reachable via its pair device. |
| 307 | // populateSubnet will update the flow to point to its pair device via spine. |
| 308 | DeviceId pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId()).orElse(null); |
| 309 | if (newLocations.stream().anyMatch(n -> n.deviceId().equals(pairDeviceId))) { |
| 310 | return; |
| 311 | } |
Charles Chan | 4dcd510 | 2019-02-28 15:40:57 -0800 | [diff] [blame] | 312 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 313 | log.debug("HostMoved. revokeRoute {}, {}, {}, {}", prevLocation, prefix, hostMac, hostVlanId); |
| 314 | srManager.defaultRoutingHandler.revokeRoute(prevLocation.deviceId(), prefix, |
| 315 | hostMac, hostVlanId, prevLocation.port(), false); |
| 316 | }); |
Charles Chan | 4dcd510 | 2019-02-28 15:40:57 -0800 | [diff] [blame] | 317 | |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 318 | // For each new location, add all new IPs. |
| 319 | Sets.difference(newLocations, prevLocations).forEach(newLocation -> { |
| 320 | log.debug("HostMoved. addSubnet {}, {}", newLocation, prefix); |
| 321 | srManager.deviceConfiguration.addSubnet(newLocation, prefix); |
| 322 | |
| 323 | //its a new connect point, not a move from an existing device, populateRoute |
| 324 | if (!oldDeviceIds.contains(newLocation.deviceId())) { |
| 325 | log.debug("HostMoved. populateRoute {}, {}, {}, {}", newLocation, prefix, hostMac, hostVlanId); |
| 326 | srManager.defaultRoutingHandler.populateRoute(newLocation.deviceId(), prefix, |
| 327 | hostMac, hostVlanId, newLocation.port(), false); |
Charles Chan | 5eec3b1 | 2019-04-18 14:30:41 -0700 | [diff] [blame] | 328 | } |
| 329 | }); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 330 | }); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 331 | }); |
Ruchi Sahota | 0786932 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 332 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 333 | } |
| 334 | |
pierventre | a3989be | 2021-01-08 16:43:17 +0100 | [diff] [blame] | 335 | public void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum) { |
| 336 | log.info("processIntfVlanUpdatedEvent {}/{}", deviceId, portNum); |
| 337 | |
| 338 | // Verify there are hosts attached to the port |
| 339 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum); |
| 340 | Set<Host> hosts = srManager.hostService.getConnectedHosts(connectPoint); |
| 341 | if (hosts == null || hosts.size() == 0) { |
| 342 | log.debug("processIntfVlanUpdatedEvent: No hosts connected to {}", connectPoint); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | // Iterate over the hosts and update if needed the vlan of the nextobjective |
| 347 | hosts.forEach(host -> { |
| 348 | // Verify if there is a nexthop and only then update the l3 indirect unicast chain |
| 349 | int nextId = srManager.getMacVlanNextObjectiveId(deviceId, host.mac(), host.vlan(), |
| 350 | null, false); |
| 351 | if (nextId != -1) { |
| 352 | //Update the nextId group bucket |
| 353 | log.debug("intfVlanUpdated. NextId exists, update L3 Ucast Group Bucket {}, {}, {} --> {}", |
| 354 | connectPoint, host.mac(), host.vlan(), nextId); |
| 355 | // NOTE: that we use the nexthop vlanId to retrieve the nextId |
| 356 | // while the vlanId used to program the L3 unicast chain |
| 357 | // is derived from the port configuration. In case of |
| 358 | // a tagged interface we use host vlanId. Host vlan should |
| 359 | // be part of the tags configured for that port. See the |
| 360 | // code in DefaultGroupHandler.updateL3UcastGroupBucket |
| 361 | srManager.updateMacVlanTreatment(deviceId, host.mac(), host.vlan(), portNum, nextId); |
| 362 | } else { |
| 363 | log.debug("intfVlanUpdated. NextId does not exist for this location {}, host {}/{}", |
| 364 | connectPoint, host.mac(), host.vlan()); |
| 365 | } |
| 366 | }); |
| 367 | } |
| 368 | |
Charles Chan | ee8dbf8 | 2017-06-22 14:15:05 -0700 | [diff] [blame] | 369 | private boolean isReady() { |
| 370 | return Objects.nonNull(srManager.deviceConfiguration) && |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 371 | Objects.nonNull(srManager.defaultRoutingHandler); |
| 372 | } |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 373 | } |