blob: ecb4765b7cd12db7b90a203ee0a0a61267d9736e [file] [log] [blame]
alshabibdf652ad2014-09-09 11:53:19 -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.of.link.impl;
17
tom782a7cf2014-09-11 23:58:38 -070018import static org.onlab.onos.of.controller.Dpid.uri;
alshabibdf652ad2014-09-09 11:53:19 -070019import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.Collections;
22import java.util.HashMap;
23import java.util.HashSet;
24import java.util.Iterator;
alshabibdf652ad2014-09-09 11:53:19 -070025import java.util.Map;
26import java.util.Set;
alshabib9ee68172014-09-09 14:45:14 -070027import java.util.concurrent.ConcurrentHashMap;
alshabibdf652ad2014-09-09 11:53:19 -070028import 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.DeviceId;
35import org.onlab.onos.net.Link.Type;
36import org.onlab.onos.net.PortNumber;
37import org.onlab.onos.net.link.DefaultLinkDescription;
38import org.onlab.onos.net.link.LinkDescription;
39import org.onlab.onos.net.link.LinkProviderService;
40import org.onlab.onos.of.controller.Dpid;
41import org.onlab.onos.of.controller.OpenFlowController;
42import org.onlab.onos.of.controller.OpenFlowSwitch;
43import org.onlab.packet.Ethernet;
44import org.onlab.packet.ONLabLddp;
45import org.onlab.packet.ONLabLddp.DPIDandPort;
tom8bf2e6b2014-09-10 20:53:54 -070046import org.onlab.util.Timer;
alshabibdf652ad2014-09-09 11:53:19 -070047import org.projectfloodlight.openflow.protocol.OFFactory;
48import org.projectfloodlight.openflow.protocol.OFMessage;
49import org.projectfloodlight.openflow.protocol.OFPacketOut;
50import org.projectfloodlight.openflow.protocol.OFPortDesc;
51import org.projectfloodlight.openflow.protocol.action.OFAction;
52import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
53import org.projectfloodlight.openflow.types.OFBufferId;
54import org.projectfloodlight.openflow.types.OFPort;
55import org.slf4j.Logger;
56
57
58
59/**
60 * Run discovery process from a physical switch. Ports are initially labeled as
61 * slow ports. When an LLDP is successfully received, label the remote port as
62 * fast. Every probeRate milliseconds, loop over all fast ports and send an
63 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
64 * discovery implementation.
65 *
66 * TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen
67 * for flow_removed messages
68 */
69public class LinkDiscovery implements TimerTask {
70
71 private final OpenFlowSwitch sw;
72 // send 1 probe every probeRate milliseconds
73 private final long probeRate;
74 private final Set<Integer> slowPorts;
75 private final Set<Integer> fastPorts;
76 // number of unacknowledged probes per port
77 private final Map<Integer, AtomicInteger> portProbeCount;
78 // number of probes to send before link is removed
79 private static final short MAX_PROBE_COUNT = 3;
80 private Iterator<Integer> slowIterator;
81 private final OFFactory ofFactory;
82 private final Logger log = getLogger(getClass());
83 private final ONLabLddp lldpPacket;
84 private final Ethernet ethPacket;
85 private Ethernet bddpEth;
86 private final boolean useBDDP;
87 private final OpenFlowController ctrl;
88 private final LinkProviderService linkProvider;
alshabib9ee68172014-09-09 14:45:14 -070089 private final Map<Integer, OFPortDesc> ports;
alshabibc944fd02014-09-10 17:55:17 -070090 private Timeout timeout;
alshabibdf652ad2014-09-09 11:53:19 -070091
92 /**
93 * Instantiates discovery manager for the given physical switch. Creates a
94 * generic LLDP packet that will be customized for the port it is sent out on.
95 * Starts the the timer for the discovery process.
96 *
97 * @param sw the physical switch
98 * @param useBDDP flag to also use BDDP for discovery
99 */
100 public LinkDiscovery(final OpenFlowSwitch sw,
101 OpenFlowController ctrl, LinkProviderService providerService, Boolean... useBDDP) {
102 this.sw = sw;
103 this.ofFactory = sw.factory();
104 this.ctrl = ctrl;
alshabib7674db42014-09-12 23:40:46 -0700105 this.probeRate = 3000;
alshabibdf652ad2014-09-09 11:53:19 -0700106 this.linkProvider = providerService;
107 this.slowPorts = Collections.synchronizedSet(new HashSet<Integer>());
108 this.fastPorts = Collections.synchronizedSet(new HashSet<Integer>());
alshabib9ee68172014-09-09 14:45:14 -0700109 this.ports = new ConcurrentHashMap<>();
alshabibdf652ad2014-09-09 11:53:19 -0700110 this.portProbeCount = new HashMap<Integer, AtomicInteger>();
111 this.lldpPacket = new ONLabLddp();
112 this.lldpPacket.setSwitch(this.sw.getId());
113 this.ethPacket = new Ethernet();
114 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
115 this.ethPacket.setDestinationMACAddress(ONLabLddp.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(ONLabLddp.BDDP_MULTICAST);
124 this.bddpEth.setPad(true);
125 log.info("Using BDDP to discover network");
126 }
127 for (OFPortDesc port : sw.getPorts()) {
128 if (port.getPortNo() != OFPort.LOCAL) {
129 addPort(port);
130 }
131 }
alshabib7674db42014-09-12 23:40:46 -0700132 timeout = Timer.getTimer().newTimeout(this, 0,
alshabibdf652ad2014-09-09 11:53:19 -0700133 TimeUnit.MILLISECONDS);
134 this.log.debug("Started discovery manager for switch {}",
135 sw.getId());
136
137 }
138
139 /**
140 * Add physical port port to discovery process.
141 * Send out initial LLDP and label it as slow port.
142 *
143 * @param port the port
144 */
145 public void addPort(final OFPortDesc port) {
146 // Ignore ports that are not on this switch, or already booted. */
alshabib9ee68172014-09-09 14:45:14 -0700147 this.ports.put(port.getPortNo().getPortNumber(), port);
alshabibdf652ad2014-09-09 11:53:19 -0700148 synchronized (this) {
149 this.log.debug("sending init probe to port {}",
150 port.getPortNo().getPortNumber());
151 OFPacketOut pkt;
152
153 pkt = this.createLLDPPacketOut(port);
154 this.sw.sendMsg(pkt);
155 if (useBDDP) {
156 OFPacketOut bpkt = this.createBDDPPacketOut(port);
157 this.sw.sendMsg(bpkt);
158 }
159
160 this.slowPorts.add(port.getPortNo().getPortNumber());
alshabibdf652ad2014-09-09 11:53:19 -0700161 }
162
163 }
164
165 /**
166 * Removes physical port from discovery process.
167 *
168 * @param port the port
169 */
alshabiba159a322014-09-09 14:50:51 -0700170 public void removePort(final OFPortDesc port) {
alshabibdf652ad2014-09-09 11:53:19 -0700171 // Ignore ports that are not on this switch
172
alshabiba159a322014-09-09 14:50:51 -0700173 int portnum = port.getPortNo().getPortNumber();
174 this.ports.remove(portnum);
alshabibdf652ad2014-09-09 11:53:19 -0700175 synchronized (this) {
176 if (this.slowPorts.contains(portnum)) {
177 this.slowPorts.remove(portnum);
alshabibdf652ad2014-09-09 11:53:19 -0700178
179 } else if (this.fastPorts.contains(portnum)) {
180 this.fastPorts.remove(portnum);
181 this.portProbeCount.remove(portnum);
182 // no iterator to update
183 } else {
184 this.log.warn(
185 "tried to dynamically remove non-existing port {}",
186 portnum);
187 }
188 }
alshabibc944fd02014-09-10 17:55:17 -0700189 ConnectPoint cp = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700190 DeviceId.deviceId(uri(sw.getId())),
alshabibc944fd02014-09-10 17:55:17 -0700191 PortNumber.portNumber(port.getPortNo().getPortNumber()));
192 linkProvider.linksVanished(cp);
alshabibdf652ad2014-09-09 11:53:19 -0700193
194 }
195
196 /**
197 * Method called by remote port to acknowledge receipt of LLDP sent by
198 * this port. If slow port, updates label to fast. If fast port, decrements
199 * number of unacknowledged probes.
200 *
201 * @param port the port
202 */
203 public void ackProbe(final Integer port) {
204 final int portNumber = port;
205 synchronized (this) {
206 if (this.slowPorts.contains(portNumber)) {
207 this.log.debug("Setting slow port to fast: {}:{}",
208 this.sw.getId(), portNumber);
209 this.slowPorts.remove(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700210 this.fastPorts.add(portNumber);
211 this.portProbeCount.put(portNumber, new AtomicInteger(0));
212 } else {
213 if (this.fastPorts.contains(portNumber)) {
214 this.portProbeCount.get(portNumber).set(0);
215 } else {
216 this.log.debug(
217 "Got ackProbe for non-existing port: {}",
218 portNumber);
219 }
220 }
221 }
222 }
223
224 /**
225 * Creates packet_out LLDP for specified output port.
226 *
227 * @param port the port
228 * @return Packet_out message with LLDP data
alshabibdf652ad2014-09-09 11:53:19 -0700229 */
230 private OFPacketOut createLLDPPacketOut(final OFPortDesc port) {
231 OFPacketOut.Builder packetOut = this.ofFactory.buildPacketOut();
232 packetOut.setBufferId(OFBufferId.NO_BUFFER);
233 OFAction act = this.ofFactory.actions().buildOutput()
234 .setPort(port.getPortNo()).build();
235 packetOut.setActions(Collections.singletonList(act));
236 this.lldpPacket.setPort(port.getPortNo().getPortNumber());
237 this.ethPacket.setSourceMACAddress(port.getHwAddr().getBytes());
238
239 final byte[] lldp = this.ethPacket.serialize();
240 packetOut.setData(lldp);
241 return packetOut.build();
242 }
243
244 /**
245 * Creates packet_out BDDP for specified output port.
246 *
247 * @param port the port
248 * @return Packet_out message with LLDP data
alshabibdf652ad2014-09-09 11:53:19 -0700249 */
250 private OFPacketOut createBDDPPacketOut(final OFPortDesc port) {
251 OFPacketOut.Builder packetOut = sw.factory().buildPacketOut();
252
253 packetOut.setBufferId(OFBufferId.NO_BUFFER);
254
255 OFActionOutput.Builder act = sw.factory().actions().buildOutput()
256 .setPort(port.getPortNo());
257 OFAction out = act.build();
258 packetOut.setActions(Collections.singletonList(out));
259 this.lldpPacket.setPort(port.getPortNo().getPortNumber());
260 this.bddpEth.setSourceMACAddress(port.getHwAddr().getBytes());
261
262 final byte[] bddp = this.bddpEth.serialize();
263 packetOut.setData(bddp);
264
265 return packetOut.build();
266 }
267
268
269 private void sendMsg(final OFMessage msg) {
270 this.sw.sendMsg(msg);
271 }
272
273 public String getName() {
274 return "LinkDiscovery " + this.sw.getStringId();
275 }
276
alshabib818d57c2014-09-15 22:34:30 -0700277 /**
alshabibdf652ad2014-09-09 11:53:19 -0700278 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
279 * to port where LLDP originated.
280 */
alshabib505bc6b2014-09-09 15:04:13 -0700281 public boolean handleLLDP(final byte[] pkt, Integer inPort) {
alshabibdf652ad2014-09-09 11:53:19 -0700282
alshabib9ee68172014-09-09 14:45:14 -0700283 short ethType = ONLabLddp.isOVXLLDP(pkt);
284 if (ethType == Ethernet.TYPE_LLDP || ethType == Ethernet.TYPE_BSN) {
alshabibdf652ad2014-09-09 11:53:19 -0700285 final Integer dstPort = inPort;
286 final DPIDandPort dp = ONLabLddp.parseLLDP(pkt);
287 final OpenFlowSwitch srcSwitch = ctrl.getSwitch(new Dpid(dp.getDpid()));
288 final Integer srcPort = dp.getPort();
289 if (srcSwitch == null) {
alshabib505bc6b2014-09-09 15:04:13 -0700290 return true;
alshabibdf652ad2014-09-09 11:53:19 -0700291 }
292 this.ackProbe(srcPort);
293 ConnectPoint src = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700294 DeviceId.deviceId(uri(srcSwitch.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700295 PortNumber.portNumber(srcPort));
296
297 ConnectPoint dst = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700298 DeviceId.deviceId(uri(sw.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700299 PortNumber.portNumber(dstPort));
300 LinkDescription ld;
alshabib9ee68172014-09-09 14:45:14 -0700301 if (ethType == Ethernet.TYPE_BSN) {
alshabibdf652ad2014-09-09 11:53:19 -0700302 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
303 } else {
304 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
305 }
306 linkProvider.linkDetected(ld);
alshabib505bc6b2014-09-09 15:04:13 -0700307 return true;
alshabibdf652ad2014-09-09 11:53:19 -0700308 } else {
309 this.log.debug("Ignoring unknown LLDP");
alshabib505bc6b2014-09-09 15:04:13 -0700310 return false;
alshabibdf652ad2014-09-09 11:53:19 -0700311 }
312 }
313
alshabib9ee68172014-09-09 14:45:14 -0700314 private OFPortDesc findPort(Integer inPort) {
315 return ports.get(inPort);
alshabibdf652ad2014-09-09 11:53:19 -0700316 }
317
318 /**
319 * Execute this method every t milliseconds. Loops over all ports
320 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
321 * port.
322 *
323 * @param t timeout
324 * @throws Exception
325 */
326 @Override
327 public void run(final Timeout t) {
328 this.log.debug("sending probes");
329 synchronized (this) {
330 final Iterator<Integer> fastIterator = this.fastPorts.iterator();
331 while (fastIterator.hasNext()) {
332 final Integer portNumber = fastIterator.next();
333 final int probeCount = this.portProbeCount.get(portNumber)
334 .getAndIncrement();
alshabib9ee68172014-09-09 14:45:14 -0700335 OFPortDesc port = findPort(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700336 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
337 this.log.debug("sending fast probe to port");
338
339 OFPacketOut pkt = this.createLLDPPacketOut(port);
340 this.sendMsg(pkt);
341 if (useBDDP) {
342 OFPacketOut bpkt = this.createBDDPPacketOut(port);
343 this.sendMsg(bpkt);
344 }
345 } else {
346 // Update fast and slow ports
347 fastIterator.remove();
348 this.slowPorts.add(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700349 this.portProbeCount.remove(portNumber);
350
351 // Remove link from topology
352 final OFPortDesc srcPort = port;
353
354 ConnectPoint cp = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700355 DeviceId.deviceId(uri(sw.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700356 PortNumber.portNumber(srcPort.getPortNo().getPortNumber()));
357 linkProvider.linksVanished(cp);
358 }
359 }
360
361 // send a probe for the next slow port
alshabib818d57c2014-09-15 22:34:30 -0700362 if (!this.slowPorts.isEmpty()) {
363 this.slowIterator = this.slowPorts.iterator();
364 while (this.slowIterator.hasNext()) {
alshabibdf652ad2014-09-09 11:53:19 -0700365 final int portNumber = this.slowIterator.next();
366 this.log.debug("sending slow probe to port {}", portNumber);
alshabib9ee68172014-09-09 14:45:14 -0700367 OFPortDesc port = findPort(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700368
369 OFPacketOut pkt = this.createLLDPPacketOut(port);
370 this.sendMsg(pkt);
371 if (useBDDP) {
372 OFPacketOut bpkt = this.createBDDPPacketOut(port);
373 this.sendMsg(bpkt);
374 }
375
376 }
377 }
378 }
379
380 // reschedule timer
alshabibc944fd02014-09-10 17:55:17 -0700381 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
alshabibdf652ad2014-09-09 11:53:19 -0700382 TimeUnit.MILLISECONDS);
383 }
384
385 public void removeAllPorts() {
alshabibc944fd02014-09-10 17:55:17 -0700386 for (OFPortDesc port : ports.values()) {
alshabiba159a322014-09-09 14:50:51 -0700387 removePort(port);
alshabibdf652ad2014-09-09 11:53:19 -0700388 }
389 }
390
alshabibc944fd02014-09-10 17:55:17 -0700391 public void stop() {
392 removeAllPorts();
393 timeout.cancel();
394 }
395
alshabibdf652ad2014-09-09 11:53:19 -0700396}