blob: 9396f919e202dcfd081cd737b1242b29fe4171c4 [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;
24import java.util.List;
25import java.util.Map;
26import java.util.Set;
27import 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;
88
89 /**
90 * Instantiates discovery manager for the given physical switch. Creates a
91 * generic LLDP packet that will be customized for the port it is sent out on.
92 * Starts the the timer for the discovery process.
93 *
94 * @param sw the physical switch
95 * @param useBDDP flag to also use BDDP for discovery
96 */
97 public LinkDiscovery(final OpenFlowSwitch sw,
98 OpenFlowController ctrl, LinkProviderService providerService, Boolean... useBDDP) {
99 this.sw = sw;
100 this.ofFactory = sw.factory();
101 this.ctrl = ctrl;
102 this.probeRate = 1000;
103 this.linkProvider = providerService;
104 this.slowPorts = Collections.synchronizedSet(new HashSet<Integer>());
105 this.fastPorts = Collections.synchronizedSet(new HashSet<Integer>());
106 this.portProbeCount = new HashMap<Integer, AtomicInteger>();
107 this.lldpPacket = new ONLabLddp();
108 this.lldpPacket.setSwitch(this.sw.getId());
109 this.ethPacket = new Ethernet();
110 this.ethPacket.setEtherType(Ethernet.TYPE_LLDP);
111 this.ethPacket.setDestinationMACAddress(ONLabLddp.LLDP_NICIRA);
112 this.ethPacket.setPayload(this.lldpPacket);
113 this.ethPacket.setPad(true);
114 this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false;
115 if (this.useBDDP) {
116 this.bddpEth = new Ethernet();
117 this.bddpEth.setPayload(this.lldpPacket);
118 this.bddpEth.setEtherType(Ethernet.TYPE_BSN);
119 this.bddpEth.setDestinationMACAddress(ONLabLddp.BDDP_MULTICAST);
120 this.bddpEth.setPad(true);
121 log.info("Using BDDP to discover network");
122 }
123 for (OFPortDesc port : sw.getPorts()) {
124 if (port.getPortNo() != OFPort.LOCAL) {
125 addPort(port);
126 }
127 }
128 Timer.getTimer().newTimeout(this, this.probeRate,
129 TimeUnit.MILLISECONDS);
130 this.log.debug("Started discovery manager for switch {}",
131 sw.getId());
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 OFPortDesc port) {
142 // Ignore ports that are not on this switch, or already booted. */
143
144 synchronized (this) {
145 this.log.debug("sending init probe to port {}",
146 port.getPortNo().getPortNumber());
147 OFPacketOut pkt;
148
149 pkt = this.createLLDPPacketOut(port);
150 this.sw.sendMsg(pkt);
151 if (useBDDP) {
152 OFPacketOut bpkt = this.createBDDPPacketOut(port);
153 this.sw.sendMsg(bpkt);
154 }
155
156 this.slowPorts.add(port.getPortNo().getPortNumber());
157 this.slowIterator = this.slowPorts.iterator();
158 }
159
160 }
161
162 /**
163 * Removes physical port from discovery process.
164 *
165 * @param port the port
166 */
167 public void removePort(final OFPort port) {
168 // Ignore ports that are not on this switch
169
170 int portnum = port.getPortNumber();
171 synchronized (this) {
172 if (this.slowPorts.contains(portnum)) {
173 this.slowPorts.remove(portnum);
174 this.slowIterator = this.slowPorts.iterator();
175
176 } else if (this.fastPorts.contains(portnum)) {
177 this.fastPorts.remove(portnum);
178 this.portProbeCount.remove(portnum);
179 // no iterator to update
180 } else {
181 this.log.warn(
182 "tried to dynamically remove non-existing port {}",
183 portnum);
184 }
185 }
186
187 }
188
189 /**
190 * Method called by remote port to acknowledge receipt of LLDP sent by
191 * this port. If slow port, updates label to fast. If fast port, decrements
192 * number of unacknowledged probes.
193 *
194 * @param port the port
195 */
196 public void ackProbe(final Integer port) {
197 final int portNumber = port;
198 synchronized (this) {
199 if (this.slowPorts.contains(portNumber)) {
200 this.log.debug("Setting slow port to fast: {}:{}",
201 this.sw.getId(), portNumber);
202 this.slowPorts.remove(portNumber);
203 this.slowIterator = this.slowPorts.iterator();
204 this.fastPorts.add(portNumber);
205 this.portProbeCount.put(portNumber, new AtomicInteger(0));
206 } else {
207 if (this.fastPorts.contains(portNumber)) {
208 this.portProbeCount.get(portNumber).set(0);
209 } else {
210 this.log.debug(
211 "Got ackProbe for non-existing port: {}",
212 portNumber);
213 }
214 }
215 }
216 }
217
218 /**
219 * Creates packet_out LLDP for specified output port.
220 *
221 * @param port the port
222 * @return Packet_out message with LLDP data
223 * @throws PortMappingException
224 */
225 private OFPacketOut createLLDPPacketOut(final OFPortDesc port) {
226 OFPacketOut.Builder packetOut = this.ofFactory.buildPacketOut();
227 packetOut.setBufferId(OFBufferId.NO_BUFFER);
228 OFAction act = this.ofFactory.actions().buildOutput()
229 .setPort(port.getPortNo()).build();
230 packetOut.setActions(Collections.singletonList(act));
231 this.lldpPacket.setPort(port.getPortNo().getPortNumber());
232 this.ethPacket.setSourceMACAddress(port.getHwAddr().getBytes());
233
234 final byte[] lldp = this.ethPacket.serialize();
235 packetOut.setData(lldp);
236 return packetOut.build();
237 }
238
239 /**
240 * Creates packet_out BDDP for specified output port.
241 *
242 * @param port the port
243 * @return Packet_out message with LLDP data
244 * @throws PortMappingException
245 */
246 private OFPacketOut createBDDPPacketOut(final OFPortDesc port) {
247 OFPacketOut.Builder packetOut = sw.factory().buildPacketOut();
248
249 packetOut.setBufferId(OFBufferId.NO_BUFFER);
250
251 OFActionOutput.Builder act = sw.factory().actions().buildOutput()
252 .setPort(port.getPortNo());
253 OFAction out = act.build();
254 packetOut.setActions(Collections.singletonList(out));
255 this.lldpPacket.setPort(port.getPortNo().getPortNumber());
256 this.bddpEth.setSourceMACAddress(port.getHwAddr().getBytes());
257
258 final byte[] bddp = this.bddpEth.serialize();
259 packetOut.setData(bddp);
260
261 return packetOut.build();
262 }
263
264
265 private void sendMsg(final OFMessage msg) {
266 this.sw.sendMsg(msg);
267 }
268
269 public String getName() {
270 return "LinkDiscovery " + this.sw.getStringId();
271 }
272
273 /*
274 * Handles an incoming LLDP packet. Creates link in topology and sends ACK
275 * to port where LLDP originated.
276 */
277 @SuppressWarnings("rawtypes")
278 public void handleLLDP(final Ethernet eth, Integer inPort) {
279
280 final byte[] pkt = eth.serialize();
281
282 if (ONLabLddp.isOVXLLDP(pkt)) {
283 final Integer dstPort = inPort;
284 final DPIDandPort dp = ONLabLddp.parseLLDP(pkt);
285 final OpenFlowSwitch srcSwitch = ctrl.getSwitch(new Dpid(dp.getDpid()));
286 final Integer srcPort = dp.getPort();
287 if (srcSwitch == null) {
288 return;
289 }
290 this.ackProbe(srcPort);
291 ConnectPoint src = new ConnectPoint(
292 DeviceId.deviceId("of:" + Long.toHexString(srcSwitch.getId())),
293 PortNumber.portNumber(srcPort));
294
295 ConnectPoint dst = new ConnectPoint(
296 DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
297 PortNumber.portNumber(dstPort));
298 LinkDescription ld;
299 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
300 ld = new DefaultLinkDescription(src, dst, Type.INDIRECT);
301 } else {
302 ld = new DefaultLinkDescription(src, dst, Type.DIRECT);
303 }
304 linkProvider.linkDetected(ld);
305 } else {
306 this.log.debug("Ignoring unknown LLDP");
307 }
308 }
309
310 private OFPortDesc findPort(List<OFPortDesc> ports, Integer inPort) {
311 for (OFPortDesc p : ports) {
312 if (p.getPortNo().getPortNumber() == inPort) {
313 return p;
314 }
315 }
316 return null;
317 }
318
319 /**
320 * Execute this method every t milliseconds. Loops over all ports
321 * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
322 * port.
323 *
324 * @param t timeout
325 * @throws Exception
326 */
327 @Override
328 public void run(final Timeout t) {
329 this.log.debug("sending probes");
330 synchronized (this) {
331 final Iterator<Integer> fastIterator = this.fastPorts.iterator();
332 while (fastIterator.hasNext()) {
333 final Integer portNumber = fastIterator.next();
334 final int probeCount = this.portProbeCount.get(portNumber)
335 .getAndIncrement();
336 OFPortDesc port = findPort(this.sw.getPorts(), portNumber);
337 if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) {
338 this.log.debug("sending fast probe to port");
339
340 OFPacketOut pkt = this.createLLDPPacketOut(port);
341 this.sendMsg(pkt);
342 if (useBDDP) {
343 OFPacketOut bpkt = this.createBDDPPacketOut(port);
344 this.sendMsg(bpkt);
345 }
346 } else {
347 // Update fast and slow ports
348 fastIterator.remove();
349 this.slowPorts.add(portNumber);
350 this.slowIterator = this.slowPorts.iterator();
351 this.portProbeCount.remove(portNumber);
352
353 // Remove link from topology
354 final OFPortDesc srcPort = port;
355
356 ConnectPoint cp = new ConnectPoint(
357 DeviceId.deviceId("of:" + Long.toHexString(sw.getId())),
358 PortNumber.portNumber(srcPort.getPortNo().getPortNumber()));
359 linkProvider.linksVanished(cp);
360 }
361 }
362
363 // send a probe for the next slow port
364 if (this.slowPorts.size() > 0) {
365 if (!this.slowIterator.hasNext()) {
366 this.slowIterator = this.slowPorts.iterator();
367 }
368 if (this.slowIterator.hasNext()) {
369 final int portNumber = this.slowIterator.next();
370 this.log.debug("sending slow probe to port {}", portNumber);
371 OFPortDesc port = findPort(this.sw.getPorts(), portNumber);
372
373 OFPacketOut pkt = this.createLLDPPacketOut(port);
374 this.sendMsg(pkt);
375 if (useBDDP) {
376 OFPacketOut bpkt = this.createBDDPPacketOut(port);
377 this.sendMsg(bpkt);
378 }
379
380 }
381 }
382 }
383
384 // reschedule timer
385 Timer.getTimer().newTimeout(this, this.probeRate,
386 TimeUnit.MILLISECONDS);
387 }
388
389 public void removeAllPorts() {
390 for (OFPortDesc port : sw.getPorts()) {
391 removePort(port.getPortNo());
392 }
393 }
394
395}