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