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