2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
11 import java.io.IOException;
12 import java.net.SocketException;
13 import java.nio.channels.AsynchronousCloseException;
14 import java.nio.channels.ClosedSelectorException;
15 import java.nio.channels.SelectionKey;
16 import java.nio.channels.Selector;
17 import java.nio.channels.SocketChannel;
18 import java.nio.channels.spi.SelectorProvider;
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
27 import java.util.Timer;
28 import java.util.TimerTask;
29 import java.util.concurrent.Callable;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.Future;
34 import java.util.concurrent.PriorityBlockingQueue;
35 import java.util.concurrent.TimeUnit;
36 import java.util.concurrent.atomic.AtomicInteger;
38 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
39 import org.opendaylight.controller.protocol_plugin.openflow.core.ISwitch;
40 import org.opendaylight.controller.protocol_plugin.openflow.core.IMessageReadWrite;
41 import org.openflow.protocol.OFBarrierReply;
42 import org.openflow.protocol.OFBarrierRequest;
43 import org.openflow.protocol.OFEchoReply;
44 import org.openflow.protocol.OFError;
45 import org.openflow.protocol.OFFeaturesReply;
46 import org.openflow.protocol.OFFlowMod;
47 import org.openflow.protocol.OFGetConfigReply;
48 import org.openflow.protocol.OFMatch;
49 import org.openflow.protocol.OFMessage;
50 import org.openflow.protocol.OFPhysicalPort;
51 import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
52 import org.openflow.protocol.OFPhysicalPort.OFPortFeatures;
53 import org.openflow.protocol.OFPhysicalPort.OFPortState;
54 import org.openflow.protocol.OFPort;
55 import org.openflow.protocol.OFPortStatus;
56 import org.openflow.protocol.OFPortStatus.OFPortReason;
57 import org.openflow.protocol.OFSetConfig;
58 import org.openflow.protocol.OFStatisticsReply;
59 import org.openflow.protocol.OFStatisticsRequest;
60 import org.openflow.protocol.OFType;
61 import org.openflow.protocol.factory.BasicFactory;
62 import org.openflow.util.HexString;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 public class SwitchHandler implements ISwitch {
67 private static final Logger logger = LoggerFactory
68 .getLogger(SwitchHandler.class);
69 private static final int SWITCH_LIVENESS_TIMER = 5000;
70 private static final int switchLivenessTimeout = getSwitchLivenessTimeout();
71 private int MESSAGE_RESPONSE_TIMER = 2000;
73 private String instanceName;
74 private ISwitch thisISwitch;
75 private IController core;
77 private Integer buffers;
78 private Integer capabilities;
80 private Integer actions;
81 private Selector selector;
82 private SocketChannel socket;
83 private BasicFactory factory;
84 private AtomicInteger xid;
85 private SwitchState state;
86 private Timer periodicTimer;
87 private Map<Short, OFPhysicalPort> physicalPorts;
88 private Map<Short, Integer> portBandwidth;
89 private Date connectedDate;
90 private Long lastMsgReceivedTimeStamp;
91 private Boolean probeSent;
92 private ExecutorService executor;
93 private ConcurrentHashMap<Integer, Callable<Object>> messageWaitingDone;
94 private boolean running;
95 private IMessageReadWrite msgReadWriteService;
96 private Thread switchHandlerThread;
97 private Integer responseTimerValue;
98 private PriorityBlockingQueue<PriorityMessage> transmitQ;
99 private Thread transmitThread;
101 private enum SwitchState {
102 NON_OPERATIONAL(0), WAIT_FEATURES_REPLY(1), WAIT_CONFIG_REPLY(2), OPERATIONAL(
107 private SwitchState(int value) {
111 @SuppressWarnings("unused")
117 public SwitchHandler(Controller core, SocketChannel sc, String name) {
118 this.instanceName = name;
119 this.thisISwitch = this;
121 this.buffers = (int) 0;
122 this.capabilities = (int) 0;
123 this.tables = (byte) 0;
124 this.actions = (int) 0;
127 this.factory = new BasicFactory();
128 this.connectedDate = new Date();
129 this.lastMsgReceivedTimeStamp = connectedDate.getTime();
130 this.physicalPorts = new HashMap<Short, OFPhysicalPort>();
131 this.portBandwidth = new HashMap<Short, Integer>();
132 this.state = SwitchState.NON_OPERATIONAL;
133 this.probeSent = false;
134 this.xid = new AtomicInteger(this.socket.hashCode());
135 this.periodicTimer = null;
136 this.executor = Executors.newFixedThreadPool(4);
137 this.messageWaitingDone = new ConcurrentHashMap<Integer, Callable<Object>>();
138 this.responseTimerValue = MESSAGE_RESPONSE_TIMER;
139 String rTimer = System.getProperty("of.messageResponseTimer");
140 if (rTimer != null) {
142 responseTimerValue = Integer.decode(rTimer);
143 } catch (NumberFormatException e) {
145 "Invalid of.messageResponseTimer: {} use default({})",
146 rTimer, MESSAGE_RESPONSE_TIMER);
151 public void start() {
153 startTransmitThread();
156 startHandlerThread();
157 } catch (Exception e) {
162 private void startHandlerThread() {
163 switchHandlerThread = new Thread(new Runnable() {
169 // wait for an incoming connection
171 Iterator<SelectionKey> selectedKeys = selector
172 .selectedKeys().iterator();
173 while (selectedKeys.hasNext()) {
174 SelectionKey skey = selectedKeys.next();
175 selectedKeys.remove();
176 if (skey.isValid() && skey.isWritable()) {
179 if (skey.isValid() && skey.isReadable()) {
183 } catch (Exception e) {
189 switchHandlerThread.start();
198 } catch (Exception e) {
202 } catch (Exception e) {
205 msgReadWriteService.stop();
206 } catch (Exception e) {
210 msgReadWriteService = null;
212 if (switchHandlerThread != null) {
213 switchHandlerThread.interrupt();
215 if (transmitThread != null) {
216 transmitThread.interrupt();
221 public int getNextXid() {
222 return this.xid.incrementAndGet();
226 * This method puts the message in an outgoing priority queue with normal
227 * priority. It will be served after high priority messages. The method
228 * should be used for non-critical messages such as statistics request,
229 * discovery packets, etc. An unique XID is generated automatically and
230 * inserted into the message.
233 * The OF message to be sent
234 * @return The XID used
237 public Integer asyncSend(OFMessage msg) {
238 return asyncSend(msg, getNextXid());
241 private Object syncSend(OFMessage msg, int xid) {
242 return syncMessageInternal(msg, xid, true);
246 * This method puts the message in an outgoing priority queue with normal
247 * priority. It will be served after high priority messages. The method
248 * should be used for non-critical messages such as statistics request,
249 * discovery packets, etc. The specified XID is inserted into the message.
252 * The OF message to be Sent
254 * The XID to be used in the message
255 * @return The XID used
258 public Integer asyncSend(OFMessage msg, int xid) {
260 if (transmitQ != null) {
261 transmitQ.add(new PriorityMessage(msg, 0));
267 * This method puts the message in an outgoing priority queue with high
268 * priority. It will be served first before normal priority messages. The
269 * method should be used for critical messages such as hello, echo reply
270 * etc. An unique XID is generated automatically and inserted into the
274 * The OF message to be sent
275 * @return The XID used
278 public Integer asyncFastSend(OFMessage msg) {
279 return asyncFastSend(msg, getNextXid());
283 * This method puts the message in an outgoing priority queue with high
284 * priority. It will be served first before normal priority messages. The
285 * method should be used for critical messages such as hello, echo reply
286 * etc. The specified XID is inserted into the message.
289 * The OF message to be sent
290 * @return The XID used
293 public Integer asyncFastSend(OFMessage msg, int xid) {
295 if (transmitQ != null) {
296 transmitQ.add(new PriorityMessage(msg, 1));
301 public void resumeSend() {
303 if (msgReadWriteService != null) {
304 msgReadWriteService.resumeSend();
306 } catch (Exception e) {
311 public void handleMessages() {
312 List<OFMessage> msgs = null;
315 if (msgReadWriteService != null) {
316 msgs = msgReadWriteService.readMessages();
318 } catch (Exception e) {
323 logger.debug("{} is down", toString());
324 // the connection is down, inform core
325 reportSwitchStateChange(false);
328 for (OFMessage msg : msgs) {
329 logger.trace("Message received: {}", msg.toString());
330 this.lastMsgReceivedTimeStamp = System.currentTimeMillis();
331 OFType type = msg.getType();
334 // send feature request
335 OFMessage featureRequest = factory
336 .getMessage(OFType.FEATURES_REQUEST);
337 asyncFastSend(featureRequest);
338 // delete all pre-existing flows
339 OFMatch match = new OFMatch().setWildcards(OFMatch.OFPFW_ALL);
340 OFFlowMod flowMod = (OFFlowMod) factory
341 .getMessage(OFType.FLOW_MOD);
342 flowMod.setMatch(match).setCommand(OFFlowMod.OFPFC_DELETE)
343 .setOutPort(OFPort.OFPP_NONE)
344 .setLength((short) OFFlowMod.MINIMUM_LENGTH);
345 asyncFastSend(flowMod);
346 this.state = SwitchState.WAIT_FEATURES_REPLY;
350 OFEchoReply echoReply = (OFEchoReply) factory
351 .getMessage(OFType.ECHO_REPLY);
352 asyncFastSend(echoReply);
355 this.probeSent = false;
358 processFeaturesReply((OFFeaturesReply) msg);
360 case GET_CONFIG_REPLY:
361 // make sure that the switch can send the whole packet to the
363 if (((OFGetConfigReply) msg).getMissSendLength() == (short) 0xffff) {
364 this.state = SwitchState.OPERATIONAL;
368 processBarrierReply((OFBarrierReply) msg);
371 processErrorReply((OFError) msg);
374 processPortStatusMsg((OFPortStatus) msg);
377 processStatsReply((OFStatisticsReply) msg);
384 if (isOperational()) {
385 ((Controller) core).takeSwitchEventMsg(thisISwitch, msg);
390 private void processPortStatusMsg(OFPortStatus msg) {
391 OFPhysicalPort port = msg.getDesc();
392 if (msg.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) {
393 updatePhysicalPort(port);
394 } else if (msg.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) {
395 updatePhysicalPort(port);
396 } else if (msg.getReason() == (byte) OFPortReason.OFPPR_DELETE
398 deletePhysicalPort(port);
403 private void startSwitchTimer() {
404 this.periodicTimer = new Timer();
405 this.periodicTimer.scheduleAtFixedRate(new TimerTask() {
409 Long now = System.currentTimeMillis();
410 if ((now - lastMsgReceivedTimeStamp) > switchLivenessTimeout) {
412 // switch failed to respond to our probe, consider
414 logger.warn("{} is idle for too long, disconnect",
416 reportSwitchStateChange(false);
418 // send a probe to see if the switch is still alive
420 "Send idle probe (Echo Request) to {}",
423 OFMessage echo = factory
424 .getMessage(OFType.ECHO_REQUEST);
428 if (state == SwitchState.WAIT_FEATURES_REPLY) {
429 // send another features request
430 OFMessage request = factory
431 .getMessage(OFType.FEATURES_REQUEST);
432 asyncFastSend(request);
434 if (state == SwitchState.WAIT_CONFIG_REPLY) {
435 // send another config request
436 OFSetConfig config = (OFSetConfig) factory
437 .getMessage(OFType.SET_CONFIG);
438 config.setMissSendLength((short) 0xffff)
439 .setLengthU(OFSetConfig.MINIMUM_LENGTH);
440 asyncFastSend(config);
441 OFMessage getConfig = factory
442 .getMessage(OFType.GET_CONFIG_REQUEST);
443 asyncFastSend(getConfig);
447 } catch (Exception e) {
451 }, SWITCH_LIVENESS_TIMER, SWITCH_LIVENESS_TIMER);
454 private void cancelSwitchTimer() {
455 if (this.periodicTimer != null) {
456 this.periodicTimer.cancel();
460 private void reportError(Exception e) {
461 if (e instanceof AsynchronousCloseException
462 || e instanceof InterruptedException
463 || e instanceof SocketException || e instanceof IOException
464 || e instanceof ClosedSelectorException) {
465 logger.debug("Caught exception {}", e.getMessage());
467 logger.warn("Caught exception ", e);
469 // notify core of this error event and disconnect the switch
470 ((Controller) core).takeSwitchEventError(this);
473 private void reportSwitchStateChange(boolean added) {
475 ((Controller) core).takeSwitchEventAdd(this);
477 ((Controller) core).takeSwitchEventDelete(this);
482 public Long getId() {
486 private void processFeaturesReply(OFFeaturesReply reply) {
487 if (this.state == SwitchState.WAIT_FEATURES_REPLY) {
488 this.sid = reply.getDatapathId();
489 this.buffers = reply.getBuffers();
490 this.capabilities = reply.getCapabilities();
491 this.tables = reply.getTables();
492 this.actions = reply.getActions();
493 // notify core of this error event
494 for (OFPhysicalPort port : reply.getPorts()) {
495 updatePhysicalPort(port);
497 // config the switch to send full data packet
498 OFSetConfig config = (OFSetConfig) factory
499 .getMessage(OFType.SET_CONFIG);
500 config.setMissSendLength((short) 0xffff).setLengthU(
501 OFSetConfig.MINIMUM_LENGTH);
502 asyncFastSend(config);
503 // send config request to make sure the switch can handle the set
505 OFMessage getConfig = factory.getMessage(OFType.GET_CONFIG_REQUEST);
506 asyncFastSend(getConfig);
507 this.state = SwitchState.WAIT_CONFIG_REPLY;
508 // inform core that a new switch is now operational
509 reportSwitchStateChange(true);
513 private void updatePhysicalPort(OFPhysicalPort port) {
514 Short portNumber = port.getPortNumber();
515 physicalPorts.put(portNumber, port);
518 port.getCurrentFeatures()
519 & (OFPortFeatures.OFPPF_10MB_FD.getValue()
520 | OFPortFeatures.OFPPF_10MB_HD
522 | OFPortFeatures.OFPPF_100MB_FD
524 | OFPortFeatures.OFPPF_100MB_HD
526 | OFPortFeatures.OFPPF_1GB_FD
528 | OFPortFeatures.OFPPF_1GB_HD
529 .getValue() | OFPortFeatures.OFPPF_10GB_FD
533 private void deletePhysicalPort(OFPhysicalPort port) {
534 Short portNumber = port.getPortNumber();
535 physicalPorts.remove(portNumber);
536 portBandwidth.remove(portNumber);
540 public boolean isOperational() {
541 return ((this.state == SwitchState.WAIT_CONFIG_REPLY) || (this.state == SwitchState.OPERATIONAL));
545 public String toString() {
548 + socket.socket().getRemoteSocketAddress().toString().split("/")[1]
549 + " SWID:" + (isOperational() ? HexString
550 .toHexString(this.sid) : "unknown"));
551 } catch (Exception e) {
552 return (isOperational() ? HexString.toHexString(this.sid)
559 public Date getConnectedDate() {
560 return this.connectedDate;
563 public String getInstanceName() {
568 public Object getStatistics(OFStatisticsRequest req) {
569 int xid = getNextXid();
570 StatisticsCollector worker = new StatisticsCollector(this, xid, req);
571 messageWaitingDone.put(xid, worker);
572 Future<Object> submit = executor.submit(worker);
573 Object result = null;
575 result = submit.get(responseTimerValue, TimeUnit.MILLISECONDS);
577 } catch (Exception e) {
578 logger.warn("Timeout while waiting for {} replies", req.getType());
579 result = null; // to indicate timeout has occurred
585 public Object syncSend(OFMessage msg) {
586 int xid = getNextXid();
587 return syncSend(msg, xid);
591 * Either a BarrierReply or a OFError is received. If this is a reply for an
592 * outstanding sync message, wake up associated task so that it can continue
594 private void processBarrierReply(OFBarrierReply msg) {
595 Integer xid = msg.getXid();
596 SynchronousMessage worker = (SynchronousMessage) messageWaitingDone
598 if (worker == null) {
604 private void processErrorReply(OFError errorMsg) {
605 OFMessage offendingMsg = errorMsg.getOffendingMsg();
607 if (offendingMsg != null) {
608 xid = offendingMsg.getXid();
610 xid = errorMsg.getXid();
613 * the error can be a reply to a synchronous message or to a statistic
616 Callable<?> worker = messageWaitingDone.remove(xid);
617 if (worker == null) {
620 if (worker instanceof SynchronousMessage) {
621 ((SynchronousMessage) worker).wakeup(errorMsg);
623 ((StatisticsCollector) worker).wakeup(errorMsg);
627 private void processStatsReply(OFStatisticsReply reply) {
628 Integer xid = reply.getXid();
629 StatisticsCollector worker = (StatisticsCollector) messageWaitingDone
631 if (worker == null) {
634 if (worker.collect(reply)) {
635 // if all the stats records are received (collect() returns true)
637 messageWaitingDone.remove(xid);
643 public Map<Short, OFPhysicalPort> getPhysicalPorts() {
644 return this.physicalPorts;
648 public OFPhysicalPort getPhysicalPort(Short portNumber) {
649 return this.physicalPorts.get(portNumber);
653 public Integer getPortBandwidth(Short portNumber) {
654 return this.portBandwidth.get(portNumber);
658 public Set<Short> getPorts() {
659 return this.physicalPorts.keySet();
663 public Byte getTables() {
668 public Integer getActions() {
673 public Integer getCapabilities() {
674 return this.capabilities;
678 public Integer getBuffers() {
683 public boolean isPortEnabled(short portNumber) {
684 return isPortEnabled(physicalPorts.get(portNumber));
688 public boolean isPortEnabled(OFPhysicalPort port) {
692 int portConfig = port.getConfig();
693 int portState = port.getState();
694 if ((portConfig & OFPortConfig.OFPPC_PORT_DOWN.getValue()) > 0) {
697 if ((portState & OFPortState.OFPPS_LINK_DOWN.getValue()) > 0) {
700 if ((portState & OFPortState.OFPPS_STP_MASK.getValue()) == OFPortState.OFPPS_STP_BLOCK
708 public List<OFPhysicalPort> getEnabledPorts() {
709 List<OFPhysicalPort> result = new ArrayList<OFPhysicalPort>();
710 synchronized (this.physicalPorts) {
711 for (OFPhysicalPort port : physicalPorts.values()) {
712 if (isPortEnabled(port)) {
721 * Transmit thread polls the message out of the priority queue and invokes
722 * messaging service to transmit it over the socket channel
724 class PriorityMessageTransmit implements Runnable {
729 if (!transmitQ.isEmpty()) {
730 PriorityMessage pmsg = transmitQ.poll();
731 msgReadWriteService.asyncSend(pmsg.msg);
732 logger.trace("Message sent: {}", pmsg.toString());
734 * If syncReply is set to true, wait for the response
737 if (pmsg.syncReply) {
738 syncMessageInternal(pmsg.msg, pmsg.msg.getXid(), false);
742 } catch (InterruptedException ie) {
743 reportError(new InterruptedException(
744 "PriorityMessageTransmit thread interrupted"));
745 } catch (Exception e) {
754 * Setup and start the transmit thread
756 private void startTransmitThread() {
757 this.transmitQ = new PriorityBlockingQueue<PriorityMessage>(11,
758 new Comparator<PriorityMessage>() {
759 public int compare(PriorityMessage p1, PriorityMessage p2) {
760 if (p2.priority != p1.priority) {
761 return p2.priority - p1.priority;
763 return (p2.seqNum < p1.seqNum) ? 1 : -1;
767 this.transmitThread = new Thread(new PriorityMessageTransmit());
768 this.transmitThread.start();
772 * Setup communication services
774 private void setupCommChannel() throws Exception {
775 this.selector = SelectorProvider.provider().openSelector();
776 this.socket.configureBlocking(false);
777 this.socket.socket().setTcpNoDelay(true);
778 this.msgReadWriteService = getMessageReadWriteService();
781 private void sendFirstHello() {
783 OFMessage msg = factory.getMessage(OFType.HELLO);
785 } catch (Exception e) {
790 private IMessageReadWrite getMessageReadWriteService() throws Exception {
791 String str = System.getProperty("secureChannelEnabled");
792 return ((str != null) && (str.trim().equalsIgnoreCase("true"))) ? new SecureMessageReadWriteService(
793 socket, selector) : new MessageReadWriteService(socket,
798 * Send Barrier message synchronously. The caller will be blocked until the
799 * Barrier reply is received.
802 public Object syncSendBarrierMessage() {
803 OFBarrierRequest barrierMsg = new OFBarrierRequest();
804 return syncSend(barrierMsg);
808 * Send Barrier message asynchronously. The caller is not blocked. The
809 * Barrier message will be sent in a transmit thread which will be blocked
810 * until the Barrier reply is received.
813 public Object asyncSendBarrierMessage() {
814 if (transmitQ == null) {
815 return Boolean.FALSE;
818 OFBarrierRequest barrierMsg = new OFBarrierRequest();
819 int xid = getNextXid();
821 barrierMsg.setXid(xid);
822 transmitQ.add(new PriorityMessage(barrierMsg, 0, true));
828 * This method returns the switch liveness timeout value. If controller did
829 * not receive any message from the switch for such a long period,
830 * controller will tear down the connection to the switch.
832 * @return The timeout value
834 private static int getSwitchLivenessTimeout() {
835 String timeout = System.getProperty("of.switchLivenessTimeout");
839 if (timeout != null) {
840 rv = Integer.parseInt(timeout);
842 } catch (Exception e) {
849 * This method performs synchronous operations for a given message. If
850 * syncRequest is set to true, the message will be sent out followed by a
851 * Barrier request message. Then it's blocked until the Barrier rely arrives
852 * or timeout. If syncRequest is false, it simply skips the message send and
853 * just waits for the response back.
860 * If set to true, the message the message will be sent out
861 * followed by a Barrier request message. If set to false, it
862 * simply skips the sending and just waits for the Barrier reply.
865 private Object syncMessageInternal(OFMessage msg, int xid, boolean syncRequest) {
866 SynchronousMessage worker = new SynchronousMessage(this, xid, msg, syncRequest);
867 messageWaitingDone.put(xid, worker);
868 Object result = null;
869 Boolean status = false;
870 Future<Object> submit = executor.submit(worker);
872 result = submit.get(responseTimerValue, TimeUnit.MILLISECONDS);
873 messageWaitingDone.remove(xid);
874 if (result == null) {
875 // if result is null, then it means the switch can handle this
876 // message successfully
877 // convert the result into a Boolean with value true
879 // logger.debug("Successfully send " +
880 // msg.getType().toString());
883 // if result is not null, this means the switch can't handle
885 // the result if OFError already
886 logger.debug("Send {} failed --> {}", msg.getType().toString(),
887 ((OFError) result).toString());
890 } catch (Exception e) {
891 logger.warn("Timeout while waiting for {} reply", msg.getType()
893 // convert the result into a Boolean with value false