blob: a395ab62f43fd982fc7f39426252ff430fb14750 [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
104 * critical sections
105 * -portLinks contains LinkTuples where one of the src or dst
106 * SwitchPortTuple matches the map key
107 * -switchLinks contains LinkTuples where one of the src or dst
108 * SwitchPortTuple's id matches the switch id
109 * -Each LinkTuple will be indexed into switchLinks for both
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 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;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -0700118 protected final static 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
142 private static final byte TLV_DIRECTION_VALUE_FORWARD[] = {0x01};
143 private static final byte TLV_DIRECTION_VALUE_REVERSE[] = {0x02};
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800144 private static final LLDPTLV forwardTLV
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
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800150 private static final LLDPTLV reverseTLV
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;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800158 protected final int DISCOVERY_TASK_INTERVAL = 1;
Umesh Krishnaswamy77601202013-04-03 21:46:01 -0700159 protected final int LINK_TIMEOUT = 35; // original 35 secs, aggressive 5 secs
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 protected 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 Milkey269ffb92014-04-03 14:43:30 -0700165 protected 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 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800184 * Map from link to the most recent time it was verified functioning
185 */
186 protected Map<Link, LinkInfo> links;
187
188 /**
189 * Map from switch id to a set of all links with it as an endpoint
190 */
191 protected Map<Long, Set<Link>> switchLinks;
192
193 /**
194 * Map from a id:port to the set of links containing it as an endpoint
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
206 /* topology aware components are called in the order they were added to the
207 * the array */
208 protected ArrayList<ILinkDiscoveryListener> linkDiscoveryAware;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800209
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700210 protected class LinkUpdate extends LDUpdate {
211
Ray Milkey269ffb92014-04-03 14:43:30 -0700212 public LinkUpdate(LDUpdate old) {
213 super(old);
214 }
215
216 @LogMessageDoc(level = "ERROR",
217 message = "Error in link discovery updates loop",
218 explanation = "An unknown error occured while dispatching " +
219 "link update notifications",
220 recommendation = LogMessageDoc.GENERIC_ACTION)
221 @Override
222 public void dispatch() {
223 if (linkDiscoveryAware != null) {
Jonathan Hartb0904bf2013-11-26 14:41:11 -0800224 if (log.isTraceEnabled()) {
225 log.trace("Dispatching link discovery update {} {} {} {} {} for {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700226 new Object[]{this.getOperation(),
227 HexString.toHexString(this.getSrc()), this.getSrcPort(),
228 HexString.toHexString(this.getDst()), this.getDstPort(),
229 linkDiscoveryAware});
Jonathan Hartb0904bf2013-11-26 14:41:11 -0800230 }
231 try {
232 for (ILinkDiscoveryListener lda : linkDiscoveryAware) { // order maintained
233 lda.linkDiscoveryUpdate(this);
234 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700235 } catch (Exception e) {
Jonathan Hartb0904bf2013-11-26 14:41:11 -0800236 log.error("Error in link discovery updates loop", e);
237 }
238 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700239 }
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700240 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800241
242 /**
243 * List of ports through which LLDP/BDDPs are not sent.
244 */
245 protected Set<NodePortTuple> suppressLinkDiscovery;
246
Ray Milkey269ffb92014-04-03 14:43:30 -0700247 /**
248 * A list of ports that are quarantined for discovering links through
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800249 * them. Data traffic from these ports are not allowed until the ports
250 * are released from quarantine.
251 */
252 protected LinkedBlockingQueue<NodePortTuple> quarantineQueue;
253 protected LinkedBlockingQueue<NodePortTuple> maintenanceQueue;
254 /**
255 * Quarantine task
256 */
257 protected SingletonTask bddpTask;
258 protected final int BDDP_TASK_INTERVAL = 100; // 100 ms.
259 protected final int BDDP_TASK_SIZE = 5; // # of ports per iteration
260
261 /**
262 * Map of broadcast domain ports and the last time a BDDP was either
263 * sent or received on that port.
264 */
265 protected Map<NodePortTuple, Long> broadcastDomainPortTimeMap;
266
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800267 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800268 * Get the LLDP sending period in seconds.
Ray Milkey269ffb92014-04-03 14:43:30 -0700269 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800270 * @return LLDP sending period in seconds.
271 */
272 public int getLldpFrequency() {
273 return LLDP_TO_KNOWN_INTERVAL;
274 }
275
276 /**
277 * Get the LLDP timeout value in seconds
Ray Milkey269ffb92014-04-03 14:43:30 -0700278 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800279 * @return LLDP timeout value in seconds
280 */
281 public int getLldpTimeout() {
282 return LINK_TIMEOUT;
283 }
284
285 public Map<NodePortTuple, Set<Link>> getPortLinks() {
286 return portLinks;
287 }
288
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800289 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800290 public Set<NodePortTuple> getSuppressLLDPsInfo() {
291 return suppressLinkDiscovery;
292 }
293
294 /**
295 * Add a switch port to the suppressed LLDP list.
296 * Remove any known links on the switch port.
297 */
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800298 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700299 public void AddToSuppressLLDPs(long sw, short port) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800300 NodePortTuple npt = new NodePortTuple(sw, port);
301 this.suppressLinkDiscovery.add(npt);
302 deleteLinksOnPort(npt, "LLDP suppressed.");
303 }
304
305 /**
306 * Remove a switch port from the suppressed LLDP list.
307 * Discover links on that switchport.
308 */
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800309 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700310 public void RemoveFromSuppressLLDPs(long sw, short port) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800311 NodePortTuple npt = new NodePortTuple(sw, port);
312 this.suppressLinkDiscovery.remove(npt);
313 discover(npt);
314 }
315
316 public boolean isShuttingDown() {
317 return shuttingDown;
318 }
319
320 public boolean isFastPort(long sw, short port) {
321 return false;
322 }
323
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800324 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800325 public ILinkDiscovery.LinkType getLinkType(Link lt, LinkInfo info) {
326 if (info.getUnicastValidTime() != null) {
327 return ILinkDiscovery.LinkType.DIRECT_LINK;
328 } else if (info.getMulticastValidTime() != null) {
329 return ILinkDiscovery.LinkType.MULTIHOP_LINK;
330 }
331 return ILinkDiscovery.LinkType.INVALID_LINK;
332 }
333
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800334
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800335 private boolean isLinkDiscoverySuppressed(long sw, short portNumber) {
336 return this.suppressLinkDiscovery.contains(new NodePortTuple(sw, portNumber));
337 }
338
339 protected void discoverLinks() {
340
341 // timeout known links.
342 timeoutLinks();
343
344 //increment LLDP clock
Ray Milkey269ffb92014-04-03 14:43:30 -0700345 lldpClock = (lldpClock + 1) % LLDP_TO_ALL_INTERVAL;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800346
347 if (lldpClock == 0) {
348 log.debug("Sending LLDP out on all ports.");
349 discoverOnAllPorts();
350 }
351 }
352
353
354 /**
Ray Milkey269ffb92014-04-03 14:43:30 -0700355 * Quarantine Ports.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800356 */
357 protected class QuarantineWorker implements Runnable {
358 @Override
359 public void run() {
360 try {
361 processBDDPLists();
Ray Milkey269ffb92014-04-03 14:43:30 -0700362 } catch (Exception e) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800363 log.error("Error in quarantine worker thread", e);
364 } finally {
Ray Milkey269ffb92014-04-03 14:43:30 -0700365 bddpTask.reschedule(BDDP_TASK_INTERVAL,
366 TimeUnit.MILLISECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800367 }
368 }
369 }
370
371 /**
372 * Add a switch port to the quarantine queue. Schedule the
373 * quarantine task if the quarantine queue was empty before adding
374 * this switch port.
Ray Milkey269ffb92014-04-03 14:43:30 -0700375 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800376 * @param npt
377 */
378 protected void addToQuarantineQueue(NodePortTuple npt) {
379 if (quarantineQueue.contains(npt) == false)
380 quarantineQueue.add(npt);
381 }
382
383 /**
384 * Remove a switch port from the quarantine queue.
385 */
386 protected void removeFromQuarantineQueue(NodePortTuple npt) {
387 // Remove all occurrences of the node port tuple from the list.
Ray Milkey269ffb92014-04-03 14:43:30 -0700388 while (quarantineQueue.remove(npt)) ;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800389 }
390
391 /**
392 * Add a switch port to maintenance queue.
Ray Milkey269ffb92014-04-03 14:43:30 -0700393 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800394 * @param npt
395 */
396 protected void addToMaintenanceQueue(NodePortTuple npt) {
397 // TODO We are not checking if the switch port tuple is already
398 // in the maintenance list or not. This will be an issue for
399 // really large number of switch ports in the network.
400 if (maintenanceQueue.contains(npt) == false)
401 maintenanceQueue.add(npt);
402 }
403
404 /**
405 * Remove a switch port from maintenance queue.
Ray Milkey269ffb92014-04-03 14:43:30 -0700406 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800407 * @param npt
408 */
409 protected void removeFromMaintenanceQueue(NodePortTuple npt) {
410 // Remove all occurrences of the node port tuple from the queue.
Ray Milkey269ffb92014-04-03 14:43:30 -0700411 while (maintenanceQueue.remove(npt)) ;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800412 }
413
414 /**
Ray Milkey269ffb92014-04-03 14:43:30 -0700415 * This method processes the quarantine list in bursts. The task is
416 * at most once per BDDP_TASK_INTERVAL.
417 * One each call, BDDP_TASK_SIZE number of switch ports are processed.
418 * Once the BDDP packets are sent out through the switch ports, the ports
419 * are removed from the quarantine list.
420 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800421
422 protected void processBDDPLists() {
423 int count = 0;
424 Set<NodePortTuple> nptList = new HashSet<NodePortTuple>();
425
Ray Milkey269ffb92014-04-03 14:43:30 -0700426 while (count < BDDP_TASK_SIZE && quarantineQueue.peek() != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800427 NodePortTuple npt;
428 npt = quarantineQueue.remove();
429 sendDiscoveryMessage(npt.getNodeId(), npt.getPortId(), false, false);
430 nptList.add(npt);
431 count++;
432 }
433
434 count = 0;
435 while (count < BDDP_TASK_SIZE && maintenanceQueue.peek() != null) {
436 NodePortTuple npt;
437 npt = maintenanceQueue.remove();
438 sendDiscoveryMessage(npt.getNodeId(), npt.getPortId(), false, false);
439 count++;
440 }
441
Ray Milkey269ffb92014-04-03 14:43:30 -0700442 for (NodePortTuple npt : nptList) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800443 generateSwitchPortStatusUpdate(npt.getNodeId(), npt.getPortId());
444 }
445 }
446
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800447 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800448 public Set<Short> getQuarantinedPorts(long sw) {
449 Set<Short> qPorts = new HashSet<Short>();
450
451 Iterator<NodePortTuple> iter = quarantineQueue.iterator();
452 while (iter.hasNext()) {
453 NodePortTuple npt = iter.next();
454 if (npt.getNodeId() == sw) {
455 qPorts.add(npt.getPortId());
456 }
457 }
458 return qPorts;
459 }
460
461 private void generateSwitchPortStatusUpdate(long sw, short port) {
462 UpdateOperation operation;
463
464 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
465 if (iofSwitch == null) return;
466
467 OFPhysicalPort ofp = iofSwitch.getPort(port);
468 if (ofp == null) return;
469
470 int srcPortState = ofp.getState();
471 boolean portUp = ((srcPortState &
472 OFPortState.OFPPS_STP_MASK.getValue()) !=
473 OFPortState.OFPPS_STP_BLOCK.getValue());
474
475 if (portUp) operation = UpdateOperation.PORT_UP;
476 else operation = UpdateOperation.PORT_DOWN;
477
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700478 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, operation));
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800479
480
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700481 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800482 }
483
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800484 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800485 * Send LLDP on known ports
486 */
487 protected void discoverOnKnownLinkPorts() {
488 // Copy the port set.
489 Set<NodePortTuple> nptSet = new HashSet<NodePortTuple>();
490 nptSet.addAll(portLinks.keySet());
491
492 // Send LLDP from each of them.
Ray Milkey269ffb92014-04-03 14:43:30 -0700493 for (NodePortTuple npt : nptSet) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800494 discover(npt);
495 }
496 }
497
498 protected void discover(NodePortTuple npt) {
499 discover(npt.getNodeId(), npt.getPortId());
500 }
501
502 protected void discover(long sw, short port) {
503 sendDiscoveryMessage(sw, port, true, false);
504 }
505
506 /**
HIGUCHI Yuta30d03302013-06-14 13:47:36 -0700507 * Learn remote switches when running as a distributed controller ONOS
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800508 */
509 protected IOFSwitch addRemoteSwitch(long sw, short port) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700510 IOnosRemoteSwitch remotesw = null;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800511
Ray Milkey269ffb92014-04-03 14:43:30 -0700512 // add a switch if we have not seen it before
513 remotesw = remoteSwitches.get(sw);
Jonathan Harte7231052013-01-25 00:01:14 -0800514
Ray Milkey269ffb92014-04-03 14:43:30 -0700515 if (remotesw == null) {
516 remotesw = new OFSwitchImpl();
517 remotesw.setupRemoteSwitch(sw);
518 remoteSwitches.put(remotesw.getId(), remotesw);
519 log.debug("addRemoteSwitch(): added fake remote sw {}", remotesw);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800520 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800521
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800522 // add the port if we have not seen it before
Umesh Krishnaswamy68c118c2013-01-25 11:07:09 -0800523 if (remotesw.getPort(port) == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700524 OFPhysicalPort remoteport = new OFPhysicalPort();
525 remoteport.setPortNumber(port);
526 remoteport.setName("fake_" + port);
527 remoteport.setConfig(0);
528 remoteport.setState(0);
529 remotesw.setPort(remoteport);
530 log.debug("addRemoteSwitch(): added fake remote port {} to sw {}", remoteport, remotesw.getId());
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800531 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800532
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800533 return remotesw;
534 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800535
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800536 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800537 * Send link discovery message out of a given switch port.
538 * The discovery message may be a standard LLDP or a modified
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800539 * LLDP, where the dst mac address is set to :ff.
Ray Milkey269ffb92014-04-03 14:43:30 -0700540 * <p/>
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800541 * TODO: The modified LLDP will updated in the future and may
542 * use a different eth-type.
Ray Milkey269ffb92014-04-03 14:43:30 -0700543 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800544 * @param sw
545 * @param port
Ray Milkey269ffb92014-04-03 14:43:30 -0700546 * @param isStandard indicates standard or modified LLDP
547 * @param isReverse indicates whether the LLDP was sent as a response
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800548 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700549 @LogMessageDoc(level = "ERROR",
550 message = "Failure sending LLDP out port {port} on switch {switch}",
551 explanation = "An I/O error occured while sending LLDP message " +
552 "to the switch.",
553 recommendation = LogMessageDoc.CHECK_SWITCH)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800554 protected void sendDiscoveryMessage(long sw, short port,
Ray Milkey269ffb92014-04-03 14:43:30 -0700555 boolean isStandard,
556 boolean isReverse) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800557
558 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
559 if (iofSwitch == null) {
560 return;
561 }
562
563 if (port == OFPort.OFPP_LOCAL.getValue())
564 return;
565
566 OFPhysicalPort ofpPort = iofSwitch.getPort(port);
567
568 if (ofpPort == null) {
569 if (log.isTraceEnabled()) {
570 log.trace("Null physical port. sw={}, port={}", sw, port);
571 }
572 return;
573 }
574
575 if (isLinkDiscoverySuppressed(sw, port)) {
576 /* Dont send LLDPs out of this port as suppressLLDPs set
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800577 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800578 */
579 return;
580 }
581
582 // For fast ports, do not send forward LLDPs or BDDPs.
583 if (!isReverse && autoPortFastFeature && isFastPort(sw, port))
584 return;
585
586 if (log.isTraceEnabled()) {
587 log.trace("Sending LLDP packet out of swich: {}, port: {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700588 sw, port);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800589 }
590
591 // using "nearest customer bridge" MAC address for broadest possible propagation
592 // through provider and TPMR bridges (see IEEE 802.1AB-2009 and 802.1Q-2011),
593 // in particular the Linux bridge which behaves mostly like a provider bridge
Ray Milkey269ffb92014-04-03 14:43:30 -0700594 byte[] chassisId = new byte[]{4, 0, 0, 0, 0, 0, 0}; // filled in later
595 byte[] portId = new byte[]{2, 0, 0}; // filled in later
596 byte[] ttlValue = new byte[]{0, 0x78};
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800597 // OpenFlow OUI - 00-26-E1
Ray Milkey269ffb92014-04-03 14:43:30 -0700598 byte[] dpidTLVValue = new byte[]{0x0, 0x26, (byte) 0xe1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800599 LLDPTLV dpidTLV = new LLDPTLV().setType((byte) 127).setLength((short) dpidTLVValue.length).setValue(dpidTLVValue);
600
601 byte[] dpidArray = new byte[8];
602 ByteBuffer dpidBB = ByteBuffer.wrap(dpidArray);
603 ByteBuffer portBB = ByteBuffer.wrap(portId, 1, 2);
604
605 Long dpid = sw;
606 dpidBB.putLong(dpid);
607 // set the ethernet source mac to last 6 bytes of dpid
608 System.arraycopy(dpidArray, 2, ofpPort.getHardwareAddress(), 0, 6);
609 // set the chassis id's value to last 6 bytes of dpid
610 System.arraycopy(dpidArray, 2, chassisId, 1, 6);
611 // set the optional tlv to the full dpid
612 System.arraycopy(dpidArray, 0, dpidTLVValue, 4, 8);
613
614
615 // set the portId to the outgoing port
616 portBB.putShort(port);
617 if (log.isTraceEnabled()) {
618 log.trace("Sending LLDP out of interface: {}/{}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700619 HexString.toHexString(sw), port);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800620 }
621
622 LLDP lldp = new LLDP();
623 lldp.setChassisId(new LLDPTLV().setType((byte) 1).setLength((short) chassisId.length).setValue(chassisId));
624 lldp.setPortId(new LLDPTLV().setType((byte) 2).setLength((short) portId.length).setValue(portId));
625 lldp.setTtl(new LLDPTLV().setType((byte) 3).setLength((short) ttlValue.length).setValue(ttlValue));
626 lldp.getOptionalTLVList().add(dpidTLV);
627
628 // Add the controller identifier to the TLV value.
629 lldp.getOptionalTLVList().add(controllerTLV);
630 if (isReverse) {
631 lldp.getOptionalTLVList().add(reverseTLV);
Ray Milkey269ffb92014-04-03 14:43:30 -0700632 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800633 lldp.getOptionalTLVList().add(forwardTLV);
634 }
635
636 Ethernet ethernet;
637 if (isStandard) {
638 ethernet = new Ethernet()
Ray Milkey269ffb92014-04-03 14:43:30 -0700639 .setSourceMACAddress(ofpPort.getHardwareAddress())
640 .setDestinationMACAddress(LLDP_STANDARD_DST_MAC_STRING)
641 .setEtherType(Ethernet.TYPE_LLDP);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800642 ethernet.setPayload(lldp);
643 } else {
644 BSN bsn = new BSN(BSN.BSN_TYPE_BDDP);
645 bsn.setPayload(lldp);
646
647 ethernet = new Ethernet()
Ray Milkey269ffb92014-04-03 14:43:30 -0700648 .setSourceMACAddress(ofpPort.getHardwareAddress())
649 .setDestinationMACAddress(LLDP_BSN_DST_MAC_STRING)
650 .setEtherType(Ethernet.TYPE_BSN);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800651 ethernet.setPayload(bsn);
652 }
653
654
655 // serialize and wrap in a packet out
656 byte[] data = ethernet.serialize();
657 OFPacketOut po = (OFPacketOut) floodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT);
658 po.setBufferId(OFPacketOut.BUFFER_ID_NONE);
659 po.setInPort(OFPort.OFPP_NONE);
660
661 // set actions
662 List<OFAction> actions = new ArrayList<OFAction>();
663 actions.add(new OFActionOutput(port, (short) 0));
664 po.setActions(actions);
665 po.setActionsLength((short) OFActionOutput.MINIMUM_LENGTH);
666
667 // set data
668 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + po.getActionsLength() + data.length);
669 po.setPacketData(data);
670
671 // send
672 try {
673 iofSwitch.write(po, null);
674 iofSwitch.flush();
675 } catch (IOException e) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700676 log.error("Failure sending LLDP out port " + port + " on switch " + iofSwitch.getStringId(), e);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800677 }
678
679 }
680
681 /**
682 * Send LLDPs to all switch-ports
683 */
684 protected void discoverOnAllPorts() {
685 if (log.isTraceEnabled()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800686 log.trace("Sending LLDP packets out of all the enabled ports on switch");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800687 }
688 Set<Long> switches = floodlightProvider.getSwitches().keySet();
689 // Send standard LLDPs
Ray Milkey269ffb92014-04-03 14:43:30 -0700690 for (long sw : switches) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800691 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
692 if (iofSwitch == null) continue;
693 if (iofSwitch.getEnabledPorts() != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700694 for (OFPhysicalPort ofp : iofSwitch.getEnabledPorts()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800695 if (isLinkDiscoverySuppressed(sw, ofp.getPortNumber()))
696 continue;
697 if (autoPortFastFeature && isFastPort(sw, ofp.getPortNumber()))
698 continue;
699
700 // sends forward LLDP only non-fastports.
701 sendDiscoveryMessage(sw, ofp.getPortNumber(), true, false);
702
703 // If the switch port is not alreayd in the maintenance
704 // queue, add it.
705 NodePortTuple npt = new NodePortTuple(sw, ofp.getPortNumber());
706 addToMaintenanceQueue(npt);
707 }
708 }
709 }
710 }
711
712 protected void setControllerTLV() {
713 //Setting the controllerTLVValue based on current nano time,
714 //controller's IP address, and the network interface object hash
715 //the corresponding IP address.
716
717 final int prime = 7867;
718 InetAddress localIPAddress = null;
719 NetworkInterface localInterface = null;
720
Ray Milkey269ffb92014-04-03 14:43:30 -0700721 byte[] controllerTLVValue = new byte[]{0, 0, 0, 0, 0, 0, 0, 0}; // 8 byte value.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800722 ByteBuffer bb = ByteBuffer.allocate(10);
723
Ray Milkey269ffb92014-04-03 14:43:30 -0700724 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800725 localIPAddress = java.net.InetAddress.getLocalHost();
726 localInterface = NetworkInterface.getByInetAddress(localIPAddress);
727 } catch (Exception e) {
728 e.printStackTrace();
729 }
730
731 long result = System.nanoTime();
732 if (localIPAddress != null)
733 result = result * prime + IPv4.toIPv4Address(localIPAddress.getHostAddress());
734 if (localInterface != null)
735 result = result * prime + localInterface.hashCode();
736 // set the first 4 bits to 0.
737 result = result & (0x0fffffffffffffffL);
738
739 bb.putLong(result);
740
741 bb.rewind();
742 bb.get(controllerTLVValue, 0, 8);
743
744 this.controllerTLV = new LLDPTLV().setType((byte) 0x0c).setLength((short) controllerTLVValue.length).setValue(controllerTLVValue);
745 }
746
747 @Override
748 public String getName() {
749 return "linkdiscovery";
750 }
751
752 @Override
753 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
754 switch (msg.getType()) {
755 case PACKET_IN:
756 return this.handlePacketIn(sw.getId(), (OFPacketIn) msg, cntx);
757 case PORT_STATUS:
758 return this.handlePortStatus(sw.getId(), (OFPortStatus) msg);
759 default:
760 break;
761 }
762 return Command.CONTINUE;
763 }
764
765 private Command handleLldp(LLDP lldp, long sw, OFPacketIn pi, boolean isStandard, FloodlightContext cntx) {
766 // If LLDP is suppressed on this port, ignore received packet as well
767 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
768 if (iofSwitch == null) {
769 return Command.STOP;
770 }
771
772 if (isLinkDiscoverySuppressed(sw, pi.getInPort()))
773 return Command.STOP;
774
775 // If this is a malformed LLDP, or not from us, exit
776 if (lldp.getPortId() == null || lldp.getPortId().getLength() != 3)
777 return Command.CONTINUE;
778
779 long myId = ByteBuffer.wrap(controllerTLV.getValue()).getLong();
780 long otherId = 0;
781 boolean myLLDP = false;
782 Boolean isReverse = null;
783
784 ByteBuffer portBB = ByteBuffer.wrap(lldp.getPortId().getValue());
785 portBB.position(1);
786
787 Short remotePort = portBB.getShort();
788 IOFSwitch remoteSwitch = null;
789
790 // Verify this LLDP packet matches what we're looking for
791 for (LLDPTLV lldptlv : lldp.getOptionalTLVList()) {
792 if (lldptlv.getType() == 127 && lldptlv.getLength() == 12 &&
793 lldptlv.getValue()[0] == 0x0 && lldptlv.getValue()[1] == 0x26 &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700794 lldptlv.getValue()[2] == (byte) 0xe1 && lldptlv.getValue()[3] == 0x0) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800795 ByteBuffer dpidBB = ByteBuffer.wrap(lldptlv.getValue());
796 remoteSwitch = floodlightProvider.getSwitches().get(dpidBB.getLong(4));
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800797 if (remoteSwitch == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700798 // Added by ONOS
799 // floodlight LLDP coming from a remote switch connected to a different controller
800 // add it to our cache of unconnected remote switches
801 remoteSwitch = addRemoteSwitch(dpidBB.getLong(4), remotePort);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800802 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700803 } else if (lldptlv.getType() == 12 && lldptlv.getLength() == 8) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800804 otherId = ByteBuffer.wrap(lldptlv.getValue()).getLong();
805 if (myId == otherId)
806 myLLDP = true;
807 } else if (lldptlv.getType() == TLV_DIRECTION_TYPE &&
808 lldptlv.getLength() == TLV_DIRECTION_LENGTH) {
809 if (lldptlv.getValue()[0] == TLV_DIRECTION_VALUE_FORWARD[0])
810 isReverse = false;
811 else if (lldptlv.getValue()[0] == TLV_DIRECTION_VALUE_REVERSE[0])
812 isReverse = true;
813 }
814 }
815
816 if (myLLDP == false) {
817 // This is not the LLDP sent by this controller.
818 // If the LLDP message has multicast bit set, then we need to broadcast
819 // the packet as a regular packet.
820 if (isStandard) {
821 if (log.isTraceEnabled()) {
822 log.trace("Getting standard LLDP from a different controller and quelching it.");
823 }
824 return Command.STOP;
Ray Milkey269ffb92014-04-03 14:43:30 -0700825 } else if (sw <= remoteSwitch.getId()) {
Teru Ub7246af2014-01-13 13:24:38 -0800826 if (log.isTraceEnabled()) {
827 log.trace("Getting BBDP from a different controller. myId {}: remoteId {}", myId, otherId);
828 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 -0700829 }
830 //XXX ONOS: Fix the BDDP broadcast issue
831 //return Command.CONTINUE;
832 return Command.STOP;
Teru Ub7246af2014-01-13 13:24:38 -0800833 }
834 /*
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800835 else if (myId < otherId) {
836 if (log.isTraceEnabled()) {
837 log.trace("Getting BDDP packets from a different controller" +
838 "and letting it go through normal processing chain.");
839 }
HIGUCHI Yuta30d03302013-06-14 13:47:36 -0700840 //XXX ONOS: Fix the BDDP broadcast issue
Jonathan Hart0b2c76a2013-02-27 17:09:33 -0800841 //return Command.CONTINUE;
842 return Command.STOP;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800843 }
Teru Ub7246af2014-01-13 13:24:38 -0800844 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800845 }
846
847
848 if (remoteSwitch == null) {
849 // Ignore LLDPs not generated by Floodlight, or from a switch that has recently
850 // disconnected, or from a switch connected to another Floodlight instance
851 if (log.isTraceEnabled()) {
852 log.trace("Received LLDP from remote switch not connected to the controller");
853 }
854 return Command.STOP;
855 }
856
857 if (!remoteSwitch.portEnabled(remotePort)) {
858 if (log.isTraceEnabled()) {
859 log.trace("Ignoring link with disabled source port: switch {} port {}", remoteSwitch, remotePort);
860 }
861 return Command.STOP;
862 }
863 if (suppressLinkDiscovery.contains(new NodePortTuple(remoteSwitch.getId(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700864 remotePort))) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800865 if (log.isTraceEnabled()) {
866 log.trace("Ignoring link with suppressed src port: switch {} port {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700867 remoteSwitch, remotePort);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800868 }
869 return Command.STOP;
870 }
871 if (!iofSwitch.portEnabled(pi.getInPort())) {
872 if (log.isTraceEnabled()) {
873 log.trace("Ignoring link with disabled dest port: switch {} port {}", sw, pi.getInPort());
874 }
875 return Command.STOP;
876 }
877
878 OFPhysicalPort physicalPort = remoteSwitch.getPort(remotePort);
879 int srcPortState = (physicalPort != null) ? physicalPort.getState() : 0;
880 physicalPort = iofSwitch.getPort(pi.getInPort());
881 int dstPortState = (physicalPort != null) ? physicalPort.getState() : 0;
882
883 // Store the time of update to this link, and push it out to routingEngine
884 Link lt = new Link(remoteSwitch.getId(), remotePort, iofSwitch.getId(), pi.getInPort());
885
886
887 Long lastLldpTime = null;
888 Long lastBddpTime = null;
889
890 Long firstSeenTime = System.currentTimeMillis();
891
892 if (isStandard)
893 lastLldpTime = System.currentTimeMillis();
894 else
895 lastBddpTime = System.currentTimeMillis();
896
897 LinkInfo newLinkInfo =
898 new LinkInfo(firstSeenTime, lastLldpTime, lastBddpTime,
Ray Milkey269ffb92014-04-03 14:43:30 -0700899 srcPortState, dstPortState);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800900
901 addOrUpdateLink(lt, newLinkInfo);
902
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800903 // Check if reverse link exists.
904 // If it doesn't exist and if the forward link was seen
905 // first seen within a small interval, send probe on the
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800906 // reverse link.
907
908 newLinkInfo = links.get(lt);
909 if (newLinkInfo != null && isStandard && isReverse == false) {
910 Link reverseLink = new Link(lt.getDst(), lt.getDstPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700911 lt.getSrc(), lt.getSrcPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800912 LinkInfo reverseInfo = links.get(reverseLink);
913 if (reverseInfo == null) {
914 // the reverse link does not exist.
915 if (newLinkInfo.getFirstSeenTime() > System.currentTimeMillis() - LINK_TIMEOUT) {
916 this.sendDiscoveryMessage(lt.getDst(), lt.getDstPort(), isStandard, true);
917 }
918 }
919 }
920
921 // If the received packet is a BDDP packet, then create a reverse BDDP
922 // link as well.
923 if (!isStandard) {
924 Link reverseLink = new Link(lt.getDst(), lt.getDstPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700925 lt.getSrc(), lt.getSrcPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800926
927 // srcPortState and dstPort state are reversed.
928 LinkInfo reverseInfo =
929 new LinkInfo(firstSeenTime, lastLldpTime, lastBddpTime,
Ray Milkey269ffb92014-04-03 14:43:30 -0700930 dstPortState, srcPortState);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800931
932 addOrUpdateLink(reverseLink, reverseInfo);
933 }
934
935 // Remove the node ports from the quarantine and maintenance queues.
936 NodePortTuple nptSrc = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
937 NodePortTuple nptDst = new NodePortTuple(lt.getDst(), lt.getDstPort());
938 removeFromQuarantineQueue(nptSrc);
939 removeFromMaintenanceQueue(nptSrc);
940 removeFromQuarantineQueue(nptDst);
941 removeFromMaintenanceQueue(nptDst);
942
943 // Consume this message
944 return Command.STOP;
945 }
946
947 protected Command handlePacketIn(long sw, OFPacketIn pi,
948 FloodlightContext cntx) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800949 Ethernet eth =
950 IFloodlightProviderService.bcStore.get(cntx,
Ray Milkey269ffb92014-04-03 14:43:30 -0700951 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800952
Ray Milkey269ffb92014-04-03 14:43:30 -0700953 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800954 BSN bsn = (BSN) eth.getPayload();
955 if (bsn == null) return Command.STOP;
956 if (bsn.getPayload() == null) return Command.STOP;
957 // It could be a packet other than BSN LLDP, therefore
958 // continue with the regular processing.
959 if (bsn.getPayload() instanceof LLDP == false)
960 return Command.CONTINUE;
Ubuntu9cbb4ca2013-02-07 17:19:59 +0000961 return handleLldp((LLDP) bsn.getPayload(), sw, pi, false, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700962 } else if (eth.getEtherType() == Ethernet.TYPE_LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800963 return handleLldp((LLDP) eth.getPayload(), sw, pi, true, cntx);
964 } else if (eth.getEtherType() < 1500) {
965 long destMac = eth.getDestinationMAC().toLong();
Ray Milkey269ffb92014-04-03 14:43:30 -0700966 if ((destMac & LINK_LOCAL_MASK) == LINK_LOCAL_VALUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800967 if (log.isTraceEnabled()) {
968 log.trace("Ignoring packet addressed to 802.1D/Q " +
969 "reserved address.");
970 }
971 return Command.STOP;
972 }
973 }
974
975 // If packet-in is from a quarantine port, stop processing.
976 NodePortTuple npt = new NodePortTuple(sw, pi.getInPort());
977 if (quarantineQueue.contains(npt)) return Command.STOP;
978
979 return Command.CONTINUE;
980 }
981
982 protected UpdateOperation getUpdateOperation(int srcPortState,
983 int dstPortState) {
984 boolean added =
985 (((srcPortState &
Ray Milkey269ffb92014-04-03 14:43:30 -0700986 OFPortState.OFPPS_STP_MASK.getValue()) !=
987 OFPortState.OFPPS_STP_BLOCK.getValue()) &&
988 ((dstPortState &
989 OFPortState.OFPPS_STP_MASK.getValue()) !=
990 OFPortState.OFPPS_STP_BLOCK.getValue()));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800991
992 if (added) return UpdateOperation.LINK_UPDATED;
993 return UpdateOperation.LINK_REMOVED;
994 }
995
996
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800997 protected UpdateOperation getUpdateOperation(int srcPortState) {
998 boolean portUp = ((srcPortState &
999 OFPortState.OFPPS_STP_MASK.getValue()) !=
1000 OFPortState.OFPPS_STP_BLOCK.getValue());
1001
1002 if (portUp) return UpdateOperation.PORT_UP;
1003 else return UpdateOperation.PORT_DOWN;
1004 }
1005
1006 protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) {
1007
1008 NodePortTuple srcNpt, dstNpt;
1009 boolean linkChanged = false;
1010
1011 lock.writeLock().lock();
1012 try {
1013 // put the new info. if an old info exists, it will be returned.
1014 LinkInfo oldInfo = links.put(lt, newInfo);
1015 if (oldInfo != null &&
1016 oldInfo.getFirstSeenTime() < newInfo.getFirstSeenTime())
1017 newInfo.setFirstSeenTime(oldInfo.getFirstSeenTime());
1018
1019 if (log.isTraceEnabled()) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001020 log.trace("addOrUpdateLink: {} {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001021 lt,
1022 (newInfo.getMulticastValidTime() != null) ? "multicast" : "unicast");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001023 }
1024
1025 UpdateOperation updateOperation = null;
1026 linkChanged = false;
1027
1028 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1029 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1030
1031 if (oldInfo == null) {
1032 // index it by switch source
1033 if (!switchLinks.containsKey(lt.getSrc()))
1034 switchLinks.put(lt.getSrc(), new HashSet<Link>());
1035 switchLinks.get(lt.getSrc()).add(lt);
1036
1037 // index it by switch dest
1038 if (!switchLinks.containsKey(lt.getDst()))
1039 switchLinks.put(lt.getDst(), new HashSet<Link>());
1040 switchLinks.get(lt.getDst()).add(lt);
1041
1042 // index both ends by switch:port
1043 if (!portLinks.containsKey(srcNpt))
1044 portLinks.put(srcNpt, new HashSet<Link>());
1045 portLinks.get(srcNpt).add(lt);
1046
1047 if (!portLinks.containsKey(dstNpt))
1048 portLinks.put(dstNpt, new HashSet<Link>());
1049 portLinks.get(dstNpt).add(lt);
1050
1051 // Add to portNOFLinks if the unicast valid time is null
1052 if (newInfo.getUnicastValidTime() == null)
1053 addLinkToBroadcastDomain(lt);
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001054
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001055 // ONOS: Distinguish added event separately from updated event
Pankaj Berdea41016d2013-06-10 21:18:18 -07001056 updateOperation = UpdateOperation.LINK_ADDED;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001057 linkChanged = true;
1058
1059 // Add to event history
1060 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001061 lt.getDst(),
1062 lt.getSrcPort(),
1063 lt.getDstPort(),
1064 newInfo.getSrcPortState(), newInfo.getDstPortState(),
1065 getLinkType(lt, newInfo),
1066 EvAction.LINK_ADDED, "LLDP Recvd");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001067 } else {
1068 // Since the link info is already there, we need to
1069 // update the right fields.
1070 if (newInfo.getUnicastValidTime() == null) {
1071 // This is due to a multicast LLDP, so copy the old unicast
1072 // value.
1073 if (oldInfo.getUnicastValidTime() != null) {
1074 newInfo.setUnicastValidTime(oldInfo.getUnicastValidTime());
1075 }
1076 } else if (newInfo.getMulticastValidTime() == null) {
1077 // This is due to a unicast LLDP, so copy the old multicast
1078 // value.
1079 if (oldInfo.getMulticastValidTime() != null) {
1080 newInfo.setMulticastValidTime(oldInfo.getMulticastValidTime());
1081 }
1082 }
1083
1084 Long oldTime = oldInfo.getUnicastValidTime();
1085 Long newTime = newInfo.getUnicastValidTime();
1086 // the link has changed its state between openflow and non-openflow
1087 // if the unicastValidTimes are null or not null
1088 if (oldTime != null & newTime == null) {
1089 // openflow -> non-openflow transition
1090 // we need to add the link tuple to the portNOFLinks
1091 addLinkToBroadcastDomain(lt);
1092 linkChanged = true;
1093 } else if (oldTime == null & newTime != null) {
1094 // non-openflow -> openflow transition
1095 // we need to remove the link from the portNOFLinks
1096 removeLinkFromBroadcastDomain(lt);
1097 linkChanged = true;
1098 }
1099
1100 // Only update the port states if they've changed
1101 if (newInfo.getSrcPortState().intValue() !=
1102 oldInfo.getSrcPortState().intValue() ||
1103 newInfo.getDstPortState().intValue() !=
Ray Milkey269ffb92014-04-03 14:43:30 -07001104 oldInfo.getDstPortState().intValue())
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001105 linkChanged = true;
1106
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001107 if (linkChanged) {
1108 updateOperation = getUpdateOperation(newInfo.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001109 newInfo.getDstPortState());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001110 if (log.isTraceEnabled()) {
1111 log.trace("Updated link {}", lt);
1112 }
1113 // Add to event history
1114 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001115 lt.getDst(),
1116 lt.getSrcPort(),
1117 lt.getDstPort(),
1118 newInfo.getSrcPortState(), newInfo.getDstPortState(),
1119 getLinkType(lt, newInfo),
1120 EvAction.LINK_PORT_STATE_UPDATED,
1121 "LLDP Recvd");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001122 }
1123 }
1124
1125 if (linkChanged) {
1126 // find out if the link was added or removed here.
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001127 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001128 lt.getDst(), lt.getDstPort(),
1129 getLinkType(lt, newInfo),
1130 updateOperation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001131 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001132 }
1133 } finally {
1134 lock.writeLock().unlock();
1135 }
1136
1137 return linkChanged;
1138 }
1139
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001140 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001141 public Map<Long, Set<Link>> getSwitchLinks() {
1142 return this.switchLinks;
1143 }
1144
1145 /**
1146 * Removes links from memory and storage.
Ray Milkey269ffb92014-04-03 14:43:30 -07001147 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001148 * @param links The List of @LinkTuple to delete.
1149 */
1150 protected void deleteLinks(List<Link> links, String reason) {
1151 NodePortTuple srcNpt, dstNpt;
1152
1153 lock.writeLock().lock();
1154 try {
1155 for (Link lt : links) {
1156 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
Ray Milkey269ffb92014-04-03 14:43:30 -07001157 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001158
1159 switchLinks.get(lt.getSrc()).remove(lt);
1160 switchLinks.get(lt.getDst()).remove(lt);
1161 if (switchLinks.containsKey(lt.getSrc()) &&
1162 switchLinks.get(lt.getSrc()).isEmpty())
1163 this.switchLinks.remove(lt.getSrc());
1164 if (this.switchLinks.containsKey(lt.getDst()) &&
1165 this.switchLinks.get(lt.getDst()).isEmpty())
1166 this.switchLinks.remove(lt.getDst());
1167
1168 if (this.portLinks.get(srcNpt) != null) {
1169 this.portLinks.get(srcNpt).remove(lt);
1170 if (this.portLinks.get(srcNpt).isEmpty())
1171 this.portLinks.remove(srcNpt);
1172 }
1173 if (this.portLinks.get(dstNpt) != null) {
1174 this.portLinks.get(dstNpt).remove(lt);
1175 if (this.portLinks.get(dstNpt).isEmpty())
1176 this.portLinks.remove(dstNpt);
1177 }
1178
1179 LinkInfo info = this.links.remove(lt);
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001180 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001181 lt.getDst(), lt.getDstPort(),
1182 getLinkType(lt, info),
1183 UpdateOperation.LINK_REMOVED));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001184 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001185
1186 // Update Event History
1187 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001188 lt.getDst(),
1189 lt.getSrcPort(),
1190 lt.getDstPort(),
1191 0, 0, // Port states
1192 ILinkDiscovery.LinkType.INVALID_LINK,
1193 EvAction.LINK_DELETED, reason);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001194
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001195 // TODO Whenever link is removed, it has to checked if
1196 // the switchports must be added to quarantine.
1197
1198 if (log.isTraceEnabled()) {
1199 log.trace("Deleted link {}", lt);
1200 }
1201 }
1202 } finally {
1203 lock.writeLock().unlock();
1204 }
1205 }
1206
1207 /**
1208 * Handles an OFPortStatus message from a switch. We will add or
1209 * delete LinkTupes as well re-compute the topology if needed.
Ray Milkey269ffb92014-04-03 14:43:30 -07001210 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001211 * @param sw The IOFSwitch that sent the port status message
1212 * @param ps The OFPortStatus message
1213 * @return The Command to continue or stop after we process this message
1214 */
1215 protected Command handlePortStatus(long sw, OFPortStatus ps) {
1216
1217 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
1218 if (iofSwitch == null) return Command.CONTINUE;
HIGUCHI Yutaa89b2842013-06-17 13:54:57 -07001219
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001220 // ONOS: If we do not control this switch, then we should not process its port status messages
Ray Milkey269ffb92014-04-03 14:43:30 -07001221 if (!registryService.hasControl(iofSwitch.getId()))
1222 return Command.CONTINUE;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001223
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001224 if (log.isTraceEnabled()) {
1225 log.trace("handlePortStatus: Switch {} port #{} reason {}; " +
1226 "config is {} state is {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001227 new Object[]{iofSwitch.getStringId(),
1228 ps.getDesc().getPortNumber(),
1229 ps.getReason(),
1230 ps.getDesc().getConfig(),
1231 ps.getDesc().getState()});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001232 }
1233
1234 short port = ps.getDesc().getPortNumber();
1235 NodePortTuple npt = new NodePortTuple(sw, port);
Ray Milkey269ffb92014-04-03 14:43:30 -07001236 boolean linkDeleted = false;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001237 boolean linkInfoChanged = false;
1238
1239 lock.writeLock().lock();
1240 try {
1241 // if ps is a delete, or a modify where the port is down or
1242 // configured down
Ray Milkey269ffb92014-04-03 14:43:30 -07001243 if ((byte) OFPortReason.OFPPR_DELETE.ordinal() == ps.getReason() ||
1244 ((byte) OFPortReason.OFPPR_MODIFY.ordinal() ==
1245 ps.getReason() && !portEnabled(ps.getDesc()))) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001246 deleteLinksOnPort(npt, "Port Status Changed");
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001247 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, UpdateOperation.PORT_DOWN));
1248 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001249 linkDeleted = true;
Ray Milkey269ffb92014-04-03 14:43:30 -07001250 } else if (ps.getReason() ==
1251 (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001252 // If ps is a port modification and the port state has changed
1253 // that affects links in the topology
1254
1255 if (this.portLinks.containsKey(npt)) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001256 for (Link lt : this.portLinks.get(npt)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001257 LinkInfo linkInfo = links.get(lt);
Ray Milkey269ffb92014-04-03 14:43:30 -07001258 assert (linkInfo != null);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001259 Integer updatedSrcPortState = null;
1260 Integer updatedDstPortState = null;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001261 if (lt.getSrc() == npt.getNodeId() &&
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001262 lt.getSrcPort() == npt.getPortId() &&
1263 (linkInfo.getSrcPortState() !=
Ray Milkey269ffb92014-04-03 14:43:30 -07001264 ps.getDesc().getState())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001265 updatedSrcPortState = ps.getDesc().getState();
1266 linkInfo.setSrcPortState(updatedSrcPortState);
1267 }
1268 if (lt.getDst() == npt.getNodeId() &&
1269 lt.getDstPort() == npt.getPortId() &&
1270 (linkInfo.getDstPortState() !=
Ray Milkey269ffb92014-04-03 14:43:30 -07001271 ps.getDesc().getState())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001272 updatedDstPortState = ps.getDesc().getState();
1273 linkInfo.setDstPortState(updatedDstPortState);
1274 }
1275 if ((updatedSrcPortState != null) ||
1276 (updatedDstPortState != null)) {
1277 // The link is already known to link discovery
1278 // manager and the status has changed, therefore
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001279 // send an LinkUpdate.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001280 UpdateOperation operation =
1281 getUpdateOperation(linkInfo.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001282 linkInfo.getDstPortState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001283 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001284 lt.getDst(), lt.getDstPort(),
1285 getLinkType(lt, linkInfo),
1286 operation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001287 controller.publishUpdate(update);
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001288
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001289 linkInfoChanged = true;
1290 }
1291 }
1292 }
1293
1294 UpdateOperation operation =
1295 getUpdateOperation(ps.getDesc().getState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001296 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, operation));
1297 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001298 }
1299
Ray Milkey269ffb92014-04-03 14:43:30 -07001300 if (!linkDeleted && !linkInfoChanged) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001301 if (log.isTraceEnabled()) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001302 log.trace("handlePortStatus: Switch {} port #{} reason {};" +
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001303 " no links to update/remove",
Ray Milkey269ffb92014-04-03 14:43:30 -07001304 new Object[]{HexString.toHexString(sw),
1305 ps.getDesc().getPortNumber(),
1306 ps.getReason()});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001307 }
1308 }
1309 } finally {
1310 lock.writeLock().unlock();
1311 }
1312
1313 if (!linkDeleted) {
1314 // Send LLDP right away when port state is changed for faster
1315 // cluster-merge. If it is a link delete then there is not need
1316 // to send the LLDPs right away and instead we wait for the LLDPs
1317 // to be sent on the timer as it is normally done
1318 // do it outside the write-lock
1319 // sendLLDPTask.reschedule(1000, TimeUnit.MILLISECONDS);
1320 processNewPort(npt.getNodeId(), npt.getPortId());
1321 }
1322 return Command.CONTINUE;
1323 }
1324
1325 /**
1326 * Process a new port.
1327 * If link discovery is disabled on the port, then do nothing.
1328 * If autoportfast feature is enabled and the port is a fast port, then
1329 * do nothing.
1330 * Otherwise, send LLDP message. Add the port to quarantine.
Ray Milkey269ffb92014-04-03 14:43:30 -07001331 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001332 * @param sw
1333 * @param p
1334 */
1335 private void processNewPort(long sw, short p) {
1336 if (isLinkDiscoverySuppressed(sw, p)) {
1337 // Do nothing as link discovery is suppressed.
Ray Milkey269ffb92014-04-03 14:43:30 -07001338 } else if (autoPortFastFeature && isFastPort(sw, p)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001339 // Do nothing as the port is a fast port.
Ray Milkey269ffb92014-04-03 14:43:30 -07001340 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001341 NodePortTuple npt = new NodePortTuple(sw, p);
1342 discover(sw, p);
1343 // if it is not a fast port, add it to quarantine.
1344 if (!isFastPort(sw, p)) {
1345 addToQuarantineQueue(npt);
1346 } else {
1347 // Add to maintenance queue to ensure that BDDP packets
1348 // are sent out.
1349 addToMaintenanceQueue(npt);
1350 }
1351 }
1352 }
1353
1354 /**
1355 * We send out LLDP messages when a switch is added to discover the topology
Ray Milkey269ffb92014-04-03 14:43:30 -07001356 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001357 * @param sw The IOFSwitch that connected to the controller
1358 */
1359 @Override
1360 public void addedSwitch(IOFSwitch sw) {
1361
1362 if (sw.getEnabledPorts() != null) {
1363 for (Short p : sw.getEnabledPortNumbers()) {
1364 processNewPort(sw.getId(), p);
1365 }
1366 }
1367 // Update event history
1368 evHistTopoSwitch(sw, EvAction.SWITCH_CONNECTED, "None");
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001369 LinkUpdate update = new LinkUpdate(new LDUpdate(sw.getId(), null,
Ray Milkey269ffb92014-04-03 14:43:30 -07001370 UpdateOperation.SWITCH_UPDATED));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001371 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001372 }
1373
1374 /**
1375 * When a switch disconnects we remove any links from our map and notify.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001376 */
1377 @Override
1378 public void removedSwitch(IOFSwitch iofSwitch) {
1379 // Update event history
1380 long sw = iofSwitch.getId();
1381 evHistTopoSwitch(iofSwitch, EvAction.SWITCH_DISCONNECTED, "None");
1382 List<Link> eraseList = new ArrayList<Link>();
1383 lock.writeLock().lock();
1384 try {
1385 if (switchLinks.containsKey(sw)) {
1386 if (log.isTraceEnabled()) {
1387 log.trace("Handle switchRemoved. Switch {}; removing links {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001388 HexString.toHexString(sw), switchLinks.get(sw));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001389 }
1390 // add all tuples with an endpoint on this switch to erase list
1391 eraseList.addAll(switchLinks.get(sw));
HIGUCHI Yutaa89b2842013-06-17 13:54:57 -07001392 deleteLinks(eraseList, "Switch Removed");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001393
1394 // Send a switch removed update
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001395 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, null, UpdateOperation.SWITCH_REMOVED));
1396 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001397 }
1398 } finally {
1399 lock.writeLock().unlock();
1400 }
1401 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001402
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001403 /**
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001404 * We don't react the port changed notifications here. we listen for
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001405 * OFPortStatus messages directly. Might consider using this notifier
1406 * instead
1407 */
1408 @Override
1409 public void switchPortChanged(Long switchId) {
1410 // no-op
1411 }
1412
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001413 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001414 * Delete links incident on a given switch port.
Ray Milkey269ffb92014-04-03 14:43:30 -07001415 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001416 * @param npt
1417 * @param reason
1418 */
1419 protected void deleteLinksOnPort(NodePortTuple npt, String reason) {
1420 List<Link> eraseList = new ArrayList<Link>();
1421 if (this.portLinks.containsKey(npt)) {
1422 if (log.isTraceEnabled()) {
1423 log.trace("handlePortStatus: Switch {} port #{} " +
1424 "removing links {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001425 new Object[]{HexString.toHexString(npt.getNodeId()),
1426 npt.getPortId(),
1427 this.portLinks.get(npt)});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001428 }
1429 eraseList.addAll(this.portLinks.get(npt));
1430 deleteLinks(eraseList, reason);
1431 }
1432 }
1433
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001434 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001435 * Iterates through the list of links and deletes if the
1436 * last discovery message reception time exceeds timeout values.
1437 */
1438 protected void timeoutLinks() {
1439 List<Link> eraseList = new ArrayList<Link>();
1440 Long curTime = System.currentTimeMillis();
1441 boolean linkChanged = false;
1442
1443 // reentrant required here because deleteLink also write locks
1444 lock.writeLock().lock();
1445 try {
1446 Iterator<Entry<Link, LinkInfo>> it =
1447 this.links.entrySet().iterator();
1448 while (it.hasNext()) {
1449 Entry<Link, LinkInfo> entry = it.next();
1450 Link lt = entry.getKey();
1451 LinkInfo info = entry.getValue();
1452
1453 // Timeout the unicast and multicast LLDP valid times
1454 // independently.
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001455 if ((info.getUnicastValidTime() != null) &&
Ray Milkey269ffb92014-04-03 14:43:30 -07001456 (info.getUnicastValidTime() + (this.LINK_TIMEOUT * 1000) < curTime)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001457 info.setUnicastValidTime(null);
1458
1459 if (info.getMulticastValidTime() != null)
1460 addLinkToBroadcastDomain(lt);
1461 // Note that even if mTime becomes null later on,
1462 // the link would be deleted, which would trigger updateClusters().
1463 linkChanged = true;
1464 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001465 if ((info.getMulticastValidTime() != null) &&
1466 (info.getMulticastValidTime() + (this.LINK_TIMEOUT * 1000) < curTime)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001467 info.setMulticastValidTime(null);
1468 // if uTime is not null, then link will remain as openflow
1469 // link. If uTime is null, it will be deleted. So, we
1470 // don't care about linkChanged flag here.
1471 removeLinkFromBroadcastDomain(lt);
1472 linkChanged = true;
1473 }
1474 // Add to the erase list only if the unicast
1475 // time is null.
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001476 if (info.getUnicastValidTime() == null &&
Ray Milkey269ffb92014-04-03 14:43:30 -07001477 info.getMulticastValidTime() == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001478 eraseList.add(entry.getKey());
1479 } else if (linkChanged) {
1480 UpdateOperation operation;
1481 operation = getUpdateOperation(info.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001482 info.getDstPortState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001483 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001484 lt.getDst(), lt.getDstPort(),
1485 getLinkType(lt, info),
1486 operation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001487 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001488 }
1489 }
1490
1491 // if any link was deleted or any link was changed.
1492 if ((eraseList.size() > 0) || linkChanged) {
1493 deleteLinks(eraseList, "LLDP timeout");
1494 }
1495 } finally {
1496 lock.writeLock().unlock();
1497 }
1498 }
1499
1500 private boolean portEnabled(OFPhysicalPort port) {
1501 if (port == null)
1502 return false;
1503 if ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0)
1504 return false;
1505 if ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)
1506 return false;
1507 // Port STP state doesn't work with multiple VLANs, so ignore it for now
1508 // if ((port.getState() & OFPortState.OFPPS_STP_MASK.getValue()) == OFPortState.OFPPS_STP_BLOCK.getValue())
1509 // return false;
1510 return true;
1511 }
1512
1513 public Map<NodePortTuple, Set<Link>> getPortBroadcastDomainLinks() {
1514 return portBroadcastDomainLinks;
1515 }
1516
1517 @Override
1518 public Map<Link, LinkInfo> getLinks() {
1519 lock.readLock().lock();
1520 Map<Link, LinkInfo> result;
1521 try {
1522 result = new HashMap<Link, LinkInfo>(links);
1523 } finally {
1524 lock.readLock().unlock();
1525 }
1526 return result;
1527 }
1528
1529 protected void addLinkToBroadcastDomain(Link lt) {
1530
1531 NodePortTuple srcNpt, dstNpt;
1532 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1533 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1534
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001535 if (!portBroadcastDomainLinks.containsKey(srcNpt))
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001536 portBroadcastDomainLinks.put(srcNpt, new HashSet<Link>());
1537 portBroadcastDomainLinks.get(srcNpt).add(lt);
1538
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001539 if (!portBroadcastDomainLinks.containsKey(dstNpt))
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001540 portBroadcastDomainLinks.put(dstNpt, new HashSet<Link>());
1541 portBroadcastDomainLinks.get(dstNpt).add(lt);
1542 }
1543
1544 protected void removeLinkFromBroadcastDomain(Link lt) {
1545
1546 NodePortTuple srcNpt, dstNpt;
1547 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1548 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1549
1550 if (portBroadcastDomainLinks.containsKey(srcNpt)) {
1551 portBroadcastDomainLinks.get(srcNpt).remove(lt);
1552 if (portBroadcastDomainLinks.get(srcNpt).isEmpty())
1553 portBroadcastDomainLinks.remove(srcNpt);
1554 }
1555
1556 if (portBroadcastDomainLinks.containsKey(dstNpt)) {
1557 portBroadcastDomainLinks.get(dstNpt).remove(lt);
1558 if (portBroadcastDomainLinks.get(dstNpt).isEmpty())
1559 portBroadcastDomainLinks.remove(dstNpt);
1560 }
1561 }
1562
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001563 @Override
1564 public void addListener(ILinkDiscoveryListener listener) {
1565 linkDiscoveryAware.add(listener);
1566 }
1567
1568 /**
1569 * Register a link discovery aware component
Ray Milkey269ffb92014-04-03 14:43:30 -07001570 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001571 * @param linkDiscoveryAwareComponent
1572 */
1573 public void addLinkDiscoveryAware(ILinkDiscoveryListener linkDiscoveryAwareComponent) {
1574 // TODO make this a copy on write set or lock it somehow
1575 this.linkDiscoveryAware.add(linkDiscoveryAwareComponent);
1576 }
1577
1578 /**
1579 * Deregister a link discovery aware component
Ray Milkey269ffb92014-04-03 14:43:30 -07001580 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001581 * @param linkDiscoveryAwareComponent
1582 */
1583 public void removeLinkDiscoveryAware(ILinkDiscoveryListener linkDiscoveryAwareComponent) {
1584 // TODO make this a copy on write set or lock it somehow
1585 this.linkDiscoveryAware.remove(linkDiscoveryAwareComponent);
1586 }
1587
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001588 @Override
1589 public boolean isCallbackOrderingPrereq(OFType type, String name) {
1590 return false;
1591 }
1592
1593 @Override
1594 public boolean isCallbackOrderingPostreq(OFType type, String name) {
1595 return false;
1596 }
1597
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001598 // IFloodlightModule classes
1599
1600 @Override
1601 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001602 Collection<Class<? extends IFloodlightService>> l =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001603 new ArrayList<Class<? extends IFloodlightService>>();
1604 l.add(ILinkDiscoveryService.class);
1605 //l.add(ITopologyService.class);
1606 return l;
1607 }
1608
1609 @Override
1610 public Map<Class<? extends IFloodlightService>, IFloodlightService>
1611 getServiceImpls() {
1612 Map<Class<? extends IFloodlightService>,
Ray Milkey269ffb92014-04-03 14:43:30 -07001613 IFloodlightService> m =
1614 new HashMap<Class<? extends IFloodlightService>,
1615 IFloodlightService>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001616 // We are the class that implements the service
1617 m.put(ILinkDiscoveryService.class, this);
1618 return m;
1619 }
1620
1621 @Override
1622 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001623 Collection<Class<? extends IFloodlightService>> l =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001624 new ArrayList<Class<? extends IFloodlightService>>();
1625 l.add(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001626 l.add(IThreadPoolService.class);
1627 l.add(IRestApiService.class);
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001628 // Added by ONOS
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001629 l.add(IControllerRegistryService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001630 return l;
1631 }
1632
1633 @Override
1634 public void init(FloodlightModuleContext context)
1635 throws FloodlightModuleException {
1636 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001637 threadPool = context.getServiceImpl(IThreadPoolService.class);
1638 restApi = context.getServiceImpl(IRestApiService.class);
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001639 // Added by ONOS
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001640 registryService = context.getServiceImpl(IControllerRegistryService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001641
1642 // Set the autoportfast feature to false.
1643 this.autoPortFastFeature = false;
1644
1645 // We create this here because there is no ordering guarantee
1646 this.linkDiscoveryAware = new ArrayList<ILinkDiscoveryListener>();
1647 this.lock = new ReentrantReadWriteLock();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001648 this.links = new HashMap<Link, LinkInfo>();
1649 this.portLinks = new HashMap<NodePortTuple, Set<Link>>();
1650 this.suppressLinkDiscovery =
1651 Collections.synchronizedSet(new HashSet<NodePortTuple>());
1652 this.portBroadcastDomainLinks = new HashMap<NodePortTuple, Set<Link>>();
1653 this.switchLinks = new HashMap<Long, Set<Link>>();
1654 this.quarantineQueue = new LinkedBlockingQueue<NodePortTuple>();
1655 this.maintenanceQueue = new LinkedBlockingQueue<NodePortTuple>();
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001656 // Added by ONOS
HIGUCHI Yuta7677a6f2013-06-14 14:13:35 -07001657 this.remoteSwitches = new HashMap<Long, IOnosRemoteSwitch>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001658
1659 this.evHistTopologySwitch =
1660 new EventHistory<EventHistoryTopologySwitch>("Topology: Switch");
1661 this.evHistTopologyLink =
1662 new EventHistory<EventHistoryTopologyLink>("Topology: Link");
1663 this.evHistTopologyCluster =
1664 new EventHistory<EventHistoryTopologyCluster>("Topology: Cluster");
1665 }
1666
1667 @Override
1668 @LogMessageDocs({
Ray Milkey269ffb92014-04-03 14:43:30 -07001669 @LogMessageDoc(level = "ERROR",
1670 message = "No storage source found.",
1671 explanation = "Storage source was not initialized; cannot initialize " +
1672 "link discovery.",
1673 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1674 @LogMessageDoc(level = "ERROR",
1675 message = "Error in installing listener for " +
1676 "switch config table {table}",
1677 explanation = "Failed to install storage notification for the " +
1678 "switch config table",
1679 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1680 @LogMessageDoc(level = "ERROR",
1681 message = "No storage source found.",
1682 explanation = "Storage source was not initialized; cannot initialize " +
1683 "link discovery.",
1684 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1685 @LogMessageDoc(level = "ERROR",
1686 message = "Exception in LLDP send timer.",
1687 explanation = "An unknown error occured while sending LLDP " +
1688 "messages to switches.",
1689 recommendation = LogMessageDoc.CHECK_SWITCH)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001690 })
1691 public void startUp(FloodlightModuleContext context) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001692 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001693 controller =
1694 context.getServiceImpl(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001695
1696 // To be started by the first switch connection
1697 discoveryTask = new SingletonTask(ses, new Runnable() {
1698 @Override
1699 public void run() {
1700 try {
1701 discoverLinks();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001702 } catch (Exception e) {
1703 log.error("Exception in LLDP send timer.", e);
1704 } finally {
1705 if (!shuttingDown) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001706 // Always reschedule link discovery if we're not
1707 // shutting down (no chance of SLAVE role now)
Jonathan Hartec4f14e2013-12-12 10:46:38 -08001708 log.trace("Rescheduling discovery task");
1709 discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL,
Ray Milkey269ffb92014-04-03 14:43:30 -07001710 TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001711 }
1712 }
1713 }
1714 });
1715
Jonathan Hartec4f14e2013-12-12 10:46:38 -08001716 // Always reschedule link discovery as we are never in SLAVE role now
1717 discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL, TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001718
1719 // Setup the BDDP task. It is invoked whenever switch port tuples
1720 // are added to the quarantine list.
1721 bddpTask = new SingletonTask(ses, new QuarantineWorker());
1722 bddpTask.reschedule(BDDP_TASK_INTERVAL, TimeUnit.MILLISECONDS);
1723
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001724
1725 // Register for the OpenFlow messages we want to receive
1726 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
1727 floodlightProvider.addOFMessageListener(OFType.PORT_STATUS, this);
1728 // Register for switch updates
1729 floodlightProvider.addOFSwitchListener(this);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001730 if (restApi != null)
1731 restApi.addRestletRoutable(new LinkDiscoveryWebRoutable());
1732 setControllerTLV();
1733 }
1734
1735 // ****************************************************
1736 // Topology Manager's Event History members and methods
1737 // ****************************************************
1738
1739 // Topology Manager event history
Ray Milkey269ffb92014-04-03 14:43:30 -07001740 public EventHistory<EventHistoryTopologySwitch> evHistTopologySwitch;
1741 public EventHistory<EventHistoryTopologyLink> evHistTopologyLink;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001742 public EventHistory<EventHistoryTopologyCluster> evHistTopologyCluster;
Ray Milkey269ffb92014-04-03 14:43:30 -07001743 public EventHistoryTopologySwitch evTopoSwitch;
1744 public EventHistoryTopologyLink evTopoLink;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001745 public EventHistoryTopologyCluster evTopoCluster;
1746
1747 // Switch Added/Deleted
1748 private void evHistTopoSwitch(IOFSwitch sw, EvAction actn, String reason) {
1749 if (evTopoSwitch == null) {
1750 evTopoSwitch = new EventHistoryTopologySwitch();
1751 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001752 evTopoSwitch.dpid = sw.getId();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001753 if ((sw.getChannel() != null) &&
1754 (SocketAddress.class.isInstance(
Ray Milkey269ffb92014-04-03 14:43:30 -07001755 sw.getChannel().getRemoteAddress()))) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001756 evTopoSwitch.ipv4Addr =
Ray Milkey269ffb92014-04-03 14:43:30 -07001757 IPv4.toIPv4Address(((InetSocketAddress) (sw.getChannel().
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001758 getRemoteAddress())).getAddress().getAddress());
Ray Milkey269ffb92014-04-03 14:43:30 -07001759 evTopoSwitch.l4Port =
1760 ((InetSocketAddress) (sw.getChannel().
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001761 getRemoteAddress())).getPort();
1762 } else {
1763 evTopoSwitch.ipv4Addr = 0;
1764 evTopoSwitch.l4Port = 0;
1765 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001766 evTopoSwitch.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001767 evTopoSwitch = evHistTopologySwitch.put(evTopoSwitch, actn);
1768 }
1769
1770 private void evHistTopoLink(long srcDpid, long dstDpid, short srcPort,
1771 short dstPort, int srcPortState, int dstPortState,
1772 ILinkDiscovery.LinkType linkType,
1773 EvAction actn, String reason) {
1774 if (evTopoLink == null) {
1775 evTopoLink = new EventHistoryTopologyLink();
1776 }
1777 evTopoLink.srcSwDpid = srcDpid;
1778 evTopoLink.dstSwDpid = dstDpid;
1779 evTopoLink.srcSwport = srcPort & 0xffff;
1780 evTopoLink.dstSwport = dstPort & 0xffff;
1781 evTopoLink.srcPortState = srcPortState;
1782 evTopoLink.dstPortState = dstPortState;
Ray Milkey269ffb92014-04-03 14:43:30 -07001783 evTopoLink.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001784 switch (linkType) {
1785 case DIRECT_LINK:
1786 evTopoLink.linkType = "DIRECT_LINK";
1787 break;
1788 case MULTIHOP_LINK:
1789 evTopoLink.linkType = "MULTIHOP_LINK";
1790 break;
1791 case TUNNEL:
1792 evTopoLink.linkType = "TUNNEL";
1793 break;
1794 case INVALID_LINK:
1795 default:
1796 evTopoLink.linkType = "Unknown";
1797 break;
1798 }
1799 evTopoLink = evHistTopologyLink.put(evTopoLink, actn);
1800 }
1801
1802 public void evHistTopoCluster(long dpid, long clusterIdOld,
1803 long clusterIdNew, EvAction action, String reason) {
1804 if (evTopoCluster == null) {
1805 evTopoCluster = new EventHistoryTopologyCluster();
1806 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001807 evTopoCluster.dpid = dpid;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001808 evTopoCluster.clusterIdOld = clusterIdOld;
1809 evTopoCluster.clusterIdNew = clusterIdNew;
Ray Milkey269ffb92014-04-03 14:43:30 -07001810 evTopoCluster.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001811 evTopoCluster = evHistTopologyCluster.put(evTopoCluster, action);
1812 }
1813
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001814 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001815 public boolean isAutoPortFastFeature() {
1816 return autoPortFastFeature;
1817 }
1818
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001819 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001820 public void setAutoPortFastFeature(boolean autoPortFastFeature) {
1821 this.autoPortFastFeature = autoPortFastFeature;
1822 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001823}