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