2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.provider.lldp.impl;
18 import com.google.common.collect.Maps;
19 import com.google.common.collect.Sets;
20 import org.jboss.netty.util.Timeout;
21 import org.jboss.netty.util.TimerTask;
22 import org.onlab.packet.Ethernet;
23 import org.onlab.packet.ONOSLLDP;
24 import org.onlab.util.Timer;
25 import org.onosproject.mastership.MastershipService;
26 import org.onosproject.net.ConnectPoint;
27 import org.onosproject.net.Device;
28 import org.onosproject.net.DeviceId;
29 import org.onosproject.net.Link.Type;
30 import org.onosproject.net.Port;
31 import org.onosproject.net.PortNumber;
32 import org.onosproject.net.link.DefaultLinkDescription;
33 import org.onosproject.net.link.LinkDescription;
34 import org.onosproject.net.link.LinkProviderService;
35 import org.onosproject.net.packet.DefaultOutboundPacket;
36 import org.onosproject.net.packet.OutboundPacket;
37 import org.onosproject.net.packet.PacketContext;
38 import org.onosproject.net.packet.PacketService;
39 import org.slf4j.Logger;
41 import java.nio.ByteBuffer;
42 import java.util.Iterator;
45 import java.util.concurrent.atomic.AtomicInteger;
47 import static com.google.common.base.Preconditions.checkNotNull;
48 import static java.util.concurrent.TimeUnit.MILLISECONDS;
49 import static org.onosproject.net.PortNumber.portNumber;
50 import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
51 import static org.slf4j.LoggerFactory.getLogger;
53 // TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen for flow_removed messages
54 // FIXME: add ability to track links using port pairs or the link inventory
57 * Run discovery process from a physical switch. Ports are initially labeled as
58 * slow ports. When an LLDP is successfully received, label the remote port as
59 * fast. Every probeRate milliseconds, loop over all fast ports and send an
60 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
61 * discovery implementation.
63 public class LinkDiscovery implements TimerTask {
65 private final Logger log = getLogger(getClass());
67 private static final short MAX_PROBE_COUNT = 3; // probes to send before link is removed
68 private static final short DEFAULT_PROBE_RATE = 3000; // millis
69 private static final String SRC_MAC = "DE:AD:BE:EF:BA:11";
70 private static final String SERVICE_NULL = "Service cannot be null";
72 private final Device device;
74 // send 1 probe every probeRate milliseconds
75 private final long probeRate = DEFAULT_PROBE_RATE;
77 private final Set<Long> slowPorts = Sets.newConcurrentHashSet();
78 // ports, known to have incoming links
79 private final Set<Long> fastPorts = Sets.newConcurrentHashSet();
81 // number of unacknowledged probes per port
82 private final Map<Long, AtomicInteger> portProbeCount = Maps.newHashMap();
84 private final ONOSLLDP lldpPacket;
85 private final Ethernet ethPacket;
86 private Ethernet bddpEth;
87 private final boolean useBDDP;
89 private Timeout timeout;
90 private volatile boolean isStopped;
92 private final LinkProviderService linkProvider;
93 private final PacketService pktService;
94 private final MastershipService mastershipService;
97 * Instantiates discovery manager for the given physical switch. Creates a
98 * generic LLDP packet that will be customized for the port it is sent out on.
99 * Starts the the timer for the discovery process.
101 * @param device the physical switch
102 * @param pktService packet service
103 * @param masterService mastership service
104 * @param providerService link provider service
105 * @param useBDDP flag to also use BDDP for discovery
107 public LinkDiscovery(Device device, PacketService pktService,
108 MastershipService masterService,
109 LinkProviderService providerService, Boolean... useBDDP) {
110 this.device = device;
111 this.linkProvider = checkNotNull(providerService, SERVICE_NULL);
112 this.pktService = checkNotNull(pktService, SERVICE_NULL);
113 this.mastershipService = checkNotNull(masterService, SERVICE_NULL);
115 lldpPacket = new ONOSLLDP();
116 lldpPacket.setChassisId(device.chassisId());
117 lldpPacket.setDevice(device.id().toString());
119 ethPacket = new Ethernet();
120 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
121 ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
122 ethPacket.setPayload(this.lldpPacket);
123 ethPacket.setPad(true);
125 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
127 bddpEth = new Ethernet();
128 bddpEth.setPayload(lldpPacket);
129 bddpEth.setEtherType(Ethernet.TYPE_BSN);
130 bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
131 bddpEth.setPad(true);
132 log.info("Using BDDP to discover network");
137 log.debug("Started discovery manager for switch {}", device.id());
142 * Add physical port port to discovery process.
143 * Send out initial LLDP and label it as slow port.
145 * @param port the port
147 public void addPort(Port port) {
148 boolean newPort = false;
149 synchronized (this) {
150 if (!containsPort(port.number().toLong())) {
152 slowPorts.add(port.number().toLong());
156 boolean isMaster = mastershipService.isLocalMaster(device.id());
157 if (newPort && isMaster) {
158 log.debug("Sending init probe to port {}@{}", port.number().toLong(), device.id());
159 sendProbes(port.number().toLong());
164 * Removes physical port from discovery process.
166 * @param port the port
168 public void removePort(Port port) {
169 // Ignore ports that are not on this switch
170 long portnum = port.number().toLong();
171 synchronized (this) {
172 if (slowPorts.contains(portnum)) {
173 slowPorts.remove(portnum);
175 } else if (fastPorts.contains(portnum)) {
176 fastPorts.remove(portnum);
177 portProbeCount.remove(portnum);
178 // no iterator to update
180 log.warn("Tried to dynamically remove non-existing port {}", portnum);
186 * Method called by remote port to acknowledge receipt of LLDP sent by
187 * this port. If slow port, updates label to fast. If fast port, decrements
188 * number of unacknowledged probes.
190 * @param portNumber the port
192 public void ackProbe(Long portNumber) {
193 synchronized (this) {
194 if (slowPorts.contains(portNumber)) {
195 log.debug("Setting slow port to fast: {}:{}", device.id(), portNumber);
196 slowPorts.remove(portNumber);
197 fastPorts.add(portNumber);
198 portProbeCount.put(portNumber, new AtomicInteger(0));
199 } else if (fastPorts.contains(portNumber)) {
200 portProbeCount.get(portNumber).set(0);
202 log.debug("Got ackProbe for non-existing port: {}", portNumber);
209 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
210 * to port where LLDP originated.
212 * @param context packet context
213 * @return true if handled
215 public boolean handleLLDP(PacketContext context) {
216 Ethernet eth = context.inPacket().parsed();
221 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
222 if (onoslldp != null) {
223 PortNumber srcPort = portNumber(onoslldp.getPort());
224 PortNumber dstPort = context.inPacket().receivedFrom().port();
225 DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
226 DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
227 ackProbe(dstPort.toLong());
229 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
230 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
232 LinkDescription ld = eth.getEtherType() == Ethernet.TYPE_LLDP ?
233 new DefaultLinkDescription(src, dst, Type.DIRECT) :
234 new DefaultLinkDescription(src, dst, Type.INDIRECT);
237 linkProvider.linkDetected(ld);
238 } catch (IllegalStateException e) {
248 * Execute this method every t milliseconds. Loops over all ports
249 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
255 public void run(Timeout t) {
259 if (!mastershipService.isLocalMaster(device.id())) {
262 timeout = Timer.getTimer().newTimeout(this, probeRate, MILLISECONDS);
267 log.trace("Sending probes from {}", device.id());
268 synchronized (this) {
269 Iterator<Long> fastIterator = fastPorts.iterator();
270 while (fastIterator.hasNext()) {
271 long portNumber = fastIterator.next();
272 int probeCount = portProbeCount.get(portNumber).getAndIncrement();
274 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
275 log.trace("Sending fast probe to port {}", portNumber);
276 sendProbes(portNumber);
279 // Link down, demote to slowPorts; update fast and slow ports
280 fastIterator.remove();
281 slowPorts.add(portNumber);
282 portProbeCount.remove(portNumber);
284 ConnectPoint cp = new ConnectPoint(device.id(), portNumber(portNumber));
285 log.debug("Link down -> {}", cp);
286 linkProvider.linksVanished(cp);
290 // send a probe for the next slow port
291 for (long portNumber : slowPorts) {
292 log.trace("Sending slow probe to port {}", portNumber);
293 sendProbes(portNumber);
299 timeout = Timer.getTimer().newTimeout(this, probeRate, MILLISECONDS);
303 public synchronized void stop() {
308 public synchronized void start() {
311 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
313 log.warn("LinkDiscovery started multiple times?");
318 * Creates packet_out LLDP for specified output port.
320 * @param port the port
321 * @return Packet_out message with LLDP data
323 private OutboundPacket createOutBoundLLDP(Long port) {
327 lldpPacket.setPortId(port.intValue());
328 ethPacket.setSourceMACAddress(SRC_MAC);
329 return new DefaultOutboundPacket(device.id(),
330 builder().setOutput(portNumber(port)).build(),
331 ByteBuffer.wrap(ethPacket.serialize()));
335 * Creates packet_out BDDP for specified output port.
337 * @param port the port
338 * @return Packet_out message with LLDP data
340 private OutboundPacket createOutBoundBDDP(Long port) {
344 lldpPacket.setPortId(port.intValue());
345 bddpEth.setSourceMACAddress(SRC_MAC);
346 return new DefaultOutboundPacket(device.id(),
347 builder().setOutput(portNumber(port)).build(),
348 ByteBuffer.wrap(bddpEth.serialize()));
351 private void sendProbes(Long portNumber) {
352 log.trace("Sending probes out to {}@{}", portNumber, device.id());
353 OutboundPacket pkt = createOutBoundLLDP(portNumber);
354 pktService.emit(pkt);
356 OutboundPacket bpkt = createOutBoundBDDP(portNumber);
357 pktService.emit(bpkt);
361 public boolean containsPort(Long portNumber) {
362 return slowPorts.contains(portNumber) || fastPorts.contains(portNumber);
365 public synchronized boolean isStopped() {
366 return isStopped || timeout.isCancelled();