Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 19 | import org.onlab.packet.EthType; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpAddress; |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
Charles Chan | 59cc16d | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 26 | import org.onosproject.net.Host; |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 27 | import org.onosproject.net.HostLocation; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 28 | import org.onosproject.net.PortNumber; |
Charles Chan | d299036 | 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 | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 31 | import org.onosproject.net.host.ProbeMode; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 35 | import com.google.common.collect.Sets; |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 36 | |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 37 | import java.util.HashSet; |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 38 | import java.util.Optional; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 39 | import java.util.Set; |
Charles Chan | f9a5270 | 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 | d299036 | 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 | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 49 | |
Charles Chan | 2e2e340 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 50 | protected final SegmentRoutingManager srManager; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 51 | private HostService hostService; |
Charles Chan | d299036 | 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 | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 58 | HostHandler(SegmentRoutingManager srManager) { |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 59 | this.srManager = srManager; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 60 | hostService = srManager.hostService; |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Charles Chan | 03a73e0 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 63 | protected void init(DeviceId devId) { |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 64 | hostService.getHosts().forEach(host -> |
| 65 | host.locations().stream() |
Charles Chan | 4793375 | 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 | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 68 | .forEach(location -> processHostAddedAtLocation(host, location)) |
| 69 | ); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 70 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 71 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 72 | void processHostAddedEvent(HostEvent event) { |
Charles Chan | 35fd1a7 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 73 | processHostAdded(event.subject()); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Charles Chan | f9a5270 | 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 | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 78 | // ensure dual-homed host locations have viable uplinks |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 79 | if (host.locations().size() > 1 || srManager.singleHomedDown) { |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 80 | host.locations().forEach(loc -> { |
| 81 | if (srManager.mastershipService.isLocalMaster(loc.deviceId())) { |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 82 | srManager.linkHandler.checkUplinksForHost(loc); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 83 | } |
| 84 | }); |
| 85 | } |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Charles Chan | f9a5270 | 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 | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 91 | MacAddress hostMac = host.mac(); |
| 92 | VlanId hostVlanId = host.vlan(); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 93 | Set<HostLocation> locations = host.locations(); |
| 94 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 95 | log.info("Host {}/{} is added at {}", hostMac, hostVlanId, locations); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 96 | |
Jonghwan Hyun | 800d9d0 | 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 | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 105 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false) |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 106 | ); |
| 107 | } |
Charles Chan | 8e786b5 | 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 | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 113 | if (host.locations().stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 114 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> { |
Charles Chan | 8e786b5 | 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 | f76de30 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 117 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 118 | if (vlanId == null) { |
| 119 | return; |
| 120 | } |
Charles Chan | 8e786b5 | 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 | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 125 | |
| 126 | if (srManager.activeProbing) { |
| 127 | probe(host, location, pairDeviceId, pairRemotePort); |
| 128 | } |
Charles Chan | 8e786b5 | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 129 | }); |
| 130 | } |
| 131 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void processHostRemovedEvent(HostEvent event) { |
Charles Chan | 35fd1a7 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 135 | processHostRemoved(event.subject()); |
| 136 | } |
| 137 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 138 | private void processHostRemoved(Host host) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 139 | MacAddress hostMac = host.mac(); |
| 140 | VlanId hostVlanId = host.vlan(); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 141 | Set<HostLocation> locations = host.locations(); |
Charles Chan | 35fd1a7 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 142 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 143 | log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 144 | |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 145 | locations.forEach(location -> { |
Jonghwan Hyun | 800d9d0 | 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 | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 154 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true) |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 155 | ); |
| 156 | } |
Charles Chan | 6523824 | 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 | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 160 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(location.deviceId()); |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 161 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent()) { |
Charles Chan | 6523824 | 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 | f76de30 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 164 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 165 | if (vlanId == null) { |
| 166 | return; |
| 167 | } |
Charles Chan | 6523824 | 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 | 4d63939 | 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 | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 184 | }); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 187 | void processHostMovedEvent(HostEvent event) { |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 188 | Host host = event.subject(); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 189 | MacAddress hostMac = host.mac(); |
| 190 | VlanId hostVlanId = host.vlan(); |
Charles Chan | 9bd0e5a | 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 | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 195 | EthType hostTpid = host.tpid(); |
| 196 | boolean doubleTaggedHost = isDoubleTaggedHost(host); |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 197 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 198 | log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 199 | Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId) |
| 200 | .collect(Collectors.toSet()); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 201 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 202 | // For each old location |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 203 | Sets.difference(prevLocations, newLocations).forEach(prevLocation -> { |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 204 | // First of all, verify each old location |
| 205 | srManager.probingService.probeHost(host, prevLocation, ProbeMode.VERIFY); |
| 206 | |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 207 | // Remove routing rules for old IPs |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 208 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 209 | if (doubleTaggedHost) { |
| 210 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 211 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 212 | } else { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 213 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 214 | ip, true); |
| 215 | } |
| 216 | }); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 217 | |
| 218 | // Redirect the flows to pair link if configured |
| 219 | // Note: Do not continue removing any rule |
| 220 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId()); |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 221 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPort(prevLocation.deviceId()); |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 222 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream() |
| 223 | .anyMatch(location -> location.deviceId().equals(pairDeviceId.get()))) { |
| 224 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 225 | // when the host is untagged |
| 226 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId); |
| 227 | |
| 228 | processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false); |
| 229 | newIps.forEach(ip -> |
| 230 | processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, |
| 231 | ip, false)); |
| 232 | return; |
| 233 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 234 | |
Charles Chan | 50bb6ef | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 235 | // Remove flows for unchanged IPs only when the host moves from a switch to another. |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 236 | // Otherwise, do not remove and let the adding part update the old flow |
| 237 | if (!newDeviceIds.contains(prevLocation.deviceId())) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 238 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 239 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 240 | if (doubleTaggedHost) { |
| 241 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 242 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 243 | } else { |
| 244 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 245 | hostMac, hostVlanId, ip, true); |
| 246 | } |
| 247 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | // Remove bridging rules if new interface vlan is different from old interface vlan |
| 251 | // Otherwise, do not remove and let the adding part update the old flow |
| 252 | if (newLocations.stream().noneMatch(newLocation -> { |
| 253 | VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation); |
| 254 | VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation); |
| 255 | // Host is tagged and the new location has the host vlan in vlan-tagged |
Charles Chan | 971d7ba | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 256 | return srManager.interfaceService.getTaggedVlanId(newLocation).contains(hostVlanId) || |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 257 | (oldAssignedVlan != null && newAssignedVlan != null && |
| 258 | // Host is untagged and the new location has the same assigned vlan |
| 259 | oldAssignedVlan.equals(newAssignedVlan)); |
| 260 | })) { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 261 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // Remove routing rules for unchanged IPs if none of the subnet of new location contains |
| 265 | // the IP. Otherwise, do not remove and let the adding part update the old flow |
| 266 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 267 | if (newLocations.stream().noneMatch(newLocation -> |
| 268 | srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) { |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 269 | if (doubleTaggedHost) { |
| 270 | processDoubleTaggedRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 271 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 272 | } else { |
| 273 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), |
| 274 | hostMac, hostVlanId, ip, true); |
| 275 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 276 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 277 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 278 | }); |
| 279 | |
| 280 | // For each new location, add all new IPs. |
Charles Chan | 50bb6ef | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 281 | Sets.difference(newLocations, prevLocations).forEach(newLocation -> { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 282 | processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 283 | newIps.forEach(ip -> { |
| 284 | if (doubleTaggedHost) { |
| 285 | processDoubleTaggedRoutingRule(newLocation.deviceId(), newLocation.port(), |
| 286 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 287 | } else { |
Charles Chan | 6523824 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 288 | processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 289 | ip, false); |
| 290 | } |
| 291 | }); |
Charles Chan | 9bd0e5a | 2018-04-25 18:51:46 -0400 | [diff] [blame] | 292 | |
| 293 | // Probe on pair device when host move |
| 294 | // Majorly for the 2nd step of [1A/x, 1B/x] -> [1A/x, 1B/y] -> [1A/y, 1B/y] |
| 295 | // But will also cover [1A/x] -> [1A/y] -> [1A/y, 1B/y] |
| 296 | if (srManager.activeProbing) { |
| 297 | srManager.getPairDeviceId(newLocation.deviceId()).ifPresent(pairDeviceId -> |
| 298 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> |
| 299 | probe(host, newLocation, pairDeviceId, pairRemotePort) |
| 300 | ) |
| 301 | ); |
| 302 | } |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 303 | }); |
| 304 | |
| 305 | // For each unchanged location, add new IPs and remove old IPs. |
Charles Chan | 50bb6ef | 2018-04-18 18:41:05 -0700 | [diff] [blame] | 306 | Sets.intersection(newLocations, prevLocations).forEach(unchangedLocation -> { |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 307 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 308 | if (doubleTaggedHost) { |
| 309 | processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 310 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 311 | } else { |
| 312 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 313 | hostMac, hostVlanId, ip, true); |
| 314 | } |
| 315 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 316 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 317 | Sets.difference(newIps, prevIps).forEach(ip -> { |
| 318 | if (doubleTaggedHost) { |
| 319 | processDoubleTaggedRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 320 | hostMac, host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 321 | } else { |
| 322 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), |
| 323 | hostMac, hostVlanId, ip, false); |
| 324 | } |
| 325 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 326 | }); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 327 | |
| 328 | // ensure dual-homed host locations have viable uplinks |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 329 | if (newLocations.size() > prevLocations.size() || srManager.singleHomedDown) { |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 330 | newLocations.forEach(loc -> { |
| 331 | if (srManager.mastershipService.isLocalMaster(loc.deviceId())) { |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 332 | srManager.linkHandler.checkUplinksForHost(loc); |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 333 | } |
| 334 | }); |
| 335 | } |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 338 | void processHostUpdatedEvent(HostEvent event) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 339 | Host host = event.subject(); |
| 340 | MacAddress hostMac = host.mac(); |
| 341 | VlanId hostVlanId = host.vlan(); |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 342 | EthType hostTpid = host.tpid(); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 343 | Set<HostLocation> locations = host.locations(); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 344 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 345 | Set<IpAddress> newIps = host.ipAddresses(); |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 346 | log.info("Host {}/{} is updated", hostMac, hostVlanId); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 347 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 348 | locations.forEach(location -> { |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 349 | Sets.difference(prevIps, newIps).forEach(ip -> { |
| 350 | if (isDoubleTaggedHost(host)) { |
| 351 | processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac, |
| 352 | host.innerVlan(), hostVlanId, hostTpid, ip, true); |
| 353 | } else { |
| 354 | processRoutingRule(location.deviceId(), location.port(), hostMac, |
| 355 | hostVlanId, ip, true); |
| 356 | } |
| 357 | }); |
| 358 | Sets.difference(newIps, prevIps).forEach(ip -> { |
| 359 | if (isDoubleTaggedHost(host)) { |
| 360 | processDoubleTaggedRoutingRule(location.deviceId(), location.port(), hostMac, |
| 361 | host.innerVlan(), hostVlanId, hostTpid, ip, false); |
| 362 | } else { |
| 363 | processRoutingRule(location.deviceId(), location.port(), hostMac, |
| 364 | hostVlanId, ip, false); |
| 365 | } |
| 366 | }); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 367 | }); |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 368 | |
| 369 | // Use the pair link temporarily before the second location of a dual-homed host shows up. |
| 370 | // This do not affect single-homed hosts since the flow will be blocked in |
| 371 | // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 372 | locations.forEach(location -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 373 | srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 374 | if (locations.stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 375 | Set<IpAddress> ipsToAdd = Sets.difference(newIps, prevIps); |
| 376 | Set<IpAddress> ipsToRemove = Sets.difference(prevIps, newIps); |
| 377 | |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 378 | srManager.getPairLocalPort(pairDeviceId).ifPresent(pairRemotePort -> { |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 379 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 380 | // when the host is untagged |
Charles Chan | f76de30 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 381 | VlanId vlanId = vlanForPairPort(hostVlanId, location); |
| 382 | if (vlanId == null) { |
| 383 | return; |
| 384 | } |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 385 | |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 386 | ipsToRemove.forEach(ip -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 387 | processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, true) |
| 388 | ); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 389 | ipsToAdd.forEach(ip -> |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 390 | processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, ip, false) |
| 391 | ); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 392 | |
| 393 | if (srManager.activeProbing) { |
| 394 | probe(host, location, pairDeviceId, pairRemotePort); |
| 395 | } |
Charles Chan | 3d650a6 | 2017-11-20 08:46:24 -0800 | [diff] [blame] | 396 | }); |
| 397 | } |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 398 | }) |
| 399 | ); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * When a non-pair port comes up, probe each host on the pair device if |
| 404 | * (1) the host is tagged and the tagged vlan of current port contains host vlan; or |
| 405 | * (2) the host is untagged and the internal vlan is the same on the host port and current port. |
| 406 | * |
| 407 | * @param cp connect point |
| 408 | */ |
| 409 | void processPortUp(ConnectPoint cp) { |
Saurav Das | 9a55429 | 2018-04-27 18:42:30 -0700 | [diff] [blame] | 410 | if (cp.port().equals(srManager.getPairLocalPort(cp.deviceId()).orElse(null))) { |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 411 | return; |
| 412 | } |
| 413 | if (srManager.activeProbing) { |
| 414 | srManager.getPairDeviceId(cp.deviceId()) |
| 415 | .ifPresent(pairDeviceId -> srManager.hostService.getConnectedHosts(pairDeviceId).stream() |
| 416 | .filter(host -> isHostInVlanOfPort(host, pairDeviceId, cp)) |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 417 | .forEach(host -> srManager.probingService.probeHost(host, cp, ProbeMode.DISCOVER)) |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 418 | ); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Checks if given host located on given device id matches VLAN config of current port. |
| 424 | * |
| 425 | * @param host host to check |
| 426 | * @param deviceId device id to check |
| 427 | * @param cp current connect point |
| 428 | * @return true if the host located at deviceId matches the VLAN config on cp |
| 429 | */ |
| 430 | private boolean isHostInVlanOfPort(Host host, DeviceId deviceId, ConnectPoint cp) { |
| 431 | VlanId internalVlan = srManager.getInternalVlanId(cp); |
Charles Chan | 971d7ba | 2018-05-01 11:50:20 -0700 | [diff] [blame] | 432 | Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(cp); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 433 | |
| 434 | return taggedVlan.contains(host.vlan()) || |
| 435 | (internalVlan != null && host.locations().stream() |
| 436 | .filter(l -> l.deviceId().equals(deviceId)) |
| 437 | .map(srManager::getInternalVlanId) |
| 438 | .anyMatch(internalVlan::equals)); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Send a probe on all locations with the same VLAN on pair device, excluding pair port. |
| 443 | * |
| 444 | * @param host host to probe |
| 445 | * @param location newly discovered host location |
| 446 | * @param pairDeviceId pair device id |
| 447 | * @param pairRemotePort pair remote port |
| 448 | */ |
| 449 | private void probe(Host host, ConnectPoint location, DeviceId pairDeviceId, PortNumber pairRemotePort) { |
| 450 | VlanId vlanToProbe = host.vlan().equals(VlanId.NONE) ? |
| 451 | srManager.getInternalVlanId(location) : host.vlan(); |
| 452 | srManager.interfaceService.getInterfaces().stream() |
| 453 | .filter(i -> i.vlanTagged().contains(vlanToProbe) || |
| 454 | i.vlanUntagged().equals(vlanToProbe) || |
| 455 | i.vlanNative().equals(vlanToProbe)) |
| 456 | .filter(i -> i.connectPoint().deviceId().equals(pairDeviceId)) |
| 457 | .filter(i -> !i.connectPoint().port().equals(pairRemotePort)) |
| 458 | .forEach(i -> { |
| 459 | log.debug("Probing host {} on pair device {}", host.id(), i.connectPoint()); |
Charles Chan | ff79dd9 | 2018-06-01 16:33:48 -0700 | [diff] [blame] | 460 | srManager.probingService.probeHost(host, i.connectPoint(), ProbeMode.DISCOVER); |
Charles Chan | 4793375 | 2017-11-30 15:37:50 -0800 | [diff] [blame] | 461 | }); |
Charles Chan | 93e71ba | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /** |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 465 | * Populates or revokes a bridging rule on given deviceId that matches given mac, |
| 466 | * given vlan and output to given port. |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 467 | * |
| 468 | * @param deviceId device ID |
| 469 | * @param port port |
| 470 | * @param mac mac address |
| 471 | * @param vlanId VLAN ID |
| 472 | * @param revoke true to revoke the rule; false to populate |
| 473 | */ |
| 474 | private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 475 | VlanId vlanId, boolean revoke) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 476 | log.info("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating", |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 477 | mac, vlanId, deviceId, port); |
| 478 | |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 479 | if (!revoke) { |
| 480 | srManager.defaultRoutingHandler.populateBridging(deviceId, port, mac, vlanId); |
| 481 | } else { |
| 482 | srManager.defaultRoutingHandler.revokeBridging(deviceId, port, mac, vlanId); |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 483 | } |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Populate or revoke a routing rule on given deviceId that matches given ip, |
| 488 | * set destination mac to given mac, set vlan to given vlan and output to given port. |
| 489 | * |
| 490 | * @param deviceId device ID |
| 491 | * @param port port |
| 492 | * @param mac mac address |
| 493 | * @param vlanId VLAN ID |
| 494 | * @param ip IP address |
| 495 | * @param revoke true to revoke the rule; false to populate |
| 496 | */ |
| 497 | private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 498 | VlanId vlanId, IpAddress ip, boolean revoke) { |
| 499 | ConnectPoint location = new ConnectPoint(deviceId, port); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 500 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 501 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 502 | return; |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 503 | } |
Charles Chan | b7b4c93 | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 504 | |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 505 | log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location); |
| 506 | if (revoke) { |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 507 | srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | f9a5270 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 508 | } else { |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 509 | srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 510 | } |
Charles Chan | 6ea94fc | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 511 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 512 | |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 513 | /** |
| 514 | * Populate or revoke a routing rule and egress rules on given deviceId that matches given IP, |
| 515 | * to set destination mac to given mac, set inner vlan and outer vlan to given vlans, |
| 516 | * set outer TPID, and output to given port. |
| 517 | * |
| 518 | * @param deviceId device ID |
| 519 | * @param port port |
| 520 | * @param mac mac address |
| 521 | * @param innerVlan inner VLAN ID |
| 522 | * @param outerVlan outer VLAN ID |
| 523 | * @param outerTpid outer TPID |
| 524 | * @param ip IP address |
| 525 | * @param revoke true to revoke the rule; false to populate |
| 526 | */ |
| 527 | private void processDoubleTaggedRoutingRule(DeviceId deviceId, PortNumber port, |
| 528 | MacAddress mac, VlanId innerVlan, |
| 529 | VlanId outerVlan, EthType outerTpid, |
| 530 | IpAddress ip, boolean revoke) { |
| 531 | ConnectPoint location = new ConnectPoint(deviceId, port); |
| 532 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 533 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 534 | return; |
| 535 | } |
| 536 | log.info("{} routing rule for double-tagged host {} at {}", |
| 537 | revoke ? "Revoking" : "Populating", ip, location); |
| 538 | if (revoke) { |
| 539 | srManager.defaultRoutingHandler.revokeDoubleTaggedRoute( |
| 540 | deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port); |
| 541 | } else { |
| 542 | srManager.defaultRoutingHandler.populateDoubleTaggedRoute( |
| 543 | deviceId, ip.toIpPrefix(), mac, innerVlan, outerVlan, outerTpid, port); |
| 544 | } |
| 545 | } |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 546 | |
| 547 | /** |
Charles Chan | f76de30 | 2018-06-15 18:54:18 -0700 | [diff] [blame] | 548 | * Returns VLAN ID to be used to program redirection flow on pair port. |
| 549 | * |
| 550 | * @param hostVlanId host VLAN ID |
| 551 | * @param location host location |
| 552 | * @return VLAN ID to be used; Or null if host VLAN does not match the interface config |
| 553 | */ |
| 554 | VlanId vlanForPairPort(VlanId hostVlanId, ConnectPoint location) { |
| 555 | VlanId internalVlan = srManager.getInternalVlanId(location); |
| 556 | Set<VlanId> taggedVlan = srManager.interfaceService.getTaggedVlanId(location); |
| 557 | |
| 558 | if (!hostVlanId.equals(VlanId.NONE) && taggedVlan.contains(hostVlanId)) { |
| 559 | return hostVlanId; |
| 560 | } else if (hostVlanId.equals(VlanId.NONE) && internalVlan != null) { |
| 561 | return internalVlan; |
| 562 | } else { |
| 563 | log.warn("VLAN mismatch. hostVlan={}, location={}, internalVlan={}, taggedVlan={}", |
| 564 | hostVlanId, location, internalVlan, taggedVlan); |
| 565 | return null; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 570 | * Update forwarding objective for unicast bridging and unicast routing. |
| 571 | * Also check the validity of updated interface configuration on VLAN. |
| 572 | * |
| 573 | * @param deviceId device ID that host attaches to |
| 574 | * @param portNum port number that host attaches to |
| 575 | * @param vlanId Vlan ID configured on the switch port |
| 576 | * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise |
| 577 | * @param install true to populate the objective, false to revoke |
| 578 | */ |
| 579 | void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId, |
| 580 | boolean popVlan, boolean install) { |
| 581 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum); |
| 582 | Set<Host> hosts = hostService.getConnectedHosts(connectPoint); |
| 583 | |
| 584 | if (hosts == null || hosts.size() == 0) { |
Charles Chan | 10b2fee | 2018-04-21 00:44:29 -0700 | [diff] [blame] | 585 | log.debug("processIntfVlanUpdatedEvent: No hosts connected to {}", connectPoint); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | |
| 589 | hosts.forEach(host -> { |
| 590 | MacAddress mac = host.mac(); |
| 591 | VlanId hostVlanId = host.vlan(); |
| 592 | |
| 593 | // Check whether the host vlan is valid for new interface configuration |
| 594 | if ((!popVlan && hostVlanId.equals(vlanId)) || |
| 595 | (popVlan && hostVlanId.equals(VlanId.NONE))) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 596 | srManager.defaultRoutingHandler.updateBridging(deviceId, portNum, mac, vlanId, popVlan, install); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 597 | // Update Forwarding objective and corresponding simple Next objective |
| 598 | // for each host and IP address connected to given port |
| 599 | host.ipAddresses().forEach(ipAddress -> |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 600 | srManager.defaultRoutingHandler.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(), |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 601 | mac, vlanId, popVlan, install) |
| 602 | ); |
| 603 | } |
| 604 | }); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Populate or revoke routing rule for each host, according to the updated |
| 609 | * subnet configuration on the interface. |
| 610 | * @param cp connect point of the updated interface |
| 611 | * @param ipPrefixSet IP Prefixes added or removed |
| 612 | * @param install true if IP Prefixes added, false otherwise |
| 613 | */ |
| 614 | void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) { |
| 615 | Set<Host> hosts = hostService.getConnectedHosts(cp); |
| 616 | |
| 617 | if (hosts == null || hosts.size() == 0) { |
Charles Chan | 10b2fee | 2018-04-21 00:44:29 -0700 | [diff] [blame] | 618 | log.debug("processIntfIpUpdatedEvent: No hosts connected to {}", cp); |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 619 | return; |
| 620 | } |
| 621 | |
| 622 | // Check whether the host IP address is in the interface's subnet |
| 623 | hosts.forEach(host -> |
| 624 | host.ipAddresses().forEach(hostIpAddress -> { |
| 625 | ipPrefixSet.forEach(ipPrefix -> { |
| 626 | if (install && ipPrefix.contains(hostIpAddress)) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 627 | srManager.defaultRoutingHandler.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 628 | host.mac(), host.vlan(), cp.port()); |
| 629 | } else if (!install && ipPrefix.contains(hostIpAddress)) { |
Charles Chan | 2ff1bac | 2018-03-29 16:03:41 -0700 | [diff] [blame] | 630 | srManager.defaultRoutingHandler.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
Jonghwan Hyun | 42fe105 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 631 | host.mac(), host.vlan(), cp.port()); |
| 632 | } |
| 633 | }); |
| 634 | })); |
| 635 | } |
Saurav Das | 45f4815 | 2018-01-18 12:07:33 -0800 | [diff] [blame] | 636 | |
| 637 | /** |
| 638 | * Returns the set of portnumbers on the given device that are part of the |
| 639 | * locations for dual-homed hosts. |
| 640 | * |
| 641 | * @param deviceId the given deviceId |
| 642 | * @return set of port numbers on given device that are dual-homed host |
| 643 | * locations. May be empty if no dual homed hosts are connected to |
| 644 | * the given device |
| 645 | */ |
| 646 | Set<PortNumber> getDualHomedHostPorts(DeviceId deviceId) { |
| 647 | Set<PortNumber> dualHomedLocations = new HashSet<>(); |
| 648 | srManager.hostService.getConnectedHosts(deviceId).stream() |
| 649 | .filter(host -> host.locations().size() == 2) |
| 650 | .forEach(host -> host.locations().stream() |
| 651 | .filter(loc -> loc.deviceId().equals(deviceId)) |
| 652 | .forEach(loc -> dualHomedLocations.add(loc.port()))); |
| 653 | return dualHomedLocations; |
| 654 | } |
Jonghwan Hyun | 800d9d0 | 2018-04-09 09:40:50 -0700 | [diff] [blame] | 655 | |
| 656 | /** |
| 657 | * Checks if the given host is double-tagged or not. |
| 658 | * |
| 659 | * @param host host to check |
| 660 | * @return true if it is double-tagged, false otherwise |
| 661 | */ |
| 662 | private boolean isDoubleTaggedHost(Host host) { |
| 663 | return !host.innerVlan().equals(VlanId.NONE); |
| 664 | } |
| 665 | |
Charles Chan | d299036 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 666 | } |