blob: 8a060c73c6e94fc646b23e14d003402a985a10a5 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
alshabib7911a052014-10-16 17:49:37 -07009 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
Thomas Vachuska781d18b2014-10-27 10:31:25 -070012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabib7911a052014-10-16 17:49:37 -070019package org.onlab.onos.provider.lldp.impl;
20
21
alshabib3d643ec2014-10-22 18:33:00 -070022import static com.google.common.base.Preconditions.checkNotNull;
alshabib7911a052014-10-16 17:49:37 -070023import static org.slf4j.LoggerFactory.getLogger;
24
25import java.nio.ByteBuffer;
26import java.util.Collections;
27import java.util.HashMap;
28import java.util.HashSet;
29import java.util.Iterator;
30import java.util.Map;
31import java.util.Set;
32import java.util.concurrent.TimeUnit;
33import java.util.concurrent.atomic.AtomicInteger;
34
35import org.jboss.netty.util.Timeout;
36import org.jboss.netty.util.TimerTask;
alshabib875d6262014-10-17 16:19:40 -070037import org.onlab.onos.mastership.MastershipService;
alshabib7911a052014-10-16 17:49:37 -070038import org.onlab.onos.net.ConnectPoint;
39import org.onlab.onos.net.Device;
40import org.onlab.onos.net.DeviceId;
41import org.onlab.onos.net.Link.Type;
alshabib875d6262014-10-17 16:19:40 -070042import org.onlab.onos.net.MastershipRole;
alshabib7911a052014-10-16 17:49:37 -070043import org.onlab.onos.net.Port;
44import org.onlab.onos.net.PortNumber;
45import org.onlab.onos.net.flow.DefaultTrafficTreatment;
46import org.onlab.onos.net.link.DefaultLinkDescription;
47import org.onlab.onos.net.link.LinkDescription;
48import org.onlab.onos.net.link.LinkProviderService;
49import org.onlab.onos.net.packet.DefaultOutboundPacket;
50import org.onlab.onos.net.packet.OutboundPacket;
51import org.onlab.onos.net.packet.PacketContext;
52import org.onlab.onos.net.packet.PacketService;
53import org.onlab.packet.Ethernet;
54import org.onlab.packet.ONOSLLDP;
55import org.onlab.util.Timer;
56import org.slf4j.Logger;
57
58
59
60/**
61 * Run discovery process from a physical switch. Ports are initially labeled as
62 * slow ports. When an LLDP is successfully received, label the remote port as
63 * fast. Every probeRate milliseconds, loop over all fast ports and send an
64 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
65 * discovery implementation.
66 *
67 * TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen
68 * for flow_removed messages
69 */
70public class LinkDiscovery implements TimerTask {
71
72 private final Device device;
73 // send 1 probe every probeRate milliseconds
74 private final long probeRate;
75 private final Set<Long> slowPorts;
76 private final Set<Long> fastPorts;
77 // number of unacknowledged probes per port
78 private final Map<Long, AtomicInteger> portProbeCount;
79 // number of probes to send before link is removed
80 private static final short MAX_PROBE_COUNT = 3;
81 private final Logger log = getLogger(getClass());
82 private final ONOSLLDP lldpPacket;
83 private final Ethernet ethPacket;
84 private Ethernet bddpEth;
85 private final boolean useBDDP;
86 private final LinkProviderService linkProvider;
87 private final PacketService pktService;
alshabib875d6262014-10-17 16:19:40 -070088 private final MastershipService mastershipService;
alshabib7911a052014-10-16 17:49:37 -070089 private Timeout timeout;
alshabib0ed6a202014-10-19 12:42:57 -070090 private boolean isStopped;
alshabib7911a052014-10-16 17:49:37 -070091
92 /**
93 * Instantiates discovery manager for the given physical switch. Creates a
94 * generic LLDP packet that will be customized for the port it is sent out on.
95 * Starts the the timer for the discovery process.
Jonathan Hart43ef46f2014-10-23 08:33:33 -070096 * @param device the physical switch
alshabib875d6262014-10-17 16:19:40 -070097 * @param masterService
alshabib7911a052014-10-16 17:49:37 -070098 * @param useBDDP flag to also use BDDP for discovery
99 */
100 public LinkDiscovery(Device device, PacketService pktService,
alshabib875d6262014-10-17 16:19:40 -0700101 MastershipService masterService, LinkProviderService providerService, Boolean... useBDDP) {
alshabib3d643ec2014-10-22 18:33:00 -0700102
alshabib7911a052014-10-16 17:49:37 -0700103 this.device = device;
104 this.probeRate = 3000;
105 this.linkProvider = providerService;
106 this.pktService = pktService;
alshabib3d643ec2014-10-22 18:33:00 -0700107
108 this.mastershipService = checkNotNull(masterService, "WTF!");
alshabib7911a052014-10-16 17:49:37 -0700109 this.slowPorts = Collections.synchronizedSet(new HashSet<Long>());
110 this.fastPorts = Collections.synchronizedSet(new HashSet<Long>());
111 this.portProbeCount = new HashMap<>();
112 this.lldpPacket = new ONOSLLDP();
113 this.lldpPacket.setChassisId(device.chassisId());
114 this.lldpPacket.setDevice(device.id().toString());
115
116
117 this.ethPacket = new Ethernet();
118 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
119 this.ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
120 this.ethPacket.setPayload(this.lldpPacket);
121 this.ethPacket.setPad(true);
122 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
123 if (this.useBDDP) {
124 this.bddpEth = new Ethernet();
125 this.bddpEth.setPayload(this.lldpPacket);
126 this.bddpEth.setEtherType(Ethernet.TYPE_BSN);
127 this.bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
128 this.bddpEth.setPad(true);
129 log.info("Using BDDP to discover network");
130 }
131
132 start();
133 this.log.debug("Started discovery manager for switch {}",
134 device.id());
135
136 }
137
138 /**
139 * Add physical port port to discovery process.
140 * Send out initial LLDP and label it as slow port.
141 *
142 * @param port the port
143 */
144 public void addPort(final Port port) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700145 this.log.debug("sending init probe to port {}@{}",
146 port.number().toLong(), device.id());
alshabib7911a052014-10-16 17:49:37 -0700147
148 sendProbes(port.number().toLong());
149
150 synchronized (this) {
151 this.slowPorts.add(port.number().toLong());
152 }
153
154
155 }
156
157 /**
158 * Removes physical port from discovery process.
159 *
160 * @param port the port
161 */
162 public void removePort(final Port port) {
163 // Ignore ports that are not on this switch
164
165 long portnum = port.number().toLong();
166 synchronized (this) {
167 if (this.slowPorts.contains(portnum)) {
168 this.slowPorts.remove(portnum);
169
170 } else if (this.fastPorts.contains(portnum)) {
171 this.fastPorts.remove(portnum);
172 this.portProbeCount.remove(portnum);
173 // no iterator to update
174 } else {
175 this.log.warn(
176 "tried to dynamically remove non-existing port {}",
177 portnum);
178 }
179 }
180 }
181
182 /**
183 * Method called by remote port to acknowledge receipt of LLDP sent by
184 * this port. If slow port, updates label to fast. If fast port, decrements
185 * number of unacknowledged probes.
186 *
187 * @param portNumber the port
188 */
189 public void ackProbe(final Long portNumber) {
190
191 synchronized (this) {
192 if (this.slowPorts.contains(portNumber)) {
193 this.log.debug("Setting slow port to fast: {}:{}",
194 this.device.id(), portNumber);
195 this.slowPorts.remove(portNumber);
196 this.fastPorts.add(portNumber);
197 this.portProbeCount.put(portNumber, new AtomicInteger(0));
alshabibacd91832014-10-17 14:38:41 -0700198 } else if (this.fastPorts.contains(portNumber)) {
alshabib7911a052014-10-16 17:49:37 -0700199 this.portProbeCount.get(portNumber).set(0);
alshabibacd91832014-10-17 14:38:41 -0700200 } else {
alshabib7911a052014-10-16 17:49:37 -0700201 this.log.debug(
202 "Got ackProbe for non-existing port: {}",
203 portNumber);
alshabibacd91832014-10-17 14:38:41 -0700204
alshabib7911a052014-10-16 17:49:37 -0700205 }
206 }
207 }
208
209
210 /**
211 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
212 * to port where LLDP originated.
213 */
214 public boolean handleLLDP(PacketContext context) {
215 Ethernet eth = context.inPacket().parsed();
216 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
217 if (onoslldp != null) {
218 final PortNumber dstPort =
219 context.inPacket().receivedFrom().port();
220 final PortNumber srcPort = PortNumber.portNumber(onoslldp.getPort());
221 final DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
222 final DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
Jonathan Hart43ef46f2014-10-23 08:33:33 -0700223 this.ackProbe(dstPort.toLong());
alshabib7911a052014-10-16 17:49:37 -0700224 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
225 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
226
227 LinkDescription ld;
228 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
229 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
230 } else {
231 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
232 }
233 linkProvider.linkDetected(ld);
234 return true;
235 }
236 return false;
237 }
238
239
240
241 /**
242 * Execute this method every t milliseconds. Loops over all ports
243 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
244 * port.
245 *
246 * @param t timeout
247 * @throws Exception
248 */
249 @Override
250 public void run(final Timeout t) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700251 this.log.trace("sending probes from {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700252 synchronized (this) {
253 final Iterator<Long> fastIterator = this.fastPorts.iterator();
254 Long portNumber;
255 Integer probeCount;
256 while (fastIterator.hasNext()) {
257 portNumber = fastIterator.next();
258 probeCount = this.portProbeCount.get(portNumber)
259 .getAndIncrement();
260
261 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700262 this.log.trace("sending fast probe to port {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700263 sendProbes(portNumber);
264 } else {
265 // Update fast and slow ports
alshabibdfc7afb2014-10-21 20:13:27 -0700266 fastIterator.remove();
267 this.slowPorts.add(portNumber);
268 this.portProbeCount.remove(portNumber);
269
alshabib7911a052014-10-16 17:49:37 -0700270
271 ConnectPoint cp = new ConnectPoint(
272 device.id(),
273 PortNumber.portNumber(portNumber));
274 log.debug("Link down -> {}", cp);
275 linkProvider.linksVanished(cp);
276 }
277 }
278
279 // send a probe for the next slow port
280 if (!this.slowPorts.isEmpty()) {
281 Iterator<Long> slowIterator = this.slowPorts.iterator();
282 while (slowIterator.hasNext()) {
283 portNumber = slowIterator.next();
Jonathan Hart43ef46f2014-10-23 08:33:33 -0700284 this.log.trace("sending slow probe to port {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700285
286 sendProbes(portNumber);
287
288 }
289 }
290 }
291
292 // reschedule timer
293 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
294 TimeUnit.MILLISECONDS);
295 }
296
297 public void stop() {
298 timeout.cancel();
alshabib0ed6a202014-10-19 12:42:57 -0700299 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700300 }
301
302 public void start() {
303 timeout = Timer.getTimer().newTimeout(this, 0,
304 TimeUnit.MILLISECONDS);
alshabib0ed6a202014-10-19 12:42:57 -0700305 isStopped = false;
alshabib7911a052014-10-16 17:49:37 -0700306 }
307
308 /**
309 * Creates packet_out LLDP for specified output port.
310 *
311 * @param port the port
312 * @return Packet_out message with LLDP data
313 */
314 private OutboundPacket createOutBoundLLDP(final Long port) {
315 if (port == null) {
316 return null;
317 }
318 this.lldpPacket.setPortId(port.intValue());
319 this.ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
320
321 final byte[] lldp = this.ethPacket.serialize();
322 OutboundPacket outboundPacket = new DefaultOutboundPacket(
323 this.device.id(),
324 DefaultTrafficTreatment.builder().setOutput(
325 PortNumber.portNumber(port)).build(),
326 ByteBuffer.wrap(lldp));
327 return outboundPacket;
328 }
329
330 /**
331 * Creates packet_out BDDP for specified output port.
332 *
333 * @param port the port
334 * @return Packet_out message with LLDP data
335 */
336 private OutboundPacket createOutBoundBDDP(final Long port) {
337 if (port == null) {
338 return null;
339 }
340 this.lldpPacket.setPortId(port.intValue());
341 this.bddpEth.setSourceMACAddress("DE:AD:BE:EF:BA:11");
342
343 final byte[] bddp = this.bddpEth.serialize();
344 OutboundPacket outboundPacket = new DefaultOutboundPacket(
345 this.device.id(),
346 DefaultTrafficTreatment.builder()
347 .setOutput(PortNumber.portNumber(port)).build(),
348 ByteBuffer.wrap(bddp));
349 return outboundPacket;
350 }
351
352 private void sendProbes(Long portNumber) {
alshabib3d643ec2014-10-22 18:33:00 -0700353 if (device == null) {
354 log.warn("CRAZY SHIT");
355 }
356 if (mastershipService == null) {
357 log.warn("INSANE");
358 }
alshabib2374fc92014-10-22 11:03:23 -0700359 if (device.type() != Device.Type.ROADM &&
360 mastershipService.getLocalRole(this.device.id()) ==
alshabib875d6262014-10-17 16:19:40 -0700361 MastershipRole.MASTER) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700362 log.debug("sending probes out to {}@{}", portNumber, device.id());
alshabib875d6262014-10-17 16:19:40 -0700363 OutboundPacket pkt = this.createOutBoundLLDP(portNumber);
364 pktService.emit(pkt);
365 if (useBDDP) {
366 OutboundPacket bpkt = this.createOutBoundBDDP(portNumber);
367 pktService.emit(bpkt);
368 }
369 }
alshabib7911a052014-10-16 17:49:37 -0700370 }
371
alshabib0ed6a202014-10-19 12:42:57 -0700372 public boolean containsPort(Long portNumber) {
373 if (slowPorts.contains(portNumber) || fastPorts.contains(portNumber)) {
374 return true;
375 }
376 return false;
377 }
378
379 public boolean isStopped() {
380 return isStopped;
381 }
382
alshabib7911a052014-10-16 17:49:37 -0700383}