blob: a20ab905bfaf9603702625903f8285ead1c89fc9 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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 */
alshabib7911a052014-10-16 17:49:37 -070016package org.onlab.onos.provider.lldp.impl;
17
Jonathan Hart4f1ba092014-10-29 17:50:48 -070018import static com.google.common.base.Preconditions.checkNotNull;
19import static java.util.concurrent.TimeUnit.MILLISECONDS;
20import static org.onlab.onos.net.MastershipRole.MASTER;
21import static org.onlab.onos.net.PortNumber.portNumber;
22import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
23import static org.slf4j.LoggerFactory.getLogger;
24
25import java.nio.ByteBuffer;
26import java.util.Collections;
27import java.util.HashMap;
28import java.util.HashSet;
29import java.util.Iterator;
30import java.util.Map;
31import java.util.Set;
32import java.util.concurrent.atomic.AtomicInteger;
33
alshabib7911a052014-10-16 17:49:37 -070034import org.jboss.netty.util.Timeout;
35import org.jboss.netty.util.TimerTask;
alshabib875d6262014-10-17 16:19:40 -070036import org.onlab.onos.mastership.MastershipService;
alshabib7911a052014-10-16 17:49:37 -070037import org.onlab.onos.net.ConnectPoint;
38import org.onlab.onos.net.Device;
39import org.onlab.onos.net.DeviceId;
40import org.onlab.onos.net.Link.Type;
41import org.onlab.onos.net.Port;
42import org.onlab.onos.net.PortNumber;
alshabib7911a052014-10-16 17:49:37 -070043import org.onlab.onos.net.link.DefaultLinkDescription;
44import org.onlab.onos.net.link.LinkDescription;
45import org.onlab.onos.net.link.LinkProviderService;
46import org.onlab.onos.net.packet.DefaultOutboundPacket;
47import org.onlab.onos.net.packet.OutboundPacket;
48import org.onlab.onos.net.packet.PacketContext;
49import org.onlab.onos.net.packet.PacketService;
50import org.onlab.packet.Ethernet;
51import org.onlab.packet.ONOSLLDP;
52import org.onlab.util.Timer;
53import org.slf4j.Logger;
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;
alshabib0ed6a202014-10-19 12:42:57 -070085 private 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
93 * @param masterService mastership service
94 * @param useBDDP flag to also use BDDP for discovery
alshabib7911a052014-10-16 17:49:37 -070095 */
96 public LinkDiscovery(Device device, PacketService pktService,
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -070097 MastershipService masterService,
98 LinkProviderService providerService, Boolean... useBDDP) {
alshabib7911a052014-10-16 17:49:37 -070099 this.device = device;
100 this.probeRate = 3000;
101 this.linkProvider = providerService;
102 this.pktService = pktService;
alshabib3d643ec2014-10-22 18:33:00 -0700103
104 this.mastershipService = checkNotNull(masterService, "WTF!");
alshabib7911a052014-10-16 17:49:37 -0700105 this.slowPorts = Collections.synchronizedSet(new HashSet<Long>());
106 this.fastPorts = Collections.synchronizedSet(new HashSet<Long>());
107 this.portProbeCount = new HashMap<>();
108 this.lldpPacket = new ONOSLLDP();
109 this.lldpPacket.setChassisId(device.chassisId());
110 this.lldpPacket.setDevice(device.id().toString());
111
112
113 this.ethPacket = new Ethernet();
114 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
115 this.ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
116 this.ethPacket.setPayload(this.lldpPacket);
117 this.ethPacket.setPad(true);
118 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
119 if (this.useBDDP) {
120 this.bddpEth = new Ethernet();
121 this.bddpEth.setPayload(this.lldpPacket);
122 this.bddpEth.setEtherType(Ethernet.TYPE_BSN);
123 this.bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
124 this.bddpEth.setPad(true);
125 log.info("Using BDDP to discover network");
126 }
127
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700128 this.isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700129 start();
130 this.log.debug("Started discovery manager for switch {}",
131 device.id());
132
133 }
134
135 /**
136 * Add physical port port to discovery process.
137 * Send out initial LLDP and label it as slow port.
138 *
139 * @param port the port
140 */
141 public void addPort(final Port port) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700142 this.log.debug("Sending init probe to port {}@{}",
143 port.number().toLong(), device.id());
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700144 boolean isMaster = mastershipService.getLocalRole(device.id()) == MASTER;
145 if (isMaster) {
146 sendProbes(port.number().toLong());
147 }
alshabib7911a052014-10-16 17:49:37 -0700148 synchronized (this) {
149 this.slowPorts.add(port.number().toLong());
150 }
alshabib7911a052014-10-16 17:49:37 -0700151 }
152
153 /**
154 * Removes physical port from discovery process.
155 *
156 * @param port the port
157 */
158 public void removePort(final Port port) {
159 // Ignore ports that are not on this switch
160
161 long portnum = port.number().toLong();
162 synchronized (this) {
163 if (this.slowPorts.contains(portnum)) {
164 this.slowPorts.remove(portnum);
165
166 } else if (this.fastPorts.contains(portnum)) {
167 this.fastPorts.remove(portnum);
168 this.portProbeCount.remove(portnum);
169 // no iterator to update
170 } else {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700171 this.log.warn("Tried to dynamically remove non-existing port {}",
172 portnum);
alshabib7911a052014-10-16 17:49:37 -0700173 }
174 }
175 }
176
177 /**
178 * Method called by remote port to acknowledge receipt of LLDP sent by
179 * this port. If slow port, updates label to fast. If fast port, decrements
180 * number of unacknowledged probes.
181 *
182 * @param portNumber the port
183 */
184 public void ackProbe(final Long portNumber) {
alshabib7911a052014-10-16 17:49:37 -0700185 synchronized (this) {
186 if (this.slowPorts.contains(portNumber)) {
187 this.log.debug("Setting slow port to fast: {}:{}",
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700188 this.device.id(), portNumber);
alshabib7911a052014-10-16 17:49:37 -0700189 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)) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700193 this.portProbeCount.get(portNumber).set(0);
alshabibacd91832014-10-17 14:38:41 -0700194 } else {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700195 this.log.debug("Got ackProbe for non-existing port: {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700196 }
197 }
198 }
199
200
201 /**
202 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
203 * to port where LLDP originated.
204 */
205 public boolean handleLLDP(PacketContext context) {
206 Ethernet eth = context.inPacket().parsed();
207 ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
208 if (onoslldp != null) {
209 final PortNumber dstPort =
210 context.inPacket().receivedFrom().port();
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700211 final PortNumber srcPort = portNumber(onoslldp.getPort());
alshabib7911a052014-10-16 17:49:37 -0700212 final DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
213 final DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId();
Jonathan Hart43ef46f2014-10-23 08:33:33 -0700214 this.ackProbe(dstPort.toLong());
alshabib7911a052014-10-16 17:49:37 -0700215 ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
216 ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
217
218 LinkDescription ld;
219 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
220 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
221 } else {
222 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
223 }
224 linkProvider.linkDetected(ld);
225 return true;
226 }
227 return false;
228 }
229
230
alshabib7911a052014-10-16 17:49:37 -0700231 /**
232 * Execute this method every t milliseconds. Loops over all ports
233 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
234 * port.
235 *
236 * @param t timeout
alshabib7911a052014-10-16 17:49:37 -0700237 */
238 @Override
239 public void run(final Timeout t) {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700240 boolean isMaster = mastershipService.getLocalRole(device.id()) == MASTER;
241 if (!isMaster) {
242 // reschedule timer
243 timeout = Timer.getTimer().newTimeout(this, this.probeRate, MILLISECONDS);
244 return;
245 }
246
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700247 this.log.trace("Sending probes from {}", device.id());
alshabib7911a052014-10-16 17:49:37 -0700248 synchronized (this) {
249 final Iterator<Long> fastIterator = this.fastPorts.iterator();
alshabib7911a052014-10-16 17:49:37 -0700250 while (fastIterator.hasNext()) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700251 long portNumber = fastIterator.next();
252 int probeCount = portProbeCount.get(portNumber).getAndIncrement();
alshabib7911a052014-10-16 17:49:37 -0700253
254 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700255 this.log.trace("Sending fast probe to port {}", portNumber);
alshabib7911a052014-10-16 17:49:37 -0700256 sendProbes(portNumber);
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700257
alshabib7911a052014-10-16 17:49:37 -0700258 } else {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700259 // Link down, demote to slowPorts
alshabib7911a052014-10-16 17:49:37 -0700260 // Update fast and slow ports
alshabibdfc7afb2014-10-21 20:13:27 -0700261 fastIterator.remove();
262 this.slowPorts.add(portNumber);
263 this.portProbeCount.remove(portNumber);
264
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700265 ConnectPoint cp = new ConnectPoint(device.id(),
266 portNumber(portNumber));
alshabib7911a052014-10-16 17:49:37 -0700267 log.debug("Link down -> {}", cp);
268 linkProvider.linksVanished(cp);
269 }
270 }
271
272 // send a probe for the next slow port
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700273 for (long portNumber : slowPorts) {
274 this.log.trace("Sending slow probe to port {}", portNumber);
275 sendProbes(portNumber);
alshabib7911a052014-10-16 17:49:37 -0700276 }
277 }
278
279 // reschedule timer
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700280 timeout = Timer.getTimer().newTimeout(this, this.probeRate, MILLISECONDS);
alshabib7911a052014-10-16 17:49:37 -0700281 }
282
283 public void stop() {
284 timeout.cancel();
alshabib0ed6a202014-10-19 12:42:57 -0700285 isStopped = true;
alshabib7911a052014-10-16 17:49:37 -0700286 }
287
288 public void start() {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700289 if (isStopped) {
290 timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
291 isStopped = false;
292 } else {
293 log.warn("LinkDiscovery started multiple times?");
294 }
alshabib7911a052014-10-16 17:49:37 -0700295 }
296
297 /**
298 * Creates packet_out LLDP for specified output port.
299 *
300 * @param port the port
301 * @return Packet_out message with LLDP data
302 */
303 private OutboundPacket createOutBoundLLDP(final Long port) {
304 if (port == null) {
305 return null;
306 }
307 this.lldpPacket.setPortId(port.intValue());
308 this.ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
309
310 final byte[] lldp = this.ethPacket.serialize();
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700311 return new DefaultOutboundPacket(this.device.id(),
312 builder().setOutput(portNumber(port)).build(),
313 ByteBuffer.wrap(lldp));
alshabib7911a052014-10-16 17:49:37 -0700314 }
315
316 /**
317 * Creates packet_out BDDP for specified output port.
318 *
319 * @param port the port
320 * @return Packet_out message with LLDP data
321 */
322 private OutboundPacket createOutBoundBDDP(final Long port) {
323 if (port == null) {
324 return null;
325 }
326 this.lldpPacket.setPortId(port.intValue());
327 this.bddpEth.setSourceMACAddress("DE:AD:BE:EF:BA:11");
328
329 final byte[] bddp = this.bddpEth.serialize();
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700330 return new DefaultOutboundPacket(this.device.id(),
331 builder().setOutput(portNumber(port)).build(),
332 ByteBuffer.wrap(bddp));
alshabib7911a052014-10-16 17:49:37 -0700333 }
334
335 private void sendProbes(Long portNumber) {
Yuta HIGUCHIf6725882014-10-29 15:25:51 -0700336 // TODO: should have suppression port configuration, not by type
337 if (device.type() != Device.Type.ROADM) {
Jonathan Hart4f1ba092014-10-29 17:50:48 -0700338 log.trace("Sending probes out to {}@{}", portNumber, device.id());
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700339 OutboundPacket pkt = this.createOutBoundLLDP(portNumber);
340 pktService.emit(pkt);
341 if (useBDDP) {
342 OutboundPacket bpkt = this.createOutBoundBDDP(portNumber);
343 pktService.emit(bpkt);
344 }
345 }
alshabib7911a052014-10-16 17:49:37 -0700346 }
347
alshabib0ed6a202014-10-19 12:42:57 -0700348 public boolean containsPort(Long portNumber) {
Thomas Vachuskae1bcb0b2014-10-27 17:45:10 -0700349 return slowPorts.contains(portNumber) || fastPorts.contains(portNumber);
alshabib0ed6a202014-10-19 12:42:57 -0700350 }
351
352 public boolean isStopped() {
353 return isStopped;
354 }
355
alshabib7911a052014-10-16 17:49:37 -0700356}