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