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 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.linkdiscovery; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
| 20 | import java.io.IOException; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 21 | import java.util.ArrayList; |
| 22 | import java.util.Collection; |
| 23 | import java.util.Collections; |
| 24 | import java.util.HashMap; |
| 25 | import java.util.HashSet; |
| 26 | import java.util.Iterator; |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 27 | import java.util.LinkedList; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 28 | import java.util.List; |
| 29 | import java.util.Map; |
| 30 | import java.util.Map.Entry; |
| 31 | import java.util.Set; |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 32 | import java.util.concurrent.CopyOnWriteArrayList; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 33 | import java.util.concurrent.ScheduledExecutorService; |
| 34 | import java.util.concurrent.TimeUnit; |
| 35 | import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 36 | |
| 37 | import net.floodlightcontroller.core.FloodlightContext; |
| 38 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 39 | import net.floodlightcontroller.core.IOFMessageListener; |
| 40 | import net.floodlightcontroller.core.IOFSwitch; |
| 41 | import net.floodlightcontroller.core.IOFSwitchListener; |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 42 | import net.floodlightcontroller.core.IUpdate; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 43 | import net.floodlightcontroller.core.annotations.LogMessageCategory; |
| 44 | import net.floodlightcontroller.core.annotations.LogMessageDoc; |
| 45 | import net.floodlightcontroller.core.annotations.LogMessageDocs; |
| 46 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 47 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
| 48 | import net.floodlightcontroller.core.module.IFloodlightModule; |
| 49 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 50 | import net.floodlightcontroller.core.util.SingletonTask; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 51 | import net.floodlightcontroller.restserver.IRestApiService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 52 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 53 | import net.onrc.onos.core.linkdiscovery.web.LinkDiscoveryWebRoutable; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 54 | import net.onrc.onos.core.packet.Ethernet; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 55 | import net.onrc.onos.core.packet.LLDP; |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 56 | import net.onrc.onos.core.packet.OnosLldp; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 57 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 58 | import net.onrc.onos.core.util.SwitchPort; |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 59 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 60 | import org.openflow.protocol.OFMessage; |
| 61 | import org.openflow.protocol.OFPacketIn; |
| 62 | import org.openflow.protocol.OFPacketOut; |
| 63 | import org.openflow.protocol.OFPhysicalPort; |
| 64 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
| 65 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
| 66 | import org.openflow.protocol.OFPort; |
| 67 | import org.openflow.protocol.OFPortStatus; |
| 68 | import org.openflow.protocol.OFPortStatus.OFPortReason; |
| 69 | import org.openflow.protocol.OFType; |
| 70 | import org.openflow.protocol.action.OFAction; |
| 71 | import org.openflow.protocol.action.OFActionOutput; |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 72 | import org.openflow.protocol.action.OFActionType; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 73 | import org.openflow.util.HexString; |
| 74 | import org.slf4j.Logger; |
| 75 | import org.slf4j.LoggerFactory; |
| 76 | |
| 77 | /** |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 78 | * Discovers links between OpenFlow switches. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | * <p/> |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 80 | * Discovery is performed by sending probes (LLDP packets) over the links in |
| 81 | * the data plane. The LinkDiscoveryManager sends probes periodically on all |
| 82 | * ports on all connected switches. The probes contain the sending switch's |
| 83 | * DPID and outgoing port number. LLDP packets that are received (via an |
| 84 | * OpenFlow packet-in) indicate there is a link between the receiving port and |
| 85 | * the sending port, which was encoded in the LLDP. When the |
| 86 | * LinkDiscoveryManager observes a new link, a Link object is created and an |
| 87 | * event is fired for any event listeners. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | * <p/> |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 89 | * Links are removed for one of three reasons: |
| 90 | * <ul> |
| 91 | * <li>A probe has not been received on the link for an interval (the timeout |
| 92 | * interval)</li> |
| 93 | * <li>The port went down or was disabled (as observed by OpenFlow port-status |
| 94 | * messages) or the disconnection of the switch</li> |
| 95 | * <li>Link discovery was explicitly disabled on a port with the |
| 96 | * {@link #disableDiscoveryOnPort(long, short)} method</li> |
| 97 | * </ul> |
| 98 | * When the LinkDiscoveryManager removes a link it also fires an event for the |
| 99 | * listeners. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 100 | */ |
| 101 | @LogMessageCategory("Network Topology") |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 102 | public class LinkDiscoveryManager implements IOFMessageListener, IOFSwitchListener, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 103 | ILinkDiscoveryService, IFloodlightModule { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 105 | private static final Logger log = |
| 106 | LoggerFactory.getLogger(LinkDiscoveryManager.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 108 | private IFloodlightProviderService controller; |
| 109 | |
| 110 | private IFloodlightProviderService floodlightProvider; |
| 111 | private IThreadPoolService threadPool; |
| 112 | private IRestApiService restApi; |
| 113 | private IControllerRegistryService registryService; |
HIGUCHI Yuta | e0515e5 | 2013-06-14 13:00:40 -0700 | [diff] [blame] | 114 | |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 115 | // LLDP fields |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 116 | static final byte[] LLDP_STANDARD_DST_MAC_STRING = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 117 | HexString.fromHexString("01:80:c2:00:00:0e"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 118 | private static final long LINK_LOCAL_MASK = 0xfffffffffff0L; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 119 | private static final long LINK_LOCAL_VALUE = 0x0180c2000000L; |
| 120 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 121 | // Link discovery task details. |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 122 | private SingletonTask discoveryTask; |
| 123 | private static final int DISCOVERY_TASK_INTERVAL = 1; |
| 124 | private static final int LINK_TIMEOUT = 35; // original 35 secs, aggressive 5 secs |
| 125 | private static final int LLDP_TO_ALL_INTERVAL = 15; //original 15 seconds, aggressive 2 secs. |
| 126 | private long lldpClock = 0; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 127 | // This value is intentionally kept higher than LLDP_TO_ALL_INTERVAL. |
| 128 | // If we want to identify link failures faster, we could decrease this |
| 129 | // value to a small number, say 1 or 2 sec. |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 130 | private static final int LLDP_TO_KNOWN_INTERVAL = 20; // LLDP frequency for known links |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 131 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 132 | private ReentrantReadWriteLock lock; |
HIGUCHI Yuta | e0515e5 | 2013-06-14 13:00:40 -0700 | [diff] [blame] | 133 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 134 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 135 | * Map from link to the most recent time it was verified functioning. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 136 | */ |
| 137 | protected Map<Link, LinkInfo> links; |
| 138 | |
| 139 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 140 | * Map from switch id to a set of all links with it as an endpoint. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 141 | */ |
| 142 | protected Map<Long, Set<Link>> switchLinks; |
| 143 | |
| 144 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 145 | * Map from a id:port to the set of links containing it as an endpoint. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 146 | */ |
| 147 | protected Map<NodePortTuple, Set<Link>> portLinks; |
| 148 | |
| 149 | /** |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 150 | * Listeners are called in the order they were added to the the list. |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 151 | */ |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 152 | private final List<ILinkDiscoveryListener> linkDiscoveryListeners |
| 153 | = new CopyOnWriteArrayList<>(); |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 154 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 155 | /** |
| 156 | * List of ports through which LLDPs are not sent. |
| 157 | */ |
| 158 | private Set<NodePortTuple> suppressLinkDiscovery; |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 159 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 160 | private enum UpdateType { |
| 161 | LINK_ADDED, |
| 162 | LINK_REMOVED |
| 163 | } |
| 164 | |
| 165 | private class LinkUpdate implements IUpdate { |
| 166 | private final Link link; |
| 167 | private final UpdateType operation; |
| 168 | |
| 169 | public LinkUpdate(Link link, UpdateType operation) { |
| 170 | this.link = link; |
| 171 | this.operation = operation; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | @Override |
| 175 | public void dispatch() { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 176 | if (log.isTraceEnabled()) { |
| 177 | log.trace("Dispatching link discovery update {} for {}", |
| 178 | operation, link); |
| 179 | } |
| 180 | for (ILinkDiscoveryListener listener : linkDiscoveryListeners) { |
| 181 | switch (operation) { |
| 182 | case LINK_ADDED: |
| 183 | listener.linkAdded(link); |
| 184 | break; |
| 185 | case LINK_REMOVED: |
| 186 | listener.linkRemoved(link); |
| 187 | break; |
| 188 | default: |
| 189 | log.warn("Unknown link update operation {}", operation); |
| 190 | break; |
Jonathan Hart | b0904bf | 2013-11-26 14:41:11 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 193 | } |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 194 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 195 | |
| 196 | /** |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 197 | * Gets the LLDP sending period in seconds. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 198 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 199 | * @return LLDP sending period in seconds. |
| 200 | */ |
| 201 | public int getLldpFrequency() { |
| 202 | return LLDP_TO_KNOWN_INTERVAL; |
| 203 | } |
| 204 | |
| 205 | /** |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 206 | * Gets the LLDP timeout value in seconds. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 207 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 208 | * @return LLDP timeout value in seconds |
| 209 | */ |
| 210 | public int getLldpTimeout() { |
| 211 | return LINK_TIMEOUT; |
| 212 | } |
| 213 | |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 214 | @Override |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 215 | public Set<NodePortTuple> getDiscoveryDisabledPorts() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 216 | return suppressLinkDiscovery; |
| 217 | } |
| 218 | |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 219 | @Override |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 220 | public void disableDiscoveryOnPort(long sw, short port) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 221 | NodePortTuple npt = new NodePortTuple(sw, port); |
| 222 | this.suppressLinkDiscovery.add(npt); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 223 | deleteLinksOnPort(npt); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 226 | @Override |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 227 | public void enableDiscoveryOnPort(long sw, short port) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 228 | NodePortTuple npt = new NodePortTuple(sw, port); |
| 229 | this.suppressLinkDiscovery.remove(npt); |
| 230 | discover(npt); |
| 231 | } |
| 232 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 233 | private boolean isLinkDiscoverySuppressed(long sw, short portNumber) { |
| 234 | return this.suppressLinkDiscovery.contains(new NodePortTuple(sw, portNumber)); |
| 235 | } |
| 236 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 237 | private void discoverLinks() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 238 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 239 | // time out known links. |
| 240 | timeOutLinks(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 241 | |
| 242 | //increment LLDP clock |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 243 | lldpClock = (lldpClock + 1) % LLDP_TO_ALL_INTERVAL; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 244 | |
| 245 | if (lldpClock == 0) { |
| 246 | log.debug("Sending LLDP out on all ports."); |
| 247 | discoverOnAllPorts(); |
| 248 | } |
| 249 | } |
| 250 | |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 251 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 252 | * Send LLDP on known ports. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 253 | */ |
| 254 | protected void discoverOnKnownLinkPorts() { |
| 255 | // Copy the port set. |
| 256 | Set<NodePortTuple> nptSet = new HashSet<NodePortTuple>(); |
| 257 | nptSet.addAll(portLinks.keySet()); |
| 258 | |
| 259 | // Send LLDP from each of them. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 260 | for (NodePortTuple npt : nptSet) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 261 | discover(npt); |
| 262 | } |
| 263 | } |
| 264 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 265 | private void discover(NodePortTuple npt) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 266 | discover(npt.getNodeId(), npt.getPortId()); |
| 267 | } |
| 268 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 269 | private void discover(long sw, short port) { |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 270 | sendDiscoveryMessage(sw, port, false); |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 271 | } |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 272 | |
Umesh Krishnaswamy | f962d64 | 2013-01-23 19:04:23 -0800 | [diff] [blame] | 273 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 274 | * Send link discovery message out of a given switch port. |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 275 | * The discovery message is a standard LLDP containing ONOS-specific TLVs. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 276 | * |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 277 | * @param sw the switch to send on |
| 278 | * @param port the port to send out |
| 279 | * @param isReverse indicates whether the LLDP was sent as a response |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 280 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 281 | @LogMessageDoc(level = "ERROR", |
| 282 | message = "Failure sending LLDP out port {port} on switch {switch}", |
| 283 | explanation = "An I/O error occured while sending LLDP message " + |
| 284 | "to the switch.", |
| 285 | recommendation = LogMessageDoc.CHECK_SWITCH) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 286 | protected void sendDiscoveryMessage(long sw, short port, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 287 | boolean isReverse) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 288 | |
| 289 | IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw); |
| 290 | if (iofSwitch == null) { |
| 291 | return; |
| 292 | } |
| 293 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 294 | if (port == OFPort.OFPP_LOCAL.getValue()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 295 | return; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 296 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 297 | |
| 298 | OFPhysicalPort ofpPort = iofSwitch.getPort(port); |
| 299 | |
| 300 | if (ofpPort == null) { |
| 301 | if (log.isTraceEnabled()) { |
| 302 | log.trace("Null physical port. sw={}, port={}", sw, port); |
| 303 | } |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | if (isLinkDiscoverySuppressed(sw, port)) { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 308 | // Don't send LLDPs out of this port as suppressLLDPs set |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 312 | if (log.isTraceEnabled()) { |
| 313 | log.trace("Sending LLDP packet out of swich: {}, port: {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 314 | sw, port); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 315 | } |
| 316 | |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 317 | OFPacketOut po = createLLDPPacketOut(sw, ofpPort, isReverse); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 318 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 319 | try { |
| 320 | iofSwitch.write(po, null); |
| 321 | iofSwitch.flush(); |
| 322 | } catch (IOException e) { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 323 | log.error("Failure sending LLDP out port " + port + " on switch " |
| 324 | + iofSwitch.getStringId(), e); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | } |
| 328 | |
| 329 | /** |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 330 | * Creates packet_out LLDP for specified output port. |
| 331 | * |
| 332 | * @param dpid the dpid of the outgoing switch |
| 333 | * @param port the outgoing port |
| 334 | * @param isReverse whether this is a reverse LLDP or not |
| 335 | * @return Packet_out message with LLDP data |
| 336 | */ |
| 337 | private OFPacketOut createLLDPPacketOut(long dpid, |
| 338 | final OFPhysicalPort port, boolean isReverse) { |
| 339 | // Set up packets |
| 340 | // TODO optimize by not creating new packets each time |
| 341 | OnosLldp lldpPacket = new OnosLldp(); |
| 342 | |
| 343 | Ethernet ethPacket = new Ethernet(); |
| 344 | ethPacket.setEtherType(Ethernet.TYPE_LLDP); |
| 345 | ethPacket.setDestinationMACAddress(LLDP_STANDARD_DST_MAC_STRING); |
| 346 | ethPacket.setPayload(lldpPacket); |
| 347 | ethPacket.setPad(true); |
| 348 | |
| 349 | final OFPacketOut packetOut = (OFPacketOut) floodlightProvider.getOFMessageFactory() |
| 350 | .getMessage(OFType.PACKET_OUT); |
| 351 | packetOut.setBufferId(OFPacketOut.BUFFER_ID_NONE); |
| 352 | |
| 353 | final List<OFAction> actionsList = new LinkedList<OFAction>(); |
| 354 | final OFActionOutput out = (OFActionOutput) floodlightProvider.getOFMessageFactory() |
| 355 | .getAction(OFActionType.OUTPUT); |
| 356 | out.setPort(port.getPortNumber()); |
| 357 | actionsList.add(out); |
| 358 | packetOut.setActions(actionsList); |
| 359 | final short alen = (short) OFActionOutput.MINIMUM_LENGTH; |
| 360 | |
| 361 | lldpPacket.setSwitch(dpid); |
| 362 | lldpPacket.setPort(port.getPortNumber()); |
| 363 | lldpPacket.setReverse(isReverse); |
| 364 | ethPacket.setSourceMACAddress(port.getHardwareAddress()); |
| 365 | |
| 366 | final byte[] lldp = ethPacket.serialize(); |
| 367 | packetOut.setActionsLength(alen); |
| 368 | packetOut.setPacketData(lldp); |
| 369 | packetOut |
| 370 | .setLength((short) (OFPacketOut.MINIMUM_LENGTH + alen + lldp.length)); |
| 371 | return packetOut; |
| 372 | } |
| 373 | |
| 374 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 375 | * Send LLDPs to all switch-ports. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 376 | */ |
| 377 | protected void discoverOnAllPorts() { |
| 378 | if (log.isTraceEnabled()) { |
Yuta HIGUCHI | 5302ddf | 2014-01-06 12:53:35 -0800 | [diff] [blame] | 379 | log.trace("Sending LLDP packets out of all the enabled ports on switch"); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 380 | } |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 381 | |
| 382 | for (IOFSwitch sw : floodlightProvider.getSwitches().values()) { |
| 383 | if (sw.getEnabledPorts() == null) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 384 | continue; |
| 385 | } |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 386 | for (OFPhysicalPort ofp : sw.getEnabledPorts()) { |
| 387 | if (isLinkDiscoverySuppressed(sw.getId(), ofp.getPortNumber())) { |
| 388 | continue; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 389 | } |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 390 | |
| 391 | sendDiscoveryMessage(sw.getId(), ofp.getPortNumber(), false); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 396 | @Override |
| 397 | public String getName() { |
| 398 | return "linkdiscovery"; |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) { |
| 403 | switch (msg.getType()) { |
| 404 | case PACKET_IN: |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 405 | if (msg instanceof OFPacketIn) { |
| 406 | return this.handlePacketIn(sw.getId(), (OFPacketIn) msg, |
| 407 | cntx); |
| 408 | } |
| 409 | break; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 410 | case PORT_STATUS: |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 411 | if (msg instanceof OFPortStatus) { |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 412 | return this.handlePortStatus(sw, (OFPortStatus) msg); |
Pavlin Radoslavov | 0b88a26 | 2014-04-10 15:43:27 -0700 | [diff] [blame] | 413 | } |
| 414 | break; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 415 | default: |
| 416 | break; |
| 417 | } |
| 418 | return Command.CONTINUE; |
| 419 | } |
| 420 | |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 421 | protected Command handleLldp(LLDP lldp, long sw, OFPacketIn pi) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 422 | // If LLDP is suppressed on this port, ignore received packet as well |
| 423 | IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw); |
| 424 | if (iofSwitch == null) { |
| 425 | return Command.STOP; |
| 426 | } |
| 427 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 428 | if (isLinkDiscoverySuppressed(sw, pi.getInPort())) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 429 | return Command.STOP; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 430 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 431 | |
| 432 | // If this is a malformed LLDP, or not from us, exit |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 433 | if (lldp.getPortId() == null || lldp.getPortId().getLength() != 3) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 434 | return Command.CONTINUE; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 435 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 436 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 437 | // Verify this LLDP packet matches what we're looking for |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 438 | byte[] packetData = pi.getPacketData(); |
| 439 | if (!OnosLldp.isOnosLldp(packetData)) { |
| 440 | log.trace("Dropping LLDP that wasn't sent by ONOS"); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 441 | return Command.STOP; |
| 442 | } |
| 443 | |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 444 | SwitchPort switchPort = OnosLldp.extractSwitchPort(packetData); |
| 445 | long remoteDpid = switchPort.dpid().value(); |
| 446 | short remotePort = switchPort.port().value(); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 447 | IOFSwitch remoteSwitch = floodlightProvider.getSwitches().get( |
| 448 | switchPort.dpid().value()); |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 449 | |
| 450 | |
| 451 | OFPhysicalPort physicalPort = null; |
| 452 | if (remoteSwitch != null) { |
| 453 | physicalPort = remoteSwitch.getPort(remotePort); |
| 454 | if (!remoteSwitch.portEnabled(remotePort)) { |
| 455 | if (log.isTraceEnabled()) { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 456 | log.trace("Ignoring link with disabled source port: " + |
| 457 | "switch {} port {}", remoteSwitch, remotePort); |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 458 | } |
| 459 | return Command.STOP; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 460 | } |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 461 | if (suppressLinkDiscovery.contains( |
| 462 | new NodePortTuple(remoteSwitch.getId(), remotePort))) { |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 463 | if (log.isTraceEnabled()) { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 464 | log.trace("Ignoring link with suppressed src port: " + |
| 465 | "switch {} port {}", remoteSwitch, remotePort); |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 466 | } |
| 467 | return Command.STOP; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 468 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 469 | } |
| 470 | if (!iofSwitch.portEnabled(pi.getInPort())) { |
| 471 | if (log.isTraceEnabled()) { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 472 | log.trace("Ignoring link with disabled dest port: " + |
| 473 | "switch {} port {}", sw, pi.getInPort()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 474 | } |
| 475 | return Command.STOP; |
| 476 | } |
| 477 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 478 | int srcPortState = (physicalPort != null) ? physicalPort.getState() : 0; |
| 479 | physicalPort = iofSwitch.getPort(pi.getInPort()); |
| 480 | int dstPortState = (physicalPort != null) ? physicalPort.getState() : 0; |
| 481 | |
| 482 | // Store the time of update to this link, and push it out to routingEngine |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 483 | Link lt = new Link(remoteDpid, remotePort, iofSwitch.getId(), pi.getInPort()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 484 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 485 | LinkInfo linkInfo = new LinkInfo(System.currentTimeMillis(), |
| 486 | System.currentTimeMillis(), srcPortState, dstPortState); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 487 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 488 | addOrUpdateLink(lt, linkInfo); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 489 | |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 490 | // Check if reverse link exists. |
| 491 | // If it doesn't exist and if the forward link was seen |
| 492 | // first seen within a small interval, send probe on the |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 493 | // reverse link. |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 494 | boolean isReverse = OnosLldp.isReverse(lldp); |
| 495 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 496 | LinkInfo newLinkInfo = links.get(lt); |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 497 | if (newLinkInfo != null && !isReverse) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 498 | Link reverseLink = new Link(lt.getDst(), lt.getDstPort(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 499 | lt.getSrc(), lt.getSrcPort()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 500 | LinkInfo reverseInfo = links.get(reverseLink); |
| 501 | if (reverseInfo == null) { |
| 502 | // the reverse link does not exist. |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 503 | if (newLinkInfo.getFirstSeenTime() > |
| 504 | System.currentTimeMillis() - LINK_TIMEOUT) { |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 505 | this.sendDiscoveryMessage(lt.getDst(), lt.getDstPort(), true); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 510 | // Consume this message |
| 511 | return Command.STOP; |
| 512 | } |
| 513 | |
| 514 | protected Command handlePacketIn(long sw, OFPacketIn pi, |
| 515 | FloodlightContext cntx) { |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 516 | Ethernet eth = |
| 517 | IFloodlightProviderService.bcStore.get(cntx, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 518 | IFloodlightProviderService.CONTEXT_PI_PAYLOAD); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 519 | |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 520 | if (eth.getEtherType() == Ethernet.TYPE_LLDP) { |
| 521 | return handleLldp((LLDP) eth.getPayload(), sw, pi); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 522 | } else if (eth.getEtherType() < 1500) { |
| 523 | long destMac = eth.getDestinationMAC().toLong(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 524 | if ((destMac & LINK_LOCAL_MASK) == LINK_LOCAL_VALUE) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 525 | if (log.isTraceEnabled()) { |
| 526 | log.trace("Ignoring packet addressed to 802.1D/Q " + |
| 527 | "reserved address."); |
| 528 | } |
| 529 | return Command.STOP; |
| 530 | } |
| 531 | } |
| 532 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 533 | return Command.CONTINUE; |
| 534 | } |
| 535 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 536 | protected void addOrUpdateLink(Link lt, LinkInfo detectedLinkInfo) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 537 | lock.writeLock().lock(); |
| 538 | try { |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 539 | LinkInfo existingInfo = links.get(lt); |
| 540 | |
| 541 | LinkInfo newLinkInfo = new LinkInfo( |
| 542 | ((existingInfo == null) ? detectedLinkInfo.getFirstSeenTime() |
| 543 | : existingInfo.getFirstSeenTime()), |
| 544 | detectedLinkInfo.getLastProbeReceivedTime(), |
| 545 | detectedLinkInfo.getSrcPortState(), |
| 546 | detectedLinkInfo.getDstPortState()); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 547 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 548 | // Add new LinkInfo or update old LinkInfo |
| 549 | links.put(lt, newLinkInfo); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 550 | |
| 551 | if (log.isTraceEnabled()) { |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 552 | log.trace("addOrUpdateLink: {}", lt); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 555 | NodePortTuple srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort()); |
| 556 | NodePortTuple dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 557 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 558 | // If this is the first time we've seen the link, add the Link |
| 559 | // object to the data structures/indexes as well |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 560 | if (existingInfo == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 561 | // index it by switch source |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 562 | if (!switchLinks.containsKey(lt.getSrc())) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 563 | switchLinks.put(lt.getSrc(), new HashSet<Link>()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 564 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 565 | switchLinks.get(lt.getSrc()).add(lt); |
| 566 | |
| 567 | // index it by switch dest |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 568 | if (!switchLinks.containsKey(lt.getDst())) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 569 | switchLinks.put(lt.getDst(), new HashSet<Link>()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 570 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 571 | switchLinks.get(lt.getDst()).add(lt); |
| 572 | |
| 573 | // index both ends by switch:port |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 574 | if (!portLinks.containsKey(srcNpt)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 575 | portLinks.put(srcNpt, new HashSet<Link>()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 576 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 577 | portLinks.get(srcNpt).add(lt); |
| 578 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 579 | if (!portLinks.containsKey(dstNpt)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 580 | portLinks.put(dstNpt, new HashSet<Link>()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 581 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 582 | portLinks.get(dstNpt).add(lt); |
| 583 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 584 | // Publish LINK_ADDED event |
| 585 | controller.publishUpdate(new LinkUpdate(lt, UpdateType.LINK_ADDED)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 586 | } |
| 587 | } finally { |
| 588 | lock.writeLock().unlock(); |
| 589 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /** |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 593 | * Removes links from the data structures. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 594 | * |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 595 | * @param linksToDelete the list of links to delete |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 596 | */ |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 597 | protected void deleteLinks(List<Link> linksToDelete) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 598 | lock.writeLock().lock(); |
| 599 | try { |
Ray Milkey | 5df613b | 2014-04-15 10:50:56 -0700 | [diff] [blame] | 600 | for (Link lt : linksToDelete) { |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 601 | NodePortTuple srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort()); |
| 602 | NodePortTuple dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 603 | |
| 604 | switchLinks.get(lt.getSrc()).remove(lt); |
| 605 | switchLinks.get(lt.getDst()).remove(lt); |
| 606 | if (switchLinks.containsKey(lt.getSrc()) && |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 607 | switchLinks.get(lt.getSrc()).isEmpty()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 608 | this.switchLinks.remove(lt.getSrc()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 609 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 610 | if (this.switchLinks.containsKey(lt.getDst()) && |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 611 | this.switchLinks.get(lt.getDst()).isEmpty()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 612 | this.switchLinks.remove(lt.getDst()); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 613 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 614 | |
| 615 | if (this.portLinks.get(srcNpt) != null) { |
| 616 | this.portLinks.get(srcNpt).remove(lt); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 617 | if (this.portLinks.get(srcNpt).isEmpty()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 618 | this.portLinks.remove(srcNpt); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 619 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 620 | } |
| 621 | if (this.portLinks.get(dstNpt) != null) { |
| 622 | this.portLinks.get(dstNpt).remove(lt); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 623 | if (this.portLinks.get(dstNpt).isEmpty()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 624 | this.portLinks.remove(dstNpt); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 625 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 628 | this.links.remove(lt); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 629 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 630 | controller.publishUpdate(new LinkUpdate(lt, |
| 631 | UpdateType.LINK_REMOVED)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 632 | |
| 633 | if (log.isTraceEnabled()) { |
| 634 | log.trace("Deleted link {}", lt); |
| 635 | } |
| 636 | } |
| 637 | } finally { |
| 638 | lock.writeLock().unlock(); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Handles an OFPortStatus message from a switch. We will add or |
| 644 | * delete LinkTupes as well re-compute the topology if needed. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 645 | * |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 646 | * @param sw The dpid of the switch that sent the port status message |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 647 | * @param ps The OFPortStatus message |
| 648 | * @return The Command to continue or stop after we process this message |
| 649 | */ |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 650 | protected Command handlePortStatus(IOFSwitch sw, OFPortStatus ps) { |
HIGUCHI Yuta | a89b284 | 2013-06-17 13:54:57 -0700 | [diff] [blame] | 651 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 652 | // If we do not control this switch, then we should not process its |
| 653 | // port status messages |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 654 | if (!registryService.hasControl(sw.getId())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 655 | return Command.CONTINUE; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 656 | } |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 657 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 658 | if (log.isTraceEnabled()) { |
| 659 | log.trace("handlePortStatus: Switch {} port #{} reason {}; " + |
| 660 | "config is {} state is {}", |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 661 | new Object[]{sw.getStringId(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 662 | ps.getDesc().getPortNumber(), |
| 663 | ps.getReason(), |
| 664 | ps.getDesc().getConfig(), |
| 665 | ps.getDesc().getState()}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | short port = ps.getDesc().getPortNumber(); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 669 | NodePortTuple npt = new NodePortTuple(sw.getId(), port); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 670 | boolean linkDeleted = false; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 671 | boolean linkInfoChanged = false; |
| 672 | |
| 673 | lock.writeLock().lock(); |
| 674 | try { |
| 675 | // if ps is a delete, or a modify where the port is down or |
| 676 | // configured down |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 677 | if ((byte) OFPortReason.OFPPR_DELETE.ordinal() == ps.getReason() || |
| 678 | ((byte) OFPortReason.OFPPR_MODIFY.ordinal() == |
| 679 | ps.getReason() && !portEnabled(ps.getDesc()))) { |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 680 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 681 | deleteLinksOnPort(npt); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 682 | linkDeleted = true; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 683 | } else if (ps.getReason() == |
| 684 | (byte) OFPortReason.OFPPR_MODIFY.ordinal()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 685 | // If ps is a port modification and the port state has changed |
| 686 | // that affects links in the topology |
| 687 | |
| 688 | if (this.portLinks.containsKey(npt)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 689 | for (Link lt : this.portLinks.get(npt)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 690 | LinkInfo linkInfo = links.get(lt); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 691 | assert (linkInfo != null); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 692 | LinkInfo newLinkInfo = null; |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 693 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 694 | if (lt.isSrcPort(npt) && |
| 695 | linkInfo.getSrcPortState() != ps.getDesc().getState()) { |
| 696 | // If this port status is for the src port and the port |
| 697 | // state has changed, create a new link info with the new state |
| 698 | |
| 699 | newLinkInfo = new LinkInfo(linkInfo.getFirstSeenTime(), |
| 700 | linkInfo.getLastProbeReceivedTime(), |
| 701 | ps.getDesc().getState(), |
| 702 | linkInfo.getDstPortState()); |
| 703 | } else if (lt.isDstPort(npt) && |
| 704 | linkInfo.getDstPortState() != ps.getDesc().getState()) { |
| 705 | // If this port status is for the dst port and the port |
| 706 | // state has changed, create a new link info with the new state |
| 707 | |
| 708 | newLinkInfo = new LinkInfo(linkInfo.getFirstSeenTime(), |
| 709 | linkInfo.getLastProbeReceivedTime(), |
| 710 | linkInfo.getSrcPortState(), |
| 711 | ps.getDesc().getState()); |
| 712 | } |
| 713 | |
| 714 | if (newLinkInfo != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 715 | linkInfoChanged = true; |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 716 | links.put(lt, newLinkInfo); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 722 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 723 | if (!linkDeleted && !linkInfoChanged) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 724 | if (log.isTraceEnabled()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 725 | log.trace("handlePortStatus: Switch {} port #{} reason {};" + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 726 | " no links to update/remove", |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 727 | new Object[]{HexString.toHexString(sw.getId()), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 728 | ps.getDesc().getPortNumber(), |
| 729 | ps.getReason()}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | } finally { |
| 733 | lock.writeLock().unlock(); |
| 734 | } |
| 735 | |
| 736 | if (!linkDeleted) { |
| 737 | // Send LLDP right away when port state is changed for faster |
| 738 | // cluster-merge. If it is a link delete then there is not need |
| 739 | // to send the LLDPs right away and instead we wait for the LLDPs |
| 740 | // to be sent on the timer as it is normally done |
| 741 | // do it outside the write-lock |
| 742 | // sendLLDPTask.reschedule(1000, TimeUnit.MILLISECONDS); |
| 743 | processNewPort(npt.getNodeId(), npt.getPortId()); |
| 744 | } |
| 745 | return Command.CONTINUE; |
| 746 | } |
| 747 | |
| 748 | /** |
| 749 | * Process a new port. |
| 750 | * If link discovery is disabled on the port, then do nothing. |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 751 | * Otherwise, send LLDP message. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 752 | * |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 753 | * @param sw the dpid of the switch the port is on |
| 754 | * @param p the number of the port |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 755 | */ |
| 756 | private void processNewPort(long sw, short p) { |
| 757 | if (isLinkDiscoverySuppressed(sw, p)) { |
| 758 | // Do nothing as link discovery is suppressed. |
Ray Milkey | 1aa71f8 | 2014-04-08 16:23:24 -0700 | [diff] [blame] | 759 | return; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 760 | } else { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 761 | discover(sw, p); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
| 765 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 766 | * We send out LLDP messages when a switch is added to discover the topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 767 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 768 | * @param sw The IOFSwitch that connected to the controller |
| 769 | */ |
| 770 | @Override |
| 771 | public void addedSwitch(IOFSwitch sw) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 772 | if (sw.getEnabledPorts() != null) { |
| 773 | for (Short p : sw.getEnabledPortNumbers()) { |
| 774 | processNewPort(sw.getId(), p); |
| 775 | } |
| 776 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /** |
| 780 | * When a switch disconnects we remove any links from our map and notify. |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 781 | * |
| 782 | * @param iofSwitch the switch that was removed |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 783 | */ |
| 784 | @Override |
| 785 | public void removedSwitch(IOFSwitch iofSwitch) { |
| 786 | // Update event history |
| 787 | long sw = iofSwitch.getId(); |
Jonathan Hart | 6d20802 | 2014-06-24 18:56:36 -0700 | [diff] [blame] | 788 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 789 | List<Link> eraseList = new ArrayList<Link>(); |
| 790 | lock.writeLock().lock(); |
| 791 | try { |
| 792 | if (switchLinks.containsKey(sw)) { |
| 793 | if (log.isTraceEnabled()) { |
| 794 | log.trace("Handle switchRemoved. Switch {}; removing links {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 795 | HexString.toHexString(sw), switchLinks.get(sw)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 796 | } |
| 797 | // add all tuples with an endpoint on this switch to erase list |
| 798 | eraseList.addAll(switchLinks.get(sw)); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 799 | deleteLinks(eraseList); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 800 | } |
| 801 | } finally { |
| 802 | lock.writeLock().unlock(); |
| 803 | } |
| 804 | } |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 805 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 806 | /* |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 807 | * We don't react the port changed notifications here. we listen for |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 808 | * OFPortStatus messages directly. Might consider using this notifier |
| 809 | * instead |
| 810 | */ |
| 811 | @Override |
| 812 | public void switchPortChanged(Long switchId) { |
| 813 | // no-op |
| 814 | } |
| 815 | |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 816 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 817 | * Delete links incident on a given switch port. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 818 | * |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 819 | * @param npt the port to delete links on |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 820 | */ |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 821 | protected void deleteLinksOnPort(NodePortTuple npt) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 822 | List<Link> eraseList = new ArrayList<Link>(); |
| 823 | if (this.portLinks.containsKey(npt)) { |
| 824 | if (log.isTraceEnabled()) { |
| 825 | log.trace("handlePortStatus: Switch {} port #{} " + |
| 826 | "removing links {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 827 | new Object[]{HexString.toHexString(npt.getNodeId()), |
| 828 | npt.getPortId(), |
| 829 | this.portLinks.get(npt)}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 830 | } |
| 831 | eraseList.addAll(this.portLinks.get(npt)); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 832 | deleteLinks(eraseList); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 836 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 837 | * Iterates through the list of links and deletes if the |
| 838 | * last discovery message reception time exceeds timeout values. |
| 839 | */ |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 840 | protected void timeOutLinks() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 841 | List<Link> eraseList = new ArrayList<Link>(); |
| 842 | Long curTime = System.currentTimeMillis(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 843 | |
| 844 | // reentrant required here because deleteLink also write locks |
| 845 | lock.writeLock().lock(); |
| 846 | try { |
| 847 | Iterator<Entry<Link, LinkInfo>> it = |
| 848 | this.links.entrySet().iterator(); |
| 849 | while (it.hasNext()) { |
| 850 | Entry<Link, LinkInfo> entry = it.next(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 851 | LinkInfo info = entry.getValue(); |
| 852 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 853 | if ((info.getLastProbeReceivedTime() + (1000L * LINK_TIMEOUT) |
| 854 | < curTime)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 855 | eraseList.add(entry.getKey()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 859 | deleteLinks(eraseList); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 860 | } finally { |
| 861 | lock.writeLock().unlock(); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | private boolean portEnabled(OFPhysicalPort port) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 866 | if (port == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 867 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 868 | } |
| 869 | if ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 870 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 871 | } |
| 872 | if ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 873 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 874 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 875 | return true; |
| 876 | } |
| 877 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 878 | @Override |
| 879 | public Map<Link, LinkInfo> getLinks() { |
| 880 | lock.readLock().lock(); |
| 881 | Map<Link, LinkInfo> result; |
| 882 | try { |
| 883 | result = new HashMap<Link, LinkInfo>(links); |
| 884 | } finally { |
| 885 | lock.readLock().unlock(); |
| 886 | } |
| 887 | return result; |
| 888 | } |
| 889 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 890 | @Override |
| 891 | public void addListener(ILinkDiscoveryListener listener) { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 892 | linkDiscoveryListeners.add(listener); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 893 | } |
| 894 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 895 | @Override |
| 896 | public void removeListener(ILinkDiscoveryListener listener) { |
| 897 | linkDiscoveryListeners.remove(listener); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 900 | @Override |
| 901 | public boolean isCallbackOrderingPrereq(OFType type, String name) { |
| 902 | return false; |
| 903 | } |
| 904 | |
| 905 | @Override |
| 906 | public boolean isCallbackOrderingPostreq(OFType type, String name) { |
| 907 | return false; |
| 908 | } |
| 909 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 910 | // IFloodlightModule classes |
| 911 | |
| 912 | @Override |
| 913 | public Collection<Class<? extends IFloodlightService>> getModuleServices() { |
Yuta HIGUCHI | e881340 | 2014-01-08 13:36:50 -0800 | [diff] [blame] | 914 | Collection<Class<? extends IFloodlightService>> l = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 915 | new ArrayList<Class<? extends IFloodlightService>>(); |
| 916 | l.add(ILinkDiscoveryService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 917 | return l; |
| 918 | } |
| 919 | |
| 920 | @Override |
| 921 | public Map<Class<? extends IFloodlightService>, IFloodlightService> |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 922 | getServiceImpls() { |
| 923 | Map<Class<? extends IFloodlightService>, IFloodlightService> m = |
| 924 | new HashMap<>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 925 | m.put(ILinkDiscoveryService.class, this); |
| 926 | return m; |
| 927 | } |
| 928 | |
| 929 | @Override |
| 930 | public Collection<Class<? extends IFloodlightService>> getModuleDependencies() { |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 931 | Collection<Class<? extends IFloodlightService>> l = new ArrayList<>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 932 | l.add(IFloodlightProviderService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 933 | l.add(IThreadPoolService.class); |
| 934 | l.add(IRestApiService.class); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 935 | l.add(IControllerRegistryService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 936 | return l; |
| 937 | } |
| 938 | |
| 939 | @Override |
| 940 | public void init(FloodlightModuleContext context) |
| 941 | throws FloodlightModuleException { |
| 942 | floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 943 | threadPool = context.getServiceImpl(IThreadPoolService.class); |
| 944 | restApi = context.getServiceImpl(IRestApiService.class); |
Umesh Krishnaswamy | 57a32a9 | 2013-03-21 14:21:15 -0700 | [diff] [blame] | 945 | registryService = context.getServiceImpl(IControllerRegistryService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 946 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 947 | this.lock = new ReentrantReadWriteLock(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 948 | this.links = new HashMap<Link, LinkInfo>(); |
| 949 | this.portLinks = new HashMap<NodePortTuple, Set<Link>>(); |
| 950 | this.suppressLinkDiscovery = |
| 951 | Collections.synchronizedSet(new HashSet<NodePortTuple>()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 952 | this.switchLinks = new HashMap<Long, Set<Link>>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | @Override |
| 956 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 957 | @LogMessageDoc(level = "ERROR", |
| 958 | message = "No storage source found.", |
| 959 | explanation = "Storage source was not initialized; cannot initialize " + |
| 960 | "link discovery.", |
| 961 | recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG), |
| 962 | @LogMessageDoc(level = "ERROR", |
| 963 | message = "Error in installing listener for " + |
| 964 | "switch config table {table}", |
| 965 | explanation = "Failed to install storage notification for the " + |
| 966 | "switch config table", |
| 967 | recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG), |
| 968 | @LogMessageDoc(level = "ERROR", |
| 969 | message = "No storage source found.", |
| 970 | explanation = "Storage source was not initialized; cannot initialize " + |
| 971 | "link discovery.", |
| 972 | recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG), |
| 973 | @LogMessageDoc(level = "ERROR", |
| 974 | message = "Exception in LLDP send timer.", |
| 975 | explanation = "An unknown error occured while sending LLDP " + |
| 976 | "messages to switches.", |
| 977 | recommendation = LogMessageDoc.CHECK_SWITCH) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 978 | }) |
| 979 | public void startUp(FloodlightModuleContext context) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 980 | ScheduledExecutorService ses = threadPool.getScheduledExecutor(); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 981 | controller = context.getServiceImpl(IFloodlightProviderService.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 982 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 983 | discoveryTask = new SingletonTask(ses, new Runnable() { |
| 984 | @Override |
| 985 | public void run() { |
| 986 | try { |
| 987 | discoverLinks(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 988 | } catch (Exception e) { |
| 989 | log.error("Exception in LLDP send timer.", e); |
| 990 | } finally { |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 991 | log.trace("Rescheduling discovery task"); |
| 992 | discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL, |
| 993 | TimeUnit.SECONDS); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 994 | } |
| 995 | } |
| 996 | }); |
| 997 | |
Jonathan Hart | ec4f14e | 2013-12-12 10:46:38 -0800 | [diff] [blame] | 998 | discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL, TimeUnit.SECONDS); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 999 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1000 | // Register for the OpenFlow messages we want to receive |
| 1001 | floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this); |
| 1002 | floodlightProvider.addOFMessageListener(OFType.PORT_STATUS, this); |
| 1003 | // Register for switch updates |
| 1004 | floodlightProvider.addOFSwitchListener(this); |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 1005 | restApi.addRestletRoutable(new LinkDiscoveryWebRoutable()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1006 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1007 | } |