blob: c0ccfb628b8fe7ebda610deaef43186401699bbf [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;
pierventrea3989be2021-01-08 16:43:17 +010025import org.onosproject.net.Host;
Charles Chan910be6a2017-08-23 14:46:43 -070026import org.onosproject.net.HostLocation;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.host.HostEvent;
Ray Milkeya8154312017-08-08 13:00:43 -070029import org.onosproject.routeservice.ResolvedRoute;
30import org.onosproject.routeservice.RouteEvent;
Charles Chandebfea32016-10-24 14:52:01 -070031import org.onosproject.net.DeviceId;
Charles Chan910be6a2017-08-23 14:46:43 -070032import org.onosproject.routeservice.RouteInfo;
Charles Chandebfea32016-10-24 14:52:01 -070033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
Charles Chan5eec3b12019-04-18 14:30:41 -070036import java.util.List;
Charles Chan910be6a2017-08-23 14:46:43 -070037import java.util.Collection;
Charles Chanee8dbf82017-06-22 14:15:05 -070038import java.util.Objects;
Charles Chan910be6a2017-08-23 14:46:43 -070039import java.util.Optional;
40import java.util.Set;
Charles Chan910be6a2017-08-23 14:46:43 -070041import java.util.stream.Collectors;
Charles Chanee8dbf82017-06-22 14:15:05 -070042
Charles Chandebfea32016-10-24 14:52:01 -070043/**
44 * Handles RouteEvent and manages routing entries.
45 */
46public class RouteHandler {
47 private static final Logger log = LoggerFactory.getLogger(RouteHandler.class);
48 private final SegmentRoutingManager srManager;
49
Charles Chanf0ae41e2017-08-23 13:55:39 -070050 RouteHandler(SegmentRoutingManager srManager) {
Charles Chandebfea32016-10-24 14:52:01 -070051 this.srManager = srManager;
52 }
53
54 protected void init(DeviceId deviceId) {
Charles Chan7d20a4e2018-04-13 14:01:49 -040055 srManager.routeService.getRouteTables().stream()
56 .map(srManager.routeService::getRoutes)
57 .flatMap(Collection::stream)
58 .map(RouteInfo::allRoutes)
Charles Chan12a8a842020-02-14 13:23:57 -080059 .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 Chandebfea32016-10-24 14:52:01 -070063 }
64
Charles Chanf0ae41e2017-08-23 13:55:39 -070065 void processRouteAdded(RouteEvent event) {
Charles Chan12a8a842020-02-14 13:23:57 -080066 processRouteAddedInternal(event.alternatives(), false);
Charles Chandebfea32016-10-24 14:52:01 -070067 }
68
Charles Chan12a8a842020-02-14 13:23:57 -080069 /**
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 Chanee8dbf82017-06-22 14:15:05 -070079 if (!isReady()) {
Charles Chan910be6a2017-08-23 14:46:43 -070080 log.info("System is not ready. Skip adding route for {}", routes);
Charles Chanee8dbf82017-06-22 14:15:05 -070081 return;
82 }
83
Charles Chan910be6a2017-08-23 14:46:43 -070084 log.info("processRouteAddedInternal. routes={}", routes);
Charles Chanf0ae41e2017-08-23 13:55:39 -070085
Charles Chan482b6422018-04-09 11:52:08 -040086 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 Chan12a8a842020-02-14 13:23:57 -080091 ResolvedRoute rr = routes.stream().findFirst().orElse(null);
92 if (rr == null) {
93 log.warn("No resolved route found. Abort processRouteAddedInternal");
94 }
95
Charles Chan910be6a2017-08-23 14:46:43 -070096 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 Chandebfea32016-10-24 14:52:01 -0700104
Charles Chan12a8a842020-02-14 13:23:57 -0800105
Charles Chan910be6a2017-08-23 14:46:43 -0700106 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 Sahota71bcb4e2019-01-28 01:08:18 +0000117 nextHopMac, nextHopVlan, location.port(), false);
Charles Chan910be6a2017-08-23 14:46:43 -0700118 });
119 });
Charles Chandebfea32016-10-24 14:52:01 -0700120 }
121
Charles Chanf0ae41e2017-08-23 13:55:39 -0700122 void processRouteUpdated(RouteEvent event) {
Charles Chanfbcb8812018-04-18 18:41:05 -0700123 processRouteUpdatedInternal(Sets.newHashSet(event.alternatives()),
124 Sets.newHashSet(event.prevAlternatives()));
Charles Chan910be6a2017-08-23 14:46:43 -0700125 }
126
127 void processAlternativeRoutesChanged(RouteEvent event) {
Charles Chanfbcb8812018-04-18 18:41:05 -0700128 processRouteUpdatedInternal(Sets.newHashSet(event.alternatives()),
129 Sets.newHashSet(event.prevAlternatives()));
Charles Chan910be6a2017-08-23 14:46:43 -0700130 }
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 Chan482b6422018-04-09 11:52:08 -0400140 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 Chan910be6a2017-08-23 14:46:43 -0700145 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 Chan482b6422018-04-09 11:52:08 -0400151
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 Chan910be6a2017-08-23 14:46:43 -0700160 log.debug("RouteUpdated. populateSubnet {}, {}", allLocations, allPrefixes);
161 srManager.defaultRoutingHandler.populateSubnet(allLocations, allPrefixes);
162
Charles Chan910be6a2017-08-23 14:46:43 -0700163 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 Chan06f626c2018-02-05 17:20:05 -0800168 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 Chan910be6a2017-08-23 14:46:43 -0700177 });
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 Sahota71bcb4e2019-01-28 01:08:18 +0000191 nextHopMac, nextHopVlan, location.port(), false);
Charles Chan910be6a2017-08-23 14:46:43 -0700192 });
193 });
Charles Chandebfea32016-10-24 14:52:01 -0700194 }
195
Charles Chanf0ae41e2017-08-23 13:55:39 -0700196 void processRouteRemoved(RouteEvent event) {
Charles Chanfbcb8812018-04-18 18:41:05 -0700197 processRouteRemovedInternal(event.alternatives());
Charles Chandebfea32016-10-24 14:52:01 -0700198 }
199
Charles Chan910be6a2017-08-23 14:46:43 -0700200 private void processRouteRemovedInternal(Collection<ResolvedRoute> routes) {
Charles Chanee8dbf82017-06-22 14:15:05 -0700201 if (!isReady()) {
Charles Chan910be6a2017-08-23 14:46:43 -0700202 log.info("System is not ready. Skip removing route for {}", routes);
Charles Chanee8dbf82017-06-22 14:15:05 -0700203 return;
204 }
205
Charles Chan910be6a2017-08-23 14:46:43 -0700206 log.info("processRouteRemovedInternal. routes={}", routes);
Charles Chanf0ae41e2017-08-23 13:55:39 -0700207
Charles Chan910be6a2017-08-23 14:46:43 -0700208 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 Chandebfea32016-10-24 14:52:01 -0700214
Charles Chan910be6a2017-08-23 14:46:43 -0700215 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 Chanc4d68882018-03-15 16:41:10 -0700224 // 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 Chan910be6a2017-08-23 14:46:43 -0700226
227 // Also remove redirection flows on the pair device if exists.
228 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId());
Saurav Dasec683dc2018-04-27 18:42:30 -0700229 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId());
Charles Chan910be6a2017-08-23 14:46:43 -0700230 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 Sahota71bcb4e2019-01-28 01:08:18 +0000237 nextHopMac, vlanId, pairLocalPort.get(), false);
Charles Chan910be6a2017-08-23 14:46:43 -0700238 }
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 Sahota07869322019-05-09 17:26:14 -0400247
Charles Chan5eec3b12019-04-18 14:30:41 -0700248 Set<HostLocation> prevLocations = event.prevSubject().locations();
249 Set<HostLocation> newLocations = event.subject().locations();
Ruchi Sahota07869322019-05-09 17:26:14 -0400250 Set<ConnectPoint> connectPoints = newLocations.stream()
251 .map(l -> (ConnectPoint) l).collect(Collectors.toSet());
Charles Chan5eec3b12019-04-18 14:30:41 -0700252 List<Set<IpPrefix>> batchedSubnets =
253 srManager.deviceConfiguration.getBatchedSubnets(event.subject().id());
Ruchi Sahota07869322019-05-09 17:26:14 -0400254 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 -> {
pierventrea3989be2021-01-08 16:43:17 +0100268 // 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 Sahota07869322019-05-09 17:26:14 -0400286 });
287 }
Charles Chan910be6a2017-08-23 14:46:43 -0700288
Charles Chan5eec3b12019-04-18 14:30:41 -0700289 batchedSubnets.forEach(subnets -> {
290 log.debug("HostMoved. populateSubnet {}, {}", newLocations, subnets);
291 srManager.defaultRoutingHandler.populateSubnet(connectPoints, subnets);
Charles Chan910be6a2017-08-23 14:46:43 -0700292
Charles Chan5eec3b12019-04-18 14:30:41 -0700293 subnets.forEach(prefix -> {
Charles Chan5eec3b12019-04-18 14:30:41 -0700294 // For each old location
295 Sets.difference(prevLocations, newLocations).forEach(prevLocation -> {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000296
Charles Chan5eec3b12019-04-18 14:30:41 -0700297 // 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 Sahota71bcb4e2019-01-28 01:08:18 +0000302
Charles Chan5eec3b12019-04-18 14:30:41 -0700303 log.debug("HostMoved. removeSubnet {}, {}", prevLocation, prefix);
304 srManager.deviceConfiguration.removeSubnet(prevLocation, prefix);
Charles Chan4dcd5102019-02-28 15:40:57 -0800305
Charles Chan5eec3b12019-04-18 14:30:41 -0700306 // 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 Chan4dcd5102019-02-28 15:40:57 -0800312
Charles Chan5eec3b12019-04-18 14:30:41 -0700313 log.debug("HostMoved. revokeRoute {}, {}, {}, {}", prevLocation, prefix, hostMac, hostVlanId);
314 srManager.defaultRoutingHandler.revokeRoute(prevLocation.deviceId(), prefix,
315 hostMac, hostVlanId, prevLocation.port(), false);
316 });
Charles Chan4dcd5102019-02-28 15:40:57 -0800317
Charles Chan5eec3b12019-04-18 14:30:41 -0700318 // 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 Chan5eec3b12019-04-18 14:30:41 -0700328 }
329 });
Charles Chan910be6a2017-08-23 14:46:43 -0700330 });
Charles Chan910be6a2017-08-23 14:46:43 -0700331 });
Ruchi Sahota07869322019-05-09 17:26:14 -0400332
Charles Chan910be6a2017-08-23 14:46:43 -0700333 }
334
pierventrea3989be2021-01-08 16:43:17 +0100335 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 Chanee8dbf82017-06-22 14:15:05 -0700369 private boolean isReady() {
370 return Objects.nonNull(srManager.deviceConfiguration) &&
Charles Chan910be6a2017-08-23 14:46:43 -0700371 Objects.nonNull(srManager.defaultRoutingHandler);
372 }
Charles Chandebfea32016-10-24 14:52:01 -0700373}