blob: e2630f5436e8d67482f5bd4558e7bedd7d0188f2 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
alshabib7911a052014-10-16 17:49:37 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
alshabib7911a052014-10-16 17:49:37 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.lldp.impl;
alshabib7911a052014-10-16 17:49:37 -070017
alshabib7911a052014-10-16 17:49:37 -070018import org.jboss.netty.util.Timeout;
19import org.jboss.netty.util.TimerTask;
Jonathan Harte8600eb2015-01-12 10:30:45 -080020import org.onlab.packet.Ethernet;
21import org.onlab.packet.ONOSLLDP;
22import org.onlab.util.Timer;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.mastership.MastershipService;
24import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Device;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.Link.Type;
28import org.onosproject.net.Port;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.link.DefaultLinkDescription;
31import org.onosproject.net.link.LinkDescription;
32import org.onosproject.net.link.LinkProviderService;
33import org.onosproject.net.packet.DefaultOutboundPacket;
34import org.onosproject.net.packet.OutboundPacket;
35import org.onosproject.net.packet.PacketContext;
36import org.onosproject.net.packet.PacketService;
alshabib7911a052014-10-16 17:49:37 -070037import org.slf4j.Logger;
38
Jonathan Hart45066bc2015-07-28 11:18:34 -070039import java.nio.ByteBuffer;
40import java.util.Collections;
41import java.util.HashMap;
42import java.util.HashSet;
43import java.util.Iterator;
44import java.util.Map;
45import java.util.Set;
46import java.util.concurrent.atomic.AtomicInteger;
47
48import static com.google.common.base.Preconditions.checkNotNull;
49import static java.util.concurrent.TimeUnit.MILLISECONDS;
50import static org.onosproject.net.MastershipRole.MASTER;
51import static org.onosproject.net.PortNumber.portNumber;
52import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
53import static org.slf4j.LoggerFactory.getLogger;
54
Thomas Vachuska4b420772014-10-30 16:46:17 -070055// TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen for flow_removed messages
56
alshabib7911a052014-10-16 17:49:37 -070057/**
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.
alshabib7911a052014-10-16 17:49:37 -070063 */
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;
Yuta HIGUCHIf6725882014-10-29 15:25:51 -070070 // ports, known to have incoming links
alshabib7911a052014-10-16 17:49:37 -070071 private final Set<Long> fastPorts;
72 // number of unacknowledged probes per port
73 private final Map<Long, AtomicInteger> portProbeCount;
74 // number of probes to send before link is removed
75 private static final short MAX_PROBE_COUNT = 3;
76 private final Logger log = getLogger(getClass());
77 private final ONOSLLDP lldpPacket;
78 private final Ethernet ethPacket;
79 private Ethernet bddpEth;
80 private final boolean useBDDP;
81 private final LinkProviderService linkProvider;
82 private final PacketService pktService;
alshabib875d6262014-10-17 16:19:40 -070083 private final MastershipService mastershipService;
alshabib7911a052014-10-16 17:49:37 -070084 private Timeout timeout;
Yuta HIGUCHI32255782014-11-04 22:32:14 -080085 private volatile boolean isStopped;
alshabib7911a052014-10-16 17:49:37 -070086
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.
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070091 *
92 * @param device the physical switch
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080093 * @param pktService packet service
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070094 * @param masterService mastership service
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080095 * @param providerService link provider service
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070096 * @param useBDDP flag to also use BDDP for discovery
alshabib7911a052014-10-16 17:49:37 -070097 */
98 public LinkDiscovery(Device device, PacketService pktService,
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070099 MastershipService masterService,
100 LinkProviderService providerService, Boolean... useBDDP) {
alshabib7911a052014-10-16 17:49:37 -0700101 this.device = device;
102 this.probeRate = 3000;
103 this.linkProvider = providerService;
104 this.pktService = pktService;
alshabib3d643ec2014-10-22 18:33:00 -0700105
106 this.mastershipService = checkNotNull(masterService, "WTF!");
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700107 this.slowPorts = Collections.synchronizedSet(new HashSet<>());
108 this.fastPorts = Collections.synchronizedSet(new HashSet<>());
alshabib7911a052014-10-16 17:49:37 -0700109 this.portProbeCount = new HashMap<>();
110 this.lldpPacket = new ONOSLLDP();
111 this.lldpPacket.setChassisId(device.chassisId());
112 this.lldpPacket.setDevice(device.id().toString());
113
114
115 this.ethPacket = new Ethernet();
116 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
117 this.ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
118 this.ethPacket.setPayload(this.lldpPacket);
119 this.ethPacket.setPad(true);
120 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
121 if (this.useBDDP) {
122 this.bddpEth = new Ethernet();
123 this.bddpEth.setPayload(this.lldpPacket);
124 this.bddpEth.setEtherType(Ethernet.TYPE_BSN);
125 this.bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
126 this.bddpEth.setPad(true);
127 log.info("Using BDDP to discover network");
128 }
129
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700130 this.isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700131 start();
132 this.log.debug("Started discovery manager for switch {}",
133 device.id());
134
135 }
136
137 /**
138 * Add physical port port to discovery process.
139 * Send out initial LLDP and label it as slow port.
140 *
141 * @param port the port
142 */
143 public void addPort(final Port port) {
Jonathan Hart45066bc2015-07-28 11:18:34 -0700144 boolean newPort = false;
alshabib7911a052014-10-16 17:49:37 -0700145 synchronized (this) {
Jonathan Hart45066bc2015-07-28 11:18:34 -0700146 if (!containsPort(port.number().toLong())) {
147 newPort = true;
148 this.slowPorts.add(port.number().toLong());
149 }
150 }
151
152 boolean isMaster = mastershipService.getLocalRole(device.id()) == MASTER;
153 if (newPort && isMaster) {
154 this.log.debug("Sending init probe to port {}@{}",
155 port.number().toLong(), device.id());
156 sendProbes(port.number().toLong());
alshabib7911a052014-10-16 17:49:37 -0700157 }
alshabib7911a052014-10-16 17:49:37 -0700158 }
159
160 /**
161 * Removes physical port from discovery process.
162 *
163 * @param port the port
164 */
165 public void removePort(final Port port) {
166 // Ignore ports that are not on this switch
167
168 long portnum = port.number().toLong();
169 synchronized (this) {
170 if (this.slowPorts.contains(portnum)) {
171 this.slowPorts.remove(portnum);
172
173 } else if (this.fastPorts.contains(portnum)) {
174 this.fastPorts.remove(portnum);
175 this.portProbeCount.remove(portnum);
176 // no iterator to update
177 } else {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700178 this.log.warn("Tried to dynamically remove non-existing port {}",
179 portnum);
alshabib7911a052014-10-16 17:49:37 -0700180 }
181 }
182 }
183
184 /**
185 * Method called by remote port to acknowledge receipt of LLDP sent by
186 * this port. If slow port, updates label to fast. If fast port, decrements
187 * number of unacknowledged probes.
188 *
189 * @param portNumber the port
190 */
191 public void ackProbe(final Long portNumber) {
alshabib7911a052014-10-16 17:49:37 -0700192 synchronized (this) {
193 if (this.slowPorts.contains(portNumber)) {
194 this.log.debug("Setting slow port to fast: {}:{}",
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700195 this.device.id(), portNumber);
alshabib7911a052014-10-16 17:49:37 -0700196 this.slowPorts.remove(portNumber);
197 this.fastPorts.add(portNumber);
198 this.portProbeCount.put(portNumber, new AtomicInteger(0));
alshabibacd91832014-10-17 14:38:41 -0700199 } else if (this.fastPorts.contains(portNumber)) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700200 this.portProbeCount.get(portNumber).set(0);
alshabibacd91832014-10-17 14:38:41 -0700201 } else {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700202 this.log.debug("Got ackProbe for non-existing port: {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700203 }
204 }
205 }
206
207
208 /**
209 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
210 * to port where LLDP originated.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800211 * @param context packet context
212 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700213 */
214 public boolean handleLLDP(PacketContext context) {
215 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800216 if (eth == null) {
217 return false;
218 }
219
alshabib7911a052014-10-16 17:49:37 -0700220 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
221 if (onoslldp != null) {
222 final PortNumber dstPort =
223 context.inPacket().receivedFrom().port();
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700224 final PortNumber srcPort = portNumber(onoslldp.getPort());
alshabib7911a052014-10-16 17:49:37 -0700225 final DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
226 final DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
Jonathan Hart43ef46f2014-10-23 08:33:33 -0700227 this.ackProbe(dstPort.toLong());
alshabib7911a052014-10-16 17:49:37 -0700228 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
229 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
230
231 LinkDescription ld;
232 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
233 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
234 } else {
235 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
236 }
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700237
238 try {
239 linkProvider.linkDetected(ld);
240 } catch (IllegalStateException e) {
241 return true;
242 }
alshabib7911a052014-10-16 17:49:37 -0700243 return true;
244 }
245 return false;
246 }
247
248
alshabib7911a052014-10-16 17:49:37 -0700249 /**
250 * Execute this method every t milliseconds. Loops over all ports
251 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
252 * port.
253 *
254 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700255 */
256 @Override
257 public void run(final Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800258 if (isStopped()) {
259 return;
260 }
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700261 boolean isMaster = mastershipService.getLocalRole(device.id()) == MASTER;
262 if (!isMaster) {
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800263 if (!isStopped()) {
264 // reschedule timer
265 timeout = Timer.getTimer().newTimeout(this, this.probeRate, MILLISECONDS);
266 }
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700267 return;
268 }
269
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700270 this.log.trace("Sending probes from {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700271 synchronized (this) {
272 final Iterator<Long> fastIterator = this.fastPorts.iterator();
alshabib7911a052014-10-16 17:49:37 -0700273 while (fastIterator.hasNext()) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700274 long portNumber = fastIterator.next();
275 int probeCount = portProbeCount.get(portNumber).getAndIncrement();
alshabib7911a052014-10-16 17:49:37 -0700276
277 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700278 this.log.trace("Sending fast probe to port {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700279 sendProbes(portNumber);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700280
alshabib7911a052014-10-16 17:49:37 -0700281 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700282 // Link down, demote to slowPorts
alshabib7911a052014-10-16 17:49:37 -0700283 // Update fast and slow ports
alshabibdfc7afb2014-10-21 20:13:27 -0700284 fastIterator.remove();
285 this.slowPorts.add(portNumber);
286 this.portProbeCount.remove(portNumber);
287
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700288 ConnectPoint cp = new ConnectPoint(device.id(),
289 portNumber(portNumber));
alshabib7911a052014-10-16 17:49:37 -0700290 log.debug("Link down -> {}", cp);
291 linkProvider.linksVanished(cp);
292 }
293 }
294
295 // send a probe for the next slow port
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700296 for (long portNumber : slowPorts) {
297 this.log.trace("Sending slow probe to port {}", portNumber);
298 sendProbes(portNumber);
alshabib7911a052014-10-16 17:49:37 -0700299 }
300 }
301
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800302 if (!isStopped()) {
303 // reschedule timer
304 timeout = Timer.getTimer().newTimeout(this, this.probeRate, MILLISECONDS);
305 }
alshabib7911a052014-10-16 17:49:37 -0700306 }
307
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800308 public synchronized void stop() {
alshabib0ed6a202014-10-19 12:42:57 -0700309 isStopped = true;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700310 timeout.cancel();
alshabib7911a052014-10-16 17:49:37 -0700311 }
312
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800313 public synchronized void start() {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700314 if (isStopped) {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700315 isStopped = false;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700316 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700317 } else {
318 log.warn("LinkDiscovery started multiple times?");
319 }
alshabib7911a052014-10-16 17:49:37 -0700320 }
321
322 /**
323 * Creates packet_out LLDP for specified output port.
324 *
325 * @param port the port
326 * @return Packet_out message with LLDP data
327 */
328 private OutboundPacket createOutBoundLLDP(final Long port) {
329 if (port == null) {
330 return null;
331 }
332 this.lldpPacket.setPortId(port.intValue());
333 this.ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
334
335 final byte[] lldp = this.ethPacket.serialize();
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700336 return new DefaultOutboundPacket(this.device.id(),
337 builder().setOutput(portNumber(port)).build(),
338 ByteBuffer.wrap(lldp));
alshabib7911a052014-10-16 17:49:37 -0700339 }
340
341 /**
342 * Creates packet_out BDDP for specified output port.
343 *
344 * @param port the port
345 * @return Packet_out message with LLDP data
346 */
347 private OutboundPacket createOutBoundBDDP(final Long port) {
348 if (port == null) {
349 return null;
350 }
351 this.lldpPacket.setPortId(port.intValue());
352 this.bddpEth.setSourceMACAddress("DE:AD:BE:EF:BA:11");
353
354 final byte[] bddp = this.bddpEth.serialize();
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700355 return new DefaultOutboundPacket(this.device.id(),
356 builder().setOutput(portNumber(port)).build(),
357 ByteBuffer.wrap(bddp));
alshabib7911a052014-10-16 17:49:37 -0700358 }
359
360 private void sendProbes(Long portNumber) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800361 log.trace("Sending probes out to {}@{}", portNumber, device.id());
362 OutboundPacket pkt = this.createOutBoundLLDP(portNumber);
363 pktService.emit(pkt);
364 if (useBDDP) {
365 OutboundPacket bpkt = this.createOutBoundBDDP(portNumber);
366 pktService.emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700367 }
alshabib7911a052014-10-16 17:49:37 -0700368 }
369
alshabib0ed6a202014-10-19 12:42:57 -0700370 public boolean containsPort(Long portNumber) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700371 return slowPorts.contains(portNumber) || fastPorts.contains(portNumber);
alshabib0ed6a202014-10-19 12:42:57 -0700372 }
373
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700374 public synchronized boolean isStopped() {
375 return isStopped || timeout.isCancelled();
alshabib0ed6a202014-10-19 12:42:57 -0700376 }
377
alshabib7911a052014-10-16 17:49:37 -0700378}