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