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