blob: a81eeb1d24b34780536fe0a0fb3c7ed5f58978cf [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
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070018import com.google.common.collect.Maps;
19import com.google.common.collect.Sets;
alshabib7911a052014-10-16 17:49:37 -070020import org.jboss.netty.util.Timeout;
21import org.jboss.netty.util.TimerTask;
Jonathan Harte8600eb2015-01-12 10:30:45 -080022import org.onlab.packet.Ethernet;
23import org.onlab.packet.ONOSLLDP;
24import org.onlab.util.Timer;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.mastership.MastershipService;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.Link.Type;
30import org.onosproject.net.Port;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.link.DefaultLinkDescription;
33import org.onosproject.net.link.LinkDescription;
34import org.onosproject.net.link.LinkProviderService;
35import org.onosproject.net.packet.DefaultOutboundPacket;
36import org.onosproject.net.packet.OutboundPacket;
37import org.onosproject.net.packet.PacketContext;
38import org.onosproject.net.packet.PacketService;
alshabib7911a052014-10-16 17:49:37 -070039import org.slf4j.Logger;
40
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070041import java.nio.ByteBuffer;
42import java.util.Iterator;
43import java.util.Map;
44import java.util.Set;
45import java.util.concurrent.atomic.AtomicInteger;
46
47import static com.google.common.base.Preconditions.checkNotNull;
48import static java.util.concurrent.TimeUnit.MILLISECONDS;
49import static org.onosproject.net.PortNumber.portNumber;
50import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
51import static org.slf4j.LoggerFactory.getLogger;
52
Thomas Vachuska4b420772014-10-30 16:46:17 -070053// TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen for flow_removed messages
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070054// FIXME: add ability to track links using port pairs or the link inventory
Thomas Vachuska4b420772014-10-30 16:46:17 -070055
alshabib7911a052014-10-16 17:49:37 -070056/**
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.
alshabib7911a052014-10-16 17:49:37 -070062 */
63public class LinkDiscovery implements TimerTask {
64
alshabib7911a052014-10-16 17:49:37 -070065 private final Logger log = getLogger(getClass());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070066
67 private static final short MAX_PROBE_COUNT = 3; // probes to send before link is removed
68 private static final short DEFAULT_PROBE_RATE = 3000; // millis
69 private static final String SRC_MAC = "DE:AD:BE:EF:BA:11";
70 private static final String SERVICE_NULL = "Service cannot be null";
71
72 private final Device device;
73
74 // send 1 probe every probeRate milliseconds
75 private final long probeRate = DEFAULT_PROBE_RATE;
76
77 private final Set<Long> slowPorts = Sets.newConcurrentHashSet();
78 // ports, known to have incoming links
79 private final Set<Long> fastPorts = Sets.newConcurrentHashSet();
80
81 // number of unacknowledged probes per port
82 private final Map<Long, AtomicInteger> portProbeCount = Maps.newHashMap();
83
alshabib7911a052014-10-16 17:49:37 -070084 private final ONOSLLDP lldpPacket;
85 private final Ethernet ethPacket;
86 private Ethernet bddpEth;
87 private final boolean useBDDP;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -070088
89 private Timeout timeout;
90 private volatile boolean isStopped;
91
alshabib7911a052014-10-16 17:49:37 -070092 private final LinkProviderService linkProvider;
93 private final PacketService pktService;
alshabib875d6262014-10-17 16:19:40 -070094 private final MastershipService mastershipService;
alshabib7911a052014-10-16 17:49:37 -070095
96 /**
97 * Instantiates discovery manager for the given physical switch. Creates a
98 * generic LLDP packet that will be customized for the port it is sent out on.
99 * Starts the the timer for the discovery process.
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700100 *
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700101 * @param device the physical switch
102 * @param pktService packet service
103 * @param masterService mastership service
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800104 * @param providerService link provider service
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700105 * @param useBDDP flag to also use BDDP for discovery
alshabib7911a052014-10-16 17:49:37 -0700106 */
107 public LinkDiscovery(Device device, PacketService pktService,
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700108 MastershipService masterService,
109 LinkProviderService providerService, Boolean... useBDDP) {
alshabib7911a052014-10-16 17:49:37 -0700110 this.device = device;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700111 this.linkProvider = checkNotNull(providerService, SERVICE_NULL);
112 this.pktService = checkNotNull(pktService, SERVICE_NULL);
113 this.mastershipService = checkNotNull(masterService, SERVICE_NULL);
alshabib3d643ec2014-10-22 18:33:00 -0700114
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700115 lldpPacket = new ONOSLLDP();
116 lldpPacket.setChassisId(device.chassisId());
117 lldpPacket.setDevice(device.id().toString());
alshabib7911a052014-10-16 17:49:37 -0700118
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700119 ethPacket = new Ethernet();
120 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
121 ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
122 ethPacket.setPayload(this.lldpPacket);
123 ethPacket.setPad(true);
alshabib7911a052014-10-16 17:49:37 -0700124
alshabib7911a052014-10-16 17:49:37 -0700125 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
126 if (this.useBDDP) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700127 bddpEth = new Ethernet();
128 bddpEth.setPayload(lldpPacket);
129 bddpEth.setEtherType(Ethernet.TYPE_BSN);
130 bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
131 bddpEth.setPad(true);
alshabib7911a052014-10-16 17:49:37 -0700132 log.info("Using BDDP to discover network");
133 }
134
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700135 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700136 start();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700137 log.debug("Started discovery manager for switch {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700138
139 }
140
141 /**
142 * Add physical port port to discovery process.
143 * Send out initial LLDP and label it as slow port.
144 *
145 * @param port the port
146 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700147 public void addPort(Port port) {
Jonathan Hart45066bc2015-07-28 11:18:34 -0700148 boolean newPort = false;
alshabib7911a052014-10-16 17:49:37 -0700149 synchronized (this) {
Jonathan Hart45066bc2015-07-28 11:18:34 -0700150 if (!containsPort(port.number().toLong())) {
151 newPort = true;
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700152 slowPorts.add(port.number().toLong());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700153 }
154 }
155
Madan Jampani565a66a2015-07-25 17:01:13 -0700156 boolean isMaster = mastershipService.isLocalMaster(device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700157 if (newPort && isMaster) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700158 log.debug("Sending init probe to port {}@{}", port.number().toLong(), device.id());
Jonathan Hart45066bc2015-07-28 11:18:34 -0700159 sendProbes(port.number().toLong());
alshabib7911a052014-10-16 17:49:37 -0700160 }
alshabib7911a052014-10-16 17:49:37 -0700161 }
162
163 /**
164 * Removes physical port from discovery process.
165 *
166 * @param port the port
167 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700168 public void removePort(Port port) {
alshabib7911a052014-10-16 17:49:37 -0700169 // Ignore ports that are not on this switch
alshabib7911a052014-10-16 17:49:37 -0700170 long portnum = port.number().toLong();
171 synchronized (this) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700172 if (slowPorts.contains(portnum)) {
173 slowPorts.remove(portnum);
alshabib7911a052014-10-16 17:49:37 -0700174
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700175 } else if (fastPorts.contains(portnum)) {
176 fastPorts.remove(portnum);
177 portProbeCount.remove(portnum);
alshabib7911a052014-10-16 17:49:37 -0700178 // no iterator to update
179 } else {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700180 log.warn("Tried to dynamically remove non-existing port {}", portnum);
alshabib7911a052014-10-16 17:49:37 -0700181 }
182 }
183 }
184
185 /**
186 * Method called by remote port to acknowledge receipt of LLDP sent by
187 * this port. If slow port, updates label to fast. If fast port, decrements
188 * number of unacknowledged probes.
189 *
190 * @param portNumber the port
191 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700192 public void ackProbe(Long portNumber) {
alshabib7911a052014-10-16 17:49:37 -0700193 synchronized (this) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700194 if (slowPorts.contains(portNumber)) {
195 log.debug("Setting slow port to fast: {}:{}", device.id(), portNumber);
196 slowPorts.remove(portNumber);
197 fastPorts.add(portNumber);
198 portProbeCount.put(portNumber, new AtomicInteger(0));
199 } else if (fastPorts.contains(portNumber)) {
200 portProbeCount.get(portNumber).set(0);
alshabibacd91832014-10-17 14:38:41 -0700201 } else {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700202 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.
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700211 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800212 * @param context packet context
213 * @return true if handled
alshabib7911a052014-10-16 17:49:37 -0700214 */
215 public boolean handleLLDP(PacketContext context) {
216 Ethernet eth = context.inPacket().parsed();
Jonathan Harte8600eb2015-01-12 10:30:45 -0800217 if (eth == null) {
218 return false;
219 }
220
alshabib7911a052014-10-16 17:49:37 -0700221 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
222 if (onoslldp != null) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700223 PortNumber srcPort = portNumber(onoslldp.getPort());
224 PortNumber dstPort = context.inPacket().receivedFrom().port();
225 DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
226 DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
227 ackProbe(dstPort.toLong());
228
alshabib7911a052014-10-16 17:49:37 -0700229 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
230 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
231
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700232 LinkDescription ld = eth.getEtherType() == Ethernet.TYPE_LLDP ?
233 new DefaultLinkDescription(src, dst, Type.DIRECT) :
234 new DefaultLinkDescription(src, dst, Type.INDIRECT);
Sahil Lele3a0cdd52015-07-21 14:16:31 -0700235
236 try {
237 linkProvider.linkDetected(ld);
238 } catch (IllegalStateException e) {
239 return true;
240 }
alshabib7911a052014-10-16 17:49:37 -0700241 return true;
242 }
243 return false;
244 }
245
246
alshabib7911a052014-10-16 17:49:37 -0700247 /**
248 * Execute this method every t milliseconds. Loops over all ports
249 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
250 * port.
251 *
252 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700253 */
254 @Override
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700255 public void run(Timeout t) {
Yuta HIGUCHI34198582014-11-10 16:24:58 -0800256 if (isStopped()) {
257 return;
258 }
Madan Jampani565a66a2015-07-25 17:01:13 -0700259 if (!mastershipService.isLocalMaster(device.id())) {
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800260 if (!isStopped()) {
261 // reschedule timer
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700262 timeout = Timer.getTimer().newTimeout(this, probeRate, MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800263 }
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700264 return;
265 }
266
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700267 log.trace("Sending probes from {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700268 synchronized (this) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700269 Iterator<Long> fastIterator = fastPorts.iterator();
alshabib7911a052014-10-16 17:49:37 -0700270 while (fastIterator.hasNext()) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700271 long portNumber = fastIterator.next();
272 int probeCount = portProbeCount.get(portNumber).getAndIncrement();
alshabib7911a052014-10-16 17:49:37 -0700273
274 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700275 log.trace("Sending fast probe to port {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700276 sendProbes(portNumber);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700277
alshabib7911a052014-10-16 17:49:37 -0700278 } else {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700279 // Link down, demote to slowPorts; update fast and slow ports
alshabibdfc7afb2014-10-21 20:13:27 -0700280 fastIterator.remove();
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700281 slowPorts.add(portNumber);
282 portProbeCount.remove(portNumber);
alshabibdfc7afb2014-10-21 20:13:27 -0700283
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700284 ConnectPoint cp = new ConnectPoint(device.id(), portNumber(portNumber));
alshabib7911a052014-10-16 17:49:37 -0700285 log.debug("Link down -> {}", cp);
286 linkProvider.linksVanished(cp);
287 }
288 }
289
290 // send a probe for the next slow port
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700291 for (long portNumber : slowPorts) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700292 log.trace("Sending slow probe to port {}", portNumber);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700293 sendProbes(portNumber);
alshabib7911a052014-10-16 17:49:37 -0700294 }
295 }
296
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800297 if (!isStopped()) {
298 // reschedule timer
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700299 timeout = Timer.getTimer().newTimeout(this, probeRate, MILLISECONDS);
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800300 }
alshabib7911a052014-10-16 17:49:37 -0700301 }
302
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800303 public synchronized void stop() {
alshabib0ed6a202014-10-19 12:42:57 -0700304 isStopped = true;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700305 timeout.cancel();
alshabib7911a052014-10-16 17:49:37 -0700306 }
307
Yuta HIGUCHI32255782014-11-04 22:32:14 -0800308 public synchronized void start() {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700309 if (isStopped) {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700310 isStopped = false;
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700311 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700312 } else {
313 log.warn("LinkDiscovery started multiple times?");
314 }
alshabib7911a052014-10-16 17:49:37 -0700315 }
316
317 /**
318 * Creates packet_out LLDP for specified output port.
319 *
320 * @param port the port
321 * @return Packet_out message with LLDP data
322 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700323 private OutboundPacket createOutBoundLLDP(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700324 if (port == null) {
325 return null;
326 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700327 lldpPacket.setPortId(port.intValue());
328 ethPacket.setSourceMACAddress(SRC_MAC);
329 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700330 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700331 ByteBuffer.wrap(ethPacket.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700332 }
333
334 /**
335 * Creates packet_out BDDP for specified output port.
336 *
337 * @param port the port
338 * @return Packet_out message with LLDP data
339 */
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700340 private OutboundPacket createOutBoundBDDP(Long port) {
alshabib7911a052014-10-16 17:49:37 -0700341 if (port == null) {
342 return null;
343 }
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700344 lldpPacket.setPortId(port.intValue());
345 bddpEth.setSourceMACAddress(SRC_MAC);
346 return new DefaultOutboundPacket(device.id(),
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700347 builder().setOutput(portNumber(port)).build(),
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700348 ByteBuffer.wrap(bddpEth.serialize()));
alshabib7911a052014-10-16 17:49:37 -0700349 }
350
351 private void sendProbes(Long portNumber) {
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800352 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700353 OutboundPacket pkt = createOutBoundLLDP(portNumber);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800354 pktService.emit(pkt);
355 if (useBDDP) {
Thomas Vachuska96f3ea72015-09-08 13:50:12 -0700356 OutboundPacket bpkt = createOutBoundBDDP(portNumber);
Yuta HIGUCHI41289382014-12-19 17:47:12 -0800357 pktService.emit(bpkt);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700358 }
alshabib7911a052014-10-16 17:49:37 -0700359 }
360
alshabib0ed6a202014-10-19 12:42:57 -0700361 public boolean containsPort(Long portNumber) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700362 return slowPorts.contains(portNumber) || fastPorts.contains(portNumber);
alshabib0ed6a202014-10-19 12:42:57 -0700363 }
364
Thomas Vachuskafc52fec2015-05-18 19:13:56 -0700365 public synchronized boolean isStopped() {
366 return isStopped || timeout.isCancelled();
alshabib0ed6a202014-10-19 12:42:57 -0700367 }
368
alshabib7911a052014-10-16 17:49:37 -0700369}