blob: 38624373a04f4671a2926f5782c4c4f2b98996cf [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
19import static org.slf4j.LoggerFactory.getLogger;
20
21import java.nio.ByteBuffer;
22import java.util.Collections;
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.Iterator;
26import java.util.Map;
27import java.util.Set;
28import java.util.concurrent.TimeUnit;
29import java.util.concurrent.atomic.AtomicInteger;
30
31import org.jboss.netty.util.Timeout;
32import org.jboss.netty.util.TimerTask;
alshabib875d6262014-10-17 16:19:40 -070033import org.onlab.onos.mastership.MastershipService;
alshabib7911a052014-10-16 17:49:37 -070034import org.onlab.onos.net.ConnectPoint;
35import org.onlab.onos.net.Device;
36import org.onlab.onos.net.DeviceId;
37import org.onlab.onos.net.Link.Type;
alshabib875d6262014-10-17 16:19:40 -070038import org.onlab.onos.net.MastershipRole;
alshabib7911a052014-10-16 17:49:37 -070039import org.onlab.onos.net.Port;
40import org.onlab.onos.net.PortNumber;
41import org.onlab.onos.net.flow.DefaultTrafficTreatment;
42import org.onlab.onos.net.link.DefaultLinkDescription;
43import org.onlab.onos.net.link.LinkDescription;
44import org.onlab.onos.net.link.LinkProviderService;
45import org.onlab.onos.net.packet.DefaultOutboundPacket;
46import org.onlab.onos.net.packet.OutboundPacket;
47import org.onlab.onos.net.packet.PacketContext;
48import org.onlab.onos.net.packet.PacketService;
49import org.onlab.packet.Ethernet;
50import org.onlab.packet.ONOSLLDP;
51import org.onlab.util.Timer;
52import org.slf4j.Logger;
53
54
55
56/**
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.
62 *
63 * TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen
64 * for flow_removed messages
65 */
66public class LinkDiscovery implements TimerTask {
67
68 private final Device device;
69 // send 1 probe every probeRate milliseconds
70 private final long probeRate;
71 private final Set<Long> slowPorts;
72 private final Set<Long> fastPorts;
73 // number of unacknowledged probes per port
74 private final Map<Long, AtomicInteger> portProbeCount;
75 // number of probes to send before link is removed
76 private static final short MAX_PROBE_COUNT = 3;
77 private final Logger log = getLogger(getClass());
78 private final ONOSLLDP lldpPacket;
79 private final Ethernet ethPacket;
80 private Ethernet bddpEth;
81 private final boolean useBDDP;
82 private final LinkProviderService linkProvider;
83 private final PacketService pktService;
alshabib875d6262014-10-17 16:19:40 -070084 private final MastershipService mastershipService;
alshabib7911a052014-10-16 17:49:37 -070085 private Timeout timeout;
alshabib0ed6a202014-10-19 12:42:57 -070086 private boolean isStopped;
alshabib7911a052014-10-16 17:49:37 -070087
88 /**
89 * Instantiates discovery manager for the given physical switch. Creates a
90 * generic LLDP packet that will be customized for the port it is sent out on.
91 * Starts the the timer for the discovery process.
alshabib875d6262014-10-17 16:19:40 -070092 * @param device the physical switch
93 * @param masterService
alshabib7911a052014-10-16 17:49:37 -070094 * @param useBDDP flag to also use BDDP for discovery
95 */
96 public LinkDiscovery(Device device, PacketService pktService,
alshabib875d6262014-10-17 16:19:40 -070097 MastershipService masterService, LinkProviderService providerService, Boolean... useBDDP) {
alshabib7911a052014-10-16 17:49:37 -070098 this.device = device;
99 this.probeRate = 3000;
100 this.linkProvider = providerService;
101 this.pktService = pktService;
alshabib875d6262014-10-17 16:19:40 -0700102 this.mastershipService = masterService;
alshabib7911a052014-10-16 17:49:37 -0700103 this.slowPorts = Collections.synchronizedSet(new HashSet<Long>());
104 this.fastPorts = Collections.synchronizedSet(new HashSet<Long>());
105 this.portProbeCount = new HashMap<>();
106 this.lldpPacket = new ONOSLLDP();
107 this.lldpPacket.setChassisId(device.chassisId());
108 this.lldpPacket.setDevice(device.id().toString());
109
110
111 this.ethPacket = new Ethernet();
112 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
113 this.ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
114 this.ethPacket.setPayload(this.lldpPacket);
115 this.ethPacket.setPad(true);
116 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
117 if (this.useBDDP) {
118 this.bddpEth = new Ethernet();
119 this.bddpEth.setPayload(this.lldpPacket);
120 this.bddpEth.setEtherType(Ethernet.TYPE_BSN);
121 this.bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
122 this.bddpEth.setPad(true);
123 log.info("Using BDDP to discover network");
124 }
125
126 start();
127 this.log.debug("Started discovery manager for switch {}",
128 device.id());
129
130 }
131
132 /**
133 * Add physical port port to discovery process.
134 * Send out initial LLDP and label it as slow port.
135 *
136 * @param port the port
137 */
138 public void addPort(final Port port) {
139 this.log.debug("sending init probe to port {}",
140 port.number().toLong());
141
142 sendProbes(port.number().toLong());
143
144 synchronized (this) {
145 this.slowPorts.add(port.number().toLong());
146 }
147
148
149 }
150
151 /**
152 * Removes physical port from discovery process.
153 *
154 * @param port the port
155 */
156 public void removePort(final Port port) {
157 // Ignore ports that are not on this switch
158
159 long portnum = port.number().toLong();
160 synchronized (this) {
161 if (this.slowPorts.contains(portnum)) {
162 this.slowPorts.remove(portnum);
163
164 } else if (this.fastPorts.contains(portnum)) {
165 this.fastPorts.remove(portnum);
166 this.portProbeCount.remove(portnum);
167 // no iterator to update
168 } else {
169 this.log.warn(
170 "tried to dynamically remove non-existing port {}",
171 portnum);
172 }
173 }
174 }
175
176 /**
177 * Method called by remote port to acknowledge receipt of LLDP sent by
178 * this port. If slow port, updates label to fast. If fast port, decrements
179 * number of unacknowledged probes.
180 *
181 * @param portNumber the port
182 */
183 public void ackProbe(final Long portNumber) {
184
185 synchronized (this) {
186 if (this.slowPorts.contains(portNumber)) {
187 this.log.debug("Setting slow port to fast: {}:{}",
188 this.device.id(), portNumber);
189 this.slowPorts.remove(portNumber);
190 this.fastPorts.add(portNumber);
191 this.portProbeCount.put(portNumber, new AtomicInteger(0));
alshabibacd91832014-10-17 14:38:41 -0700192 } else if (this.fastPorts.contains(portNumber)) {
alshabib7911a052014-10-16 17:49:37 -0700193 this.portProbeCount.get(portNumber).set(0);
alshabibacd91832014-10-17 14:38:41 -0700194 } else {
alshabib7911a052014-10-16 17:49:37 -0700195 this.log.debug(
196 "Got ackProbe for non-existing port: {}",
197 portNumber);
alshabibacd91832014-10-17 14:38:41 -0700198
alshabib7911a052014-10-16 17:49:37 -0700199 }
200 }
201 }
202
203
204 /**
205 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
206 * to port where LLDP originated.
207 */
208 public boolean handleLLDP(PacketContext context) {
209 Ethernet eth = context.inPacket().parsed();
210 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
211 if (onoslldp != null) {
212 final PortNumber dstPort =
213 context.inPacket().receivedFrom().port();
214 final PortNumber srcPort = PortNumber.portNumber(onoslldp.getPort());
215 final DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
216 final DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
217 this.ackProbe(srcPort.toLong());
218 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
219 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
220
221 LinkDescription ld;
222 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
223 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
224 } else {
225 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
226 }
227 linkProvider.linkDetected(ld);
228 return true;
229 }
230 return false;
231 }
232
233
234
235 /**
236 * Execute this method every t milliseconds. Loops over all ports
237 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
238 * port.
239 *
240 * @param t timeout
241 * @throws Exception
242 */
243 @Override
244 public void run(final Timeout t) {
245 this.log.debug("sending probes");
246 synchronized (this) {
247 final Iterator<Long> fastIterator = this.fastPorts.iterator();
248 Long portNumber;
249 Integer probeCount;
250 while (fastIterator.hasNext()) {
251 portNumber = fastIterator.next();
252 probeCount = this.portProbeCount.get(portNumber)
253 .getAndIncrement();
254
255 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
256 this.log.debug("sending fast probe to port");
257 sendProbes(portNumber);
258 } else {
259 // Update fast and slow ports
260 //fastIterator.remove();
261 //this.slowPorts.add(portNumber);
262 //this.portProbeCount.remove(portNumber);
alshabibacd91832014-10-17 14:38:41 -0700263 this.portProbeCount.get(portNumber).set(0);
alshabib7911a052014-10-16 17:49:37 -0700264
265 ConnectPoint cp = new ConnectPoint(
266 device.id(),
267 PortNumber.portNumber(portNumber));
268 log.debug("Link down -> {}", cp);
269 linkProvider.linksVanished(cp);
270 }
271 }
272
273 // send a probe for the next slow port
274 if (!this.slowPorts.isEmpty()) {
275 Iterator<Long> slowIterator = this.slowPorts.iterator();
276 while (slowIterator.hasNext()) {
277 portNumber = slowIterator.next();
278 this.log.debug("sending slow probe to port {}", portNumber);
279
280 sendProbes(portNumber);
281
282 }
283 }
284 }
285
286 // reschedule timer
287 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
288 TimeUnit.MILLISECONDS);
289 }
290
291 public void stop() {
292 timeout.cancel();
alshabib0ed6a202014-10-19 12:42:57 -0700293 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700294 }
295
296 public void start() {
297 timeout = Timer.getTimer().newTimeout(this, 0,
298 TimeUnit.MILLISECONDS);
alshabib0ed6a202014-10-19 12:42:57 -0700299 isStopped = false;
alshabib7911a052014-10-16 17:49:37 -0700300 }
301
302 /**
303 * Creates packet_out LLDP for specified output port.
304 *
305 * @param port the port
306 * @return Packet_out message with LLDP data
307 */
308 private OutboundPacket createOutBoundLLDP(final Long port) {
309 if (port == null) {
310 return null;
311 }
312 this.lldpPacket.setPortId(port.intValue());
313 this.ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
314
315 final byte[] lldp = this.ethPacket.serialize();
316 OutboundPacket outboundPacket = new DefaultOutboundPacket(
317 this.device.id(),
318 DefaultTrafficTreatment.builder().setOutput(
319 PortNumber.portNumber(port)).build(),
320 ByteBuffer.wrap(lldp));
321 return outboundPacket;
322 }
323
324 /**
325 * Creates packet_out BDDP for specified output port.
326 *
327 * @param port the port
328 * @return Packet_out message with LLDP data
329 */
330 private OutboundPacket createOutBoundBDDP(final Long port) {
331 if (port == null) {
332 return null;
333 }
334 this.lldpPacket.setPortId(port.intValue());
335 this.bddpEth.setSourceMACAddress("DE:AD:BE:EF:BA:11");
336
337 final byte[] bddp = this.bddpEth.serialize();
338 OutboundPacket outboundPacket = new DefaultOutboundPacket(
339 this.device.id(),
340 DefaultTrafficTreatment.builder()
341 .setOutput(PortNumber.portNumber(port)).build(),
342 ByteBuffer.wrap(bddp));
343 return outboundPacket;
344 }
345
346 private void sendProbes(Long portNumber) {
alshabib875d6262014-10-17 16:19:40 -0700347 if (mastershipService.getLocalRole(this.device.id()) ==
348 MastershipRole.MASTER) {
349 OutboundPacket pkt = this.createOutBoundLLDP(portNumber);
350 pktService.emit(pkt);
351 if (useBDDP) {
352 OutboundPacket bpkt = this.createOutBoundBDDP(portNumber);
353 pktService.emit(bpkt);
354 }
355 }
alshabib7911a052014-10-16 17:49:37 -0700356 }
357
alshabib0ed6a202014-10-19 12:42:57 -0700358 public boolean containsPort(Long portNumber) {
359 if (slowPorts.contains(portNumber) || fastPorts.contains(portNumber)) {
360 return true;
361 }
362 return false;
363 }
364
365 public boolean isStopped() {
366 return isStopped;
367 }
368
alshabib7911a052014-10-16 17:49:37 -0700369}