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