blob: 9bcd8c62f77211efc07e47cc460c642a69b0b9c3 [file] [log] [blame]
alshabib7911a052014-10-16 17:49:37 -07001/*******************************************************************************
2 * Copyright 2014 Open Networking Laboratory
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 ******************************************************************************/
16package org.onlab.onos.provider.lldp.impl;
17
18
alshabib3d643ec2014-10-22 18:33:00 -070019import static com.google.common.base.Preconditions.checkNotNull;
alshabib7911a052014-10-16 17:49:37 -070020import static org.slf4j.LoggerFactory.getLogger;
21
22import java.nio.ByteBuffer;
23import java.util.Collections;
24import java.util.HashMap;
25import java.util.HashSet;
26import java.util.Iterator;
27import java.util.Map;
28import java.util.Set;
29import java.util.concurrent.TimeUnit;
30import java.util.concurrent.atomic.AtomicInteger;
31
32import org.jboss.netty.util.Timeout;
33import org.jboss.netty.util.TimerTask;
alshabib875d6262014-10-17 16:19:40 -070034import org.onlab.onos.mastership.MastershipService;
alshabib7911a052014-10-16 17:49:37 -070035import org.onlab.onos.net.ConnectPoint;
36import org.onlab.onos.net.Device;
37import org.onlab.onos.net.DeviceId;
38import org.onlab.onos.net.Link.Type;
alshabib875d6262014-10-17 16:19:40 -070039import org.onlab.onos.net.MastershipRole;
alshabib7911a052014-10-16 17:49:37 -070040import org.onlab.onos.net.Port;
41import org.onlab.onos.net.PortNumber;
42import org.onlab.onos.net.flow.DefaultTrafficTreatment;
43import org.onlab.onos.net.link.DefaultLinkDescription;
44import org.onlab.onos.net.link.LinkDescription;
45import org.onlab.onos.net.link.LinkProviderService;
46import org.onlab.onos.net.packet.DefaultOutboundPacket;
47import org.onlab.onos.net.packet.OutboundPacket;
48import org.onlab.onos.net.packet.PacketContext;
49import org.onlab.onos.net.packet.PacketService;
50import org.onlab.packet.Ethernet;
51import org.onlab.packet.ONOSLLDP;
52import org.onlab.util.Timer;
53import org.slf4j.Logger;
54
55
56
57/**
58 * Run discovery process from a physical switch. Ports are initially labeled as
59 * slow ports. When an LLDP is successfully received, label the remote port as
60 * fast. Every probeRate milliseconds, loop over all fast ports and send an
61 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
62 * discovery implementation.
63 *
64 * TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen
65 * for flow_removed messages
66 */
67public class LinkDiscovery implements TimerTask {
68
69 private final Device device;
70 // send 1 probe every probeRate milliseconds
71 private final long probeRate;
72 private final Set<Long> slowPorts;
73 private final Set<Long> fastPorts;
74 // number of unacknowledged probes per port
75 private final Map<Long, AtomicInteger> portProbeCount;
76 // number of probes to send before link is removed
77 private static final short MAX_PROBE_COUNT = 3;
78 private final Logger log = getLogger(getClass());
79 private final ONOSLLDP lldpPacket;
80 private final Ethernet ethPacket;
81 private Ethernet bddpEth;
82 private final boolean useBDDP;
83 private final LinkProviderService linkProvider;
84 private final PacketService pktService;
alshabib875d6262014-10-17 16:19:40 -070085 private final MastershipService mastershipService;
alshabib7911a052014-10-16 17:49:37 -070086 private Timeout timeout;
alshabib0ed6a202014-10-19 12:42:57 -070087 private boolean isStopped;
alshabib7911a052014-10-16 17:49:37 -070088
89 /**
90 * Instantiates discovery manager for the given physical switch. Creates a
91 * generic LLDP packet that will be customized for the port it is sent out on.
92 * Starts the the timer for the discovery process.
Jonathan Hart43ef46f2014-10-23 08:33:33 -070093 * @param device the physical switch
alshabib875d6262014-10-17 16:19:40 -070094 * @param masterService
alshabib7911a052014-10-16 17:49:37 -070095 * @param useBDDP flag to also use BDDP for discovery
96 */
97 public LinkDiscovery(Device device, PacketService pktService,
alshabib875d6262014-10-17 16:19:40 -070098 MastershipService masterService, LinkProviderService providerService, Boolean... useBDDP) {
alshabib3d643ec2014-10-22 18:33:00 -070099
alshabib7911a052014-10-16 17:49:37 -0700100 this.device = device;
101 this.probeRate = 3000;
102 this.linkProvider = providerService;
103 this.pktService = pktService;
alshabib3d643ec2014-10-22 18:33:00 -0700104
105 this.mastershipService = checkNotNull(masterService, "WTF!");
alshabib7911a052014-10-16 17:49:37 -0700106 this.slowPorts = Collections.synchronizedSet(new HashSet<Long>());
107 this.fastPorts = Collections.synchronizedSet(new HashSet<Long>());
108 this.portProbeCount = new HashMap<>();
109 this.lldpPacket = new ONOSLLDP();
110 this.lldpPacket.setChassisId(device.chassisId());
111 this.lldpPacket.setDevice(device.id().toString());
112
113
114 this.ethPacket = new Ethernet();
115 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
116 this.ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
117 this.ethPacket.setPayload(this.lldpPacket);
118 this.ethPacket.setPad(true);
119 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
120 if (this.useBDDP) {
121 this.bddpEth = new Ethernet();
122 this.bddpEth.setPayload(this.lldpPacket);
123 this.bddpEth.setEtherType(Ethernet.TYPE_BSN);
124 this.bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
125 this.bddpEth.setPad(true);
126 log.info("Using BDDP to discover network");
127 }
128
129 start();
130 this.log.debug("Started discovery manager for switch {}",
131 device.id());
132
133 }
134
135 /**
136 * Add physical port port to discovery process.
137 * Send out initial LLDP and label it as slow port.
138 *
139 * @param port the port
140 */
141 public void addPort(final Port port) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700142 this.log.debug("sending init probe to port {}@{}",
143 port.number().toLong(), device.id());
alshabib7911a052014-10-16 17:49:37 -0700144
145 sendProbes(port.number().toLong());
146
147 synchronized (this) {
148 this.slowPorts.add(port.number().toLong());
149 }
150
151
152 }
153
154 /**
155 * Removes physical port from discovery process.
156 *
157 * @param port the port
158 */
159 public void removePort(final Port port) {
160 // Ignore ports that are not on this switch
161
162 long portnum = port.number().toLong();
163 synchronized (this) {
164 if (this.slowPorts.contains(portnum)) {
165 this.slowPorts.remove(portnum);
166
167 } else if (this.fastPorts.contains(portnum)) {
168 this.fastPorts.remove(portnum);
169 this.portProbeCount.remove(portnum);
170 // no iterator to update
171 } else {
172 this.log.warn(
173 "tried to dynamically remove non-existing port {}",
174 portnum);
175 }
176 }
177 }
178
179 /**
180 * Method called by remote port to acknowledge receipt of LLDP sent by
181 * this port. If slow port, updates label to fast. If fast port, decrements
182 * number of unacknowledged probes.
183 *
184 * @param portNumber the port
185 */
186 public void ackProbe(final Long portNumber) {
187
188 synchronized (this) {
189 if (this.slowPorts.contains(portNumber)) {
190 this.log.debug("Setting slow port to fast: {}:{}",
191 this.device.id(), portNumber);
192 this.slowPorts.remove(portNumber);
193 this.fastPorts.add(portNumber);
194 this.portProbeCount.put(portNumber, new AtomicInteger(0));
alshabibacd91832014-10-17 14:38:41 -0700195 } else if (this.fastPorts.contains(portNumber)) {
alshabib7911a052014-10-16 17:49:37 -0700196 this.portProbeCount.get(portNumber).set(0);
alshabibacd91832014-10-17 14:38:41 -0700197 } else {
alshabib7911a052014-10-16 17:49:37 -0700198 this.log.debug(
199 "Got ackProbe for non-existing port: {}",
200 portNumber);
alshabibacd91832014-10-17 14:38:41 -0700201
alshabib7911a052014-10-16 17:49:37 -0700202 }
203 }
204 }
205
206
207 /**
208 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
209 * to port where LLDP originated.
210 */
211 public boolean handleLLDP(PacketContext context) {
212 Ethernet eth = context.inPacket().parsed();
213 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
214 if (onoslldp != null) {
215 final PortNumber dstPort =
216 context.inPacket().receivedFrom().port();
217 final PortNumber srcPort = PortNumber.portNumber(onoslldp.getPort());
218 final DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
219 final DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
Jonathan Hart43ef46f2014-10-23 08:33:33 -0700220 this.ackProbe(dstPort.toLong());
alshabib7911a052014-10-16 17:49:37 -0700221 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
222 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
223
224 LinkDescription ld;
225 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
226 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
227 } else {
228 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
229 }
230 linkProvider.linkDetected(ld);
231 return true;
232 }
233 return false;
234 }
235
236
237
238 /**
239 * Execute this method every t milliseconds. Loops over all ports
240 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
241 * port.
242 *
243 * @param t timeout
244 * @throws Exception
245 */
246 @Override
247 public void run(final Timeout t) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700248 this.log.trace("sending probes from {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700249 synchronized (this) {
250 final Iterator<Long> fastIterator = this.fastPorts.iterator();
251 Long portNumber;
252 Integer probeCount;
253 while (fastIterator.hasNext()) {
254 portNumber = fastIterator.next();
255 probeCount = this.portProbeCount.get(portNumber)
256 .getAndIncrement();
257
258 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700259 this.log.trace("sending fast probe to port {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700260 sendProbes(portNumber);
261 } else {
262 // Update fast and slow ports
alshabibdfc7afb2014-10-21 20:13:27 -0700263 fastIterator.remove();
264 this.slowPorts.add(portNumber);
265 this.portProbeCount.remove(portNumber);
266
alshabib7911a052014-10-16 17:49:37 -0700267
268 ConnectPoint cp = new ConnectPoint(
269 device.id(),
270 PortNumber.portNumber(portNumber));
271 log.debug("Link down -> {}", cp);
272 linkProvider.linksVanished(cp);
273 }
274 }
275
276 // send a probe for the next slow port
277 if (!this.slowPorts.isEmpty()) {
278 Iterator<Long> slowIterator = this.slowPorts.iterator();
279 while (slowIterator.hasNext()) {
280 portNumber = slowIterator.next();
Jonathan Hart43ef46f2014-10-23 08:33:33 -0700281 this.log.trace("sending slow probe to port {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700282
283 sendProbes(portNumber);
284
285 }
286 }
287 }
288
289 // reschedule timer
290 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
291 TimeUnit.MILLISECONDS);
292 }
293
294 public void stop() {
295 timeout.cancel();
alshabib0ed6a202014-10-19 12:42:57 -0700296 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700297 }
298
299 public void start() {
300 timeout = Timer.getTimer().newTimeout(this, 0,
301 TimeUnit.MILLISECONDS);
alshabib0ed6a202014-10-19 12:42:57 -0700302 isStopped = false;
alshabib7911a052014-10-16 17:49:37 -0700303 }
304
305 /**
306 * Creates packet_out LLDP for specified output port.
307 *
308 * @param port the port
309 * @return Packet_out message with LLDP data
310 */
311 private OutboundPacket createOutBoundLLDP(final Long port) {
312 if (port == null) {
313 return null;
314 }
315 this.lldpPacket.setPortId(port.intValue());
316 this.ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
317
318 final byte[] lldp = this.ethPacket.serialize();
319 OutboundPacket outboundPacket = new DefaultOutboundPacket(
320 this.device.id(),
321 DefaultTrafficTreatment.builder().setOutput(
322 PortNumber.portNumber(port)).build(),
323 ByteBuffer.wrap(lldp));
324 return outboundPacket;
325 }
326
327 /**
328 * Creates packet_out BDDP for specified output port.
329 *
330 * @param port the port
331 * @return Packet_out message with LLDP data
332 */
333 private OutboundPacket createOutBoundBDDP(final Long port) {
334 if (port == null) {
335 return null;
336 }
337 this.lldpPacket.setPortId(port.intValue());
338 this.bddpEth.setSourceMACAddress("DE:AD:BE:EF:BA:11");
339
340 final byte[] bddp = this.bddpEth.serialize();
341 OutboundPacket outboundPacket = new DefaultOutboundPacket(
342 this.device.id(),
343 DefaultTrafficTreatment.builder()
344 .setOutput(PortNumber.portNumber(port)).build(),
345 ByteBuffer.wrap(bddp));
346 return outboundPacket;
347 }
348
349 private void sendProbes(Long portNumber) {
alshabib3d643ec2014-10-22 18:33:00 -0700350 if (device == null) {
351 log.warn("CRAZY SHIT");
352 }
353 if (mastershipService == null) {
354 log.warn("INSANE");
355 }
alshabib2374fc92014-10-22 11:03:23 -0700356 if (device.type() != Device.Type.ROADM &&
357 mastershipService.getLocalRole(this.device.id()) ==
alshabib875d6262014-10-17 16:19:40 -0700358 MastershipRole.MASTER) {
Yuta HIGUCHIeb24e9d2014-10-26 19:34:20 -0700359 log.debug("sending probes out to {}@{}", portNumber, device.id());
alshabib875d6262014-10-17 16:19:40 -0700360 OutboundPacket pkt = this.createOutBoundLLDP(portNumber);
361 pktService.emit(pkt);
362 if (useBDDP) {
363 OutboundPacket bpkt = this.createOutBoundBDDP(portNumber);
364 pktService.emit(bpkt);
365 }
366 }
alshabib7911a052014-10-16 17:49:37 -0700367 }
368
alshabib0ed6a202014-10-19 12:42:57 -0700369 public boolean containsPort(Long portNumber) {
370 if (slowPorts.contains(portNumber) || fastPorts.contains(portNumber)) {
371 return true;
372 }
373 return false;
374 }
375
376 public boolean isStopped() {
377 return isStopped;
378 }
379
alshabib7911a052014-10-16 17:49:37 -0700380}