Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | d299036 | 2016-04-18 13:44:03 -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 | |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpPrefix; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Host; |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 26 | import org.onosproject.net.HostLocation; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 28 | import org.onosproject.net.host.HostEvent; |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 29 | import org.onosproject.net.host.HostLocationProbingService.ProbeMode; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 30 | import org.onosproject.net.host.HostService; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 34 | import com.google.common.collect.Sets; |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 35 | |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 36 | import java.util.HashSet; |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 37 | import java.util.Optional; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 38 | import java.util.Set; |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 39 | import java.util.concurrent.Executors; |
| 40 | import java.util.concurrent.ScheduledExecutorService; |
| 41 | import java.util.concurrent.TimeUnit; |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 42 | import java.util.stream.Collectors; |
| 43 | |
| 44 | import static com.google.common.base.Preconditions.checkArgument; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Handles host-related events. |
| 48 | */ |
| 49 | public class HostHandler { |
| 50 | private static final Logger log = LoggerFactory.getLogger(HostHandler.class); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 51 | static final int HOST_MOVED_DELAY_MS = 1000; |
| 52 | |
Charles Chan | 2e2e340 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 53 | protected final SegmentRoutingManager srManager; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 54 | private HostService hostService; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Constructs the HostHandler. |
| 58 | * |
| 59 | * @param srManager Segment Routing manager |
| 60 | */ |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 61 | HostHandler(SegmentRoutingManager srManager) { |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 62 | this.srManager = srManager; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 63 | hostService = srManager.hostService; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Charles Chan | 03a73e0 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 66 | protected void init(DeviceId devId) { |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 67 | hostService.getHosts().forEach(host -> |
| 68 | host.locations().stream() |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 69 | .filter(location -> location.deviceId().equals(devId) || |
| 70 | location.deviceId().equals(srManager.getPairDeviceId(devId).orElse(null))) |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 71 | .forEach(location -> processHostAddedAtLocation(host, location)) |
| 72 | ); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 73 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 74 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 75 | void processHostAddedEvent(HostEvent event) { |
Charles Chan | 35fd1a7 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 76 | processHostAdded(event.subject()); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 79 | private void processHostAdded(Host host) { |
| 80 | host.locations().forEach(location -> processHostAddedAtLocation(host, location)); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 81 | // ensure dual-homed host locations have viable uplinks |
| 82 | if (host.locations().size() > 1) { |
| 83 | host.locations().forEach(loc -> { |
| 84 | if (srManager.mastershipService.isLocalMaster(loc.deviceId())) { |
| 85 | srManager.linkHandler.checkUplinksForDualHomedHosts(loc); |
| 86 | } |
| 87 | }); |
| 88 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 91 | void processHostAddedAtLocation(Host host, HostLocation location) { |
| 92 | checkArgument(host.locations().contains(location), "{} is not a location of {}", location, host); |
| 93 | |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 94 | MacAddress hostMac = host.mac(); |
| 95 | VlanId hostVlanId = host.vlan(); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 96 | Set<HostLocation> locations = host.locations(); |
| 97 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 98 | log.info("Host {}/{} is added at {}", hostMac, hostVlanId, locations); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 99 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 100 | processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, false); |
| 101 | ips.forEach(ip -> |
| 102 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false) |
| 103 | ); |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 104 | |
| 105 | // Use the pair link temporarily before the second location of a dual-homed host shows up. |
| 106 | // This do not affect single-homed hosts since the flow will be blocked in |
| 107 | // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively |
| 108 | srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 109 | if (host.locations().stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 110 | srManager.getPairLocalPorts(pairDeviceId).ifPresent(pairRemotePort -> { |
| 111 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 112 | // when the host is untagged |
| 113 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId); |
| 114 | |
| 115 | processBridgingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, false); |
| 116 | ips.forEach(ip -> processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, |
| 117 | ip, false)); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 118 | |
| 119 | if (srManager.activeProbing) { |
| 120 | probe(host, location, pairDeviceId, pairRemotePort); |
| 121 | } |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 122 | }); |
| 123 | } |
| 124 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void processHostRemovedEvent(HostEvent event) { |
Charles Chan | 35fd1a7 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 128 | processHostRemoved(event.subject()); |
| 129 | } |
| 130 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 131 | private void processHostRemoved(Host host) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 132 | MacAddress hostMac = host.mac(); |
| 133 | VlanId hostVlanId = host.vlan(); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 134 | Set<HostLocation> locations = host.locations(); |
Charles Chan | 35fd1a7 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 135 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 136 | log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 137 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 138 | locations.forEach(location -> { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 139 | processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, true); |
| 140 | ips.forEach(ip -> |
| 141 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true) |
| 142 | ); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 143 | |
| 144 | // Also remove redirection flows on the pair device if exists. |
| 145 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId()); |
| 146 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPorts(location.deviceId()); |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 147 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 148 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 149 | // when the host is untagged |
| 150 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId); |
| 151 | |
| 152 | processBridgingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, true); |
| 153 | ips.forEach(ip -> |
| 154 | processRoutingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, |
| 155 | ip, true)); |
| 156 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 157 | }); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 160 | void processHostMovedEvent(HostEvent event) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 161 | MacAddress hostMac = event.subject().mac(); |
| 162 | VlanId hostVlanId = event.subject().vlan(); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 163 | Set<HostLocation> prevLocations = event.prevSubject().locations(); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 164 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 165 | Set<HostLocation> newLocations = event.subject().locations(); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 166 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 167 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 168 | // FIXME: Delay event handling a little bit to wait for the previous redirection flows to be completed |
| 169 | // The permanent solution would be introducing CompletableFuture and wait for it |
| 170 | if (prevLocations.size() == 1 && newLocations.size() == 2) { |
| 171 | log.debug("Delay event handling when host {}/{} moves from 1 to 2 locations", hostMac, hostVlanId); |
| 172 | ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); |
| 173 | executorService.schedule(() -> |
| 174 | processHostMoved(hostMac, hostVlanId, prevLocations, prevIps, newLocations, newIps), |
| 175 | HOST_MOVED_DELAY_MS, TimeUnit.MILLISECONDS); |
| 176 | } else { |
| 177 | processHostMoved(hostMac, hostVlanId, prevLocations, prevIps, newLocations, newIps); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private void processHostMoved(MacAddress hostMac, VlanId hostVlanId, Set<HostLocation> prevLocations, |
| 182 | Set<IpAddress> prevIps, Set<HostLocation> newLocations, Set<IpAddress> newIps) { |
| 183 | log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 184 | Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId) |
| 185 | .collect(Collectors.toSet()); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 186 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 187 | // For each old location |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 188 | Sets.difference(prevLocations, newLocations).forEach(prevLocation -> { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 189 | // Remove routing rules for old IPs |
| 190 | Sets.difference(prevIps, newIps).forEach(ip -> |
| 191 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
| 192 | ip, true) |
| 193 | ); |
| 194 | |
| 195 | // Redirect the flows to pair link if configured |
| 196 | // Note: Do not continue removing any rule |
| 197 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId()); |
| 198 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPorts(prevLocation.deviceId()); |
| 199 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream() |
| 200 | .anyMatch(location -> location.deviceId().equals(pairDeviceId.get()))) { |
| 201 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 202 | // when the host is untagged |
| 203 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId); |
| 204 | |
| 205 | processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false); |
| 206 | newIps.forEach(ip -> |
| 207 | processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, |
| 208 | ip, false)); |
| 209 | return; |
| 210 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 211 | |
| 212 | // Remove bridging rule and routing rules for unchanged IPs if the host moves from a switch to another. |
| 213 | // Otherwise, do not remove and let the adding part update the old flow |
| 214 | if (!newDeviceIds.contains(prevLocation.deviceId())) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 215 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 216 | Sets.intersection(prevIps, newIps).forEach(ip -> |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 217 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 218 | ip, true) |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | // Remove bridging rules if new interface vlan is different from old interface vlan |
| 223 | // Otherwise, do not remove and let the adding part update the old flow |
| 224 | if (newLocations.stream().noneMatch(newLocation -> { |
| 225 | VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation); |
| 226 | VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation); |
| 227 | // Host is tagged and the new location has the host vlan in vlan-tagged |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 228 | return srManager.getTaggedVlanId(newLocation).contains(hostVlanId) || |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 229 | (oldAssignedVlan != null && newAssignedVlan != null && |
| 230 | // Host is untagged and the new location has the same assigned vlan |
| 231 | oldAssignedVlan.equals(newAssignedVlan)); |
| 232 | })) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 233 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // Remove routing rules for unchanged IPs if none of the subnet of new location contains |
| 237 | // the IP. Otherwise, do not remove and let the adding part update the old flow |
| 238 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 239 | if (newLocations.stream().noneMatch(newLocation -> |
| 240 | srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 241 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 242 | ip, true); |
| 243 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 244 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 245 | }); |
| 246 | |
| 247 | // For each new location, add all new IPs. |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 248 | Sets.difference(newLocations, prevLocations).stream() |
Charles Chan | 9f08b10 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 249 | .forEach(newLocation -> { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 250 | processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 251 | newIps.forEach(ip -> |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 252 | processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 253 | ip, false) |
| 254 | ); |
| 255 | }); |
| 256 | |
| 257 | // For each unchanged location, add new IPs and remove old IPs. |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 258 | Sets.intersection(newLocations, prevLocations).stream() |
Charles Chan | 9f08b10 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 259 | .forEach(unchangedLocation -> { |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 260 | Sets.difference(prevIps, newIps).forEach(ip -> |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 261 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), hostMac, |
| 262 | hostVlanId, ip, true) |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 263 | ); |
| 264 | |
| 265 | Sets.difference(newIps, prevIps).forEach(ip -> |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 266 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), hostMac, |
| 267 | hostVlanId, ip, false) |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 268 | ); |
| 269 | }); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 270 | |
| 271 | // ensure dual-homed host locations have viable uplinks |
| 272 | if (newLocations.size() > prevLocations.size()) { |
| 273 | newLocations.forEach(loc -> { |
| 274 | if (srManager.mastershipService.isLocalMaster(loc.deviceId())) { |
| 275 | srManager.linkHandler.checkUplinksForDualHomedHosts(loc); |
| 276 | } |
| 277 | }); |
| 278 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 281 | void processHostUpdatedEvent(HostEvent event) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 282 | Host host = event.subject(); |
| 283 | MacAddress hostMac = host.mac(); |
| 284 | VlanId hostVlanId = host.vlan(); |
| 285 | Set<HostLocation> locations = host.locations(); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 286 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 287 | Set<IpAddress> newIps = host.ipAddresses(); |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 288 | log.info("Host {}/{} is updated", hostMac, hostVlanId); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 289 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 290 | locations.forEach(location -> { |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 291 | Sets.difference(prevIps, newIps).forEach(ip -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 292 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true) |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 293 | ); |
| 294 | Sets.difference(newIps, prevIps).forEach(ip -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 295 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false) |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 296 | ); |
| 297 | }); |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 298 | |
| 299 | // Use the pair link temporarily before the second location of a dual-homed host shows up. |
| 300 | // This do not affect single-homed hosts since the flow will be blocked in |
| 301 | // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 302 | locations.forEach(location -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 303 | srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 304 | if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 305 | Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps); |
| 306 | Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps); |
| 307 | |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 308 | srManager.getPairLocalPorts(pairDeviceId).ifPresent(pairRemotePort -> { |
| 309 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 310 | // when the host is untagged |
| 311 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId); |
| 312 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 313 | ipsToRemove.forEach(ip -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 314 | processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true) |
| 315 | ); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 316 | ipsToAdd.forEach(ip -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 317 | processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false) |
| 318 | ); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 319 | |
| 320 | if (srManager.activeProbing) { |
| 321 | probe(host, location, pairDeviceId, pairRemotePort); |
| 322 | } |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 323 | }); |
| 324 | } |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 325 | }) |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * When a non-pair port comes up, probe each host on the pair device if |
| 331 | * (1) the host is tagged and the tagged vlan of current port contains host vlan; or |
| 332 | * (2) the host is untagged and the internal vlan is the same on the host port and current port. |
| 333 | * |
| 334 | * @param cp connect point |
| 335 | */ |
| 336 | void processPortUp(ConnectPoint cp) { |
| 337 | if (cp.port().equals(srManager.getPairLocalPorts(cp.deviceId()).orElse(null))) { |
| 338 | return; |
| 339 | } |
| 340 | if (srManager.activeProbing) { |
| 341 | srManager.getPairDeviceId(cp.deviceId()) |
| 342 | .ifPresent(pairDeviceId -> srManager.hostService.getConnectedHosts(pairDeviceId).stream() |
| 343 | .filter(host -> isHostInVlanOfPort(host, pairDeviceId, cp)) |
| 344 | .forEach(host -> srManager.probingService.probeHostLocation(host, cp, ProbeMode.DISCOVER)) |
| 345 | ); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Checks if given host located on given device id matches VLAN config of current port. |
| 351 | * |
| 352 | * @param host host to check |
| 353 | * @param deviceId device id to check |
| 354 | * @param cp current connect point |
| 355 | * @return true if the host located at deviceId matches the VLAN config on cp |
| 356 | */ |
| 357 | private boolean isHostInVlanOfPort(Host host, DeviceId deviceId, ConnectPoint cp) { |
| 358 | VlanId internalVlan = srManager.getInternalVlanId(cp); |
| 359 | Set<VlanId> taggedVlan = srManager.getTaggedVlanId(cp); |
| 360 | |
| 361 | return taggedVlan.contains(host.vlan()) || |
| 362 | (internalVlan != null && host.locations().stream() |
| 363 | .filter(l -> l.deviceId().equals(deviceId)) |
| 364 | .map(srManager::getInternalVlanId) |
| 365 | .anyMatch(internalVlan::equals)); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Send a probe on all locations with the same VLAN on pair device, excluding pair port. |
| 370 | * |
| 371 | * @param host host to probe |
| 372 | * @param location newly discovered host location |
| 373 | * @param pairDeviceId pair device id |
| 374 | * @param pairRemotePort pair remote port |
| 375 | */ |
| 376 | private void probe(Host host, ConnectPoint location, DeviceId pairDeviceId, PortNumber pairRemotePort) { |
| 377 | VlanId vlanToProbe = host.vlan().equals(VlanId.NONE) ? |
| 378 | srManager.getInternalVlanId(location) : host.vlan(); |
| 379 | srManager.interfaceService.getInterfaces().stream() |
| 380 | .filter(i -> i.vlanTagged().contains(vlanToProbe) || |
| 381 | i.vlanUntagged().equals(vlanToProbe) || |
| 382 | i.vlanNative().equals(vlanToProbe)) |
| 383 | .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId)) |
| 384 | .filter(i -> !i.connectPoint().port().equals(pairRemotePort)) |
| 385 | .forEach(i -> { |
| 386 | log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint()); |
| 387 | srManager.probingService.probeHostLocation(host, i.connectPoint(), ProbeMode.DISCOVER); |
| 388 | }); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /** |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 392 | * Populate or revoke a bridging rule on given deviceId that matches given mac, given vlan and |
| 393 | * output to given port. |
| 394 | * |
| 395 | * @param deviceId device ID |
| 396 | * @param port port |
| 397 | * @param mac mac address |
| 398 | * @param vlanId VLAN ID |
| 399 | * @param revoke true to revoke the rule; false to populate |
| 400 | */ |
| 401 | private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 402 | VlanId vlanId, boolean revoke) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 403 | log.info("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating", |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 404 | mac, vlanId, deviceId, port); |
| 405 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 406 | if (!revoke) { |
| 407 | srManager.defaultRoutingHandler.populateBridging(deviceId, port, mac, vlanId); |
| 408 | } else { |
| 409 | srManager.defaultRoutingHandler.revokeBridging(deviceId, port, mac, vlanId); |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 410 | } |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Populate or revoke a routing rule on given deviceId that matches given ip, |
| 415 | * set destination mac to given mac, set vlan to given vlan and output to given port. |
| 416 | * |
| 417 | * @param deviceId device ID |
| 418 | * @param port port |
| 419 | * @param mac mac address |
| 420 | * @param vlanId VLAN ID |
| 421 | * @param ip IP address |
| 422 | * @param revoke true to revoke the rule; false to populate |
| 423 | */ |
| 424 | private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 425 | VlanId vlanId, IpAddress ip, boolean revoke) { |
| 426 | ConnectPoint location = new ConnectPoint(deviceId, port); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 427 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 428 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 429 | return; |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 430 | } |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 431 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 432 | log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location); |
| 433 | if (revoke) { |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 434 | srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 435 | } else { |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 436 | srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 437 | } |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 438 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 439 | |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 440 | |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 441 | |
| 442 | /** |
| 443 | * Update forwarding objective for unicast bridging and unicast routing. |
| 444 | * Also check the validity of updated interface configuration on VLAN. |
| 445 | * |
| 446 | * @param deviceId device ID that host attaches to |
| 447 | * @param portNum port number that host attaches to |
| 448 | * @param vlanId Vlan ID configured on the switch port |
| 449 | * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise |
| 450 | * @param install true to populate the objective, false to revoke |
| 451 | */ |
| 452 | void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId, |
| 453 | boolean popVlan, boolean install) { |
| 454 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum); |
| 455 | Set<Host> hosts = hostService.getConnectedHosts(connectPoint); |
| 456 | |
| 457 | if (hosts == null || hosts.size() == 0) { |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | hosts.forEach(host -> { |
| 462 | MacAddress mac = host.mac(); |
| 463 | VlanId hostVlanId = host.vlan(); |
| 464 | |
| 465 | // Check whether the host vlan is valid for new interface configuration |
| 466 | if ((!popVlan && hostVlanId.equals(vlanId)) || |
| 467 | (popVlan && hostVlanId.equals(VlanId.NONE))) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 468 | srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, install); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 469 | // Update Forwarding objective and corresponding simple Next objective |
| 470 | // for each host and IP address connected to given port |
| 471 | host.ipAddresses().forEach(ipAddress -> |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 472 | srManager.defaultRoutingHandler.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(), |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 473 | mac, vlanId, popVlan, install) |
| 474 | ); |
| 475 | } |
| 476 | }); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Populate or revoke routing rule for each host, according to the updated |
| 481 | * subnet configuration on the interface. |
| 482 | * @param cp connect point of the updated interface |
| 483 | * @param ipPrefixSet IP Prefixes added or removed |
| 484 | * @param install true if IP Prefixes added, false otherwise |
| 485 | */ |
| 486 | void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) { |
| 487 | Set<Host> hosts = hostService.getConnectedHosts(cp); |
| 488 | |
| 489 | if (hosts == null || hosts.size() == 0) { |
| 490 | log.warn("processIntfIpUpdatedEvent: No hosts connected to {}", cp); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | // Check whether the host IP address is in the interface's subnet |
| 495 | hosts.forEach(host -> |
| 496 | host.ipAddresses().forEach(hostIpAddress -> { |
| 497 | ipPrefixSet.forEach(ipPrefix -> { |
| 498 | if (install && ipPrefix.contains(hostIpAddress)) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 499 | srManager.defaultRoutingHandler.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 500 | host.mac(), host.vlan(), cp.port()); |
| 501 | } else if (!install && ipPrefix.contains(hostIpAddress)) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame^] | 502 | srManager.defaultRoutingHandler.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 503 | host.mac(), host.vlan(), cp.port()); |
| 504 | } |
| 505 | }); |
| 506 | })); |
| 507 | } |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 508 | |
| 509 | /** |
| 510 | * Returns the set of portnumbers on the given device that are part of the |
| 511 | * locations for dual-homed hosts. |
| 512 | * |
| 513 | * @param deviceId the given deviceId |
| 514 | * @return set of port numbers on given device that are dual-homed host |
| 515 | * locations. May be empty if no dual homed hosts are connected to |
| 516 | * the given device |
| 517 | */ |
| 518 | Set<PortNumber> getDualHomedHostPorts(DeviceId deviceId) { |
| 519 | Set<PortNumber> dualHomedLocations = new HashSet<>(); |
| 520 | srManager.hostService.getConnectedHosts(deviceId).stream() |
| 521 | .filter(host -> host.locations().size() == 2) |
| 522 | .forEach(host -> host.locations().stream() |
| 523 | .filter(loc -> loc.deviceId().equals(deviceId)) |
| 524 | .forEach(loc -> dualHomedLocations.add(loc.port()))); |
| 525 | return dualHomedLocations; |
| 526 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 527 | } |