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