alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame^] | 1 | /******************************************************************************* |
| 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 | ******************************************************************************/ |
| 16 | package org.onlab.onos.provider.lldp.impl; |
| 17 | |
| 18 | |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.Collections; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | import java.util.concurrent.atomic.AtomicInteger; |
| 30 | |
| 31 | import org.jboss.netty.util.Timeout; |
| 32 | import org.jboss.netty.util.TimerTask; |
| 33 | import org.onlab.onos.net.ConnectPoint; |
| 34 | import org.onlab.onos.net.Device; |
| 35 | import org.onlab.onos.net.DeviceId; |
| 36 | import org.onlab.onos.net.Link.Type; |
| 37 | import org.onlab.onos.net.Port; |
| 38 | import org.onlab.onos.net.PortNumber; |
| 39 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 40 | import org.onlab.onos.net.link.DefaultLinkDescription; |
| 41 | import org.onlab.onos.net.link.LinkDescription; |
| 42 | import org.onlab.onos.net.link.LinkProviderService; |
| 43 | import org.onlab.onos.net.packet.DefaultOutboundPacket; |
| 44 | import org.onlab.onos.net.packet.OutboundPacket; |
| 45 | import org.onlab.onos.net.packet.PacketContext; |
| 46 | import org.onlab.onos.net.packet.PacketService; |
| 47 | import org.onlab.packet.Ethernet; |
| 48 | import org.onlab.packet.ONOSLLDP; |
| 49 | import org.onlab.util.Timer; |
| 50 | import org.slf4j.Logger; |
| 51 | |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Run discovery process from a physical switch. Ports are initially labeled as |
| 56 | * slow ports. When an LLDP is successfully received, label the remote port as |
| 57 | * fast. Every probeRate milliseconds, loop over all fast ports and send an |
| 58 | * LLDP, send an LLDP for a single slow port. Based on FlowVisor topology |
| 59 | * discovery implementation. |
| 60 | * |
| 61 | * TODO: add 'fast discovery' mode: drop LLDPs in destination switch but listen |
| 62 | * for flow_removed messages |
| 63 | */ |
| 64 | public class LinkDiscovery implements TimerTask { |
| 65 | |
| 66 | private final Device device; |
| 67 | // send 1 probe every probeRate milliseconds |
| 68 | private final long probeRate; |
| 69 | private final Set<Long> slowPorts; |
| 70 | private final Set<Long> fastPorts; |
| 71 | // number of unacknowledged probes per port |
| 72 | private final Map<Long, AtomicInteger> portProbeCount; |
| 73 | // number of probes to send before link is removed |
| 74 | private static final short MAX_PROBE_COUNT = 3; |
| 75 | private final Logger log = getLogger(getClass()); |
| 76 | private final ONOSLLDP lldpPacket; |
| 77 | private final Ethernet ethPacket; |
| 78 | private Ethernet bddpEth; |
| 79 | private final boolean useBDDP; |
| 80 | private final LinkProviderService linkProvider; |
| 81 | private final PacketService pktService; |
| 82 | private Timeout timeout; |
| 83 | |
| 84 | /** |
| 85 | * Instantiates discovery manager for the given physical switch. Creates a |
| 86 | * generic LLDP packet that will be customized for the port it is sent out on. |
| 87 | * Starts the the timer for the discovery process. |
| 88 | * |
| 89 | * @param device the physical switch |
| 90 | * @param useBDDP flag to also use BDDP for discovery |
| 91 | */ |
| 92 | public LinkDiscovery(Device device, PacketService pktService, |
| 93 | LinkProviderService providerService, Boolean... useBDDP) { |
| 94 | this.device = device; |
| 95 | this.probeRate = 3000; |
| 96 | this.linkProvider = providerService; |
| 97 | this.pktService = pktService; |
| 98 | this.slowPorts = Collections.synchronizedSet(new HashSet<Long>()); |
| 99 | this.fastPorts = Collections.synchronizedSet(new HashSet<Long>()); |
| 100 | this.portProbeCount = new HashMap<>(); |
| 101 | this.lldpPacket = new ONOSLLDP(); |
| 102 | this.lldpPacket.setChassisId(device.chassisId()); |
| 103 | this.lldpPacket.setDevice(device.id().toString()); |
| 104 | |
| 105 | |
| 106 | this.ethPacket = new Ethernet(); |
| 107 | this.ethPacket.setEtherType(Ethernet.TYPE_LLDP); |
| 108 | this.ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA); |
| 109 | this.ethPacket.setPayload(this.lldpPacket); |
| 110 | this.ethPacket.setPad(true); |
| 111 | this.useBDDP = useBDDP.length > 0 ? useBDDP[0] : false; |
| 112 | if (this.useBDDP) { |
| 113 | this.bddpEth = new Ethernet(); |
| 114 | this.bddpEth.setPayload(this.lldpPacket); |
| 115 | this.bddpEth.setEtherType(Ethernet.TYPE_BSN); |
| 116 | this.bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST); |
| 117 | this.bddpEth.setPad(true); |
| 118 | log.info("Using BDDP to discover network"); |
| 119 | } |
| 120 | |
| 121 | start(); |
| 122 | this.log.debug("Started discovery manager for switch {}", |
| 123 | device.id()); |
| 124 | |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Add physical port port to discovery process. |
| 129 | * Send out initial LLDP and label it as slow port. |
| 130 | * |
| 131 | * @param port the port |
| 132 | */ |
| 133 | public void addPort(final Port port) { |
| 134 | this.log.debug("sending init probe to port {}", |
| 135 | port.number().toLong()); |
| 136 | |
| 137 | sendProbes(port.number().toLong()); |
| 138 | |
| 139 | synchronized (this) { |
| 140 | this.slowPorts.add(port.number().toLong()); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Removes physical port from discovery process. |
| 148 | * |
| 149 | * @param port the port |
| 150 | */ |
| 151 | public void removePort(final Port port) { |
| 152 | // Ignore ports that are not on this switch |
| 153 | |
| 154 | long portnum = port.number().toLong(); |
| 155 | synchronized (this) { |
| 156 | if (this.slowPorts.contains(portnum)) { |
| 157 | this.slowPorts.remove(portnum); |
| 158 | |
| 159 | } else if (this.fastPorts.contains(portnum)) { |
| 160 | this.fastPorts.remove(portnum); |
| 161 | this.portProbeCount.remove(portnum); |
| 162 | // no iterator to update |
| 163 | } else { |
| 164 | this.log.warn( |
| 165 | "tried to dynamically remove non-existing port {}", |
| 166 | portnum); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Method called by remote port to acknowledge receipt of LLDP sent by |
| 173 | * this port. If slow port, updates label to fast. If fast port, decrements |
| 174 | * number of unacknowledged probes. |
| 175 | * |
| 176 | * @param portNumber the port |
| 177 | */ |
| 178 | public void ackProbe(final Long portNumber) { |
| 179 | |
| 180 | synchronized (this) { |
| 181 | if (this.slowPorts.contains(portNumber)) { |
| 182 | this.log.debug("Setting slow port to fast: {}:{}", |
| 183 | this.device.id(), portNumber); |
| 184 | this.slowPorts.remove(portNumber); |
| 185 | this.fastPorts.add(portNumber); |
| 186 | this.portProbeCount.put(portNumber, new AtomicInteger(0)); |
| 187 | } else { |
| 188 | if (this.fastPorts.contains(portNumber)) { |
| 189 | this.portProbeCount.get(portNumber).set(0); |
| 190 | } else { |
| 191 | this.log.debug( |
| 192 | "Got ackProbe for non-existing port: {}", |
| 193 | portNumber); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /** |
| 201 | * Handles an incoming LLDP packet. Creates link in topology and sends ACK |
| 202 | * to port where LLDP originated. |
| 203 | */ |
| 204 | public boolean handleLLDP(PacketContext context) { |
| 205 | Ethernet eth = context.inPacket().parsed(); |
| 206 | ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth); |
| 207 | if (onoslldp != null) { |
| 208 | final PortNumber dstPort = |
| 209 | context.inPacket().receivedFrom().port(); |
| 210 | final PortNumber srcPort = PortNumber.portNumber(onoslldp.getPort()); |
| 211 | final DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString()); |
| 212 | final DeviceId dstDeviceId = context.inPacket().receivedFrom().deviceId(); |
| 213 | this.ackProbe(srcPort.toLong()); |
| 214 | ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort); |
| 215 | ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort); |
| 216 | |
| 217 | LinkDescription ld; |
| 218 | if (eth.getEtherType() == Ethernet.TYPE_BSN) { |
| 219 | ld = new DefaultLinkDescription(src, dst, Type.INDIRECT); |
| 220 | } else { |
| 221 | ld = new DefaultLinkDescription(src, dst, Type.DIRECT); |
| 222 | } |
| 223 | linkProvider.linkDetected(ld); |
| 224 | return true; |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | /** |
| 232 | * Execute this method every t milliseconds. Loops over all ports |
| 233 | * labeled as fast and sends out an LLDP. Send out an LLDP on a single slow |
| 234 | * port. |
| 235 | * |
| 236 | * @param t timeout |
| 237 | * @throws Exception |
| 238 | */ |
| 239 | @Override |
| 240 | public void run(final Timeout t) { |
| 241 | this.log.debug("sending probes"); |
| 242 | synchronized (this) { |
| 243 | final Iterator<Long> fastIterator = this.fastPorts.iterator(); |
| 244 | Long portNumber; |
| 245 | Integer probeCount; |
| 246 | while (fastIterator.hasNext()) { |
| 247 | portNumber = fastIterator.next(); |
| 248 | probeCount = this.portProbeCount.get(portNumber) |
| 249 | .getAndIncrement(); |
| 250 | |
| 251 | if (probeCount < LinkDiscovery.MAX_PROBE_COUNT) { |
| 252 | this.log.debug("sending fast probe to port"); |
| 253 | sendProbes(portNumber); |
| 254 | } else { |
| 255 | // Update fast and slow ports |
| 256 | //fastIterator.remove(); |
| 257 | //this.slowPorts.add(portNumber); |
| 258 | //this.portProbeCount.remove(portNumber); |
| 259 | |
| 260 | |
| 261 | ConnectPoint cp = new ConnectPoint( |
| 262 | device.id(), |
| 263 | PortNumber.portNumber(portNumber)); |
| 264 | log.debug("Link down -> {}", cp); |
| 265 | linkProvider.linksVanished(cp); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // send a probe for the next slow port |
| 270 | if (!this.slowPorts.isEmpty()) { |
| 271 | Iterator<Long> slowIterator = this.slowPorts.iterator(); |
| 272 | while (slowIterator.hasNext()) { |
| 273 | portNumber = slowIterator.next(); |
| 274 | this.log.debug("sending slow probe to port {}", portNumber); |
| 275 | |
| 276 | sendProbes(portNumber); |
| 277 | |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // reschedule timer |
| 283 | timeout = Timer.getTimer().newTimeout(this, this.probeRate, |
| 284 | TimeUnit.MILLISECONDS); |
| 285 | } |
| 286 | |
| 287 | public void stop() { |
| 288 | timeout.cancel(); |
| 289 | } |
| 290 | |
| 291 | public void start() { |
| 292 | timeout = Timer.getTimer().newTimeout(this, 0, |
| 293 | TimeUnit.MILLISECONDS); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Creates packet_out LLDP for specified output port. |
| 298 | * |
| 299 | * @param port the port |
| 300 | * @return Packet_out message with LLDP data |
| 301 | */ |
| 302 | private OutboundPacket createOutBoundLLDP(final Long port) { |
| 303 | if (port == null) { |
| 304 | return null; |
| 305 | } |
| 306 | this.lldpPacket.setPortId(port.intValue()); |
| 307 | this.ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11"); |
| 308 | |
| 309 | final byte[] lldp = this.ethPacket.serialize(); |
| 310 | OutboundPacket outboundPacket = new DefaultOutboundPacket( |
| 311 | this.device.id(), |
| 312 | DefaultTrafficTreatment.builder().setOutput( |
| 313 | PortNumber.portNumber(port)).build(), |
| 314 | ByteBuffer.wrap(lldp)); |
| 315 | return outboundPacket; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Creates packet_out BDDP for specified output port. |
| 320 | * |
| 321 | * @param port the port |
| 322 | * @return Packet_out message with LLDP data |
| 323 | */ |
| 324 | private OutboundPacket createOutBoundBDDP(final Long port) { |
| 325 | if (port == null) { |
| 326 | return null; |
| 327 | } |
| 328 | this.lldpPacket.setPortId(port.intValue()); |
| 329 | this.bddpEth.setSourceMACAddress("DE:AD:BE:EF:BA:11"); |
| 330 | |
| 331 | final byte[] bddp = this.bddpEth.serialize(); |
| 332 | OutboundPacket outboundPacket = new DefaultOutboundPacket( |
| 333 | this.device.id(), |
| 334 | DefaultTrafficTreatment.builder() |
| 335 | .setOutput(PortNumber.portNumber(port)).build(), |
| 336 | ByteBuffer.wrap(bddp)); |
| 337 | return outboundPacket; |
| 338 | } |
| 339 | |
| 340 | private void sendProbes(Long portNumber) { |
| 341 | OutboundPacket pkt = this.createOutBoundLLDP(portNumber); |
| 342 | pktService.emit(pkt); |
| 343 | if (useBDDP) { |
| 344 | OutboundPacket bpkt = this.createOutBoundBDDP(portNumber); |
| 345 | pktService.emit(bpkt); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | } |