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