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 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 19 | import org.onlab.packet.EthType; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 24 | import org.onlab.util.PredictableExecutor; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Host; |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 28 | import org.onosproject.net.HostLocation; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 29 | import org.onosproject.net.PortNumber; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 30 | import org.onosproject.net.host.HostEvent; |
| 31 | import org.onosproject.net.host.HostService; |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 32 | import org.onosproject.net.host.ProbeMode; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 36 | import com.google.common.collect.Sets; |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 37 | |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 38 | import java.util.HashSet; |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 39 | import java.util.Optional; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 40 | import java.util.Set; |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 41 | import java.util.stream.Collectors; |
| 42 | |
| 43 | import static com.google.common.base.Preconditions.checkArgument; |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 44 | import static org.onlab.util.Tools.groupedThreads; |
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 | |
Charles Chan | 2e2e340 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 52 | protected final SegmentRoutingManager srManager; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 53 | private HostService hostService; |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 54 | // Host workers - 0 will leverage available processors |
| 55 | private static final int DEFAULT_THREADS = 0; |
| 56 | protected PredictableExecutor hostWorkers; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Constructs the HostHandler. |
| 60 | * |
| 61 | * @param srManager Segment Routing manager |
| 62 | */ |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 63 | HostHandler(SegmentRoutingManager srManager) { |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 64 | this.srManager = srManager; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 65 | hostService = srManager.hostService; |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 66 | this.hostWorkers = new PredictableExecutor(DEFAULT_THREADS, |
| 67 | groupedThreads("onos/sr", "h-worker-%d", log)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Shutdowns the workers. |
| 72 | */ |
| 73 | void terminate() { |
| 74 | hostWorkers.shutdown(); |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Charles Chan | 03a73e0 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 77 | protected void init(DeviceId devId) { |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 78 | // Init hosts in parallel using hostWorkers executor |
| 79 | hostService.getHosts().forEach( |
| 80 | host -> hostWorkers.execute(() -> initHost(host, devId), host.id().hashCode()) |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 81 | ); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 82 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 83 | |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 84 | private void initHost(Host host, DeviceId deviceId) { |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 85 | effectiveLocations(host).forEach(location -> { |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 86 | if (location.deviceId().equals(deviceId) || |
| 87 | location.deviceId().equals(srManager.getPairDeviceId(deviceId).orElse(null))) { |
| 88 | processHostAddedAtLocation(host, location); |
| 89 | } |
| 90 | }); |
| 91 | } |
| 92 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 93 | void processHostAddedEvent(HostEvent event) { |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 94 | Host host = event.subject(); |
| 95 | hostWorkers.execute(() -> processHostAdded(host), host.id().hashCode()); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 98 | private void processHostAdded(Host host) { |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 99 | effectiveLocations(host).forEach(location -> processHostAddedAtLocation(host, location)); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 100 | // ensure dual-homed host locations have viable uplinks |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 101 | if (effectiveLocations(host).size() > 1 || srManager.singleHomedDown) { |
| 102 | effectiveLocations(host).forEach(loc -> { |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 103 | if (srManager.mastershipService.isLocalMaster(loc.deviceId())) { |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 104 | srManager.linkHandler.checkUplinksForHost(loc); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 105 | } |
| 106 | }); |
| 107 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 110 | void processHostAddedAtLocation(Host host, HostLocation location) { |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 111 | checkArgument(effectiveLocations(host).contains(location), "{} is not a location of {}", location, host); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 112 | |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 113 | MacAddress hostMac = host.mac(); |
| 114 | VlanId hostVlanId = host.vlan(); |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 115 | Set<HostLocation> locations = effectiveLocations(host); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 116 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 117 | log.info("Host {}/{} is added at {}", hostMac, hostVlanId, locations); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 118 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 119 | if (isDoubleTaggedHost(host)) { |
| 120 | ips.forEach(ip -> |
| 121 | processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac, |
| 122 | host.innerVlan(), hostVlanId, host.tpid(), ip, false) |
| 123 | ); |
| 124 | } else { |
| 125 | processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, false); |
| 126 | ips.forEach(ip -> |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 127 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false) |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 128 | ); |
| 129 | } |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 130 | |
| 131 | // Use the pair link temporarily before the second location of a dual-homed host shows up. |
| 132 | // This do not affect single-homed hosts since the flow will be blocked in |
| 133 | // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively |
| 134 | srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> { |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 135 | if (effectiveLocations(host).stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 136 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> { |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 137 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 138 | // when the host is untagged |
Charles Chan | f76de30 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 139 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 140 | if (vlanId == null) { |
| 141 | return; |
| 142 | } |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 143 | |
| 144 | processBridgingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, false); |
| 145 | ips.forEach(ip -> processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, |
| 146 | ip, false)); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 147 | |
| 148 | if (srManager.activeProbing) { |
| 149 | probe(host, location, pairDeviceId, pairRemotePort); |
| 150 | } |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 151 | }); |
| 152 | } |
| 153 | }); |
Ruchi Sahota | c8aa9f3 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 154 | |
| 155 | int nextId = srManager.getMacVlanNextObjectiveId(location.deviceId(), hostMac, hostVlanId, null, false); |
| 156 | if (nextId != -1) { |
| 157 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId); |
| 158 | log.debug(" Updating next objective for device {}, host {}/{}, port {}, nextid {}", |
| 159 | location.deviceId(), hostMac, vlanId, location.port(), nextId); |
| 160 | srManager.updateMacVlanTreatment(location.deviceId(), hostMac, vlanId, |
| 161 | location.port(), nextId); |
| 162 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void processHostRemovedEvent(HostEvent event) { |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 166 | Host host = event.subject(); |
| 167 | hostWorkers.execute(() -> processHostRemoved(host), host.id().hashCode()); |
Charles Chan | 35fd1a7 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 170 | private void processHostRemoved(Host host) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 171 | MacAddress hostMac = host.mac(); |
| 172 | VlanId hostVlanId = host.vlan(); |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 173 | Set<HostLocation> locations = effectiveLocations(host); |
Charles Chan | 35fd1a7 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 174 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 175 | log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 176 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 177 | locations.forEach(location -> { |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 178 | if (isDoubleTaggedHost(host)) { |
| 179 | ips.forEach(ip -> |
| 180 | processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac, |
| 181 | host.innerVlan(), hostVlanId, host.tpid(), ip, true) |
| 182 | ); |
| 183 | } else { |
| 184 | processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, true); |
| 185 | ips.forEach(ip -> |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 186 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true) |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 187 | ); |
| 188 | } |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 189 | |
| 190 | // Also remove redirection flows on the pair device if exists. |
| 191 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId()); |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 192 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId()); |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 193 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 194 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 195 | // when the host is untagged |
Charles Chan | f76de30 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 196 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 197 | if (vlanId == null) { |
| 198 | return; |
| 199 | } |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 200 | |
| 201 | processBridgingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, true); |
| 202 | ips.forEach(ip -> |
| 203 | processRoutingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, |
| 204 | ip, true)); |
| 205 | } |
Charles Chan | 4d63939 | 2018-06-21 19:07:12 -0700 | [diff] [blame] | 206 | |
| 207 | // Delete prefix from sr-device-subnet when the next hop host is removed |
| 208 | srManager.routeService.getRouteTables().forEach(tableId -> { |
| 209 | srManager.routeService.getRoutes(tableId).forEach(routeInfo -> { |
| 210 | if (routeInfo.allRoutes().stream().anyMatch(rr -> ips.contains(rr.nextHop()))) { |
| 211 | log.debug("HostRemoved. removeSubnet {}, {}", location, routeInfo.prefix()); |
| 212 | srManager.deviceConfiguration.removeSubnet(location, routeInfo.prefix()); |
| 213 | } |
| 214 | }); |
| 215 | }); |
Ruchi Sahota | c8aa9f3 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 216 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 217 | }); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 220 | void processHostMovedEvent(HostEvent event) { |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 221 | Host host = event.subject(); |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 222 | hostWorkers.execute(() -> processHostMovedEventInternal(event), host.id().hashCode()); |
| 223 | } |
| 224 | |
| 225 | private void processHostMovedEventInternal(HostEvent event) { |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 226 | // This method will be called when one of the following value has changed: |
| 227 | // (1) locations (2) auxLocations or (3) both locations and auxLocations. |
| 228 | // We only need to proceed when effectiveLocation has changed. |
| 229 | Set<HostLocation> newLocations = effectiveLocations(event.subject()); |
| 230 | Set<HostLocation> prevLocations = effectiveLocations(event.prevSubject()); |
| 231 | |
| 232 | if (newLocations.equals(prevLocations)) { |
| 233 | log.info("effectiveLocations of {} has not changed. Skipping {}", event.subject().id(), event); |
| 234 | return; |
| 235 | } |
| 236 | |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 237 | Host host = event.subject(); |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 238 | Host prevHost = event.prevSubject(); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 239 | MacAddress hostMac = host.mac(); |
| 240 | VlanId hostVlanId = host.vlan(); |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 241 | Set<IpAddress> prevIps = prevHost.ipAddresses(); |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 242 | Set<IpAddress> newIps = host.ipAddresses(); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 243 | EthType hostTpid = host.tpid(); |
| 244 | boolean doubleTaggedHost = isDoubleTaggedHost(host); |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 245 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 246 | log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 247 | Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId) |
| 248 | .collect(Collectors.toSet()); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 249 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 250 | // For each old location |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 251 | Sets.difference(prevLocations, newLocations).forEach(prevLocation -> { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 252 | // Remove routing rules for old IPs |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 253 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 254 | if (doubleTaggedHost) { |
| 255 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 256 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 257 | } else { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 258 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 259 | ip, true); |
| 260 | } |
| 261 | }); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 262 | |
| 263 | // Redirect the flows to pair link if configured |
| 264 | // Note: Do not continue removing any rule |
| 265 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId()); |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 266 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(prevLocation.deviceId()); |
Charles Chan | 5145de2 | 2018-10-11 12:35:36 -0700 | [diff] [blame] | 267 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && |
| 268 | newLocations.stream().anyMatch(location -> location.deviceId().equals(pairDeviceId.get())) && |
| 269 | newLocations.stream().noneMatch(location -> location.deviceId().equals(prevLocation.deviceId()))) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 270 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 271 | // when the host is untagged |
| 272 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId); |
| 273 | |
| 274 | processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false); |
| 275 | newIps.forEach(ip -> |
| 276 | processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, |
| 277 | ip, false)); |
| 278 | return; |
| 279 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 280 | |
Charles Chan | 50bb6ef | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 281 | // Remove flows for unchanged IPs only when the host moves from a switch to another. |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 282 | // Otherwise, do not remove and let the adding part update the old flow |
| 283 | if (!newDeviceIds.contains(prevLocation.deviceId())) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 284 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 285 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 286 | if (doubleTaggedHost) { |
| 287 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 288 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 289 | } else { |
| 290 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 291 | hostMac, hostVlanId, ip, true); |
| 292 | } |
| 293 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // Remove bridging rules if new interface vlan is different from old interface vlan |
| 297 | // Otherwise, do not remove and let the adding part update the old flow |
| 298 | if (newLocations.stream().noneMatch(newLocation -> { |
| 299 | VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation); |
| 300 | VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation); |
| 301 | // Host is tagged and the new location has the host vlan in vlan-tagged |
Charles Chan | 971d7ba | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 302 | return srManager.interfaceService.getTaggedVlanId(newLocation).contains(hostVlanId) || |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 303 | (oldAssignedVlan != null && newAssignedVlan != null && |
| 304 | // Host is untagged and the new location has the same assigned vlan |
| 305 | oldAssignedVlan.equals(newAssignedVlan)); |
| 306 | })) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 307 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Remove routing rules for unchanged IPs if none of the subnet of new location contains |
| 311 | // the IP. Otherwise, do not remove and let the adding part update the old flow |
| 312 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 313 | if (newLocations.stream().noneMatch(newLocation -> |
| 314 | srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) { |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 315 | if (doubleTaggedHost) { |
| 316 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 317 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 318 | } else { |
| 319 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 320 | hostMac, hostVlanId, ip, true); |
| 321 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 322 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 323 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 324 | }); |
| 325 | |
| 326 | // For each new location, add all new IPs. |
Charles Chan | 50bb6ef | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 327 | Sets.difference(newLocations, prevLocations).forEach(newLocation -> { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 328 | processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 329 | newIps.forEach(ip -> { |
| 330 | if (doubleTaggedHost) { |
| 331 | processDoubleTaggedRoutingRule(newLocation.deviceId(), newLocation.port(), |
| 332 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 333 | } else { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 334 | processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 335 | ip, false); |
| 336 | } |
| 337 | }); |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 338 | |
| 339 | // Probe on pair device when host move |
| 340 | // Majorly for the 2nd step of [1A/x, 1B/x] -> [1A/x, 1B/y] -> [1A/y, 1B/y] |
| 341 | // But will also cover [1A/x] -> [1A/y] -> [1A/y, 1B/y] |
| 342 | if (srManager.activeProbing) { |
Charles Chan | f82d155 | 2018-07-23 15:27:34 -0700 | [diff] [blame] | 343 | |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 344 | srManager.getPairDeviceId(newLocation.deviceId()).ifPresent(pairDeviceId -> |
| 345 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> |
| 346 | probe(host, newLocation, pairDeviceId, pairRemotePort) |
| 347 | ) |
| 348 | ); |
| 349 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 350 | }); |
| 351 | |
| 352 | // For each unchanged location, add new IPs and remove old IPs. |
Charles Chan | 50bb6ef | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 353 | Sets.intersection(newLocations, prevLocations).forEach(unchangedLocation -> { |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 354 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 355 | if (doubleTaggedHost) { |
| 356 | processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 357 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 358 | } else { |
| 359 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 360 | hostMac, hostVlanId, ip, true); |
| 361 | } |
| 362 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 363 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 364 | Sets.difference(newIps, prevIps).forEach(ip -> { |
| 365 | if (doubleTaggedHost) { |
| 366 | processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 367 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 368 | } else { |
| 369 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 370 | hostMac, hostVlanId, ip, false); |
| 371 | } |
| 372 | }); |
Charles Chan | f82d155 | 2018-07-23 15:27:34 -0700 | [diff] [blame] | 373 | |
| 374 | // Verify existing location and see if it is still valid |
| 375 | srManager.probingService.probeHost(host, unchangedLocation, ProbeMode.VERIFY); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 376 | }); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 377 | |
| 378 | // ensure dual-homed host locations have viable uplinks |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 379 | if (newLocations.size() > prevLocations.size() || srManager.singleHomedDown) { |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 380 | newLocations.forEach(loc -> { |
| 381 | if (srManager.mastershipService.isLocalMaster(loc.deviceId())) { |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 382 | srManager.linkHandler.checkUplinksForHost(loc); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 383 | } |
| 384 | }); |
| 385 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 388 | void processHostUpdatedEvent(HostEvent event) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 389 | Host host = event.subject(); |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 390 | hostWorkers.execute(() -> processHostUpdatedEventInternal(event), host.id().hashCode()); |
| 391 | } |
| 392 | |
| 393 | private void processHostUpdatedEventInternal(HostEvent event) { |
| 394 | Host host = event.subject(); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 395 | MacAddress hostMac = host.mac(); |
| 396 | VlanId hostVlanId = host.vlan(); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 397 | EthType hostTpid = host.tpid(); |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 398 | Set<HostLocation> locations = effectiveLocations(host); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 399 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 400 | Set<IpAddress> newIps = host.ipAddresses(); |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 401 | log.info("Host {}/{} is updated", hostMac, hostVlanId); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 402 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 403 | locations.forEach(location -> { |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 404 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 405 | if (isDoubleTaggedHost(host)) { |
| 406 | processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac, |
| 407 | host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 408 | } else { |
| 409 | processRoutingRule(location.deviceId(), location.port(), hostMac, |
| 410 | hostVlanId, ip, true); |
| 411 | } |
| 412 | }); |
| 413 | Sets.difference(newIps, prevIps).forEach(ip -> { |
| 414 | if (isDoubleTaggedHost(host)) { |
| 415 | processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac, |
| 416 | host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 417 | } else { |
| 418 | processRoutingRule(location.deviceId(), location.port(), hostMac, |
| 419 | hostVlanId, ip, false); |
| 420 | } |
| 421 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 422 | }); |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 423 | |
| 424 | // Use the pair link temporarily before the second location of a dual-homed host shows up. |
| 425 | // This do not affect single-homed hosts since the flow will be blocked in |
| 426 | // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 427 | locations.forEach(location -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 428 | srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 429 | if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 430 | Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps); |
| 431 | Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps); |
| 432 | |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 433 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> { |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 434 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 435 | // when the host is untagged |
Charles Chan | f76de30 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 436 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 437 | if (vlanId == null) { |
| 438 | return; |
| 439 | } |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 440 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 441 | ipsToRemove.forEach(ip -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 442 | processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true) |
| 443 | ); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 444 | ipsToAdd.forEach(ip -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 445 | processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false) |
| 446 | ); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 447 | |
| 448 | if (srManager.activeProbing) { |
| 449 | probe(host, location, pairDeviceId, pairRemotePort); |
| 450 | } |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 451 | }); |
| 452 | } |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 453 | }) |
| 454 | ); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * When a non-pair port comes up, probe each host on the pair device if |
| 459 | * (1) the host is tagged and the tagged vlan of current port contains host vlan; or |
| 460 | * (2) the host is untagged and the internal vlan is the same on the host port and current port. |
| 461 | * |
| 462 | * @param cp connect point |
| 463 | */ |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 464 | // TODO Current implementation does not handle dual-homed hosts with auxiliary locations. |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 465 | void processPortUp(ConnectPoint cp) { |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 466 | if (cp.port().equals(srManager.getPairLocalPort(cp.deviceId()).orElse(null))) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 467 | return; |
| 468 | } |
| 469 | if (srManager.activeProbing) { |
| 470 | srManager.getPairDeviceId(cp.deviceId()) |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 471 | .ifPresent(pairDeviceId -> srManager.hostService.getConnectedHosts(pairDeviceId).forEach( |
| 472 | host -> hostWorkers.execute(() -> probingIfNecessary(host, pairDeviceId, cp), |
| 473 | host.id().hashCode()))); |
| 474 | } |
| 475 | } |
| 476 | |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 477 | // TODO Current implementation does not handle dual-homed hosts with auxiliary locations. |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 478 | private void probingIfNecessary(Host host, DeviceId pairDeviceId, ConnectPoint cp) { |
| 479 | if (isHostInVlanOfPort(host, pairDeviceId, cp)) { |
| 480 | srManager.probingService.probeHost(host, cp, ProbeMode.DISCOVER); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Checks if given host located on given device id matches VLAN config of current port. |
| 486 | * |
| 487 | * @param host host to check |
| 488 | * @param deviceId device id to check |
| 489 | * @param cp current connect point |
| 490 | * @return true if the host located at deviceId matches the VLAN config on cp |
| 491 | */ |
| 492 | private boolean isHostInVlanOfPort(Host host, DeviceId deviceId, ConnectPoint cp) { |
| 493 | VlanId internalVlan = srManager.getInternalVlanId(cp); |
Charles Chan | 971d7ba | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 494 | Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(cp); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 495 | |
| 496 | return taggedVlan.contains(host.vlan()) || |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 497 | (internalVlan != null && effectiveLocations(host).stream() |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 498 | .filter(l -> l.deviceId().equals(deviceId)) |
| 499 | .map(srManager::getInternalVlanId) |
| 500 | .anyMatch(internalVlan::equals)); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Send a probe on all locations with the same VLAN on pair device, excluding pair port. |
| 505 | * |
| 506 | * @param host host to probe |
| 507 | * @param location newly discovered host location |
| 508 | * @param pairDeviceId pair device id |
| 509 | * @param pairRemotePort pair remote port |
| 510 | */ |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 511 | // TODO Current implementation does not handle dual-homed hosts with auxiliary locations. |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 512 | private void probe(Host host, ConnectPoint location, DeviceId pairDeviceId, PortNumber pairRemotePort) { |
Mayank Tiwari | a61b0a8 | 2018-10-29 18:27:35 -0400 | [diff] [blame] | 513 | //Check if the host still exists in the host store |
| 514 | if (hostService.getHost(host.id()) == null) { |
| 515 | log.debug("Host entry for host {} no more present. Aborting hostprobe discover for this host", host.id()); |
| 516 | return; |
| 517 | } |
| 518 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 519 | VlanId vlanToProbe = host.vlan().equals(VlanId.NONE) ? |
| 520 | srManager.getInternalVlanId(location) : host.vlan(); |
Mayank Tiwari | a61b0a8 | 2018-10-29 18:27:35 -0400 | [diff] [blame] | 521 | if (srManager.symmetricProbing) { |
| 522 | srManager.interfaceService.getInterfaces().stream() |
| 523 | .filter(i -> i.vlanTagged().contains(vlanToProbe) || |
| 524 | i.vlanUntagged().equals(vlanToProbe) || |
| 525 | i.vlanNative().equals(vlanToProbe)) |
| 526 | .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId)) |
| 527 | .filter(i -> i.connectPoint().port().equals(location.port())) |
| 528 | .forEach(i -> { |
| 529 | log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint()); |
| 530 | srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER); |
| 531 | }); |
| 532 | } else { |
| 533 | srManager.interfaceService.getInterfaces().stream() |
| 534 | .filter(i -> i.vlanTagged().contains(vlanToProbe) || |
| 535 | i.vlanUntagged().equals(vlanToProbe) || |
| 536 | i.vlanNative().equals(vlanToProbe)) |
| 537 | .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId)) |
| 538 | .filter(i -> !i.connectPoint().port().equals(pairRemotePort)) |
| 539 | .forEach(i -> { |
| 540 | log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint()); |
| 541 | srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER); |
| 542 | }); |
| 543 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /** |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 547 | * Populates or revokes a bridging rule on given deviceId that matches given mac, |
| 548 | * given vlan and output to given port. |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 549 | * |
| 550 | * @param deviceId device ID |
| 551 | * @param port port |
| 552 | * @param mac mac address |
| 553 | * @param vlanId VLAN ID |
| 554 | * @param revoke true to revoke the rule; false to populate |
| 555 | */ |
| 556 | private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 557 | VlanId vlanId, boolean revoke) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 558 | log.info("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating", |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 559 | mac, vlanId, deviceId, port); |
| 560 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 561 | if (!revoke) { |
| 562 | srManager.defaultRoutingHandler.populateBridging(deviceId, port, mac, vlanId); |
| 563 | } else { |
| 564 | srManager.defaultRoutingHandler.revokeBridging(deviceId, port, mac, vlanId); |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 565 | } |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /** |
| 569 | * Populate or revoke a routing rule on given deviceId that matches given ip, |
| 570 | * set destination mac to given mac, set vlan to given vlan and output to given port. |
| 571 | * |
| 572 | * @param deviceId device ID |
| 573 | * @param port port |
| 574 | * @param mac mac address |
| 575 | * @param vlanId VLAN ID |
| 576 | * @param ip IP address |
| 577 | * @param revoke true to revoke the rule; false to populate |
| 578 | */ |
| 579 | private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 580 | VlanId vlanId, IpAddress ip, boolean revoke) { |
| 581 | ConnectPoint location = new ConnectPoint(deviceId, port); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 582 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 583 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 584 | return; |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 585 | } |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 586 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 587 | log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location); |
| 588 | if (revoke) { |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 589 | srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port, true); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 590 | } else { |
Ruchi Sahota | ef0761c | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 591 | srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port, true); |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 592 | } |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 593 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 594 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 595 | /** |
| 596 | * Populate or revoke a routing rule and egress rules on given deviceId that matches given IP, |
| 597 | * to set destination mac to given mac, set inner vlan and outer vlan to given vlans, |
| 598 | * set outer TPID, and output to given port. |
| 599 | * |
| 600 | * @param deviceId device ID |
| 601 | * @param port port |
| 602 | * @param mac mac address |
| 603 | * @param innerVlan inner VLAN ID |
| 604 | * @param outerVlan outer VLAN ID |
| 605 | * @param outerTpid outer TPID |
| 606 | * @param ip IP address |
| 607 | * @param revoke true to revoke the rule; false to populate |
| 608 | */ |
| 609 | private void processDoubleTaggedRoutingRule(DeviceId deviceId, PortNumber port, |
| 610 | MacAddress mac, VlanId innerVlan, |
| 611 | VlanId outerVlan, EthType outerTpid, |
| 612 | IpAddress ip, boolean revoke) { |
Charles Chan | b67dfdd | 2018-07-24 16:40:35 -0700 | [diff] [blame] | 613 | if (!srManager.routeDoubleTaggedHosts) { |
| 614 | log.debug("Routing for double tagged host is disabled. Ignore {}/{}/{}", mac, outerVlan, innerVlan); |
Charles Chan | 57465d3 | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 615 | return; |
| 616 | } |
| 617 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 618 | ConnectPoint location = new ConnectPoint(deviceId, port); |
| 619 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 620 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 621 | return; |
| 622 | } |
| 623 | log.info("{} routing rule for double-tagged host {} at {}", |
| 624 | revoke ? "Revoking" : "Populating", ip, location); |
| 625 | if (revoke) { |
| 626 | srManager.defaultRoutingHandler.revokeDoubleTaggedRoute( |
| 627 | deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port); |
| 628 | } else { |
| 629 | srManager.defaultRoutingHandler.populateDoubleTaggedRoute( |
| 630 | deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port); |
| 631 | } |
| 632 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 633 | |
Charles Chan | 57465d3 | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 634 | void populateAllDoubleTaggedHost() { |
Charles Chan | b67dfdd | 2018-07-24 16:40:35 -0700 | [diff] [blame] | 635 | log.info("Enabling routing for all double tagged hosts"); |
Charles Chan | 57465d3 | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 636 | Sets.newHashSet(srManager.hostService.getHosts()).stream().filter(this::isDoubleTaggedHost) |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 637 | .forEach(h -> effectiveLocations(h).forEach(l -> |
Charles Chan | 57465d3 | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 638 | h.ipAddresses().forEach(i -> |
| 639 | processDoubleTaggedRoutingRule(l.deviceId(), l.port(), h.mac(), h.innerVlan(), |
| 640 | h.vlan(), h.tpid(), i, false) |
| 641 | ) |
| 642 | ) |
| 643 | ); |
| 644 | } |
| 645 | |
| 646 | void revokeAllDoubleTaggedHost() { |
Charles Chan | b67dfdd | 2018-07-24 16:40:35 -0700 | [diff] [blame] | 647 | log.info("Disabling routing for all double tagged hosts"); |
Charles Chan | 57465d3 | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 648 | Sets.newHashSet(srManager.hostService.getHosts()).stream().filter(this::isDoubleTaggedHost) |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 649 | .forEach(h -> effectiveLocations(h).forEach(l -> |
Charles Chan | 57465d3 | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 650 | h.ipAddresses().forEach(i -> |
| 651 | processDoubleTaggedRoutingRule(l.deviceId(), l.port(), h.mac(), h.innerVlan(), |
| 652 | h.vlan(), h.tpid(), i, true) |
| 653 | ) |
| 654 | ) |
| 655 | ); |
| 656 | } |
| 657 | |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 658 | /** |
Charles Chan | f76de30 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 659 | * Returns VLAN ID to be used to program redirection flow on pair port. |
| 660 | * |
| 661 | * @param hostVlanId host VLAN ID |
| 662 | * @param location host location |
| 663 | * @return VLAN ID to be used; Or null if host VLAN does not match the interface config |
| 664 | */ |
| 665 | VlanId vlanForPairPort(VlanId hostVlanId, ConnectPoint location) { |
| 666 | VlanId internalVlan = srManager.getInternalVlanId(location); |
| 667 | Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(location); |
| 668 | |
| 669 | if (!hostVlanId.equals(VlanId.NONE) && taggedVlan.contains(hostVlanId)) { |
| 670 | return hostVlanId; |
| 671 | } else if (hostVlanId.equals(VlanId.NONE) && internalVlan != null) { |
| 672 | return internalVlan; |
| 673 | } else { |
| 674 | log.warn("VLAN mismatch. hostVlan={}, location={}, internalVlan={}, taggedVlan={}", |
| 675 | hostVlanId, location, internalVlan, taggedVlan); |
| 676 | return null; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | /** |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 681 | * Update forwarding objective for unicast bridging and unicast routing. |
| 682 | * Also check the validity of updated interface configuration on VLAN. |
| 683 | * |
| 684 | * @param deviceId device ID that host attaches to |
| 685 | * @param portNum port number that host attaches to |
| 686 | * @param vlanId Vlan ID configured on the switch port |
| 687 | * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise |
| 688 | * @param install true to populate the objective, false to revoke |
| 689 | */ |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 690 | // TODO Current implementation does not handle dual-homed hosts with auxiliary locations. |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 691 | void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId, |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 692 | boolean popVlan, boolean install) { |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 693 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum); |
| 694 | Set<Host> hosts = hostService.getConnectedHosts(connectPoint); |
| 695 | |
| 696 | if (hosts == null || hosts.size() == 0) { |
Charles Chan | 10b2fee | 2018-04-21 00:44:29 -0700 | [diff] [blame] | 697 | log.debug("processIntfVlanUpdatedEvent: No hosts connected to {}", connectPoint); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 701 | hosts.forEach(host -> hostWorkers.execute(() -> processIntfVlanUpdatedEventInternal( |
| 702 | host, deviceId, portNum, vlanId, popVlan, install), host.id().hashCode())); |
| 703 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 704 | |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 705 | private void processIntfVlanUpdatedEventInternal(Host host, DeviceId deviceId, PortNumber portNum, |
| 706 | VlanId vlanId, boolean popVlan, boolean install) { |
| 707 | MacAddress mac = host.mac(); |
| 708 | VlanId hostVlanId = host.vlan(); |
| 709 | |
Shibu Vijayakumar | a0db2db | 2018-03-01 15:45:59 -0800 | [diff] [blame^] | 710 | if (!install) { |
| 711 | // Do not check the host validity. Just remove all rules corresponding to the vlan id |
| 712 | // Revoke forwarding objective for bridging to the host |
| 713 | srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, false); |
| 714 | |
| 715 | // Revoke forwarding objective and corresponding simple Next objective |
| 716 | // for each Host and IP address connected to given port |
| 717 | host.ipAddresses().forEach(ipAddress -> |
| 718 | srManager.routingRulePopulator.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(), |
| 719 | mac, vlanId, popVlan, false) |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 720 | ); |
Shibu Vijayakumar | a0db2db | 2018-03-01 15:45:59 -0800 | [diff] [blame^] | 721 | } else { |
| 722 | // Check whether the host vlan is valid for new interface configuration |
| 723 | if ((!popVlan && hostVlanId.equals(vlanId)) || |
| 724 | (popVlan && hostVlanId.equals(VlanId.NONE))) { |
| 725 | srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, true); |
| 726 | // Update Forwarding objective and corresponding simple Next objective |
| 727 | // for each Host and IP address connected to given port |
| 728 | host.ipAddresses().forEach(ipAddress -> |
| 729 | srManager.routingRulePopulator.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(), |
| 730 | mac, vlanId, popVlan, true) |
| 731 | ); |
| 732 | } |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 733 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /** |
| 737 | * Populate or revoke routing rule for each host, according to the updated |
| 738 | * subnet configuration on the interface. |
| 739 | * @param cp connect point of the updated interface |
| 740 | * @param ipPrefixSet IP Prefixes added or removed |
| 741 | * @param install true if IP Prefixes added, false otherwise |
| 742 | */ |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 743 | // TODO Current implementation does not handle dual-homed hosts with auxiliary locations. |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 744 | void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) { |
| 745 | Set<Host> hosts = hostService.getConnectedHosts(cp); |
| 746 | |
| 747 | if (hosts == null || hosts.size() == 0) { |
Charles Chan | 10b2fee | 2018-04-21 00:44:29 -0700 | [diff] [blame] | 748 | log.debug("processIntfIpUpdatedEvent: No hosts connected to {}", cp); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 749 | return; |
| 750 | } |
| 751 | |
| 752 | // Check whether the host IP address is in the interface's subnet |
pier | ff0a45a | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 753 | hosts.forEach(host -> hostWorkers.execute(() -> processIntfIpUpdatedEventInternal( |
| 754 | host, cp, ipPrefixSet, install))); |
| 755 | } |
| 756 | |
| 757 | private void processIntfIpUpdatedEventInternal(Host host, ConnectPoint cp, Set<IpPrefix> ipPrefixSet, |
| 758 | boolean install) { |
| 759 | host.ipAddresses().forEach(hostIpAddress -> { |
| 760 | ipPrefixSet.forEach(ipPrefix -> { |
| 761 | if (install && ipPrefix.contains(hostIpAddress)) { |
| 762 | srManager.defaultRoutingHandler.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
| 763 | host.mac(), host.vlan(), cp.port(), true); |
| 764 | } else if (!install && ipPrefix.contains(hostIpAddress)) { |
| 765 | srManager.defaultRoutingHandler.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
| 766 | host.mac(), host.vlan(), cp.port(), true); |
| 767 | } |
| 768 | }); |
| 769 | }); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 770 | } |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 771 | |
| 772 | /** |
| 773 | * Returns the set of portnumbers on the given device that are part of the |
| 774 | * locations for dual-homed hosts. |
| 775 | * |
| 776 | * @param deviceId the given deviceId |
| 777 | * @return set of port numbers on given device that are dual-homed host |
| 778 | * locations. May be empty if no dual homed hosts are connected to |
| 779 | * the given device |
| 780 | */ |
| 781 | Set<PortNumber> getDualHomedHostPorts(DeviceId deviceId) { |
| 782 | Set<PortNumber> dualHomedLocations = new HashSet<>(); |
| 783 | srManager.hostService.getConnectedHosts(deviceId).stream() |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 784 | .filter(host -> effectiveLocations(host).size() == 2) |
| 785 | .forEach(host -> effectiveLocations(host).stream() |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 786 | .filter(loc -> loc.deviceId().equals(deviceId)) |
| 787 | .forEach(loc -> dualHomedLocations.add(loc.port()))); |
| 788 | return dualHomedLocations; |
| 789 | } |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 790 | |
| 791 | /** |
| 792 | * Checks if the given host is double-tagged or not. |
| 793 | * |
| 794 | * @param host host to check |
| 795 | * @return true if it is double-tagged, false otherwise |
| 796 | */ |
| 797 | private boolean isDoubleTaggedHost(Host host) { |
| 798 | return !host.innerVlan().equals(VlanId.NONE); |
| 799 | } |
| 800 | |
Charles Chan | 2126f4a | 2019-11-25 09:47:22 -0800 | [diff] [blame] | 801 | /** |
| 802 | * Returns effective locations of given host. |
| 803 | * |
| 804 | * @param host host to check |
| 805 | * @return auxLocations of the host if exists, or locations of the host otherwise. |
| 806 | */ |
| 807 | Set<HostLocation> effectiveLocations(Host host) { |
| 808 | return (host.auxLocations() != null) ? host.auxLocations() : host.locations(); |
| 809 | } |
| 810 | |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 811 | } |