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