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 | |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 19 | import org.onlab.packet.EthType; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 24 | import org.onlab.util.PredictableExecutor; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 25 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Host; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 28 | import org.onosproject.net.HostLocation; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 29 | import org.onosproject.net.PortNumber; |
Charles Chan | 1eaf480 | 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 | c6bcdf9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 32 | import org.onosproject.net.host.ProbeMode; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 36 | import com.google.common.collect.Sets; |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 37 | |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 38 | import java.util.HashSet; |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 39 | import java.util.Optional; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 40 | import java.util.Set; |
Charles Chan | d9265a3 | 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 | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 44 | import static org.onlab.util.Tools.groupedThreads; |
Charles Chan | 1eaf480 | 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 | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 51 | |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 52 | protected final SegmentRoutingManager srManager; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 53 | private HostService hostService; |
pier | cf55792 | 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 | 1eaf480 | 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 | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 63 | HostHandler(SegmentRoutingManager srManager) { |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 64 | this.srManager = srManager; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 65 | hostService = srManager.hostService; |
pier | cf55792 | 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 | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 77 | protected void init(DeviceId devId) { |
pier | cf55792 | 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 | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 81 | ); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 82 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 83 | |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 84 | private void initHost(Host host, DeviceId deviceId) { |
| 85 | host.locations().forEach(location -> { |
| 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 | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 93 | void processHostAddedEvent(HostEvent event) { |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 94 | Host host = event.subject(); |
| 95 | hostWorkers.execute(() -> processHostAdded(host), host.id().hashCode()); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 98 | private void processHostAdded(Host host) { |
| 99 | host.locations().forEach(location -> processHostAddedAtLocation(host, location)); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 100 | // ensure dual-homed host locations have viable uplinks |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 101 | if (host.locations().size() > 1 || srManager.singleHomedDown) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 102 | host.locations().forEach(loc -> { |
| 103 | if (srManager.mastershipService.isLocalMaster(loc.deviceId())) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 104 | srManager.linkHandler.checkUplinksForHost(loc); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 105 | } |
| 106 | }); |
| 107 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 110 | void processHostAddedAtLocation(Host host, HostLocation location) { |
| 111 | checkArgument(host.locations().contains(location), "{} is not a location of {}", location, host); |
| 112 | |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 113 | MacAddress hostMac = host.mac(); |
| 114 | VlanId hostVlanId = host.vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 115 | Set<HostLocation> locations = host.locations(); |
| 116 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 117 | log.info("Host {}/{} is added at {}", hostMac, hostVlanId, locations); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 118 | |
Jonghwan Hyun | 9aaa34f | 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 | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 127 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false) |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 128 | ); |
| 129 | } |
Charles Chan | ceb2a2e | 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 | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 135 | if (host.locations().stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 136 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> { |
Charles Chan | ceb2a2e | 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 | 868c957 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 139 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 140 | if (vlanId == null) { |
| 141 | return; |
| 142 | } |
Charles Chan | ceb2a2e | 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 | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 147 | |
| 148 | if (srManager.activeProbing) { |
| 149 | probe(host, location, pairDeviceId, pairRemotePort); |
| 150 | } |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 151 | }); |
| 152 | } |
| 153 | }); |
Ruchi Sahota | 0786932 | 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 | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void processHostRemovedEvent(HostEvent event) { |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 166 | Host host = event.subject(); |
| 167 | hostWorkers.execute(() -> processHostRemoved(host), host.id().hashCode()); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 170 | private void processHostRemoved(Host host) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 171 | MacAddress hostMac = host.mac(); |
| 172 | VlanId hostVlanId = host.vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 173 | Set<HostLocation> locations = host.locations(); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 174 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 175 | log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 176 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 177 | locations.forEach(location -> { |
Jonghwan Hyun | 9aaa34f | 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 | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 186 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true) |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 187 | ); |
| 188 | } |
Charles Chan | 3ed34d8 | 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 | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 192 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId()); |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 193 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) { |
Charles Chan | 3ed34d8 | 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 | 868c957 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 196 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 197 | if (vlanId == null) { |
| 198 | return; |
| 199 | } |
Charles Chan | 3ed34d8 | 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 | bd84dd5 | 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 | 0786932 | 2019-05-09 17:26:14 -0400 | [diff] [blame] | 216 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 217 | }); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 220 | void processHostMovedEvent(HostEvent event) { |
Charles Chan | 4b5769a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 221 | Host host = event.subject(); |
pier | cf55792 | 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) { |
| 226 | Host host = event.subject(); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 227 | MacAddress hostMac = host.mac(); |
| 228 | VlanId hostVlanId = host.vlan(); |
Charles Chan | 4b5769a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 229 | Set<HostLocation> prevLocations = event.prevSubject().locations(); |
| 230 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
| 231 | Set<HostLocation> newLocations = host.locations(); |
| 232 | Set<IpAddress> newIps = host.ipAddresses(); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 233 | EthType hostTpid = host.tpid(); |
| 234 | boolean doubleTaggedHost = isDoubleTaggedHost(host); |
Charles Chan | 4b5769a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 235 | |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 236 | log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 237 | Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId) |
| 238 | .collect(Collectors.toSet()); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 239 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 240 | // For each old location |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 241 | Sets.difference(prevLocations, newLocations).forEach(prevLocation -> { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 242 | // Remove routing rules for old IPs |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 243 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 244 | if (doubleTaggedHost) { |
| 245 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 246 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 247 | } else { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 248 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 249 | ip, true); |
| 250 | } |
| 251 | }); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 252 | |
| 253 | // Redirect the flows to pair link if configured |
| 254 | // Note: Do not continue removing any rule |
| 255 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId()); |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 256 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(prevLocation.deviceId()); |
Charles Chan | 6d43c16 | 2018-10-11 12:35:36 -0700 | [diff] [blame] | 257 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && |
| 258 | newLocations.stream().anyMatch(location -> location.deviceId().equals(pairDeviceId.get())) && |
| 259 | newLocations.stream().noneMatch(location -> location.deviceId().equals(prevLocation.deviceId()))) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 260 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 261 | // when the host is untagged |
| 262 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId); |
| 263 | |
| 264 | processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false); |
| 265 | newIps.forEach(ip -> |
| 266 | processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, |
| 267 | ip, false)); |
| 268 | return; |
| 269 | } |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 270 | |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 271 | // 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] | 272 | // Otherwise, do not remove and let the adding part update the old flow |
| 273 | if (!newDeviceIds.contains(prevLocation.deviceId())) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 274 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 275 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 276 | if (doubleTaggedHost) { |
| 277 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 278 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 279 | } else { |
| 280 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 281 | hostMac, hostVlanId, ip, true); |
| 282 | } |
| 283 | }); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // Remove bridging rules if new interface vlan is different from old interface vlan |
| 287 | // Otherwise, do not remove and let the adding part update the old flow |
| 288 | if (newLocations.stream().noneMatch(newLocation -> { |
| 289 | VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation); |
| 290 | VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation); |
| 291 | // 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] | 292 | return srManager.interfaceService.getTaggedVlanId(newLocation).contains(hostVlanId) || |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 293 | (oldAssignedVlan != null && newAssignedVlan != null && |
| 294 | // Host is untagged and the new location has the same assigned vlan |
| 295 | oldAssignedVlan.equals(newAssignedVlan)); |
| 296 | })) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 297 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Remove routing rules for unchanged IPs if none of the subnet of new location contains |
| 301 | // the IP. Otherwise, do not remove and let the adding part update the old flow |
| 302 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 303 | if (newLocations.stream().noneMatch(newLocation -> |
| 304 | srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) { |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 305 | if (doubleTaggedHost) { |
| 306 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 307 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 308 | } else { |
| 309 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 310 | hostMac, hostVlanId, ip, true); |
| 311 | } |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 312 | } |
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 | }); |
| 315 | |
| 316 | // For each new location, add all new IPs. |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 317 | Sets.difference(newLocations, prevLocations).forEach(newLocation -> { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 318 | processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 319 | newIps.forEach(ip -> { |
| 320 | if (doubleTaggedHost) { |
| 321 | processDoubleTaggedRoutingRule(newLocation.deviceId(), newLocation.port(), |
| 322 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 323 | } else { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 324 | processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 325 | ip, false); |
| 326 | } |
| 327 | }); |
Charles Chan | 4b5769a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 328 | |
| 329 | // Probe on pair device when host move |
| 330 | // Majorly for the 2nd step of [1A/x, 1B/x] -> [1A/x, 1B/y] -> [1A/y, 1B/y] |
| 331 | // But will also cover [1A/x] -> [1A/y] -> [1A/y, 1B/y] |
| 332 | if (srManager.activeProbing) { |
Charles Chan | 6e90fbb | 2018-07-23 15:27:34 -0700 | [diff] [blame] | 333 | |
Charles Chan | 4b5769a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 334 | srManager.getPairDeviceId(newLocation.deviceId()).ifPresent(pairDeviceId -> |
| 335 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> |
| 336 | probe(host, newLocation, pairDeviceId, pairRemotePort) |
| 337 | ) |
| 338 | ); |
| 339 | } |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 340 | }); |
| 341 | |
| 342 | // For each unchanged location, add new IPs and remove old IPs. |
Charles Chan | fbcb881 | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 343 | Sets.intersection(newLocations, prevLocations).forEach(unchangedLocation -> { |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 344 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 345 | if (doubleTaggedHost) { |
| 346 | processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 347 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 348 | } else { |
| 349 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 350 | hostMac, hostVlanId, ip, true); |
| 351 | } |
| 352 | }); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 353 | |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 354 | Sets.difference(newIps, prevIps).forEach(ip -> { |
| 355 | if (doubleTaggedHost) { |
| 356 | processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 357 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 358 | } else { |
| 359 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 360 | hostMac, hostVlanId, ip, false); |
| 361 | } |
| 362 | }); |
Charles Chan | 6e90fbb | 2018-07-23 15:27:34 -0700 | [diff] [blame] | 363 | |
| 364 | // Verify existing location and see if it is still valid |
| 365 | srManager.probingService.probeHost(host, unchangedLocation, ProbeMode.VERIFY); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 366 | }); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 367 | |
| 368 | // ensure dual-homed host locations have viable uplinks |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 369 | if (newLocations.size() > prevLocations.size() || srManager.singleHomedDown) { |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 370 | newLocations.forEach(loc -> { |
| 371 | if (srManager.mastershipService.isLocalMaster(loc.deviceId())) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 372 | srManager.linkHandler.checkUplinksForHost(loc); |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 373 | } |
| 374 | }); |
| 375 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 378 | void processHostUpdatedEvent(HostEvent event) { |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 379 | Host host = event.subject(); |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 380 | hostWorkers.execute(() -> processHostUpdatedEventInternal(event), host.id().hashCode()); |
| 381 | } |
| 382 | |
| 383 | private void processHostUpdatedEventInternal(HostEvent event) { |
| 384 | Host host = event.subject(); |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 385 | MacAddress hostMac = host.mac(); |
| 386 | VlanId hostVlanId = host.vlan(); |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 387 | EthType hostTpid = host.tpid(); |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 388 | Set<HostLocation> locations = host.locations(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 389 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 390 | Set<IpAddress> newIps = host.ipAddresses(); |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 391 | log.info("Host {}/{} is updated", hostMac, hostVlanId); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 392 | |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 393 | locations.forEach(location -> { |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 394 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 395 | if (isDoubleTaggedHost(host)) { |
| 396 | processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac, |
| 397 | host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 398 | } else { |
| 399 | processRoutingRule(location.deviceId(), location.port(), hostMac, |
| 400 | hostVlanId, ip, true); |
| 401 | } |
| 402 | }); |
| 403 | Sets.difference(newIps, prevIps).forEach(ip -> { |
| 404 | if (isDoubleTaggedHost(host)) { |
| 405 | processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac, |
| 406 | host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 407 | } else { |
| 408 | processRoutingRule(location.deviceId(), location.port(), hostMac, |
| 409 | hostVlanId, ip, false); |
| 410 | } |
| 411 | }); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 412 | }); |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 413 | |
| 414 | // Use the pair link temporarily before the second location of a dual-homed host shows up. |
| 415 | // This do not affect single-homed hosts since the flow will be blocked in |
| 416 | // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 417 | locations.forEach(location -> |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 418 | srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 419 | if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 420 | Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps); |
| 421 | Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps); |
| 422 | |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 423 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> { |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 424 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 425 | // when the host is untagged |
Charles Chan | 868c957 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 426 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 427 | if (vlanId == null) { |
| 428 | return; |
| 429 | } |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 430 | |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 431 | ipsToRemove.forEach(ip -> |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 432 | processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true) |
| 433 | ); |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 434 | ipsToAdd.forEach(ip -> |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 435 | processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false) |
| 436 | ); |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 437 | |
| 438 | if (srManager.activeProbing) { |
| 439 | probe(host, location, pairDeviceId, pairRemotePort); |
| 440 | } |
Charles Chan | b3c1faf | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 441 | }); |
| 442 | } |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 443 | }) |
| 444 | ); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * When a non-pair port comes up, probe each host on the pair device if |
| 449 | * (1) the host is tagged and the tagged vlan of current port contains host vlan; or |
| 450 | * (2) the host is untagged and the internal vlan is the same on the host port and current port. |
| 451 | * |
| 452 | * @param cp connect point |
| 453 | */ |
| 454 | void processPortUp(ConnectPoint cp) { |
Saurav Das | ec683dc | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 455 | if (cp.port().equals(srManager.getPairLocalPort(cp.deviceId()).orElse(null))) { |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 456 | return; |
| 457 | } |
| 458 | if (srManager.activeProbing) { |
| 459 | srManager.getPairDeviceId(cp.deviceId()) |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 460 | .ifPresent(pairDeviceId -> srManager.hostService.getConnectedHosts(pairDeviceId).forEach( |
| 461 | host -> hostWorkers.execute(() -> probingIfNecessary(host, pairDeviceId, cp), |
| 462 | host.id().hashCode()))); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | private void probingIfNecessary(Host host, DeviceId pairDeviceId, ConnectPoint cp) { |
| 467 | if (isHostInVlanOfPort(host, pairDeviceId, cp)) { |
| 468 | srManager.probingService.probeHost(host, cp, ProbeMode.DISCOVER); |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Checks if given host located on given device id matches VLAN config of current port. |
| 474 | * |
| 475 | * @param host host to check |
| 476 | * @param deviceId device id to check |
| 477 | * @param cp current connect point |
| 478 | * @return true if the host located at deviceId matches the VLAN config on cp |
| 479 | */ |
| 480 | private boolean isHostInVlanOfPort(Host host, DeviceId deviceId, ConnectPoint cp) { |
| 481 | VlanId internalVlan = srManager.getInternalVlanId(cp); |
Charles Chan | 098ca20 | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 482 | Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(cp); |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 483 | |
| 484 | return taggedVlan.contains(host.vlan()) || |
| 485 | (internalVlan != null && host.locations().stream() |
| 486 | .filter(l -> l.deviceId().equals(deviceId)) |
| 487 | .map(srManager::getInternalVlanId) |
| 488 | .anyMatch(internalVlan::equals)); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Send a probe on all locations with the same VLAN on pair device, excluding pair port. |
| 493 | * |
| 494 | * @param host host to probe |
| 495 | * @param location newly discovered host location |
| 496 | * @param pairDeviceId pair device id |
| 497 | * @param pairRemotePort pair remote port |
| 498 | */ |
| 499 | private void probe(Host host, ConnectPoint location, DeviceId pairDeviceId, PortNumber pairRemotePort) { |
Mayank Tiwari | f794240 | 2018-10-29 18:27:35 -0400 | [diff] [blame] | 500 | //Check if the host still exists in the host store |
| 501 | if (hostService.getHost(host.id()) == null) { |
| 502 | log.debug("Host entry for host {} no more present. Aborting hostprobe discover for this host", host.id()); |
| 503 | return; |
| 504 | } |
| 505 | |
Charles Chan | 873661e | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 506 | VlanId vlanToProbe = host.vlan().equals(VlanId.NONE) ? |
| 507 | srManager.getInternalVlanId(location) : host.vlan(); |
Mayank Tiwari | f794240 | 2018-10-29 18:27:35 -0400 | [diff] [blame] | 508 | if (srManager.symmetricProbing) { |
| 509 | srManager.interfaceService.getInterfaces().stream() |
| 510 | .filter(i -> i.vlanTagged().contains(vlanToProbe) || |
| 511 | i.vlanUntagged().equals(vlanToProbe) || |
| 512 | i.vlanNative().equals(vlanToProbe)) |
| 513 | .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId)) |
| 514 | .filter(i -> i.connectPoint().port().equals(location.port())) |
| 515 | .forEach(i -> { |
| 516 | log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint()); |
| 517 | srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER); |
| 518 | }); |
| 519 | } else { |
| 520 | srManager.interfaceService.getInterfaces().stream() |
| 521 | .filter(i -> i.vlanTagged().contains(vlanToProbe) || |
| 522 | i.vlanUntagged().equals(vlanToProbe) || |
| 523 | i.vlanNative().equals(vlanToProbe)) |
| 524 | .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId)) |
| 525 | .filter(i -> !i.connectPoint().port().equals(pairRemotePort)) |
| 526 | .forEach(i -> { |
| 527 | log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint()); |
| 528 | srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER); |
| 529 | }); |
| 530 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /** |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 534 | * Populates or revokes a bridging rule on given deviceId that matches given mac, |
| 535 | * given vlan and output to given port. |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 536 | * |
| 537 | * @param deviceId device ID |
| 538 | * @param port port |
| 539 | * @param mac mac address |
| 540 | * @param vlanId VLAN ID |
| 541 | * @param revoke true to revoke the rule; false to populate |
| 542 | */ |
| 543 | private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 544 | VlanId vlanId, boolean revoke) { |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 545 | log.info("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating", |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 546 | mac, vlanId, deviceId, port); |
| 547 | |
Charles Chan | d66d671 | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 548 | if (!revoke) { |
| 549 | srManager.defaultRoutingHandler.populateBridging(deviceId, port, mac, vlanId); |
| 550 | } else { |
| 551 | srManager.defaultRoutingHandler.revokeBridging(deviceId, port, mac, vlanId); |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 552 | } |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Populate or revoke a routing rule on given deviceId that matches given ip, |
| 557 | * set destination mac to given mac, set vlan to given vlan and output to given port. |
| 558 | * |
| 559 | * @param deviceId device ID |
| 560 | * @param port port |
| 561 | * @param mac mac address |
| 562 | * @param vlanId VLAN ID |
| 563 | * @param ip IP address |
| 564 | * @param revoke true to revoke the rule; false to populate |
| 565 | */ |
| 566 | private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 567 | VlanId vlanId, IpAddress ip, boolean revoke) { |
| 568 | ConnectPoint location = new ConnectPoint(deviceId, port); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 569 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 570 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 571 | return; |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 572 | } |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 573 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 574 | log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location); |
| 575 | if (revoke) { |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 576 | srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port, true); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 577 | } else { |
Ruchi Sahota | 71bcb4e | 2019-01-28 01:08:18 +0000 | [diff] [blame] | 578 | srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port, true); |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 579 | } |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 580 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 581 | |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 582 | /** |
| 583 | * Populate or revoke a routing rule and egress rules on given deviceId that matches given IP, |
| 584 | * to set destination mac to given mac, set inner vlan and outer vlan to given vlans, |
| 585 | * set outer TPID, and output to given port. |
| 586 | * |
| 587 | * @param deviceId device ID |
| 588 | * @param port port |
| 589 | * @param mac mac address |
| 590 | * @param innerVlan inner VLAN ID |
| 591 | * @param outerVlan outer VLAN ID |
| 592 | * @param outerTpid outer TPID |
| 593 | * @param ip IP address |
| 594 | * @param revoke true to revoke the rule; false to populate |
| 595 | */ |
| 596 | private void processDoubleTaggedRoutingRule(DeviceId deviceId, PortNumber port, |
| 597 | MacAddress mac, VlanId innerVlan, |
| 598 | VlanId outerVlan, EthType outerTpid, |
| 599 | IpAddress ip, boolean revoke) { |
Charles Chan | fbca55e | 2018-07-24 16:40:35 -0700 | [diff] [blame] | 600 | if (!srManager.routeDoubleTaggedHosts) { |
| 601 | 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] | 602 | return; |
| 603 | } |
| 604 | |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 605 | ConnectPoint location = new ConnectPoint(deviceId, port); |
| 606 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 607 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 608 | return; |
| 609 | } |
| 610 | log.info("{} routing rule for double-tagged host {} at {}", |
| 611 | revoke ? "Revoking" : "Populating", ip, location); |
| 612 | if (revoke) { |
| 613 | srManager.defaultRoutingHandler.revokeDoubleTaggedRoute( |
| 614 | deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port); |
| 615 | } else { |
| 616 | srManager.defaultRoutingHandler.populateDoubleTaggedRoute( |
| 617 | deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port); |
| 618 | } |
| 619 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 620 | |
Charles Chan | 4eb73bb | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 621 | void populateAllDoubleTaggedHost() { |
Charles Chan | fbca55e | 2018-07-24 16:40:35 -0700 | [diff] [blame] | 622 | log.info("Enabling routing for all double tagged hosts"); |
Charles Chan | 4eb73bb | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 623 | Sets.newHashSet(srManager.hostService.getHosts()).stream().filter(this::isDoubleTaggedHost) |
| 624 | .forEach(h -> h.locations().forEach(l -> |
| 625 | h.ipAddresses().forEach(i -> |
| 626 | processDoubleTaggedRoutingRule(l.deviceId(), l.port(), h.mac(), h.innerVlan(), |
| 627 | h.vlan(), h.tpid(), i, false) |
| 628 | ) |
| 629 | ) |
| 630 | ); |
| 631 | } |
| 632 | |
| 633 | void revokeAllDoubleTaggedHost() { |
Charles Chan | fbca55e | 2018-07-24 16:40:35 -0700 | [diff] [blame] | 634 | log.info("Disabling routing for all double tagged hosts"); |
Charles Chan | 4eb73bb | 2018-07-19 14:55:31 -0700 | [diff] [blame] | 635 | Sets.newHashSet(srManager.hostService.getHosts()).stream().filter(this::isDoubleTaggedHost) |
| 636 | .forEach(h -> h.locations().forEach(l -> |
| 637 | h.ipAddresses().forEach(i -> |
| 638 | processDoubleTaggedRoutingRule(l.deviceId(), l.port(), h.mac(), h.innerVlan(), |
| 639 | h.vlan(), h.tpid(), i, true) |
| 640 | ) |
| 641 | ) |
| 642 | ); |
| 643 | } |
| 644 | |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 645 | /** |
Charles Chan | 868c957 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 646 | * Returns VLAN ID to be used to program redirection flow on pair port. |
| 647 | * |
| 648 | * @param hostVlanId host VLAN ID |
| 649 | * @param location host location |
| 650 | * @return VLAN ID to be used; Or null if host VLAN does not match the interface config |
| 651 | */ |
| 652 | VlanId vlanForPairPort(VlanId hostVlanId, ConnectPoint location) { |
| 653 | VlanId internalVlan = srManager.getInternalVlanId(location); |
| 654 | Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(location); |
| 655 | |
| 656 | if (!hostVlanId.equals(VlanId.NONE) && taggedVlan.contains(hostVlanId)) { |
| 657 | return hostVlanId; |
| 658 | } else if (hostVlanId.equals(VlanId.NONE) && internalVlan != null) { |
| 659 | return internalVlan; |
| 660 | } else { |
| 661 | log.warn("VLAN mismatch. hostVlan={}, location={}, internalVlan={}, taggedVlan={}", |
| 662 | hostVlanId, location, internalVlan, taggedVlan); |
| 663 | return null; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | /** |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 668 | * Update forwarding objective for unicast bridging and unicast routing. |
| 669 | * Also check the validity of updated interface configuration on VLAN. |
| 670 | * |
| 671 | * @param deviceId device ID that host attaches to |
| 672 | * @param portNum port number that host attaches to |
| 673 | * @param vlanId Vlan ID configured on the switch port |
| 674 | * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise |
| 675 | * @param install true to populate the objective, false to revoke |
| 676 | */ |
| 677 | void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId, |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 678 | boolean popVlan, boolean install) { |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 679 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum); |
| 680 | Set<Host> hosts = hostService.getConnectedHosts(connectPoint); |
| 681 | |
| 682 | if (hosts == null || hosts.size() == 0) { |
Charles Chan | 39b7552 | 2018-04-21 00:44:29 -0700 | [diff] [blame] | 683 | log.debug("processIntfVlanUpdatedEvent: No hosts connected to {}", connectPoint); |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 687 | hosts.forEach(host -> hostWorkers.execute(() -> processIntfVlanUpdatedEventInternal( |
| 688 | host, deviceId, portNum, vlanId, popVlan, install), host.id().hashCode())); |
| 689 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 690 | |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 691 | private void processIntfVlanUpdatedEventInternal(Host host, DeviceId deviceId, PortNumber portNum, |
| 692 | VlanId vlanId, boolean popVlan, boolean install) { |
| 693 | MacAddress mac = host.mac(); |
| 694 | VlanId hostVlanId = host.vlan(); |
| 695 | |
| 696 | // Check whether the host vlan is valid for new interface configuration |
| 697 | if ((!popVlan && hostVlanId.equals(vlanId)) || |
| 698 | (popVlan && hostVlanId.equals(VlanId.NONE))) { |
| 699 | srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, install); |
| 700 | // Update Forwarding objective and corresponding simple Next objective |
| 701 | // for each host and IP address connected to given port |
| 702 | host.ipAddresses().forEach(ipAddress -> srManager.defaultRoutingHandler.updateFwdObj( |
| 703 | deviceId, portNum, ipAddress.toIpPrefix(), mac, vlanId, popVlan, install) |
| 704 | ); |
| 705 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | /** |
| 709 | * Populate or revoke routing rule for each host, according to the updated |
| 710 | * subnet configuration on the interface. |
| 711 | * @param cp connect point of the updated interface |
| 712 | * @param ipPrefixSet IP Prefixes added or removed |
| 713 | * @param install true if IP Prefixes added, false otherwise |
| 714 | */ |
| 715 | void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) { |
| 716 | Set<Host> hosts = hostService.getConnectedHosts(cp); |
| 717 | |
| 718 | if (hosts == null || hosts.size() == 0) { |
Charles Chan | 39b7552 | 2018-04-21 00:44:29 -0700 | [diff] [blame] | 719 | log.debug("processIntfIpUpdatedEvent: No hosts connected to {}", cp); |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | |
| 723 | // Check whether the host IP address is in the interface's subnet |
pier | cf55792 | 2019-05-17 20:47:06 +0200 | [diff] [blame] | 724 | hosts.forEach(host -> hostWorkers.execute(() -> processIntfIpUpdatedEventInternal( |
| 725 | host, cp, ipPrefixSet, install))); |
| 726 | } |
| 727 | |
| 728 | private void processIntfIpUpdatedEventInternal(Host host, ConnectPoint cp, Set<IpPrefix> ipPrefixSet, |
| 729 | boolean install) { |
| 730 | host.ipAddresses().forEach(hostIpAddress -> { |
| 731 | ipPrefixSet.forEach(ipPrefix -> { |
| 732 | if (install && ipPrefix.contains(hostIpAddress)) { |
| 733 | srManager.defaultRoutingHandler.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
| 734 | host.mac(), host.vlan(), cp.port(), true); |
| 735 | } else if (!install && ipPrefix.contains(hostIpAddress)) { |
| 736 | srManager.defaultRoutingHandler.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
| 737 | host.mac(), host.vlan(), cp.port(), true); |
| 738 | } |
| 739 | }); |
| 740 | }); |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 741 | } |
Saurav Das | e6c448a | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 742 | |
| 743 | /** |
| 744 | * Returns the set of portnumbers on the given device that are part of the |
| 745 | * locations for dual-homed hosts. |
| 746 | * |
| 747 | * @param deviceId the given deviceId |
| 748 | * @return set of port numbers on given device that are dual-homed host |
| 749 | * locations. May be empty if no dual homed hosts are connected to |
| 750 | * the given device |
| 751 | */ |
| 752 | Set<PortNumber> getDualHomedHostPorts(DeviceId deviceId) { |
| 753 | Set<PortNumber> dualHomedLocations = new HashSet<>(); |
| 754 | srManager.hostService.getConnectedHosts(deviceId).stream() |
| 755 | .filter(host -> host.locations().size() == 2) |
| 756 | .forEach(host -> host.locations().stream() |
| 757 | .filter(loc -> loc.deviceId().equals(deviceId)) |
| 758 | .forEach(loc -> dualHomedLocations.add(loc.port()))); |
| 759 | return dualHomedLocations; |
| 760 | } |
Jonghwan Hyun | 9aaa34f | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 761 | |
| 762 | /** |
| 763 | * Checks if the given host is double-tagged or not. |
| 764 | * |
| 765 | * @param host host to check |
| 766 | * @return true if it is double-tagged, false otherwise |
| 767 | */ |
| 768 | private boolean isDoubleTaggedHost(Host host) { |
| 769 | return !host.innerVlan().equals(VlanId.NONE); |
| 770 | } |
| 771 | |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 772 | } |