blob: b99f33f2d9b64020dd0ffe78e093b92d87271678 [file] [log] [blame]
Charles Chandebfea32016-10-24 14:52:01 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chandebfea32016-10-24 14:52:01 -07003 *
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
17package org.onosproject.segmentrouting;
18
Saurav Das261c3002017-06-13 15:35:54 -070019import com.google.common.collect.Sets;
20
Charles Chandebfea32016-10-24 14:52:01 -070021import org.onlab.packet.IpPrefix;
22import org.onlab.packet.MacAddress;
Charles Chan90772a72017-02-08 15:52:08 -080023import org.onlab.packet.VlanId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070024import org.onosproject.net.ConnectPoint;
Charles Chan910be6a2017-08-23 14:46:43 -070025import org.onosproject.net.HostLocation;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.host.HostEvent;
Ray Milkeya8154312017-08-08 13:00:43 -070028import org.onosproject.routeservice.ResolvedRoute;
29import org.onosproject.routeservice.RouteEvent;
Charles Chandebfea32016-10-24 14:52:01 -070030import org.onosproject.net.DeviceId;
Charles Chan910be6a2017-08-23 14:46:43 -070031import org.onosproject.routeservice.RouteInfo;
Charles Chandebfea32016-10-24 14:52:01 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000035import java.util.HashMap;
Charles Chan5eec3b12019-04-18 14:30:41 -070036import java.util.List;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000037import java.util.Map;
Charles Chan910be6a2017-08-23 14:46:43 -070038import java.util.Collection;
Charles Chanee8dbf82017-06-22 14:15:05 -070039import java.util.Objects;
Charles Chan910be6a2017-08-23 14:46:43 -070040import java.util.Optional;
41import java.util.Set;
Charles Chan910be6a2017-08-23 14:46:43 -070042import java.util.stream.Collectors;
Charles Chanee8dbf82017-06-22 14:15:05 -070043
Charles Chandebfea32016-10-24 14:52:01 -070044/**
45 * Handles RouteEvent and manages routing entries.
46 */
47public class RouteHandler {
48 private static final Logger log = LoggerFactory.getLogger(RouteHandler.class);
49 private final SegmentRoutingManager srManager;
50
Charles Chanf0ae41e2017-08-23 13:55:39 -070051 RouteHandler(SegmentRoutingManager srManager) {
Charles Chandebfea32016-10-24 14:52:01 -070052 this.srManager = srManager;
53 }
54
55 protected void init(DeviceId deviceId) {
Charles Chan7d20a4e2018-04-13 14:01:49 -040056 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(deviceId);
57
58 srManager.routeService.getRouteTables().stream()
59 .map(srManager.routeService::getRoutes)
60 .flatMap(Collection::stream)
61 .map(RouteInfo::allRoutes)
62 .filter(allRoutes -> allRoutes.stream().allMatch(resolvedRoute ->
63 srManager.nextHopLocations(resolvedRoute).stream().allMatch(cp ->
64 deviceId.equals(cp.deviceId()) ||
65 (pairDeviceId.isPresent() && pairDeviceId.get().equals(cp.deviceId()))
66 )))
67 .forEach(this::processRouteAddedInternal);
Charles Chandebfea32016-10-24 14:52:01 -070068 }
69
Charles Chanf0ae41e2017-08-23 13:55:39 -070070 void processRouteAdded(RouteEvent event) {
Charles Chanfbcb8812018-04-18 18:41:05 -070071 processRouteAddedInternal(event.alternatives());
Charles Chandebfea32016-10-24 14:52:01 -070072 }
73
Charles Chan910be6a2017-08-23 14:46:43 -070074 private void processRouteAddedInternal(Collection<ResolvedRoute> routes) {
Charles Chanee8dbf82017-06-22 14:15:05 -070075 if (!isReady()) {
Charles Chan910be6a2017-08-23 14:46:43 -070076 log.info("System is not ready. Skip adding route for {}", routes);
Charles Chanee8dbf82017-06-22 14:15:05 -070077 return;
78 }
79
Charles Chan910be6a2017-08-23 14:46:43 -070080 log.info("processRouteAddedInternal. routes={}", routes);
Charles Chanf0ae41e2017-08-23 13:55:39 -070081
Charles Chan482b6422018-04-09 11:52:08 -040082 if (routes.size() > 2) {
83 log.info("Route {} has more than two next hops. Do not process route change", routes);
84 return;
85 }
86
Charles Chan910be6a2017-08-23 14:46:43 -070087 Set<ConnectPoint> allLocations = Sets.newHashSet();
88 Set<IpPrefix> allPrefixes = Sets.newHashSet();
89 routes.forEach(route -> {
90 allLocations.addAll(srManager.nextHopLocations(route));
91 allPrefixes.add(route.prefix());
92 });
93 log.debug("RouteAdded. populateSubnet {}, {}", allLocations, allPrefixes);
94 srManager.defaultRoutingHandler.populateSubnet(allLocations, allPrefixes);
Charles Chandebfea32016-10-24 14:52:01 -070095
Charles Chan910be6a2017-08-23 14:46:43 -070096 routes.forEach(route -> {
97 IpPrefix prefix = route.prefix();
98 MacAddress nextHopMac = route.nextHopMac();
99 VlanId nextHopVlan = route.nextHopVlan();
100 Set<ConnectPoint> locations = srManager.nextHopLocations(route);
101
102 locations.forEach(location -> {
103 log.debug("RouteAdded. addSubnet {}, {}", location, prefix);
104 srManager.deviceConfiguration.addSubnet(location, prefix);
105 log.debug("RouteAdded populateRoute {}, {}, {}, {}", location, prefix, nextHopMac, nextHopVlan);
106 srManager.defaultRoutingHandler.populateRoute(location.deviceId(), prefix,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000107 nextHopMac, nextHopVlan, location.port(), false);
Charles Chan910be6a2017-08-23 14:46:43 -0700108 });
109 });
Charles Chandebfea32016-10-24 14:52:01 -0700110 }
111
Charles Chanf0ae41e2017-08-23 13:55:39 -0700112 void processRouteUpdated(RouteEvent event) {
Charles Chanfbcb8812018-04-18 18:41:05 -0700113 processRouteUpdatedInternal(Sets.newHashSet(event.alternatives()),
114 Sets.newHashSet(event.prevAlternatives()));
Charles Chan910be6a2017-08-23 14:46:43 -0700115 }
116
117 void processAlternativeRoutesChanged(RouteEvent event) {
Charles Chanfbcb8812018-04-18 18:41:05 -0700118 processRouteUpdatedInternal(Sets.newHashSet(event.alternatives()),
119 Sets.newHashSet(event.prevAlternatives()));
Charles Chan910be6a2017-08-23 14:46:43 -0700120 }
121
122 private void processRouteUpdatedInternal(Set<ResolvedRoute> routes, Set<ResolvedRoute> oldRoutes) {
123 if (!isReady()) {
124 log.info("System is not ready. Skip updating route for {} -> {}", oldRoutes, routes);
125 return;
126 }
127
128 log.info("processRouteUpdatedInternal. routes={}, oldRoutes={}", routes, oldRoutes);
129
Charles Chan482b6422018-04-09 11:52:08 -0400130 if (routes.size() > 2) {
131 log.info("Route {} has more than two next hops. Do not process route change", routes);
132 return;
133 }
134
Charles Chan910be6a2017-08-23 14:46:43 -0700135 Set<ConnectPoint> allLocations = Sets.newHashSet();
136 Set<IpPrefix> allPrefixes = Sets.newHashSet();
137 routes.forEach(route -> {
138 allLocations.addAll(srManager.nextHopLocations(route));
139 allPrefixes.add(route.prefix());
140 });
Charles Chan482b6422018-04-09 11:52:08 -0400141
142 // Just come back from an invalid next hop count
143 // Revoke subnet from all locations and reset oldRoutes such that system will be reprogrammed from scratch
144 if (oldRoutes.size() > 2) {
145 log.info("Revoke subnet {} and reset oldRoutes");
146 srManager.defaultRoutingHandler.revokeSubnet(allPrefixes);
147 oldRoutes = Sets.newHashSet();
148 }
149
Charles Chan910be6a2017-08-23 14:46:43 -0700150 log.debug("RouteUpdated. populateSubnet {}, {}", allLocations, allPrefixes);
151 srManager.defaultRoutingHandler.populateSubnet(allLocations, allPrefixes);
152
Charles Chan910be6a2017-08-23 14:46:43 -0700153 Set<ResolvedRoute> toBeRemoved = Sets.difference(oldRoutes, routes).immutableCopy();
154 Set<ResolvedRoute> toBeAdded = Sets.difference(routes, oldRoutes).immutableCopy();
155
156 toBeRemoved.forEach(route -> {
157 srManager.nextHopLocations(route).forEach(oldLocation -> {
Charles Chan06f626c2018-02-05 17:20:05 -0800158 if (toBeAdded.stream().map(srManager::nextHopLocations)
159 .flatMap(Set::stream).map(ConnectPoint::deviceId)
160 .noneMatch(deviceId -> deviceId.equals(oldLocation.deviceId()))) {
161 IpPrefix prefix = route.prefix();
162 log.debug("RouteUpdated. removeSubnet {}, {}", oldLocation, prefix);
163 srManager.deviceConfiguration.removeSubnet(oldLocation, prefix);
164 // We don't remove the flow on the old location in occasion of two next hops becoming one
165 // since the populateSubnet will point the old location to the new location via spine.
166 }
Charles Chan910be6a2017-08-23 14:46:43 -0700167 });
168 });
169
170 toBeAdded.forEach(route -> {
171 IpPrefix prefix = route.prefix();
172 MacAddress nextHopMac = route.nextHopMac();
173 VlanId nextHopVlan = route.nextHopVlan();
174 Set<ConnectPoint> locations = srManager.nextHopLocations(route);
175
176 locations.forEach(location -> {
177 log.debug("RouteUpdated. addSubnet {}, {}", location, prefix);
178 srManager.deviceConfiguration.addSubnet(location, prefix);
179 log.debug("RouteUpdated. populateRoute {}, {}, {}, {}", location, prefix, nextHopMac, nextHopVlan);
180 srManager.defaultRoutingHandler.populateRoute(location.deviceId(), prefix,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000181 nextHopMac, nextHopVlan, location.port(), false);
Charles Chan910be6a2017-08-23 14:46:43 -0700182 });
183 });
Charles Chandebfea32016-10-24 14:52:01 -0700184 }
185
Charles Chanf0ae41e2017-08-23 13:55:39 -0700186 void processRouteRemoved(RouteEvent event) {
Charles Chanfbcb8812018-04-18 18:41:05 -0700187 processRouteRemovedInternal(event.alternatives());
Charles Chandebfea32016-10-24 14:52:01 -0700188 }
189
Charles Chan910be6a2017-08-23 14:46:43 -0700190 private void processRouteRemovedInternal(Collection<ResolvedRoute> routes) {
Charles Chanee8dbf82017-06-22 14:15:05 -0700191 if (!isReady()) {
Charles Chan910be6a2017-08-23 14:46:43 -0700192 log.info("System is not ready. Skip removing route for {}", routes);
Charles Chanee8dbf82017-06-22 14:15:05 -0700193 return;
194 }
195
Charles Chan910be6a2017-08-23 14:46:43 -0700196 log.info("processRouteRemovedInternal. routes={}", routes);
Charles Chanf0ae41e2017-08-23 13:55:39 -0700197
Charles Chan910be6a2017-08-23 14:46:43 -0700198 Set<IpPrefix> allPrefixes = Sets.newHashSet();
199 routes.forEach(route -> {
200 allPrefixes.add(route.prefix());
201 });
202 log.debug("RouteRemoved. revokeSubnet {}", allPrefixes);
203 srManager.defaultRoutingHandler.revokeSubnet(allPrefixes);
Charles Chandebfea32016-10-24 14:52:01 -0700204
Charles Chan910be6a2017-08-23 14:46:43 -0700205 routes.forEach(route -> {
206 IpPrefix prefix = route.prefix();
207 MacAddress nextHopMac = route.nextHopMac();
208 VlanId nextHopVlan = route.nextHopVlan();
209 Set<ConnectPoint> locations = srManager.nextHopLocations(route);
210
211 locations.forEach(location -> {
212 log.debug("RouteRemoved. removeSubnet {}, {}", location, prefix);
213 srManager.deviceConfiguration.removeSubnet(location, prefix);
Charles Chanc4d68882018-03-15 16:41:10 -0700214 // We don't need to call revokeRoute again since revokeSubnet will remove the prefix
215 // from all devices, including the ones that next hop attaches to.
Charles Chan910be6a2017-08-23 14:46:43 -0700216
217 // Also remove redirection flows on the pair device if exists.
218 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId());
Saurav Dasec683dc2018-04-27 18:42:30 -0700219 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId());
Charles Chan910be6a2017-08-23 14:46:43 -0700220 if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) {
221 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
222 // when the host is untagged
223 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(nextHopVlan);
224
225 log.debug("RouteRemoved. revokeRoute {}, {}, {}, {}", location, prefix, nextHopMac, nextHopVlan);
226 srManager.defaultRoutingHandler.revokeRoute(pairDeviceId.get(), prefix,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000227 nextHopMac, vlanId, pairLocalPort.get(), false);
Charles Chan910be6a2017-08-23 14:46:43 -0700228 }
229 });
230 });
231 }
232
233 void processHostMovedEvent(HostEvent event) {
234 log.info("processHostMovedEvent {}", event);
235 MacAddress hostMac = event.subject().mac();
236 VlanId hostVlanId = event.subject().vlan();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000237 // map of nextId for prev port in each device
238 Map<DeviceId, Integer> nextIdMap = new HashMap<>();
Charles Chan5eec3b12019-04-18 14:30:41 -0700239 Set<HostLocation> prevLocations = event.prevSubject().locations();
240 Set<HostLocation> newLocations = event.subject().locations();
241 Set<ConnectPoint> connectPoints = newLocations.stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
242 List<Set<IpPrefix>> batchedSubnets =
243 srManager.deviceConfiguration.getBatchedSubnets(event.subject().id());
Charles Chan910be6a2017-08-23 14:46:43 -0700244
Charles Chan5eec3b12019-04-18 14:30:41 -0700245 batchedSubnets.forEach(subnets -> {
246 log.debug("HostMoved. populateSubnet {}, {}", newLocations, subnets);
247 srManager.defaultRoutingHandler.populateSubnet(connectPoints, subnets);
Charles Chan910be6a2017-08-23 14:46:43 -0700248
Charles Chan5eec3b12019-04-18 14:30:41 -0700249 subnets.forEach(prefix -> {
250 Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId)
251 .collect(Collectors.toSet());
Charles Chan07203792018-07-17 16:33:11 -0700252
Charles Chan5eec3b12019-04-18 14:30:41 -0700253 // Set of deviceIDs of the previous locations where the host was connected
254 // Used to determine if host moved to different connect points
255 // on same device or moved to a different device altogether
256 Set<DeviceId> oldDeviceIds = prevLocations.stream().map(HostLocation::deviceId)
257 .collect(Collectors.toSet());
Charles Chanfbcb8812018-04-18 18:41:05 -0700258
Charles Chan5eec3b12019-04-18 14:30:41 -0700259 // For each old location
260 Sets.difference(prevLocations, newLocations).forEach(prevLocation -> {
261 //find next Id for each old port, add to map
262 int nextId = srManager.getNextIdForHostPort(prevLocation.deviceId(), hostMac,
263 hostVlanId, prevLocation.port());
264 log.debug("HostMoved. NextId For Host {}, {}, {}, {} {}", prevLocation, prefix,
265 hostMac, hostVlanId, nextId);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000266
Charles Chan5eec3b12019-04-18 14:30:41 -0700267 nextIdMap.put(prevLocation.deviceId(), nextId);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000268
Charles Chan5eec3b12019-04-18 14:30:41 -0700269 // Remove flows for unchanged IPs only when the host moves from a switch to another.
270 // Otherwise, do not remove and let the adding part update the old flow
271 if (newDeviceIds.contains(prevLocation.deviceId())) {
272 return;
273 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000274
Charles Chan5eec3b12019-04-18 14:30:41 -0700275 log.debug("HostMoved. removeSubnet {}, {}", prevLocation, prefix);
276 srManager.deviceConfiguration.removeSubnet(prevLocation, prefix);
Charles Chan4dcd5102019-02-28 15:40:57 -0800277
Charles Chan5eec3b12019-04-18 14:30:41 -0700278 // Do not remove flow from a device if the route is still reachable via its pair device.
279 // populateSubnet will update the flow to point to its pair device via spine.
280 DeviceId pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId()).orElse(null);
281 if (newLocations.stream().anyMatch(n -> n.deviceId().equals(pairDeviceId))) {
282 return;
283 }
Charles Chan4dcd5102019-02-28 15:40:57 -0800284
Charles Chan5eec3b12019-04-18 14:30:41 -0700285 log.debug("HostMoved. revokeRoute {}, {}, {}, {}", prevLocation, prefix, hostMac, hostVlanId);
286 srManager.defaultRoutingHandler.revokeRoute(prevLocation.deviceId(), prefix,
287 hostMac, hostVlanId, prevLocation.port(), false);
288 });
Charles Chan4dcd5102019-02-28 15:40:57 -0800289
Charles Chan5eec3b12019-04-18 14:30:41 -0700290 // For each new location, add all new IPs.
291 Sets.difference(newLocations, prevLocations).forEach(newLocation -> {
292 log.debug("HostMoved. addSubnet {}, {}", newLocation, prefix);
293 srManager.deviceConfiguration.addSubnet(newLocation, prefix);
294
295 //its a new connect point, not a move from an existing device, populateRoute
296 if (!oldDeviceIds.contains(newLocation.deviceId())) {
297 log.debug("HostMoved. populateRoute {}, {}, {}, {}", newLocation, prefix, hostMac, hostVlanId);
298 srManager.defaultRoutingHandler.populateRoute(newLocation.deviceId(), prefix,
299 hostMac, hostVlanId, newLocation.port(), false);
300 } else {
301 // update same flow to point to new nextObj
302 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(newLocation)).orElse(hostVlanId);
303 //use nextIdMap to send new port details to update the nextId group bucket
304 log.debug("HostMoved. update L3 Ucast Group Bucket {}, {}, {} --> {}",
305 newLocation, hostMac, vlanId, nextIdMap.get(newLocation.deviceId()));
306 srManager.updateMacVlanTreatment(newLocation.deviceId(), hostMac, vlanId,
307 newLocation.port(), nextIdMap.get(newLocation.deviceId()));
308 }
309 });
Charles Chan910be6a2017-08-23 14:46:43 -0700310 });
Charles Chan910be6a2017-08-23 14:46:43 -0700311 });
312 }
313
Charles Chanee8dbf82017-06-22 14:15:05 -0700314 private boolean isReady() {
315 return Objects.nonNull(srManager.deviceConfiguration) &&
Charles Chan910be6a2017-08-23 14:46:43 -0700316 Objects.nonNull(srManager.defaultRoutingHandler);
317 }
Charles Chandebfea32016-10-24 14:52:01 -0700318}