blob: 4282199bed2b5c860c90fad250c63df84f80ec91 [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;
86
87 /**
88 * Instantiates discovery manager for the given physical switch. Creates a
89 * generic LLDP packet that will be customized for the port it is sent out on.
90 * Starts the the timer for the discovery process.
alshabib875d6262014-10-17 16:19:40 -070091 * @param device the physical switch
92 * @param masterService
alshabib7911a052014-10-16 17:49:37 -070093 * @param useBDDP flag to also use BDDP for discovery
94 */
95 public LinkDiscovery(Device device, PacketService pktService,
alshabib875d6262014-10-17 16:19:40 -070096 MastershipService masterService, LinkProviderService providerService, Boolean... useBDDP) {
alshabib7911a052014-10-16 17:49:37 -070097 this.device = device;
98 this.probeRate = 3000;
99 this.linkProvider = providerService;
100 this.pktService = pktService;
alshabib875d6262014-10-17 16:19:40 -0700101 this.mastershipService = masterService;
alshabib7911a052014-10-16 17:49:37 -0700102 this.slowPorts = Collections.synchronizedSet(new HashSet<Long>());
103 this.fastPorts = Collections.synchronizedSet(new HashSet<Long>());
104 this.portProbeCount = new HashMap<>();
105 this.lldpPacket = new ONOSLLDP();
106 this.lldpPacket.setChassisId(device.chassisId());
107 this.lldpPacket.setDevice(device.id().toString());
108
109
110 this.ethPacket = new Ethernet();
111 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
112 this.ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
113 this.ethPacket.setPayload(this.lldpPacket);
114 this.ethPacket.setPad(true);
115 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
116 if (this.useBDDP) {
117 this.bddpEth = new Ethernet();
118 this.bddpEth.setPayload(this.lldpPacket);
119 this.bddpEth.setEtherType(Ethernet.TYPE_BSN);
120 this.bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
121 this.bddpEth.setPad(true);
122 log.info("Using BDDP to discover network");
123 }
124
125 start();
126 this.log.debug("Started discovery manager for switch {}",
127 device.id());
128
129 }
130
131 /**
132 * Add physical port port to discovery process.
133 * Send out initial LLDP and label it as slow port.
134 *
135 * @param port the port
136 */
137 public void addPort(final Port port) {
138 this.log.debug("sending init probe to port {}",
139 port.number().toLong());
140
141 sendProbes(port.number().toLong());
142
143 synchronized (this) {
144 this.slowPorts.add(port.number().toLong());
145 }
146
147
148 }
149
150 /**
151 * Removes physical port from discovery process.
152 *
153 * @param port the port
154 */
155 public void removePort(final Port port) {
156 // Ignore ports that are not on this switch
157
158 long portnum = port.number().toLong();
159 synchronized (this) {
160 if (this.slowPorts.contains(portnum)) {
161 this.slowPorts.remove(portnum);
162
163 } else if (this.fastPorts.contains(portnum)) {
164 this.fastPorts.remove(portnum);
165 this.portProbeCount.remove(portnum);
166 // no iterator to update
167 } else {
168 this.log.warn(
169 "tried to dynamically remove non-existing port {}",
170 portnum);
171 }
172 }
173 }
174
175 /**
176 * Method called by remote port to acknowledge receipt of LLDP sent by
177 * this port. If slow port, updates label to fast. If fast port, decrements
178 * number of unacknowledged probes.
179 *
180 * @param portNumber the port
181 */
182 public void ackProbe(final Long portNumber) {
183
184 synchronized (this) {
185 if (this.slowPorts.contains(portNumber)) {
186 this.log.debug("Setting slow port to fast: {}:{}",
187 this.device.id(), portNumber);
188 this.slowPorts.remove(portNumber);
189 this.fastPorts.add(portNumber);
190 this.portProbeCount.put(portNumber, new AtomicInteger(0));
alshabibacd91832014-10-17 14:38:41 -0700191 } else if (this.fastPorts.contains(portNumber)) {
alshabib7911a052014-10-16 17:49:37 -0700192 this.portProbeCount.get(portNumber).set(0);
alshabibacd91832014-10-17 14:38:41 -0700193 } else {
alshabib7911a052014-10-16 17:49:37 -0700194 this.log.debug(
195 "Got ackProbe for non-existing port: {}",
196 portNumber);
alshabibacd91832014-10-17 14:38:41 -0700197
alshabib7911a052014-10-16 17:49:37 -0700198 }
199 }
200 }
201
202
203 /**
204 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
205 * to port where LLDP originated.
206 */
207 public boolean handleLLDP(PacketContext context) {
208 Ethernet eth = context.inPacket().parsed();
209 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
210 if (onoslldp != null) {
211 final PortNumber dstPort =
212 context.inPacket().receivedFrom().port();
213 final PortNumber srcPort = PortNumber.portNumber(onoslldp.getPort());
214 final DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
215 final DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
216 this.ackProbe(srcPort.toLong());
217 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
218 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
219
220 LinkDescription ld;
221 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
222 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
223 } else {
224 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
225 }
226 linkProvider.linkDetected(ld);
227 return true;
228 }
229 return false;
230 }
231
232
233
234 /**
235 * Execute this method every t milliseconds. Loops over all ports
236 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
237 * port.
238 *
239 * @param t timeout
240 * @throws Exception
241 */
242 @Override
243 public void run(final Timeout t) {
244 this.log.debug("sending probes");
245 synchronized (this) {
246 final Iterator<Long> fastIterator = this.fastPorts.iterator();
247 Long portNumber;
248 Integer probeCount;
249 while (fastIterator.hasNext()) {
250 portNumber = fastIterator.next();
251 probeCount = this.portProbeCount.get(portNumber)
252 .getAndIncrement();
253
254 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
255 this.log.debug("sending fast probe to port");
256 sendProbes(portNumber);
257 } else {
258 // Update fast and slow ports
259 //fastIterator.remove();
260 //this.slowPorts.add(portNumber);
261 //this.portProbeCount.remove(portNumber);
alshabibacd91832014-10-17 14:38:41 -0700262 this.portProbeCount.get(portNumber).set(0);
alshabib7911a052014-10-16 17:49:37 -0700263
264 ConnectPoint cp = new ConnectPoint(
265 device.id(),
266 PortNumber.portNumber(portNumber));
267 log.debug("Link down -> {}", cp);
268 linkProvider.linksVanished(cp);
269 }
270 }
271
272 // send a probe for the next slow port
273 if (!this.slowPorts.isEmpty()) {
274 Iterator<Long> slowIterator = this.slowPorts.iterator();
275 while (slowIterator.hasNext()) {
276 portNumber = slowIterator.next();
277 this.log.debug("sending slow probe to port {}", portNumber);
278
279 sendProbes(portNumber);
280
281 }
282 }
283 }
284
285 // reschedule timer
286 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
287 TimeUnit.MILLISECONDS);
288 }
289
290 public void stop() {
291 timeout.cancel();
292 }
293
294 public void start() {
295 timeout = Timer.getTimer().newTimeout(this, 0,
296 TimeUnit.MILLISECONDS);
297 }
298
299 /**
300 * Creates packet_out LLDP for specified output port.
301 *
302 * @param port the port
303 * @return Packet_out message with LLDP data
304 */
305 private OutboundPacket createOutBoundLLDP(final Long port) {
306 if (port == null) {
307 return null;
308 }
309 this.lldpPacket.setPortId(port.intValue());
310 this.ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
311
312 final byte[] lldp = this.ethPacket.serialize();
313 OutboundPacket outboundPacket = new DefaultOutboundPacket(
314 this.device.id(),
315 DefaultTrafficTreatment.builder().setOutput(
316 PortNumber.portNumber(port)).build(),
317 ByteBuffer.wrap(lldp));
318 return outboundPacket;
319 }
320
321 /**
322 * Creates packet_out BDDP for specified output port.
323 *
324 * @param port the port
325 * @return Packet_out message with LLDP data
326 */
327 private OutboundPacket createOutBoundBDDP(final Long port) {
328 if (port == null) {
329 return null;
330 }
331 this.lldpPacket.setPortId(port.intValue());
332 this.bddpEth.setSourceMACAddress("DE:AD:BE:EF:BA:11");
333
334 final byte[] bddp = this.bddpEth.serialize();
335 OutboundPacket outboundPacket = new DefaultOutboundPacket(
336 this.device.id(),
337 DefaultTrafficTreatment.builder()
338 .setOutput(PortNumber.portNumber(port)).build(),
339 ByteBuffer.wrap(bddp));
340 return outboundPacket;
341 }
342
343 private void sendProbes(Long portNumber) {
alshabib875d6262014-10-17 16:19:40 -0700344 if (mastershipService.getLocalRole(this.device.id()) ==
345 MastershipRole.MASTER) {
346 OutboundPacket pkt = this.createOutBoundLLDP(portNumber);
347 pktService.emit(pkt);
348 if (useBDDP) {
349 OutboundPacket bpkt = this.createOutBoundBDDP(portNumber);
350 pktService.emit(bpkt);
351 }
352 }
alshabib7911a052014-10-16 17:49:37 -0700353 }
354
355}