blob: 9b913c1a1e92db64f85d135d6fc0293724d7c66a [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
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 Hart23701d12014-04-03 10:45:48 -070018package net.onrc.onos.core.linkdiscovery.internal;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.io.IOException;
21import java.net.InetAddress;
22import java.net.InetSocketAddress;
23import java.net.NetworkInterface;
24import java.net.SocketAddress;
25import java.nio.ByteBuffer;
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.Collections;
29import java.util.HashMap;
30import java.util.HashSet;
31import java.util.Iterator;
32import java.util.List;
33import java.util.Map;
34import java.util.Map.Entry;
35import java.util.Set;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036import java.util.concurrent.LinkedBlockingQueue;
37import java.util.concurrent.ScheduledExecutorService;
38import java.util.concurrent.TimeUnit;
39import java.util.concurrent.locks.ReentrantReadWriteLock;
40
41import net.floodlightcontroller.core.FloodlightContext;
42import net.floodlightcontroller.core.IFloodlightProviderService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080043import net.floodlightcontroller.core.IOFMessageListener;
44import net.floodlightcontroller.core.IOFSwitch;
45import net.floodlightcontroller.core.IOFSwitchListener;
46import net.floodlightcontroller.core.annotations.LogMessageCategory;
47import net.floodlightcontroller.core.annotations.LogMessageDoc;
48import net.floodlightcontroller.core.annotations.LogMessageDocs;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -070049import net.floodlightcontroller.core.internal.OFSwitchImpl;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080050import net.floodlightcontroller.core.module.FloodlightModuleContext;
51import net.floodlightcontroller.core.module.FloodlightModuleException;
52import net.floodlightcontroller.core.module.IFloodlightModule;
53import net.floodlightcontroller.core.module.IFloodlightService;
54import net.floodlightcontroller.core.util.SingletonTask;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080055import net.floodlightcontroller.restserver.IRestApiService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056import net.floodlightcontroller.threadpool.IThreadPoolService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080057import net.floodlightcontroller.util.EventHistory;
58import net.floodlightcontroller.util.EventHistory.EvAction;
Jonathan Hart23701d12014-04-03 10:45:48 -070059import net.onrc.onos.core.linkdiscovery.ILinkDiscovery;
Jonathan Harta99ec672014-04-03 11:30:34 -070060import net.onrc.onos.core.linkdiscovery.ILinkDiscovery.LDUpdate;
61import net.onrc.onos.core.linkdiscovery.ILinkDiscovery.UpdateOperation;
Jonathan Hart23701d12014-04-03 10:45:48 -070062import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryListener;
63import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService;
64import net.onrc.onos.core.linkdiscovery.Link;
65import net.onrc.onos.core.linkdiscovery.LinkInfo;
66import net.onrc.onos.core.linkdiscovery.NodePortTuple;
Jonathan Hart23701d12014-04-03 10:45:48 -070067import net.onrc.onos.core.linkdiscovery.web.LinkDiscoveryWebRoutable;
Jonathan Hart51f6f5b2014-04-03 10:32:10 -070068import net.onrc.onos.core.main.IOnosRemoteSwitch;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070069import net.onrc.onos.core.packet.BSN;
70import net.onrc.onos.core.packet.Ethernet;
71import net.onrc.onos.core.packet.IPv4;
72import net.onrc.onos.core.packet.LLDP;
73import net.onrc.onos.core.packet.LLDPTLV;
74import net.onrc.onos.core.registry.IControllerRegistryService;
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -070075
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080076import org.openflow.protocol.OFMessage;
77import org.openflow.protocol.OFPacketIn;
78import org.openflow.protocol.OFPacketOut;
79import org.openflow.protocol.OFPhysicalPort;
80import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
81import org.openflow.protocol.OFPhysicalPort.OFPortState;
82import org.openflow.protocol.OFPort;
83import org.openflow.protocol.OFPortStatus;
84import org.openflow.protocol.OFPortStatus.OFPortReason;
85import org.openflow.protocol.OFType;
86import org.openflow.protocol.action.OFAction;
87import org.openflow.protocol.action.OFActionOutput;
88import org.openflow.util.HexString;
89import org.slf4j.Logger;
90import 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 Milkey269ffb92014-04-03 14:43:30 -070098 * <p/>
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080099 * This class also handles removing links that are associated to switch ports
100 * that go down, and switches that are disconnected.
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 * <p/>
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800102 * Invariants:
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 * -portLinks and switchLinks will not contain empty Sets outside of
Ray Milkeyb41100a2014-04-10 10:42:15 -0700104 * critical sections.
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 * -portLinks contains LinkTuples where one of the src or dst
Ray Milkeyb41100a2014-04-10 10:42:15 -0700106 * SwitchPortTuple matches the map key.
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 * -switchLinks contains LinkTuples where one of the src or dst
Ray Milkeyb41100a2014-04-10 10:42:15 -0700108 * SwitchPortTuple's id matches the switch id.
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 * -Each LinkTuple will be indexed into switchLinks for both
Ray Milkeyb41100a2014-04-10 10:42:15 -0700110 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -0800112 */
113@LogMessageCategory("Network Topology")
114public class LinkDiscoveryManager
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 implements IOFMessageListener, IOFSwitchListener,
116 ILinkDiscoveryService, IFloodlightModule {
117 protected IFloodlightProviderService controller;
Ray Milkeyec838942014-04-09 11:28:43 -0700118 private static final Logger log = LoggerFactory.getLogger(LinkDiscoveryManager.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800119
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800120 protected IFloodlightProviderService floodlightProvider;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800121 protected IThreadPoolService threadPool;
122 protected IRestApiService restApi;
HIGUCHI Yuta30d03302013-06-14 13:47:36 -0700123 // Registry Service for ONOS
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -0700124 protected IControllerRegistryService registryService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800125
HIGUCHI Yutae0515e52013-06-14 13:00:40 -0700126
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800127 // LLDP and BDDP fields
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800128 private static final byte[] LLDP_STANDARD_DST_MAC_STRING =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800129 HexString.fromHexString("01:80:c2:00:00:0e");
Ray Milkey269ffb92014-04-03 14:43:30 -0700130 private static final long LINK_LOCAL_MASK = 0xfffffffffff0L;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800131 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 Kobayashi71137362013-07-12 15:54:52 -0700134 // 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 Krishnaswamy345ee992012-12-13 20:29:48 -0800136
137
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800138 // Direction TLVs are used to indicate if the LLDPs were sent
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800139 // 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 Milkey45614c52014-04-07 16:30:54 -0700142 private static final byte[] TLV_DIRECTION_VALUE_FORWARD = {0x01};
143 private static final byte[] TLV_DIRECTION_VALUE_REVERSE = {0x02};
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700144 private static final LLDPTLV FORWARD_TLV
Ray Milkey269ffb92014-04-03 14:43:30 -0700145 = new LLDPTLV().
146 setType(TLV_DIRECTION_TYPE).
147 setLength(TLV_DIRECTION_LENGTH).
148 setValue(TLV_DIRECTION_VALUE_FORWARD);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800149
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700150 private static final LLDPTLV REVERSE_TLV
Ray Milkey269ffb92014-04-03 14:43:30 -0700151 = new LLDPTLV().
152 setType(TLV_DIRECTION_TYPE).
153 setLength(TLV_DIRECTION_LENGTH).
154 setValue(TLV_DIRECTION_VALUE_REVERSE);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800155
156 // Link discovery task details.
157 protected SingletonTask discoveryTask;
Ray Milkey2476cac2014-04-08 11:03:21 -0700158 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 Krishnaswamy345ee992012-12-13 20:29:48 -0800161 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 Milkey2476cac2014-04-08 11:03:21 -0700165 protected static final int LLDP_TO_KNOWN_INTERVAL = 20; // LLDP frequency for known links
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800166
167 protected LLDPTLV controllerTLV;
168 protected ReentrantReadWriteLock lock;
169 int lldpTimeCount = 0;
HIGUCHI Yutae0515e52013-06-14 13:00:40 -0700170
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800171 /**
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 Krishnaswamyf962d642013-01-23 19:04:23 -0800178 * Map of remote switches that are not connected to this controller. This
HIGUCHI Yuta30d03302013-06-14 13:47:36 -0700179 * is used to learn remote switches in a distributed controller ONOS.
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800180 */
HIGUCHI Yuta7677a6f2013-06-14 14:13:35 -0700181 protected Map<Long, IOnosRemoteSwitch> remoteSwitches;
HIGUCHI Yuta3d96f652013-06-17 12:07:48 -0700182
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800183 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700184 * Map from link to the most recent time it was verified functioning.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800185 */
186 protected Map<Link, LinkInfo> links;
187
188 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700189 * Map from switch id to a set of all links with it as an endpoint.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800190 */
191 protected Map<Long, Set<Link>> switchLinks;
192
193 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700194 * Map from a id:port to the set of links containing it as an endpoint.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800195 */
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 Milkeyb41100a2014-04-10 10:42:15 -0700206 /**
207 * Topology aware components are called in the order they were added to the
208 * the array.
209 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800210 protected ArrayList<ILinkDiscoveryListener> linkDiscoveryAware;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800211
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700212 protected class LinkUpdate extends LDUpdate {
213
Ray Milkey269ffb92014-04-03 14:43:30 -0700214 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 Hartb0904bf2013-11-26 14:41:11 -0800226 if (log.isTraceEnabled()) {
227 log.trace("Dispatching link discovery update {} {} {} {} {} for {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700228 new Object[]{this.getOperation(),
229 HexString.toHexString(this.getSrc()), this.getSrcPort(),
230 HexString.toHexString(this.getDst()), this.getDstPort(),
231 linkDiscoveryAware});
Jonathan Hartb0904bf2013-11-26 14:41:11 -0800232 }
233 try {
234 for (ILinkDiscoveryListener lda : linkDiscoveryAware) { // order maintained
235 lda.linkDiscoveryUpdate(this);
236 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700237 } catch (Exception e) {
Jonathan Hartb0904bf2013-11-26 14:41:11 -0800238 log.error("Error in link discovery updates loop", e);
239 }
240 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700241 }
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700242 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800243
244 /**
245 * List of ports through which LLDP/BDDPs are not sent.
246 */
247 protected Set<NodePortTuple> suppressLinkDiscovery;
248
Ray Milkey269ffb92014-04-03 14:43:30 -0700249 /**
250 * A list of ports that are quarantined for discovering links through
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800251 * 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 Milkeyb41100a2014-04-10 10:42:15 -0700257 * Quarantine task.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800258 */
259 protected SingletonTask bddpTask;
Ray Milkey2476cac2014-04-08 11:03:21 -0700260 protected static final int BDDP_TASK_INTERVAL = 100; // 100 ms.
261 protected static final int BDDP_TASK_SIZE = 5; // # of ports per iteration
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800262
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 HIGUCHIe8813402014-01-08 13:36:50 -0800269 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800270 * Get the LLDP sending period in seconds.
Ray Milkey269ffb92014-04-03 14:43:30 -0700271 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800272 * @return LLDP sending period in seconds.
273 */
274 public int getLldpFrequency() {
275 return LLDP_TO_KNOWN_INTERVAL;
276 }
277
278 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700279 * Get the LLDP timeout value in seconds.
Ray Milkey269ffb92014-04-03 14:43:30 -0700280 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800281 * @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 HIGUCHIe8813402014-01-08 13:36:50 -0800291 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800292 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 HIGUCHIe8813402014-01-08 13:36:50 -0800300 @Override
Pavlin Radoslavov7d21c0a2014-04-10 10:32:59 -0700301 public void addToSuppressLLDPs(long sw, short port) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800302 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 HIGUCHIe8813402014-01-08 13:36:50 -0800311 @Override
Pavlin Radoslavov7d21c0a2014-04-10 10:32:59 -0700312 public void removeFromSuppressLLDPs(long sw, short port) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800313 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 HIGUCHIe8813402014-01-08 13:36:50 -0800326 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800327 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 HIGUCHIe8813402014-01-08 13:36:50 -0800336
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800337 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 Milkey269ffb92014-04-03 14:43:30 -0700347 lldpClock = (lldpClock + 1) % LLDP_TO_ALL_INTERVAL;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800348
349 if (lldpClock == 0) {
350 log.debug("Sending LLDP out on all ports.");
351 discoverOnAllPorts();
352 }
353 }
354
355
356 /**
Ray Milkey269ffb92014-04-03 14:43:30 -0700357 * Quarantine Ports.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800358 */
359 protected class QuarantineWorker implements Runnable {
360 @Override
361 public void run() {
362 try {
363 processBDDPLists();
Ray Milkey269ffb92014-04-03 14:43:30 -0700364 } catch (Exception e) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800365 log.error("Error in quarantine worker thread", e);
366 } finally {
Ray Milkey269ffb92014-04-03 14:43:30 -0700367 bddpTask.reschedule(BDDP_TASK_INTERVAL,
368 TimeUnit.MILLISECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800369 }
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 Milkey269ffb92014-04-03 14:43:30 -0700377 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800378 * @param npt
379 */
380 protected void addToQuarantineQueue(NodePortTuple npt) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700381 if (quarantineQueue.contains(npt) == false) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800382 quarantineQueue.add(npt);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700383 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800384 }
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 Milkey1aa71f82014-04-08 16:23:24 -0700391 boolean removedSomething;
392
393 do {
394 removedSomething = quarantineQueue.remove(npt);
395 } while (removedSomething);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800396 }
397
398 /**
399 * Add a switch port to maintenance queue.
Ray Milkey269ffb92014-04-03 14:43:30 -0700400 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800401 * @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 Milkeyb29e6262014-04-09 16:02:14 -0700407 if (maintenanceQueue.contains(npt) == false) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800408 maintenanceQueue.add(npt);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700409 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800410 }
411
412 /**
413 * Remove a switch port from maintenance queue.
Ray Milkey269ffb92014-04-03 14:43:30 -0700414 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800415 * @param npt
416 */
417 protected void removeFromMaintenanceQueue(NodePortTuple npt) {
418 // Remove all occurrences of the node port tuple from the queue.
Ray Milkey1aa71f82014-04-08 16:23:24 -0700419 boolean removedSomething;
420 do {
421 removedSomething = maintenanceQueue.remove(npt);
422 } while (removedSomething);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800423 }
424
425 /**
Ray Milkey269ffb92014-04-03 14:43:30 -0700426 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -0800432
433 protected void processBDDPLists() {
434 int count = 0;
435 Set<NodePortTuple> nptList = new HashSet<NodePortTuple>();
436
Ray Milkey269ffb92014-04-03 14:43:30 -0700437 while (count < BDDP_TASK_SIZE && quarantineQueue.peek() != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800438 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 Milkey269ffb92014-04-03 14:43:30 -0700453 for (NodePortTuple npt : nptList) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800454 generateSwitchPortStatusUpdate(npt.getNodeId(), npt.getPortId());
455 }
456 }
457
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800458 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800459 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 Milkeyb29e6262014-04-09 16:02:14 -0700476 if (iofSwitch == null) {
477 return;
478 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800479
480 OFPhysicalPort ofp = iofSwitch.getPort(port);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700481 if (ofp == null) {
482 return;
483 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800484
485 int srcPortState = ofp.getState();
486 boolean portUp = ((srcPortState &
487 OFPortState.OFPPS_STP_MASK.getValue()) !=
488 OFPortState.OFPPS_STP_BLOCK.getValue());
489
Ray Milkeyb29e6262014-04-09 16:02:14 -0700490 if (portUp) {
491 operation = UpdateOperation.PORT_UP;
492 } else {
493 operation = UpdateOperation.PORT_DOWN;
494 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800495
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700496 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, operation));
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800497
498
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700499 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800500 }
501
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800502 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700503 * Send LLDP on known ports.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800504 */
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 Milkey269ffb92014-04-03 14:43:30 -0700511 for (NodePortTuple npt : nptSet) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800512 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 Milkeyb41100a2014-04-10 10:42:15 -0700525 * Learn remote switches when running as a distributed controller ONOS.
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800526 */
527 protected IOFSwitch addRemoteSwitch(long sw, short port) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700528 IOnosRemoteSwitch remotesw = null;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800529
Ray Milkey269ffb92014-04-03 14:43:30 -0700530 // add a switch if we have not seen it before
531 remotesw = remoteSwitches.get(sw);
Jonathan Harte7231052013-01-25 00:01:14 -0800532
Ray Milkey269ffb92014-04-03 14:43:30 -0700533 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 Krishnaswamyf962d642013-01-23 19:04:23 -0800538 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800539
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800540 // add the port if we have not seen it before
Umesh Krishnaswamy68c118c2013-01-25 11:07:09 -0800541 if (remotesw.getPort(port) == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700542 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 Krishnaswamyf962d642013-01-23 19:04:23 -0800549 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800550
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800551 return remotesw;
552 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800553
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800554 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800555 * Send link discovery message out of a given switch port.
556 * The discovery message may be a standard LLDP or a modified
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800557 * LLDP, where the dst mac address is set to :ff.
Ray Milkey269ffb92014-04-03 14:43:30 -0700558 * <p/>
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800559 * TODO: The modified LLDP will updated in the future and may
560 * use a different eth-type.
Ray Milkey269ffb92014-04-03 14:43:30 -0700561 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800562 * @param sw
563 * @param port
Ray Milkey269ffb92014-04-03 14:43:30 -0700564 * @param isStandard indicates standard or modified LLDP
565 * @param isReverse indicates whether the LLDP was sent as a response
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800566 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700567 @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 Krishnaswamy345ee992012-12-13 20:29:48 -0800572 protected void sendDiscoveryMessage(long sw, short port,
Ray Milkey269ffb92014-04-03 14:43:30 -0700573 boolean isStandard,
574 boolean isReverse) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800575
576 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
577 if (iofSwitch == null) {
578 return;
579 }
580
Ray Milkeyb29e6262014-04-09 16:02:14 -0700581 if (port == OFPort.OFPP_LOCAL.getValue()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800582 return;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700583 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800584
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 HIGUCHIe8813402014-01-08 13:36:50 -0800596 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800597 */
598 return;
599 }
600
601 // For fast ports, do not send forward LLDPs or BDDPs.
Ray Milkeyb29e6262014-04-09 16:02:14 -0700602 if (!isReverse && autoPortFastFeature && isFastPort(sw, port)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800603 return;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700604 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800605
606 if (log.isTraceEnabled()) {
607 log.trace("Sending LLDP packet out of swich: {}, port: {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700608 sw, port);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800609 }
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 Milkey269ffb92014-04-03 14:43:30 -0700614 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 Krishnaswamy345ee992012-12-13 20:29:48 -0800617 // OpenFlow OUI - 00-26-E1
Ray Milkey269ffb92014-04-03 14:43:30 -0700618 byte[] dpidTLVValue = new byte[]{0x0, 0x26, (byte) 0xe1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800619 LLDPTLV dpidTLV = new LLDPTLV().setType((byte) 127).setLength((short) dpidTLVValue.length).setValue(dpidTLVValue);
620
621 byte[] dpidArray = new byte[8];
622 ByteBuffer dpidBB = ByteBuffer.wrap(dpidArray);
623 ByteBuffer portBB = ByteBuffer.wrap(portId, 1, 2);
624
625 Long dpid = sw;
626 dpidBB.putLong(dpid);
627 // set the ethernet source mac to last 6 bytes of dpid
628 System.arraycopy(dpidArray, 2, ofpPort.getHardwareAddress(), 0, 6);
629 // set the chassis id's value to last 6 bytes of dpid
630 System.arraycopy(dpidArray, 2, chassisId, 1, 6);
631 // set the optional tlv to the full dpid
632 System.arraycopy(dpidArray, 0, dpidTLVValue, 4, 8);
633
634
635 // set the portId to the outgoing port
636 portBB.putShort(port);
637 if (log.isTraceEnabled()) {
638 log.trace("Sending LLDP out of interface: {}/{}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700639 HexString.toHexString(sw), port);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800640 }
641
642 LLDP lldp = new LLDP();
643 lldp.setChassisId(new LLDPTLV().setType((byte) 1).setLength((short) chassisId.length).setValue(chassisId));
644 lldp.setPortId(new LLDPTLV().setType((byte) 2).setLength((short) portId.length).setValue(portId));
645 lldp.setTtl(new LLDPTLV().setType((byte) 3).setLength((short) ttlValue.length).setValue(ttlValue));
646 lldp.getOptionalTLVList().add(dpidTLV);
647
648 // Add the controller identifier to the TLV value.
649 lldp.getOptionalTLVList().add(controllerTLV);
650 if (isReverse) {
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700651 lldp.getOptionalTLVList().add(REVERSE_TLV);
Ray Milkey269ffb92014-04-03 14:43:30 -0700652 } else {
Ray Milkey5c9f2db2014-04-09 10:31:21 -0700653 lldp.getOptionalTLVList().add(FORWARD_TLV);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800654 }
655
656 Ethernet ethernet;
657 if (isStandard) {
658 ethernet = new Ethernet()
Ray Milkey269ffb92014-04-03 14:43:30 -0700659 .setSourceMACAddress(ofpPort.getHardwareAddress())
660 .setDestinationMACAddress(LLDP_STANDARD_DST_MAC_STRING)
661 .setEtherType(Ethernet.TYPE_LLDP);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800662 ethernet.setPayload(lldp);
663 } else {
664 BSN bsn = new BSN(BSN.BSN_TYPE_BDDP);
665 bsn.setPayload(lldp);
666
667 ethernet = new Ethernet()
Ray Milkey269ffb92014-04-03 14:43:30 -0700668 .setSourceMACAddress(ofpPort.getHardwareAddress())
669 .setDestinationMACAddress(LLDP_BSN_DST_MAC_STRING)
670 .setEtherType(Ethernet.TYPE_BSN);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800671 ethernet.setPayload(bsn);
672 }
673
674
675 // serialize and wrap in a packet out
676 byte[] data = ethernet.serialize();
677 OFPacketOut po = (OFPacketOut) floodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT);
678 po.setBufferId(OFPacketOut.BUFFER_ID_NONE);
679 po.setInPort(OFPort.OFPP_NONE);
680
681 // set actions
682 List<OFAction> actions = new ArrayList<OFAction>();
683 actions.add(new OFActionOutput(port, (short) 0));
684 po.setActions(actions);
685 po.setActionsLength((short) OFActionOutput.MINIMUM_LENGTH);
686
687 // set data
688 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + po.getActionsLength() + data.length);
689 po.setPacketData(data);
690
691 // send
692 try {
693 iofSwitch.write(po, null);
694 iofSwitch.flush();
695 } catch (IOException e) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700696 log.error("Failure sending LLDP out port " + port + " on switch " + iofSwitch.getStringId(), e);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800697 }
698
699 }
700
701 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700702 * Send LLDPs to all switch-ports.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800703 */
704 protected void discoverOnAllPorts() {
705 if (log.isTraceEnabled()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800706 log.trace("Sending LLDP packets out of all the enabled ports on switch");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800707 }
708 Set<Long> switches = floodlightProvider.getSwitches().keySet();
709 // Send standard LLDPs
Ray Milkey269ffb92014-04-03 14:43:30 -0700710 for (long sw : switches) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800711 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700712 if (iofSwitch == null) {
713 continue;
714 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800715 if (iofSwitch.getEnabledPorts() != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700716 for (OFPhysicalPort ofp : iofSwitch.getEnabledPorts()) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700717 if (isLinkDiscoverySuppressed(sw, ofp.getPortNumber())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800718 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700719 }
720 if (autoPortFastFeature && isFastPort(sw, ofp.getPortNumber())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800721 continue;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700722 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800723
724 // sends forward LLDP only non-fastports.
725 sendDiscoveryMessage(sw, ofp.getPortNumber(), true, false);
726
727 // If the switch port is not alreayd in the maintenance
728 // queue, add it.
729 NodePortTuple npt = new NodePortTuple(sw, ofp.getPortNumber());
730 addToMaintenanceQueue(npt);
731 }
732 }
733 }
734 }
735
736 protected void setControllerTLV() {
737 //Setting the controllerTLVValue based on current nano time,
738 //controller's IP address, and the network interface object hash
739 //the corresponding IP address.
740
741 final int prime = 7867;
742 InetAddress localIPAddress = null;
743 NetworkInterface localInterface = null;
744
Ray Milkey269ffb92014-04-03 14:43:30 -0700745 byte[] controllerTLVValue = new byte[]{0, 0, 0, 0, 0, 0, 0, 0}; // 8 byte value.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800746 ByteBuffer bb = ByteBuffer.allocate(10);
747
Ray Milkey269ffb92014-04-03 14:43:30 -0700748 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800749 localIPAddress = java.net.InetAddress.getLocalHost();
750 localInterface = NetworkInterface.getByInetAddress(localIPAddress);
751 } catch (Exception e) {
752 e.printStackTrace();
753 }
754
755 long result = System.nanoTime();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700756 if (localIPAddress != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800757 result = result * prime + IPv4.toIPv4Address(localIPAddress.getHostAddress());
Ray Milkeyb29e6262014-04-09 16:02:14 -0700758 }
759 if (localInterface != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800760 result = result * prime + localInterface.hashCode();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700761 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800762 // set the first 4 bits to 0.
763 result = result & (0x0fffffffffffffffL);
764
765 bb.putLong(result);
766
767 bb.rewind();
768 bb.get(controllerTLVValue, 0, 8);
769
770 this.controllerTLV = new LLDPTLV().setType((byte) 0x0c).setLength((short) controllerTLVValue.length).setValue(controllerTLVValue);
771 }
772
773 @Override
774 public String getName() {
775 return "linkdiscovery";
776 }
777
778 @Override
779 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
780 switch (msg.getType()) {
781 case PACKET_IN:
782 return this.handlePacketIn(sw.getId(), (OFPacketIn) msg, cntx);
783 case PORT_STATUS:
784 return this.handlePortStatus(sw.getId(), (OFPortStatus) msg);
785 default:
786 break;
787 }
788 return Command.CONTINUE;
789 }
790
791 private Command handleLldp(LLDP lldp, long sw, OFPacketIn pi, boolean isStandard, FloodlightContext cntx) {
792 // If LLDP is suppressed on this port, ignore received packet as well
793 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
794 if (iofSwitch == null) {
795 return Command.STOP;
796 }
797
Ray Milkeyb29e6262014-04-09 16:02:14 -0700798 if (isLinkDiscoverySuppressed(sw, pi.getInPort())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800799 return Command.STOP;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700800 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800801
802 // If this is a malformed LLDP, or not from us, exit
Ray Milkeyb29e6262014-04-09 16:02:14 -0700803 if (lldp.getPortId() == null || lldp.getPortId().getLength() != 3) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800804 return Command.CONTINUE;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700805 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800806
807 long myId = ByteBuffer.wrap(controllerTLV.getValue()).getLong();
808 long otherId = 0;
809 boolean myLLDP = false;
810 Boolean isReverse = null;
811
812 ByteBuffer portBB = ByteBuffer.wrap(lldp.getPortId().getValue());
813 portBB.position(1);
814
815 Short remotePort = portBB.getShort();
816 IOFSwitch remoteSwitch = null;
817
818 // Verify this LLDP packet matches what we're looking for
819 for (LLDPTLV lldptlv : lldp.getOptionalTLVList()) {
820 if (lldptlv.getType() == 127 && lldptlv.getLength() == 12 &&
821 lldptlv.getValue()[0] == 0x0 && lldptlv.getValue()[1] == 0x26 &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700822 lldptlv.getValue()[2] == (byte) 0xe1 && lldptlv.getValue()[3] == 0x0) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800823 ByteBuffer dpidBB = ByteBuffer.wrap(lldptlv.getValue());
824 remoteSwitch = floodlightProvider.getSwitches().get(dpidBB.getLong(4));
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800825 if (remoteSwitch == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700826 // Added by ONOS
827 // floodlight LLDP coming from a remote switch connected to a different controller
828 // add it to our cache of unconnected remote switches
829 remoteSwitch = addRemoteSwitch(dpidBB.getLong(4), remotePort);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800830 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700831 } else if (lldptlv.getType() == 12 && lldptlv.getLength() == 8) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800832 otherId = ByteBuffer.wrap(lldptlv.getValue()).getLong();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700833 if (myId == otherId) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800834 myLLDP = true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700835 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800836 } else if (lldptlv.getType() == TLV_DIRECTION_TYPE &&
837 lldptlv.getLength() == TLV_DIRECTION_LENGTH) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700838 if (lldptlv.getValue()[0] == TLV_DIRECTION_VALUE_FORWARD[0]) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800839 isReverse = false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700840 } else if (lldptlv.getValue()[0] == TLV_DIRECTION_VALUE_REVERSE[0]) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800841 isReverse = true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700842 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800843 }
844 }
845
846 if (myLLDP == false) {
847 // This is not the LLDP sent by this controller.
848 // If the LLDP message has multicast bit set, then we need to broadcast
849 // the packet as a regular packet.
850 if (isStandard) {
851 if (log.isTraceEnabled()) {
852 log.trace("Getting standard LLDP from a different controller and quelching it.");
853 }
854 return Command.STOP;
Ray Milkey269ffb92014-04-03 14:43:30 -0700855 } else if (sw <= remoteSwitch.getId()) {
Teru Ub7246af2014-01-13 13:24:38 -0800856 if (log.isTraceEnabled()) {
857 log.trace("Getting BBDP from a different controller. myId {}: remoteId {}", myId, otherId);
858 log.trace("and my controller id is smaller than the other, so quelching it. myPort {}: rPort {}", pi.getInPort(), remotePort);
Ray Milkey269ffb92014-04-03 14:43:30 -0700859 }
860 //XXX ONOS: Fix the BDDP broadcast issue
861 //return Command.CONTINUE;
862 return Command.STOP;
Teru Ub7246af2014-01-13 13:24:38 -0800863 }
864 /*
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800865 else if (myId < otherId) {
866 if (log.isTraceEnabled()) {
867 log.trace("Getting BDDP packets from a different controller" +
868 "and letting it go through normal processing chain.");
869 }
HIGUCHI Yuta30d03302013-06-14 13:47:36 -0700870 //XXX ONOS: Fix the BDDP broadcast issue
Jonathan Hart0b2c76a2013-02-27 17:09:33 -0800871 //return Command.CONTINUE;
872 return Command.STOP;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800873 }
Teru Ub7246af2014-01-13 13:24:38 -0800874 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800875 }
876
877
878 if (remoteSwitch == null) {
879 // Ignore LLDPs not generated by Floodlight, or from a switch that has recently
880 // disconnected, or from a switch connected to another Floodlight instance
881 if (log.isTraceEnabled()) {
882 log.trace("Received LLDP from remote switch not connected to the controller");
883 }
884 return Command.STOP;
885 }
886
887 if (!remoteSwitch.portEnabled(remotePort)) {
888 if (log.isTraceEnabled()) {
889 log.trace("Ignoring link with disabled source port: switch {} port {}", remoteSwitch, remotePort);
890 }
891 return Command.STOP;
892 }
893 if (suppressLinkDiscovery.contains(new NodePortTuple(remoteSwitch.getId(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700894 remotePort))) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800895 if (log.isTraceEnabled()) {
896 log.trace("Ignoring link with suppressed src port: switch {} port {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700897 remoteSwitch, remotePort);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800898 }
899 return Command.STOP;
900 }
901 if (!iofSwitch.portEnabled(pi.getInPort())) {
902 if (log.isTraceEnabled()) {
903 log.trace("Ignoring link with disabled dest port: switch {} port {}", sw, pi.getInPort());
904 }
905 return Command.STOP;
906 }
907
908 OFPhysicalPort physicalPort = remoteSwitch.getPort(remotePort);
909 int srcPortState = (physicalPort != null) ? physicalPort.getState() : 0;
910 physicalPort = iofSwitch.getPort(pi.getInPort());
911 int dstPortState = (physicalPort != null) ? physicalPort.getState() : 0;
912
913 // Store the time of update to this link, and push it out to routingEngine
914 Link lt = new Link(remoteSwitch.getId(), remotePort, iofSwitch.getId(), pi.getInPort());
915
916
917 Long lastLldpTime = null;
918 Long lastBddpTime = null;
919
920 Long firstSeenTime = System.currentTimeMillis();
921
Ray Milkeyb29e6262014-04-09 16:02:14 -0700922 if (isStandard) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800923 lastLldpTime = System.currentTimeMillis();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700924 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800925 lastBddpTime = System.currentTimeMillis();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700926 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800927
928 LinkInfo newLinkInfo =
929 new LinkInfo(firstSeenTime, lastLldpTime, lastBddpTime,
Ray Milkey269ffb92014-04-03 14:43:30 -0700930 srcPortState, dstPortState);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800931
932 addOrUpdateLink(lt, newLinkInfo);
933
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800934 // Check if reverse link exists.
935 // If it doesn't exist and if the forward link was seen
936 // first seen within a small interval, send probe on the
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800937 // reverse link.
938
939 newLinkInfo = links.get(lt);
940 if (newLinkInfo != null && isStandard && isReverse == false) {
941 Link reverseLink = new Link(lt.getDst(), lt.getDstPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700942 lt.getSrc(), lt.getSrcPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800943 LinkInfo reverseInfo = links.get(reverseLink);
944 if (reverseInfo == null) {
945 // the reverse link does not exist.
946 if (newLinkInfo.getFirstSeenTime() > System.currentTimeMillis() - LINK_TIMEOUT) {
947 this.sendDiscoveryMessage(lt.getDst(), lt.getDstPort(), isStandard, true);
948 }
949 }
950 }
951
952 // If the received packet is a BDDP packet, then create a reverse BDDP
953 // link as well.
954 if (!isStandard) {
955 Link reverseLink = new Link(lt.getDst(), lt.getDstPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700956 lt.getSrc(), lt.getSrcPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800957
958 // srcPortState and dstPort state are reversed.
959 LinkInfo reverseInfo =
960 new LinkInfo(firstSeenTime, lastLldpTime, lastBddpTime,
Ray Milkey269ffb92014-04-03 14:43:30 -0700961 dstPortState, srcPortState);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800962
963 addOrUpdateLink(reverseLink, reverseInfo);
964 }
965
966 // Remove the node ports from the quarantine and maintenance queues.
967 NodePortTuple nptSrc = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
968 NodePortTuple nptDst = new NodePortTuple(lt.getDst(), lt.getDstPort());
969 removeFromQuarantineQueue(nptSrc);
970 removeFromMaintenanceQueue(nptSrc);
971 removeFromQuarantineQueue(nptDst);
972 removeFromMaintenanceQueue(nptDst);
973
974 // Consume this message
975 return Command.STOP;
976 }
977
978 protected Command handlePacketIn(long sw, OFPacketIn pi,
979 FloodlightContext cntx) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800980 Ethernet eth =
981 IFloodlightProviderService.bcStore.get(cntx,
Ray Milkey269ffb92014-04-03 14:43:30 -0700982 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800983
Ray Milkey269ffb92014-04-03 14:43:30 -0700984 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800985 BSN bsn = (BSN) eth.getPayload();
Ray Milkeyb29e6262014-04-09 16:02:14 -0700986 if (bsn == null) {
987 return Command.STOP;
988 }
989 if (bsn.getPayload() == null) {
990 return Command.STOP;
991 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800992 // It could be a packet other than BSN LLDP, therefore
993 // continue with the regular processing.
Ray Milkeyb29e6262014-04-09 16:02:14 -0700994 if (bsn.getPayload() instanceof LLDP == false) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800995 return Command.CONTINUE;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700996 }
Ubuntu9cbb4ca2013-02-07 17:19:59 +0000997 return handleLldp((LLDP) bsn.getPayload(), sw, pi, false, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700998 } else if (eth.getEtherType() == Ethernet.TYPE_LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800999 return handleLldp((LLDP) eth.getPayload(), sw, pi, true, cntx);
1000 } else if (eth.getEtherType() < 1500) {
1001 long destMac = eth.getDestinationMAC().toLong();
Ray Milkey269ffb92014-04-03 14:43:30 -07001002 if ((destMac & LINK_LOCAL_MASK) == LINK_LOCAL_VALUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001003 if (log.isTraceEnabled()) {
1004 log.trace("Ignoring packet addressed to 802.1D/Q " +
1005 "reserved address.");
1006 }
1007 return Command.STOP;
1008 }
1009 }
1010
1011 // If packet-in is from a quarantine port, stop processing.
1012 NodePortTuple npt = new NodePortTuple(sw, pi.getInPort());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001013 if (quarantineQueue.contains(npt)) {
1014 return Command.STOP;
1015 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001016
1017 return Command.CONTINUE;
1018 }
1019
1020 protected UpdateOperation getUpdateOperation(int srcPortState,
1021 int dstPortState) {
1022 boolean added =
1023 (((srcPortState &
Ray Milkey269ffb92014-04-03 14:43:30 -07001024 OFPortState.OFPPS_STP_MASK.getValue()) !=
1025 OFPortState.OFPPS_STP_BLOCK.getValue()) &&
1026 ((dstPortState &
1027 OFPortState.OFPPS_STP_MASK.getValue()) !=
1028 OFPortState.OFPPS_STP_BLOCK.getValue()));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001029
Ray Milkeyb29e6262014-04-09 16:02:14 -07001030 if (added) {
1031 return UpdateOperation.LINK_UPDATED;
1032 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001033 return UpdateOperation.LINK_REMOVED;
1034 }
1035
1036
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001037 protected UpdateOperation getUpdateOperation(int srcPortState) {
1038 boolean portUp = ((srcPortState &
1039 OFPortState.OFPPS_STP_MASK.getValue()) !=
1040 OFPortState.OFPPS_STP_BLOCK.getValue());
1041
Ray Milkeyb29e6262014-04-09 16:02:14 -07001042 if (portUp) {
1043 return UpdateOperation.PORT_UP;
1044 } else {
1045 return UpdateOperation.PORT_DOWN;
1046 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001047 }
1048
1049 protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) {
1050
1051 NodePortTuple srcNpt, dstNpt;
1052 boolean linkChanged = false;
1053
1054 lock.writeLock().lock();
1055 try {
1056 // put the new info. if an old info exists, it will be returned.
1057 LinkInfo oldInfo = links.put(lt, newInfo);
1058 if (oldInfo != null &&
Ray Milkeyb29e6262014-04-09 16:02:14 -07001059 oldInfo.getFirstSeenTime() < newInfo.getFirstSeenTime()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001060 newInfo.setFirstSeenTime(oldInfo.getFirstSeenTime());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001061 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001062
1063 if (log.isTraceEnabled()) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001064 log.trace("addOrUpdateLink: {} {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001065 lt,
1066 (newInfo.getMulticastValidTime() != null) ? "multicast" : "unicast");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001067 }
1068
1069 UpdateOperation updateOperation = null;
1070 linkChanged = false;
1071
1072 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1073 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1074
1075 if (oldInfo == null) {
1076 // index it by switch source
Ray Milkeyb29e6262014-04-09 16:02:14 -07001077 if (!switchLinks.containsKey(lt.getSrc())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001078 switchLinks.put(lt.getSrc(), new HashSet<Link>());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001079 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001080 switchLinks.get(lt.getSrc()).add(lt);
1081
1082 // index it by switch dest
Ray Milkeyb29e6262014-04-09 16:02:14 -07001083 if (!switchLinks.containsKey(lt.getDst())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001084 switchLinks.put(lt.getDst(), new HashSet<Link>());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001085 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001086 switchLinks.get(lt.getDst()).add(lt);
1087
1088 // index both ends by switch:port
Ray Milkeyb29e6262014-04-09 16:02:14 -07001089 if (!portLinks.containsKey(srcNpt)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001090 portLinks.put(srcNpt, new HashSet<Link>());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001091 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001092 portLinks.get(srcNpt).add(lt);
1093
Ray Milkeyb29e6262014-04-09 16:02:14 -07001094 if (!portLinks.containsKey(dstNpt)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001095 portLinks.put(dstNpt, new HashSet<Link>());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001096 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001097 portLinks.get(dstNpt).add(lt);
1098
1099 // Add to portNOFLinks if the unicast valid time is null
Ray Milkeyb29e6262014-04-09 16:02:14 -07001100 if (newInfo.getUnicastValidTime() == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001101 addLinkToBroadcastDomain(lt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001102 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001103
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001104 // ONOS: Distinguish added event separately from updated event
Pankaj Berdea41016d2013-06-10 21:18:18 -07001105 updateOperation = UpdateOperation.LINK_ADDED;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001106 linkChanged = true;
1107
1108 // Add to event history
1109 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001110 lt.getDst(),
1111 lt.getSrcPort(),
1112 lt.getDstPort(),
1113 newInfo.getSrcPortState(), newInfo.getDstPortState(),
1114 getLinkType(lt, newInfo),
1115 EvAction.LINK_ADDED, "LLDP Recvd");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001116 } else {
1117 // Since the link info is already there, we need to
1118 // update the right fields.
1119 if (newInfo.getUnicastValidTime() == null) {
1120 // This is due to a multicast LLDP, so copy the old unicast
1121 // value.
1122 if (oldInfo.getUnicastValidTime() != null) {
1123 newInfo.setUnicastValidTime(oldInfo.getUnicastValidTime());
1124 }
1125 } else if (newInfo.getMulticastValidTime() == null) {
1126 // This is due to a unicast LLDP, so copy the old multicast
1127 // value.
1128 if (oldInfo.getMulticastValidTime() != null) {
1129 newInfo.setMulticastValidTime(oldInfo.getMulticastValidTime());
1130 }
1131 }
1132
1133 Long oldTime = oldInfo.getUnicastValidTime();
1134 Long newTime = newInfo.getUnicastValidTime();
1135 // the link has changed its state between openflow and non-openflow
1136 // if the unicastValidTimes are null or not null
1137 if (oldTime != null & newTime == null) {
1138 // openflow -> non-openflow transition
1139 // we need to add the link tuple to the portNOFLinks
1140 addLinkToBroadcastDomain(lt);
1141 linkChanged = true;
1142 } else if (oldTime == null & newTime != null) {
1143 // non-openflow -> openflow transition
1144 // we need to remove the link from the portNOFLinks
1145 removeLinkFromBroadcastDomain(lt);
1146 linkChanged = true;
1147 }
1148
1149 // Only update the port states if they've changed
1150 if (newInfo.getSrcPortState().intValue() !=
1151 oldInfo.getSrcPortState().intValue() ||
1152 newInfo.getDstPortState().intValue() !=
Ray Milkeyb29e6262014-04-09 16:02:14 -07001153 oldInfo.getDstPortState().intValue()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001154 linkChanged = true;
Ray Milkeyb29e6262014-04-09 16:02:14 -07001155 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001156
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001157 if (linkChanged) {
1158 updateOperation = getUpdateOperation(newInfo.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001159 newInfo.getDstPortState());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001160 if (log.isTraceEnabled()) {
1161 log.trace("Updated link {}", lt);
1162 }
1163 // Add to event history
1164 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001165 lt.getDst(),
1166 lt.getSrcPort(),
1167 lt.getDstPort(),
1168 newInfo.getSrcPortState(), newInfo.getDstPortState(),
1169 getLinkType(lt, newInfo),
1170 EvAction.LINK_PORT_STATE_UPDATED,
1171 "LLDP Recvd");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001172 }
1173 }
1174
1175 if (linkChanged) {
1176 // find out if the link was added or removed here.
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001177 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001178 lt.getDst(), lt.getDstPort(),
1179 getLinkType(lt, newInfo),
1180 updateOperation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001181 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001182 }
1183 } finally {
1184 lock.writeLock().unlock();
1185 }
1186
1187 return linkChanged;
1188 }
1189
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001190 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001191 public Map<Long, Set<Link>> getSwitchLinks() {
1192 return this.switchLinks;
1193 }
1194
1195 /**
1196 * Removes links from memory and storage.
Ray Milkey269ffb92014-04-03 14:43:30 -07001197 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001198 * @param links The List of @LinkTuple to delete.
1199 */
1200 protected void deleteLinks(List<Link> links, String reason) {
1201 NodePortTuple srcNpt, dstNpt;
1202
1203 lock.writeLock().lock();
1204 try {
1205 for (Link lt : links) {
1206 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
Ray Milkey269ffb92014-04-03 14:43:30 -07001207 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001208
1209 switchLinks.get(lt.getSrc()).remove(lt);
1210 switchLinks.get(lt.getDst()).remove(lt);
1211 if (switchLinks.containsKey(lt.getSrc()) &&
Ray Milkeyb29e6262014-04-09 16:02:14 -07001212 switchLinks.get(lt.getSrc()).isEmpty()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001213 this.switchLinks.remove(lt.getSrc());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001214 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001215 if (this.switchLinks.containsKey(lt.getDst()) &&
Ray Milkeyb29e6262014-04-09 16:02:14 -07001216 this.switchLinks.get(lt.getDst()).isEmpty()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001217 this.switchLinks.remove(lt.getDst());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001218 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001219
1220 if (this.portLinks.get(srcNpt) != null) {
1221 this.portLinks.get(srcNpt).remove(lt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001222 if (this.portLinks.get(srcNpt).isEmpty()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001223 this.portLinks.remove(srcNpt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001224 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001225 }
1226 if (this.portLinks.get(dstNpt) != null) {
1227 this.portLinks.get(dstNpt).remove(lt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001228 if (this.portLinks.get(dstNpt).isEmpty()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001229 this.portLinks.remove(dstNpt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001230 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001231 }
1232
1233 LinkInfo info = this.links.remove(lt);
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001234 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001235 lt.getDst(), lt.getDstPort(),
1236 getLinkType(lt, info),
1237 UpdateOperation.LINK_REMOVED));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001238 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001239
1240 // Update Event History
1241 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001242 lt.getDst(),
1243 lt.getSrcPort(),
1244 lt.getDstPort(),
1245 0, 0, // Port states
1246 ILinkDiscovery.LinkType.INVALID_LINK,
1247 EvAction.LINK_DELETED, reason);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001248
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001249 // TODO Whenever link is removed, it has to checked if
1250 // the switchports must be added to quarantine.
1251
1252 if (log.isTraceEnabled()) {
1253 log.trace("Deleted link {}", lt);
1254 }
1255 }
1256 } finally {
1257 lock.writeLock().unlock();
1258 }
1259 }
1260
1261 /**
1262 * Handles an OFPortStatus message from a switch. We will add or
1263 * delete LinkTupes as well re-compute the topology if needed.
Ray Milkey269ffb92014-04-03 14:43:30 -07001264 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001265 * @param sw The IOFSwitch that sent the port status message
1266 * @param ps The OFPortStatus message
1267 * @return The Command to continue or stop after we process this message
1268 */
1269 protected Command handlePortStatus(long sw, OFPortStatus ps) {
1270
1271 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001272 if (iofSwitch == null) {
1273 return Command.CONTINUE;
1274 }
HIGUCHI Yutaa89b2842013-06-17 13:54:57 -07001275
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001276 // ONOS: If we do not control this switch, then we should not process its port status messages
Ray Milkeyb29e6262014-04-09 16:02:14 -07001277 if (!registryService.hasControl(iofSwitch.getId())) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001278 return Command.CONTINUE;
Ray Milkeyb29e6262014-04-09 16:02:14 -07001279 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001280
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001281 if (log.isTraceEnabled()) {
1282 log.trace("handlePortStatus: Switch {} port #{} reason {}; " +
1283 "config is {} state is {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001284 new Object[]{iofSwitch.getStringId(),
1285 ps.getDesc().getPortNumber(),
1286 ps.getReason(),
1287 ps.getDesc().getConfig(),
1288 ps.getDesc().getState()});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001289 }
1290
1291 short port = ps.getDesc().getPortNumber();
1292 NodePortTuple npt = new NodePortTuple(sw, port);
Ray Milkey269ffb92014-04-03 14:43:30 -07001293 boolean linkDeleted = false;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001294 boolean linkInfoChanged = false;
1295
1296 lock.writeLock().lock();
1297 try {
1298 // if ps is a delete, or a modify where the port is down or
1299 // configured down
Ray Milkey269ffb92014-04-03 14:43:30 -07001300 if ((byte) OFPortReason.OFPPR_DELETE.ordinal() == ps.getReason() ||
1301 ((byte) OFPortReason.OFPPR_MODIFY.ordinal() ==
1302 ps.getReason() && !portEnabled(ps.getDesc()))) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001303 deleteLinksOnPort(npt, "Port Status Changed");
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001304 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, UpdateOperation.PORT_DOWN));
1305 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001306 linkDeleted = true;
Ray Milkey269ffb92014-04-03 14:43:30 -07001307 } else if (ps.getReason() ==
1308 (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001309 // If ps is a port modification and the port state has changed
1310 // that affects links in the topology
1311
1312 if (this.portLinks.containsKey(npt)) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001313 for (Link lt : this.portLinks.get(npt)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001314 LinkInfo linkInfo = links.get(lt);
Ray Milkey269ffb92014-04-03 14:43:30 -07001315 assert (linkInfo != null);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001316 Integer updatedSrcPortState = null;
1317 Integer updatedDstPortState = null;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001318 if (lt.getSrc() == npt.getNodeId() &&
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001319 lt.getSrcPort() == npt.getPortId() &&
1320 (linkInfo.getSrcPortState() !=
Ray Milkey269ffb92014-04-03 14:43:30 -07001321 ps.getDesc().getState())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001322 updatedSrcPortState = ps.getDesc().getState();
1323 linkInfo.setSrcPortState(updatedSrcPortState);
1324 }
1325 if (lt.getDst() == npt.getNodeId() &&
1326 lt.getDstPort() == npt.getPortId() &&
1327 (linkInfo.getDstPortState() !=
Ray Milkey269ffb92014-04-03 14:43:30 -07001328 ps.getDesc().getState())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001329 updatedDstPortState = ps.getDesc().getState();
1330 linkInfo.setDstPortState(updatedDstPortState);
1331 }
1332 if ((updatedSrcPortState != null) ||
1333 (updatedDstPortState != null)) {
1334 // The link is already known to link discovery
1335 // manager and the status has changed, therefore
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001336 // send an LinkUpdate.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001337 UpdateOperation operation =
1338 getUpdateOperation(linkInfo.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001339 linkInfo.getDstPortState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001340 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001341 lt.getDst(), lt.getDstPort(),
1342 getLinkType(lt, linkInfo),
1343 operation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001344 controller.publishUpdate(update);
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001345
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001346 linkInfoChanged = true;
1347 }
1348 }
1349 }
1350
1351 UpdateOperation operation =
1352 getUpdateOperation(ps.getDesc().getState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001353 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, operation));
1354 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001355 }
1356
Ray Milkey269ffb92014-04-03 14:43:30 -07001357 if (!linkDeleted && !linkInfoChanged) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001358 if (log.isTraceEnabled()) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001359 log.trace("handlePortStatus: Switch {} port #{} reason {};" +
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001360 " no links to update/remove",
Ray Milkey269ffb92014-04-03 14:43:30 -07001361 new Object[]{HexString.toHexString(sw),
1362 ps.getDesc().getPortNumber(),
1363 ps.getReason()});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001364 }
1365 }
1366 } finally {
1367 lock.writeLock().unlock();
1368 }
1369
1370 if (!linkDeleted) {
1371 // Send LLDP right away when port state is changed for faster
1372 // cluster-merge. If it is a link delete then there is not need
1373 // to send the LLDPs right away and instead we wait for the LLDPs
1374 // to be sent on the timer as it is normally done
1375 // do it outside the write-lock
1376 // sendLLDPTask.reschedule(1000, TimeUnit.MILLISECONDS);
1377 processNewPort(npt.getNodeId(), npt.getPortId());
1378 }
1379 return Command.CONTINUE;
1380 }
1381
1382 /**
1383 * Process a new port.
1384 * If link discovery is disabled on the port, then do nothing.
1385 * If autoportfast feature is enabled and the port is a fast port, then
1386 * do nothing.
1387 * Otherwise, send LLDP message. Add the port to quarantine.
Ray Milkey269ffb92014-04-03 14:43:30 -07001388 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001389 * @param sw
1390 * @param p
1391 */
1392 private void processNewPort(long sw, short p) {
1393 if (isLinkDiscoverySuppressed(sw, p)) {
1394 // Do nothing as link discovery is suppressed.
Ray Milkey1aa71f82014-04-08 16:23:24 -07001395 return;
Ray Milkey269ffb92014-04-03 14:43:30 -07001396 } else if (autoPortFastFeature && isFastPort(sw, p)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001397 // Do nothing as the port is a fast port.
Ray Milkey1aa71f82014-04-08 16:23:24 -07001398 return;
Ray Milkey269ffb92014-04-03 14:43:30 -07001399 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001400 NodePortTuple npt = new NodePortTuple(sw, p);
1401 discover(sw, p);
1402 // if it is not a fast port, add it to quarantine.
1403 if (!isFastPort(sw, p)) {
1404 addToQuarantineQueue(npt);
1405 } else {
1406 // Add to maintenance queue to ensure that BDDP packets
1407 // are sent out.
1408 addToMaintenanceQueue(npt);
1409 }
1410 }
1411 }
1412
1413 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -07001414 * We send out LLDP messages when a switch is added to discover the topology.
Ray Milkey269ffb92014-04-03 14:43:30 -07001415 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001416 * @param sw The IOFSwitch that connected to the controller
1417 */
1418 @Override
1419 public void addedSwitch(IOFSwitch sw) {
1420
1421 if (sw.getEnabledPorts() != null) {
1422 for (Short p : sw.getEnabledPortNumbers()) {
1423 processNewPort(sw.getId(), p);
1424 }
1425 }
1426 // Update event history
1427 evHistTopoSwitch(sw, EvAction.SWITCH_CONNECTED, "None");
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001428 LinkUpdate update = new LinkUpdate(new LDUpdate(sw.getId(), null,
Ray Milkey269ffb92014-04-03 14:43:30 -07001429 UpdateOperation.SWITCH_UPDATED));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001430 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001431 }
1432
1433 /**
1434 * When a switch disconnects we remove any links from our map and notify.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001435 */
1436 @Override
1437 public void removedSwitch(IOFSwitch iofSwitch) {
1438 // Update event history
1439 long sw = iofSwitch.getId();
1440 evHistTopoSwitch(iofSwitch, EvAction.SWITCH_DISCONNECTED, "None");
1441 List<Link> eraseList = new ArrayList<Link>();
1442 lock.writeLock().lock();
1443 try {
1444 if (switchLinks.containsKey(sw)) {
1445 if (log.isTraceEnabled()) {
1446 log.trace("Handle switchRemoved. Switch {}; removing links {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001447 HexString.toHexString(sw), switchLinks.get(sw));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001448 }
1449 // add all tuples with an endpoint on this switch to erase list
1450 eraseList.addAll(switchLinks.get(sw));
HIGUCHI Yutaa89b2842013-06-17 13:54:57 -07001451 deleteLinks(eraseList, "Switch Removed");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001452
1453 // Send a switch removed update
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001454 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, null, UpdateOperation.SWITCH_REMOVED));
1455 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001456 }
1457 } finally {
1458 lock.writeLock().unlock();
1459 }
1460 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001461
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001462 /**
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001463 * We don't react the port changed notifications here. we listen for
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001464 * OFPortStatus messages directly. Might consider using this notifier
1465 * instead
1466 */
1467 @Override
1468 public void switchPortChanged(Long switchId) {
1469 // no-op
1470 }
1471
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001472 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001473 * Delete links incident on a given switch port.
Ray Milkey269ffb92014-04-03 14:43:30 -07001474 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001475 * @param npt
1476 * @param reason
1477 */
1478 protected void deleteLinksOnPort(NodePortTuple npt, String reason) {
1479 List<Link> eraseList = new ArrayList<Link>();
1480 if (this.portLinks.containsKey(npt)) {
1481 if (log.isTraceEnabled()) {
1482 log.trace("handlePortStatus: Switch {} port #{} " +
1483 "removing links {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001484 new Object[]{HexString.toHexString(npt.getNodeId()),
1485 npt.getPortId(),
1486 this.portLinks.get(npt)});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001487 }
1488 eraseList.addAll(this.portLinks.get(npt));
1489 deleteLinks(eraseList, reason);
1490 }
1491 }
1492
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001493 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001494 * Iterates through the list of links and deletes if the
1495 * last discovery message reception time exceeds timeout values.
1496 */
1497 protected void timeoutLinks() {
1498 List<Link> eraseList = new ArrayList<Link>();
1499 Long curTime = System.currentTimeMillis();
1500 boolean linkChanged = false;
1501
1502 // reentrant required here because deleteLink also write locks
1503 lock.writeLock().lock();
1504 try {
1505 Iterator<Entry<Link, LinkInfo>> it =
1506 this.links.entrySet().iterator();
1507 while (it.hasNext()) {
1508 Entry<Link, LinkInfo> entry = it.next();
1509 Link lt = entry.getKey();
1510 LinkInfo info = entry.getValue();
1511
1512 // Timeout the unicast and multicast LLDP valid times
1513 // independently.
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001514 if ((info.getUnicastValidTime() != null) &&
Ray Milkey269ffb92014-04-03 14:43:30 -07001515 (info.getUnicastValidTime() + (this.LINK_TIMEOUT * 1000) < curTime)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001516 info.setUnicastValidTime(null);
1517
Ray Milkeyb29e6262014-04-09 16:02:14 -07001518 if (info.getMulticastValidTime() != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001519 addLinkToBroadcastDomain(lt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001520 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001521 // Note that even if mTime becomes null later on,
1522 // the link would be deleted, which would trigger updateClusters().
1523 linkChanged = true;
1524 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001525 if ((info.getMulticastValidTime() != null) &&
1526 (info.getMulticastValidTime() + (this.LINK_TIMEOUT * 1000) < curTime)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001527 info.setMulticastValidTime(null);
1528 // if uTime is not null, then link will remain as openflow
1529 // link. If uTime is null, it will be deleted. So, we
1530 // don't care about linkChanged flag here.
1531 removeLinkFromBroadcastDomain(lt);
1532 linkChanged = true;
1533 }
1534 // Add to the erase list only if the unicast
1535 // time is null.
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001536 if (info.getUnicastValidTime() == null &&
Ray Milkey269ffb92014-04-03 14:43:30 -07001537 info.getMulticastValidTime() == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001538 eraseList.add(entry.getKey());
1539 } else if (linkChanged) {
1540 UpdateOperation operation;
1541 operation = getUpdateOperation(info.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001542 info.getDstPortState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001543 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001544 lt.getDst(), lt.getDstPort(),
1545 getLinkType(lt, info),
1546 operation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001547 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001548 }
1549 }
1550
1551 // if any link was deleted or any link was changed.
1552 if ((eraseList.size() > 0) || linkChanged) {
1553 deleteLinks(eraseList, "LLDP timeout");
1554 }
1555 } finally {
1556 lock.writeLock().unlock();
1557 }
1558 }
1559
1560 private boolean portEnabled(OFPhysicalPort port) {
Ray Milkeyb29e6262014-04-09 16:02:14 -07001561 if (port == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001562 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -07001563 }
1564 if ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001565 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -07001566 }
1567 if ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001568 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -07001569 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001570 // Port STP state doesn't work with multiple VLANs, so ignore it for now
1571 // if ((port.getState() & OFPortState.OFPPS_STP_MASK.getValue()) == OFPortState.OFPPS_STP_BLOCK.getValue())
1572 // return false;
1573 return true;
1574 }
1575
1576 public Map<NodePortTuple, Set<Link>> getPortBroadcastDomainLinks() {
1577 return portBroadcastDomainLinks;
1578 }
1579
1580 @Override
1581 public Map<Link, LinkInfo> getLinks() {
1582 lock.readLock().lock();
1583 Map<Link, LinkInfo> result;
1584 try {
1585 result = new HashMap<Link, LinkInfo>(links);
1586 } finally {
1587 lock.readLock().unlock();
1588 }
1589 return result;
1590 }
1591
1592 protected void addLinkToBroadcastDomain(Link lt) {
1593
1594 NodePortTuple srcNpt, dstNpt;
1595 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1596 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1597
Ray Milkeyb29e6262014-04-09 16:02:14 -07001598 if (!portBroadcastDomainLinks.containsKey(srcNpt)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001599 portBroadcastDomainLinks.put(srcNpt, new HashSet<Link>());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001600 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001601 portBroadcastDomainLinks.get(srcNpt).add(lt);
1602
Ray Milkeyb29e6262014-04-09 16:02:14 -07001603 if (!portBroadcastDomainLinks.containsKey(dstNpt)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001604 portBroadcastDomainLinks.put(dstNpt, new HashSet<Link>());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001605 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001606 portBroadcastDomainLinks.get(dstNpt).add(lt);
1607 }
1608
1609 protected void removeLinkFromBroadcastDomain(Link lt) {
1610
1611 NodePortTuple srcNpt, dstNpt;
1612 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1613 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1614
1615 if (portBroadcastDomainLinks.containsKey(srcNpt)) {
1616 portBroadcastDomainLinks.get(srcNpt).remove(lt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001617 if (portBroadcastDomainLinks.get(srcNpt).isEmpty()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001618 portBroadcastDomainLinks.remove(srcNpt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001619 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001620 }
1621
1622 if (portBroadcastDomainLinks.containsKey(dstNpt)) {
1623 portBroadcastDomainLinks.get(dstNpt).remove(lt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001624 if (portBroadcastDomainLinks.get(dstNpt).isEmpty()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001625 portBroadcastDomainLinks.remove(dstNpt);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001626 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001627 }
1628 }
1629
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001630 @Override
1631 public void addListener(ILinkDiscoveryListener listener) {
1632 linkDiscoveryAware.add(listener);
1633 }
1634
1635 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -07001636 * Register a link discovery aware component.
Ray Milkey269ffb92014-04-03 14:43:30 -07001637 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001638 * @param linkDiscoveryAwareComponent
1639 */
1640 public void addLinkDiscoveryAware(ILinkDiscoveryListener linkDiscoveryAwareComponent) {
1641 // TODO make this a copy on write set or lock it somehow
1642 this.linkDiscoveryAware.add(linkDiscoveryAwareComponent);
1643 }
1644
1645 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -07001646 * Deregister a link discovery aware component.
Ray Milkey269ffb92014-04-03 14:43:30 -07001647 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001648 * @param linkDiscoveryAwareComponent
1649 */
1650 public void removeLinkDiscoveryAware(ILinkDiscoveryListener linkDiscoveryAwareComponent) {
1651 // TODO make this a copy on write set or lock it somehow
1652 this.linkDiscoveryAware.remove(linkDiscoveryAwareComponent);
1653 }
1654
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001655 @Override
1656 public boolean isCallbackOrderingPrereq(OFType type, String name) {
1657 return false;
1658 }
1659
1660 @Override
1661 public boolean isCallbackOrderingPostreq(OFType type, String name) {
1662 return false;
1663 }
1664
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001665 // IFloodlightModule classes
1666
1667 @Override
1668 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001669 Collection<Class<? extends IFloodlightService>> l =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001670 new ArrayList<Class<? extends IFloodlightService>>();
1671 l.add(ILinkDiscoveryService.class);
1672 //l.add(ITopologyService.class);
1673 return l;
1674 }
1675
1676 @Override
1677 public Map<Class<? extends IFloodlightService>, IFloodlightService>
1678 getServiceImpls() {
1679 Map<Class<? extends IFloodlightService>,
Ray Milkey269ffb92014-04-03 14:43:30 -07001680 IFloodlightService> m =
1681 new HashMap<Class<? extends IFloodlightService>,
1682 IFloodlightService>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001683 // We are the class that implements the service
1684 m.put(ILinkDiscoveryService.class, this);
1685 return m;
1686 }
1687
1688 @Override
1689 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001690 Collection<Class<? extends IFloodlightService>> l =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001691 new ArrayList<Class<? extends IFloodlightService>>();
1692 l.add(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001693 l.add(IThreadPoolService.class);
1694 l.add(IRestApiService.class);
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001695 // Added by ONOS
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001696 l.add(IControllerRegistryService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001697 return l;
1698 }
1699
1700 @Override
1701 public void init(FloodlightModuleContext context)
1702 throws FloodlightModuleException {
1703 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001704 threadPool = context.getServiceImpl(IThreadPoolService.class);
1705 restApi = context.getServiceImpl(IRestApiService.class);
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001706 // Added by ONOS
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001707 registryService = context.getServiceImpl(IControllerRegistryService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001708
1709 // Set the autoportfast feature to false.
1710 this.autoPortFastFeature = false;
1711
1712 // We create this here because there is no ordering guarantee
1713 this.linkDiscoveryAware = new ArrayList<ILinkDiscoveryListener>();
1714 this.lock = new ReentrantReadWriteLock();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001715 this.links = new HashMap<Link, LinkInfo>();
1716 this.portLinks = new HashMap<NodePortTuple, Set<Link>>();
1717 this.suppressLinkDiscovery =
1718 Collections.synchronizedSet(new HashSet<NodePortTuple>());
1719 this.portBroadcastDomainLinks = new HashMap<NodePortTuple, Set<Link>>();
1720 this.switchLinks = new HashMap<Long, Set<Link>>();
1721 this.quarantineQueue = new LinkedBlockingQueue<NodePortTuple>();
1722 this.maintenanceQueue = new LinkedBlockingQueue<NodePortTuple>();
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001723 // Added by ONOS
HIGUCHI Yuta7677a6f2013-06-14 14:13:35 -07001724 this.remoteSwitches = new HashMap<Long, IOnosRemoteSwitch>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001725
1726 this.evHistTopologySwitch =
1727 new EventHistory<EventHistoryTopologySwitch>("Topology: Switch");
1728 this.evHistTopologyLink =
1729 new EventHistory<EventHistoryTopologyLink>("Topology: Link");
1730 this.evHistTopologyCluster =
1731 new EventHistory<EventHistoryTopologyCluster>("Topology: Cluster");
1732 }
1733
1734 @Override
1735 @LogMessageDocs({
Ray Milkey269ffb92014-04-03 14:43:30 -07001736 @LogMessageDoc(level = "ERROR",
1737 message = "No storage source found.",
1738 explanation = "Storage source was not initialized; cannot initialize " +
1739 "link discovery.",
1740 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1741 @LogMessageDoc(level = "ERROR",
1742 message = "Error in installing listener for " +
1743 "switch config table {table}",
1744 explanation = "Failed to install storage notification for the " +
1745 "switch config table",
1746 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1747 @LogMessageDoc(level = "ERROR",
1748 message = "No storage source found.",
1749 explanation = "Storage source was not initialized; cannot initialize " +
1750 "link discovery.",
1751 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1752 @LogMessageDoc(level = "ERROR",
1753 message = "Exception in LLDP send timer.",
1754 explanation = "An unknown error occured while sending LLDP " +
1755 "messages to switches.",
1756 recommendation = LogMessageDoc.CHECK_SWITCH)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001757 })
1758 public void startUp(FloodlightModuleContext context) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001759 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001760 controller =
1761 context.getServiceImpl(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001762
1763 // To be started by the first switch connection
1764 discoveryTask = new SingletonTask(ses, new Runnable() {
1765 @Override
1766 public void run() {
1767 try {
1768 discoverLinks();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001769 } catch (Exception e) {
1770 log.error("Exception in LLDP send timer.", e);
1771 } finally {
1772 if (!shuttingDown) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001773 // Always reschedule link discovery if we're not
1774 // shutting down (no chance of SLAVE role now)
Jonathan Hartec4f14e2013-12-12 10:46:38 -08001775 log.trace("Rescheduling discovery task");
1776 discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL,
Ray Milkey269ffb92014-04-03 14:43:30 -07001777 TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001778 }
1779 }
1780 }
1781 });
1782
Jonathan Hartec4f14e2013-12-12 10:46:38 -08001783 // Always reschedule link discovery as we are never in SLAVE role now
1784 discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL, TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001785
1786 // Setup the BDDP task. It is invoked whenever switch port tuples
1787 // are added to the quarantine list.
1788 bddpTask = new SingletonTask(ses, new QuarantineWorker());
1789 bddpTask.reschedule(BDDP_TASK_INTERVAL, TimeUnit.MILLISECONDS);
1790
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001791
1792 // Register for the OpenFlow messages we want to receive
1793 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
1794 floodlightProvider.addOFMessageListener(OFType.PORT_STATUS, this);
1795 // Register for switch updates
1796 floodlightProvider.addOFSwitchListener(this);
Ray Milkeyb29e6262014-04-09 16:02:14 -07001797 if (restApi != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001798 restApi.addRestletRoutable(new LinkDiscoveryWebRoutable());
Ray Milkeyb29e6262014-04-09 16:02:14 -07001799 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001800 setControllerTLV();
1801 }
1802
1803 // ****************************************************
1804 // Topology Manager's Event History members and methods
1805 // ****************************************************
1806
1807 // Topology Manager event history
Ray Milkey269ffb92014-04-03 14:43:30 -07001808 public EventHistory<EventHistoryTopologySwitch> evHistTopologySwitch;
1809 public EventHistory<EventHistoryTopologyLink> evHistTopologyLink;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001810 public EventHistory<EventHistoryTopologyCluster> evHistTopologyCluster;
Ray Milkey269ffb92014-04-03 14:43:30 -07001811 public EventHistoryTopologySwitch evTopoSwitch;
1812 public EventHistoryTopologyLink evTopoLink;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001813 public EventHistoryTopologyCluster evTopoCluster;
1814
1815 // Switch Added/Deleted
1816 private void evHistTopoSwitch(IOFSwitch sw, EvAction actn, String reason) {
1817 if (evTopoSwitch == null) {
1818 evTopoSwitch = new EventHistoryTopologySwitch();
1819 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001820 evTopoSwitch.dpid = sw.getId();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001821 if ((sw.getChannel() != null) &&
1822 (SocketAddress.class.isInstance(
Ray Milkey269ffb92014-04-03 14:43:30 -07001823 sw.getChannel().getRemoteAddress()))) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001824 evTopoSwitch.ipv4Addr =
Ray Milkey269ffb92014-04-03 14:43:30 -07001825 IPv4.toIPv4Address(((InetSocketAddress) (sw.getChannel().
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001826 getRemoteAddress())).getAddress().getAddress());
Ray Milkey269ffb92014-04-03 14:43:30 -07001827 evTopoSwitch.l4Port =
1828 ((InetSocketAddress) (sw.getChannel().
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001829 getRemoteAddress())).getPort();
1830 } else {
1831 evTopoSwitch.ipv4Addr = 0;
1832 evTopoSwitch.l4Port = 0;
1833 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001834 evTopoSwitch.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001835 evTopoSwitch = evHistTopologySwitch.put(evTopoSwitch, actn);
1836 }
1837
1838 private void evHistTopoLink(long srcDpid, long dstDpid, short srcPort,
1839 short dstPort, int srcPortState, int dstPortState,
1840 ILinkDiscovery.LinkType linkType,
1841 EvAction actn, String reason) {
1842 if (evTopoLink == null) {
1843 evTopoLink = new EventHistoryTopologyLink();
1844 }
1845 evTopoLink.srcSwDpid = srcDpid;
1846 evTopoLink.dstSwDpid = dstDpid;
1847 evTopoLink.srcSwport = srcPort & 0xffff;
1848 evTopoLink.dstSwport = dstPort & 0xffff;
1849 evTopoLink.srcPortState = srcPortState;
1850 evTopoLink.dstPortState = dstPortState;
Ray Milkey269ffb92014-04-03 14:43:30 -07001851 evTopoLink.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001852 switch (linkType) {
1853 case DIRECT_LINK:
1854 evTopoLink.linkType = "DIRECT_LINK";
1855 break;
1856 case MULTIHOP_LINK:
1857 evTopoLink.linkType = "MULTIHOP_LINK";
1858 break;
1859 case TUNNEL:
1860 evTopoLink.linkType = "TUNNEL";
1861 break;
1862 case INVALID_LINK:
1863 default:
1864 evTopoLink.linkType = "Unknown";
1865 break;
1866 }
1867 evTopoLink = evHistTopologyLink.put(evTopoLink, actn);
1868 }
1869
1870 public void evHistTopoCluster(long dpid, long clusterIdOld,
1871 long clusterIdNew, EvAction action, String reason) {
1872 if (evTopoCluster == null) {
1873 evTopoCluster = new EventHistoryTopologyCluster();
1874 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001875 evTopoCluster.dpid = dpid;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001876 evTopoCluster.clusterIdOld = clusterIdOld;
1877 evTopoCluster.clusterIdNew = clusterIdNew;
Ray Milkey269ffb92014-04-03 14:43:30 -07001878 evTopoCluster.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001879 evTopoCluster = evHistTopologyCluster.put(evTopoCluster, action);
1880 }
1881
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001882 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001883 public boolean isAutoPortFastFeature() {
1884 return autoPortFastFeature;
1885 }
1886
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001887 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001888 public void setAutoPortFastFeature(boolean autoPortFastFeature) {
1889 this.autoPortFastFeature = autoPortFastFeature;
1890 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001891}