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