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