blob: bc45f7978de35ee6fb32a1ed04133eb3fee8aaf6 [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
tom9c94c5b2014-09-17 13:14:42 -070018import static org.onlab.onos.openflow.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;
tom9c94c5b2014-09-17 13:14:42 -070040import org.onlab.onos.openflow.controller.Dpid;
41import org.onlab.onos.openflow.controller.OpenFlowController;
42import org.onlab.onos.openflow.controller.OpenFlowSwitch;
alshabibdf652ad2014-09-09 11:53:19 -070043import 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;
alshabib64231d82014-09-25 18:25:31 -070089 protected 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);
alshabib6eb438a2014-10-01 16:39:37 -0700134 this.log.info("Started discovery manager for switch {}",
alshabibdf652ad2014-09-09 11:53:19 -0700135 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) {
alshabib89e43ef2014-09-16 10:36:34 -0700231 if (port == null) {
232 return null;
233 }
alshabibdf652ad2014-09-09 11:53:19 -0700234 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) {
alshabib89e43ef2014-09-16 10:36:34 -0700254 if (port == null) {
255 return null;
256 }
alshabibdf652ad2014-09-09 11:53:19 -0700257 OFPacketOut.Builder packetOut = sw.factory().buildPacketOut();
258
259 packetOut.setBufferId(OFBufferId.NO_BUFFER);
260
261 OFActionOutput.Builder act = sw.factory().actions().buildOutput()
262 .setPort(port.getPortNo());
263 OFAction out = act.build();
264 packetOut.setActions(Collections.singletonList(out));
265 this.lldpPacket.setPort(port.getPortNo().getPortNumber());
266 this.bddpEth.setSourceMACAddress(port.getHwAddr().getBytes());
267
268 final byte[] bddp = this.bddpEth.serialize();
269 packetOut.setData(bddp);
270
271 return packetOut.build();
272 }
273
274
275 private void sendMsg(final OFMessage msg) {
alshabib89e43ef2014-09-16 10:36:34 -0700276 if (msg == null) {
277 return;
278 }
alshabibdf652ad2014-09-09 11:53:19 -0700279 this.sw.sendMsg(msg);
280 }
281
282 public String getName() {
283 return "LinkDiscovery " + this.sw.getStringId();
284 }
285
alshabib818d57c2014-09-15 22:34:30 -0700286 /**
alshabibdf652ad2014-09-09 11:53:19 -0700287 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
288 * to port where LLDP originated.
289 */
alshabib505bc6b2014-09-09 15:04:13 -0700290 public boolean handleLLDP(final byte[] pkt, Integer inPort) {
alshabibdf652ad2014-09-09 11:53:19 -0700291
alshabib9ee68172014-09-09 14:45:14 -0700292 short ethType = ONLabLddp.isOVXLLDP(pkt);
293 if (ethType == Ethernet.TYPE_LLDP || ethType == Ethernet.TYPE_BSN) {
alshabibdf652ad2014-09-09 11:53:19 -0700294 final Integer dstPort = inPort;
295 final DPIDandPort dp = ONLabLddp.parseLLDP(pkt);
296 final OpenFlowSwitch srcSwitch = ctrl.getSwitch(new Dpid(dp.getDpid()));
297 final Integer srcPort = dp.getPort();
298 if (srcSwitch == null) {
alshabib505bc6b2014-09-09 15:04:13 -0700299 return true;
alshabibdf652ad2014-09-09 11:53:19 -0700300 }
301 this.ackProbe(srcPort);
302 ConnectPoint src = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700303 DeviceId.deviceId(uri(srcSwitch.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700304 PortNumber.portNumber(srcPort));
305
306 ConnectPoint dst = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700307 DeviceId.deviceId(uri(sw.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700308 PortNumber.portNumber(dstPort));
309 LinkDescription ld;
alshabib9ee68172014-09-09 14:45:14 -0700310 if (ethType == Ethernet.TYPE_BSN) {
alshabibdf652ad2014-09-09 11:53:19 -0700311 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
312 } else {
313 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
314 }
315 linkProvider.linkDetected(ld);
alshabib505bc6b2014-09-09 15:04:13 -0700316 return true;
alshabibdf652ad2014-09-09 11:53:19 -0700317 } else {
318 this.log.debug("Ignoring unknown LLDP");
alshabib505bc6b2014-09-09 15:04:13 -0700319 return false;
alshabibdf652ad2014-09-09 11:53:19 -0700320 }
321 }
322
alshabib9ee68172014-09-09 14:45:14 -0700323 private OFPortDesc findPort(Integer inPort) {
324 return ports.get(inPort);
alshabibdf652ad2014-09-09 11:53:19 -0700325 }
326
327 /**
328 * Execute this method every t milliseconds. Loops over all ports
329 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
330 * port.
331 *
332 * @param t timeout
333 * @throws Exception
334 */
335 @Override
336 public void run(final Timeout t) {
337 this.log.debug("sending probes");
338 synchronized (this) {
339 final Iterator<Integer> fastIterator = this.fastPorts.iterator();
340 while (fastIterator.hasNext()) {
341 final Integer portNumber = fastIterator.next();
Yuta HIGUCHI70ce8432014-10-15 23:59:10 -0700342 OFPortDesc port = findPort(portNumber);
343 if (port == null) {
344 // port can be null
345 // #removePort modifies `ports` outside synchronized block
346 continue;
347 }
alshabibdf652ad2014-09-09 11:53:19 -0700348 final int probeCount = this.portProbeCount.get(portNumber)
349 .getAndIncrement();
alshabibdf652ad2014-09-09 11:53:19 -0700350 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
351 this.log.debug("sending fast probe to port");
352
353 OFPacketOut pkt = this.createLLDPPacketOut(port);
354 this.sendMsg(pkt);
355 if (useBDDP) {
356 OFPacketOut bpkt = this.createBDDPPacketOut(port);
357 this.sendMsg(bpkt);
358 }
359 } else {
360 // Update fast and slow ports
361 fastIterator.remove();
362 this.slowPorts.add(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700363 this.portProbeCount.remove(portNumber);
364
365 // Remove link from topology
366 final OFPortDesc srcPort = port;
367
368 ConnectPoint cp = new ConnectPoint(
tom782a7cf2014-09-11 23:58:38 -0700369 DeviceId.deviceId(uri(sw.getId())),
alshabibdf652ad2014-09-09 11:53:19 -0700370 PortNumber.portNumber(srcPort.getPortNo().getPortNumber()));
371 linkProvider.linksVanished(cp);
372 }
373 }
374
375 // send a probe for the next slow port
alshabib818d57c2014-09-15 22:34:30 -0700376 if (!this.slowPorts.isEmpty()) {
377 this.slowIterator = this.slowPorts.iterator();
378 while (this.slowIterator.hasNext()) {
alshabibdf652ad2014-09-09 11:53:19 -0700379 final int portNumber = this.slowIterator.next();
380 this.log.debug("sending slow probe to port {}", portNumber);
alshabib9ee68172014-09-09 14:45:14 -0700381 OFPortDesc port = findPort(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700382
383 OFPacketOut pkt = this.createLLDPPacketOut(port);
384 this.sendMsg(pkt);
385 if (useBDDP) {
386 OFPacketOut bpkt = this.createBDDPPacketOut(port);
387 this.sendMsg(bpkt);
388 }
389
390 }
391 }
392 }
393
394 // reschedule timer
alshabibc944fd02014-09-10 17:55:17 -0700395 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
alshabibdf652ad2014-09-09 11:53:19 -0700396 TimeUnit.MILLISECONDS);
397 }
398
399 public void removeAllPorts() {
alshabibc944fd02014-09-10 17:55:17 -0700400 for (OFPortDesc port : ports.values()) {
alshabiba159a322014-09-09 14:50:51 -0700401 removePort(port);
alshabibdf652ad2014-09-09 11:53:19 -0700402 }
403 }
404
alshabibc944fd02014-09-10 17:55:17 -0700405 public void stop() {
alshabibc944fd02014-09-10 17:55:17 -0700406 timeout.cancel();
alshabib89e43ef2014-09-16 10:36:34 -0700407 removeAllPorts();
alshabibc944fd02014-09-10 17:55:17 -0700408 }
409
alshabibdf652ad2014-09-09 11:53:19 -0700410}