Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpPrefix; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Host; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 26 | import org.onosproject.net.HostLocation; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficTreatment; |
| 32 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 33 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
| 34 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 35 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 36 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 37 | import org.onosproject.net.host.HostEvent; |
| 38 | import org.onosproject.net.host.HostService; |
| 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
| 41 | |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 42 | import com.google.common.collect.Sets; |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 43 | |
| 44 | import java.util.Optional; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 45 | import java.util.Set; |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 46 | import java.util.stream.Collectors; |
| 47 | |
| 48 | import static com.google.common.base.Preconditions.checkArgument; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Handles host-related events. |
| 52 | */ |
| 53 | public class HostHandler { |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 54 | |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 55 | private static final Logger log = LoggerFactory.getLogger(HostHandler.class); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 56 | protected final SegmentRoutingManager srManager; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 57 | private HostService hostService; |
| 58 | private FlowObjectiveService flowObjectiveService; |
| 59 | |
| 60 | /** |
| 61 | * Constructs the HostHandler. |
| 62 | * |
| 63 | * @param srManager Segment Routing manager |
| 64 | */ |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 65 | HostHandler(SegmentRoutingManager srManager) { |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 66 | this.srManager = srManager; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 67 | hostService = srManager.hostService; |
| 68 | flowObjectiveService = srManager.flowObjectiveService; |
| 69 | } |
| 70 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 71 | protected void init(DeviceId devId) { |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 72 | hostService.getHosts().forEach(host -> |
| 73 | host.locations().stream() |
| 74 | .filter(location -> location.deviceId().equals(devId)) |
| 75 | .forEach(location -> processHostAddedAtLocation(host, location)) |
| 76 | ); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 77 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 78 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 79 | void processHostAddedEvent(HostEvent event) { |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 80 | processHostAdded(event.subject()); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 83 | private void processHostAdded(Host host) { |
| 84 | host.locations().forEach(location -> processHostAddedAtLocation(host, location)); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 87 | void processHostAddedAtLocation(Host host, HostLocation location) { |
| 88 | checkArgument(host.locations().contains(location), "{} is not a location of {}", location, host); |
| 89 | |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 90 | MacAddress hostMac = host.mac(); |
| 91 | VlanId hostVlanId = host.vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 92 | Set<HostLocation> locations = host.locations(); |
| 93 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 94 | log.info("Host {}/{} is added at {}", hostMac, hostVlanId, locations); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 95 | |
Charles Chan | ceb2a2e | 2017-09-12 18:57:47 -0700 | [diff] [blame] | 96 | if (srManager.isMasterOf(location)) { |
| 97 | processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, false); |
| 98 | ips.forEach(ip -> |
| 99 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, false) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | // Use the pair link temporarily before the second location of a dual-homed host shows up. |
| 104 | // This do not affect single-homed hosts since the flow will be blocked in |
| 105 | // processBridgingRule or processRoutingRule due to VLAN or IP mismatch respectively |
| 106 | srManager.getPairDeviceId(location.deviceId()).ifPresent(pairDeviceId -> { |
| 107 | if (srManager.mastershipService.isLocalMaster(pairDeviceId) && |
| 108 | host.locations().stream().noneMatch(l -> l.deviceId().equals(pairDeviceId))) { |
| 109 | srManager.getPairLocalPorts(pairDeviceId).ifPresent(pairRemotePort -> { |
| 110 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 111 | // when the host is untagged |
| 112 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId); |
| 113 | |
| 114 | processBridgingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, false); |
| 115 | ips.forEach(ip -> processRoutingRule(pairDeviceId, pairRemotePort, hostMac, vlanId, |
| 116 | ip, false)); |
| 117 | }); |
| 118 | } |
| 119 | }); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void processHostRemovedEvent(HostEvent event) { |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 123 | processHostRemoved(event.subject()); |
| 124 | } |
| 125 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 126 | private void processHostRemoved(Host host) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 127 | MacAddress hostMac = host.mac(); |
| 128 | VlanId hostVlanId = host.vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 129 | Set<HostLocation> locations = host.locations(); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 130 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 131 | log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 132 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 133 | locations.forEach(location -> { |
| 134 | if (srManager.isMasterOf(location)) { |
| 135 | processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, true); |
| 136 | ips.forEach(ip -> |
| 137 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true) |
| 138 | ); |
| 139 | } |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 140 | |
| 141 | // Also remove redirection flows on the pair device if exists. |
| 142 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId()); |
| 143 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPorts(location.deviceId()); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 144 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && |
| 145 | srManager.mastershipService.isLocalMaster(pairDeviceId.get())) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 146 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 147 | // when the host is untagged |
| 148 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId); |
| 149 | |
| 150 | processBridgingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, true); |
| 151 | ips.forEach(ip -> |
| 152 | processRoutingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, |
| 153 | ip, true)); |
| 154 | } |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 155 | }); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 158 | void processHostMovedEvent(HostEvent event) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 159 | MacAddress hostMac = event.subject().mac(); |
| 160 | VlanId hostVlanId = event.subject().vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 161 | Set<HostLocation> prevLocations = event.prevSubject().locations(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 162 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 163 | Set<HostLocation> newLocations = event.subject().locations(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 164 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 165 | log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 166 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 167 | Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId) |
| 168 | .collect(Collectors.toSet()); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 169 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 170 | // For each old location |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 171 | Sets.difference(prevLocations, newLocations).stream().filter(srManager::isMasterOf) |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 172 | .forEach(prevLocation -> { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 173 | // Remove routing rules for old IPs |
| 174 | Sets.difference(prevIps, newIps).forEach(ip -> |
| 175 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
| 176 | ip, true) |
| 177 | ); |
| 178 | |
| 179 | // Redirect the flows to pair link if configured |
| 180 | // Note: Do not continue removing any rule |
| 181 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId()); |
| 182 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPorts(prevLocation.deviceId()); |
| 183 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream() |
| 184 | .anyMatch(location -> location.deviceId().equals(pairDeviceId.get()))) { |
| 185 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 186 | // when the host is untagged |
| 187 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId); |
| 188 | |
| 189 | processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false); |
| 190 | newIps.forEach(ip -> |
| 191 | processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, |
| 192 | ip, false)); |
| 193 | return; |
| 194 | } |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 195 | |
| 196 | // Remove bridging rule and routing rules for unchanged IPs if the host moves from a switch to another. |
| 197 | // Otherwise, do not remove and let the adding part update the old flow |
| 198 | if (!newDeviceIds.contains(prevLocation.deviceId())) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 199 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 200 | Sets.intersection(prevIps, newIps).forEach(ip -> |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 201 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 202 | ip, true) |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | // Remove bridging rules if new interface vlan is different from old interface vlan |
| 207 | // Otherwise, do not remove and let the adding part update the old flow |
| 208 | if (newLocations.stream().noneMatch(newLocation -> { |
| 209 | VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation); |
| 210 | VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation); |
| 211 | // Host is tagged and the new location has the host vlan in vlan-tagged |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 212 | return srManager.getTaggedVlanId(newLocation).contains(hostVlanId) || |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 213 | (oldAssignedVlan != null && newAssignedVlan != null && |
| 214 | // Host is untagged and the new location has the same assigned vlan |
| 215 | oldAssignedVlan.equals(newAssignedVlan)); |
| 216 | })) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 217 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Remove routing rules for unchanged IPs if none of the subnet of new location contains |
| 221 | // the IP. Otherwise, do not remove and let the adding part update the old flow |
| 222 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 223 | if (newLocations.stream().noneMatch(newLocation -> |
| 224 | srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 225 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 226 | ip, true); |
| 227 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 228 | }); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 229 | }); |
| 230 | |
| 231 | // For each new location, add all new IPs. |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 232 | Sets.difference(newLocations, prevLocations).stream().filter(srManager::isMasterOf) |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 233 | .forEach(newLocation -> { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 234 | processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 235 | newIps.forEach(ip -> |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 236 | processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 237 | ip, false) |
| 238 | ); |
| 239 | }); |
| 240 | |
| 241 | // For each unchanged location, add new IPs and remove old IPs. |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 242 | Sets.intersection(newLocations, prevLocations).stream().filter(srManager::isMasterOf) |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 243 | .forEach(unchangedLocation -> { |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 244 | Sets.difference(prevIps, newIps).forEach(ip -> |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 245 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), hostMac, |
| 246 | hostVlanId, ip, true) |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 247 | ); |
| 248 | |
| 249 | Sets.difference(newIps, prevIps).forEach(ip -> |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 250 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), hostMac, |
| 251 | hostVlanId, ip, false) |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 252 | ); |
| 253 | }); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 256 | void processHostUpdatedEvent(HostEvent event) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 257 | MacAddress mac = event.subject().mac(); |
| 258 | VlanId vlanId = event.subject().vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 259 | Set<HostLocation> locations = event.subject().locations(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 260 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 261 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 262 | log.info("Host {}/{} is updated", mac, vlanId); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 263 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 264 | locations.stream().filter(srManager::isMasterOf).forEach(location -> { |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 265 | Sets.difference(prevIps, newIps).forEach(ip -> |
| 266 | processRoutingRule(location.deviceId(), location.port(), mac, vlanId, ip, true) |
| 267 | ); |
| 268 | Sets.difference(newIps, prevIps).forEach(ip -> |
| 269 | processRoutingRule(location.deviceId(), location.port(), mac, vlanId, ip, false) |
| 270 | ); |
| 271 | }); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 275 | * Generates a forwarding objective builder for bridging rules. |
| 276 | * <p> |
| 277 | * The forwarding objective bridges packets destined to a given MAC to |
| 278 | * given port on given device. |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 279 | * |
| 280 | * @param deviceId Device that host attaches to |
| 281 | * @param mac MAC address of the host |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 282 | * @param hostVlanId VLAN ID of the host |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 283 | * @param outport Port that host attaches to |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 284 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 285 | * @return Forwarding objective builder |
| 286 | */ |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 287 | ForwardingObjective.Builder bridgingFwdObjBuilder( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 288 | DeviceId deviceId, MacAddress mac, VlanId hostVlanId, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 289 | PortNumber outport, boolean revoke) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 290 | ConnectPoint connectPoint = new ConnectPoint(deviceId, outport); |
| 291 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 292 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 293 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 294 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 295 | // Create host selector |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 296 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 297 | sbuilder.matchEthDst(mac); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 298 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 299 | // Create host treatment |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 300 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 301 | tbuilder.immediate().setOutput(outport); |
| 302 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 303 | // Create host meta |
| 304 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 305 | |
| 306 | // Adjust the selector, treatment and meta according to VLAN configuration |
| 307 | if (taggedVlans.contains(hostVlanId)) { |
| 308 | sbuilder.matchVlanId(hostVlanId); |
| 309 | mbuilder.matchVlanId(hostVlanId); |
| 310 | } else if (hostVlanId.equals(VlanId.NONE)) { |
| 311 | if (untaggedVlan != null) { |
| 312 | sbuilder.matchVlanId(untaggedVlan); |
| 313 | mbuilder.matchVlanId(untaggedVlan); |
| 314 | tbuilder.immediate().popVlan(); |
| 315 | } else if (nativeVlan != null) { |
| 316 | sbuilder.matchVlanId(nativeVlan); |
| 317 | mbuilder.matchVlanId(nativeVlan); |
| 318 | tbuilder.immediate().popVlan(); |
| 319 | } else { |
Charles Chan | 184e024 | 2017-05-26 14:23:58 -0700 | [diff] [blame] | 320 | log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" + |
| 321 | "vlan config", mac, hostVlanId, connectPoint); |
| 322 | return null; |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 323 | } |
| 324 | } else { |
| 325 | log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan", |
| 326 | mac, hostVlanId, connectPoint); |
| 327 | return null; |
| 328 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 329 | |
| 330 | // All forwarding is via Groups. Drivers can re-purpose to flow-actions if needed. |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 331 | // If the objective is to revoke an existing rule, and for some reason |
| 332 | // the next-objective does not exist, then a new one should not be created |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 333 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 334 | tbuilder.build(), mbuilder.build(), !revoke); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 335 | if (portNextObjId == -1) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 336 | // Warning log will come from getPortNextObjective method |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 337 | return null; |
| 338 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 339 | |
| 340 | return DefaultForwardingObjective.builder() |
| 341 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 342 | .withSelector(sbuilder.build()) |
| 343 | .nextStep(portNextObjId) |
| 344 | .withPriority(100) |
| 345 | .fromApp(srManager.appId) |
| 346 | .makePermanent(); |
| 347 | } |
| 348 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 349 | /** |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 350 | * Populate or revoke a bridging rule on given deviceId that matches given mac, given vlan and |
| 351 | * output to given port. |
| 352 | * |
| 353 | * @param deviceId device ID |
| 354 | * @param port port |
| 355 | * @param mac mac address |
| 356 | * @param vlanId VLAN ID |
| 357 | * @param revoke true to revoke the rule; false to populate |
| 358 | */ |
| 359 | private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 360 | VlanId vlanId, boolean revoke) { |
| 361 | log.debug("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating", |
| 362 | mac, vlanId, deviceId, port); |
| 363 | |
| 364 | ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, revoke); |
| 365 | if (fob == null) { |
| 366 | log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | ObjectiveContext context = new DefaultObjectiveContext( |
| 371 | (objective) -> log.debug("Brigding rule for {}/{} {}", mac, vlanId, |
| 372 | revoke ? "revoked" : "populated"), |
| 373 | (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}", |
| 374 | revoke ? "revoked" : "populated", mac, vlanId, error)); |
| 375 | flowObjectiveService.forward(deviceId, revoke ? fob.remove(context) : fob.add(context)); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Populate or revoke a routing rule on given deviceId that matches given ip, |
| 380 | * set destination mac to given mac, set vlan to given vlan and output to given port. |
| 381 | * |
| 382 | * @param deviceId device ID |
| 383 | * @param port port |
| 384 | * @param mac mac address |
| 385 | * @param vlanId VLAN ID |
| 386 | * @param ip IP address |
| 387 | * @param revoke true to revoke the rule; false to populate |
| 388 | */ |
| 389 | private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 390 | VlanId vlanId, IpAddress ip, boolean revoke) { |
| 391 | ConnectPoint location = new ConnectPoint(deviceId, port); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 392 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 393 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 394 | return; |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 395 | } |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 396 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 397 | log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location); |
| 398 | if (revoke) { |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 399 | srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 400 | } else { |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 401 | srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 402 | } |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 403 | } |
Jonghwan Hyun | e5ef762 | 2017-08-25 17:48:36 -0700 | [diff] [blame] | 404 | |
| 405 | /** |
| 406 | * Populate or revoke a bridging rule on given deviceId that matches given vlanId, |
| 407 | * and hostMAC connected to given port, and output to given port only when |
| 408 | * vlan information is valid. |
| 409 | * |
| 410 | * @param deviceId device ID that host attaches to |
| 411 | * @param portNum port number that host attaches to |
| 412 | * @param hostMac mac address of the host connected to the switch port |
| 413 | * @param vlanId Vlan ID configured on the switch port |
| 414 | * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise |
| 415 | * @param install true to populate the objective, false to revoke |
| 416 | */ |
| 417 | private void updateBridgingRule(DeviceId deviceId, PortNumber portNum, MacAddress hostMac, |
| 418 | VlanId vlanId, boolean popVlan, boolean install) { |
| 419 | // Create host selector |
| 420 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 421 | sbuilder.matchEthDst(hostMac); |
| 422 | |
| 423 | // Create host meta |
| 424 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 425 | |
| 426 | sbuilder.matchVlanId(vlanId); |
| 427 | mbuilder.matchVlanId(vlanId); |
| 428 | |
| 429 | // Create host treatment |
| 430 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 431 | tbuilder.immediate().setOutput(portNum); |
| 432 | |
| 433 | if (popVlan) { |
| 434 | tbuilder.immediate().popVlan(); |
| 435 | } |
| 436 | |
| 437 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, portNum, |
| 438 | tbuilder.build(), mbuilder.build(), install); |
| 439 | if (portNextObjId != -1) { |
| 440 | ForwardingObjective.Builder fob = DefaultForwardingObjective.builder() |
| 441 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 442 | .withSelector(sbuilder.build()) |
| 443 | .nextStep(portNextObjId) |
| 444 | .withPriority(100) |
| 445 | .fromApp(srManager.appId) |
| 446 | .makePermanent(); |
| 447 | |
| 448 | ObjectiveContext context = new DefaultObjectiveContext( |
| 449 | (objective) -> log.debug("Brigding rule for {}/{} {}", hostMac, vlanId, |
| 450 | install ? "populated" : "revoked"), |
| 451 | (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}", |
| 452 | install ? "populate" : "revoke", hostMac, vlanId, error)); |
| 453 | flowObjectiveService.forward(deviceId, install ? fob.add(context) : fob.remove(context)); |
| 454 | } else { |
| 455 | log.warn("Failed to retrieve next objective for {}/{}", hostMac, vlanId); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Update forwarding objective for unicast bridging and unicast routing. |
| 461 | * Also check the validity of updated interface configuration on VLAN. |
| 462 | * |
| 463 | * @param deviceId device ID that host attaches to |
| 464 | * @param portNum port number that host attaches to |
| 465 | * @param vlanId Vlan ID configured on the switch port |
| 466 | * @param popVlan true to pop Vlan tag at TrafficTreatment, false otherwise |
| 467 | * @param install true to populate the objective, false to revoke |
| 468 | */ |
| 469 | void processIntfVlanUpdatedEvent(DeviceId deviceId, PortNumber portNum, VlanId vlanId, |
| 470 | boolean popVlan, boolean install) { |
| 471 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNum); |
| 472 | Set<Host> hosts = hostService.getConnectedHosts(connectPoint); |
| 473 | |
| 474 | if (hosts == null || hosts.size() == 0) { |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | hosts.forEach(host -> { |
| 479 | MacAddress mac = host.mac(); |
| 480 | VlanId hostVlanId = host.vlan(); |
| 481 | |
| 482 | // Check whether the host vlan is valid for new interface configuration |
| 483 | if ((!popVlan && hostVlanId.equals(vlanId)) || |
| 484 | (popVlan && hostVlanId.equals(VlanId.NONE))) { |
| 485 | updateBridgingRule(deviceId, portNum, mac, vlanId, popVlan, install); |
| 486 | // Update Forwarding objective and corresponding simple Next objective |
| 487 | // for each host and IP address connected to given port |
| 488 | host.ipAddresses().forEach(ipAddress -> |
| 489 | srManager.routingRulePopulator.updateFwdObj(deviceId, portNum, ipAddress.toIpPrefix(), |
| 490 | mac, vlanId, popVlan, install) |
| 491 | ); |
| 492 | } |
| 493 | }); |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * Populate or revoke routing rule for each host, according to the updated |
| 498 | * subnet configuration on the interface. |
| 499 | * @param cp connect point of the updated interface |
| 500 | * @param ipPrefixSet IP Prefixes added or removed |
| 501 | * @param install true if IP Prefixes added, false otherwise |
| 502 | */ |
| 503 | void processIntfIpUpdatedEvent(ConnectPoint cp, Set<IpPrefix> ipPrefixSet, boolean install) { |
| 504 | Set<Host> hosts = hostService.getConnectedHosts(cp); |
| 505 | |
| 506 | if (hosts == null || hosts.size() == 0) { |
| 507 | log.warn("processIntfIpUpdatedEvent: No hosts connected to {}", cp); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // Check whether the host IP address is in the interface's subnet |
| 512 | hosts.forEach(host -> |
| 513 | host.ipAddresses().forEach(hostIpAddress -> { |
| 514 | ipPrefixSet.forEach(ipPrefix -> { |
| 515 | if (install && ipPrefix.contains(hostIpAddress)) { |
| 516 | srManager.routingRulePopulator.populateRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
| 517 | host.mac(), host.vlan(), cp.port()); |
| 518 | } else if (!install && ipPrefix.contains(hostIpAddress)) { |
| 519 | srManager.routingRulePopulator.revokeRoute(cp.deviceId(), hostIpAddress.toIpPrefix(), |
| 520 | host.mac(), host.vlan(), cp.port()); |
| 521 | } |
| 522 | }); |
| 523 | })); |
| 524 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 525 | } |