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; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
Charles Chan | 10b0fb7 | 2017-02-02 16:20:42 -0800 | [diff] [blame] | 22 | import org.onosproject.net.ConnectPoint; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 24 | import org.onosproject.net.Host; |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 25 | import org.onosproject.net.HostLocation; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
| 31 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 32 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
| 33 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 34 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 35 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 36 | import org.onosproject.net.host.HostEvent; |
| 37 | import org.onosproject.net.host.HostService; |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 41 | import com.google.common.collect.Sets; |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 42 | |
| 43 | import java.util.Optional; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 44 | import java.util.Set; |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 45 | import java.util.stream.Collectors; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkArgument; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Handles host-related events. |
| 51 | */ |
| 52 | public class HostHandler { |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 53 | |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 54 | private static final Logger log = LoggerFactory.getLogger(HostHandler.class); |
Charles Chan | 114aec7 | 2017-06-19 14:00:53 -0700 | [diff] [blame] | 55 | protected final SegmentRoutingManager srManager; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 56 | private HostService hostService; |
| 57 | private FlowObjectiveService flowObjectiveService; |
| 58 | |
| 59 | /** |
| 60 | * Constructs the HostHandler. |
| 61 | * |
| 62 | * @param srManager Segment Routing manager |
| 63 | */ |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 64 | HostHandler(SegmentRoutingManager srManager) { |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 65 | this.srManager = srManager; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 66 | hostService = srManager.hostService; |
| 67 | flowObjectiveService = srManager.flowObjectiveService; |
| 68 | } |
| 69 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 70 | protected void init(DeviceId devId) { |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 71 | hostService.getHosts().forEach(host -> |
| 72 | host.locations().stream() |
| 73 | .filter(location -> location.deviceId().equals(devId)) |
| 74 | .forEach(location -> processHostAddedAtLocation(host, location)) |
| 75 | ); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 76 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 77 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 78 | void processHostAddedEvent(HostEvent event) { |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 79 | processHostAdded(event.subject()); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 82 | private void processHostAdded(Host host) { |
| 83 | host.locations().forEach(location -> processHostAddedAtLocation(host, location)); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 86 | void processHostAddedAtLocation(Host host, HostLocation location) { |
| 87 | checkArgument(host.locations().contains(location), "{} is not a location of {}", location, host); |
| 88 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 89 | if (!srManager.isMasterOf(location)) { |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 93 | MacAddress mac = host.mac(); |
| 94 | VlanId vlanId = host.vlan(); |
| 95 | Set<HostLocation> locations = host.locations(); |
| 96 | Set<IpAddress> ips = host.ipAddresses(); |
| 97 | log.info("Host {}/{} is added at {}", mac, vlanId, locations); |
| 98 | |
| 99 | processBridgingRule(location.deviceId(), location.port(), mac, vlanId, false); |
| 100 | ips.forEach(ip -> |
| 101 | processRoutingRule(location.deviceId(), location.port(), mac, vlanId, ip, false) |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | void processHostRemovedEvent(HostEvent event) { |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 106 | processHostRemoved(event.subject()); |
| 107 | } |
| 108 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 109 | private void processHostRemoved(Host host) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 110 | MacAddress hostMac = host.mac(); |
| 111 | VlanId hostVlanId = host.vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 112 | Set<HostLocation> locations = host.locations(); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 113 | Set<IpAddress> ips = host.ipAddresses(); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 114 | log.info("Host {}/{} is removed from {}", hostMac, hostVlanId, locations); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 115 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 116 | locations.forEach(location -> { |
| 117 | if (srManager.isMasterOf(location)) { |
| 118 | processBridgingRule(location.deviceId(), location.port(), hostMac, hostVlanId, true); |
| 119 | ips.forEach(ip -> |
| 120 | processRoutingRule(location.deviceId(), location.port(), hostMac, hostVlanId, ip, true) |
| 121 | ); |
| 122 | } |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 123 | |
| 124 | // Also remove redirection flows on the pair device if exists. |
| 125 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(location.deviceId()); |
| 126 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPorts(location.deviceId()); |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 127 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && |
| 128 | srManager.mastershipService.isLocalMaster(pairDeviceId.get())) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 129 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 130 | // when the host is untagged |
| 131 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(location)).orElse(hostVlanId); |
| 132 | |
| 133 | processBridgingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, true); |
| 134 | ips.forEach(ip -> |
| 135 | processRoutingRule(pairDeviceId.get(), pairLocalPort.get(), hostMac, vlanId, |
| 136 | ip, true)); |
| 137 | } |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 138 | }); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 141 | void processHostMovedEvent(HostEvent event) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 142 | MacAddress hostMac = event.subject().mac(); |
| 143 | VlanId hostVlanId = event.subject().vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 144 | Set<HostLocation> prevLocations = event.prevSubject().locations(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 145 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 146 | Set<HostLocation> newLocations = event.subject().locations(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 147 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 148 | log.info("Host {}/{} is moved from {} to {}", hostMac, hostVlanId, prevLocations, newLocations); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 149 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 150 | Set<DeviceId> newDeviceIds = newLocations.stream().map(HostLocation::deviceId) |
| 151 | .collect(Collectors.toSet()); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 152 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 153 | // For each old location |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 154 | Sets.difference(prevLocations, newLocations).stream().filter(srManager::isMasterOf) |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 155 | .forEach(prevLocation -> { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 156 | // Remove routing rules for old IPs |
| 157 | Sets.difference(prevIps, newIps).forEach(ip -> |
| 158 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
| 159 | ip, true) |
| 160 | ); |
| 161 | |
| 162 | // Redirect the flows to pair link if configured |
| 163 | // Note: Do not continue removing any rule |
| 164 | Optional<DeviceId> pairDeviceId = srManager.getPairDeviceId(prevLocation.deviceId()); |
| 165 | Optional<PortNumber> pairLocalPort = srManager.getPairLocalPorts(prevLocation.deviceId()); |
| 166 | if (pairDeviceId.isPresent() && pairLocalPort.isPresent() && newLocations.stream() |
| 167 | .anyMatch(location -> location.deviceId().equals(pairDeviceId.get()))) { |
| 168 | // NOTE: Since the pairLocalPort is trunk port, use assigned vlan of original port |
| 169 | // when the host is untagged |
| 170 | VlanId vlanId = Optional.ofNullable(srManager.getInternalVlanId(prevLocation)).orElse(hostVlanId); |
| 171 | |
| 172 | processBridgingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, false); |
| 173 | newIps.forEach(ip -> |
| 174 | processRoutingRule(prevLocation.deviceId(), pairLocalPort.get(), hostMac, vlanId, |
| 175 | ip, false)); |
| 176 | return; |
| 177 | } |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 178 | |
| 179 | // Remove bridging rule and routing rules for unchanged IPs if the host moves from a switch to another. |
| 180 | // Otherwise, do not remove and let the adding part update the old flow |
| 181 | if (!newDeviceIds.contains(prevLocation.deviceId())) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 182 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 183 | Sets.intersection(prevIps, newIps).forEach(ip -> |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 184 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 185 | ip, true) |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | // Remove bridging rules if new interface vlan is different from old interface vlan |
| 190 | // Otherwise, do not remove and let the adding part update the old flow |
| 191 | if (newLocations.stream().noneMatch(newLocation -> { |
| 192 | VlanId oldAssignedVlan = srManager.getInternalVlanId(prevLocation); |
| 193 | VlanId newAssignedVlan = srManager.getInternalVlanId(newLocation); |
| 194 | // 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] | 195 | return srManager.getTaggedVlanId(newLocation).contains(hostVlanId) || |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 196 | (oldAssignedVlan != null && newAssignedVlan != null && |
| 197 | // Host is untagged and the new location has the same assigned vlan |
| 198 | oldAssignedVlan.equals(newAssignedVlan)); |
| 199 | })) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 200 | processBridgingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, true); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Remove routing rules for unchanged IPs if none of the subnet of new location contains |
| 204 | // the IP. Otherwise, do not remove and let the adding part update the old flow |
| 205 | Sets.intersection(prevIps, newIps).forEach(ip -> { |
| 206 | if (newLocations.stream().noneMatch(newLocation -> |
| 207 | srManager.deviceConfiguration.inSameSubnet(newLocation, ip))) { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 208 | processRoutingRule(prevLocation.deviceId(), prevLocation.port(), hostMac, hostVlanId, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 209 | ip, true); |
| 210 | } |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 211 | }); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 212 | }); |
| 213 | |
| 214 | // For each new location, add all new IPs. |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 215 | Sets.difference(newLocations, prevLocations).stream().filter(srManager::isMasterOf) |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 216 | .forEach(newLocation -> { |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 217 | processBridgingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, false); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 218 | newIps.forEach(ip -> |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 219 | processRoutingRule(newLocation.deviceId(), newLocation.port(), hostMac, hostVlanId, |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 220 | ip, false) |
| 221 | ); |
| 222 | }); |
| 223 | |
| 224 | // For each unchanged location, add new IPs and remove old IPs. |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 225 | Sets.intersection(newLocations, prevLocations).stream().filter(srManager::isMasterOf) |
Charles Chan | abfe7e0 | 2017-08-09 16:50:15 -0700 | [diff] [blame] | 226 | .forEach(unchangedLocation -> { |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 227 | Sets.difference(prevIps, newIps).forEach(ip -> |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 228 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), hostMac, |
| 229 | hostVlanId, ip, true) |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 230 | ); |
| 231 | |
| 232 | Sets.difference(newIps, prevIps).forEach(ip -> |
Charles Chan | 3ed34d8 | 2017-06-22 18:03:14 -0700 | [diff] [blame] | 233 | processRoutingRule(unchangedLocation.deviceId(), unchangedLocation.port(), hostMac, |
| 234 | hostVlanId, ip, false) |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 235 | ); |
| 236 | }); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 239 | void processHostUpdatedEvent(HostEvent event) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 240 | MacAddress mac = event.subject().mac(); |
| 241 | VlanId vlanId = event.subject().vlan(); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 242 | Set<HostLocation> locations = event.subject().locations(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 243 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 244 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 245 | log.info("Host {}/{} is updated", mac, vlanId); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 246 | |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 247 | locations.stream().filter(srManager::isMasterOf).forEach(location -> { |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 248 | Sets.difference(prevIps, newIps).forEach(ip -> |
| 249 | processRoutingRule(location.deviceId(), location.port(), mac, vlanId, ip, true) |
| 250 | ); |
| 251 | Sets.difference(newIps, prevIps).forEach(ip -> |
| 252 | processRoutingRule(location.deviceId(), location.port(), mac, vlanId, ip, false) |
| 253 | ); |
| 254 | }); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /** |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 258 | * Generates a forwarding objective builder for bridging rules. |
| 259 | * <p> |
| 260 | * The forwarding objective bridges packets destined to a given MAC to |
| 261 | * given port on given device. |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 262 | * |
| 263 | * @param deviceId Device that host attaches to |
| 264 | * @param mac MAC address of the host |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 265 | * @param hostVlanId VLAN ID of the host |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 266 | * @param outport Port that host attaches to |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 267 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 268 | * @return Forwarding objective builder |
| 269 | */ |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 270 | ForwardingObjective.Builder bridgingFwdObjBuilder( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 271 | DeviceId deviceId, MacAddress mac, VlanId hostVlanId, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 272 | PortNumber outport, boolean revoke) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 273 | ConnectPoint connectPoint = new ConnectPoint(deviceId, outport); |
| 274 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 275 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 276 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 277 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 278 | // Create host selector |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 279 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 280 | sbuilder.matchEthDst(mac); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 281 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 282 | // Create host treatment |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 283 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 284 | tbuilder.immediate().setOutput(outport); |
| 285 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 286 | // Create host meta |
| 287 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 288 | |
| 289 | // Adjust the selector, treatment and meta according to VLAN configuration |
| 290 | if (taggedVlans.contains(hostVlanId)) { |
| 291 | sbuilder.matchVlanId(hostVlanId); |
| 292 | mbuilder.matchVlanId(hostVlanId); |
| 293 | } else if (hostVlanId.equals(VlanId.NONE)) { |
| 294 | if (untaggedVlan != null) { |
| 295 | sbuilder.matchVlanId(untaggedVlan); |
| 296 | mbuilder.matchVlanId(untaggedVlan); |
| 297 | tbuilder.immediate().popVlan(); |
| 298 | } else if (nativeVlan != null) { |
| 299 | sbuilder.matchVlanId(nativeVlan); |
| 300 | mbuilder.matchVlanId(nativeVlan); |
| 301 | tbuilder.immediate().popVlan(); |
| 302 | } else { |
Charles Chan | 184e024 | 2017-05-26 14:23:58 -0700 | [diff] [blame] | 303 | log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" + |
| 304 | "vlan config", mac, hostVlanId, connectPoint); |
| 305 | return null; |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 306 | } |
| 307 | } else { |
| 308 | log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan", |
| 309 | mac, hostVlanId, connectPoint); |
| 310 | return null; |
| 311 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 312 | |
| 313 | // 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] | 314 | // If the objective is to revoke an existing rule, and for some reason |
| 315 | // 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] | 316 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 317 | tbuilder.build(), mbuilder.build(), !revoke); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 318 | if (portNextObjId == -1) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 319 | // Warning log will come from getPortNextObjective method |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 320 | return null; |
| 321 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 322 | |
| 323 | return DefaultForwardingObjective.builder() |
| 324 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 325 | .withSelector(sbuilder.build()) |
| 326 | .nextStep(portNextObjId) |
| 327 | .withPriority(100) |
| 328 | .fromApp(srManager.appId) |
| 329 | .makePermanent(); |
| 330 | } |
| 331 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 332 | /** |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 333 | * Populate or revoke a bridging rule on given deviceId that matches given mac, given vlan and |
| 334 | * output to given port. |
| 335 | * |
| 336 | * @param deviceId device ID |
| 337 | * @param port port |
| 338 | * @param mac mac address |
| 339 | * @param vlanId VLAN ID |
| 340 | * @param revoke true to revoke the rule; false to populate |
| 341 | */ |
| 342 | private void processBridgingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 343 | VlanId vlanId, boolean revoke) { |
| 344 | log.debug("{} bridging entry for host {}/{} at {}:{}", revoke ? "Revoking" : "Populating", |
| 345 | mac, vlanId, deviceId, port); |
| 346 | |
| 347 | ForwardingObjective.Builder fob = bridgingFwdObjBuilder(deviceId, mac, vlanId, port, revoke); |
| 348 | if (fob == null) { |
| 349 | log.warn("Fail to build fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | ObjectiveContext context = new DefaultObjectiveContext( |
| 354 | (objective) -> log.debug("Brigding rule for {}/{} {}", mac, vlanId, |
| 355 | revoke ? "revoked" : "populated"), |
| 356 | (objective, error) -> log.warn("Failed to {} bridging rule for {}/{}: {}", |
| 357 | revoke ? "revoked" : "populated", mac, vlanId, error)); |
| 358 | flowObjectiveService.forward(deviceId, revoke ? fob.remove(context) : fob.add(context)); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Populate or revoke a routing rule on given deviceId that matches given ip, |
| 363 | * set destination mac to given mac, set vlan to given vlan and output to given port. |
| 364 | * |
| 365 | * @param deviceId device ID |
| 366 | * @param port port |
| 367 | * @param mac mac address |
| 368 | * @param vlanId VLAN ID |
| 369 | * @param ip IP address |
| 370 | * @param revoke true to revoke the rule; false to populate |
| 371 | */ |
| 372 | private void processRoutingRule(DeviceId deviceId, PortNumber port, MacAddress mac, |
| 373 | VlanId vlanId, IpAddress ip, boolean revoke) { |
| 374 | ConnectPoint location = new ConnectPoint(deviceId, port); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 375 | if (!srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
| 376 | log.info("{} is not included in the subnet config of {}/{}. Ignored.", ip, deviceId, port); |
| 377 | return; |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 378 | } |
Charles Chan | 9595e6a | 2017-06-15 19:25:25 -0700 | [diff] [blame] | 379 | |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 380 | log.info("{} routing rule for {} at {}", revoke ? "Revoking" : "Populating", ip, location); |
| 381 | if (revoke) { |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 382 | srManager.defaultRoutingHandler.revokeRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | d9265a3 | 2017-06-16 15:19:24 -0700 | [diff] [blame] | 383 | } else { |
Charles Chan | 910be6a | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 384 | srManager.defaultRoutingHandler.populateRoute(deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 385 | } |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 386 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 387 | } |