Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 18 | package net.onrc.onos.ofcontroller.linkdiscovery.internal; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.net.InetAddress; |
| 22 | import java.net.InetSocketAddress; |
| 23 | import java.net.NetworkInterface; |
| 24 | import java.net.SocketAddress; |
| 25 | import java.nio.ByteBuffer; |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Collection; |
| 28 | import java.util.Collections; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.HashSet; |
| 31 | import java.util.Iterator; |
| 32 | import java.util.List; |
| 33 | import java.util.Map; |
| 34 | import java.util.Map.Entry; |
| 35 | import java.util.Set; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | import java.util.concurrent.LinkedBlockingQueue; |
| 37 | import java.util.concurrent.ScheduledExecutorService; |
| 38 | import java.util.concurrent.TimeUnit; |
| 39 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 40 | |
| 41 | import net.floodlightcontroller.core.FloodlightContext; |
| 42 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 43 | import net.floodlightcontroller.core.IOFMessageListener; |
| 44 | import net.floodlightcontroller.core.IOFSwitch; |
| 45 | import net.floodlightcontroller.core.IOFSwitchListener; |
| 46 | import net.floodlightcontroller.core.annotations.LogMessageCategory; |
| 47 | import net.floodlightcontroller.core.annotations.LogMessageDoc; |
| 48 | import net.floodlightcontroller.core.annotations.LogMessageDocs; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 49 | import net.floodlightcontroller.core.internal.OFSwitchImpl; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 50 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 51 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 52 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 53 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 54 | import net.floodlightcontroller.core.util.SingletonTask; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 55 | import net.floodlightcontroller.packet.BSN; |
| 56 | import net.floodlightcontroller.packet.Ethernet; |
| 57 | import net.floodlightcontroller.packet.IPv4; |
| 58 | import net.floodlightcontroller.packet.LLDP; |
| 59 | import net.floodlightcontroller.packet.LLDPTLV; |
| 60 | import net.floodlightcontroller.restserver.IRestApiService; |
| 61 | import net.floodlightcontroller.routing.Link; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 62 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
| 63 | import net.floodlightcontroller.topology.NodePortTuple; |
| 64 | import net.floodlightcontroller.util.EventHistory; |
| 65 | import net.floodlightcontroller.util.EventHistory.EvAction; |
HIGUCHI Yuta | 7677a6f | 2013-06-14 14:13:35 -0700 | [diff] [blame] | 66 | import net.onrc.onos.ofcontroller.core.IOnosRemoteSwitch; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 67 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 68 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery.LDUpdate; |
| 69 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscovery.UpdateOperation; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 70 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryListener; |
| 71 | import net.onrc.onos.ofcontroller.linkdiscovery.ILinkDiscoveryService; |
| 72 | import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo; |
HIGUCHI Yuta | a56fbde | 2013-06-17 14:26:05 -0700 | [diff] [blame] | 73 | import net.onrc.onos.ofcontroller.linkdiscovery.web.LinkDiscoveryWebRoutable; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 74 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 75 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 76 | import org.openflow.protocol.OFMessage; |
| 77 | import org.openflow.protocol.OFPacketIn; |
| 78 | import org.openflow.protocol.OFPacketOut; |
| 79 | import org.openflow.protocol.OFPhysicalPort; |
| 80 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
| 81 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
| 82 | import org.openflow.protocol.OFPort; |
| 83 | import org.openflow.protocol.OFPortStatus; |
| 84 | import org.openflow.protocol.OFPortStatus.OFPortReason; |
| 85 | import org.openflow.protocol.OFType; |
| 86 | import org.openflow.protocol.action.OFAction; |
| 87 | import org.openflow.protocol.action.OFActionOutput; |
| 88 | import org.openflow.util.HexString; |
| 89 | import org.slf4j.Logger; |
| 90 | import org.slf4j.LoggerFactory; |
| 91 | |
| 92 | /** |
| 93 | * This class sends out LLDP messages containing the sending switch's datapath |
| 94 | * id as well as the outgoing port number. Received LLrescDP messages that |
| 95 | * match a known switch cause a new LinkTuple to be created according to the |
| 96 | * invariant rules listed below. This new LinkTuple is also passed to routing |
| 97 | * if it exists to trigger updates. |
| 98 | * |
| 99 | * This class also handles removing links that are associated to switch ports |
| 100 | * that go down, and switches that are disconnected. |
| 101 | * |
| 102 | * Invariants: |
| 103 | * -portLinks and switchLinks will not contain empty Sets outside of |
| 104 | * critical sections |
| 105 | * -portLinks contains LinkTuples where one of the src or dst |
| 106 | * SwitchPortTuple matches the map key |
| 107 | * -switchLinks contains LinkTuples where one of the src or dst |
| 108 | * SwitchPortTuple's id matches the switch id |
| 109 | * -Each LinkTuple will be indexed into switchLinks for both |
| 110 | * src.id and dst.id, and portLinks for each src and dst |
| 111 | * -The updates queue is only added to from within a held write lock |
| 112 | */ |
| 113 | @LogMessageCategory("Network Topology") |
| 114 | public class LinkDiscoveryManager |
| 115 | implements IOFMessageListener, IOFSwitchListener, |
Jonathan Hart | ec4f14e | 2013-12-12 10:46:38 -0800 | [diff] [blame] | 116 | ILinkDiscoveryService, IFloodlightModule { |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 117 | protected IFloodlightProviderService controller; |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 118 | protected final static Logger log = LoggerFactory.getLogger(LinkDiscoveryManager.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 119 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 120 | protected IFloodlightProviderService floodlightProvider; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 121 | protected IThreadPoolService threadPool; |
| 122 | protected IRestApiService restApi; |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 123 | // Registry Service for ONOS |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 124 | protected IControllerRegistryService registryService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 125 | |
HIGUCHI Yuta | e0515e5 | 2013-06-14 13:00:40 -0700 | [diff] [blame] | 126 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 127 | // LLDP and BDDP fields |
| 128 | private static final byte[] LLDP_STANDARD_DST_MAC_STRING = |
| 129 | HexString.fromHexString("01:80:c2:00:00:0e"); |
| 130 | private static final long LINK_LOCAL_MASK = 0xfffffffffff0L; |
| 131 | private static final long LINK_LOCAL_VALUE = 0x0180c2000000L; |
| 132 | |
| 133 | // BigSwitch OUI is 5C:16:C7, so 5D:16:C7 is the multicast version |
Masayoshi Kobayashi | 7113736 | 2013-07-12 15:54:52 -0700 | [diff] [blame] | 134 | // private static final String LLDP_BSN_DST_MAC_STRING = "5d:16:c7:00:00:01"; |
| 135 | private static final String LLDP_BSN_DST_MAC_STRING = "ff:ff:ff:ff:ff:ff"; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 136 | |
| 137 | |
| 138 | // Direction TLVs are used to indicate if the LLDPs were sent |
| 139 | // periodically or in response to a recieved LLDP |
| 140 | private static final byte TLV_DIRECTION_TYPE = 0x73; |
| 141 | private static final short TLV_DIRECTION_LENGTH = 1; // 1 byte |
| 142 | private static final byte TLV_DIRECTION_VALUE_FORWARD[] = {0x01}; |
| 143 | private static final byte TLV_DIRECTION_VALUE_REVERSE[] = {0x02}; |
| 144 | private static final LLDPTLV forwardTLV |
| 145 | = new LLDPTLV(). |
| 146 | setType((byte)TLV_DIRECTION_TYPE). |
| 147 | setLength((short)TLV_DIRECTION_LENGTH). |
| 148 | setValue(TLV_DIRECTION_VALUE_FORWARD); |
| 149 | |
| 150 | private static final LLDPTLV reverseTLV |
| 151 | = new LLDPTLV(). |
| 152 | setType((byte)TLV_DIRECTION_TYPE). |
| 153 | setLength((short)TLV_DIRECTION_LENGTH). |
| 154 | setValue(TLV_DIRECTION_VALUE_REVERSE); |
| 155 | |
| 156 | // Link discovery task details. |
| 157 | protected SingletonTask discoveryTask; |
| 158 | protected final int DISCOVERY_TASK_INTERVAL = 1; |
Umesh Krishnaswamy | 7760120 | 2013-04-03 21:46:01 -0700 | [diff] [blame] | 159 | protected final int LINK_TIMEOUT = 35; // original 35 secs, aggressive 5 secs |
| 160 | protected final int LLDP_TO_ALL_INTERVAL = 15 ; //original 15 seconds, aggressive 2 secs. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 161 | protected long lldpClock = 0; |
| 162 | // This value is intentionally kept higher than LLDP_TO_ALL_INTERVAL. |
| 163 | // If we want to identify link failures faster, we could decrease this |
| 164 | // value to a small number, say 1 or 2 sec. |
Umesh Krishnaswamy | 7760120 | 2013-04-03 21:46:01 -0700 | [diff] [blame] | 165 | protected final int LLDP_TO_KNOWN_INTERVAL= 20; // LLDP frequency for known links |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 166 | |
| 167 | protected LLDPTLV controllerTLV; |
| 168 | protected ReentrantReadWriteLock lock; |
| 169 | int lldpTimeCount = 0; |
HIGUCHI Yuta | e0515e5 | 2013-06-14 13:00:40 -0700 | [diff] [blame] | 170 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 171 | /** |
| 172 | * Flag to indicate if automatic port fast is enabled or not. |
| 173 | * Default is set to false -- Initialized in the init method as well. |
| 174 | */ |
| 175 | boolean autoPortFastFeature = false; |
| 176 | |
| 177 | /** |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 178 | * Map of remote switches that are not connected to this controller. This |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 179 | * is used to learn remote switches in a distributed controller ONOS. |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 180 | */ |
HIGUCHI Yuta | 7677a6f | 2013-06-14 14:13:35 -0700 | [diff] [blame] | 181 | protected Map<Long, IOnosRemoteSwitch> remoteSwitches; |
HIGUCHI Yuta | 3d96f65 | 2013-06-17 12:07:48 -0700 | [diff] [blame] | 182 | |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 183 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 184 | * Map from link to the most recent time it was verified functioning |
| 185 | */ |
| 186 | protected Map<Link, LinkInfo> links; |
| 187 | |
| 188 | /** |
| 189 | * Map from switch id to a set of all links with it as an endpoint |
| 190 | */ |
| 191 | protected Map<Long, Set<Link>> switchLinks; |
| 192 | |
| 193 | /** |
| 194 | * Map from a id:port to the set of links containing it as an endpoint |
| 195 | */ |
| 196 | protected Map<NodePortTuple, Set<Link>> portLinks; |
| 197 | |
| 198 | /** |
| 199 | * Set of link tuples over which multicast LLDPs are received |
| 200 | * and unicast LLDPs are not received. |
| 201 | */ |
| 202 | protected Map<NodePortTuple, Set<Link>> portBroadcastDomainLinks; |
| 203 | |
| 204 | protected volatile boolean shuttingDown = false; |
| 205 | |
| 206 | /* topology aware components are called in the order they were added to the |
| 207 | * the array */ |
| 208 | protected ArrayList<ILinkDiscoveryListener> linkDiscoveryAware; |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 209 | |
| 210 | protected class LinkUpdate extends LDUpdate { |
| 211 | |
| 212 | public LinkUpdate(LDUpdate old) { |
| 213 | super(old); |
| 214 | } |
| 215 | @LogMessageDoc(level="ERROR", |
| 216 | message="Error in link discovery updates loop", |
| 217 | explanation="An unknown error occured while dispatching " + |
| 218 | "link update notifications", |
| 219 | recommendation=LogMessageDoc.GENERIC_ACTION) |
| 220 | @Override |
| 221 | public void dispatch() { |
Jonathan Hart | b0904bf | 2013-11-26 14:41:11 -0800 | [diff] [blame] | 222 | if (linkDiscoveryAware != null) { |
| 223 | if (log.isTraceEnabled()) { |
| 224 | log.trace("Dispatching link discovery update {} {} {} {} {} for {}", |
| 225 | new Object[]{this.getOperation(), |
| 226 | HexString.toHexString(this.getSrc()), this.getSrcPort(), |
| 227 | HexString.toHexString(this.getDst()), this.getDstPort(), |
| 228 | linkDiscoveryAware}); |
| 229 | } |
| 230 | try { |
| 231 | for (ILinkDiscoveryListener lda : linkDiscoveryAware) { // order maintained |
| 232 | lda.linkDiscoveryUpdate(this); |
| 233 | } |
| 234 | } |
| 235 | catch (Exception e) { |
| 236 | log.error("Error in link discovery updates loop", e); |
| 237 | } |
| 238 | } |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 239 | } |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 240 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * List of ports through which LLDP/BDDPs are not sent. |
| 244 | */ |
| 245 | protected Set<NodePortTuple> suppressLinkDiscovery; |
| 246 | |
| 247 | /** A list of ports that are quarantined for discovering links through |
| 248 | * them. Data traffic from these ports are not allowed until the ports |
| 249 | * are released from quarantine. |
| 250 | */ |
| 251 | protected LinkedBlockingQueue<NodePortTuple> quarantineQueue; |
| 252 | protected LinkedBlockingQueue<NodePortTuple> maintenanceQueue; |
| 253 | /** |
| 254 | * Quarantine task |
| 255 | */ |
| 256 | protected SingletonTask bddpTask; |
| 257 | protected final int BDDP_TASK_INTERVAL = 100; // 100 ms. |
| 258 | protected final int BDDP_TASK_SIZE = 5; // # of ports per iteration |
| 259 | |
| 260 | /** |
| 261 | * Map of broadcast domain ports and the last time a BDDP was either |
| 262 | * sent or received on that port. |
| 263 | */ |
| 264 | protected Map<NodePortTuple, Long> broadcastDomainPortTimeMap; |
| 265 | |
| 266 | /** |
| 267 | * Get the LLDP sending period in seconds. |
| 268 | * @return LLDP sending period in seconds. |
| 269 | */ |
| 270 | public int getLldpFrequency() { |
| 271 | return LLDP_TO_KNOWN_INTERVAL; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Get the LLDP timeout value in seconds |
| 276 | * @return LLDP timeout value in seconds |
| 277 | */ |
| 278 | public int getLldpTimeout() { |
| 279 | return LINK_TIMEOUT; |
| 280 | } |
| 281 | |
| 282 | public Map<NodePortTuple, Set<Link>> getPortLinks() { |
| 283 | return portLinks; |
| 284 | } |
| 285 | |
| 286 | public Set<NodePortTuple> getSuppressLLDPsInfo() { |
| 287 | return suppressLinkDiscovery; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Add a switch port to the suppressed LLDP list. |
| 292 | * Remove any known links on the switch port. |
| 293 | */ |
| 294 | public void AddToSuppressLLDPs(long sw, short port) |
| 295 | { |
| 296 | NodePortTuple npt = new NodePortTuple(sw, port); |
| 297 | this.suppressLinkDiscovery.add(npt); |
| 298 | deleteLinksOnPort(npt, "LLDP suppressed."); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Remove a switch port from the suppressed LLDP list. |
| 303 | * Discover links on that switchport. |
| 304 | */ |
| 305 | public void RemoveFromSuppressLLDPs(long sw, short port) |
| 306 | { |
| 307 | NodePortTuple npt = new NodePortTuple(sw, port); |
| 308 | this.suppressLinkDiscovery.remove(npt); |
| 309 | discover(npt); |
| 310 | } |
| 311 | |
| 312 | public boolean isShuttingDown() { |
| 313 | return shuttingDown; |
| 314 | } |
| 315 | |
| 316 | public boolean isFastPort(long sw, short port) { |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | public ILinkDiscovery.LinkType getLinkType(Link lt, LinkInfo info) { |
| 321 | if (info.getUnicastValidTime() != null) { |
| 322 | return ILinkDiscovery.LinkType.DIRECT_LINK; |
| 323 | } else if (info.getMulticastValidTime() != null) { |
| 324 | return ILinkDiscovery.LinkType.MULTIHOP_LINK; |
| 325 | } |
| 326 | return ILinkDiscovery.LinkType.INVALID_LINK; |
| 327 | } |
| 328 | |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 329 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 330 | private boolean isLinkDiscoverySuppressed(long sw, short portNumber) { |
| 331 | return this.suppressLinkDiscovery.contains(new NodePortTuple(sw, portNumber)); |
| 332 | } |
| 333 | |
| 334 | protected void discoverLinks() { |
| 335 | |
| 336 | // timeout known links. |
| 337 | timeoutLinks(); |
| 338 | |
| 339 | //increment LLDP clock |
| 340 | lldpClock = (lldpClock + 1)% LLDP_TO_ALL_INTERVAL; |
| 341 | |
| 342 | if (lldpClock == 0) { |
| 343 | log.debug("Sending LLDP out on all ports."); |
| 344 | discoverOnAllPorts(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | |
| 349 | /** |
| 350 | * Quarantine Ports. |
| 351 | */ |
| 352 | protected class QuarantineWorker implements Runnable { |
| 353 | @Override |
| 354 | public void run() { |
| 355 | try { |
| 356 | processBDDPLists(); |
| 357 | } |
| 358 | catch (Exception e) { |
| 359 | log.error("Error in quarantine worker thread", e); |
| 360 | } finally { |
| 361 | bddpTask.reschedule(BDDP_TASK_INTERVAL, |
| 362 | TimeUnit.MILLISECONDS); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Add a switch port to the quarantine queue. Schedule the |
| 369 | * quarantine task if the quarantine queue was empty before adding |
| 370 | * this switch port. |
| 371 | * @param npt |
| 372 | */ |
| 373 | protected void addToQuarantineQueue(NodePortTuple npt) { |
| 374 | if (quarantineQueue.contains(npt) == false) |
| 375 | quarantineQueue.add(npt); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Remove a switch port from the quarantine queue. |
| 380 | */ |
| 381 | protected void removeFromQuarantineQueue(NodePortTuple npt) { |
| 382 | // Remove all occurrences of the node port tuple from the list. |
| 383 | while (quarantineQueue.remove(npt)); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Add a switch port to maintenance queue. |
| 388 | * @param npt |
| 389 | */ |
| 390 | protected void addToMaintenanceQueue(NodePortTuple npt) { |
| 391 | // TODO We are not checking if the switch port tuple is already |
| 392 | // in the maintenance list or not. This will be an issue for |
| 393 | // really large number of switch ports in the network. |
| 394 | if (maintenanceQueue.contains(npt) == false) |
| 395 | maintenanceQueue.add(npt); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Remove a switch port from maintenance queue. |
| 400 | * @param npt |
| 401 | */ |
| 402 | protected void removeFromMaintenanceQueue(NodePortTuple npt) { |
| 403 | // Remove all occurrences of the node port tuple from the queue. |
| 404 | while (maintenanceQueue.remove(npt)); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * This method processes the quarantine list in bursts. The task is |
| 409 | * at most once per BDDP_TASK_INTERVAL. |
| 410 | * One each call, BDDP_TASK_SIZE number of switch ports are processed. |
| 411 | * Once the BDDP packets are sent out through the switch ports, the ports |
| 412 | * are removed from the quarantine list. |
| 413 | */ |
| 414 | |
| 415 | protected void processBDDPLists() { |
| 416 | int count = 0; |
| 417 | Set<NodePortTuple> nptList = new HashSet<NodePortTuple>(); |
| 418 | |
| 419 | while(count < BDDP_TASK_SIZE && quarantineQueue.peek() !=null) { |
| 420 | NodePortTuple npt; |
| 421 | npt = quarantineQueue.remove(); |
| 422 | sendDiscoveryMessage(npt.getNodeId(), npt.getPortId(), false, false); |
| 423 | nptList.add(npt); |
| 424 | count++; |
| 425 | } |
| 426 | |
| 427 | count = 0; |
| 428 | while (count < BDDP_TASK_SIZE && maintenanceQueue.peek() != null) { |
| 429 | NodePortTuple npt; |
| 430 | npt = maintenanceQueue.remove(); |
| 431 | sendDiscoveryMessage(npt.getNodeId(), npt.getPortId(), false, false); |
| 432 | count++; |
| 433 | } |
| 434 | |
| 435 | for(NodePortTuple npt:nptList) { |
| 436 | generateSwitchPortStatusUpdate(npt.getNodeId(), npt.getPortId()); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | public Set<Short> getQuarantinedPorts(long sw) { |
| 441 | Set<Short> qPorts = new HashSet<Short>(); |
| 442 | |
| 443 | Iterator<NodePortTuple> iter = quarantineQueue.iterator(); |
| 444 | while (iter.hasNext()) { |
| 445 | NodePortTuple npt = iter.next(); |
| 446 | if (npt.getNodeId() == sw) { |
| 447 | qPorts.add(npt.getPortId()); |
| 448 | } |
| 449 | } |
| 450 | return qPorts; |
| 451 | } |
| 452 | |
| 453 | private void generateSwitchPortStatusUpdate(long sw, short port) { |
| 454 | UpdateOperation operation; |
| 455 | |
| 456 | IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw); |
| 457 | if (iofSwitch == null) return; |
| 458 | |
| 459 | OFPhysicalPort ofp = iofSwitch.getPort(port); |
| 460 | if (ofp == null) return; |
| 461 | |
| 462 | int srcPortState = ofp.getState(); |
| 463 | boolean portUp = ((srcPortState & |
| 464 | OFPortState.OFPPS_STP_MASK.getValue()) != |
| 465 | OFPortState.OFPPS_STP_BLOCK.getValue()); |
| 466 | |
| 467 | if (portUp) operation = UpdateOperation.PORT_UP; |
| 468 | else operation = UpdateOperation.PORT_DOWN; |
| 469 | |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 470 | LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, operation)); |
| 471 | |
| 472 | |
| 473 | controller.publishUpdate(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Send LLDP on known ports |
| 478 | */ |
| 479 | protected void discoverOnKnownLinkPorts() { |
| 480 | // Copy the port set. |
| 481 | Set<NodePortTuple> nptSet = new HashSet<NodePortTuple>(); |
| 482 | nptSet.addAll(portLinks.keySet()); |
| 483 | |
| 484 | // Send LLDP from each of them. |
| 485 | for(NodePortTuple npt: nptSet) { |
| 486 | discover(npt); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | protected void discover(NodePortTuple npt) { |
| 491 | discover(npt.getNodeId(), npt.getPortId()); |
| 492 | } |
| 493 | |
| 494 | protected void discover(long sw, short port) { |
| 495 | sendDiscoveryMessage(sw, port, true, false); |
| 496 | } |
| 497 | |
| 498 | /** |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 499 | * Learn remote switches when running as a distributed controller ONOS |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 500 | */ |
| 501 | protected IOFSwitch addRemoteSwitch(long sw, short port) { |
HIGUCHI Yuta | 7677a6f | 2013-06-14 14:13:35 -0700 | [diff] [blame] | 502 | IOnosRemoteSwitch remotesw = null; |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 503 | |
| 504 | // add a switch if we have not seen it before |
Umesh Krishnaswamy | 4c8e108 | 2013-01-24 23:15:37 -0800 | [diff] [blame] | 505 | remotesw = remoteSwitches.get(sw); |
Jonathan Hart | e723105 | 2013-01-25 00:01:14 -0800 | [diff] [blame] | 506 | |
Pankaj Berde | c125e62 | 2013-01-25 06:39:39 -0800 | [diff] [blame] | 507 | if (remotesw == null) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 508 | remotesw = new OFSwitchImpl(); |
| 509 | remotesw.setupRemoteSwitch(sw); |
| 510 | remoteSwitches.put(remotesw.getId(), remotesw); |
| 511 | log.debug("addRemoteSwitch(): added fake remote sw {}", remotesw); |
| 512 | } |
| 513 | |
| 514 | // add the port if we have not seen it before |
Umesh Krishnaswamy | 68c118c | 2013-01-25 11:07:09 -0800 | [diff] [blame] | 515 | if (remotesw.getPort(port) == null) { |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 516 | OFPhysicalPort remoteport = new OFPhysicalPort(); |
| 517 | remoteport.setPortNumber(port); |
| 518 | remoteport.setName("fake_" + port); |
| 519 | remoteport.setConfig(0); |
| 520 | remoteport.setState(0); |
| 521 | remotesw.setPort(remoteport); |
Umesh Krishnaswamy | 82dcd98 | 2013-02-01 15:36:15 -0800 | [diff] [blame] | 522 | log.debug("addRemoteSwitch(): added fake remote port {} to sw {}", remoteport, remotesw.getId()); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return remotesw; |
| 526 | } |
| 527 | |
| 528 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 529 | * Send link discovery message out of a given switch port. |
| 530 | * The discovery message may be a standard LLDP or a modified |
| 531 | * LLDP, where the dst mac address is set to :ff. |
| 532 | * |
| 533 | * TODO: The modified LLDP will updated in the future and may |
| 534 | * use a different eth-type. |
| 535 | * @param sw |
| 536 | * @param port |
| 537 | * @param isStandard indicates standard or modified LLDP |
| 538 | * @param isReverse indicates whether the LLDP was sent as a response |
| 539 | */ |
| 540 | @LogMessageDoc(level="ERROR", |
| 541 | message="Failure sending LLDP out port {port} on switch {switch}", |
| 542 | explanation="An I/O error occured while sending LLDP message " + |
| 543 | "to the switch.", |
| 544 | recommendation=LogMessageDoc.CHECK_SWITCH) |
| 545 | protected void sendDiscoveryMessage(long sw, short port, |
| 546 | boolean isStandard, |
| 547 | boolean isReverse) { |
| 548 | |
| 549 | IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw); |
| 550 | if (iofSwitch == null) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | if (port == OFPort.OFPP_LOCAL.getValue()) |
| 555 | return; |
| 556 | |
| 557 | OFPhysicalPort ofpPort = iofSwitch.getPort(port); |
| 558 | |
| 559 | if (ofpPort == null) { |
| 560 | if (log.isTraceEnabled()) { |
| 561 | log.trace("Null physical port. sw={}, port={}", sw, port); |
| 562 | } |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | if (isLinkDiscoverySuppressed(sw, port)) { |
| 567 | /* Dont send LLDPs out of this port as suppressLLDPs set |
| 568 | * |
| 569 | */ |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | // For fast ports, do not send forward LLDPs or BDDPs. |
| 574 | if (!isReverse && autoPortFastFeature && isFastPort(sw, port)) |
| 575 | return; |
| 576 | |
| 577 | if (log.isTraceEnabled()) { |
| 578 | log.trace("Sending LLDP packet out of swich: {}, port: {}", |
| 579 | sw, port); |
| 580 | } |
| 581 | |
| 582 | // using "nearest customer bridge" MAC address for broadest possible propagation |
| 583 | // through provider and TPMR bridges (see IEEE 802.1AB-2009 and 802.1Q-2011), |
| 584 | // in particular the Linux bridge which behaves mostly like a provider bridge |
| 585 | byte[] chassisId = new byte[] {4, 0, 0, 0, 0, 0, 0}; // filled in later |
| 586 | byte[] portId = new byte[] {2, 0, 0}; // filled in later |
| 587 | byte[] ttlValue = new byte[] {0, 0x78}; |
| 588 | // OpenFlow OUI - 00-26-E1 |
| 589 | byte[] dpidTLVValue = new byte[] {0x0, 0x26, (byte) 0xe1, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 590 | LLDPTLV dpidTLV = new LLDPTLV().setType((byte) 127).setLength((short) dpidTLVValue.length).setValue(dpidTLVValue); |
| 591 | |
| 592 | byte[] dpidArray = new byte[8]; |
| 593 | ByteBuffer dpidBB = ByteBuffer.wrap(dpidArray); |
| 594 | ByteBuffer portBB = ByteBuffer.wrap(portId, 1, 2); |
| 595 | |
| 596 | Long dpid = sw; |
| 597 | dpidBB.putLong(dpid); |
| 598 | // set the ethernet source mac to last 6 bytes of dpid |
| 599 | System.arraycopy(dpidArray, 2, ofpPort.getHardwareAddress(), 0, 6); |
| 600 | // set the chassis id's value to last 6 bytes of dpid |
| 601 | System.arraycopy(dpidArray, 2, chassisId, 1, 6); |
| 602 | // set the optional tlv to the full dpid |
| 603 | System.arraycopy(dpidArray, 0, dpidTLVValue, 4, 8); |
| 604 | |
| 605 | |
| 606 | // set the portId to the outgoing port |
| 607 | portBB.putShort(port); |
| 608 | if (log.isTraceEnabled()) { |
| 609 | log.trace("Sending LLDP out of interface: {}/{}", |
| 610 | HexString.toHexString(sw), port); |
| 611 | } |
| 612 | |
| 613 | LLDP lldp = new LLDP(); |
| 614 | lldp.setChassisId(new LLDPTLV().setType((byte) 1).setLength((short) chassisId.length).setValue(chassisId)); |
| 615 | lldp.setPortId(new LLDPTLV().setType((byte) 2).setLength((short) portId.length).setValue(portId)); |
| 616 | lldp.setTtl(new LLDPTLV().setType((byte) 3).setLength((short) ttlValue.length).setValue(ttlValue)); |
| 617 | lldp.getOptionalTLVList().add(dpidTLV); |
| 618 | |
| 619 | // Add the controller identifier to the TLV value. |
| 620 | lldp.getOptionalTLVList().add(controllerTLV); |
| 621 | if (isReverse) { |
| 622 | lldp.getOptionalTLVList().add(reverseTLV); |
| 623 | }else { |
| 624 | lldp.getOptionalTLVList().add(forwardTLV); |
| 625 | } |
| 626 | |
| 627 | Ethernet ethernet; |
| 628 | if (isStandard) { |
| 629 | ethernet = new Ethernet() |
| 630 | .setSourceMACAddress(ofpPort.getHardwareAddress()) |
| 631 | .setDestinationMACAddress(LLDP_STANDARD_DST_MAC_STRING) |
| 632 | .setEtherType(Ethernet.TYPE_LLDP); |
| 633 | ethernet.setPayload(lldp); |
| 634 | } else { |
| 635 | BSN bsn = new BSN(BSN.BSN_TYPE_BDDP); |
| 636 | bsn.setPayload(lldp); |
| 637 | |
| 638 | ethernet = new Ethernet() |
| 639 | .setSourceMACAddress(ofpPort.getHardwareAddress()) |
| 640 | .setDestinationMACAddress(LLDP_BSN_DST_MAC_STRING) |
| 641 | .setEtherType(Ethernet.TYPE_BSN); |
| 642 | ethernet.setPayload(bsn); |
| 643 | } |
| 644 | |
| 645 | |
| 646 | // serialize and wrap in a packet out |
| 647 | byte[] data = ethernet.serialize(); |
| 648 | OFPacketOut po = (OFPacketOut) floodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT); |
| 649 | po.setBufferId(OFPacketOut.BUFFER_ID_NONE); |
| 650 | po.setInPort(OFPort.OFPP_NONE); |
| 651 | |
| 652 | // set actions |
| 653 | List<OFAction> actions = new ArrayList<OFAction>(); |
| 654 | actions.add(new OFActionOutput(port, (short) 0)); |
| 655 | po.setActions(actions); |
| 656 | po.setActionsLength((short) OFActionOutput.MINIMUM_LENGTH); |
| 657 | |
| 658 | // set data |
| 659 | po.setLengthU(OFPacketOut.MINIMUM_LENGTH + po.getActionsLength() + data.length); |
| 660 | po.setPacketData(data); |
| 661 | |
| 662 | // send |
| 663 | try { |
| 664 | iofSwitch.write(po, null); |
| 665 | iofSwitch.flush(); |
| 666 | } catch (IOException e) { |
| 667 | log.error("Failure sending LLDP out port {} on switch {}", |
| 668 | new Object[]{ port, iofSwitch.getStringId() }, e); |
| 669 | } |
| 670 | |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Send LLDPs to all switch-ports |
| 675 | */ |
| 676 | protected void discoverOnAllPorts() { |
| 677 | if (log.isTraceEnabled()) { |
| 678 | log.trace("Sending LLDP packets out of all the enabled ports on switch {}"); |
| 679 | } |
| 680 | Set<Long> switches = floodlightProvider.getSwitches().keySet(); |
| 681 | // Send standard LLDPs |
| 682 | for (long sw: switches) { |
| 683 | IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw); |
| 684 | if (iofSwitch == null) continue; |
| 685 | if (iofSwitch.getEnabledPorts() != null) { |
| 686 | for (OFPhysicalPort ofp: iofSwitch.getEnabledPorts()) { |
| 687 | if (isLinkDiscoverySuppressed(sw, ofp.getPortNumber())) |
| 688 | continue; |
| 689 | if (autoPortFastFeature && isFastPort(sw, ofp.getPortNumber())) |
| 690 | continue; |
| 691 | |
| 692 | // sends forward LLDP only non-fastports. |
| 693 | sendDiscoveryMessage(sw, ofp.getPortNumber(), true, false); |
| 694 | |
| 695 | // If the switch port is not alreayd in the maintenance |
| 696 | // queue, add it. |
| 697 | NodePortTuple npt = new NodePortTuple(sw, ofp.getPortNumber()); |
| 698 | addToMaintenanceQueue(npt); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | protected void setControllerTLV() { |
| 705 | //Setting the controllerTLVValue based on current nano time, |
| 706 | //controller's IP address, and the network interface object hash |
| 707 | //the corresponding IP address. |
| 708 | |
| 709 | final int prime = 7867; |
| 710 | InetAddress localIPAddress = null; |
| 711 | NetworkInterface localInterface = null; |
| 712 | |
| 713 | byte[] controllerTLVValue = new byte[] {0, 0, 0, 0, 0, 0, 0, 0}; // 8 byte value. |
| 714 | ByteBuffer bb = ByteBuffer.allocate(10); |
| 715 | |
| 716 | try{ |
| 717 | localIPAddress = java.net.InetAddress.getLocalHost(); |
| 718 | localInterface = NetworkInterface.getByInetAddress(localIPAddress); |
| 719 | } catch (Exception e) { |
| 720 | e.printStackTrace(); |
| 721 | } |
| 722 | |
| 723 | long result = System.nanoTime(); |
| 724 | if (localIPAddress != null) |
| 725 | result = result * prime + IPv4.toIPv4Address(localIPAddress.getHostAddress()); |
| 726 | if (localInterface != null) |
| 727 | result = result * prime + localInterface.hashCode(); |
| 728 | // set the first 4 bits to 0. |
| 729 | result = result & (0x0fffffffffffffffL); |
| 730 | |
| 731 | bb.putLong(result); |
| 732 | |
| 733 | bb.rewind(); |
| 734 | bb.get(controllerTLVValue, 0, 8); |
| 735 | |
| 736 | this.controllerTLV = new LLDPTLV().setType((byte) 0x0c).setLength((short) controllerTLVValue.length).setValue(controllerTLVValue); |
| 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public String getName() { |
| 741 | return "linkdiscovery"; |
| 742 | } |
| 743 | |
| 744 | @Override |
| 745 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 746 | switch (msg.getType()) { |
| 747 | case PACKET_IN: |
| 748 | return this.handlePacketIn(sw.getId(), (OFPacketIn) msg, cntx); |
| 749 | case PORT_STATUS: |
| 750 | return this.handlePortStatus(sw.getId(), (OFPortStatus) msg); |
| 751 | default: |
| 752 | break; |
| 753 | } |
| 754 | return Command.CONTINUE; |
| 755 | } |
| 756 | |
| 757 | private Command handleLldp(LLDP lldp, long sw, OFPacketIn pi, boolean isStandard, FloodlightContext cntx) { |
| 758 | // If LLDP is suppressed on this port, ignore received packet as well |
| 759 | IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw); |
| 760 | if (iofSwitch == null) { |
| 761 | return Command.STOP; |
| 762 | } |
| 763 | |
| 764 | if (isLinkDiscoverySuppressed(sw, pi.getInPort())) |
| 765 | return Command.STOP; |
| 766 | |
| 767 | // If this is a malformed LLDP, or not from us, exit |
| 768 | if (lldp.getPortId() == null || lldp.getPortId().getLength() != 3) |
| 769 | return Command.CONTINUE; |
| 770 | |
| 771 | long myId = ByteBuffer.wrap(controllerTLV.getValue()).getLong(); |
| 772 | long otherId = 0; |
| 773 | boolean myLLDP = false; |
| 774 | Boolean isReverse = null; |
| 775 | |
| 776 | ByteBuffer portBB = ByteBuffer.wrap(lldp.getPortId().getValue()); |
| 777 | portBB.position(1); |
| 778 | |
| 779 | Short remotePort = portBB.getShort(); |
| 780 | IOFSwitch remoteSwitch = null; |
| 781 | |
| 782 | // Verify this LLDP packet matches what we're looking for |
| 783 | for (LLDPTLV lldptlv : lldp.getOptionalTLVList()) { |
| 784 | if (lldptlv.getType() == 127 && lldptlv.getLength() == 12 && |
| 785 | lldptlv.getValue()[0] == 0x0 && lldptlv.getValue()[1] == 0x26 && |
| 786 | lldptlv.getValue()[2] == (byte)0xe1 && lldptlv.getValue()[3] == 0x0) { |
| 787 | ByteBuffer dpidBB = ByteBuffer.wrap(lldptlv.getValue()); |
| 788 | remoteSwitch = floodlightProvider.getSwitches().get(dpidBB.getLong(4)); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 789 | if (remoteSwitch == null) { |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 790 | // Added by ONOS |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 791 | // floodlight LLDP coming from a remote switch connected to a different controller |
| 792 | // add it to our cache of unconnected remote switches |
| 793 | remoteSwitch = addRemoteSwitch(dpidBB.getLong(4), remotePort); |
| 794 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 795 | } else if (lldptlv.getType() == 12 && lldptlv.getLength() == 8){ |
| 796 | otherId = ByteBuffer.wrap(lldptlv.getValue()).getLong(); |
| 797 | if (myId == otherId) |
| 798 | myLLDP = true; |
| 799 | } else if (lldptlv.getType() == TLV_DIRECTION_TYPE && |
| 800 | lldptlv.getLength() == TLV_DIRECTION_LENGTH) { |
| 801 | if (lldptlv.getValue()[0] == TLV_DIRECTION_VALUE_FORWARD[0]) |
| 802 | isReverse = false; |
| 803 | else if (lldptlv.getValue()[0] == TLV_DIRECTION_VALUE_REVERSE[0]) |
| 804 | isReverse = true; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | if (myLLDP == false) { |
| 809 | // This is not the LLDP sent by this controller. |
| 810 | // If the LLDP message has multicast bit set, then we need to broadcast |
| 811 | // the packet as a regular packet. |
| 812 | if (isStandard) { |
| 813 | if (log.isTraceEnabled()) { |
| 814 | log.trace("Getting standard LLDP from a different controller and quelching it."); |
| 815 | } |
| 816 | return Command.STOP; |
| 817 | } |
| 818 | else if (myId < otherId) { |
| 819 | if (log.isTraceEnabled()) { |
| 820 | log.trace("Getting BDDP packets from a different controller" + |
| 821 | "and letting it go through normal processing chain."); |
| 822 | } |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 823 | //XXX ONOS: Fix the BDDP broadcast issue |
Jonathan Hart | 0b2c76a | 2013-02-27 17:09:33 -0800 | [diff] [blame] | 824 | //return Command.CONTINUE; |
| 825 | return Command.STOP; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | |
| 830 | if (remoteSwitch == null) { |
| 831 | // Ignore LLDPs not generated by Floodlight, or from a switch that has recently |
| 832 | // disconnected, or from a switch connected to another Floodlight instance |
| 833 | if (log.isTraceEnabled()) { |
| 834 | log.trace("Received LLDP from remote switch not connected to the controller"); |
| 835 | } |
| 836 | return Command.STOP; |
| 837 | } |
| 838 | |
| 839 | if (!remoteSwitch.portEnabled(remotePort)) { |
| 840 | if (log.isTraceEnabled()) { |
| 841 | log.trace("Ignoring link with disabled source port: switch {} port {}", remoteSwitch, remotePort); |
| 842 | } |
| 843 | return Command.STOP; |
| 844 | } |
| 845 | if (suppressLinkDiscovery.contains(new NodePortTuple(remoteSwitch.getId(), |
| 846 | remotePort))) { |
| 847 | if (log.isTraceEnabled()) { |
| 848 | log.trace("Ignoring link with suppressed src port: switch {} port {}", |
| 849 | remoteSwitch, remotePort); |
| 850 | } |
| 851 | return Command.STOP; |
| 852 | } |
| 853 | if (!iofSwitch.portEnabled(pi.getInPort())) { |
| 854 | if (log.isTraceEnabled()) { |
| 855 | log.trace("Ignoring link with disabled dest port: switch {} port {}", sw, pi.getInPort()); |
| 856 | } |
| 857 | return Command.STOP; |
| 858 | } |
| 859 | |
| 860 | OFPhysicalPort physicalPort = remoteSwitch.getPort(remotePort); |
| 861 | int srcPortState = (physicalPort != null) ? physicalPort.getState() : 0; |
| 862 | physicalPort = iofSwitch.getPort(pi.getInPort()); |
| 863 | int dstPortState = (physicalPort != null) ? physicalPort.getState() : 0; |
| 864 | |
| 865 | // Store the time of update to this link, and push it out to routingEngine |
| 866 | Link lt = new Link(remoteSwitch.getId(), remotePort, iofSwitch.getId(), pi.getInPort()); |
| 867 | |
| 868 | |
| 869 | Long lastLldpTime = null; |
| 870 | Long lastBddpTime = null; |
| 871 | |
| 872 | Long firstSeenTime = System.currentTimeMillis(); |
| 873 | |
| 874 | if (isStandard) |
| 875 | lastLldpTime = System.currentTimeMillis(); |
| 876 | else |
| 877 | lastBddpTime = System.currentTimeMillis(); |
| 878 | |
| 879 | LinkInfo newLinkInfo = |
| 880 | new LinkInfo(firstSeenTime, lastLldpTime, lastBddpTime, |
| 881 | srcPortState, dstPortState); |
| 882 | |
| 883 | addOrUpdateLink(lt, newLinkInfo); |
| 884 | |
| 885 | // Check if reverse link exists. |
| 886 | // If it doesn't exist and if the forward link was seen |
| 887 | // first seen within a small interval, send probe on the |
| 888 | // reverse link. |
| 889 | |
| 890 | newLinkInfo = links.get(lt); |
| 891 | if (newLinkInfo != null && isStandard && isReverse == false) { |
| 892 | Link reverseLink = new Link(lt.getDst(), lt.getDstPort(), |
| 893 | lt.getSrc(), lt.getSrcPort()); |
| 894 | LinkInfo reverseInfo = links.get(reverseLink); |
| 895 | if (reverseInfo == null) { |
| 896 | // the reverse link does not exist. |
| 897 | if (newLinkInfo.getFirstSeenTime() > System.currentTimeMillis() - LINK_TIMEOUT) { |
| 898 | this.sendDiscoveryMessage(lt.getDst(), lt.getDstPort(), isStandard, true); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | // If the received packet is a BDDP packet, then create a reverse BDDP |
| 904 | // link as well. |
| 905 | if (!isStandard) { |
| 906 | Link reverseLink = new Link(lt.getDst(), lt.getDstPort(), |
| 907 | lt.getSrc(), lt.getSrcPort()); |
| 908 | |
| 909 | // srcPortState and dstPort state are reversed. |
| 910 | LinkInfo reverseInfo = |
| 911 | new LinkInfo(firstSeenTime, lastLldpTime, lastBddpTime, |
| 912 | dstPortState, srcPortState); |
| 913 | |
| 914 | addOrUpdateLink(reverseLink, reverseInfo); |
| 915 | } |
| 916 | |
| 917 | // Remove the node ports from the quarantine and maintenance queues. |
| 918 | NodePortTuple nptSrc = new NodePortTuple(lt.getSrc(), lt.getSrcPort()); |
| 919 | NodePortTuple nptDst = new NodePortTuple(lt.getDst(), lt.getDstPort()); |
| 920 | removeFromQuarantineQueue(nptSrc); |
| 921 | removeFromMaintenanceQueue(nptSrc); |
| 922 | removeFromQuarantineQueue(nptDst); |
| 923 | removeFromMaintenanceQueue(nptDst); |
| 924 | |
| 925 | // Consume this message |
| 926 | return Command.STOP; |
| 927 | } |
| 928 | |
| 929 | protected Command handlePacketIn(long sw, OFPacketIn pi, |
| 930 | FloodlightContext cntx) { |
| 931 | Ethernet eth = |
| 932 | IFloodlightProviderService.bcStore.get(cntx, |
| 933 | IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
| 934 | |
| 935 | if(eth.getEtherType() == Ethernet.TYPE_BSN) { |
| 936 | BSN bsn = (BSN) eth.getPayload(); |
| 937 | if (bsn == null) return Command.STOP; |
| 938 | if (bsn.getPayload() == null) return Command.STOP; |
| 939 | // It could be a packet other than BSN LLDP, therefore |
| 940 | // continue with the regular processing. |
| 941 | if (bsn.getPayload() instanceof LLDP == false) |
| 942 | return Command.CONTINUE; |
Ubuntu | 9cbb4ca | 2013-02-07 17:19:59 +0000 | [diff] [blame] | 943 | return handleLldp((LLDP) bsn.getPayload(), sw, pi, false, cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 944 | } else if (eth.getEtherType() == Ethernet.TYPE_LLDP) { |
| 945 | return handleLldp((LLDP) eth.getPayload(), sw, pi, true, cntx); |
| 946 | } else if (eth.getEtherType() < 1500) { |
| 947 | long destMac = eth.getDestinationMAC().toLong(); |
| 948 | if ((destMac & LINK_LOCAL_MASK) == LINK_LOCAL_VALUE){ |
| 949 | if (log.isTraceEnabled()) { |
| 950 | log.trace("Ignoring packet addressed to 802.1D/Q " + |
| 951 | "reserved address."); |
| 952 | } |
| 953 | return Command.STOP; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | // If packet-in is from a quarantine port, stop processing. |
| 958 | NodePortTuple npt = new NodePortTuple(sw, pi.getInPort()); |
| 959 | if (quarantineQueue.contains(npt)) return Command.STOP; |
| 960 | |
| 961 | return Command.CONTINUE; |
| 962 | } |
| 963 | |
| 964 | protected UpdateOperation getUpdateOperation(int srcPortState, |
| 965 | int dstPortState) { |
| 966 | boolean added = |
| 967 | (((srcPortState & |
| 968 | OFPortState.OFPPS_STP_MASK.getValue()) != |
| 969 | OFPortState.OFPPS_STP_BLOCK.getValue()) && |
| 970 | ((dstPortState & |
| 971 | OFPortState.OFPPS_STP_MASK.getValue()) != |
| 972 | OFPortState.OFPPS_STP_BLOCK.getValue())); |
| 973 | |
| 974 | if (added) return UpdateOperation.LINK_UPDATED; |
| 975 | return UpdateOperation.LINK_REMOVED; |
| 976 | } |
| 977 | |
| 978 | |
| 979 | |
| 980 | protected UpdateOperation getUpdateOperation(int srcPortState) { |
| 981 | boolean portUp = ((srcPortState & |
| 982 | OFPortState.OFPPS_STP_MASK.getValue()) != |
| 983 | OFPortState.OFPPS_STP_BLOCK.getValue()); |
| 984 | |
| 985 | if (portUp) return UpdateOperation.PORT_UP; |
| 986 | else return UpdateOperation.PORT_DOWN; |
| 987 | } |
| 988 | |
| 989 | protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) { |
| 990 | |
| 991 | NodePortTuple srcNpt, dstNpt; |
| 992 | boolean linkChanged = false; |
| 993 | |
| 994 | lock.writeLock().lock(); |
| 995 | try { |
| 996 | // put the new info. if an old info exists, it will be returned. |
| 997 | LinkInfo oldInfo = links.put(lt, newInfo); |
| 998 | if (oldInfo != null && |
| 999 | oldInfo.getFirstSeenTime() < newInfo.getFirstSeenTime()) |
| 1000 | newInfo.setFirstSeenTime(oldInfo.getFirstSeenTime()); |
| 1001 | |
| 1002 | if (log.isTraceEnabled()) { |
| 1003 | log.trace("addOrUpdateLink: {} {}", |
| 1004 | lt, |
| 1005 | (newInfo.getMulticastValidTime()!=null) ? "multicast" : "unicast"); |
| 1006 | } |
| 1007 | |
| 1008 | UpdateOperation updateOperation = null; |
| 1009 | linkChanged = false; |
| 1010 | |
| 1011 | srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort()); |
| 1012 | dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort()); |
| 1013 | |
| 1014 | if (oldInfo == null) { |
| 1015 | // index it by switch source |
| 1016 | if (!switchLinks.containsKey(lt.getSrc())) |
| 1017 | switchLinks.put(lt.getSrc(), new HashSet<Link>()); |
| 1018 | switchLinks.get(lt.getSrc()).add(lt); |
| 1019 | |
| 1020 | // index it by switch dest |
| 1021 | if (!switchLinks.containsKey(lt.getDst())) |
| 1022 | switchLinks.put(lt.getDst(), new HashSet<Link>()); |
| 1023 | switchLinks.get(lt.getDst()).add(lt); |
| 1024 | |
| 1025 | // index both ends by switch:port |
| 1026 | if (!portLinks.containsKey(srcNpt)) |
| 1027 | portLinks.put(srcNpt, new HashSet<Link>()); |
| 1028 | portLinks.get(srcNpt).add(lt); |
| 1029 | |
| 1030 | if (!portLinks.containsKey(dstNpt)) |
| 1031 | portLinks.put(dstNpt, new HashSet<Link>()); |
| 1032 | portLinks.get(dstNpt).add(lt); |
| 1033 | |
| 1034 | // Add to portNOFLinks if the unicast valid time is null |
| 1035 | if (newInfo.getUnicastValidTime() == null) |
| 1036 | addLinkToBroadcastDomain(lt); |
Umesh Krishnaswamy | 2b9d564 | 2013-01-04 11:00:27 -0800 | [diff] [blame] | 1037 | |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 1038 | // ONOS: Distinguish added event separately from updated event |
Pankaj Berde | a41016d | 2013-06-10 21:18:18 -0700 | [diff] [blame] | 1039 | updateOperation = UpdateOperation.LINK_ADDED; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1040 | linkChanged = true; |
| 1041 | |
| 1042 | // Add to event history |
| 1043 | evHistTopoLink(lt.getSrc(), |
| 1044 | lt.getDst(), |
| 1045 | lt.getSrcPort(), |
| 1046 | lt.getDstPort(), |
| 1047 | newInfo.getSrcPortState(), newInfo.getDstPortState(), |
| 1048 | getLinkType(lt, newInfo), |
| 1049 | EvAction.LINK_ADDED, "LLDP Recvd"); |
| 1050 | } else { |
| 1051 | // Since the link info is already there, we need to |
| 1052 | // update the right fields. |
| 1053 | if (newInfo.getUnicastValidTime() == null) { |
| 1054 | // This is due to a multicast LLDP, so copy the old unicast |
| 1055 | // value. |
| 1056 | if (oldInfo.getUnicastValidTime() != null) { |
| 1057 | newInfo.setUnicastValidTime(oldInfo.getUnicastValidTime()); |
| 1058 | } |
| 1059 | } else if (newInfo.getMulticastValidTime() == null) { |
| 1060 | // This is due to a unicast LLDP, so copy the old multicast |
| 1061 | // value. |
| 1062 | if (oldInfo.getMulticastValidTime() != null) { |
| 1063 | newInfo.setMulticastValidTime(oldInfo.getMulticastValidTime()); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | Long oldTime = oldInfo.getUnicastValidTime(); |
| 1068 | Long newTime = newInfo.getUnicastValidTime(); |
| 1069 | // the link has changed its state between openflow and non-openflow |
| 1070 | // if the unicastValidTimes are null or not null |
| 1071 | if (oldTime != null & newTime == null) { |
| 1072 | // openflow -> non-openflow transition |
| 1073 | // we need to add the link tuple to the portNOFLinks |
| 1074 | addLinkToBroadcastDomain(lt); |
| 1075 | linkChanged = true; |
| 1076 | } else if (oldTime == null & newTime != null) { |
| 1077 | // non-openflow -> openflow transition |
| 1078 | // we need to remove the link from the portNOFLinks |
| 1079 | removeLinkFromBroadcastDomain(lt); |
| 1080 | linkChanged = true; |
| 1081 | } |
| 1082 | |
| 1083 | // Only update the port states if they've changed |
| 1084 | if (newInfo.getSrcPortState().intValue() != |
| 1085 | oldInfo.getSrcPortState().intValue() || |
| 1086 | newInfo.getDstPortState().intValue() != |
| 1087 | oldInfo.getDstPortState().intValue()) |
| 1088 | linkChanged = true; |
| 1089 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1090 | if (linkChanged) { |
| 1091 | updateOperation = getUpdateOperation(newInfo.getSrcPortState(), |
| 1092 | newInfo.getDstPortState()); |
| 1093 | if (log.isTraceEnabled()) { |
| 1094 | log.trace("Updated link {}", lt); |
| 1095 | } |
| 1096 | // Add to event history |
| 1097 | evHistTopoLink(lt.getSrc(), |
| 1098 | lt.getDst(), |
| 1099 | lt.getSrcPort(), |
| 1100 | lt.getDstPort(), |
| 1101 | newInfo.getSrcPortState(), newInfo.getDstPortState(), |
| 1102 | getLinkType(lt, newInfo), |
| 1103 | EvAction.LINK_PORT_STATE_UPDATED, |
| 1104 | "LLDP Recvd"); |
| 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | if (linkChanged) { |
| 1109 | // find out if the link was added or removed here. |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1110 | LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(), |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1111 | lt.getDst(), lt.getDstPort(), |
| 1112 | getLinkType(lt, newInfo), |
| 1113 | updateOperation)); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1114 | controller.publishUpdate(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1115 | } |
| 1116 | } finally { |
| 1117 | lock.writeLock().unlock(); |
| 1118 | } |
| 1119 | |
| 1120 | return linkChanged; |
| 1121 | } |
| 1122 | |
| 1123 | public Map<Long, Set<Link>> getSwitchLinks() { |
| 1124 | return this.switchLinks; |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * Removes links from memory and storage. |
| 1129 | * @param links The List of @LinkTuple to delete. |
| 1130 | */ |
| 1131 | protected void deleteLinks(List<Link> links, String reason) { |
| 1132 | NodePortTuple srcNpt, dstNpt; |
| 1133 | |
| 1134 | lock.writeLock().lock(); |
| 1135 | try { |
| 1136 | for (Link lt : links) { |
| 1137 | srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort()); |
| 1138 | dstNpt =new NodePortTuple(lt.getDst(), lt.getDstPort()); |
| 1139 | |
| 1140 | switchLinks.get(lt.getSrc()).remove(lt); |
| 1141 | switchLinks.get(lt.getDst()).remove(lt); |
| 1142 | if (switchLinks.containsKey(lt.getSrc()) && |
| 1143 | switchLinks.get(lt.getSrc()).isEmpty()) |
| 1144 | this.switchLinks.remove(lt.getSrc()); |
| 1145 | if (this.switchLinks.containsKey(lt.getDst()) && |
| 1146 | this.switchLinks.get(lt.getDst()).isEmpty()) |
| 1147 | this.switchLinks.remove(lt.getDst()); |
| 1148 | |
| 1149 | if (this.portLinks.get(srcNpt) != null) { |
| 1150 | this.portLinks.get(srcNpt).remove(lt); |
| 1151 | if (this.portLinks.get(srcNpt).isEmpty()) |
| 1152 | this.portLinks.remove(srcNpt); |
| 1153 | } |
| 1154 | if (this.portLinks.get(dstNpt) != null) { |
| 1155 | this.portLinks.get(dstNpt).remove(lt); |
| 1156 | if (this.portLinks.get(dstNpt).isEmpty()) |
| 1157 | this.portLinks.remove(dstNpt); |
| 1158 | } |
| 1159 | |
| 1160 | LinkInfo info = this.links.remove(lt); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1161 | LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(), |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1162 | lt.getDst(), lt.getDstPort(), |
| 1163 | getLinkType(lt, info), |
| 1164 | UpdateOperation.LINK_REMOVED)); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1165 | controller.publishUpdate(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1166 | |
| 1167 | // Update Event History |
| 1168 | evHistTopoLink(lt.getSrc(), |
| 1169 | lt.getDst(), |
| 1170 | lt.getSrcPort(), |
| 1171 | lt.getDstPort(), |
| 1172 | 0, 0, // Port states |
| 1173 | ILinkDiscovery.LinkType.INVALID_LINK, |
| 1174 | EvAction.LINK_DELETED, reason); |
| 1175 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1176 | // TODO Whenever link is removed, it has to checked if |
| 1177 | // the switchports must be added to quarantine. |
| 1178 | |
| 1179 | if (log.isTraceEnabled()) { |
| 1180 | log.trace("Deleted link {}", lt); |
| 1181 | } |
| 1182 | } |
| 1183 | } finally { |
| 1184 | lock.writeLock().unlock(); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /** |
| 1189 | * Handles an OFPortStatus message from a switch. We will add or |
| 1190 | * delete LinkTupes as well re-compute the topology if needed. |
| 1191 | * @param sw The IOFSwitch that sent the port status message |
| 1192 | * @param ps The OFPortStatus message |
| 1193 | * @return The Command to continue or stop after we process this message |
| 1194 | */ |
| 1195 | protected Command handlePortStatus(long sw, OFPortStatus ps) { |
| 1196 | |
| 1197 | IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw); |
| 1198 | if (iofSwitch == null) return Command.CONTINUE; |
HIGUCHI Yuta | a89b284 | 2013-06-17 13:54:57 -0700 | [diff] [blame] | 1199 | |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 1200 | // ONOS: If we do not control this switch, then we should not process its port status messages |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1201 | if (!registryService.hasControl(iofSwitch.getId())) return Command.CONTINUE; |
| 1202 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1203 | if (log.isTraceEnabled()) { |
| 1204 | log.trace("handlePortStatus: Switch {} port #{} reason {}; " + |
| 1205 | "config is {} state is {}", |
| 1206 | new Object[] {iofSwitch.getStringId(), |
| 1207 | ps.getDesc().getPortNumber(), |
| 1208 | ps.getReason(), |
| 1209 | ps.getDesc().getConfig(), |
| 1210 | ps.getDesc().getState()}); |
| 1211 | } |
| 1212 | |
| 1213 | short port = ps.getDesc().getPortNumber(); |
| 1214 | NodePortTuple npt = new NodePortTuple(sw, port); |
| 1215 | boolean linkDeleted = false; |
| 1216 | boolean linkInfoChanged = false; |
| 1217 | |
| 1218 | lock.writeLock().lock(); |
| 1219 | try { |
| 1220 | // if ps is a delete, or a modify where the port is down or |
| 1221 | // configured down |
| 1222 | if ((byte)OFPortReason.OFPPR_DELETE.ordinal() == ps.getReason() || |
| 1223 | ((byte)OFPortReason.OFPPR_MODIFY.ordinal() == |
| 1224 | ps.getReason() && !portEnabled(ps.getDesc()))) { |
| 1225 | deleteLinksOnPort(npt, "Port Status Changed"); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1226 | LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, UpdateOperation.PORT_DOWN)); |
| 1227 | controller.publishUpdate(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1228 | linkDeleted = true; |
| 1229 | } |
| 1230 | else if (ps.getReason() == |
| 1231 | (byte)OFPortReason.OFPPR_MODIFY.ordinal()) { |
| 1232 | // If ps is a port modification and the port state has changed |
| 1233 | // that affects links in the topology |
| 1234 | |
| 1235 | if (this.portLinks.containsKey(npt)) { |
| 1236 | for (Link lt: this.portLinks.get(npt)) { |
| 1237 | LinkInfo linkInfo = links.get(lt); |
| 1238 | assert(linkInfo != null); |
| 1239 | Integer updatedSrcPortState = null; |
| 1240 | Integer updatedDstPortState = null; |
| 1241 | if (lt.getSrc() == npt.getNodeId() && |
| 1242 | lt.getSrcPort() == npt.getPortId() && |
| 1243 | (linkInfo.getSrcPortState() != |
| 1244 | ps.getDesc().getState())) { |
| 1245 | updatedSrcPortState = ps.getDesc().getState(); |
| 1246 | linkInfo.setSrcPortState(updatedSrcPortState); |
| 1247 | } |
| 1248 | if (lt.getDst() == npt.getNodeId() && |
| 1249 | lt.getDstPort() == npt.getPortId() && |
| 1250 | (linkInfo.getDstPortState() != |
| 1251 | ps.getDesc().getState())) { |
| 1252 | updatedDstPortState = ps.getDesc().getState(); |
| 1253 | linkInfo.setDstPortState(updatedDstPortState); |
| 1254 | } |
| 1255 | if ((updatedSrcPortState != null) || |
| 1256 | (updatedDstPortState != null)) { |
| 1257 | // The link is already known to link discovery |
| 1258 | // manager and the status has changed, therefore |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1259 | // send an LinkUpdate. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1260 | UpdateOperation operation = |
| 1261 | getUpdateOperation(linkInfo.getSrcPortState(), |
| 1262 | linkInfo.getDstPortState()); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1263 | LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(), |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1264 | lt.getDst(), lt.getDstPort(), |
| 1265 | getLinkType(lt, linkInfo), |
| 1266 | operation)); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1267 | controller.publishUpdate(update); |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 1268 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1269 | linkInfoChanged = true; |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | UpdateOperation operation = |
| 1275 | getUpdateOperation(ps.getDesc().getState()); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1276 | LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, operation)); |
| 1277 | controller.publishUpdate(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | if (!linkDeleted && !linkInfoChanged){ |
| 1281 | if (log.isTraceEnabled()) { |
| 1282 | log.trace("handlePortStatus: Switch {} port #{} reason {};"+ |
| 1283 | " no links to update/remove", |
| 1284 | new Object[] {HexString.toHexString(sw), |
| 1285 | ps.getDesc().getPortNumber(), |
| 1286 | ps.getReason()}); |
| 1287 | } |
| 1288 | } |
| 1289 | } finally { |
| 1290 | lock.writeLock().unlock(); |
| 1291 | } |
| 1292 | |
| 1293 | if (!linkDeleted) { |
| 1294 | // Send LLDP right away when port state is changed for faster |
| 1295 | // cluster-merge. If it is a link delete then there is not need |
| 1296 | // to send the LLDPs right away and instead we wait for the LLDPs |
| 1297 | // to be sent on the timer as it is normally done |
| 1298 | // do it outside the write-lock |
| 1299 | // sendLLDPTask.reschedule(1000, TimeUnit.MILLISECONDS); |
| 1300 | processNewPort(npt.getNodeId(), npt.getPortId()); |
| 1301 | } |
| 1302 | return Command.CONTINUE; |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Process a new port. |
| 1307 | * If link discovery is disabled on the port, then do nothing. |
| 1308 | * If autoportfast feature is enabled and the port is a fast port, then |
| 1309 | * do nothing. |
| 1310 | * Otherwise, send LLDP message. Add the port to quarantine. |
| 1311 | * @param sw |
| 1312 | * @param p |
| 1313 | */ |
| 1314 | private void processNewPort(long sw, short p) { |
| 1315 | if (isLinkDiscoverySuppressed(sw, p)) { |
| 1316 | // Do nothing as link discovery is suppressed. |
| 1317 | } |
| 1318 | else if (autoPortFastFeature && isFastPort(sw, p)) { |
| 1319 | // Do nothing as the port is a fast port. |
| 1320 | } |
| 1321 | else { |
| 1322 | NodePortTuple npt = new NodePortTuple(sw, p); |
| 1323 | discover(sw, p); |
| 1324 | // if it is not a fast port, add it to quarantine. |
| 1325 | if (!isFastPort(sw, p)) { |
| 1326 | addToQuarantineQueue(npt); |
| 1327 | } else { |
| 1328 | // Add to maintenance queue to ensure that BDDP packets |
| 1329 | // are sent out. |
| 1330 | addToMaintenanceQueue(npt); |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | /** |
| 1336 | * We send out LLDP messages when a switch is added to discover the topology |
| 1337 | * @param sw The IOFSwitch that connected to the controller |
| 1338 | */ |
| 1339 | @Override |
| 1340 | public void addedSwitch(IOFSwitch sw) { |
| 1341 | |
| 1342 | if (sw.getEnabledPorts() != null) { |
| 1343 | for (Short p : sw.getEnabledPortNumbers()) { |
| 1344 | processNewPort(sw.getId(), p); |
| 1345 | } |
| 1346 | } |
| 1347 | // Update event history |
| 1348 | evHistTopoSwitch(sw, EvAction.SWITCH_CONNECTED, "None"); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1349 | LinkUpdate update = new LinkUpdate(new LDUpdate(sw.getId(), null, |
| 1350 | UpdateOperation.SWITCH_UPDATED)); |
| 1351 | controller.publishUpdate(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | /** |
| 1355 | * When a switch disconnects we remove any links from our map and notify. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1356 | */ |
| 1357 | @Override |
| 1358 | public void removedSwitch(IOFSwitch iofSwitch) { |
| 1359 | // Update event history |
| 1360 | long sw = iofSwitch.getId(); |
| 1361 | evHistTopoSwitch(iofSwitch, EvAction.SWITCH_DISCONNECTED, "None"); |
| 1362 | List<Link> eraseList = new ArrayList<Link>(); |
| 1363 | lock.writeLock().lock(); |
| 1364 | try { |
| 1365 | if (switchLinks.containsKey(sw)) { |
| 1366 | if (log.isTraceEnabled()) { |
| 1367 | log.trace("Handle switchRemoved. Switch {}; removing links {}", |
| 1368 | HexString.toHexString(sw), switchLinks.get(sw)); |
| 1369 | } |
| 1370 | // add all tuples with an endpoint on this switch to erase list |
| 1371 | eraseList.addAll(switchLinks.get(sw)); |
HIGUCHI Yuta | a89b284 | 2013-06-17 13:54:57 -0700 | [diff] [blame] | 1372 | deleteLinks(eraseList, "Switch Removed"); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1373 | |
| 1374 | // Send a switch removed update |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1375 | LinkUpdate update = new LinkUpdate(new LDUpdate(sw, null, UpdateOperation.SWITCH_REMOVED)); |
| 1376 | controller.publishUpdate(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1377 | } |
| 1378 | } finally { |
| 1379 | lock.writeLock().unlock(); |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | /** |
| 1384 | * We don't react the port changed notifications here. we listen for |
| 1385 | * OFPortStatus messages directly. Might consider using this notifier |
| 1386 | * instead |
| 1387 | */ |
| 1388 | @Override |
| 1389 | public void switchPortChanged(Long switchId) { |
| 1390 | // no-op |
| 1391 | } |
| 1392 | |
| 1393 | /** |
| 1394 | * Delete links incident on a given switch port. |
| 1395 | * @param npt |
| 1396 | * @param reason |
| 1397 | */ |
| 1398 | protected void deleteLinksOnPort(NodePortTuple npt, String reason) { |
| 1399 | List<Link> eraseList = new ArrayList<Link>(); |
| 1400 | if (this.portLinks.containsKey(npt)) { |
| 1401 | if (log.isTraceEnabled()) { |
| 1402 | log.trace("handlePortStatus: Switch {} port #{} " + |
| 1403 | "removing links {}", |
| 1404 | new Object[] {HexString.toHexString(npt.getNodeId()), |
| 1405 | npt.getPortId(), |
| 1406 | this.portLinks.get(npt)}); |
| 1407 | } |
| 1408 | eraseList.addAll(this.portLinks.get(npt)); |
| 1409 | deleteLinks(eraseList, reason); |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | /** |
| 1414 | * Iterates through the list of links and deletes if the |
| 1415 | * last discovery message reception time exceeds timeout values. |
| 1416 | */ |
| 1417 | protected void timeoutLinks() { |
| 1418 | List<Link> eraseList = new ArrayList<Link>(); |
| 1419 | Long curTime = System.currentTimeMillis(); |
| 1420 | boolean linkChanged = false; |
| 1421 | |
| 1422 | // reentrant required here because deleteLink also write locks |
| 1423 | lock.writeLock().lock(); |
| 1424 | try { |
| 1425 | Iterator<Entry<Link, LinkInfo>> it = |
| 1426 | this.links.entrySet().iterator(); |
| 1427 | while (it.hasNext()) { |
| 1428 | Entry<Link, LinkInfo> entry = it.next(); |
| 1429 | Link lt = entry.getKey(); |
| 1430 | LinkInfo info = entry.getValue(); |
| 1431 | |
| 1432 | // Timeout the unicast and multicast LLDP valid times |
| 1433 | // independently. |
| 1434 | if ((info.getUnicastValidTime() != null) && |
| 1435 | (info.getUnicastValidTime() + (this.LINK_TIMEOUT * 1000) < curTime)){ |
| 1436 | info.setUnicastValidTime(null); |
| 1437 | |
| 1438 | if (info.getMulticastValidTime() != null) |
| 1439 | addLinkToBroadcastDomain(lt); |
| 1440 | // Note that even if mTime becomes null later on, |
| 1441 | // the link would be deleted, which would trigger updateClusters(). |
| 1442 | linkChanged = true; |
| 1443 | } |
| 1444 | if ((info.getMulticastValidTime()!= null) && |
| 1445 | (info.getMulticastValidTime()+ (this.LINK_TIMEOUT * 1000) < curTime)) { |
| 1446 | info.setMulticastValidTime(null); |
| 1447 | // if uTime is not null, then link will remain as openflow |
| 1448 | // link. If uTime is null, it will be deleted. So, we |
| 1449 | // don't care about linkChanged flag here. |
| 1450 | removeLinkFromBroadcastDomain(lt); |
| 1451 | linkChanged = true; |
| 1452 | } |
| 1453 | // Add to the erase list only if the unicast |
| 1454 | // time is null. |
| 1455 | if (info.getUnicastValidTime() == null && |
| 1456 | info.getMulticastValidTime() == null){ |
| 1457 | eraseList.add(entry.getKey()); |
| 1458 | } else if (linkChanged) { |
| 1459 | UpdateOperation operation; |
| 1460 | operation = getUpdateOperation(info.getSrcPortState(), |
| 1461 | info.getDstPortState()); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1462 | LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(), |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1463 | lt.getDst(), lt.getDstPort(), |
| 1464 | getLinkType(lt, info), |
| 1465 | operation)); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1466 | controller.publishUpdate(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | // if any link was deleted or any link was changed. |
| 1471 | if ((eraseList.size() > 0) || linkChanged) { |
| 1472 | deleteLinks(eraseList, "LLDP timeout"); |
| 1473 | } |
| 1474 | } finally { |
| 1475 | lock.writeLock().unlock(); |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | private boolean portEnabled(OFPhysicalPort port) { |
| 1480 | if (port == null) |
| 1481 | return false; |
| 1482 | if ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) |
| 1483 | return false; |
| 1484 | if ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0) |
| 1485 | return false; |
| 1486 | // Port STP state doesn't work with multiple VLANs, so ignore it for now |
| 1487 | // if ((port.getState() & OFPortState.OFPPS_STP_MASK.getValue()) == OFPortState.OFPPS_STP_BLOCK.getValue()) |
| 1488 | // return false; |
| 1489 | return true; |
| 1490 | } |
| 1491 | |
| 1492 | public Map<NodePortTuple, Set<Link>> getPortBroadcastDomainLinks() { |
| 1493 | return portBroadcastDomainLinks; |
| 1494 | } |
| 1495 | |
| 1496 | @Override |
| 1497 | public Map<Link, LinkInfo> getLinks() { |
| 1498 | lock.readLock().lock(); |
| 1499 | Map<Link, LinkInfo> result; |
| 1500 | try { |
| 1501 | result = new HashMap<Link, LinkInfo>(links); |
| 1502 | } finally { |
| 1503 | lock.readLock().unlock(); |
| 1504 | } |
| 1505 | return result; |
| 1506 | } |
| 1507 | |
| 1508 | protected void addLinkToBroadcastDomain(Link lt) { |
| 1509 | |
| 1510 | NodePortTuple srcNpt, dstNpt; |
| 1511 | srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort()); |
| 1512 | dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort()); |
| 1513 | |
| 1514 | if (!portBroadcastDomainLinks.containsKey(lt.getSrc())) |
| 1515 | portBroadcastDomainLinks.put(srcNpt, new HashSet<Link>()); |
| 1516 | portBroadcastDomainLinks.get(srcNpt).add(lt); |
| 1517 | |
| 1518 | if (!portBroadcastDomainLinks.containsKey(lt.getDst())) |
| 1519 | portBroadcastDomainLinks.put(dstNpt, new HashSet<Link>()); |
| 1520 | portBroadcastDomainLinks.get(dstNpt).add(lt); |
| 1521 | } |
| 1522 | |
| 1523 | protected void removeLinkFromBroadcastDomain(Link lt) { |
| 1524 | |
| 1525 | NodePortTuple srcNpt, dstNpt; |
| 1526 | srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort()); |
| 1527 | dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort()); |
| 1528 | |
| 1529 | if (portBroadcastDomainLinks.containsKey(srcNpt)) { |
| 1530 | portBroadcastDomainLinks.get(srcNpt).remove(lt); |
| 1531 | if (portBroadcastDomainLinks.get(srcNpt).isEmpty()) |
| 1532 | portBroadcastDomainLinks.remove(srcNpt); |
| 1533 | } |
| 1534 | |
| 1535 | if (portBroadcastDomainLinks.containsKey(dstNpt)) { |
| 1536 | portBroadcastDomainLinks.get(dstNpt).remove(lt); |
| 1537 | if (portBroadcastDomainLinks.get(dstNpt).isEmpty()) |
| 1538 | portBroadcastDomainLinks.remove(dstNpt); |
| 1539 | } |
| 1540 | } |
| 1541 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1542 | @Override |
| 1543 | public void addListener(ILinkDiscoveryListener listener) { |
| 1544 | linkDiscoveryAware.add(listener); |
| 1545 | } |
| 1546 | |
| 1547 | /** |
| 1548 | * Register a link discovery aware component |
| 1549 | * @param linkDiscoveryAwareComponent |
| 1550 | */ |
| 1551 | public void addLinkDiscoveryAware(ILinkDiscoveryListener linkDiscoveryAwareComponent) { |
| 1552 | // TODO make this a copy on write set or lock it somehow |
| 1553 | this.linkDiscoveryAware.add(linkDiscoveryAwareComponent); |
| 1554 | } |
| 1555 | |
| 1556 | /** |
| 1557 | * Deregister a link discovery aware component |
| 1558 | * @param linkDiscoveryAwareComponent |
| 1559 | */ |
| 1560 | public void removeLinkDiscoveryAware(ILinkDiscoveryListener linkDiscoveryAwareComponent) { |
| 1561 | // TODO make this a copy on write set or lock it somehow |
| 1562 | this.linkDiscoveryAware.remove(linkDiscoveryAwareComponent); |
| 1563 | } |
| 1564 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1565 | @Override |
| 1566 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 1567 | return false; |
| 1568 | } |
| 1569 | |
| 1570 | @Override |
| 1571 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 1572 | return false; |
| 1573 | } |
| 1574 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1575 | // IFloodlightModule classes |
| 1576 | |
| 1577 | @Override |
| 1578 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
| 1579 | Collection<Class<? extends IFloodlightService>> l = |
| 1580 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 1581 | l.add(ILinkDiscoveryService.class); |
| 1582 | //l.add(ITopologyService.class); |
| 1583 | return l; |
| 1584 | } |
| 1585 | |
| 1586 | @Override |
| 1587 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
| 1588 | getServiceImpls() { |
| 1589 | Map<Class<? extends IFloodlightService>, |
| 1590 | IFloodlightService> m = |
| 1591 | new HashMap<Class<? extends IFloodlightService>, |
| 1592 | IFloodlightService>(); |
| 1593 | // We are the class that implements the service |
| 1594 | m.put(ILinkDiscoveryService.class, this); |
| 1595 | return m; |
| 1596 | } |
| 1597 | |
| 1598 | @Override |
| 1599 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
| 1600 | Collection<Class<? extends IFloodlightService>> l = |
| 1601 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 1602 | l.add(IFloodlightProviderService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1603 | l.add(IThreadPoolService.class); |
| 1604 | l.add(IRestApiService.class); |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 1605 | // Added by ONOS |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1606 | l.add(IControllerRegistryService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1607 | return l; |
| 1608 | } |
| 1609 | |
| 1610 | @Override |
| 1611 | public void init(FloodlightModuleContext context) |
| 1612 | throws FloodlightModuleException { |
| 1613 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1614 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
| 1615 | restApi = context.getServiceImpl(IRestApiService.class); |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 1616 | // Added by ONOS |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 1617 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1618 | |
| 1619 | // Set the autoportfast feature to false. |
| 1620 | this.autoPortFastFeature = false; |
| 1621 | |
| 1622 | // We create this here because there is no ordering guarantee |
| 1623 | this.linkDiscoveryAware = new ArrayList<ILinkDiscoveryListener>(); |
| 1624 | this.lock = new ReentrantReadWriteLock(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1625 | this.links = new HashMap<Link, LinkInfo>(); |
| 1626 | this.portLinks = new HashMap<NodePortTuple, Set<Link>>(); |
| 1627 | this.suppressLinkDiscovery = |
| 1628 | Collections.synchronizedSet(new HashSet<NodePortTuple>()); |
| 1629 | this.portBroadcastDomainLinks = new HashMap<NodePortTuple, Set<Link>>(); |
| 1630 | this.switchLinks = new HashMap<Long, Set<Link>>(); |
| 1631 | this.quarantineQueue = new LinkedBlockingQueue<NodePortTuple>(); |
| 1632 | this.maintenanceQueue = new LinkedBlockingQueue<NodePortTuple>(); |
HIGUCHI Yuta | 30d0330 | 2013-06-14 13:47:36 -0700 | [diff] [blame] | 1633 | // Added by ONOS |
HIGUCHI Yuta | 7677a6f | 2013-06-14 14:13:35 -0700 | [diff] [blame] | 1634 | this.remoteSwitches = new HashMap<Long, IOnosRemoteSwitch>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1635 | |
| 1636 | this.evHistTopologySwitch = |
| 1637 | new EventHistory<EventHistoryTopologySwitch>("Topology: Switch"); |
| 1638 | this.evHistTopologyLink = |
| 1639 | new EventHistory<EventHistoryTopologyLink>("Topology: Link"); |
| 1640 | this.evHistTopologyCluster = |
| 1641 | new EventHistory<EventHistoryTopologyCluster>("Topology: Cluster"); |
| 1642 | } |
| 1643 | |
| 1644 | @Override |
| 1645 | @LogMessageDocs({ |
| 1646 | @LogMessageDoc(level="ERROR", |
| 1647 | message="No storage source found.", |
| 1648 | explanation="Storage source was not initialized; cannot initialize " + |
| 1649 | "link discovery.", |
| 1650 | recommendation=LogMessageDoc.REPORT_CONTROLLER_BUG), |
| 1651 | @LogMessageDoc(level="ERROR", |
| 1652 | message="Error in installing listener for " + |
| 1653 | "switch config table {table}", |
| 1654 | explanation="Failed to install storage notification for the " + |
| 1655 | "switch config table", |
| 1656 | recommendation=LogMessageDoc.REPORT_CONTROLLER_BUG), |
| 1657 | @LogMessageDoc(level="ERROR", |
| 1658 | message="No storage source found.", |
| 1659 | explanation="Storage source was not initialized; cannot initialize " + |
| 1660 | "link discovery.", |
| 1661 | recommendation=LogMessageDoc.REPORT_CONTROLLER_BUG), |
| 1662 | @LogMessageDoc(level="ERROR", |
| 1663 | message="Exception in LLDP send timer.", |
| 1664 | explanation="An unknown error occured while sending LLDP " + |
| 1665 | "messages to switches.", |
| 1666 | recommendation=LogMessageDoc.CHECK_SWITCH) |
| 1667 | }) |
| 1668 | public void startUp(FloodlightModuleContext context) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1669 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 1670 | controller = |
| 1671 | context.getServiceImpl(IFloodlightProviderService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1672 | |
| 1673 | // To be started by the first switch connection |
| 1674 | discoveryTask = new SingletonTask(ses, new Runnable() { |
| 1675 | @Override |
| 1676 | public void run() { |
| 1677 | try { |
| 1678 | discoverLinks(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1679 | } catch (Exception e) { |
| 1680 | log.error("Exception in LLDP send timer.", e); |
| 1681 | } finally { |
| 1682 | if (!shuttingDown) { |
Jonathan Hart | ec4f14e | 2013-12-12 10:46:38 -0800 | [diff] [blame] | 1683 | // Always reschedule link discovery if we're not |
| 1684 | // shutting down (no chance of SLAVE role now) |
| 1685 | log.trace("Rescheduling discovery task"); |
| 1686 | discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL, |
| 1687 | TimeUnit.SECONDS); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | }); |
| 1692 | |
Jonathan Hart | ec4f14e | 2013-12-12 10:46:38 -0800 | [diff] [blame] | 1693 | // Always reschedule link discovery as we are never in SLAVE role now |
| 1694 | discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL, TimeUnit.SECONDS); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1695 | |
| 1696 | // Setup the BDDP task. It is invoked whenever switch port tuples |
| 1697 | // are added to the quarantine list. |
| 1698 | bddpTask = new SingletonTask(ses, new QuarantineWorker()); |
| 1699 | bddpTask.reschedule(BDDP_TASK_INTERVAL, TimeUnit.MILLISECONDS); |
| 1700 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1701 | |
| 1702 | // Register for the OpenFlow messages we want to receive |
| 1703 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
| 1704 | floodlightProvider.addOFMessageListener(OFType.PORT_STATUS, this); |
| 1705 | // Register for switch updates |
| 1706 | floodlightProvider.addOFSwitchListener(this); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1707 | if (restApi != null) |
| 1708 | restApi.addRestletRoutable(new LinkDiscoveryWebRoutable()); |
| 1709 | setControllerTLV(); |
| 1710 | } |
| 1711 | |
| 1712 | // **************************************************** |
| 1713 | // Topology Manager's Event History members and methods |
| 1714 | // **************************************************** |
| 1715 | |
| 1716 | // Topology Manager event history |
| 1717 | public EventHistory<EventHistoryTopologySwitch> evHistTopologySwitch; |
| 1718 | public EventHistory<EventHistoryTopologyLink> evHistTopologyLink; |
| 1719 | public EventHistory<EventHistoryTopologyCluster> evHistTopologyCluster; |
| 1720 | public EventHistoryTopologySwitch evTopoSwitch; |
| 1721 | public EventHistoryTopologyLink evTopoLink; |
| 1722 | public EventHistoryTopologyCluster evTopoCluster; |
| 1723 | |
| 1724 | // Switch Added/Deleted |
| 1725 | private void evHistTopoSwitch(IOFSwitch sw, EvAction actn, String reason) { |
| 1726 | if (evTopoSwitch == null) { |
| 1727 | evTopoSwitch = new EventHistoryTopologySwitch(); |
| 1728 | } |
| 1729 | evTopoSwitch.dpid = sw.getId(); |
| 1730 | if ((sw.getChannel() != null) && |
| 1731 | (SocketAddress.class.isInstance( |
| 1732 | sw.getChannel().getRemoteAddress()))) { |
| 1733 | evTopoSwitch.ipv4Addr = |
| 1734 | IPv4.toIPv4Address(((InetSocketAddress)(sw.getChannel(). |
| 1735 | getRemoteAddress())).getAddress().getAddress()); |
| 1736 | evTopoSwitch.l4Port = |
| 1737 | ((InetSocketAddress)(sw.getChannel(). |
| 1738 | getRemoteAddress())).getPort(); |
| 1739 | } else { |
| 1740 | evTopoSwitch.ipv4Addr = 0; |
| 1741 | evTopoSwitch.l4Port = 0; |
| 1742 | } |
| 1743 | evTopoSwitch.reason = reason; |
| 1744 | evTopoSwitch = evHistTopologySwitch.put(evTopoSwitch, actn); |
| 1745 | } |
| 1746 | |
| 1747 | private void evHistTopoLink(long srcDpid, long dstDpid, short srcPort, |
| 1748 | short dstPort, int srcPortState, int dstPortState, |
| 1749 | ILinkDiscovery.LinkType linkType, |
| 1750 | EvAction actn, String reason) { |
| 1751 | if (evTopoLink == null) { |
| 1752 | evTopoLink = new EventHistoryTopologyLink(); |
| 1753 | } |
| 1754 | evTopoLink.srcSwDpid = srcDpid; |
| 1755 | evTopoLink.dstSwDpid = dstDpid; |
| 1756 | evTopoLink.srcSwport = srcPort & 0xffff; |
| 1757 | evTopoLink.dstSwport = dstPort & 0xffff; |
| 1758 | evTopoLink.srcPortState = srcPortState; |
| 1759 | evTopoLink.dstPortState = dstPortState; |
| 1760 | evTopoLink.reason = reason; |
| 1761 | switch (linkType) { |
| 1762 | case DIRECT_LINK: |
| 1763 | evTopoLink.linkType = "DIRECT_LINK"; |
| 1764 | break; |
| 1765 | case MULTIHOP_LINK: |
| 1766 | evTopoLink.linkType = "MULTIHOP_LINK"; |
| 1767 | break; |
| 1768 | case TUNNEL: |
| 1769 | evTopoLink.linkType = "TUNNEL"; |
| 1770 | break; |
| 1771 | case INVALID_LINK: |
| 1772 | default: |
| 1773 | evTopoLink.linkType = "Unknown"; |
| 1774 | break; |
| 1775 | } |
| 1776 | evTopoLink = evHistTopologyLink.put(evTopoLink, actn); |
| 1777 | } |
| 1778 | |
| 1779 | public void evHistTopoCluster(long dpid, long clusterIdOld, |
| 1780 | long clusterIdNew, EvAction action, String reason) { |
| 1781 | if (evTopoCluster == null) { |
| 1782 | evTopoCluster = new EventHistoryTopologyCluster(); |
| 1783 | } |
| 1784 | evTopoCluster.dpid = dpid; |
| 1785 | evTopoCluster.clusterIdOld = clusterIdOld; |
| 1786 | evTopoCluster.clusterIdNew = clusterIdNew; |
| 1787 | evTopoCluster.reason = reason; |
| 1788 | evTopoCluster = evHistTopologyCluster.put(evTopoCluster, action); |
| 1789 | } |
| 1790 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1791 | public boolean isAutoPortFastFeature() { |
| 1792 | return autoPortFastFeature; |
| 1793 | } |
| 1794 | |
| 1795 | public void setAutoPortFastFeature(boolean autoPortFastFeature) { |
| 1796 | this.autoPortFastFeature = autoPortFastFeature; |
| 1797 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1798 | } |