Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [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 | package org.onosproject.provider.netcfglinks; |
| 17 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 18 | import org.onlab.packet.Ethernet; |
| 19 | import org.onlab.packet.ONOSLLDP; |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 20 | import org.onosproject.cluster.ClusterMetadataService; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.mastership.MastershipService; |
| 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.DefaultAnnotations; |
| 26 | import org.onosproject.net.Device; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.LinkKey; |
| 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.config.NetworkConfigEvent; |
| 33 | import org.onosproject.net.config.NetworkConfigListener; |
| 34 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 35 | import org.onosproject.net.config.basics.BasicLinkConfig; |
| 36 | import org.onosproject.net.device.DeviceEvent; |
| 37 | import org.onosproject.net.device.DeviceListener; |
| 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficSelector; |
| 41 | import org.onosproject.net.link.DefaultLinkDescription; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 42 | import org.onosproject.net.link.LinkProviderRegistry; |
| 43 | import org.onosproject.net.link.LinkProviderService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 44 | import org.onosproject.net.link.ProbedLinkProvider; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 45 | import org.onosproject.net.packet.InboundPacket; |
| 46 | import org.onosproject.net.packet.PacketContext; |
| 47 | import org.onosproject.net.packet.PacketPriority; |
| 48 | import org.onosproject.net.packet.PacketProcessor; |
| 49 | import org.onosproject.net.packet.PacketService; |
| 50 | import org.onosproject.net.provider.AbstractProvider; |
| 51 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 52 | import org.onosproject.provider.lldpcommon.LinkDiscovery; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | import org.onosproject.provider.lldpcommon.LinkDiscoveryContext; |
| 54 | import org.osgi.service.component.annotations.Activate; |
| 55 | import org.osgi.service.component.annotations.Component; |
| 56 | import org.osgi.service.component.annotations.Deactivate; |
| 57 | import org.osgi.service.component.annotations.Reference; |
| 58 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 62 | import java.util.HashSet; |
| 63 | import java.util.Map; |
| 64 | import java.util.Optional; |
| 65 | import java.util.Set; |
| 66 | import java.util.concurrent.ConcurrentHashMap; |
| 67 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 68 | import static org.onlab.packet.Ethernet.TYPE_BSN; |
| 69 | import static org.onlab.packet.Ethernet.TYPE_LLDP; |
| 70 | import static org.onosproject.net.PortNumber.portNumber; |
Ray Milkey | d17309c | 2018-10-18 09:34:54 -0700 | [diff] [blame] | 71 | import static org.onosproject.provider.netcfglinks.OsgiPropertyConstants.DISCOVERY_DELAY_DEFAULT; |
| 72 | import static org.onosproject.provider.netcfglinks.OsgiPropertyConstants.PROP_DISCOVERY_DELAY; |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 73 | import static org.onosproject.provider.netcfglinks.OsgiPropertyConstants.PROP_PROBE_RATE; |
| 74 | import static org.onosproject.provider.netcfglinks.OsgiPropertyConstants.PROBE_RATE_DEFAULT; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Provider to pre-discover links and devices based on a specified network |
| 78 | * config. |
| 79 | */ |
| 80 | |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 81 | @Component(immediate = true, |
| 82 | property = { |
| 83 | PROP_PROBE_RATE + ":Integer=" + PROBE_RATE_DEFAULT, |
Ray Milkey | d17309c | 2018-10-18 09:34:54 -0700 | [diff] [blame] | 84 | PROP_DISCOVERY_DELAY + ":Integer=" + DISCOVERY_DELAY_DEFAULT, |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 85 | }) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 86 | public class NetworkConfigLinksProvider |
| 87 | extends AbstractProvider |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 88 | implements ProbedLinkProvider { |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 91 | protected LinkProviderRegistry providerRegistry; |
| 92 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 94 | protected DeviceService deviceService; |
| 95 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 97 | protected PacketService packetService; |
| 98 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 100 | protected MastershipService masterService; |
| 101 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 103 | protected NetworkConfigRegistry netCfgService; |
| 104 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 106 | protected CoreService coreService; |
| 107 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 109 | protected ClusterMetadataService metadataService; |
| 110 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 111 | /** LLDP and BDDP probe rate specified in millis. */ |
Thomas Vachuska | 4167c3f | 2018-10-16 07:16:31 -0700 | [diff] [blame] | 112 | private int probeRate = PROBE_RATE_DEFAULT; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 113 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 114 | /** Number of millis beyond which an LLDP packet will not be accepted. */ |
Ray Milkey | bd508ed | 2019-03-19 14:22:02 -0700 | [diff] [blame] | 115 | private int maxLldpAge = DISCOVERY_DELAY_DEFAULT; |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 116 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 117 | // Device link discovery helpers. |
| 118 | protected final Map<DeviceId, LinkDiscovery> discoverers = new ConcurrentHashMap<>(); |
| 119 | |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 120 | private final LinkDiscoveryContext context = new InternalDiscoveryContext(); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 121 | |
| 122 | private LinkProviderService providerService; |
| 123 | |
| 124 | private static final String PROVIDER_NAME = |
| 125 | "org.onosproject.provider.netcfglinks"; |
| 126 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 127 | |
| 128 | private ApplicationId appId; |
| 129 | private final InternalPacketProcessor packetProcessor = new InternalPacketProcessor(); |
| 130 | private final InternalDeviceListener deviceListener = new InternalDeviceListener(); |
| 131 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 132 | |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 133 | protected Set<LinkKey> configuredLinks = new HashSet<>(); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 134 | |
| 135 | public NetworkConfigLinksProvider() { |
| 136 | super(new ProviderId("lldp", PROVIDER_NAME)); |
| 137 | } |
| 138 | |
Sho SHIMIZU | 9efeb81 | 2016-08-18 09:29:20 -0700 | [diff] [blame] | 139 | private String buildSrcMac() { |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 140 | String srcMac = ProbedLinkProvider.fingerprintMac(metadataService.getClusterMetadata()); |
| 141 | String defMac = ProbedLinkProvider.defaultMac(); |
| 142 | if (srcMac.equals(defMac)) { |
| 143 | log.warn("Couldn't generate fingerprint. Using default value {}", defMac); |
| 144 | return defMac; |
| 145 | } |
| 146 | log.trace("Generated MAC address {}", srcMac); |
| 147 | return srcMac; |
| 148 | } |
| 149 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 150 | private void createLinks() { |
| 151 | netCfgService.getSubjects(LinkKey.class) |
| 152 | .forEach(linkKey -> configuredLinks.add(linkKey)); |
| 153 | } |
| 154 | |
| 155 | @Activate |
| 156 | protected void activate() { |
| 157 | log.info("Activated"); |
| 158 | appId = coreService.registerApplication(PROVIDER_NAME); |
| 159 | packetService.addProcessor(packetProcessor, PacketProcessor.advisor(0)); |
| 160 | providerService = providerRegistry.register(this); |
| 161 | deviceService.addListener(deviceListener); |
| 162 | netCfgService.addListener(cfgListener); |
| 163 | requestIntercepts(); |
| 164 | loadDevices(); |
| 165 | createLinks(); |
| 166 | } |
| 167 | |
| 168 | @Deactivate |
| 169 | protected void deactivate() { |
| 170 | withdrawIntercepts(); |
| 171 | providerRegistry.unregister(this); |
Deepa Vaddireddy | 21f5ae8 | 2017-05-10 18:27:30 +0530 | [diff] [blame] | 172 | deviceService.removeListener(deviceListener); |
| 173 | netCfgService.removeListener(cfgListener); |
| 174 | packetService.removeProcessor(packetProcessor); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 175 | disable(); |
| 176 | log.info("Deactivated"); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Loads available devices and registers their ports to be probed. |
| 181 | */ |
| 182 | private void loadDevices() { |
| 183 | deviceService.getAvailableDevices() |
| 184 | .forEach(d -> updateDevice(d) |
| 185 | .ifPresent(ld -> updatePorts(ld, d.id()))); |
| 186 | } |
| 187 | |
| 188 | private Optional<LinkDiscovery> updateDevice(Device device) { |
| 189 | if (device == null) { |
| 190 | return Optional.empty(); |
| 191 | } |
| 192 | |
| 193 | LinkDiscovery ld = discoverers.computeIfAbsent(device.id(), |
DongRyeol Cha | c80570b | 2018-11-07 11:55:32 +0900 | [diff] [blame] | 194 | did -> new LinkDiscovery(device.id(), context)); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 195 | if (ld.isStopped()) { |
| 196 | ld.start(); |
| 197 | } |
| 198 | return Optional.of(ld); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Updates ports of the specified device to the specified discovery helper. |
| 203 | */ |
| 204 | private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) { |
| 205 | deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p)); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | private void updatePort(LinkDiscovery discoverer, Port port) { |
| 210 | if (port == null) { |
| 211 | return; |
| 212 | } |
| 213 | if (port.number().isLogical()) { |
| 214 | // silently ignore logical ports |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | discoverer.addPort(port); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Disables link discovery processing. |
| 223 | */ |
| 224 | private void disable() { |
| 225 | |
| 226 | providerRegistry.unregister(this); |
| 227 | discoverers.values().forEach(LinkDiscovery::stop); |
| 228 | discoverers.clear(); |
| 229 | |
| 230 | providerService = null; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Provides processing context for the device link discovery helpers. |
| 235 | */ |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 236 | private class InternalDiscoveryContext implements LinkDiscoveryContext { |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 237 | @Override |
| 238 | public MastershipService mastershipService() { |
| 239 | return masterService; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public LinkProviderService providerService() { |
| 244 | return providerService; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public PacketService packetService() { |
| 249 | return packetService; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public long probeRate() { |
| 254 | return probeRate; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public boolean useBddp() { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public void touchLink(LinkKey key) { |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 267 | public void setTtl(LinkKey key, short ttl) { |
| 268 | } |
| 269 | |
| 270 | @Override |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 271 | public String fingerprint() { |
Ayaka Koshibe | 4822922 | 2016-05-16 18:04:26 -0700 | [diff] [blame] | 272 | return buildSrcMac(); |
Ray Milkey | 957390e | 2016-02-09 10:02:46 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public DeviceService deviceService() { |
| 277 | return deviceService; |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 278 | } |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 279 | |
| 280 | @Override |
| 281 | public String lldpSecret() { |
| 282 | return metadataService.getClusterMetadata().getClusterSecret(); |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public long maxDiscoveryDelay() { |
Ray Milkey | bd508ed | 2019-03-19 14:22:02 -0700 | [diff] [blame] | 287 | return maxLldpAge; |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 288 | } |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 291 | // true if *NOT* this cluster's own probe. |
| 292 | private boolean isOthercluster(String mac) { |
| 293 | // if we are using DEFAULT_MAC, clustering hadn't initialized, so conservative 'yes' |
| 294 | String ourMac = context.fingerprint(); |
| 295 | if (ProbedLinkProvider.defaultMac().equalsIgnoreCase(ourMac)) { |
| 296 | return true; |
| 297 | } |
| 298 | return !mac.equalsIgnoreCase(ourMac); |
| 299 | } |
| 300 | |
| 301 | //doesn't validate. Used just to decide if this is expected link. |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 302 | LinkKey extractLinkKey(PacketContext packetContext) { |
| 303 | Ethernet eth = packetContext.inPacket().parsed(); |
| 304 | if (eth == null) { |
| 305 | return null; |
| 306 | } |
| 307 | |
| 308 | ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth); |
| 309 | if (onoslldp != null) { |
| 310 | PortNumber srcPort = portNumber(onoslldp.getPort()); |
| 311 | PortNumber dstPort = packetContext.inPacket().receivedFrom().port(); |
| 312 | DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString()); |
| 313 | DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId(); |
| 314 | |
| 315 | ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort); |
| 316 | ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort); |
| 317 | return LinkKey.linkKey(src, dst); |
| 318 | } |
| 319 | return null; |
| 320 | } |
| 321 | |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 322 | private boolean verify(PacketContext packetContext) { |
| 323 | Ethernet eth = packetContext.inPacket().parsed(); |
| 324 | if (eth == null) { |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth); |
| 329 | if (onoslldp != null) { |
| 330 | if (!isOthercluster(eth.getSourceMAC().toString())) { |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | if (!ONOSLLDP.verify(onoslldp, context.lldpSecret(), context.maxDiscoveryDelay())) { |
| 335 | log.warn("LLDP Packet failed to validate!"); |
| 336 | return false; |
| 337 | } |
| 338 | return true; |
| 339 | } |
| 340 | return false; |
| 341 | } |
| 342 | |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 343 | /** |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 344 | * Removes after stopping discovery helper for specified device. |
| 345 | * @param deviceId device to remove |
| 346 | */ |
| 347 | private void removeDevice(final DeviceId deviceId) { |
| 348 | discoverers.computeIfPresent(deviceId, (did, ld) -> { |
| 349 | ld.stop(); |
| 350 | return null; |
| 351 | }); |
| 352 | |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Removes a port from the specified discovery helper. |
| 357 | * @param port the port |
| 358 | */ |
| 359 | private void removePort(Port port) { |
| 360 | if (port.element() instanceof Device) { |
| 361 | Device d = (Device) port.element(); |
| 362 | LinkDiscovery ld = discoverers.get(d.id()); |
| 363 | if (ld != null) { |
| 364 | ld.removePort(port.number()); |
| 365 | } |
| 366 | } else { |
| 367 | log.warn("Attempted to remove non-Device port", port); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | |
| 372 | /** |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 373 | * Processes incoming packets. |
| 374 | */ |
| 375 | private class InternalPacketProcessor implements PacketProcessor { |
| 376 | @Override |
| 377 | public void process(PacketContext context) { |
| 378 | if (context == null || context.isHandled()) { |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | Ethernet eth = context.inPacket().parsed(); |
| 383 | if (eth == null || (eth.getEtherType() != TYPE_LLDP && eth.getEtherType() != TYPE_BSN)) { |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | InboundPacket inPacket = context.inPacket(); |
| 388 | LinkKey linkKey = extractLinkKey(context); |
| 389 | if (linkKey != null) { |
| 390 | if (configuredLinks.contains(linkKey)) { |
| 391 | log.debug("Found configured link {}", linkKey); |
| 392 | LinkDiscovery ld = discoverers.get(inPacket.receivedFrom().deviceId()); |
| 393 | if (ld == null) { |
| 394 | return; |
| 395 | } |
| 396 | if (ld.handleLldp(context)) { |
| 397 | context.block(); |
| 398 | } |
| 399 | } else { |
Samuel Jero | 31e16f5 | 2018-09-21 10:34:28 -0400 | [diff] [blame] | 400 | if (verify(context)) { |
| 401 | log.debug("Found link that was not in the configuration {}", linkKey); |
| 402 | providerService.linkDetected( |
| 403 | new DefaultLinkDescription(linkKey.src(), |
| 404 | linkKey.dst(), |
| 405 | Link.Type.DIRECT, |
| 406 | DefaultLinkDescription.NOT_EXPECTED, |
| 407 | DefaultAnnotations.EMPTY)); |
| 408 | } |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Requests packet intercepts. |
| 416 | */ |
| 417 | private void requestIntercepts() { |
| 418 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 419 | selector.matchEthType(TYPE_LLDP); |
| 420 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, |
| 421 | appId, Optional.empty()); |
| 422 | |
| 423 | selector.matchEthType(TYPE_BSN); |
| 424 | |
| 425 | packetService.requestPackets(selector.build(), PacketPriority.CONTROL, |
| 426 | appId, Optional.empty()); |
| 427 | |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Withdraws packet intercepts. |
| 432 | */ |
| 433 | private void withdrawIntercepts() { |
| 434 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 435 | selector.matchEthType(TYPE_LLDP); |
| 436 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, |
| 437 | appId, Optional.empty()); |
| 438 | selector.matchEthType(TYPE_BSN); |
| 439 | packetService.cancelPackets(selector.build(), PacketPriority.CONTROL, |
| 440 | appId, Optional.empty()); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Processes device events. |
| 445 | */ |
| 446 | private class InternalDeviceListener implements DeviceListener { |
| 447 | @Override |
| 448 | public void event(DeviceEvent event) { |
| 449 | if (event.type() == DeviceEvent.Type.PORT_STATS_UPDATED) { |
| 450 | return; |
| 451 | } |
| 452 | Device device = event.subject(); |
| 453 | Port port = event.port(); |
| 454 | if (device == null) { |
| 455 | log.error("Device is null."); |
| 456 | return; |
| 457 | } |
| 458 | log.trace("{} {} {}", event.type(), event.subject(), event); |
| 459 | final DeviceId deviceId = device.id(); |
| 460 | switch (event.type()) { |
| 461 | case DEVICE_ADDED: |
| 462 | case DEVICE_UPDATED: |
| 463 | updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId)); |
| 464 | break; |
| 465 | case PORT_ADDED: |
| 466 | case PORT_UPDATED: |
| 467 | if (port.isEnabled()) { |
| 468 | updateDevice(device).ifPresent(ld -> updatePort(ld, port)); |
| 469 | } else { |
| 470 | log.debug("Port down {}", port); |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 471 | removePort(port); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 472 | providerService.linksVanished(new ConnectPoint(port.element().id(), |
| 473 | port.number())); |
| 474 | } |
| 475 | break; |
| 476 | case PORT_REMOVED: |
| 477 | log.debug("Port removed {}", port); |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 478 | removePort(port); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 479 | providerService.linksVanished(new ConnectPoint(port.element().id(), |
| 480 | port.number())); |
| 481 | break; |
| 482 | case DEVICE_REMOVED: |
| 483 | case DEVICE_SUSPENDED: |
| 484 | log.debug("Device removed {}", deviceId); |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 485 | removeDevice(deviceId); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 486 | providerService.linksVanished(deviceId); |
| 487 | break; |
| 488 | case DEVICE_AVAILABILITY_CHANGED: |
| 489 | if (deviceService.isAvailable(deviceId)) { |
| 490 | log.debug("Device up {}", deviceId); |
| 491 | updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId)); |
| 492 | } else { |
| 493 | log.debug("Device down {}", deviceId); |
Ray Milkey | cd6ab18 | 2016-02-03 11:13:09 -0800 | [diff] [blame] | 494 | removeDevice(deviceId); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 495 | providerService.linksVanished(deviceId); |
| 496 | } |
| 497 | break; |
| 498 | case PORT_STATS_UPDATED: |
| 499 | break; |
| 500 | default: |
| 501 | log.debug("Unknown event {}", event); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | private class InternalConfigListener implements NetworkConfigListener { |
| 507 | |
| 508 | private void addLink(LinkKey linkKey) { |
| 509 | configuredLinks.add(linkKey); |
| 510 | } |
| 511 | |
| 512 | private void removeLink(LinkKey linkKey) { |
| 513 | DefaultLinkDescription linkDescription = |
| 514 | new DefaultLinkDescription(linkKey.src(), linkKey.dst(), |
| 515 | Link.Type.DIRECT); |
| 516 | configuredLinks.remove(linkKey); |
| 517 | providerService.linkVanished(linkDescription); |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void event(NetworkConfigEvent event) { |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 522 | if (event.configClass().equals(BasicLinkConfig.class)) { |
| 523 | log.info("net config event of type {} for basic link {}", |
| 524 | event.type(), event.subject()); |
| 525 | LinkKey linkKey = (LinkKey) event.subject(); |
| 526 | if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) { |
| 527 | addLink(linkKey); |
| 528 | } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) { |
| 529 | removeLink(linkKey); |
| 530 | } |
Charles Chan | e527eff | 2016-05-18 14:04:57 -0700 | [diff] [blame] | 531 | log.info("Link reconfigured"); |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 532 | } |
Ray Milkey | b7f0f64 | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | } |