blob: 9f8a4d8021e6cec4d4684e5d4695ee431bd3438a [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;
105 this.probeRate = 1000;
106 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 }
alshabibc944fd02014-09-10 17:55:17 -0700132 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
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());
161 this.slowIterator = this.slowPorts.iterator();
162 }
163
164 }
165
166 /**
167 * Removes physical port from discovery process.
168 *
169 * @param port the port
170 */
alshabiba159a322014-09-09 14:50:51 -0700171 public void removePort(final OFPortDesc port) {
alshabibdf652ad2014-09-09 11:53:19 -0700172 // Ignore ports that are not on this switch
173
alshabiba159a322014-09-09 14:50:51 -0700174 int portnum = port.getPortNo().getPortNumber();
175 this.ports.remove(portnum);
alshabibdf652ad2014-09-09 11:53:19 -0700176 synchronized (this) {
177 if (this.slowPorts.contains(portnum)) {
178 this.slowPorts.remove(portnum);
179 this.slowIterator = this.slowPorts.iterator();
180
181 } else if (this.fastPorts.contains(portnum)) {
182 this.fastPorts.remove(portnum);
183 this.portProbeCount.remove(portnum);
184 // no iterator to update
185 } else {
186 this.log.warn(
187 "tried to dynamically remove non-existing port {}",
188 portnum);
189 }
190 }
alshabibc944fd02014-09-10 17:55:17 -0700191 ConnectPoint cp = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700192 DeviceId.deviceId(uri(sw.getId())),
alshabibc944fd02014-09-10 17:55:17 -0700193 PortNumber.portNumber(port.getPortNo().getPortNumber()));
194 linkProvider.linksVanished(cp);
alshabibdf652ad2014-09-09 11:53:19 -0700195
196 }
197
198 /**
199 * Method called by remote port to acknowledge receipt of LLDP sent by
200 * this port. If slow port, updates label to fast. If fast port, decrements
201 * number of unacknowledged probes.
202 *
203 * @param port the port
204 */
205 public void ackProbe(final Integer port) {
206 final int portNumber = port;
207 synchronized (this) {
208 if (this.slowPorts.contains(portNumber)) {
209 this.log.debug("Setting slow port to fast: {}:{}",
210 this.sw.getId(), portNumber);
211 this.slowPorts.remove(portNumber);
212 this.slowIterator = this.slowPorts.iterator();
213 this.fastPorts.add(portNumber);
214 this.portProbeCount.put(portNumber, new AtomicInteger(0));
215 } else {
216 if (this.fastPorts.contains(portNumber)) {
217 this.portProbeCount.get(portNumber).set(0);
218 } else {
219 this.log.debug(
220 "Got ackProbe for non-existing port: {}",
221 portNumber);
222 }
223 }
224 }
225 }
226
227 /**
228 * Creates packet_out LLDP for specified output port.
229 *
230 * @param port the port
231 * @return Packet_out message with LLDP data
alshabibdf652ad2014-09-09 11:53:19 -0700232 */
233 private OFPacketOut createLLDPPacketOut(final OFPortDesc port) {
234 OFPacketOut.Builder packetOut = this.ofFactory.buildPacketOut();
235 packetOut.setBufferId(OFBufferId.NO_BUFFER);
236 OFAction act = this.ofFactory.actions().buildOutput()
237 .setPort(port.getPortNo()).build();
238 packetOut.setActions(Collections.singletonList(act));
239 this.lldpPacket.setPort(port.getPortNo().getPortNumber());
240 this.ethPacket.setSourceMACAddress(port.getHwAddr().getBytes());
241
242 final byte[] lldp = this.ethPacket.serialize();
243 packetOut.setData(lldp);
244 return packetOut.build();
245 }
246
247 /**
248 * Creates packet_out BDDP for specified output port.
249 *
250 * @param port the port
251 * @return Packet_out message with LLDP data
alshabibdf652ad2014-09-09 11:53:19 -0700252 */
253 private OFPacketOut createBDDPPacketOut(final OFPortDesc port) {
254 OFPacketOut.Builder packetOut = sw.factory().buildPacketOut();
255
256 packetOut.setBufferId(OFBufferId.NO_BUFFER);
257
258 OFActionOutput.Builder act = sw.factory().actions().buildOutput()
259 .setPort(port.getPortNo());
260 OFAction out = act.build();
261 packetOut.setActions(Collections.singletonList(out));
262 this.lldpPacket.setPort(port.getPortNo().getPortNumber());
263 this.bddpEth.setSourceMACAddress(port.getHwAddr().getBytes());
264
265 final byte[] bddp = this.bddpEth.serialize();
266 packetOut.setData(bddp);
267
268 return packetOut.build();
269 }
270
271
272 private void sendMsg(final OFMessage msg) {
273 this.sw.sendMsg(msg);
274 }
275
276 public String getName() {
277 return "LinkDiscovery " + this.sw.getStringId();
278 }
279
280 /*
281 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
282 * to port where LLDP originated.
283 */
alshabib505bc6b2014-09-09 15:04:13 -0700284 public boolean handleLLDP(final byte[] pkt, Integer inPort) {
alshabibdf652ad2014-09-09 11:53:19 -0700285
alshabib9ee68172014-09-09 14:45:14 -0700286 short ethType = ONLabLddp.isOVXLLDP(pkt);
287 if (ethType == Ethernet.TYPE_LLDP || ethType == Ethernet.TYPE_BSN) {
alshabibdf652ad2014-09-09 11:53:19 -0700288 final Integer dstPort = inPort;
289 final DPIDandPort dp = ONLabLddp.parseLLDP(pkt);
290 final OpenFlowSwitch srcSwitch = ctrl.getSwitch(new Dpid(dp.getDpid()));
291 final Integer srcPort = dp.getPort();
292 if (srcSwitch == null) {
alshabib505bc6b2014-09-09 15:04:13 -0700293 return true;
alshabibdf652ad2014-09-09 11:53:19 -0700294 }
295 this.ackProbe(srcPort);
296 ConnectPoint src = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700297 DeviceId.deviceId(uri(srcSwitch.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700298 PortNumber.portNumber(srcPort));
299
300 ConnectPoint dst = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700301 DeviceId.deviceId(uri(sw.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700302 PortNumber.portNumber(dstPort));
303 LinkDescription ld;
alshabib9ee68172014-09-09 14:45:14 -0700304 if (ethType == Ethernet.TYPE_BSN) {
alshabibdf652ad2014-09-09 11:53:19 -0700305 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
306 } else {
307 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
308 }
309 linkProvider.linkDetected(ld);
alshabib505bc6b2014-09-09 15:04:13 -0700310 return true;
alshabibdf652ad2014-09-09 11:53:19 -0700311 } else {
312 this.log.debug("Ignoring unknown LLDP");
alshabib505bc6b2014-09-09 15:04:13 -0700313 return false;
alshabibdf652ad2014-09-09 11:53:19 -0700314 }
315 }
316
alshabib9ee68172014-09-09 14:45:14 -0700317 private OFPortDesc findPort(Integer inPort) {
318 return ports.get(inPort);
alshabibdf652ad2014-09-09 11:53:19 -0700319 }
320
321 /**
322 * Execute this method every t milliseconds. Loops over all ports
323 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
324 * port.
325 *
326 * @param t timeout
327 * @throws Exception
328 */
329 @Override
330 public void run(final Timeout t) {
331 this.log.debug("sending probes");
332 synchronized (this) {
333 final Iterator<Integer> fastIterator = this.fastPorts.iterator();
334 while (fastIterator.hasNext()) {
335 final Integer portNumber = fastIterator.next();
336 final int probeCount = this.portProbeCount.get(portNumber)
337 .getAndIncrement();
alshabib9ee68172014-09-09 14:45:14 -0700338 OFPortDesc port = findPort(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700339 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
340 this.log.debug("sending fast probe to port");
341
342 OFPacketOut pkt = this.createLLDPPacketOut(port);
343 this.sendMsg(pkt);
344 if (useBDDP) {
345 OFPacketOut bpkt = this.createBDDPPacketOut(port);
346 this.sendMsg(bpkt);
347 }
348 } else {
349 // Update fast and slow ports
350 fastIterator.remove();
351 this.slowPorts.add(portNumber);
352 this.slowIterator = this.slowPorts.iterator();
353 this.portProbeCount.remove(portNumber);
354
355 // Remove link from topology
356 final OFPortDesc srcPort = port;
357
358 ConnectPoint cp = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700359 DeviceId.deviceId(uri(sw.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700360 PortNumber.portNumber(srcPort.getPortNo().getPortNumber()));
361 linkProvider.linksVanished(cp);
362 }
363 }
364
365 // send a probe for the next slow port
366 if (this.slowPorts.size() > 0) {
367 if (!this.slowIterator.hasNext()) {
368 this.slowIterator = this.slowPorts.iterator();
369 }
370 if (this.slowIterator.hasNext()) {
371 final int portNumber = this.slowIterator.next();
372 this.log.debug("sending slow probe to port {}", portNumber);
alshabib9ee68172014-09-09 14:45:14 -0700373 OFPortDesc port = findPort(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700374
375 OFPacketOut pkt = this.createLLDPPacketOut(port);
376 this.sendMsg(pkt);
377 if (useBDDP) {
378 OFPacketOut bpkt = this.createBDDPPacketOut(port);
379 this.sendMsg(bpkt);
380 }
381
382 }
383 }
384 }
385
386 // reschedule timer
alshabibc944fd02014-09-10 17:55:17 -0700387 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
alshabibdf652ad2014-09-09 11:53:19 -0700388 TimeUnit.MILLISECONDS);
389 }
390
391 public void removeAllPorts() {
alshabibc944fd02014-09-10 17:55:17 -0700392 for (OFPortDesc port : ports.values()) {
alshabiba159a322014-09-09 14:50:51 -0700393 removePort(port);
alshabibdf652ad2014-09-09 11:53:19 -0700394 }
395 }
396
alshabibc944fd02014-09-10 17:55:17 -0700397 public void stop() {
398 removeAllPorts();
399 timeout.cancel();
400 }
401
alshabibdf652ad2014-09-09 11:53:19 -0700402}