blob: eee990054bbbbca134f716660d87e38310bc0d4f [file] [log] [blame]
Charles Chan1eaf4802016-04-18 13:44:03 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Charles Chan1eaf4802016-04-18 13:44:03 -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
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070019import org.onlab.packet.EthType;
Charles Chan1eaf4802016-04-18 13:44:03 -070020import org.onlab.packet.IpAddress;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070021import org.onlab.packet.IpPrefix;
Charles Chan1eaf4802016-04-18 13:44:03 -070022import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Charles Chan10b0fb72017-02-02 16:20:42 -080024import org.onosproject.net.ConnectPoint;
Charles Chan1eaf4802016-04-18 13:44:03 -070025import org.onosproject.net.DeviceId;
Charles Chan370a65b2016-05-10 17:29:47 -070026import org.onosproject.net.Host;
Charles Chanc22cef32016-04-29 14:38:22 -070027import org.onosproject.net.HostLocation;
Charles Chan1eaf4802016-04-18 13:44:03 -070028import org.onosproject.net.PortNumber;
Charles Chan1eaf4802016-04-18 13:44:03 -070029import org.onosproject.net.host.HostEvent;
30import org.onosproject.net.host.HostService;
Charles Chanc6bcdf92018-06-01 16:33:48 -070031import org.onosproject.net.host.ProbeMode;
Charles Chan1eaf4802016-04-18 13:44:03 -070032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
Saurav Dasf9332192017-02-18 14:05:44 -080035import com.google.common.collect.Sets;
Charles Chan3ed34d82017-06-22 18:03:14 -070036
Saurav Dase6c448a2018-01-18 12:07:33 -080037import java.util.HashSet;
Charles Chan3ed34d82017-06-22 18:03:14 -070038import java.util.Optional;
Charles Chan1eaf4802016-04-18 13:44:03 -070039import java.util.Set;
Charles Chand9265a32017-06-16 15:19:24 -070040import java.util.stream.Collectors;
41
42import static com.google.common.base.Preconditions.checkArgument;
Charles Chan1eaf4802016-04-18 13:44:03 -070043
44/**
45 * Handles host-related events.
46 */
47public class HostHandler {
48 private static final Logger log = LoggerFactory.getLogger(HostHandler.class);
Charles Chan873661e2017-11-30 15:37:50 -080049
Charles Chan114aec72017-06-19 14:00:53 -070050 protected final SegmentRoutingManager srManager;
Charles Chan1eaf4802016-04-18 13:44:03 -070051 private HostService hostService;
Charles Chan1eaf4802016-04-18 13:44:03 -070052
53 /**
54 * Constructs the HostHandler.
55 *
56 * @param srManager Segment Routing manager
57 */
Charles Chand9265a32017-06-16 15:19:24 -070058 HostHandler(SegmentRoutingManager srManager) {
Charles Chan1eaf4802016-04-18 13:44:03 -070059 this.srManager = srManager;
Charles Chan1eaf4802016-04-18 13:44:03 -070060 hostService = srManager.hostService;
Charles Chan1eaf4802016-04-18 13:44:03 -070061 }
62
Charles Chandebfea32016-10-24 14:52:01 -070063 protected void init(DeviceId devId) {
Charles Chand9265a32017-06-16 15:19:24 -070064 hostService.getHosts().forEach(host ->
65 host.locations().stream()
Charles Chan873661e2017-11-30 15:37:50 -080066 .filter(location -> location.deviceId().equals(devId) ||
67 location.deviceId().equals(srManager.getPairDeviceId(devId).orElse(null)))
Charles Chand9265a32017-06-16 15:19:24 -070068 .forEach(location -> processHostAddedAtLocation(host, location))
69 );
Charles Chanc22cef32016-04-29 14:38:22 -070070 }
Charles Chan1eaf4802016-04-18 13:44:03 -070071
Charles Chand9265a32017-06-16 15:19:24 -070072 void processHostAddedEvent(HostEvent event) {
Charles Chan41f5ec02016-06-13 18:54:31 -070073 processHostAdded(event.subject());
Charles Chanc22cef32016-04-29 14:38:22 -070074 }
75
Charles Chand9265a32017-06-16 15:19:24 -070076 private void processHostAdded(Host host) {
77 host.locations().forEach(location -> processHostAddedAtLocation(host, location));
Saurav Dase6c448a2018-01-18 12:07:33 -080078 // ensure dual-homed host locations have viable uplinks
Saurav Dasec683dc2018-04-27 18:42:30 -070079 if (host.locations().size() > 1 || srManager.singleHomedDown) {
Saurav Dase6c448a2018-01-18 12:07:33 -080080 host.locations().forEach(loc -> {
81 if (srManager.mastershipService.isLocalMaster(loc.deviceId())) {
Saurav Dasec683dc2018-04-27 18:42:30 -070082 srManager.linkHandler.checkUplinksForHost(loc);
Saurav Dase6c448a2018-01-18 12:07:33 -080083 }
84 });
85 }
Charles Chan1eaf4802016-04-18 13:44:03 -070086 }
87
Charles Chand9265a32017-06-16 15:19:24 -070088 void processHostAddedAtLocation(Host host, HostLocation location) {
89 checkArgument(host.locations().contains(location), "{} is not a location of {}", location, host);
90
Charles Chanceb2a2e2017-09-12 18:57:47 -070091 MacAddress hostMac = host.mac();
92 VlanId hostVlanId = host.vlan();
Charles Chand9265a32017-06-16 15:19:24 -070093 Set<HostLocation> locations = host.locations();
94 Set<IpAddress> ips = host.ipAddresses();
Charles Chanceb2a2e2017-09-12 18:57:47 -070095 log.info("Host {}/{} is added at {}", hostMac, hostVlanId, locations);
Charles Chand9265a32017-06-16 15:19:24 -070096
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070097 if (isDoubleTaggedHost(host)) {
98 ips.forEach(ip ->
99 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
100 host.innerVlan(), hostVlanId, host.tpid(), ip, false)
101 );
102 } else {
103 processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, false);
104 ips.forEach(ip ->
Charles Chand66d6712018-03-29 16:03:41 -0700105 processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700106 );
107 }
Charles Chanceb2a2e2017-09-12 18:57:47 -0700108
109 // Use the pair link temporarily before the second location of a dual-homed host shows up.
110 // This do not affect single-homed hosts since the flow will be blocked in
111 // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively
112 srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> {
Charles Chand66d6712018-03-29 16:03:41 -0700113 if (host.locations().stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700114 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> {
Charles Chanceb2a2e2017-09-12 18:57:47 -0700115 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
116 // when the host is untagged
Charles Chan868c9572018-06-15 18:54:18 -0700117 VlanId vlanId = vlanForPairPort(hostVlanId, location);
118 if (vlanId == null) {
119 return;
120 }
Charles Chanceb2a2e2017-09-12 18:57:47 -0700121
122 processBridgingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, false);
123 ips.forEach(ip -> processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId,
124 ip, false));
Charles Chan873661e2017-11-30 15:37:50 -0800125
126 if (srManager.activeProbing) {
127 probe(host, location, pairDeviceId, pairRemotePort);
128 }
Charles Chanceb2a2e2017-09-12 18:57:47 -0700129 });
130 }
131 });
Charles Chand9265a32017-06-16 15:19:24 -0700132 }
133
134 void processHostRemovedEvent(HostEvent event) {
Charles Chan41f5ec02016-06-13 18:54:31 -0700135 processHostRemoved(event.subject());
136 }
137
Charles Chand9265a32017-06-16 15:19:24 -0700138 private void processHostRemoved(Host host) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700139 MacAddress hostMac = host.mac();
140 VlanId hostVlanId = host.vlan();
Charles Chand9265a32017-06-16 15:19:24 -0700141 Set<HostLocation> locations = host.locations();
Charles Chan41f5ec02016-06-13 18:54:31 -0700142 Set<IpAddress> ips = host.ipAddresses();
Charles Chan3ed34d82017-06-22 18:03:14 -0700143 log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations);
Charles Chanc22cef32016-04-29 14:38:22 -0700144
Charles Chan910be6a2017-08-23 14:46:43 -0700145 locations.forEach(location -> {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700146 if (isDoubleTaggedHost(host)) {
147 ips.forEach(ip ->
148 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
149 host.innerVlan(), hostVlanId, host.tpid(), ip, true)
150 );
151 } else {
152 processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, true);
153 ips.forEach(ip ->
Charles Chand66d6712018-03-29 16:03:41 -0700154 processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700155 );
156 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700157
158 // Also remove redirection flows on the pair device if exists.
159 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId());
Saurav Dasec683dc2018-04-27 18:42:30 -0700160 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId());
Charles Chand66d6712018-03-29 16:03:41 -0700161 if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700162 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
163 // when the host is untagged
Charles Chan868c9572018-06-15 18:54:18 -0700164 VlanId vlanId = vlanForPairPort(hostVlanId, location);
165 if (vlanId == null) {
166 return;
167 }
Charles Chan3ed34d82017-06-22 18:03:14 -0700168
169 processBridgingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, true);
170 ips.forEach(ip ->
171 processRoutingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId,
172 ip, true));
173 }
Charles Chanbd84dd52018-06-21 19:07:12 -0700174
175 // Delete prefix from sr-device-subnet when the next hop host is removed
176 srManager.routeService.getRouteTables().forEach(tableId -> {
177 srManager.routeService.getRoutes(tableId).forEach(routeInfo -> {
178 if (routeInfo.allRoutes().stream().anyMatch(rr -> ips.contains(rr.nextHop()))) {
179 log.debug("HostRemoved. removeSubnet {}, {}", location, routeInfo.prefix());
180 srManager.deviceConfiguration.removeSubnet(location, routeInfo.prefix());
181 }
182 });
183 });
Charles Chand9265a32017-06-16 15:19:24 -0700184 });
Charles Chanc22cef32016-04-29 14:38:22 -0700185 }
186
Charles Chand9265a32017-06-16 15:19:24 -0700187 void processHostMovedEvent(HostEvent event) {
Charles Chan4b5769a2018-04-25 18:51:46 -0400188 Host host = event.subject();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700189 MacAddress hostMac = host.mac();
190 VlanId hostVlanId = host.vlan();
Charles Chan4b5769a2018-04-25 18:51:46 -0400191 Set<HostLocation> prevLocations = event.prevSubject().locations();
192 Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
193 Set<HostLocation> newLocations = host.locations();
194 Set<IpAddress> newIps = host.ipAddresses();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700195 EthType hostTpid = host.tpid();
196 boolean doubleTaggedHost = isDoubleTaggedHost(host);
Charles Chan4b5769a2018-04-25 18:51:46 -0400197
Charles Chan873661e2017-11-30 15:37:50 -0800198 log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations);
Charles Chand9265a32017-06-16 15:19:24 -0700199 Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId)
200 .collect(Collectors.toSet());
Charles Chanc22cef32016-04-29 14:38:22 -0700201
Charles Chand9265a32017-06-16 15:19:24 -0700202 // For each old location
Charles Chand66d6712018-03-29 16:03:41 -0700203 Sets.difference(prevLocations, newLocations).forEach(prevLocation -> {
Charles Chan3ed34d82017-06-22 18:03:14 -0700204 // Remove routing rules for old IPs
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700205 Sets.difference(prevIps, newIps).forEach(ip -> {
206 if (doubleTaggedHost) {
207 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
208 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
209 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700210 processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700211 ip, true);
212 }
213 });
Charles Chan3ed34d82017-06-22 18:03:14 -0700214
215 // Redirect the flows to pair link if configured
216 // Note: Do not continue removing any rule
217 Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId());
Saurav Dasec683dc2018-04-27 18:42:30 -0700218 Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(prevLocation.deviceId());
Charles Chan6d43c162018-10-11 12:35:36 -0700219 if (pairDeviceId.isPresent() && pairLocalPort.isPresent() &&
220 newLocations.stream().anyMatch(location -> location.deviceId().equals(pairDeviceId.get())) &&
221 newLocations.stream().noneMatch(location -> location.deviceId().equals(prevLocation.deviceId()))) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700222 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
223 // when the host is untagged
224 VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId);
225
226 processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false);
227 newIps.forEach(ip ->
228 processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId,
229 ip, false));
230 return;
231 }
Charles Chand9265a32017-06-16 15:19:24 -0700232
Charles Chanfbcb8812018-04-18 18:41:05 -0700233 // Remove flows for unchanged IPs only when the host moves from a switch to another.
Charles Chand9265a32017-06-16 15:19:24 -0700234 // Otherwise, do not remove and let the adding part update the old flow
235 if (!newDeviceIds.contains(prevLocation.deviceId())) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700236 processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700237 Sets.intersection(prevIps, newIps).forEach(ip -> {
238 if (doubleTaggedHost) {
239 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
240 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
241 } else {
242 processRoutingRule(prevLocation.deviceId(), prevLocation.port(),
243 hostMac, hostVlanId, ip, true);
244 }
245 });
Charles Chand9265a32017-06-16 15:19:24 -0700246 }
247
248 // Remove bridging rules if new interface vlan is different from old interface vlan
249 // Otherwise, do not remove and let the adding part update the old flow
250 if (newLocations.stream().noneMatch(newLocation -> {
251 VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation);
252 VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation);
253 // Host is tagged and the new location has the host vlan in vlan-tagged
Charles Chan098ca202018-05-01 11:50:20 -0700254 return srManager.interfaceService.getTaggedVlanId(newLocation).contains(hostVlanId) ||
Charles Chand9265a32017-06-16 15:19:24 -0700255 (oldAssignedVlan != null && newAssignedVlan != null &&
256 // Host is untagged and the new location has the same assigned vlan
257 oldAssignedVlan.equals(newAssignedVlan));
258 })) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700259 processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true);
Charles Chand9265a32017-06-16 15:19:24 -0700260 }
261
262 // Remove routing rules for unchanged IPs if none of the subnet of new location contains
263 // the IP. Otherwise, do not remove and let the adding part update the old flow
264 Sets.intersection(prevIps, newIps).forEach(ip -> {
265 if (newLocations.stream().noneMatch(newLocation ->
266 srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700267 if (doubleTaggedHost) {
268 processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(),
269 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
270 } else {
271 processRoutingRule(prevLocation.deviceId(), prevLocation.port(),
272 hostMac, hostVlanId, ip, true);
273 }
Charles Chand9265a32017-06-16 15:19:24 -0700274 }
Charles Chanc22cef32016-04-29 14:38:22 -0700275 });
Charles Chand9265a32017-06-16 15:19:24 -0700276 });
277
278 // For each new location, add all new IPs.
Charles Chanfbcb8812018-04-18 18:41:05 -0700279 Sets.difference(newLocations, prevLocations).forEach(newLocation -> {
Charles Chan3ed34d82017-06-22 18:03:14 -0700280 processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false);
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700281 newIps.forEach(ip -> {
282 if (doubleTaggedHost) {
283 processDoubleTaggedRoutingRule(newLocation.deviceId(), newLocation.port(),
284 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false);
285 } else {
Charles Chan3ed34d82017-06-22 18:03:14 -0700286 processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700287 ip, false);
288 }
289 });
Charles Chan4b5769a2018-04-25 18:51:46 -0400290
291 // Probe on pair device when host move
292 // Majorly for the 2nd step of [1A/x, 1B/x] -> [1A/x, 1B/y] -> [1A/y, 1B/y]
293 // But will also cover [1A/x] -> [1A/y] -> [1A/y, 1B/y]
294 if (srManager.activeProbing) {
Charles Chan6e90fbb2018-07-23 15:27:34 -0700295
Charles Chan4b5769a2018-04-25 18:51:46 -0400296 srManager.getPairDeviceId(newLocation.deviceId()).ifPresent(pairDeviceId ->
297 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort ->
298 probe(host, newLocation, pairDeviceId, pairRemotePort)
299 )
300 );
301 }
Charles Chand9265a32017-06-16 15:19:24 -0700302 });
303
304 // For each unchanged location, add new IPs and remove old IPs.
Charles Chanfbcb8812018-04-18 18:41:05 -0700305 Sets.intersection(newLocations, prevLocations).forEach(unchangedLocation -> {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700306 Sets.difference(prevIps, newIps).forEach(ip -> {
307 if (doubleTaggedHost) {
308 processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
309 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true);
310 } else {
311 processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
312 hostMac, hostVlanId, ip, true);
313 }
314 });
Charles Chand9265a32017-06-16 15:19:24 -0700315
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700316 Sets.difference(newIps, prevIps).forEach(ip -> {
317 if (doubleTaggedHost) {
318 processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
319 hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false);
320 } else {
321 processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(),
322 hostMac, hostVlanId, ip, false);
323 }
324 });
Charles Chan6e90fbb2018-07-23 15:27:34 -0700325
326 // Verify existing location and see if it is still valid
327 srManager.probingService.probeHost(host, unchangedLocation, ProbeMode.VERIFY);
Charles Chand9265a32017-06-16 15:19:24 -0700328 });
Saurav Dase6c448a2018-01-18 12:07:33 -0800329
330 // ensure dual-homed host locations have viable uplinks
Saurav Dasec683dc2018-04-27 18:42:30 -0700331 if (newLocations.size() > prevLocations.size() || srManager.singleHomedDown) {
Saurav Dase6c448a2018-01-18 12:07:33 -0800332 newLocations.forEach(loc -> {
333 if (srManager.mastershipService.isLocalMaster(loc.deviceId())) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700334 srManager.linkHandler.checkUplinksForHost(loc);
Saurav Dase6c448a2018-01-18 12:07:33 -0800335 }
336 });
337 }
Charles Chanc22cef32016-04-29 14:38:22 -0700338 }
339
Charles Chand9265a32017-06-16 15:19:24 -0700340 void processHostUpdatedEvent(HostEvent event) {
Charles Chan873661e2017-11-30 15:37:50 -0800341 Host host = event.subject();
342 MacAddress hostMac = host.mac();
343 VlanId hostVlanId = host.vlan();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700344 EthType hostTpid = host.tpid();
Charles Chan873661e2017-11-30 15:37:50 -0800345 Set<HostLocation> locations = host.locations();
Charles Chanc22cef32016-04-29 14:38:22 -0700346 Set<IpAddress> prevIps = event.prevSubject().ipAddresses();
Charles Chan873661e2017-11-30 15:37:50 -0800347 Set<IpAddress> newIps = host.ipAddresses();
Charles Chanb3c1faf2017-11-20 08:46:24 -0800348 log.info("Host {}/{} is updated", hostMac, hostVlanId);
Charles Chanc22cef32016-04-29 14:38:22 -0700349
Charles Chand66d6712018-03-29 16:03:41 -0700350 locations.forEach(location -> {
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700351 Sets.difference(prevIps, newIps).forEach(ip -> {
352 if (isDoubleTaggedHost(host)) {
353 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
354 host.innerVlan(), hostVlanId, hostTpid, ip, true);
355 } else {
356 processRoutingRule(location.deviceId(), location.port(), hostMac,
357 hostVlanId, ip, true);
358 }
359 });
360 Sets.difference(newIps, prevIps).forEach(ip -> {
361 if (isDoubleTaggedHost(host)) {
362 processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac,
363 host.innerVlan(), hostVlanId, hostTpid, ip, false);
364 } else {
365 processRoutingRule(location.deviceId(), location.port(), hostMac,
366 hostVlanId, ip, false);
367 }
368 });
Charles Chand9265a32017-06-16 15:19:24 -0700369 });
Charles Chanb3c1faf2017-11-20 08:46:24 -0800370
371 // Use the pair link temporarily before the second location of a dual-homed host shows up.
372 // This do not affect single-homed hosts since the flow will be blocked in
373 // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively
Charles Chan873661e2017-11-30 15:37:50 -0800374 locations.forEach(location ->
Charles Chanb3c1faf2017-11-20 08:46:24 -0800375 srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> {
Charles Chand66d6712018-03-29 16:03:41 -0700376 if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) {
Charles Chan873661e2017-11-30 15:37:50 -0800377 Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps);
378 Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps);
379
Saurav Dasec683dc2018-04-27 18:42:30 -0700380 srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> {
Charles Chanb3c1faf2017-11-20 08:46:24 -0800381 // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port
382 // when the host is untagged
Charles Chan868c9572018-06-15 18:54:18 -0700383 VlanId vlanId = vlanForPairPort(hostVlanId, location);
384 if (vlanId == null) {
385 return;
386 }
Charles Chanb3c1faf2017-11-20 08:46:24 -0800387
Charles Chan873661e2017-11-30 15:37:50 -0800388 ipsToRemove.forEach(ip ->
Charles Chanb3c1faf2017-11-20 08:46:24 -0800389 processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true)
390 );
Charles Chan873661e2017-11-30 15:37:50 -0800391 ipsToAdd.forEach(ip ->
Charles Chanb3c1faf2017-11-20 08:46:24 -0800392 processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false)
393 );
Charles Chan873661e2017-11-30 15:37:50 -0800394
395 if (srManager.activeProbing) {
396 probe(host, location, pairDeviceId, pairRemotePort);
397 }
Charles Chanb3c1faf2017-11-20 08:46:24 -0800398 });
399 }
Charles Chan873661e2017-11-30 15:37:50 -0800400 })
401 );
402 }
403
404 /**
405 * When a non-pair port comes up, probe each host on the pair device if
406 * (1) the host is tagged and the tagged vlan of current port contains host vlan; or
407 * (2) the host is untagged and the internal vlan is the same on the host port and current port.
408 *
409 * @param cp connect point
410 */
411 void processPortUp(ConnectPoint cp) {
Saurav Dasec683dc2018-04-27 18:42:30 -0700412 if (cp.port().equals(srManager.getPairLocalPort(cp.deviceId()).orElse(null))) {
Charles Chan873661e2017-11-30 15:37:50 -0800413 return;
414 }
415 if (srManager.activeProbing) {
416 srManager.getPairDeviceId(cp.deviceId())
417 .ifPresent(pairDeviceId -> srManager.hostService.getConnectedHosts(pairDeviceId).stream()
418 .filter(host -> isHostInVlanOfPort(host, pairDeviceId, cp))
Charles Chanc6bcdf92018-06-01 16:33:48 -0700419 .forEach(host -> srManager.probingService.probeHost(host, cp, ProbeMode.DISCOVER))
Charles Chan873661e2017-11-30 15:37:50 -0800420 );
421 }
422 }
423
424 /**
425 * Checks if given host located on given device id matches VLAN config of current port.
426 *
427 * @param host host to check
428 * @param deviceId device id to check
429 * @param cp current connect point
430 * @return true if the host located at deviceId matches the VLAN config on cp
431 */
432 private boolean isHostInVlanOfPort(Host host, DeviceId deviceId, ConnectPoint cp) {
433 VlanId internalVlan = srManager.getInternalVlanId(cp);
Charles Chan098ca202018-05-01 11:50:20 -0700434 Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(cp);
Charles Chan873661e2017-11-30 15:37:50 -0800435
436 return taggedVlan.contains(host.vlan()) ||
437 (internalVlan != null && host.locations().stream()
438 .filter(l -> l.deviceId().equals(deviceId))
439 .map(srManager::getInternalVlanId)
440 .anyMatch(internalVlan::equals));
441 }
442
443 /**
444 * Send a probe on all locations with the same VLAN on pair device, excluding pair port.
445 *
446 * @param host host to probe
447 * @param location newly discovered host location
448 * @param pairDeviceId pair device id
449 * @param pairRemotePort pair remote port
450 */
451 private void probe(Host host, ConnectPoint location, DeviceId pairDeviceId, PortNumber pairRemotePort) {
Mayank Tiwarif7942402018-10-29 18:27:35 -0400452 //Check if the host still exists in the host store
453 if (hostService.getHost(host.id()) == null) {
454 log.debug("Host entry for host {} no more present. Aborting hostprobe discover for this host", host.id());
455 return;
456 }
457
Charles Chan873661e2017-11-30 15:37:50 -0800458 VlanId vlanToProbe = host.vlan().equals(VlanId.NONE) ?
459 srManager.getInternalVlanId(location) : host.vlan();
Mayank Tiwarif7942402018-10-29 18:27:35 -0400460 if (srManager.symmetricProbing) {
461 srManager.interfaceService.getInterfaces().stream()
462 .filter(i -> i.vlanTagged().contains(vlanToProbe) ||
463 i.vlanUntagged().equals(vlanToProbe) ||
464 i.vlanNative().equals(vlanToProbe))
465 .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId))
466 .filter(i -> i.connectPoint().port().equals(location.port()))
467 .forEach(i -> {
468 log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint());
469 srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER);
470 });
471 } else {
472 srManager.interfaceService.getInterfaces().stream()
473 .filter(i -> i.vlanTagged().contains(vlanToProbe) ||
474 i.vlanUntagged().equals(vlanToProbe) ||
475 i.vlanNative().equals(vlanToProbe))
476 .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId))
477 .filter(i -> !i.connectPoint().port().equals(pairRemotePort))
478 .forEach(i -> {
479 log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint());
480 srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER);
481 });
482 }
Charles Chanc22cef32016-04-29 14:38:22 -0700483 }
484
485 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700486 * Populates or revokes a bridging rule on given deviceId that matches given mac,
487 * given vlan and output to given port.
Charles Chan9595e6a2017-06-15 19:25:25 -0700488 *
489 * @param deviceId device ID
490 * @param port port
491 * @param mac mac address
492 * @param vlanId VLAN ID
493 * @param revoke true to revoke the rule; false to populate
494 */
495 private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac,
496 VlanId vlanId, boolean revoke) {
Charles Chand66d6712018-03-29 16:03:41 -0700497 log.info("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating",
Charles Chan9595e6a2017-06-15 19:25:25 -0700498 mac, vlanId, deviceId, port);
499
Charles Chand66d6712018-03-29 16:03:41 -0700500 if (!revoke) {
501 srManager.defaultRoutingHandler.populateBridging(deviceId, port, mac, vlanId);
502 } else {
503 srManager.defaultRoutingHandler.revokeBridging(deviceId, port, mac, vlanId);
Charles Chan9595e6a2017-06-15 19:25:25 -0700504 }
Charles Chan9595e6a2017-06-15 19:25:25 -0700505 }
506
507 /**
508 * Populate or revoke a routing rule on given deviceId that matches given ip,
509 * set destination mac to given mac, set vlan to given vlan and output to given port.
510 *
511 * @param deviceId device ID
512 * @param port port
513 * @param mac mac address
514 * @param vlanId VLAN ID
515 * @param ip IP address
516 * @param revoke true to revoke the rule; false to populate
517 */
518 private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac,
519 VlanId vlanId, IpAddress ip, boolean revoke) {
520 ConnectPoint location = new ConnectPoint(deviceId, port);
Charles Chand9265a32017-06-16 15:19:24 -0700521 if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) {
522 log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port);
523 return;
Charles Chan9595e6a2017-06-15 19:25:25 -0700524 }
Charles Chan9595e6a2017-06-15 19:25:25 -0700525
Charles Chand9265a32017-06-16 15:19:24 -0700526 log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location);
527 if (revoke) {
Charles Chan910be6a2017-08-23 14:46:43 -0700528 srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port);
Charles Chand9265a32017-06-16 15:19:24 -0700529 } else {
Charles Chan910be6a2017-08-23 14:46:43 -0700530 srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port);
Charles Chan370a65b2016-05-10 17:29:47 -0700531 }
Charles Chan370a65b2016-05-10 17:29:47 -0700532 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700533
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700534 /**
535 * Populate or revoke a routing rule and egress rules on given deviceId that matches given IP,
536 * to set destination mac to given mac, set inner vlan and outer vlan to given vlans,
537 * set outer TPID, and output to given port.
538 *
539 * @param deviceId device ID
540 * @param port port
541 * @param mac mac address
542 * @param innerVlan inner VLAN ID
543 * @param outerVlan outer VLAN ID
544 * @param outerTpid outer TPID
545 * @param ip IP address
546 * @param revoke true to revoke the rule; false to populate
547 */
548 private void processDoubleTaggedRoutingRule(DeviceId deviceId, PortNumber port,
549 MacAddress mac, VlanId innerVlan,
550 VlanId outerVlan, EthType outerTpid,
551 IpAddress ip, boolean revoke) {
Charles Chanfbca55e2018-07-24 16:40:35 -0700552 if (!srManager.routeDoubleTaggedHosts) {
553 log.debug("Routing for double tagged host is disabled. Ignore {}/{}/{}", mac, outerVlan, innerVlan);
Charles Chan4eb73bb2018-07-19 14:55:31 -0700554 return;
555 }
556
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700557 ConnectPoint location = new ConnectPoint(deviceId, port);
558 if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) {
559 log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port);
560 return;
561 }
562 log.info("{} routing rule for double-tagged host {} at {}",
563 revoke ? "Revoking" : "Populating", ip, location);
564 if (revoke) {
565 srManager.defaultRoutingHandler.revokeDoubleTaggedRoute(
566 deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port);
567 } else {
568 srManager.defaultRoutingHandler.populateDoubleTaggedRoute(
569 deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port);
570 }
571 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700572
Charles Chan4eb73bb2018-07-19 14:55:31 -0700573 void populateAllDoubleTaggedHost() {
Charles Chanfbca55e2018-07-24 16:40:35 -0700574 log.info("Enabling routing for all double tagged hosts");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700575 Sets.newHashSet(srManager.hostService.getHosts()).stream().filter(this::isDoubleTaggedHost)
576 .forEach(h -> h.locations().forEach(l ->
577 h.ipAddresses().forEach(i ->
578 processDoubleTaggedRoutingRule(l.deviceId(), l.port(), h.mac(), h.innerVlan(),
579 h.vlan(), h.tpid(), i, false)
580 )
581 )
582 );
583 }
584
585 void revokeAllDoubleTaggedHost() {
Charles Chanfbca55e2018-07-24 16:40:35 -0700586 log.info("Disabling routing for all double tagged hosts");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700587 Sets.newHashSet(srManager.hostService.getHosts()).stream().filter(this::isDoubleTaggedHost)
588 .forEach(h -> h.locations().forEach(l ->
589 h.ipAddresses().forEach(i ->
590 processDoubleTaggedRoutingRule(l.deviceId(), l.port(), h.mac(), h.innerVlan(),
591 h.vlan(), h.tpid(), i, true)
592 )
593 )
594 );
595 }
596
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700597 /**
Charles Chan868c9572018-06-15 18:54:18 -0700598 * Returns VLAN ID to be used to program redirection flow on pair port.
599 *
600 * @param hostVlanId host VLAN ID
601 * @param location host location
602 * @return VLAN ID to be used; Or null if host VLAN does not match the interface config
603 */
604 VlanId vlanForPairPort(VlanId hostVlanId, ConnectPoint location) {
605 VlanId internalVlan = srManager.getInternalVlanId(location);
606 Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(location);
607
608 if (!hostVlanId.equals(VlanId.NONE) && taggedVlan.contains(hostVlanId)) {
609 return hostVlanId;
610 } else if (hostVlanId.equals(VlanId.NONE) && internalVlan != null) {
611 return internalVlan;
612 } else {
613 log.warn("VLAN mismatch. hostVlan={}, location={}, internalVlan={}, taggedVlan={}",
614 hostVlanId, location, internalVlan, taggedVlan);
615 return null;
616 }
617 }
618
619 /**
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700620 * Update forwarding objective for unicast bridging and unicast routing.
621 * Also check the validity of updated interface configuration on VLAN.
622 *
623 * @param deviceId device ID that host attaches to
624 * @param portNum port number that host attaches to
625 * @param vlanId Vlan ID configured on the switch port
626 * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise
627 * @param install true to populate the objective, false to revoke
628 */
629 void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId,
630 boolean popVlan, boolean install) {
631 ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum);
632 Set<Host> hosts = hostService.getConnectedHosts(connectPoint);
633
634 if (hosts == null || hosts.size() == 0) {
Charles Chan39b75522018-04-21 00:44:29 -0700635 log.debug("processIntfVlanUpdatedEvent: No hosts connected to {}", connectPoint);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700636 return;
637 }
638
639 hosts.forEach(host -> {
640 MacAddress mac = host.mac();
641 VlanId hostVlanId = host.vlan();
642
643 // Check whether the host vlan is valid for new interface configuration
644 if ((!popVlan && hostVlanId.equals(vlanId)) ||
645 (popVlan && hostVlanId.equals(VlanId.NONE))) {
Charles Chand66d6712018-03-29 16:03:41 -0700646 srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700647 // Update Forwarding objective and corresponding simple Next objective
648 // for each host and IP address connected to given port
649 host.ipAddresses().forEach(ipAddress ->
Charles Chand66d6712018-03-29 16:03:41 -0700650 srManager.defaultRoutingHandler.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(),
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700651 mac, vlanId, popVlan, install)
652 );
653 }
654 });
655 }
656
657 /**
658 * Populate or revoke routing rule for each host, according to the updated
659 * subnet configuration on the interface.
660 * @param cp connect point of the updated interface
661 * @param ipPrefixSet IP Prefixes added or removed
662 * @param install true if IP Prefixes added, false otherwise
663 */
664 void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) {
665 Set<Host> hosts = hostService.getConnectedHosts(cp);
666
667 if (hosts == null || hosts.size() == 0) {
Charles Chan39b75522018-04-21 00:44:29 -0700668 log.debug("processIntfIpUpdatedEvent: No hosts connected to {}", cp);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700669 return;
670 }
671
672 // Check whether the host IP address is in the interface's subnet
673 hosts.forEach(host ->
674 host.ipAddresses().forEach(hostIpAddress -> {
675 ipPrefixSet.forEach(ipPrefix -> {
676 if (install && ipPrefix.contains(hostIpAddress)) {
Charles Chand66d6712018-03-29 16:03:41 -0700677 srManager.defaultRoutingHandler.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(),
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700678 host.mac(), host.vlan(), cp.port());
679 } else if (!install && ipPrefix.contains(hostIpAddress)) {
Charles Chand66d6712018-03-29 16:03:41 -0700680 srManager.defaultRoutingHandler.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(),
Jonghwan Hyune5ef7622017-08-25 17:48:36 -0700681 host.mac(), host.vlan(), cp.port());
682 }
683 });
684 }));
685 }
Saurav Dase6c448a2018-01-18 12:07:33 -0800686
687 /**
688 * Returns the set of portnumbers on the given device that are part of the
689 * locations for dual-homed hosts.
690 *
691 * @param deviceId the given deviceId
692 * @return set of port numbers on given device that are dual-homed host
693 * locations. May be empty if no dual homed hosts are connected to
694 * the given device
695 */
696 Set<PortNumber> getDualHomedHostPorts(DeviceId deviceId) {
697 Set<PortNumber> dualHomedLocations = new HashSet<>();
698 srManager.hostService.getConnectedHosts(deviceId).stream()
699 .filter(host -> host.locations().size() == 2)
700 .forEach(host -> host.locations().stream()
701 .filter(loc -> loc.deviceId().equals(deviceId))
702 .forEach(loc -> dualHomedLocations.add(loc.port())));
703 return dualHomedLocations;
704 }
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700705
706 /**
707 * Checks if the given host is double-tagged or not.
708 *
709 * @param host host to check
710 * @return true if it is double-tagged, false otherwise
711 */
712 private boolean isDoubleTaggedHost(Host host) {
713 return !host.innerVlan().equals(VlanId.NONE);
714 }
715
Charles Chan1eaf4802016-04-18 13:44:03 -0700716}