blob: b0b86f0d7f9d419e4fa96f83cb42144aa6623a99 [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
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.util.Collections;
21import java.util.HashMap;
22import java.util.HashSet;
23import java.util.Iterator;
alshabibdf652ad2014-09-09 11:53:19 -070024import java.util.Map;
25import java.util.Set;
alshabib9ee68172014-09-09 14:45:14 -070026import java.util.concurrent.ConcurrentHashMap;
alshabibdf652ad2014-09-09 11:53:19 -070027import java.util.concurrent.TimeUnit;
28import java.util.concurrent.atomic.AtomicInteger;
29
30import org.jboss.netty.util.Timeout;
31import org.jboss.netty.util.TimerTask;
32import org.onlab.onos.net.ConnectPoint;
33import org.onlab.onos.net.DeviceId;
34import org.onlab.onos.net.Link.Type;
35import org.onlab.onos.net.PortNumber;
36import org.onlab.onos.net.link.DefaultLinkDescription;
37import org.onlab.onos.net.link.LinkDescription;
38import org.onlab.onos.net.link.LinkProviderService;
39import org.onlab.onos.of.controller.Dpid;
40import org.onlab.onos.of.controller.OpenFlowController;
41import org.onlab.onos.of.controller.OpenFlowSwitch;
42import org.onlab.packet.Ethernet;
43import org.onlab.packet.ONLabLddp;
44import org.onlab.packet.ONLabLddp.DPIDandPort;
45import org.onlab.timer.Timer;
46import org.projectfloodlight.openflow.protocol.OFFactory;
47import org.projectfloodlight.openflow.protocol.OFMessage;
48import org.projectfloodlight.openflow.protocol.OFPacketOut;
49import org.projectfloodlight.openflow.protocol.OFPortDesc;
50import org.projectfloodlight.openflow.protocol.action.OFAction;
51import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
52import org.projectfloodlight.openflow.types.OFBufferId;
53import org.projectfloodlight.openflow.types.OFPort;
54import org.slf4j.Logger;
55
56
57
58/**
59 * Run discovery process from a physical switch. Ports are initially labeled as
60 * slow ports. When an LLDP is successfully received, label the remote port as
61 * fast. Every probeRate milliseconds, loop over all fast ports and send an
62 * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
63 * discovery implementation.
64 *
65 * TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen
66 * for flow_removed messages
67 */
68public class LinkDiscovery implements TimerTask {
69
70 private final OpenFlowSwitch sw;
71 // send 1 probe every probeRate milliseconds
72 private final long probeRate;
73 private final Set<Integer> slowPorts;
74 private final Set<Integer> fastPorts;
75 // number of unacknowledged probes per port
76 private final Map<Integer, AtomicInteger> portProbeCount;
77 // number of probes to send before link is removed
78 private static final short MAX_PROBE_COUNT = 3;
79 private Iterator<Integer> slowIterator;
80 private final OFFactory ofFactory;
81 private final Logger log = getLogger(getClass());
82 private final ONLabLddp lldpPacket;
83 private final Ethernet ethPacket;
84 private Ethernet bddpEth;
85 private final boolean useBDDP;
86 private final OpenFlowController ctrl;
87 private final LinkProviderService linkProvider;
alshabib9ee68172014-09-09 14:45:14 -070088 private final Map<Integer, OFPortDesc> ports;
alshabibc944fd02014-09-10 17:55:17 -070089 private Timeout timeout;
alshabibdf652ad2014-09-09 11:53:19 -070090
91 /**
92 * Instantiates discovery manager for the given physical switch. Creates a
93 * generic LLDP packet that will be customized for the port it is sent out on.
94 * Starts the the timer for the discovery process.
95 *
96 * @param sw the physical switch
97 * @param useBDDP flag to also use BDDP for discovery
98 */
99 public LinkDiscovery(final OpenFlowSwitch sw,
100 OpenFlowController ctrl, LinkProviderService providerService, Boolean... useBDDP) {
101 this.sw = sw;
102 this.ofFactory = sw.factory();
103 this.ctrl = ctrl;
104 this.probeRate = 1000;
105 this.linkProvider = providerService;
106 this.slowPorts = Collections.synchronizedSet(new HashSet<Integer>());
107 this.fastPorts = Collections.synchronizedSet(new HashSet<Integer>());
alshabib9ee68172014-09-09 14:45:14 -0700108 this.ports = new ConcurrentHashMap<>();
alshabibdf652ad2014-09-09 11:53:19 -0700109 this.portProbeCount = new HashMap<Integer, AtomicInteger>();
110 this.lldpPacket = new ONLabLddp();
111 this.lldpPacket.setSwitch(this.sw.getId());
112 this.ethPacket = new Ethernet();
113 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
114 this.ethPacket.setDestinationMACAddress(ONLabLddp.LLDP_NICIRA);
115 this.ethPacket.setPayload(this.lldpPacket);
116 this.ethPacket.setPad(true);
117 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
118 if (this.useBDDP) {
119 this.bddpEth = new Ethernet();
120 this.bddpEth.setPayload(this.lldpPacket);
121 this.bddpEth.setEtherType(Ethernet.TYPE_BSN);
122 this.bddpEth.setDestinationMACAddress(ONLabLddp.BDDP_MULTICAST);
123 this.bddpEth.setPad(true);
124 log.info("Using BDDP to discover network");
125 }
126 for (OFPortDesc port : sw.getPorts()) {
127 if (port.getPortNo() != OFPort.LOCAL) {
128 addPort(port);
129 }
130 }
alshabibc944fd02014-09-10 17:55:17 -0700131 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
alshabibdf652ad2014-09-09 11:53:19 -0700132 TimeUnit.MILLISECONDS);
133 this.log.debug("Started discovery manager for switch {}",
134 sw.getId());
135
136 }
137
138 /**
139 * Add physical port port to discovery process.
140 * Send out initial LLDP and label it as slow port.
141 *
142 * @param port the port
143 */
144 public void addPort(final OFPortDesc port) {
145 // Ignore ports that are not on this switch, or already booted. */
alshabib9ee68172014-09-09 14:45:14 -0700146 this.ports.put(port.getPortNo().getPortNumber(), port);
alshabibdf652ad2014-09-09 11:53:19 -0700147 synchronized (this) {
148 this.log.debug("sending init probe to port {}",
149 port.getPortNo().getPortNumber());
150 OFPacketOut pkt;
151
152 pkt = this.createLLDPPacketOut(port);
153 this.sw.sendMsg(pkt);
154 if (useBDDP) {
155 OFPacketOut bpkt = this.createBDDPPacketOut(port);
156 this.sw.sendMsg(bpkt);
157 }
158
159 this.slowPorts.add(port.getPortNo().getPortNumber());
160 this.slowIterator = this.slowPorts.iterator();
161 }
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);
178 this.slowIterator = this.slowPorts.iterator();
179
180 } else if (this.fastPorts.contains(portnum)) {
181 this.fastPorts.remove(portnum);
182 this.portProbeCount.remove(portnum);
183 // no iterator to update
184 } else {
185 this.log.warn(
186 "tried to dynamically remove non-existing port {}",
187 portnum);
188 }
189 }
alshabibc944fd02014-09-10 17:55:17 -0700190 ConnectPoint cp = new ConnectPoint(
191 DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
192 PortNumber.portNumber(port.getPortNo().getPortNumber()));
193 linkProvider.linksVanished(cp);
alshabibdf652ad2014-09-09 11:53:19 -0700194
195 }
196
197 /**
198 * Method called by remote port to acknowledge receipt of LLDP sent by
199 * this port. If slow port, updates label to fast. If fast port, decrements
200 * number of unacknowledged probes.
201 *
202 * @param port the port
203 */
204 public void ackProbe(final Integer port) {
205 final int portNumber = port;
206 synchronized (this) {
207 if (this.slowPorts.contains(portNumber)) {
208 this.log.debug("Setting slow port to fast: {}:{}",
209 this.sw.getId(), portNumber);
210 this.slowPorts.remove(portNumber);
211 this.slowIterator = this.slowPorts.iterator();
212 this.fastPorts.add(portNumber);
213 this.portProbeCount.put(portNumber, new AtomicInteger(0));
214 } else {
215 if (this.fastPorts.contains(portNumber)) {
216 this.portProbeCount.get(portNumber).set(0);
217 } else {
218 this.log.debug(
219 "Got ackProbe for non-existing port: {}",
220 portNumber);
221 }
222 }
223 }
224 }
225
226 /**
227 * Creates packet_out LLDP for specified output port.
228 *
229 * @param port the port
230 * @return Packet_out message with LLDP data
231 * @throws PortMappingException
232 */
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
252 * @throws PortMappingException
253 */
254 private OFPacketOut createBDDPPacketOut(final OFPortDesc port) {
255 OFPacketOut.Builder packetOut = sw.factory().buildPacketOut();
256
257 packetOut.setBufferId(OFBufferId.NO_BUFFER);
258
259 OFActionOutput.Builder act = sw.factory().actions().buildOutput()
260 .setPort(port.getPortNo());
261 OFAction out = act.build();
262 packetOut.setActions(Collections.singletonList(out));
263 this.lldpPacket.setPort(port.getPortNo().getPortNumber());
264 this.bddpEth.setSourceMACAddress(port.getHwAddr().getBytes());
265
266 final byte[] bddp = this.bddpEth.serialize();
267 packetOut.setData(bddp);
268
269 return packetOut.build();
270 }
271
272
273 private void sendMsg(final OFMessage msg) {
274 this.sw.sendMsg(msg);
275 }
276
277 public String getName() {
278 return "LinkDiscovery " + this.sw.getStringId();
279 }
280
281 /*
282 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
283 * to port where LLDP originated.
284 */
alshabib505bc6b2014-09-09 15:04:13 -0700285 public boolean handleLLDP(final byte[] pkt, Integer inPort) {
alshabibdf652ad2014-09-09 11:53:19 -0700286
alshabib9ee68172014-09-09 14:45:14 -0700287 short ethType = ONLabLddp.isOVXLLDP(pkt);
288 if (ethType == Ethernet.TYPE_LLDP || ethType == Ethernet.TYPE_BSN) {
alshabibdf652ad2014-09-09 11:53:19 -0700289 final Integer dstPort = inPort;
290 final DPIDandPort dp = ONLabLddp.parseLLDP(pkt);
291 final OpenFlowSwitch srcSwitch = ctrl.getSwitch(new Dpid(dp.getDpid()));
292 final Integer srcPort = dp.getPort();
293 if (srcSwitch == null) {
alshabib505bc6b2014-09-09 15:04:13 -0700294 return true;
alshabibdf652ad2014-09-09 11:53:19 -0700295 }
296 this.ackProbe(srcPort);
297 ConnectPoint src = new ConnectPoint(
298 DeviceId.deviceId("of:" + Long.toHexString(srcSwitch.getId())),
299 PortNumber.portNumber(srcPort));
300
301 ConnectPoint dst = new ConnectPoint(
302 DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
303 PortNumber.portNumber(dstPort));
304 LinkDescription ld;
alshabib9ee68172014-09-09 14:45:14 -0700305 if (ethType == Ethernet.TYPE_BSN) {
alshabibdf652ad2014-09-09 11:53:19 -0700306 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
307 } else {
308 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
309 }
310 linkProvider.linkDetected(ld);
alshabib505bc6b2014-09-09 15:04:13 -0700311 return true;
alshabibdf652ad2014-09-09 11:53:19 -0700312 } else {
313 this.log.debug("Ignoring unknown LLDP");
alshabib505bc6b2014-09-09 15:04:13 -0700314 return false;
alshabibdf652ad2014-09-09 11:53:19 -0700315 }
316 }
317
alshabib9ee68172014-09-09 14:45:14 -0700318 private OFPortDesc findPort(Integer inPort) {
319 return ports.get(inPort);
alshabibdf652ad2014-09-09 11:53:19 -0700320 }
321
322 /**
323 * Execute this method every t milliseconds. Loops over all ports
324 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
325 * port.
326 *
327 * @param t timeout
328 * @throws Exception
329 */
330 @Override
331 public void run(final Timeout t) {
332 this.log.debug("sending probes");
333 synchronized (this) {
334 final Iterator<Integer> fastIterator = this.fastPorts.iterator();
335 while (fastIterator.hasNext()) {
336 final Integer portNumber = fastIterator.next();
337 final int probeCount = this.portProbeCount.get(portNumber)
338 .getAndIncrement();
alshabib9ee68172014-09-09 14:45:14 -0700339 OFPortDesc port = findPort(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700340 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
341 this.log.debug("sending fast probe to port");
342
343 OFPacketOut pkt = this.createLLDPPacketOut(port);
344 this.sendMsg(pkt);
345 if (useBDDP) {
346 OFPacketOut bpkt = this.createBDDPPacketOut(port);
347 this.sendMsg(bpkt);
348 }
349 } else {
350 // Update fast and slow ports
351 fastIterator.remove();
352 this.slowPorts.add(portNumber);
353 this.slowIterator = this.slowPorts.iterator();
354 this.portProbeCount.remove(portNumber);
355
356 // Remove link from topology
357 final OFPortDesc srcPort = port;
358
359 ConnectPoint cp = new ConnectPoint(
360 DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
361 PortNumber.portNumber(srcPort.getPortNo().getPortNumber()));
362 linkProvider.linksVanished(cp);
363 }
364 }
365
366 // send a probe for the next slow port
367 if (this.slowPorts.size() > 0) {
368 if (!this.slowIterator.hasNext()) {
369 this.slowIterator = this.slowPorts.iterator();
370 }
371 if (this.slowIterator.hasNext()) {
372 final int portNumber = this.slowIterator.next();
373 this.log.debug("sending slow probe to port {}", portNumber);
alshabib9ee68172014-09-09 14:45:14 -0700374 OFPortDesc port = findPort(portNumber);
alshabibdf652ad2014-09-09 11:53:19 -0700375
376 OFPacketOut pkt = this.createLLDPPacketOut(port);
377 this.sendMsg(pkt);
378 if (useBDDP) {
379 OFPacketOut bpkt = this.createBDDPPacketOut(port);
380 this.sendMsg(bpkt);
381 }
382
383 }
384 }
385 }
386
387 // reschedule timer
alshabibc944fd02014-09-10 17:55:17 -0700388 timeout = Timer.getTimer().newTimeout(this, this.probeRate,
alshabibdf652ad2014-09-09 11:53:19 -0700389 TimeUnit.MILLISECONDS);
390 }
391
392 public void removeAllPorts() {
alshabibc944fd02014-09-10 17:55:17 -0700393 for (OFPortDesc port : ports.values()) {
alshabiba159a322014-09-09 14:50:51 -0700394 removePort(port);
alshabibdf652ad2014-09-09 11:53:19 -0700395 }
396 }
397
alshabibc944fd02014-09-10 17:55:17 -0700398 public void stop() {
399 removeAllPorts();
400 timeout.cancel();
401 }
402
alshabibdf652ad2014-09-09 11:53:19 -0700403}