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