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