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