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