blob: 6e0dc6932bf099e0a46980411bfa4e7f29beef5e [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
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};
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;
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 /**
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;
Ray Milkey2476cac2014-04-08 11:03:21 -0700258 protected static final int BDDP_TASK_INTERVAL = 100; // 100 ms.
259 protected static final int BDDP_TASK_SIZE = 5; // # of ports per iteration
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800260
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 Milkey1aa71f82014-04-08 16:23:24 -0700388 boolean removedSomething;
389
390 do {
391 removedSomething = quarantineQueue.remove(npt);
392 } while (removedSomething);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800393 }
394
395 /**
396 * Add a switch port to maintenance queue.
Ray Milkey269ffb92014-04-03 14:43:30 -0700397 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800398 * @param npt
399 */
400 protected void addToMaintenanceQueue(NodePortTuple npt) {
401 // TODO We are not checking if the switch port tuple is already
402 // in the maintenance list or not. This will be an issue for
403 // really large number of switch ports in the network.
404 if (maintenanceQueue.contains(npt) == false)
405 maintenanceQueue.add(npt);
406 }
407
408 /**
409 * Remove a switch port from maintenance queue.
Ray Milkey269ffb92014-04-03 14:43:30 -0700410 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800411 * @param npt
412 */
413 protected void removeFromMaintenanceQueue(NodePortTuple npt) {
414 // Remove all occurrences of the node port tuple from the queue.
Ray Milkey1aa71f82014-04-08 16:23:24 -0700415 boolean removedSomething;
416 do {
417 removedSomething = maintenanceQueue.remove(npt);
418 } while (removedSomething);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800419 }
420
421 /**
Ray Milkey269ffb92014-04-03 14:43:30 -0700422 * This method processes the quarantine list in bursts. The task is
423 * at most once per BDDP_TASK_INTERVAL.
424 * One each call, BDDP_TASK_SIZE number of switch ports are processed.
425 * Once the BDDP packets are sent out through the switch ports, the ports
426 * are removed from the quarantine list.
427 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800428
429 protected void processBDDPLists() {
430 int count = 0;
431 Set<NodePortTuple> nptList = new HashSet<NodePortTuple>();
432
Ray Milkey269ffb92014-04-03 14:43:30 -0700433 while (count < BDDP_TASK_SIZE && quarantineQueue.peek() != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800434 NodePortTuple npt;
435 npt = quarantineQueue.remove();
436 sendDiscoveryMessage(npt.getNodeId(), npt.getPortId(), false, false);
437 nptList.add(npt);
438 count++;
439 }
440
441 count = 0;
442 while (count < BDDP_TASK_SIZE && maintenanceQueue.peek() != null) {
443 NodePortTuple npt;
444 npt = maintenanceQueue.remove();
445 sendDiscoveryMessage(npt.getNodeId(), npt.getPortId(), false, false);
446 count++;
447 }
448
Ray Milkey269ffb92014-04-03 14:43:30 -0700449 for (NodePortTuple npt : nptList) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800450 generateSwitchPortStatusUpdate(npt.getNodeId(), npt.getPortId());
451 }
452 }
453
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800454 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800455 public Set<Short> getQuarantinedPorts(long sw) {
456 Set<Short> qPorts = new HashSet<Short>();
457
458 Iterator<NodePortTuple> iter = quarantineQueue.iterator();
459 while (iter.hasNext()) {
460 NodePortTuple npt = iter.next();
461 if (npt.getNodeId() == sw) {
462 qPorts.add(npt.getPortId());
463 }
464 }
465 return qPorts;
466 }
467
468 private void generateSwitchPortStatusUpdate(long sw, short port) {
469 UpdateOperation operation;
470
471 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
472 if (iofSwitch == null) return;
473
474 OFPhysicalPort ofp = iofSwitch.getPort(port);
475 if (ofp == null) return;
476
477 int srcPortState = ofp.getState();
478 boolean portUp = ((srcPortState &
479 OFPortState.OFPPS_STP_MASK.getValue()) !=
480 OFPortState.OFPPS_STP_BLOCK.getValue());
481
482 if (portUp) operation = UpdateOperation.PORT_UP;
483 else operation = UpdateOperation.PORT_DOWN;
484
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700485 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, operation));
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800486
487
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700488 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800489 }
490
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800491 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800492 * Send LLDP on known ports
493 */
494 protected void discoverOnKnownLinkPorts() {
495 // Copy the port set.
496 Set<NodePortTuple> nptSet = new HashSet<NodePortTuple>();
497 nptSet.addAll(portLinks.keySet());
498
499 // Send LLDP from each of them.
Ray Milkey269ffb92014-04-03 14:43:30 -0700500 for (NodePortTuple npt : nptSet) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800501 discover(npt);
502 }
503 }
504
505 protected void discover(NodePortTuple npt) {
506 discover(npt.getNodeId(), npt.getPortId());
507 }
508
509 protected void discover(long sw, short port) {
510 sendDiscoveryMessage(sw, port, true, false);
511 }
512
513 /**
HIGUCHI Yuta30d03302013-06-14 13:47:36 -0700514 * Learn remote switches when running as a distributed controller ONOS
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800515 */
516 protected IOFSwitch addRemoteSwitch(long sw, short port) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700517 IOnosRemoteSwitch remotesw = null;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800518
Ray Milkey269ffb92014-04-03 14:43:30 -0700519 // add a switch if we have not seen it before
520 remotesw = remoteSwitches.get(sw);
Jonathan Harte7231052013-01-25 00:01:14 -0800521
Ray Milkey269ffb92014-04-03 14:43:30 -0700522 if (remotesw == null) {
523 remotesw = new OFSwitchImpl();
524 remotesw.setupRemoteSwitch(sw);
525 remoteSwitches.put(remotesw.getId(), remotesw);
526 log.debug("addRemoteSwitch(): added fake remote sw {}", remotesw);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800527 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800528
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800529 // add the port if we have not seen it before
Umesh Krishnaswamy68c118c2013-01-25 11:07:09 -0800530 if (remotesw.getPort(port) == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700531 OFPhysicalPort remoteport = new OFPhysicalPort();
532 remoteport.setPortNumber(port);
533 remoteport.setName("fake_" + port);
534 remoteport.setConfig(0);
535 remoteport.setState(0);
536 remotesw.setPort(remoteport);
537 log.debug("addRemoteSwitch(): added fake remote port {} to sw {}", remoteport, remotesw.getId());
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800538 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800539
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800540 return remotesw;
541 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800542
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800543 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800544 * Send link discovery message out of a given switch port.
545 * The discovery message may be a standard LLDP or a modified
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800546 * LLDP, where the dst mac address is set to :ff.
Ray Milkey269ffb92014-04-03 14:43:30 -0700547 * <p/>
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800548 * TODO: The modified LLDP will updated in the future and may
549 * use a different eth-type.
Ray Milkey269ffb92014-04-03 14:43:30 -0700550 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800551 * @param sw
552 * @param port
Ray Milkey269ffb92014-04-03 14:43:30 -0700553 * @param isStandard indicates standard or modified LLDP
554 * @param isReverse indicates whether the LLDP was sent as a response
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800555 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700556 @LogMessageDoc(level = "ERROR",
557 message = "Failure sending LLDP out port {port} on switch {switch}",
558 explanation = "An I/O error occured while sending LLDP message " +
559 "to the switch.",
560 recommendation = LogMessageDoc.CHECK_SWITCH)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800561 protected void sendDiscoveryMessage(long sw, short port,
Ray Milkey269ffb92014-04-03 14:43:30 -0700562 boolean isStandard,
563 boolean isReverse) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800564
565 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
566 if (iofSwitch == null) {
567 return;
568 }
569
570 if (port == OFPort.OFPP_LOCAL.getValue())
571 return;
572
573 OFPhysicalPort ofpPort = iofSwitch.getPort(port);
574
575 if (ofpPort == null) {
576 if (log.isTraceEnabled()) {
577 log.trace("Null physical port. sw={}, port={}", sw, port);
578 }
579 return;
580 }
581
582 if (isLinkDiscoverySuppressed(sw, port)) {
583 /* Dont send LLDPs out of this port as suppressLLDPs set
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800584 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800585 */
586 return;
587 }
588
589 // For fast ports, do not send forward LLDPs or BDDPs.
590 if (!isReverse && autoPortFastFeature && isFastPort(sw, port))
591 return;
592
593 if (log.isTraceEnabled()) {
594 log.trace("Sending LLDP packet out of swich: {}, port: {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700595 sw, port);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800596 }
597
598 // using "nearest customer bridge" MAC address for broadest possible propagation
599 // through provider and TPMR bridges (see IEEE 802.1AB-2009 and 802.1Q-2011),
600 // in particular the Linux bridge which behaves mostly like a provider bridge
Ray Milkey269ffb92014-04-03 14:43:30 -0700601 byte[] chassisId = new byte[]{4, 0, 0, 0, 0, 0, 0}; // filled in later
602 byte[] portId = new byte[]{2, 0, 0}; // filled in later
603 byte[] ttlValue = new byte[]{0, 0x78};
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800604 // OpenFlow OUI - 00-26-E1
Ray Milkey269ffb92014-04-03 14:43:30 -0700605 byte[] dpidTLVValue = new byte[]{0x0, 0x26, (byte) 0xe1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800606 LLDPTLV dpidTLV = new LLDPTLV().setType((byte) 127).setLength((short) dpidTLVValue.length).setValue(dpidTLVValue);
607
608 byte[] dpidArray = new byte[8];
609 ByteBuffer dpidBB = ByteBuffer.wrap(dpidArray);
610 ByteBuffer portBB = ByteBuffer.wrap(portId, 1, 2);
611
612 Long dpid = sw;
613 dpidBB.putLong(dpid);
614 // set the ethernet source mac to last 6 bytes of dpid
615 System.arraycopy(dpidArray, 2, ofpPort.getHardwareAddress(), 0, 6);
616 // set the chassis id's value to last 6 bytes of dpid
617 System.arraycopy(dpidArray, 2, chassisId, 1, 6);
618 // set the optional tlv to the full dpid
619 System.arraycopy(dpidArray, 0, dpidTLVValue, 4, 8);
620
621
622 // set the portId to the outgoing port
623 portBB.putShort(port);
624 if (log.isTraceEnabled()) {
625 log.trace("Sending LLDP out of interface: {}/{}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700626 HexString.toHexString(sw), port);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800627 }
628
629 LLDP lldp = new LLDP();
630 lldp.setChassisId(new LLDPTLV().setType((byte) 1).setLength((short) chassisId.length).setValue(chassisId));
631 lldp.setPortId(new LLDPTLV().setType((byte) 2).setLength((short) portId.length).setValue(portId));
632 lldp.setTtl(new LLDPTLV().setType((byte) 3).setLength((short) ttlValue.length).setValue(ttlValue));
633 lldp.getOptionalTLVList().add(dpidTLV);
634
635 // Add the controller identifier to the TLV value.
636 lldp.getOptionalTLVList().add(controllerTLV);
637 if (isReverse) {
638 lldp.getOptionalTLVList().add(reverseTLV);
Ray Milkey269ffb92014-04-03 14:43:30 -0700639 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800640 lldp.getOptionalTLVList().add(forwardTLV);
641 }
642
643 Ethernet ethernet;
644 if (isStandard) {
645 ethernet = new Ethernet()
Ray Milkey269ffb92014-04-03 14:43:30 -0700646 .setSourceMACAddress(ofpPort.getHardwareAddress())
647 .setDestinationMACAddress(LLDP_STANDARD_DST_MAC_STRING)
648 .setEtherType(Ethernet.TYPE_LLDP);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800649 ethernet.setPayload(lldp);
650 } else {
651 BSN bsn = new BSN(BSN.BSN_TYPE_BDDP);
652 bsn.setPayload(lldp);
653
654 ethernet = new Ethernet()
Ray Milkey269ffb92014-04-03 14:43:30 -0700655 .setSourceMACAddress(ofpPort.getHardwareAddress())
656 .setDestinationMACAddress(LLDP_BSN_DST_MAC_STRING)
657 .setEtherType(Ethernet.TYPE_BSN);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800658 ethernet.setPayload(bsn);
659 }
660
661
662 // serialize and wrap in a packet out
663 byte[] data = ethernet.serialize();
664 OFPacketOut po = (OFPacketOut) floodlightProvider.getOFMessageFactory().getMessage(OFType.PACKET_OUT);
665 po.setBufferId(OFPacketOut.BUFFER_ID_NONE);
666 po.setInPort(OFPort.OFPP_NONE);
667
668 // set actions
669 List<OFAction> actions = new ArrayList<OFAction>();
670 actions.add(new OFActionOutput(port, (short) 0));
671 po.setActions(actions);
672 po.setActionsLength((short) OFActionOutput.MINIMUM_LENGTH);
673
674 // set data
675 po.setLengthU(OFPacketOut.MINIMUM_LENGTH + po.getActionsLength() + data.length);
676 po.setPacketData(data);
677
678 // send
679 try {
680 iofSwitch.write(po, null);
681 iofSwitch.flush();
682 } catch (IOException e) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700683 log.error("Failure sending LLDP out port " + port + " on switch " + iofSwitch.getStringId(), e);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800684 }
685
686 }
687
688 /**
689 * Send LLDPs to all switch-ports
690 */
691 protected void discoverOnAllPorts() {
692 if (log.isTraceEnabled()) {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800693 log.trace("Sending LLDP packets out of all the enabled ports on switch");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800694 }
695 Set<Long> switches = floodlightProvider.getSwitches().keySet();
696 // Send standard LLDPs
Ray Milkey269ffb92014-04-03 14:43:30 -0700697 for (long sw : switches) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800698 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
699 if (iofSwitch == null) continue;
700 if (iofSwitch.getEnabledPorts() != null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700701 for (OFPhysicalPort ofp : iofSwitch.getEnabledPorts()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800702 if (isLinkDiscoverySuppressed(sw, ofp.getPortNumber()))
703 continue;
704 if (autoPortFastFeature && isFastPort(sw, ofp.getPortNumber()))
705 continue;
706
707 // sends forward LLDP only non-fastports.
708 sendDiscoveryMessage(sw, ofp.getPortNumber(), true, false);
709
710 // If the switch port is not alreayd in the maintenance
711 // queue, add it.
712 NodePortTuple npt = new NodePortTuple(sw, ofp.getPortNumber());
713 addToMaintenanceQueue(npt);
714 }
715 }
716 }
717 }
718
719 protected void setControllerTLV() {
720 //Setting the controllerTLVValue based on current nano time,
721 //controller's IP address, and the network interface object hash
722 //the corresponding IP address.
723
724 final int prime = 7867;
725 InetAddress localIPAddress = null;
726 NetworkInterface localInterface = null;
727
Ray Milkey269ffb92014-04-03 14:43:30 -0700728 byte[] controllerTLVValue = new byte[]{0, 0, 0, 0, 0, 0, 0, 0}; // 8 byte value.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800729 ByteBuffer bb = ByteBuffer.allocate(10);
730
Ray Milkey269ffb92014-04-03 14:43:30 -0700731 try {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800732 localIPAddress = java.net.InetAddress.getLocalHost();
733 localInterface = NetworkInterface.getByInetAddress(localIPAddress);
734 } catch (Exception e) {
735 e.printStackTrace();
736 }
737
738 long result = System.nanoTime();
739 if (localIPAddress != null)
740 result = result * prime + IPv4.toIPv4Address(localIPAddress.getHostAddress());
741 if (localInterface != null)
742 result = result * prime + localInterface.hashCode();
743 // set the first 4 bits to 0.
744 result = result & (0x0fffffffffffffffL);
745
746 bb.putLong(result);
747
748 bb.rewind();
749 bb.get(controllerTLVValue, 0, 8);
750
751 this.controllerTLV = new LLDPTLV().setType((byte) 0x0c).setLength((short) controllerTLVValue.length).setValue(controllerTLVValue);
752 }
753
754 @Override
755 public String getName() {
756 return "linkdiscovery";
757 }
758
759 @Override
760 public Command receive(IOFSwitch sw, OFMessage msg, FloodlightContext cntx) {
761 switch (msg.getType()) {
762 case PACKET_IN:
763 return this.handlePacketIn(sw.getId(), (OFPacketIn) msg, cntx);
764 case PORT_STATUS:
765 return this.handlePortStatus(sw.getId(), (OFPortStatus) msg);
766 default:
767 break;
768 }
769 return Command.CONTINUE;
770 }
771
772 private Command handleLldp(LLDP lldp, long sw, OFPacketIn pi, boolean isStandard, FloodlightContext cntx) {
773 // If LLDP is suppressed on this port, ignore received packet as well
774 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
775 if (iofSwitch == null) {
776 return Command.STOP;
777 }
778
779 if (isLinkDiscoverySuppressed(sw, pi.getInPort()))
780 return Command.STOP;
781
782 // If this is a malformed LLDP, or not from us, exit
783 if (lldp.getPortId() == null || lldp.getPortId().getLength() != 3)
784 return Command.CONTINUE;
785
786 long myId = ByteBuffer.wrap(controllerTLV.getValue()).getLong();
787 long otherId = 0;
788 boolean myLLDP = false;
789 Boolean isReverse = null;
790
791 ByteBuffer portBB = ByteBuffer.wrap(lldp.getPortId().getValue());
792 portBB.position(1);
793
794 Short remotePort = portBB.getShort();
795 IOFSwitch remoteSwitch = null;
796
797 // Verify this LLDP packet matches what we're looking for
798 for (LLDPTLV lldptlv : lldp.getOptionalTLVList()) {
799 if (lldptlv.getType() == 127 && lldptlv.getLength() == 12 &&
800 lldptlv.getValue()[0] == 0x0 && lldptlv.getValue()[1] == 0x26 &&
Ray Milkey269ffb92014-04-03 14:43:30 -0700801 lldptlv.getValue()[2] == (byte) 0xe1 && lldptlv.getValue()[3] == 0x0) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800802 ByteBuffer dpidBB = ByteBuffer.wrap(lldptlv.getValue());
803 remoteSwitch = floodlightProvider.getSwitches().get(dpidBB.getLong(4));
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800804 if (remoteSwitch == null) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700805 // Added by ONOS
806 // floodlight LLDP coming from a remote switch connected to a different controller
807 // add it to our cache of unconnected remote switches
808 remoteSwitch = addRemoteSwitch(dpidBB.getLong(4), remotePort);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800809 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700810 } else if (lldptlv.getType() == 12 && lldptlv.getLength() == 8) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800811 otherId = ByteBuffer.wrap(lldptlv.getValue()).getLong();
812 if (myId == otherId)
813 myLLDP = true;
814 } else if (lldptlv.getType() == TLV_DIRECTION_TYPE &&
815 lldptlv.getLength() == TLV_DIRECTION_LENGTH) {
816 if (lldptlv.getValue()[0] == TLV_DIRECTION_VALUE_FORWARD[0])
817 isReverse = false;
818 else if (lldptlv.getValue()[0] == TLV_DIRECTION_VALUE_REVERSE[0])
819 isReverse = true;
820 }
821 }
822
823 if (myLLDP == false) {
824 // This is not the LLDP sent by this controller.
825 // If the LLDP message has multicast bit set, then we need to broadcast
826 // the packet as a regular packet.
827 if (isStandard) {
828 if (log.isTraceEnabled()) {
829 log.trace("Getting standard LLDP from a different controller and quelching it.");
830 }
831 return Command.STOP;
Ray Milkey269ffb92014-04-03 14:43:30 -0700832 } else if (sw <= remoteSwitch.getId()) {
Teru Ub7246af2014-01-13 13:24:38 -0800833 if (log.isTraceEnabled()) {
834 log.trace("Getting BBDP from a different controller. myId {}: remoteId {}", myId, otherId);
835 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 -0700836 }
837 //XXX ONOS: Fix the BDDP broadcast issue
838 //return Command.CONTINUE;
839 return Command.STOP;
Teru Ub7246af2014-01-13 13:24:38 -0800840 }
841 /*
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800842 else if (myId < otherId) {
843 if (log.isTraceEnabled()) {
844 log.trace("Getting BDDP packets from a different controller" +
845 "and letting it go through normal processing chain.");
846 }
HIGUCHI Yuta30d03302013-06-14 13:47:36 -0700847 //XXX ONOS: Fix the BDDP broadcast issue
Jonathan Hart0b2c76a2013-02-27 17:09:33 -0800848 //return Command.CONTINUE;
849 return Command.STOP;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800850 }
Teru Ub7246af2014-01-13 13:24:38 -0800851 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800852 }
853
854
855 if (remoteSwitch == null) {
856 // Ignore LLDPs not generated by Floodlight, or from a switch that has recently
857 // disconnected, or from a switch connected to another Floodlight instance
858 if (log.isTraceEnabled()) {
859 log.trace("Received LLDP from remote switch not connected to the controller");
860 }
861 return Command.STOP;
862 }
863
864 if (!remoteSwitch.portEnabled(remotePort)) {
865 if (log.isTraceEnabled()) {
866 log.trace("Ignoring link with disabled source port: switch {} port {}", remoteSwitch, remotePort);
867 }
868 return Command.STOP;
869 }
870 if (suppressLinkDiscovery.contains(new NodePortTuple(remoteSwitch.getId(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700871 remotePort))) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800872 if (log.isTraceEnabled()) {
873 log.trace("Ignoring link with suppressed src port: switch {} port {}",
Ray Milkey269ffb92014-04-03 14:43:30 -0700874 remoteSwitch, remotePort);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800875 }
876 return Command.STOP;
877 }
878 if (!iofSwitch.portEnabled(pi.getInPort())) {
879 if (log.isTraceEnabled()) {
880 log.trace("Ignoring link with disabled dest port: switch {} port {}", sw, pi.getInPort());
881 }
882 return Command.STOP;
883 }
884
885 OFPhysicalPort physicalPort = remoteSwitch.getPort(remotePort);
886 int srcPortState = (physicalPort != null) ? physicalPort.getState() : 0;
887 physicalPort = iofSwitch.getPort(pi.getInPort());
888 int dstPortState = (physicalPort != null) ? physicalPort.getState() : 0;
889
890 // Store the time of update to this link, and push it out to routingEngine
891 Link lt = new Link(remoteSwitch.getId(), remotePort, iofSwitch.getId(), pi.getInPort());
892
893
894 Long lastLldpTime = null;
895 Long lastBddpTime = null;
896
897 Long firstSeenTime = System.currentTimeMillis();
898
899 if (isStandard)
900 lastLldpTime = System.currentTimeMillis();
901 else
902 lastBddpTime = System.currentTimeMillis();
903
904 LinkInfo newLinkInfo =
905 new LinkInfo(firstSeenTime, lastLldpTime, lastBddpTime,
Ray Milkey269ffb92014-04-03 14:43:30 -0700906 srcPortState, dstPortState);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800907
908 addOrUpdateLink(lt, newLinkInfo);
909
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800910 // Check if reverse link exists.
911 // If it doesn't exist and if the forward link was seen
912 // first seen within a small interval, send probe on the
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800913 // reverse link.
914
915 newLinkInfo = links.get(lt);
916 if (newLinkInfo != null && isStandard && isReverse == false) {
917 Link reverseLink = new Link(lt.getDst(), lt.getDstPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700918 lt.getSrc(), lt.getSrcPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800919 LinkInfo reverseInfo = links.get(reverseLink);
920 if (reverseInfo == null) {
921 // the reverse link does not exist.
922 if (newLinkInfo.getFirstSeenTime() > System.currentTimeMillis() - LINK_TIMEOUT) {
923 this.sendDiscoveryMessage(lt.getDst(), lt.getDstPort(), isStandard, true);
924 }
925 }
926 }
927
928 // If the received packet is a BDDP packet, then create a reverse BDDP
929 // link as well.
930 if (!isStandard) {
931 Link reverseLink = new Link(lt.getDst(), lt.getDstPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -0700932 lt.getSrc(), lt.getSrcPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800933
934 // srcPortState and dstPort state are reversed.
935 LinkInfo reverseInfo =
936 new LinkInfo(firstSeenTime, lastLldpTime, lastBddpTime,
Ray Milkey269ffb92014-04-03 14:43:30 -0700937 dstPortState, srcPortState);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800938
939 addOrUpdateLink(reverseLink, reverseInfo);
940 }
941
942 // Remove the node ports from the quarantine and maintenance queues.
943 NodePortTuple nptSrc = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
944 NodePortTuple nptDst = new NodePortTuple(lt.getDst(), lt.getDstPort());
945 removeFromQuarantineQueue(nptSrc);
946 removeFromMaintenanceQueue(nptSrc);
947 removeFromQuarantineQueue(nptDst);
948 removeFromMaintenanceQueue(nptDst);
949
950 // Consume this message
951 return Command.STOP;
952 }
953
954 protected Command handlePacketIn(long sw, OFPacketIn pi,
955 FloodlightContext cntx) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -0800956 Ethernet eth =
957 IFloodlightProviderService.bcStore.get(cntx,
Ray Milkey269ffb92014-04-03 14:43:30 -0700958 IFloodlightProviderService.CONTEXT_PI_PAYLOAD);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800959
Ray Milkey269ffb92014-04-03 14:43:30 -0700960 if (eth.getEtherType() == Ethernet.TYPE_BSN) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800961 BSN bsn = (BSN) eth.getPayload();
962 if (bsn == null) return Command.STOP;
963 if (bsn.getPayload() == null) return Command.STOP;
964 // It could be a packet other than BSN LLDP, therefore
965 // continue with the regular processing.
966 if (bsn.getPayload() instanceof LLDP == false)
967 return Command.CONTINUE;
Ubuntu9cbb4ca2013-02-07 17:19:59 +0000968 return handleLldp((LLDP) bsn.getPayload(), sw, pi, false, cntx);
Ray Milkey269ffb92014-04-03 14:43:30 -0700969 } else if (eth.getEtherType() == Ethernet.TYPE_LLDP) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800970 return handleLldp((LLDP) eth.getPayload(), sw, pi, true, cntx);
971 } else if (eth.getEtherType() < 1500) {
972 long destMac = eth.getDestinationMAC().toLong();
Ray Milkey269ffb92014-04-03 14:43:30 -0700973 if ((destMac & LINK_LOCAL_MASK) == LINK_LOCAL_VALUE) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800974 if (log.isTraceEnabled()) {
975 log.trace("Ignoring packet addressed to 802.1D/Q " +
976 "reserved address.");
977 }
978 return Command.STOP;
979 }
980 }
981
982 // If packet-in is from a quarantine port, stop processing.
983 NodePortTuple npt = new NodePortTuple(sw, pi.getInPort());
984 if (quarantineQueue.contains(npt)) return Command.STOP;
985
986 return Command.CONTINUE;
987 }
988
989 protected UpdateOperation getUpdateOperation(int srcPortState,
990 int dstPortState) {
991 boolean added =
992 (((srcPortState &
Ray Milkey269ffb92014-04-03 14:43:30 -0700993 OFPortState.OFPPS_STP_MASK.getValue()) !=
994 OFPortState.OFPPS_STP_BLOCK.getValue()) &&
995 ((dstPortState &
996 OFPortState.OFPPS_STP_MASK.getValue()) !=
997 OFPortState.OFPPS_STP_BLOCK.getValue()));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800998
999 if (added) return UpdateOperation.LINK_UPDATED;
1000 return UpdateOperation.LINK_REMOVED;
1001 }
1002
1003
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001004 protected UpdateOperation getUpdateOperation(int srcPortState) {
1005 boolean portUp = ((srcPortState &
1006 OFPortState.OFPPS_STP_MASK.getValue()) !=
1007 OFPortState.OFPPS_STP_BLOCK.getValue());
1008
1009 if (portUp) return UpdateOperation.PORT_UP;
1010 else return UpdateOperation.PORT_DOWN;
1011 }
1012
1013 protected boolean addOrUpdateLink(Link lt, LinkInfo newInfo) {
1014
1015 NodePortTuple srcNpt, dstNpt;
1016 boolean linkChanged = false;
1017
1018 lock.writeLock().lock();
1019 try {
1020 // put the new info. if an old info exists, it will be returned.
1021 LinkInfo oldInfo = links.put(lt, newInfo);
1022 if (oldInfo != null &&
1023 oldInfo.getFirstSeenTime() < newInfo.getFirstSeenTime())
1024 newInfo.setFirstSeenTime(oldInfo.getFirstSeenTime());
1025
1026 if (log.isTraceEnabled()) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001027 log.trace("addOrUpdateLink: {} {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001028 lt,
1029 (newInfo.getMulticastValidTime() != null) ? "multicast" : "unicast");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001030 }
1031
1032 UpdateOperation updateOperation = null;
1033 linkChanged = false;
1034
1035 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1036 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1037
1038 if (oldInfo == null) {
1039 // index it by switch source
1040 if (!switchLinks.containsKey(lt.getSrc()))
1041 switchLinks.put(lt.getSrc(), new HashSet<Link>());
1042 switchLinks.get(lt.getSrc()).add(lt);
1043
1044 // index it by switch dest
1045 if (!switchLinks.containsKey(lt.getDst()))
1046 switchLinks.put(lt.getDst(), new HashSet<Link>());
1047 switchLinks.get(lt.getDst()).add(lt);
1048
1049 // index both ends by switch:port
1050 if (!portLinks.containsKey(srcNpt))
1051 portLinks.put(srcNpt, new HashSet<Link>());
1052 portLinks.get(srcNpt).add(lt);
1053
1054 if (!portLinks.containsKey(dstNpt))
1055 portLinks.put(dstNpt, new HashSet<Link>());
1056 portLinks.get(dstNpt).add(lt);
1057
1058 // Add to portNOFLinks if the unicast valid time is null
1059 if (newInfo.getUnicastValidTime() == null)
1060 addLinkToBroadcastDomain(lt);
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001061
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001062 // ONOS: Distinguish added event separately from updated event
Pankaj Berdea41016d2013-06-10 21:18:18 -07001063 updateOperation = UpdateOperation.LINK_ADDED;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001064 linkChanged = true;
1065
1066 // Add to event history
1067 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001068 lt.getDst(),
1069 lt.getSrcPort(),
1070 lt.getDstPort(),
1071 newInfo.getSrcPortState(), newInfo.getDstPortState(),
1072 getLinkType(lt, newInfo),
1073 EvAction.LINK_ADDED, "LLDP Recvd");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001074 } else {
1075 // Since the link info is already there, we need to
1076 // update the right fields.
1077 if (newInfo.getUnicastValidTime() == null) {
1078 // This is due to a multicast LLDP, so copy the old unicast
1079 // value.
1080 if (oldInfo.getUnicastValidTime() != null) {
1081 newInfo.setUnicastValidTime(oldInfo.getUnicastValidTime());
1082 }
1083 } else if (newInfo.getMulticastValidTime() == null) {
1084 // This is due to a unicast LLDP, so copy the old multicast
1085 // value.
1086 if (oldInfo.getMulticastValidTime() != null) {
1087 newInfo.setMulticastValidTime(oldInfo.getMulticastValidTime());
1088 }
1089 }
1090
1091 Long oldTime = oldInfo.getUnicastValidTime();
1092 Long newTime = newInfo.getUnicastValidTime();
1093 // the link has changed its state between openflow and non-openflow
1094 // if the unicastValidTimes are null or not null
1095 if (oldTime != null & newTime == null) {
1096 // openflow -> non-openflow transition
1097 // we need to add the link tuple to the portNOFLinks
1098 addLinkToBroadcastDomain(lt);
1099 linkChanged = true;
1100 } else if (oldTime == null & newTime != null) {
1101 // non-openflow -> openflow transition
1102 // we need to remove the link from the portNOFLinks
1103 removeLinkFromBroadcastDomain(lt);
1104 linkChanged = true;
1105 }
1106
1107 // Only update the port states if they've changed
1108 if (newInfo.getSrcPortState().intValue() !=
1109 oldInfo.getSrcPortState().intValue() ||
1110 newInfo.getDstPortState().intValue() !=
Ray Milkey269ffb92014-04-03 14:43:30 -07001111 oldInfo.getDstPortState().intValue())
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001112 linkChanged = true;
1113
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001114 if (linkChanged) {
1115 updateOperation = getUpdateOperation(newInfo.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001116 newInfo.getDstPortState());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001117 if (log.isTraceEnabled()) {
1118 log.trace("Updated link {}", lt);
1119 }
1120 // Add to event history
1121 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001122 lt.getDst(),
1123 lt.getSrcPort(),
1124 lt.getDstPort(),
1125 newInfo.getSrcPortState(), newInfo.getDstPortState(),
1126 getLinkType(lt, newInfo),
1127 EvAction.LINK_PORT_STATE_UPDATED,
1128 "LLDP Recvd");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001129 }
1130 }
1131
1132 if (linkChanged) {
1133 // find out if the link was added or removed here.
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001134 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001135 lt.getDst(), lt.getDstPort(),
1136 getLinkType(lt, newInfo),
1137 updateOperation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001138 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001139 }
1140 } finally {
1141 lock.writeLock().unlock();
1142 }
1143
1144 return linkChanged;
1145 }
1146
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001147 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001148 public Map<Long, Set<Link>> getSwitchLinks() {
1149 return this.switchLinks;
1150 }
1151
1152 /**
1153 * Removes links from memory and storage.
Ray Milkey269ffb92014-04-03 14:43:30 -07001154 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001155 * @param links The List of @LinkTuple to delete.
1156 */
1157 protected void deleteLinks(List<Link> links, String reason) {
1158 NodePortTuple srcNpt, dstNpt;
1159
1160 lock.writeLock().lock();
1161 try {
1162 for (Link lt : links) {
1163 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
Ray Milkey269ffb92014-04-03 14:43:30 -07001164 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001165
1166 switchLinks.get(lt.getSrc()).remove(lt);
1167 switchLinks.get(lt.getDst()).remove(lt);
1168 if (switchLinks.containsKey(lt.getSrc()) &&
1169 switchLinks.get(lt.getSrc()).isEmpty())
1170 this.switchLinks.remove(lt.getSrc());
1171 if (this.switchLinks.containsKey(lt.getDst()) &&
1172 this.switchLinks.get(lt.getDst()).isEmpty())
1173 this.switchLinks.remove(lt.getDst());
1174
1175 if (this.portLinks.get(srcNpt) != null) {
1176 this.portLinks.get(srcNpt).remove(lt);
1177 if (this.portLinks.get(srcNpt).isEmpty())
1178 this.portLinks.remove(srcNpt);
1179 }
1180 if (this.portLinks.get(dstNpt) != null) {
1181 this.portLinks.get(dstNpt).remove(lt);
1182 if (this.portLinks.get(dstNpt).isEmpty())
1183 this.portLinks.remove(dstNpt);
1184 }
1185
1186 LinkInfo info = this.links.remove(lt);
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001187 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001188 lt.getDst(), lt.getDstPort(),
1189 getLinkType(lt, info),
1190 UpdateOperation.LINK_REMOVED));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001191 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001192
1193 // Update Event History
1194 evHistTopoLink(lt.getSrc(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001195 lt.getDst(),
1196 lt.getSrcPort(),
1197 lt.getDstPort(),
1198 0, 0, // Port states
1199 ILinkDiscovery.LinkType.INVALID_LINK,
1200 EvAction.LINK_DELETED, reason);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001201
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001202 // TODO Whenever link is removed, it has to checked if
1203 // the switchports must be added to quarantine.
1204
1205 if (log.isTraceEnabled()) {
1206 log.trace("Deleted link {}", lt);
1207 }
1208 }
1209 } finally {
1210 lock.writeLock().unlock();
1211 }
1212 }
1213
1214 /**
1215 * Handles an OFPortStatus message from a switch. We will add or
1216 * delete LinkTupes as well re-compute the topology if needed.
Ray Milkey269ffb92014-04-03 14:43:30 -07001217 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001218 * @param sw The IOFSwitch that sent the port status message
1219 * @param ps The OFPortStatus message
1220 * @return The Command to continue or stop after we process this message
1221 */
1222 protected Command handlePortStatus(long sw, OFPortStatus ps) {
1223
1224 IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
1225 if (iofSwitch == null) return Command.CONTINUE;
HIGUCHI Yutaa89b2842013-06-17 13:54:57 -07001226
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001227 // ONOS: If we do not control this switch, then we should not process its port status messages
Ray Milkey269ffb92014-04-03 14:43:30 -07001228 if (!registryService.hasControl(iofSwitch.getId()))
1229 return Command.CONTINUE;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001230
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001231 if (log.isTraceEnabled()) {
1232 log.trace("handlePortStatus: Switch {} port #{} reason {}; " +
1233 "config is {} state is {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001234 new Object[]{iofSwitch.getStringId(),
1235 ps.getDesc().getPortNumber(),
1236 ps.getReason(),
1237 ps.getDesc().getConfig(),
1238 ps.getDesc().getState()});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001239 }
1240
1241 short port = ps.getDesc().getPortNumber();
1242 NodePortTuple npt = new NodePortTuple(sw, port);
Ray Milkey269ffb92014-04-03 14:43:30 -07001243 boolean linkDeleted = false;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001244 boolean linkInfoChanged = false;
1245
1246 lock.writeLock().lock();
1247 try {
1248 // if ps is a delete, or a modify where the port is down or
1249 // configured down
Ray Milkey269ffb92014-04-03 14:43:30 -07001250 if ((byte) OFPortReason.OFPPR_DELETE.ordinal() == ps.getReason() ||
1251 ((byte) OFPortReason.OFPPR_MODIFY.ordinal() ==
1252 ps.getReason() && !portEnabled(ps.getDesc()))) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001253 deleteLinksOnPort(npt, "Port Status Changed");
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001254 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, UpdateOperation.PORT_DOWN));
1255 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001256 linkDeleted = true;
Ray Milkey269ffb92014-04-03 14:43:30 -07001257 } else if (ps.getReason() ==
1258 (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001259 // If ps is a port modification and the port state has changed
1260 // that affects links in the topology
1261
1262 if (this.portLinks.containsKey(npt)) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001263 for (Link lt : this.portLinks.get(npt)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001264 LinkInfo linkInfo = links.get(lt);
Ray Milkey269ffb92014-04-03 14:43:30 -07001265 assert (linkInfo != null);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001266 Integer updatedSrcPortState = null;
1267 Integer updatedDstPortState = null;
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001268 if (lt.getSrc() == npt.getNodeId() &&
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001269 lt.getSrcPort() == npt.getPortId() &&
1270 (linkInfo.getSrcPortState() !=
Ray Milkey269ffb92014-04-03 14:43:30 -07001271 ps.getDesc().getState())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001272 updatedSrcPortState = ps.getDesc().getState();
1273 linkInfo.setSrcPortState(updatedSrcPortState);
1274 }
1275 if (lt.getDst() == npt.getNodeId() &&
1276 lt.getDstPort() == npt.getPortId() &&
1277 (linkInfo.getDstPortState() !=
Ray Milkey269ffb92014-04-03 14:43:30 -07001278 ps.getDesc().getState())) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001279 updatedDstPortState = ps.getDesc().getState();
1280 linkInfo.setDstPortState(updatedDstPortState);
1281 }
1282 if ((updatedSrcPortState != null) ||
1283 (updatedDstPortState != null)) {
1284 // The link is already known to link discovery
1285 // manager and the status has changed, therefore
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001286 // send an LinkUpdate.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001287 UpdateOperation operation =
1288 getUpdateOperation(linkInfo.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001289 linkInfo.getDstPortState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001290 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001291 lt.getDst(), lt.getDstPort(),
1292 getLinkType(lt, linkInfo),
1293 operation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001294 controller.publishUpdate(update);
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001295
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001296 linkInfoChanged = true;
1297 }
1298 }
1299 }
1300
1301 UpdateOperation operation =
1302 getUpdateOperation(ps.getDesc().getState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001303 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, port, operation));
1304 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001305 }
1306
Ray Milkey269ffb92014-04-03 14:43:30 -07001307 if (!linkDeleted && !linkInfoChanged) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001308 if (log.isTraceEnabled()) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001309 log.trace("handlePortStatus: Switch {} port #{} reason {};" +
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001310 " no links to update/remove",
Ray Milkey269ffb92014-04-03 14:43:30 -07001311 new Object[]{HexString.toHexString(sw),
1312 ps.getDesc().getPortNumber(),
1313 ps.getReason()});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001314 }
1315 }
1316 } finally {
1317 lock.writeLock().unlock();
1318 }
1319
1320 if (!linkDeleted) {
1321 // Send LLDP right away when port state is changed for faster
1322 // cluster-merge. If it is a link delete then there is not need
1323 // to send the LLDPs right away and instead we wait for the LLDPs
1324 // to be sent on the timer as it is normally done
1325 // do it outside the write-lock
1326 // sendLLDPTask.reschedule(1000, TimeUnit.MILLISECONDS);
1327 processNewPort(npt.getNodeId(), npt.getPortId());
1328 }
1329 return Command.CONTINUE;
1330 }
1331
1332 /**
1333 * Process a new port.
1334 * If link discovery is disabled on the port, then do nothing.
1335 * If autoportfast feature is enabled and the port is a fast port, then
1336 * do nothing.
1337 * Otherwise, send LLDP message. Add the port to quarantine.
Ray Milkey269ffb92014-04-03 14:43:30 -07001338 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001339 * @param sw
1340 * @param p
1341 */
1342 private void processNewPort(long sw, short p) {
1343 if (isLinkDiscoverySuppressed(sw, p)) {
1344 // Do nothing as link discovery is suppressed.
Ray Milkey1aa71f82014-04-08 16:23:24 -07001345 return;
Ray Milkey269ffb92014-04-03 14:43:30 -07001346 } else if (autoPortFastFeature && isFastPort(sw, p)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001347 // Do nothing as the port is a fast port.
Ray Milkey1aa71f82014-04-08 16:23:24 -07001348 return;
Ray Milkey269ffb92014-04-03 14:43:30 -07001349 } else {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001350 NodePortTuple npt = new NodePortTuple(sw, p);
1351 discover(sw, p);
1352 // if it is not a fast port, add it to quarantine.
1353 if (!isFastPort(sw, p)) {
1354 addToQuarantineQueue(npt);
1355 } else {
1356 // Add to maintenance queue to ensure that BDDP packets
1357 // are sent out.
1358 addToMaintenanceQueue(npt);
1359 }
1360 }
1361 }
1362
1363 /**
1364 * We send out LLDP messages when a switch is added to discover the topology
Ray Milkey269ffb92014-04-03 14:43:30 -07001365 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001366 * @param sw The IOFSwitch that connected to the controller
1367 */
1368 @Override
1369 public void addedSwitch(IOFSwitch sw) {
1370
1371 if (sw.getEnabledPorts() != null) {
1372 for (Short p : sw.getEnabledPortNumbers()) {
1373 processNewPort(sw.getId(), p);
1374 }
1375 }
1376 // Update event history
1377 evHistTopoSwitch(sw, EvAction.SWITCH_CONNECTED, "None");
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001378 LinkUpdate update = new LinkUpdate(new LDUpdate(sw.getId(), null,
Ray Milkey269ffb92014-04-03 14:43:30 -07001379 UpdateOperation.SWITCH_UPDATED));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001380 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001381 }
1382
1383 /**
1384 * When a switch disconnects we remove any links from our map and notify.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001385 */
1386 @Override
1387 public void removedSwitch(IOFSwitch iofSwitch) {
1388 // Update event history
1389 long sw = iofSwitch.getId();
1390 evHistTopoSwitch(iofSwitch, EvAction.SWITCH_DISCONNECTED, "None");
1391 List<Link> eraseList = new ArrayList<Link>();
1392 lock.writeLock().lock();
1393 try {
1394 if (switchLinks.containsKey(sw)) {
1395 if (log.isTraceEnabled()) {
1396 log.trace("Handle switchRemoved. Switch {}; removing links {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001397 HexString.toHexString(sw), switchLinks.get(sw));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001398 }
1399 // add all tuples with an endpoint on this switch to erase list
1400 eraseList.addAll(switchLinks.get(sw));
HIGUCHI Yutaa89b2842013-06-17 13:54:57 -07001401 deleteLinks(eraseList, "Switch Removed");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001402
1403 // Send a switch removed update
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001404 LinkUpdate update = new LinkUpdate(new LDUpdate(sw, null, UpdateOperation.SWITCH_REMOVED));
1405 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001406 }
1407 } finally {
1408 lock.writeLock().unlock();
1409 }
1410 }
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001411
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001412 /**
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001413 * We don't react the port changed notifications here. we listen for
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001414 * OFPortStatus messages directly. Might consider using this notifier
1415 * instead
1416 */
1417 @Override
1418 public void switchPortChanged(Long switchId) {
1419 // no-op
1420 }
1421
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001422 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001423 * Delete links incident on a given switch port.
Ray Milkey269ffb92014-04-03 14:43:30 -07001424 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001425 * @param npt
1426 * @param reason
1427 */
1428 protected void deleteLinksOnPort(NodePortTuple npt, String reason) {
1429 List<Link> eraseList = new ArrayList<Link>();
1430 if (this.portLinks.containsKey(npt)) {
1431 if (log.isTraceEnabled()) {
1432 log.trace("handlePortStatus: Switch {} port #{} " +
1433 "removing links {}",
Ray Milkey269ffb92014-04-03 14:43:30 -07001434 new Object[]{HexString.toHexString(npt.getNodeId()),
1435 npt.getPortId(),
1436 this.portLinks.get(npt)});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001437 }
1438 eraseList.addAll(this.portLinks.get(npt));
1439 deleteLinks(eraseList, reason);
1440 }
1441 }
1442
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001443 /**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001444 * Iterates through the list of links and deletes if the
1445 * last discovery message reception time exceeds timeout values.
1446 */
1447 protected void timeoutLinks() {
1448 List<Link> eraseList = new ArrayList<Link>();
1449 Long curTime = System.currentTimeMillis();
1450 boolean linkChanged = false;
1451
1452 // reentrant required here because deleteLink also write locks
1453 lock.writeLock().lock();
1454 try {
1455 Iterator<Entry<Link, LinkInfo>> it =
1456 this.links.entrySet().iterator();
1457 while (it.hasNext()) {
1458 Entry<Link, LinkInfo> entry = it.next();
1459 Link lt = entry.getKey();
1460 LinkInfo info = entry.getValue();
1461
1462 // Timeout the unicast and multicast LLDP valid times
1463 // independently.
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001464 if ((info.getUnicastValidTime() != null) &&
Ray Milkey269ffb92014-04-03 14:43:30 -07001465 (info.getUnicastValidTime() + (this.LINK_TIMEOUT * 1000) < curTime)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001466 info.setUnicastValidTime(null);
1467
1468 if (info.getMulticastValidTime() != null)
1469 addLinkToBroadcastDomain(lt);
1470 // Note that even if mTime becomes null later on,
1471 // the link would be deleted, which would trigger updateClusters().
1472 linkChanged = true;
1473 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001474 if ((info.getMulticastValidTime() != null) &&
1475 (info.getMulticastValidTime() + (this.LINK_TIMEOUT * 1000) < curTime)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001476 info.setMulticastValidTime(null);
1477 // if uTime is not null, then link will remain as openflow
1478 // link. If uTime is null, it will be deleted. So, we
1479 // don't care about linkChanged flag here.
1480 removeLinkFromBroadcastDomain(lt);
1481 linkChanged = true;
1482 }
1483 // Add to the erase list only if the unicast
1484 // time is null.
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001485 if (info.getUnicastValidTime() == null &&
Ray Milkey269ffb92014-04-03 14:43:30 -07001486 info.getMulticastValidTime() == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001487 eraseList.add(entry.getKey());
1488 } else if (linkChanged) {
1489 UpdateOperation operation;
1490 operation = getUpdateOperation(info.getSrcPortState(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001491 info.getDstPortState());
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001492 LinkUpdate update = new LinkUpdate(new LDUpdate(lt.getSrc(), lt.getSrcPort(),
Ray Milkey269ffb92014-04-03 14:43:30 -07001493 lt.getDst(), lt.getDstPort(),
1494 getLinkType(lt, info),
1495 operation));
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001496 controller.publishUpdate(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001497 }
1498 }
1499
1500 // if any link was deleted or any link was changed.
1501 if ((eraseList.size() > 0) || linkChanged) {
1502 deleteLinks(eraseList, "LLDP timeout");
1503 }
1504 } finally {
1505 lock.writeLock().unlock();
1506 }
1507 }
1508
1509 private boolean portEnabled(OFPhysicalPort port) {
1510 if (port == null)
1511 return false;
1512 if ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0)
1513 return false;
1514 if ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)
1515 return false;
1516 // Port STP state doesn't work with multiple VLANs, so ignore it for now
1517 // if ((port.getState() & OFPortState.OFPPS_STP_MASK.getValue()) == OFPortState.OFPPS_STP_BLOCK.getValue())
1518 // return false;
1519 return true;
1520 }
1521
1522 public Map<NodePortTuple, Set<Link>> getPortBroadcastDomainLinks() {
1523 return portBroadcastDomainLinks;
1524 }
1525
1526 @Override
1527 public Map<Link, LinkInfo> getLinks() {
1528 lock.readLock().lock();
1529 Map<Link, LinkInfo> result;
1530 try {
1531 result = new HashMap<Link, LinkInfo>(links);
1532 } finally {
1533 lock.readLock().unlock();
1534 }
1535 return result;
1536 }
1537
1538 protected void addLinkToBroadcastDomain(Link lt) {
1539
1540 NodePortTuple srcNpt, dstNpt;
1541 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1542 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1543
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001544 if (!portBroadcastDomainLinks.containsKey(srcNpt))
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001545 portBroadcastDomainLinks.put(srcNpt, new HashSet<Link>());
1546 portBroadcastDomainLinks.get(srcNpt).add(lt);
1547
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001548 if (!portBroadcastDomainLinks.containsKey(dstNpt))
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001549 portBroadcastDomainLinks.put(dstNpt, new HashSet<Link>());
1550 portBroadcastDomainLinks.get(dstNpt).add(lt);
1551 }
1552
1553 protected void removeLinkFromBroadcastDomain(Link lt) {
1554
1555 NodePortTuple srcNpt, dstNpt;
1556 srcNpt = new NodePortTuple(lt.getSrc(), lt.getSrcPort());
1557 dstNpt = new NodePortTuple(lt.getDst(), lt.getDstPort());
1558
1559 if (portBroadcastDomainLinks.containsKey(srcNpt)) {
1560 portBroadcastDomainLinks.get(srcNpt).remove(lt);
1561 if (portBroadcastDomainLinks.get(srcNpt).isEmpty())
1562 portBroadcastDomainLinks.remove(srcNpt);
1563 }
1564
1565 if (portBroadcastDomainLinks.containsKey(dstNpt)) {
1566 portBroadcastDomainLinks.get(dstNpt).remove(lt);
1567 if (portBroadcastDomainLinks.get(dstNpt).isEmpty())
1568 portBroadcastDomainLinks.remove(dstNpt);
1569 }
1570 }
1571
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001572 @Override
1573 public void addListener(ILinkDiscoveryListener listener) {
1574 linkDiscoveryAware.add(listener);
1575 }
1576
1577 /**
1578 * Register a link discovery aware component
Ray Milkey269ffb92014-04-03 14:43:30 -07001579 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001580 * @param linkDiscoveryAwareComponent
1581 */
1582 public void addLinkDiscoveryAware(ILinkDiscoveryListener linkDiscoveryAwareComponent) {
1583 // TODO make this a copy on write set or lock it somehow
1584 this.linkDiscoveryAware.add(linkDiscoveryAwareComponent);
1585 }
1586
1587 /**
1588 * Deregister a link discovery aware component
Ray Milkey269ffb92014-04-03 14:43:30 -07001589 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001590 * @param linkDiscoveryAwareComponent
1591 */
1592 public void removeLinkDiscoveryAware(ILinkDiscoveryListener linkDiscoveryAwareComponent) {
1593 // TODO make this a copy on write set or lock it somehow
1594 this.linkDiscoveryAware.remove(linkDiscoveryAwareComponent);
1595 }
1596
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001597 @Override
1598 public boolean isCallbackOrderingPrereq(OFType type, String name) {
1599 return false;
1600 }
1601
1602 @Override
1603 public boolean isCallbackOrderingPostreq(OFType type, String name) {
1604 return false;
1605 }
1606
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001607 // IFloodlightModule classes
1608
1609 @Override
1610 public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001611 Collection<Class<? extends IFloodlightService>> l =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001612 new ArrayList<Class<? extends IFloodlightService>>();
1613 l.add(ILinkDiscoveryService.class);
1614 //l.add(ITopologyService.class);
1615 return l;
1616 }
1617
1618 @Override
1619 public Map<Class<? extends IFloodlightService>, IFloodlightService>
1620 getServiceImpls() {
1621 Map<Class<? extends IFloodlightService>,
Ray Milkey269ffb92014-04-03 14:43:30 -07001622 IFloodlightService> m =
1623 new HashMap<Class<? extends IFloodlightService>,
1624 IFloodlightService>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001625 // We are the class that implements the service
1626 m.put(ILinkDiscoveryService.class, this);
1627 return m;
1628 }
1629
1630 @Override
1631 public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001632 Collection<Class<? extends IFloodlightService>> l =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001633 new ArrayList<Class<? extends IFloodlightService>>();
1634 l.add(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001635 l.add(IThreadPoolService.class);
1636 l.add(IRestApiService.class);
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001637 // Added by ONOS
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001638 l.add(IControllerRegistryService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001639 return l;
1640 }
1641
1642 @Override
1643 public void init(FloodlightModuleContext context)
1644 throws FloodlightModuleException {
1645 floodlightProvider = context.getServiceImpl(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001646 threadPool = context.getServiceImpl(IThreadPoolService.class);
1647 restApi = context.getServiceImpl(IRestApiService.class);
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001648 // Added by ONOS
Umesh Krishnaswamy57a32a92013-03-21 14:21:15 -07001649 registryService = context.getServiceImpl(IControllerRegistryService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001650
1651 // Set the autoportfast feature to false.
1652 this.autoPortFastFeature = false;
1653
1654 // We create this here because there is no ordering guarantee
1655 this.linkDiscoveryAware = new ArrayList<ILinkDiscoveryListener>();
1656 this.lock = new ReentrantReadWriteLock();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001657 this.links = new HashMap<Link, LinkInfo>();
1658 this.portLinks = new HashMap<NodePortTuple, Set<Link>>();
1659 this.suppressLinkDiscovery =
1660 Collections.synchronizedSet(new HashSet<NodePortTuple>());
1661 this.portBroadcastDomainLinks = new HashMap<NodePortTuple, Set<Link>>();
1662 this.switchLinks = new HashMap<Long, Set<Link>>();
1663 this.quarantineQueue = new LinkedBlockingQueue<NodePortTuple>();
1664 this.maintenanceQueue = new LinkedBlockingQueue<NodePortTuple>();
HIGUCHI Yuta30d03302013-06-14 13:47:36 -07001665 // Added by ONOS
HIGUCHI Yuta7677a6f2013-06-14 14:13:35 -07001666 this.remoteSwitches = new HashMap<Long, IOnosRemoteSwitch>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001667
1668 this.evHistTopologySwitch =
1669 new EventHistory<EventHistoryTopologySwitch>("Topology: Switch");
1670 this.evHistTopologyLink =
1671 new EventHistory<EventHistoryTopologyLink>("Topology: Link");
1672 this.evHistTopologyCluster =
1673 new EventHistory<EventHistoryTopologyCluster>("Topology: Cluster");
1674 }
1675
1676 @Override
1677 @LogMessageDocs({
Ray Milkey269ffb92014-04-03 14:43:30 -07001678 @LogMessageDoc(level = "ERROR",
1679 message = "No storage source found.",
1680 explanation = "Storage source was not initialized; cannot initialize " +
1681 "link discovery.",
1682 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1683 @LogMessageDoc(level = "ERROR",
1684 message = "Error in installing listener for " +
1685 "switch config table {table}",
1686 explanation = "Failed to install storage notification for the " +
1687 "switch config table",
1688 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1689 @LogMessageDoc(level = "ERROR",
1690 message = "No storage source found.",
1691 explanation = "Storage source was not initialized; cannot initialize " +
1692 "link discovery.",
1693 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG),
1694 @LogMessageDoc(level = "ERROR",
1695 message = "Exception in LLDP send timer.",
1696 explanation = "An unknown error occured while sending LLDP " +
1697 "messages to switches.",
1698 recommendation = LogMessageDoc.CHECK_SWITCH)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001699 })
1700 public void startUp(FloodlightModuleContext context) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001701 ScheduledExecutorService ses = threadPool.getScheduledExecutor();
Pankaj Berdedc73bb12013-08-14 13:46:38 -07001702 controller =
1703 context.getServiceImpl(IFloodlightProviderService.class);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001704
1705 // To be started by the first switch connection
1706 discoveryTask = new SingletonTask(ses, new Runnable() {
1707 @Override
1708 public void run() {
1709 try {
1710 discoverLinks();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001711 } catch (Exception e) {
1712 log.error("Exception in LLDP send timer.", e);
1713 } finally {
1714 if (!shuttingDown) {
Ray Milkey269ffb92014-04-03 14:43:30 -07001715 // Always reschedule link discovery if we're not
1716 // shutting down (no chance of SLAVE role now)
Jonathan Hartec4f14e2013-12-12 10:46:38 -08001717 log.trace("Rescheduling discovery task");
1718 discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL,
Ray Milkey269ffb92014-04-03 14:43:30 -07001719 TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001720 }
1721 }
1722 }
1723 });
1724
Jonathan Hartec4f14e2013-12-12 10:46:38 -08001725 // Always reschedule link discovery as we are never in SLAVE role now
1726 discoveryTask.reschedule(DISCOVERY_TASK_INTERVAL, TimeUnit.SECONDS);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001727
1728 // Setup the BDDP task. It is invoked whenever switch port tuples
1729 // are added to the quarantine list.
1730 bddpTask = new SingletonTask(ses, new QuarantineWorker());
1731 bddpTask.reschedule(BDDP_TASK_INTERVAL, TimeUnit.MILLISECONDS);
1732
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001733
1734 // Register for the OpenFlow messages we want to receive
1735 floodlightProvider.addOFMessageListener(OFType.PACKET_IN, this);
1736 floodlightProvider.addOFMessageListener(OFType.PORT_STATUS, this);
1737 // Register for switch updates
1738 floodlightProvider.addOFSwitchListener(this);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001739 if (restApi != null)
1740 restApi.addRestletRoutable(new LinkDiscoveryWebRoutable());
1741 setControllerTLV();
1742 }
1743
1744 // ****************************************************
1745 // Topology Manager's Event History members and methods
1746 // ****************************************************
1747
1748 // Topology Manager event history
Ray Milkey269ffb92014-04-03 14:43:30 -07001749 public EventHistory<EventHistoryTopologySwitch> evHistTopologySwitch;
1750 public EventHistory<EventHistoryTopologyLink> evHistTopologyLink;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001751 public EventHistory<EventHistoryTopologyCluster> evHistTopologyCluster;
Ray Milkey269ffb92014-04-03 14:43:30 -07001752 public EventHistoryTopologySwitch evTopoSwitch;
1753 public EventHistoryTopologyLink evTopoLink;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001754 public EventHistoryTopologyCluster evTopoCluster;
1755
1756 // Switch Added/Deleted
1757 private void evHistTopoSwitch(IOFSwitch sw, EvAction actn, String reason) {
1758 if (evTopoSwitch == null) {
1759 evTopoSwitch = new EventHistoryTopologySwitch();
1760 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001761 evTopoSwitch.dpid = sw.getId();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001762 if ((sw.getChannel() != null) &&
1763 (SocketAddress.class.isInstance(
Ray Milkey269ffb92014-04-03 14:43:30 -07001764 sw.getChannel().getRemoteAddress()))) {
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001765 evTopoSwitch.ipv4Addr =
Ray Milkey269ffb92014-04-03 14:43:30 -07001766 IPv4.toIPv4Address(((InetSocketAddress) (sw.getChannel().
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001767 getRemoteAddress())).getAddress().getAddress());
Ray Milkey269ffb92014-04-03 14:43:30 -07001768 evTopoSwitch.l4Port =
1769 ((InetSocketAddress) (sw.getChannel().
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001770 getRemoteAddress())).getPort();
1771 } else {
1772 evTopoSwitch.ipv4Addr = 0;
1773 evTopoSwitch.l4Port = 0;
1774 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001775 evTopoSwitch.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001776 evTopoSwitch = evHistTopologySwitch.put(evTopoSwitch, actn);
1777 }
1778
1779 private void evHistTopoLink(long srcDpid, long dstDpid, short srcPort,
1780 short dstPort, int srcPortState, int dstPortState,
1781 ILinkDiscovery.LinkType linkType,
1782 EvAction actn, String reason) {
1783 if (evTopoLink == null) {
1784 evTopoLink = new EventHistoryTopologyLink();
1785 }
1786 evTopoLink.srcSwDpid = srcDpid;
1787 evTopoLink.dstSwDpid = dstDpid;
1788 evTopoLink.srcSwport = srcPort & 0xffff;
1789 evTopoLink.dstSwport = dstPort & 0xffff;
1790 evTopoLink.srcPortState = srcPortState;
1791 evTopoLink.dstPortState = dstPortState;
Ray Milkey269ffb92014-04-03 14:43:30 -07001792 evTopoLink.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001793 switch (linkType) {
1794 case DIRECT_LINK:
1795 evTopoLink.linkType = "DIRECT_LINK";
1796 break;
1797 case MULTIHOP_LINK:
1798 evTopoLink.linkType = "MULTIHOP_LINK";
1799 break;
1800 case TUNNEL:
1801 evTopoLink.linkType = "TUNNEL";
1802 break;
1803 case INVALID_LINK:
1804 default:
1805 evTopoLink.linkType = "Unknown";
1806 break;
1807 }
1808 evTopoLink = evHistTopologyLink.put(evTopoLink, actn);
1809 }
1810
1811 public void evHistTopoCluster(long dpid, long clusterIdOld,
1812 long clusterIdNew, EvAction action, String reason) {
1813 if (evTopoCluster == null) {
1814 evTopoCluster = new EventHistoryTopologyCluster();
1815 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001816 evTopoCluster.dpid = dpid;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001817 evTopoCluster.clusterIdOld = clusterIdOld;
1818 evTopoCluster.clusterIdNew = clusterIdNew;
Ray Milkey269ffb92014-04-03 14:43:30 -07001819 evTopoCluster.reason = reason;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001820 evTopoCluster = evHistTopologyCluster.put(evTopoCluster, action);
1821 }
1822
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001823 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001824 public boolean isAutoPortFastFeature() {
1825 return autoPortFastFeature;
1826 }
1827
Yuta HIGUCHIe8813402014-01-08 13:36:50 -08001828 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001829 public void setAutoPortFastFeature(boolean autoPortFastFeature) {
1830 this.autoPortFastFeature = autoPortFastFeature;
1831 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001832}