Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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; |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 38 | import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 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 | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 43 | import java.util.Set; |
| 44 | |
| 45 | /** |
| 46 | * Handles host-related events. |
| 47 | */ |
| 48 | public class HostHandler { |
| 49 | private static final Logger log = LoggerFactory.getLogger(HostHandler.class); |
| 50 | private final SegmentRoutingManager srManager; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 51 | private HostService hostService; |
| 52 | private FlowObjectiveService flowObjectiveService; |
| 53 | |
| 54 | /** |
| 55 | * Constructs the HostHandler. |
| 56 | * |
| 57 | * @param srManager Segment Routing manager |
| 58 | */ |
| 59 | public HostHandler(SegmentRoutingManager srManager) { |
| 60 | this.srManager = srManager; |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 61 | hostService = srManager.hostService; |
| 62 | flowObjectiveService = srManager.flowObjectiveService; |
| 63 | } |
| 64 | |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 65 | protected void init(DeviceId devId) { |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 66 | hostService.getHosts().forEach(host -> { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 67 | DeviceId deviceId = host.location().deviceId(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 68 | // The host does not attach to this device |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 69 | if (!deviceId.equals(devId)) { |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 70 | return; |
| 71 | } |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 72 | processHostAdded(host); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 73 | }); |
| 74 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 75 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 76 | protected void processHostAddedEvent(HostEvent event) { |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 77 | processHostAdded(event.subject()); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 80 | protected void processHostAdded(Host host) { |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 81 | MacAddress mac = host.mac(); |
| 82 | VlanId vlanId = host.vlan(); |
| 83 | HostLocation location = host.location(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 84 | DeviceId deviceId = location.deviceId(); |
| 85 | PortNumber port = location.port(); |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 86 | Set<IpAddress> ips = host.ipAddresses(); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 87 | log.info("Host {}/{} is added at {}:{}", mac, vlanId, deviceId, port); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 88 | |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 89 | if (accepted(host)) { |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 90 | // Populate bridging table entry |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 91 | log.debug("Populating bridging entry for host {}/{} at {}:{}", |
| 92 | mac, vlanId, deviceId, port); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 93 | ForwardingObjective.Builder fob = |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 94 | bridgingFwdObjBuilder(deviceId, mac, vlanId, port, false); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 95 | if (fob == null) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 96 | log.warn("Fail to create fwd obj for host {}/{}. Abort.", mac, vlanId); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 99 | ObjectiveContext context = new DefaultObjectiveContext( |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 100 | (objective) -> log.debug("Brigding rule for {}/{} populated", |
| 101 | mac, vlanId), |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 102 | (objective, error) -> |
Saurav Das | 368cf21 | 2017-03-15 15:15:14 -0700 | [diff] [blame] | 103 | log.warn("Failed to populate bridging rule for {}/{}: {}", |
| 104 | mac, vlanId, error)); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 105 | flowObjectiveService.forward(deviceId, fob.add(context)); |
| 106 | |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 107 | ips.forEach(ip -> { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 108 | // Populate IP table entry |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 109 | if (srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 110 | srManager.routingRulePopulator.populateRoute( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 111 | deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 112 | } |
| 113 | }); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 114 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 117 | protected void processHostRemoveEvent(HostEvent event) { |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 118 | processHostRemoved(event.subject()); |
| 119 | } |
| 120 | |
| 121 | protected void processHostRemoved(Host host) { |
| 122 | MacAddress mac = host.mac(); |
| 123 | VlanId vlanId = host.vlan(); |
| 124 | HostLocation location = host.location(); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 125 | DeviceId deviceId = location.deviceId(); |
| 126 | PortNumber port = location.port(); |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 127 | Set<IpAddress> ips = host.ipAddresses(); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 128 | log.info("Host {}/{} is removed from {}:{}", mac, vlanId, deviceId, port); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 129 | |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 130 | if (accepted(host)) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 131 | // Revoke bridging table entry |
| 132 | ForwardingObjective.Builder fob = |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 133 | bridgingFwdObjBuilder(deviceId, mac, vlanId, port, true); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 134 | if (fob == null) { |
| 135 | log.warn("Fail to create fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 136 | return; |
| 137 | } |
| 138 | ObjectiveContext context = new DefaultObjectiveContext( |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 139 | (objective) -> log.debug("Host rule for {} revoked", host), |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 140 | (objective, error) -> |
Charles Chan | 41f5ec0 | 2016-06-13 18:54:31 -0700 | [diff] [blame] | 141 | log.warn("Failed to revoke host rule for {}: {}", host, error)); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 142 | flowObjectiveService.forward(deviceId, fob.remove(context)); |
| 143 | |
| 144 | // Revoke IP table entry |
| 145 | ips.forEach(ip -> { |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 146 | if (srManager.deviceConfiguration.inSameSubnet(location, ip)) { |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 147 | srManager.routingRulePopulator.revokeRoute( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 148 | deviceId, ip.toIpPrefix(), mac, vlanId, port); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 149 | } |
| 150 | }); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | protected void processHostMovedEvent(HostEvent event) { |
| 155 | MacAddress mac = event.subject().mac(); |
| 156 | VlanId vlanId = event.subject().vlan(); |
| 157 | HostLocation prevLocation = event.prevSubject().location(); |
| 158 | DeviceId prevDeviceId = prevLocation.deviceId(); |
| 159 | PortNumber prevPort = prevLocation.port(); |
| 160 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
| 161 | HostLocation newLocation = event.subject().location(); |
| 162 | DeviceId newDeviceId = newLocation.deviceId(); |
| 163 | PortNumber newPort = newLocation.port(); |
| 164 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 165 | log.info("Host {}/{} is moved from {}:{} to {}:{}", |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 166 | mac, vlanId, prevDeviceId, prevPort, newDeviceId, newPort); |
| 167 | |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 168 | if (accepted(event.prevSubject())) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 169 | // Revoke previous bridging table entry |
| 170 | ForwardingObjective.Builder prevFob = |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 171 | bridgingFwdObjBuilder(prevDeviceId, mac, vlanId, prevPort, true); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 172 | if (prevFob == null) { |
| 173 | log.warn("Fail to create fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 174 | return; |
| 175 | } |
| 176 | ObjectiveContext context = new DefaultObjectiveContext( |
| 177 | (objective) -> log.debug("Host rule for {} revoked", event.subject()), |
| 178 | (objective, error) -> |
| 179 | log.warn("Failed to revoke host rule for {}: {}", event.subject(), error)); |
| 180 | flowObjectiveService.forward(prevDeviceId, prevFob.remove(context)); |
| 181 | |
| 182 | // Revoke previous IP table entry |
| 183 | prevIps.forEach(ip -> { |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 184 | if (srManager.deviceConfiguration.inSameSubnet(prevLocation, ip)) { |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 185 | srManager.routingRulePopulator.revokeRoute( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 186 | prevDeviceId, ip.toIpPrefix(), mac, vlanId, prevPort); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 187 | } |
| 188 | }); |
| 189 | } |
| 190 | |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 191 | if (accepted(event.subject())) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 192 | // Populate new bridging table entry |
| 193 | ForwardingObjective.Builder newFob = |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 194 | bridgingFwdObjBuilder(newDeviceId, mac, vlanId, newPort, false); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 195 | if (newFob == null) { |
| 196 | log.warn("Fail to create fwd obj for host {}/{}. Abort.", mac, vlanId); |
| 197 | return; |
| 198 | } |
| 199 | ObjectiveContext context = new DefaultObjectiveContext( |
| 200 | (objective) -> log.debug("Host rule for {} populated", event.subject()), |
| 201 | (objective, error) -> |
| 202 | log.warn("Failed to populate host rule for {}: {}", event.subject(), error)); |
| 203 | flowObjectiveService.forward(newDeviceId, newFob.add(context)); |
| 204 | |
| 205 | // Populate new IP table entry |
| 206 | newIps.forEach(ip -> { |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 207 | if (srManager.deviceConfiguration.inSameSubnet(newLocation, ip)) { |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 208 | srManager.routingRulePopulator.populateRoute( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 209 | newDeviceId, ip.toIpPrefix(), mac, vlanId, newPort); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 210 | } |
| 211 | }); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | protected void processHostUpdatedEvent(HostEvent event) { |
| 216 | MacAddress mac = event.subject().mac(); |
| 217 | VlanId vlanId = event.subject().vlan(); |
| 218 | HostLocation prevLocation = event.prevSubject().location(); |
| 219 | DeviceId prevDeviceId = prevLocation.deviceId(); |
| 220 | PortNumber prevPort = prevLocation.port(); |
| 221 | Set<IpAddress> prevIps = event.prevSubject().ipAddresses(); |
| 222 | HostLocation newLocation = event.subject().location(); |
| 223 | DeviceId newDeviceId = newLocation.deviceId(); |
| 224 | PortNumber newPort = newLocation.port(); |
| 225 | Set<IpAddress> newIps = event.subject().ipAddresses(); |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 226 | log.info("Host {}/{} is updated", mac, vlanId); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 227 | |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 228 | if (accepted(event.prevSubject())) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 229 | // Revoke previous IP table entry |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 230 | Sets.difference(prevIps, newIps).forEach(ip -> { |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 231 | if (srManager.deviceConfiguration.inSameSubnet(prevLocation, ip)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 232 | log.info("revoking previous IP rule:{}", ip); |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 233 | srManager.routingRulePopulator.revokeRoute( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 234 | prevDeviceId, ip.toIpPrefix(), mac, vlanId, prevPort); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 235 | } |
| 236 | }); |
| 237 | } |
| 238 | |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 239 | if (accepted(event.subject())) { |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 240 | // Populate new IP table entry |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 241 | Sets.difference(newIps, prevIps).forEach(ip -> { |
Pier Ventre | 6b2c1b3 | 2016-12-09 17:26:04 -0800 | [diff] [blame] | 242 | if (srManager.deviceConfiguration.inSameSubnet(newLocation, ip)) { |
Saurav Das | f933219 | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 243 | log.info("populating new IP rule:{}", ip); |
Charles Chan | ddac7fd | 2016-10-27 14:19:48 -0700 | [diff] [blame] | 244 | srManager.routingRulePopulator.populateRoute( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 245 | newDeviceId, ip.toIpPrefix(), mac, vlanId, newPort); |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 246 | } |
| 247 | }); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 252 | * Generates a forwarding objective builder for bridging rules. |
| 253 | * <p> |
| 254 | * The forwarding objective bridges packets destined to a given MAC to |
| 255 | * given port on given device. |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 256 | * |
| 257 | * @param deviceId Device that host attaches to |
| 258 | * @param mac MAC address of the host |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 259 | * @param hostVlanId VLAN ID of the host |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 260 | * @param outport Port that host attaches to |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 261 | * @param revoke true if forwarding objective is meant to revoke forwarding rule |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 262 | * @return Forwarding objective builder |
| 263 | */ |
Charles Chan | 18fa425 | 2017-02-08 16:10:40 -0800 | [diff] [blame] | 264 | private ForwardingObjective.Builder bridgingFwdObjBuilder( |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 265 | DeviceId deviceId, MacAddress mac, VlanId hostVlanId, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 266 | PortNumber outport, boolean revoke) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 267 | ConnectPoint connectPoint = new ConnectPoint(deviceId, outport); |
| 268 | VlanId untaggedVlan = srManager.getUntaggedVlanId(connectPoint); |
| 269 | Set<VlanId> taggedVlans = srManager.getTaggedVlanId(connectPoint); |
| 270 | VlanId nativeVlan = srManager.getNativeVlanId(connectPoint); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 271 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 272 | // Create host selector |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 273 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 274 | sbuilder.matchEthDst(mac); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 275 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 276 | // Create host treatment |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 277 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 278 | tbuilder.immediate().setOutput(outport); |
| 279 | |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 280 | // Create host meta |
| 281 | TrafficSelector.Builder mbuilder = DefaultTrafficSelector.builder(); |
| 282 | |
| 283 | // Adjust the selector, treatment and meta according to VLAN configuration |
| 284 | if (taggedVlans.contains(hostVlanId)) { |
| 285 | sbuilder.matchVlanId(hostVlanId); |
| 286 | mbuilder.matchVlanId(hostVlanId); |
| 287 | } else if (hostVlanId.equals(VlanId.NONE)) { |
| 288 | if (untaggedVlan != null) { |
| 289 | sbuilder.matchVlanId(untaggedVlan); |
| 290 | mbuilder.matchVlanId(untaggedVlan); |
| 291 | tbuilder.immediate().popVlan(); |
| 292 | } else if (nativeVlan != null) { |
| 293 | sbuilder.matchVlanId(nativeVlan); |
| 294 | mbuilder.matchVlanId(nativeVlan); |
| 295 | tbuilder.immediate().popVlan(); |
| 296 | } else { |
Charles Chan | 184e024 | 2017-05-26 14:23:58 -0700 | [diff] [blame^] | 297 | log.warn("Untagged host {}/{} is not allowed on {} without untagged or native" + |
| 298 | "vlan config", mac, hostVlanId, connectPoint); |
| 299 | return null; |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 300 | } |
| 301 | } else { |
| 302 | log.warn("Tagged host {}/{} is not allowed on {} without VLAN listed in tagged vlan", |
| 303 | mac, hostVlanId, connectPoint); |
| 304 | return null; |
| 305 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 306 | |
| 307 | // 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] | 308 | // If the objective is to revoke an existing rule, and for some reason |
| 309 | // 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] | 310 | int portNextObjId = srManager.getPortNextObjectiveId(deviceId, outport, |
Saurav Das | 2cb3829 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 311 | tbuilder.build(), mbuilder.build(), !revoke); |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 312 | if (portNextObjId == -1) { |
Charles Chan | 90772a7 | 2017-02-08 15:52:08 -0800 | [diff] [blame] | 313 | // Warning log will come from getPortNextObjective method |
Saurav Das | 07c7460 | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 314 | return null; |
| 315 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 316 | |
| 317 | return DefaultForwardingObjective.builder() |
| 318 | .withFlag(ForwardingObjective.Flag.SPECIFIC) |
| 319 | .withSelector(sbuilder.build()) |
| 320 | .nextStep(portNextObjId) |
| 321 | .withPriority(100) |
| 322 | .fromApp(srManager.appId) |
| 323 | .makePermanent(); |
| 324 | } |
| 325 | |
Charles Chan | c22cef3 | 2016-04-29 14:38:22 -0700 | [diff] [blame] | 326 | /** |
Charles Chan | debfea3 | 2016-10-24 14:52:01 -0700 | [diff] [blame] | 327 | * Determines whether a host should be accepted by SR or not. |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 328 | * |
| 329 | * @param host host to be checked |
| 330 | * @return true if segment routing accepts the host |
| 331 | */ |
| 332 | private boolean accepted(Host host) { |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 333 | SegmentRoutingAppConfig appConfig = srManager.cfgService |
| 334 | .getConfig(srManager.appId, SegmentRoutingAppConfig.class); |
Charles Chan | 3bf64b9 | 2016-05-20 10:55:40 -0700 | [diff] [blame] | 335 | |
| 336 | boolean accepted = appConfig == null || |
| 337 | (!appConfig.suppressHostByProvider().contains(host.providerId().id()) && |
| 338 | !appConfig.suppressHostByPort().contains(host.location())); |
Charles Chan | 370a65b | 2016-05-10 17:29:47 -0700 | [diff] [blame] | 339 | if (!accepted) { |
| 340 | log.info("Ignore suppressed host {}", host.id()); |
| 341 | } |
| 342 | return accepted; |
| 343 | } |
Charles Chan | 1eaf480 | 2016-04-18 13:44:03 -0700 | [diff] [blame] | 344 | } |