Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
| 18 | package net.floodlightcontroller.core.internal; |
| 19 | |
| 20 | import java.io.FileInputStream; |
| 21 | import java.io.IOException; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 22 | import java.net.InetSocketAddress; |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 23 | import java.net.UnknownHostException; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | import java.nio.channels.ClosedChannelException; |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 25 | import java.util.ArrayList; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | import java.util.Collection; |
| 27 | import java.util.Collections; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 28 | import java.util.HashMap; |
| 29 | import java.util.HashSet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Map.Entry; |
| 33 | import java.util.Properties; |
| 34 | import java.util.Set; |
| 35 | import java.util.Stack; |
| 36 | import java.util.concurrent.BlockingQueue; |
| 37 | import java.util.concurrent.ConcurrentHashMap; |
| 38 | import java.util.concurrent.ConcurrentMap; |
| 39 | import java.util.concurrent.CopyOnWriteArraySet; |
| 40 | import java.util.concurrent.Executors; |
| 41 | import java.util.concurrent.Future; |
| 42 | import java.util.concurrent.LinkedBlockingQueue; |
| 43 | import java.util.concurrent.RejectedExecutionException; |
| 44 | import java.util.concurrent.TimeUnit; |
| 45 | import java.util.concurrent.TimeoutException; |
| 46 | |
| 47 | import net.floodlightcontroller.core.FloodlightContext; |
| 48 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 49 | import net.floodlightcontroller.core.IListener.Command; |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 50 | import net.floodlightcontroller.core.IOFMessageListener; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 51 | import net.floodlightcontroller.core.IOFSwitch; |
| 52 | import net.floodlightcontroller.core.IOFSwitchFilter; |
| 53 | import net.floodlightcontroller.core.IOFSwitchListener; |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 54 | import net.floodlightcontroller.core.IUpdate; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 55 | import net.floodlightcontroller.core.annotations.LogMessageDoc; |
| 56 | import net.floodlightcontroller.core.annotations.LogMessageDocs; |
| 57 | import net.floodlightcontroller.core.internal.OFChannelState.HandshakeState; |
| 58 | import net.floodlightcontroller.core.util.ListenerDispatcher; |
| 59 | import net.floodlightcontroller.core.web.CoreWebRoutable; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 60 | import net.floodlightcontroller.restserver.IRestApiService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 61 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 62 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | 51f6f5b | 2014-04-03 10:32:10 -0700 | [diff] [blame] | 63 | import net.onrc.onos.core.main.IOFSwitchPortListener; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 64 | import net.onrc.onos.core.packet.Ethernet; |
| 65 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 66 | import net.onrc.onos.core.registry.IControllerRegistryService.ControlChangeCallback; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 67 | import net.onrc.onos.core.registry.RegistryException; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 68 | |
| 69 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 70 | import org.jboss.netty.buffer.ChannelBuffer; |
| 71 | import org.jboss.netty.buffer.ChannelBuffers; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 72 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 73 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 74 | import org.jboss.netty.channel.ChannelStateEvent; |
| 75 | import org.jboss.netty.channel.ChannelUpstreamHandler; |
| 76 | import org.jboss.netty.channel.Channels; |
| 77 | import org.jboss.netty.channel.ExceptionEvent; |
| 78 | import org.jboss.netty.channel.MessageEvent; |
| 79 | import org.jboss.netty.channel.group.ChannelGroup; |
| 80 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 81 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
| 82 | import org.jboss.netty.handler.timeout.IdleStateAwareChannelUpstreamHandler; |
| 83 | import org.jboss.netty.handler.timeout.IdleStateEvent; |
| 84 | import org.jboss.netty.handler.timeout.ReadTimeoutException; |
| 85 | import org.openflow.protocol.OFEchoReply; |
| 86 | import org.openflow.protocol.OFError; |
| 87 | import org.openflow.protocol.OFError.OFBadActionCode; |
| 88 | import org.openflow.protocol.OFError.OFBadRequestCode; |
| 89 | import org.openflow.protocol.OFError.OFErrorType; |
| 90 | import org.openflow.protocol.OFError.OFFlowModFailedCode; |
| 91 | import org.openflow.protocol.OFError.OFHelloFailedCode; |
| 92 | import org.openflow.protocol.OFError.OFPortModFailedCode; |
| 93 | import org.openflow.protocol.OFError.OFQueueOpFailedCode; |
| 94 | import org.openflow.protocol.OFFeaturesReply; |
| 95 | import org.openflow.protocol.OFGetConfigReply; |
| 96 | import org.openflow.protocol.OFMessage; |
| 97 | import org.openflow.protocol.OFPacketIn; |
| 98 | import org.openflow.protocol.OFPhysicalPort; |
Pankaj Berde | 6a4075d | 2013-01-22 16:42:54 -0800 | [diff] [blame] | 99 | import org.openflow.protocol.OFPhysicalPort.OFPortConfig; |
Pankaj Berde | 6debb04 | 2013-01-16 18:04:32 -0800 | [diff] [blame] | 100 | import org.openflow.protocol.OFPhysicalPort.OFPortState; |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 101 | import org.openflow.protocol.OFPortStatus; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 102 | import org.openflow.protocol.OFPortStatus.OFPortReason; |
| 103 | import org.openflow.protocol.OFSetConfig; |
| 104 | import org.openflow.protocol.OFStatisticsRequest; |
| 105 | import org.openflow.protocol.OFSwitchConfig; |
| 106 | import org.openflow.protocol.OFType; |
| 107 | import org.openflow.protocol.OFVendor; |
| 108 | import org.openflow.protocol.factory.BasicFactory; |
| 109 | import org.openflow.protocol.factory.MessageParseException; |
| 110 | import org.openflow.protocol.statistics.OFDescriptionStatistics; |
| 111 | import org.openflow.protocol.statistics.OFStatistics; |
| 112 | import org.openflow.protocol.statistics.OFStatisticsType; |
| 113 | import org.openflow.protocol.vendor.OFBasicVendorDataType; |
| 114 | import org.openflow.protocol.vendor.OFBasicVendorId; |
| 115 | import org.openflow.protocol.vendor.OFVendorId; |
| 116 | import org.openflow.util.HexString; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 117 | import org.openflow.vendor.nicira.OFNiciraVendorData; |
| 118 | import org.openflow.vendor.nicira.OFRoleReplyVendorData; |
| 119 | import org.openflow.vendor.nicira.OFRoleRequestVendorData; |
| 120 | import org.openflow.vendor.nicira.OFRoleVendorData; |
| 121 | import org.slf4j.Logger; |
| 122 | import org.slf4j.LoggerFactory; |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * The main controller class. Handles all setup and network listeners |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | * <p/> |
HIGUCHI Yuta | 1136070 | 2013-06-17 10:28:06 -0700 | [diff] [blame] | 128 | * Extensions made by ONOS are: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 129 | * - Detailed Port event: PORTCHANGED -> {PORTCHANGED, PORTADDED, PORTREMOVED} |
| 130 | * Available as net.onrc.onos.core.main.IOFSwitchPortListener |
HIGUCHI Yuta | 1136070 | 2013-06-17 10:28:06 -0700 | [diff] [blame] | 131 | * - Distributed ownership control of switch through RegistryService(IControllerRegistryService) |
Pavlin Radoslavov | a653e9f | 2013-10-16 03:08:52 -0700 | [diff] [blame] | 132 | * - Register ONOS services. (IControllerRegistryService) |
HIGUCHI Yuta | 1136070 | 2013-06-17 10:28:06 -0700 | [diff] [blame] | 133 | * - Additional DEBUG logs |
| 134 | * - Try using hostname as controller ID, when ID was not explicitly given. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 135 | */ |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 136 | public class Controller implements IFloodlightProviderService { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 138 | protected final static Logger log = LoggerFactory.getLogger(Controller.class); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 139 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 140 | private static final String ERROR_DATABASE = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 141 | "The controller could not communicate with the system database."; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 142 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 143 | protected BasicFactory factory; |
| 144 | protected ConcurrentMap<OFType, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 145 | ListenerDispatcher<OFType, IOFMessageListener>> |
| 146 | messageListeners; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 147 | // The activeSwitches map contains only those switches that are actively |
| 148 | // being controlled by us -- it doesn't contain switches that are |
| 149 | // in the slave role |
| 150 | protected ConcurrentHashMap<Long, IOFSwitch> activeSwitches; |
| 151 | // connectedSwitches contains all connected switches, including ones where |
| 152 | // we're a slave controller. We need to keep track of them so that we can |
| 153 | // send role request messages to switches when our role changes to master |
| 154 | // We add a switch to this set after it successfully completes the |
| 155 | // handshake. Access to this Set needs to be synchronized with roleChanger |
| 156 | protected HashSet<OFSwitchImpl> connectedSwitches; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 158 | // The controllerNodeIPsCache maps Controller IDs to their IP address. |
| 159 | // It's only used by handleControllerNodeIPsChanged |
| 160 | protected HashMap<String, String> controllerNodeIPsCache; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 161 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 162 | protected Set<IOFSwitchListener> switchListeners; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 163 | protected BlockingQueue<IUpdate> updates; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 164 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 165 | // Module dependencies |
| 166 | protected IRestApiService restApi; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 167 | protected IThreadPoolService threadPool; |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 168 | protected IControllerRegistryService registryService; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 169 | |
Jonathan Hart | 8a5d097 | 2013-12-04 10:02:44 -0800 | [diff] [blame] | 170 | protected ILinkDiscoveryService linkDiscovery; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 171 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 172 | // Configuration options |
| 173 | protected int openFlowPort = 6633; |
| 174 | protected int workerThreads = 0; |
| 175 | // The id for this controller node. Should be unique for each controller |
| 176 | // node in a controller cluster. |
| 177 | protected String controllerId = "localhost"; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 178 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 179 | // The current role of the controller. |
| 180 | // If the controller isn't configured to support roles, then this is null. |
| 181 | protected Role role; |
| 182 | // A helper that handles sending and timeout handling for role requests |
| 183 | protected RoleChanger roleChanger; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 184 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 185 | // Start time of the controller |
| 186 | protected long systemStartTime; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 187 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 188 | // Flag to always flush flow table on switch reconnect (HA or otherwise) |
| 189 | protected boolean alwaysClearFlowsOnSwAdd = false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 190 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 191 | // Perf. related configuration |
| 192 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
| 193 | protected static final int BATCH_MAX_SIZE = 100; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 194 | protected static final boolean ALWAYS_DECODE_ETH = true; |
| 195 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 196 | public enum SwitchUpdateType { |
| 197 | ADDED, |
| 198 | REMOVED, |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 199 | PORTCHANGED, |
| 200 | PORTADDED, |
| 201 | PORTREMOVED |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 202 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 203 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 204 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 205 | * Update message indicating a switch was added or removed |
HIGUCHI Yuta | ec4bff8 | 2013-06-17 11:49:31 -0700 | [diff] [blame] | 206 | * ONOS: This message extended to indicate Port add or removed event. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 207 | */ |
| 208 | protected class SwitchUpdate implements IUpdate { |
| 209 | public IOFSwitch sw; |
HIGUCHI Yuta | ec4bff8 | 2013-06-17 11:49:31 -0700 | [diff] [blame] | 210 | public OFPhysicalPort port; // Added by ONOS |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 211 | public SwitchUpdateType switchUpdateType; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 212 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 213 | public SwitchUpdate(IOFSwitch sw, SwitchUpdateType switchUpdateType) { |
| 214 | this.sw = sw; |
| 215 | this.switchUpdateType = switchUpdateType; |
| 216 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 217 | |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 218 | public SwitchUpdate(IOFSwitch sw, OFPhysicalPort port, SwitchUpdateType switchUpdateType) { |
| 219 | this.sw = sw; |
| 220 | this.port = port; |
| 221 | this.switchUpdateType = switchUpdateType; |
| 222 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 223 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 224 | public void dispatch() { |
| 225 | if (log.isTraceEnabled()) { |
| 226 | log.trace("Dispatching switch update {} {}", |
| 227 | sw, switchUpdateType); |
| 228 | } |
| 229 | if (switchListeners != null) { |
| 230 | for (IOFSwitchListener listener : switchListeners) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 231 | switch (switchUpdateType) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 232 | case ADDED: |
| 233 | listener.addedSwitch(sw); |
| 234 | break; |
| 235 | case REMOVED: |
| 236 | listener.removedSwitch(sw); |
| 237 | break; |
| 238 | case PORTCHANGED: |
| 239 | listener.switchPortChanged(sw.getId()); |
| 240 | break; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 241 | case PORTADDED: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 242 | if (listener instanceof IOFSwitchPortListener) { |
| 243 | ((IOFSwitchPortListener) listener).switchPortAdded(sw.getId(), port); |
| 244 | } |
| 245 | break; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 246 | case PORTREMOVED: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 247 | if (listener instanceof IOFSwitchPortListener) { |
| 248 | ((IOFSwitchPortListener) listener).switchPortRemoved(sw.getId(), port); |
| 249 | } |
| 250 | break; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 251 | default: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 252 | break; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 258 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 259 | // *************** |
| 260 | // Getters/Setters |
Jonathan Hart | 20fab1a | 2013-12-12 11:06:50 -0800 | [diff] [blame] | 261 | // *************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 262 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 263 | public void setRestApiService(IRestApiService restApi) { |
| 264 | this.restApi = restApi; |
| 265 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 266 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 267 | public void setThreadPoolService(IThreadPoolService tp) { |
| 268 | this.threadPool = tp; |
| 269 | } |
| 270 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 271 | public void setMastershipService(IControllerRegistryService serviceImpl) { |
| 272 | this.registryService = serviceImpl; |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 273 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 274 | |
| 275 | public void setLinkDiscoveryService(ILinkDiscoveryService linkDiscovery) { |
| 276 | this.linkDiscovery = linkDiscovery; |
| 277 | } |
| 278 | |
| 279 | public void publishUpdate(IUpdate update) { |
| 280 | try { |
| 281 | this.updates.put(update); |
| 282 | } catch (InterruptedException e) { |
| 283 | log.error("Failure adding update to queue", e); |
| 284 | } |
| 285 | } |
| 286 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 287 | // ********************** |
| 288 | // ChannelUpstreamHandler |
| 289 | // ********************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 290 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 291 | /** |
| 292 | * Return a new channel handler for processing a switch connections |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 293 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 294 | * @param state The channel state object for the connection |
| 295 | * @return the new channel handler |
| 296 | */ |
| 297 | protected ChannelUpstreamHandler getChannelHandler(OFChannelState state) { |
| 298 | return new OFChannelHandler(state); |
| 299 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 300 | |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 301 | protected class RoleChangeCallback implements ControlChangeCallback { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 302 | @Override |
| 303 | public void controlChanged(long dpid, boolean hasControl) { |
| 304 | log.info("Role change callback for switch {}, hasControl {}", |
| 305 | HexString.toHexString(dpid), hasControl); |
Pankaj Berde | 01939e9 | 2013-03-08 14:38:27 -0800 | [diff] [blame] | 306 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 307 | synchronized (roleChanger) { |
| 308 | OFSwitchImpl sw = null; |
| 309 | for (OFSwitchImpl connectedSw : connectedSwitches) { |
| 310 | if (connectedSw.getId() == dpid) { |
| 311 | sw = connectedSw; |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | if (sw == null) { |
| 316 | log.warn("Switch {} not found in connected switches", |
| 317 | HexString.toHexString(dpid)); |
| 318 | return; |
| 319 | } |
Pankaj Berde | 01939e9 | 2013-03-08 14:38:27 -0800 | [diff] [blame] | 320 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 321 | Role role = null; |
| 322 | |
| 323 | /* |
| 324 | * issue #229 |
| 325 | * Cannot rely on sw.getRole() as it can be behind due to pending |
| 326 | * role changes in the queue. Just submit it and late the RoleChanger |
| 327 | * handle duplicates. |
| 328 | */ |
Pankaj Berde | 01939e9 | 2013-03-08 14:38:27 -0800 | [diff] [blame] | 329 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 330 | if (hasControl) { |
| 331 | role = Role.MASTER; |
| 332 | } else { |
| 333 | role = Role.SLAVE; |
| 334 | } |
| 335 | |
| 336 | log.debug("Sending role request {} msg to {}", role, sw); |
| 337 | Collection<OFSwitchImpl> swList = new ArrayList<OFSwitchImpl>(1); |
| 338 | swList.add(sw); |
| 339 | roleChanger.submitRequest(swList, role); |
| 340 | |
| 341 | } |
| 342 | |
| 343 | } |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 344 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 345 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 346 | /** |
| 347 | * Channel handler deals with the switch connection and dispatches |
| 348 | * switch messages to the appropriate locations. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 349 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 350 | * @author readams |
| 351 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 352 | protected class OFChannelHandler |
| 353 | extends IdleStateAwareChannelUpstreamHandler { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 354 | protected OFSwitchImpl sw; |
| 355 | protected OFChannelState state; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 356 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 357 | public OFChannelHandler(OFChannelState state) { |
| 358 | this.state = state; |
| 359 | } |
| 360 | |
| 361 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 362 | @LogMessageDoc(message = "New switch connection from {ip address}", |
| 363 | explanation = "A new switch has connected from the " + |
| 364 | "specified IP address") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 365 | public void channelConnected(ChannelHandlerContext ctx, |
| 366 | ChannelStateEvent e) throws Exception { |
| 367 | log.info("New switch connection from {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 368 | e.getChannel().getRemoteAddress()); |
| 369 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 370 | sw = new OFSwitchImpl(); |
| 371 | sw.setChannel(e.getChannel()); |
| 372 | sw.setFloodlightProvider(Controller.this); |
| 373 | sw.setThreadPoolService(threadPool); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 374 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 375 | List<OFMessage> msglist = new ArrayList<OFMessage>(1); |
| 376 | msglist.add(factory.getMessage(OFType.HELLO)); |
| 377 | e.getChannel().write(msglist); |
| 378 | |
| 379 | } |
| 380 | |
| 381 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 382 | @LogMessageDoc(message = "Disconnected switch {switch information}", |
| 383 | explanation = "The specified switch has disconnected.") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 384 | public void channelDisconnected(ChannelHandlerContext ctx, |
| 385 | ChannelStateEvent e) throws Exception { |
| 386 | if (sw != null && state.hsState == HandshakeState.READY) { |
| 387 | if (activeSwitches.containsKey(sw.getId())) { |
| 388 | // It's safe to call removeSwitch even though the map might |
| 389 | // not contain this particular switch but another with the |
| 390 | // same DPID |
| 391 | removeSwitch(sw); |
| 392 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 393 | synchronized (roleChanger) { |
| 394 | if (controlRequested) { |
| 395 | registryService.releaseControl(sw.getId()); |
| 396 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 397 | connectedSwitches.remove(sw); |
| 398 | } |
| 399 | sw.setConnected(false); |
| 400 | } |
| 401 | log.info("Disconnected switch {}", sw); |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 406 | @LogMessageDoc(level = "ERROR", |
| 407 | message = "Disconnecting switch {switch} due to read timeout", |
| 408 | explanation = "The connected switch has failed to send any " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 409 | "messages or respond to echo requests", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 410 | recommendation = LogMessageDoc.CHECK_SWITCH), |
| 411 | @LogMessageDoc(level = "ERROR", |
| 412 | message = "Disconnecting switch {switch}: failed to " + |
| 413 | "complete handshake", |
| 414 | explanation = "The switch did not respond correctly " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 415 | "to handshake messages", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 416 | recommendation = LogMessageDoc.CHECK_SWITCH), |
| 417 | @LogMessageDoc(level = "ERROR", |
| 418 | message = "Disconnecting switch {switch} due to IO Error: {}", |
| 419 | explanation = "There was an error communicating with the switch", |
| 420 | recommendation = LogMessageDoc.CHECK_SWITCH), |
| 421 | @LogMessageDoc(level = "ERROR", |
| 422 | message = "Disconnecting switch {switch} due to switch " + |
| 423 | "state error: {error}", |
| 424 | explanation = "The switch sent an unexpected message", |
| 425 | recommendation = LogMessageDoc.CHECK_SWITCH), |
| 426 | @LogMessageDoc(level = "ERROR", |
| 427 | message = "Disconnecting switch {switch} due to " + |
| 428 | "message parse failure", |
| 429 | explanation = "Could not parse a message from the switch", |
| 430 | recommendation = LogMessageDoc.CHECK_SWITCH), |
| 431 | @LogMessageDoc(level = "ERROR", |
| 432 | message = "Could not process message: queue full", |
| 433 | explanation = "OpenFlow messages are arriving faster than " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 434 | " the controller can process them.", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 435 | recommendation = LogMessageDoc.CHECK_CONTROLLER), |
| 436 | @LogMessageDoc(level = "ERROR", |
| 437 | message = "Error while processing message " + |
| 438 | "from switch {switch} {cause}", |
| 439 | explanation = "An error occurred processing the switch message", |
| 440 | recommendation = LogMessageDoc.GENERIC_ACTION) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 441 | }) |
| 442 | public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) |
| 443 | throws Exception { |
| 444 | if (e.getCause() instanceof ReadTimeoutException) { |
| 445 | // switch timeout |
| 446 | log.error("Disconnecting switch {} due to read timeout", sw); |
| 447 | ctx.getChannel().close(); |
| 448 | } else if (e.getCause() instanceof HandshakeTimeoutException) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 449 | log.error("Disconnecting switch {}: failed to complete handshake", |
| 450 | sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 451 | ctx.getChannel().close(); |
| 452 | } else if (e.getCause() instanceof ClosedChannelException) { |
| 453 | //log.warn("Channel for sw {} already closed", sw); |
| 454 | } else if (e.getCause() instanceof IOException) { |
| 455 | log.error("Disconnecting switch {} due to IO Error: {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 456 | sw, e.getCause().getMessage()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 457 | ctx.getChannel().close(); |
| 458 | } else if (e.getCause() instanceof SwitchStateException) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 459 | log.error("Disconnecting switch {} due to switch state error: {}", |
| 460 | sw, e.getCause().getMessage()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 461 | ctx.getChannel().close(); |
| 462 | } else if (e.getCause() instanceof MessageParseException) { |
| 463 | log.error("Disconnecting switch " + sw + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 464 | " due to message parse failure", |
| 465 | e.getCause()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 466 | ctx.getChannel().close(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 467 | } else if (e.getCause() instanceof RejectedExecutionException) { |
| 468 | log.warn("Could not process message: queue full"); |
| 469 | } else { |
| 470 | log.error("Error while processing message from switch " + sw, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 471 | e.getCause()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 472 | ctx.getChannel().close(); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | @Override |
| 477 | public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) |
| 478 | throws Exception { |
| 479 | List<OFMessage> msglist = new ArrayList<OFMessage>(1); |
| 480 | msglist.add(factory.getMessage(OFType.ECHO_REQUEST)); |
| 481 | e.getChannel().write(msglist); |
| 482 | } |
| 483 | |
| 484 | @Override |
| 485 | public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) |
| 486 | throws Exception { |
| 487 | if (e.getMessage() instanceof List) { |
| 488 | @SuppressWarnings("unchecked") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 489 | List<OFMessage> msglist = (List<OFMessage>) e.getMessage(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 490 | |
| 491 | for (OFMessage ofm : msglist) { |
| 492 | try { |
| 493 | processOFMessage(ofm); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 494 | } catch (Exception ex) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 495 | // We are the last handler in the stream, so run the |
| 496 | // exception through the channel again by passing in |
| 497 | // ctx.getChannel(). |
| 498 | Channels.fireExceptionCaught(ctx.getChannel(), ex); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // Flush all flow-mods/packet-out generated from this "train" |
| 503 | OFSwitchImpl.flush_all(); |
| 504 | } |
| 505 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 506 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 507 | /** |
| 508 | * Process the request for the switch description |
| 509 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 510 | @LogMessageDoc(level = "ERROR", |
| 511 | message = "Exception in reading description " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 512 | " during handshake {exception}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 513 | explanation = "Could not process the switch description string", |
| 514 | recommendation = LogMessageDoc.CHECK_SWITCH) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 515 | void processSwitchDescReply() { |
| 516 | try { |
| 517 | // Read description, if it has been updated |
| 518 | @SuppressWarnings("unchecked") |
| 519 | Future<List<OFStatistics>> desc_future = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 520 | (Future<List<OFStatistics>>) sw. |
| 521 | getAttribute(IOFSwitch.SWITCH_DESCRIPTION_FUTURE); |
| 522 | List<OFStatistics> values = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 523 | desc_future.get(0, TimeUnit.MILLISECONDS); |
| 524 | if (values != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 525 | OFDescriptionStatistics description = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 526 | new OFDescriptionStatistics(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 527 | ChannelBuffer data = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 528 | ChannelBuffers.buffer(description.getLength()); |
| 529 | for (OFStatistics f : values) { |
| 530 | f.writeTo(data); |
| 531 | description.readFrom(data); |
| 532 | break; // SHOULD be a list of length 1 |
| 533 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 534 | sw.setAttribute(IOFSwitch.SWITCH_DESCRIPTION_DATA, |
| 535 | description); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 536 | sw.setSwitchProperties(description); |
| 537 | data = null; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 538 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 539 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 540 | sw.removeAttribute(IOFSwitch.SWITCH_DESCRIPTION_FUTURE); |
| 541 | state.hasDescription = true; |
| 542 | checkSwitchReady(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 543 | } catch (InterruptedException ex) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 544 | // Ignore |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 545 | } catch (TimeoutException ex) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 546 | // Ignore |
| 547 | } catch (Exception ex) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 548 | log.error("Exception in reading description " + |
| 549 | " during handshake", ex); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Send initial switch setup information that we need before adding |
| 555 | * the switch |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 556 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 557 | * @throws IOException |
| 558 | */ |
| 559 | void sendHelloConfiguration() throws IOException { |
| 560 | // Send initial Features Request |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 561 | log.debug("Sending FEATURES_REQUEST to {}", sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 562 | sw.write(factory.getMessage(OFType.FEATURES_REQUEST), null); |
| 563 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 564 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 565 | /** |
| 566 | * Send the configuration requests we can only do after we have |
| 567 | * the features reply |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 568 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 569 | * @throws IOException |
| 570 | */ |
| 571 | void sendFeatureReplyConfiguration() throws IOException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 572 | log.debug("Sending CONFIG_REQUEST to {}", sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 573 | // Ensure we receive the full packet via PacketIn |
| 574 | OFSetConfig config = (OFSetConfig) factory |
| 575 | .getMessage(OFType.SET_CONFIG); |
| 576 | config.setMissSendLength((short) 0xffff) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 577 | .setLengthU(OFSwitchConfig.MINIMUM_LENGTH); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 578 | sw.write(config, null); |
| 579 | sw.write(factory.getMessage(OFType.GET_CONFIG_REQUEST), |
| 580 | null); |
| 581 | |
| 582 | // Get Description to set switch-specific flags |
| 583 | OFStatisticsRequest req = new OFStatisticsRequest(); |
| 584 | req.setStatisticType(OFStatisticsType.DESC); |
| 585 | req.setLengthU(req.getLengthU()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 586 | Future<List<OFStatistics>> dfuture = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 587 | sw.getStatistics(req); |
| 588 | sw.setAttribute(IOFSwitch.SWITCH_DESCRIPTION_FUTURE, |
| 589 | dfuture); |
| 590 | |
| 591 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 592 | |
| 593 | volatile Boolean controlRequested = Boolean.FALSE; |
| 594 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 595 | protected void checkSwitchReady() { |
| 596 | if (state.hsState == HandshakeState.FEATURES_REPLY && |
| 597 | state.hasDescription && state.hasGetConfigReply) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 598 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 599 | state.hsState = HandshakeState.READY; |
Jonathan Hart | 9e92c51 | 2013-03-20 16:24:44 -0700 | [diff] [blame] | 600 | log.debug("Handshake with {} complete", sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 601 | |
| 602 | synchronized (roleChanger) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 603 | // We need to keep track of all of the switches that are connected |
| 604 | // to the controller, in any role, so that we can later send the |
| 605 | // role request messages when the controller role changes. |
| 606 | // We need to be synchronized while doing this: we must not |
| 607 | // send a another role request to the connectedSwitches until |
| 608 | // we were able to add this new switch to connectedSwitches |
| 609 | // *and* send the current role to the new switch. |
| 610 | connectedSwitches.add(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 611 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 612 | if (role != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 613 | //Put the switch in SLAVE mode until we know we have control |
| 614 | log.debug("Setting new switch {} to SLAVE", sw.getStringId()); |
| 615 | Collection<OFSwitchImpl> swList = new ArrayList<OFSwitchImpl>(1); |
| 616 | swList.add(sw); |
| 617 | roleChanger.submitRequest(swList, Role.SLAVE); |
| 618 | |
| 619 | //Request control of the switch from the global registry |
| 620 | try { |
| 621 | controlRequested = Boolean.TRUE; |
| 622 | registryService.requestControl(sw.getId(), |
| 623 | new RoleChangeCallback()); |
| 624 | } catch (RegistryException e) { |
| 625 | log.debug("Registry error: {}", e.getMessage()); |
| 626 | controlRequested = Boolean.FALSE; |
| 627 | } |
| 628 | |
| 629 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 630 | // Send a role request if role support is enabled for the controller |
| 631 | // This is a probe that we'll use to determine if the switch |
| 632 | // actually supports the role request message. If it does we'll |
| 633 | // get back a role reply message. If it doesn't we'll get back an |
| 634 | // OFError message. |
| 635 | // If role is MASTER we will promote switch to active |
| 636 | // list when we receive the switch's role reply messages |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 637 | /* |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 638 | log.debug("This controller's role is {}, " + |
| 639 | "sending initial role request msg to {}", |
| 640 | role, sw); |
| 641 | Collection<OFSwitchImpl> swList = new ArrayList<OFSwitchImpl>(1); |
| 642 | swList.add(sw); |
| 643 | roleChanger.submitRequest(swList, role); |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 644 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 645 | } else { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 646 | // Role supported not enabled on controller (for now) |
| 647 | // automatically promote switch to active state. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 648 | log.debug("This controller's role is {}, " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 649 | "not sending role request msg to {}", |
| 650 | role, sw); |
| 651 | // Need to clear FlowMods before we add the switch |
| 652 | // and dispatch updates otherwise we have a race condition. |
| 653 | sw.clearAllFlowMods(); |
| 654 | addSwitch(sw); |
| 655 | state.firstRoleReplyReceived = true; |
| 656 | } |
| 657 | } |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 658 | if (!controlRequested) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 659 | // yield to allow other thread(s) to release control |
| 660 | try { |
| 661 | Thread.sleep(10); |
| 662 | } catch (InterruptedException e) { |
| 663 | // Ignore interruptions |
| 664 | } |
| 665 | // safer to bounce the switch to reconnect here than proceeding further |
| 666 | log.debug("Closing {} because we weren't able to request control " + |
| 667 | "successfully" + sw); |
| 668 | sw.channel.close(); |
Pankaj Berde | 99fcee1 | 2013-03-18 09:41:53 -0700 | [diff] [blame] | 669 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 670 | } |
| 671 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 672 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 673 | /* Handle a role reply message we received from the switch. Since |
| 674 | * netty serializes message dispatch we don't need to synchronize |
| 675 | * against other receive operations from the same switch, so no need |
| 676 | * to synchronize addSwitch(), removeSwitch() operations from the same |
| 677 | * connection. |
| 678 | * FIXME: However, when a switch with the same DPID connects we do |
| 679 | * need some synchronization. However, handling switches with same |
| 680 | * DPID needs to be revisited anyways (get rid of r/w-lock and synchronous |
| 681 | * removedSwitch notification):1 |
| 682 | * |
| 683 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 684 | @LogMessageDoc(level = "ERROR", |
| 685 | message = "Invalid role value in role reply message", |
| 686 | explanation = "Was unable to set the HA role (master or slave) " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 687 | "for the controller.", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 688 | recommendation = LogMessageDoc.CHECK_CONTROLLER) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 689 | protected void handleRoleReplyMessage(OFVendor vendorMessage, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 690 | OFRoleReplyVendorData roleReplyVendorData) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 691 | // Map from the role code in the message to our role enum |
| 692 | int nxRole = roleReplyVendorData.getRole(); |
| 693 | Role role = null; |
| 694 | switch (nxRole) { |
| 695 | case OFRoleVendorData.NX_ROLE_OTHER: |
| 696 | role = Role.EQUAL; |
| 697 | break; |
| 698 | case OFRoleVendorData.NX_ROLE_MASTER: |
| 699 | role = Role.MASTER; |
| 700 | break; |
| 701 | case OFRoleVendorData.NX_ROLE_SLAVE: |
| 702 | role = Role.SLAVE; |
| 703 | break; |
| 704 | default: |
| 705 | log.error("Invalid role value in role reply message"); |
| 706 | sw.getChannel().close(); |
| 707 | return; |
| 708 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 709 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 710 | log.debug("Handling role reply for role {} from {}. " + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 711 | "Controller's role is {} ", |
| 712 | new Object[]{role, sw, Controller.this.role} |
| 713 | ); |
| 714 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 715 | sw.deliverRoleReply(vendorMessage.getXid(), role); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 716 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 717 | boolean isActive = activeSwitches.containsKey(sw.getId()); |
| 718 | if (!isActive && sw.isActive()) { |
| 719 | // Transition from SLAVE to MASTER. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 720 | |
| 721 | if (!state.firstRoleReplyReceived || |
| 722 | getAlwaysClearFlowsOnSwAdd()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 723 | // This is the first role-reply message we receive from |
| 724 | // this switch or roles were disabled when the switch |
| 725 | // connected: |
| 726 | // Delete all pre-existing flows for new connections to |
| 727 | // the master |
| 728 | // |
| 729 | // FIXME: Need to think more about what the test should |
| 730 | // be for when we flush the flow-table? For example, |
| 731 | // if all the controllers are temporarily in the backup |
| 732 | // role (e.g. right after a failure of the master |
| 733 | // controller) at the point the switch connects, then |
| 734 | // all of the controllers will initially connect as |
| 735 | // backup controllers and not flush the flow-table. |
| 736 | // Then when one of them is promoted to master following |
| 737 | // the master controller election the flow-table |
| 738 | // will still not be flushed because that's treated as |
| 739 | // a failover event where we don't want to flush the |
| 740 | // flow-table. The end result would be that the flow |
| 741 | // table for a newly connected switch is never |
| 742 | // flushed. Not sure how to handle that case though... |
| 743 | sw.clearAllFlowMods(); |
| 744 | log.debug("First role reply from master switch {}, " + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 745 | "clear FlowTable to active switch list", |
| 746 | HexString.toHexString(sw.getId())); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 747 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 748 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 749 | // Some switches don't seem to update us with port |
| 750 | // status messages while in slave role. |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 751 | //readSwitchPortStateFromStorage(sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 752 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 753 | // Only add the switch to the active switch list if |
| 754 | // we're not in the slave role. Note that if the role |
| 755 | // attribute is null, then that means that the switch |
| 756 | // doesn't support the role request messages, so in that |
| 757 | // case we're effectively in the EQUAL role and the |
| 758 | // switch should be included in the active switch list. |
| 759 | addSwitch(sw); |
| 760 | log.debug("Added master switch {} to active switch list", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 761 | HexString.toHexString(sw.getId())); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 762 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 763 | } else if (isActive && !sw.isActive()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 764 | // Transition from MASTER to SLAVE: remove switch |
| 765 | // from active switch list. |
| 766 | log.debug("Removed slave switch {} from active switch" + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 767 | " list", HexString.toHexString(sw.getId())); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 768 | removeSwitch(sw); |
| 769 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 770 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 771 | // Indicate that we have received a role reply message. |
| 772 | state.firstRoleReplyReceived = true; |
| 773 | } |
| 774 | |
| 775 | protected boolean handleVendorMessage(OFVendor vendorMessage) { |
| 776 | boolean shouldHandleMessage = false; |
| 777 | int vendor = vendorMessage.getVendor(); |
| 778 | switch (vendor) { |
| 779 | case OFNiciraVendorData.NX_VENDOR_ID: |
| 780 | OFNiciraVendorData niciraVendorData = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 781 | (OFNiciraVendorData) vendorMessage.getVendorData(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 782 | int dataType = niciraVendorData.getDataType(); |
| 783 | switch (dataType) { |
| 784 | case OFRoleReplyVendorData.NXT_ROLE_REPLY: |
| 785 | OFRoleReplyVendorData roleReplyVendorData = |
| 786 | (OFRoleReplyVendorData) niciraVendorData; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 787 | handleRoleReplyMessage(vendorMessage, |
| 788 | roleReplyVendorData); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 789 | break; |
| 790 | default: |
| 791 | log.warn("Unhandled Nicira VENDOR message; " + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 792 | "data type = {}", dataType); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 793 | break; |
| 794 | } |
| 795 | break; |
| 796 | default: |
| 797 | log.warn("Unhandled VENDOR message; vendor id = {}", vendor); |
| 798 | break; |
| 799 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 800 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 801 | return shouldHandleMessage; |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * Dispatch an Openflow message from a switch to the appropriate |
| 806 | * handler. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 807 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 808 | * @param m The message to process |
| 809 | * @throws IOException |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 810 | * @throws SwitchStateException |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 811 | */ |
| 812 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 813 | @LogMessageDoc(level = "WARN", |
| 814 | message = "Config Reply from {switch} has " + |
| 815 | "miss length set to {length}", |
| 816 | explanation = "The controller requires that the switch " + |
| 817 | "use a miss length of 0xffff for correct " + |
| 818 | "function", |
| 819 | recommendation = "Use a different switch to ensure " + |
| 820 | "correct function"), |
| 821 | @LogMessageDoc(level = "WARN", |
| 822 | message = "Received ERROR from sw {switch} that " |
| 823 | + "indicates roles are not supported " |
| 824 | + "but we have received a valid " |
| 825 | + "role reply earlier", |
| 826 | explanation = "The switch sent a confusing message to the" + |
| 827 | "controller") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 828 | }) |
| 829 | protected void processOFMessage(OFMessage m) |
| 830 | throws IOException, SwitchStateException { |
| 831 | boolean shouldHandleMessage = false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 832 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 833 | switch (m.getType()) { |
| 834 | case HELLO: |
| 835 | if (log.isTraceEnabled()) |
| 836 | log.trace("HELLO from {}", sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 837 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 838 | if (state.hsState.equals(HandshakeState.START)) { |
| 839 | state.hsState = HandshakeState.HELLO; |
| 840 | sendHelloConfiguration(); |
| 841 | } else { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 842 | throw new SwitchStateException("Unexpected HELLO from " |
| 843 | + sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 844 | } |
| 845 | break; |
| 846 | case ECHO_REQUEST: |
| 847 | OFEchoReply reply = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 848 | (OFEchoReply) factory.getMessage(OFType.ECHO_REPLY); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 849 | reply.setXid(m.getXid()); |
| 850 | sw.write(reply, null); |
| 851 | break; |
| 852 | case ECHO_REPLY: |
| 853 | break; |
| 854 | case FEATURES_REPLY: |
| 855 | if (log.isTraceEnabled()) |
| 856 | log.trace("Features Reply from {}", sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 857 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 858 | sw.setFeaturesReply((OFFeaturesReply) m); |
| 859 | if (state.hsState.equals(HandshakeState.HELLO)) { |
| 860 | sendFeatureReplyConfiguration(); |
| 861 | state.hsState = HandshakeState.FEATURES_REPLY; |
| 862 | // uncomment to enable "dumb" switches like cbench |
| 863 | // state.hsState = HandshakeState.READY; |
| 864 | // addSwitch(sw); |
| 865 | } else { |
| 866 | // return results to rest api caller |
| 867 | sw.deliverOFFeaturesReply(m); |
| 868 | // update database */ |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 869 | //updateActiveSwitchInfo(sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 870 | } |
| 871 | break; |
| 872 | case GET_CONFIG_REPLY: |
| 873 | if (log.isTraceEnabled()) |
| 874 | log.trace("Get config reply from {}", sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 875 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 876 | if (!state.hsState.equals(HandshakeState.FEATURES_REPLY)) { |
| 877 | String em = "Unexpected GET_CONFIG_REPLY from " + sw; |
| 878 | throw new SwitchStateException(em); |
| 879 | } |
| 880 | OFGetConfigReply cr = (OFGetConfigReply) m; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 881 | if (cr.getMissSendLength() == (short) 0xffff) { |
| 882 | log.trace("Config Reply from {} confirms " + |
| 883 | "miss length set to 0xffff", sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 884 | } else { |
| 885 | log.warn("Config Reply from {} has " + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 886 | "miss length set to {}", |
| 887 | sw, cr.getMissSendLength() & 0xffff); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 888 | } |
| 889 | state.hasGetConfigReply = true; |
| 890 | checkSwitchReady(); |
| 891 | break; |
| 892 | case VENDOR: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 893 | shouldHandleMessage = handleVendorMessage((OFVendor) m); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 894 | break; |
| 895 | case ERROR: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 896 | log.debug("Recieved ERROR message from switch {}: {}", sw, m); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 897 | // TODO: we need better error handling. Especially for |
| 898 | // request/reply style message (stats, roles) we should have |
| 899 | // a unified way to lookup the xid in the error message. |
| 900 | // This will probable involve rewriting the way we handle |
| 901 | // request/reply style messages. |
| 902 | OFError error = (OFError) m; |
| 903 | boolean shouldLogError = true; |
| 904 | // TODO: should we check that firstRoleReplyReceived is false, |
| 905 | // i.e., check only whether the first request fails? |
| 906 | if (sw.checkFirstPendingRoleRequestXid(error.getXid())) { |
| 907 | boolean isBadVendorError = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 908 | (error.getErrorType() == OFError.OFErrorType. |
| 909 | OFPET_BAD_REQUEST.getValue()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 910 | // We expect to receive a bad vendor error when |
| 911 | // we're connected to a switch that doesn't support |
| 912 | // the Nicira vendor extensions (i.e. not OVS or |
| 913 | // derived from OVS). By protocol, it should also be |
| 914 | // BAD_VENDOR, but too many switch implementations |
| 915 | // get it wrong and we can already check the xid() |
| 916 | // so we can ignore the type with confidence that this |
| 917 | // is not a spurious error |
| 918 | shouldLogError = !isBadVendorError; |
| 919 | if (isBadVendorError) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 920 | log.debug("Handling bad vendor error for {}", sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 921 | if (state.firstRoleReplyReceived && (role != null)) { |
| 922 | log.warn("Received ERROR from sw {} that " |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 923 | + "indicates roles are not supported " |
| 924 | + "but we have received a valid " |
| 925 | + "role reply earlier", sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 926 | } |
| 927 | state.firstRoleReplyReceived = true; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 928 | Role requestedRole = |
| 929 | sw.deliverRoleRequestNotSupportedEx(error.getXid()); |
| 930 | synchronized (roleChanger) { |
| 931 | if (sw.role == null && Controller.this.role == Role.SLAVE) { |
| 932 | //This will now never happen. The Controller's role |
| 933 | //is now never SLAVE, always MASTER. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 934 | // the switch doesn't understand role request |
| 935 | // messages and the current controller role is |
| 936 | // slave. We need to disconnect the switch. |
| 937 | // @see RoleChanger for rationale |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 938 | log.warn("Closing {} channel because controller's role " + |
| 939 | "is SLAVE", sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 940 | sw.getChannel().close(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 941 | } else if (sw.role == null && requestedRole == Role.MASTER) { |
| 942 | log.debug("Adding switch {} because we got an error" + |
| 943 | " returned from a MASTER role request", sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 944 | // Controller's role is master: add to |
| 945 | // active |
| 946 | // TODO: check if clearing flow table is |
| 947 | // right choice here. |
| 948 | // Need to clear FlowMods before we add the switch |
| 949 | // and dispatch updates otherwise we have a race condition. |
| 950 | // TODO: switch update is async. Won't we still have a potential |
| 951 | // race condition? |
| 952 | sw.clearAllFlowMods(); |
| 953 | addSwitch(sw); |
| 954 | } |
| 955 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 956 | } else { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 957 | // TODO: Is this the right thing to do if we receive |
| 958 | // some other error besides a bad vendor error? |
| 959 | // Presumably that means the switch did actually |
| 960 | // understand the role request message, but there |
| 961 | // was some other error from processing the message. |
| 962 | // OF 1.2 specifies a OFPET_ROLE_REQUEST_FAILED |
| 963 | // error code, but it doesn't look like the Nicira |
| 964 | // role request has that. Should check OVS source |
| 965 | // code to see if it's possible for any other errors |
| 966 | // to be returned. |
| 967 | // If we received an error the switch is not |
| 968 | // in the correct role, so we need to disconnect it. |
| 969 | // We could also resend the request but then we need to |
| 970 | // check if there are other pending request in which |
| 971 | // case we shouldn't resend. If we do resend we need |
| 972 | // to make sure that the switch eventually accepts one |
| 973 | // of our requests or disconnect the switch. This feels |
| 974 | // cumbersome. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 975 | log.debug("Closing {} channel because we recieved an " + |
| 976 | "error other than BAD_VENDOR", sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 977 | sw.getChannel().close(); |
| 978 | } |
| 979 | } |
| 980 | // Once we support OF 1.2, we'd add code to handle it here. |
| 981 | //if (error.getXid() == state.ofRoleRequestXid) { |
| 982 | //} |
| 983 | if (shouldLogError) |
| 984 | logError(sw, error); |
| 985 | break; |
| 986 | case STATS_REPLY: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 987 | if (state.hsState.ordinal() < |
| 988 | HandshakeState.FEATURES_REPLY.ordinal()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 989 | String em = "Unexpected STATS_REPLY from " + sw; |
| 990 | throw new SwitchStateException(em); |
| 991 | } |
| 992 | sw.deliverStatisticsReply(m); |
| 993 | if (sw.hasAttribute(IOFSwitch.SWITCH_DESCRIPTION_FUTURE)) { |
| 994 | processSwitchDescReply(); |
| 995 | } |
| 996 | break; |
| 997 | case PORT_STATUS: |
| 998 | // We want to update our port state info even if we're in |
| 999 | // the slave role, but we only want to update storage if |
| 1000 | // we're the master (or equal). |
| 1001 | boolean updateStorage = state.hsState. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1002 | equals(HandshakeState.READY) && |
| 1003 | (sw.getRole() != Role.SLAVE); |
| 1004 | handlePortStatusMessage(sw, (OFPortStatus) m, updateStorage); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1005 | shouldHandleMessage = true; |
| 1006 | break; |
| 1007 | |
| 1008 | default: |
| 1009 | shouldHandleMessage = true; |
| 1010 | break; |
| 1011 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1012 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1013 | if (shouldHandleMessage) { |
| 1014 | sw.getListenerReadLock().lock(); |
| 1015 | try { |
| 1016 | if (sw.isConnected()) { |
| 1017 | if (!state.hsState.equals(HandshakeState.READY)) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1018 | log.debug("Ignoring message type {} received " + |
| 1019 | "from switch {} before switch is " + |
| 1020 | "fully configured.", m.getType(), sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1021 | } |
| 1022 | // Check if the controller is in the slave role for the |
| 1023 | // switch. If it is, then don't dispatch the message to |
| 1024 | // the listeners. |
| 1025 | // TODO: Should we dispatch messages that we expect to |
| 1026 | // receive when we're in the slave role, e.g. port |
| 1027 | // status messages? Since we're "hiding" switches from |
| 1028 | // the listeners when we're in the slave role, then it |
| 1029 | // seems a little weird to dispatch port status messages |
| 1030 | // to them. On the other hand there might be special |
| 1031 | // modules that care about all of the connected switches |
| 1032 | // and would like to receive port status notifications. |
| 1033 | else if (sw.getRole() == Role.SLAVE) { |
| 1034 | // Don't log message if it's a port status message |
| 1035 | // since we expect to receive those from the switch |
| 1036 | // and don't want to emit spurious messages. |
| 1037 | if (m.getType() != OFType.PORT_STATUS) { |
| 1038 | log.debug("Ignoring message type {} received " + |
| 1039 | "from switch {} while in the slave role.", |
| 1040 | m.getType(), sw); |
| 1041 | } |
| 1042 | } else { |
| 1043 | handleMessage(sw, m, null); |
| 1044 | } |
| 1045 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1046 | } finally { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1047 | sw.getListenerReadLock().unlock(); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | // **************** |
| 1054 | // Message handlers |
| 1055 | // **************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1056 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1057 | protected void handlePortStatusMessage(IOFSwitch sw, |
| 1058 | OFPortStatus m, |
| 1059 | boolean updateStorage) { |
| 1060 | short portNumber = m.getDesc().getPortNumber(); |
| 1061 | OFPhysicalPort port = m.getDesc(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1062 | if (m.getReason() == (byte) OFPortReason.OFPPR_MODIFY.ordinal()) { |
| 1063 | boolean portDown = ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) || |
| 1064 | ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1065 | sw.setPort(port); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1066 | if (!portDown) { |
| 1067 | SwitchUpdate update = new SwitchUpdate(sw, port, SwitchUpdateType.PORTADDED); |
| 1068 | try { |
| 1069 | this.updates.put(update); |
| 1070 | } catch (InterruptedException e) { |
| 1071 | log.error("Failure adding update to queue", e); |
| 1072 | } |
| 1073 | } else { |
| 1074 | SwitchUpdate update = new SwitchUpdate(sw, port, SwitchUpdateType.PORTREMOVED); |
| 1075 | try { |
| 1076 | this.updates.put(update); |
| 1077 | } catch (InterruptedException e) { |
| 1078 | log.error("Failure adding update to queue", e); |
| 1079 | } |
| 1080 | } |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 1081 | //if (updateStorage) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1082 | //updatePortInfo(sw, port); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1083 | log.debug("Port #{} modified for {}", portNumber, sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1084 | } else if (m.getReason() == (byte) OFPortReason.OFPPR_ADD.ordinal()) { |
| 1085 | // XXX Workaround to prevent race condition where a link is detected |
| 1086 | // and attempted to be written to the database before the port is in |
| 1087 | // the database. We now suppress link discovery on ports until we're |
| 1088 | // sure they're in the database. |
Pavlin Radoslavov | 7d21c0a | 2014-04-10 10:32:59 -0700 | [diff] [blame] | 1089 | linkDiscovery.addToSuppressLLDPs(sw.getId(), port.getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1090 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1091 | sw.setPort(port); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 1092 | SwitchUpdate update = new SwitchUpdate(sw, port, SwitchUpdateType.PORTADDED); |
| 1093 | try { |
| 1094 | this.updates.put(update); |
| 1095 | } catch (InterruptedException e) { |
| 1096 | log.error("Failure adding update to queue", e); |
| 1097 | } |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 1098 | //if (updateStorage) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1099 | //updatePortInfo(sw, port); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1100 | log.debug("Port #{} added for {}", portNumber, sw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1101 | } else if (m.getReason() == |
| 1102 | (byte) OFPortReason.OFPPR_DELETE.ordinal()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1103 | sw.deletePort(portNumber); |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 1104 | SwitchUpdate update = new SwitchUpdate(sw, port, SwitchUpdateType.PORTREMOVED); |
| 1105 | try { |
| 1106 | this.updates.put(update); |
| 1107 | } catch (InterruptedException e) { |
| 1108 | log.error("Failure adding update to queue", e); |
| 1109 | } |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 1110 | //if (updateStorage) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1111 | //removePortInfo(sw, portNumber); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1112 | log.debug("Port #{} deleted for {}", portNumber, sw); |
| 1113 | } |
| 1114 | SwitchUpdate update = new SwitchUpdate(sw, SwitchUpdateType.PORTCHANGED); |
| 1115 | try { |
| 1116 | this.updates.put(update); |
| 1117 | } catch (InterruptedException e) { |
| 1118 | log.error("Failure adding update to queue", e); |
| 1119 | } |
| 1120 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1121 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1122 | /** |
| 1123 | * flcontext_cache - Keep a thread local stack of contexts |
| 1124 | */ |
| 1125 | protected static final ThreadLocal<Stack<FloodlightContext>> flcontext_cache = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1126 | new ThreadLocal<Stack<FloodlightContext>>() { |
| 1127 | @Override |
| 1128 | protected Stack<FloodlightContext> initialValue() { |
| 1129 | return new Stack<FloodlightContext>(); |
| 1130 | } |
| 1131 | }; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1132 | |
| 1133 | /** |
| 1134 | * flcontext_alloc - pop a context off the stack, if required create a new one |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1135 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1136 | * @return FloodlightContext |
| 1137 | */ |
| 1138 | protected static FloodlightContext flcontext_alloc() { |
| 1139 | FloodlightContext flcontext = null; |
| 1140 | |
| 1141 | if (flcontext_cache.get().empty()) { |
| 1142 | flcontext = new FloodlightContext(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1143 | } else { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1144 | flcontext = flcontext_cache.get().pop(); |
| 1145 | } |
| 1146 | |
| 1147 | return flcontext; |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * flcontext_free - Free the context to the current thread |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1152 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1153 | * @param flcontext |
| 1154 | */ |
| 1155 | protected void flcontext_free(FloodlightContext flcontext) { |
| 1156 | flcontext.getStorage().clear(); |
| 1157 | flcontext_cache.get().push(flcontext); |
| 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * Handle replies to certain OFMessages, and pass others off to listeners |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1162 | * |
| 1163 | * @param sw The switch for the message |
| 1164 | * @param m The message |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1165 | * @param bContext The floodlight context. If null then floodlight context would |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1166 | * be allocated in this function |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1167 | * @throws IOException |
| 1168 | */ |
| 1169 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1170 | @LogMessageDoc(level = "ERROR", |
| 1171 | message = "Ignoring PacketIn (Xid = {xid}) because the data" + |
| 1172 | " field is empty.", |
| 1173 | explanation = "The switch sent an improperly-formatted PacketIn" + |
| 1174 | " message", |
| 1175 | recommendation = LogMessageDoc.CHECK_SWITCH), |
| 1176 | @LogMessageDoc(level = "WARN", |
| 1177 | message = "Unhandled OF Message: {} from {}", |
| 1178 | explanation = "The switch sent a message not handled by " + |
| 1179 | "the controller") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1180 | }) |
| 1181 | protected void handleMessage(IOFSwitch sw, OFMessage m, |
| 1182 | FloodlightContext bContext) |
| 1183 | throws IOException { |
| 1184 | Ethernet eth = null; |
| 1185 | |
| 1186 | switch (m.getType()) { |
| 1187 | case PACKET_IN: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1188 | OFPacketIn pi = (OFPacketIn) m; |
| 1189 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1190 | if (pi.getPacketData().length <= 0) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1191 | log.error("Ignoring PacketIn (Xid = " + pi.getXid() + |
| 1192 | ") because the data field is empty."); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1193 | return; |
| 1194 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1195 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1196 | if (Controller.ALWAYS_DECODE_ETH) { |
| 1197 | eth = new Ethernet(); |
| 1198 | eth.deserialize(pi.getPacketData(), 0, |
| 1199 | pi.getPacketData().length); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1200 | } |
| 1201 | // fall through to default case... |
| 1202 | |
| 1203 | default: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1204 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1205 | List<IOFMessageListener> listeners = null; |
| 1206 | if (messageListeners.containsKey(m.getType())) { |
| 1207 | listeners = messageListeners.get(m.getType()). |
| 1208 | getOrderedListeners(); |
| 1209 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1210 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1211 | FloodlightContext bc = null; |
| 1212 | if (listeners != null) { |
| 1213 | // Check if floodlight context is passed from the calling |
| 1214 | // function, if so use that floodlight context, otherwise |
| 1215 | // allocate one |
| 1216 | if (bContext == null) { |
| 1217 | bc = flcontext_alloc(); |
| 1218 | } else { |
| 1219 | bc = bContext; |
| 1220 | } |
| 1221 | if (eth != null) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1222 | IFloodlightProviderService.bcStore.put(bc, |
| 1223 | IFloodlightProviderService.CONTEXT_PI_PAYLOAD, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1224 | eth); |
| 1225 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1226 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1227 | // Get the starting time (overall and per-component) of |
| 1228 | // the processing chain for this packet if performance |
| 1229 | // monitoring is turned on |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1230 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1231 | Command cmd; |
| 1232 | for (IOFMessageListener listener : listeners) { |
| 1233 | if (listener instanceof IOFSwitchFilter) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1234 | if (!((IOFSwitchFilter) listener).isInterested(sw)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1235 | continue; |
| 1236 | } |
| 1237 | } |
| 1238 | |
mininet | 73e7fb7 | 2013-12-03 14:25:53 -0800 | [diff] [blame] | 1239 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1240 | cmd = listener.receive(sw, m, bc); |
mininet | 73e7fb7 | 2013-12-03 14:25:53 -0800 | [diff] [blame] | 1241 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1242 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1243 | if (Command.STOP.equals(cmd)) { |
| 1244 | break; |
| 1245 | } |
| 1246 | } |
mininet | 73e7fb7 | 2013-12-03 14:25:53 -0800 | [diff] [blame] | 1247 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1248 | } else { |
| 1249 | log.warn("Unhandled OF Message: {} from {}", m, sw); |
| 1250 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1251 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1252 | if ((bContext == null) && (bc != null)) flcontext_free(bc); |
| 1253 | } |
| 1254 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1255 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1256 | /** |
| 1257 | * Log an OpenFlow error message from a switch |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1258 | * |
| 1259 | * @param sw The switch that sent the error |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1260 | * @param error The error message |
| 1261 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1262 | @LogMessageDoc(level = "ERROR", |
| 1263 | message = "Error {error type} {error code} from {switch}", |
| 1264 | explanation = "The switch responded with an unexpected error" + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1265 | "to an OpenFlow message from the controller", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1266 | recommendation = "This could indicate improper network operation. " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1267 | "If the problem persists restarting the switch and " + |
| 1268 | "controller may help." |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1269 | ) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1270 | protected void logError(IOFSwitch sw, OFError error) { |
| 1271 | int etint = 0xffff & error.getErrorType(); |
| 1272 | if (etint < 0 || etint >= OFErrorType.values().length) { |
| 1273 | log.error("Unknown error code {} from sw {}", etint, sw); |
| 1274 | } |
| 1275 | OFErrorType et = OFErrorType.values()[etint]; |
| 1276 | switch (et) { |
| 1277 | case OFPET_HELLO_FAILED: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1278 | OFHelloFailedCode hfc = |
| 1279 | OFHelloFailedCode.values()[0xffff & error.getErrorCode()]; |
| 1280 | log.error("Error {} {} from {}", new Object[]{et, hfc, sw}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1281 | break; |
| 1282 | case OFPET_BAD_REQUEST: |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1283 | OFBadRequestCode brc = |
| 1284 | OFBadRequestCode.values()[0xffff & error.getErrorCode()]; |
| 1285 | log.error("Error {} {} from {}", new Object[]{et, brc, sw}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1286 | break; |
| 1287 | case OFPET_BAD_ACTION: |
| 1288 | OFBadActionCode bac = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1289 | OFBadActionCode.values()[0xffff & error.getErrorCode()]; |
| 1290 | log.error("Error {} {} from {}", new Object[]{et, bac, sw}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1291 | break; |
| 1292 | case OFPET_FLOW_MOD_FAILED: |
| 1293 | OFFlowModFailedCode fmfc = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1294 | OFFlowModFailedCode.values()[0xffff & error.getErrorCode()]; |
| 1295 | log.error("Error {} {} from {}", new Object[]{et, fmfc, sw}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1296 | break; |
| 1297 | case OFPET_PORT_MOD_FAILED: |
| 1298 | OFPortModFailedCode pmfc = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1299 | OFPortModFailedCode.values()[0xffff & error.getErrorCode()]; |
| 1300 | log.error("Error {} {} from {}", new Object[]{et, pmfc, sw}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1301 | break; |
| 1302 | case OFPET_QUEUE_OP_FAILED: |
| 1303 | OFQueueOpFailedCode qofc = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1304 | OFQueueOpFailedCode.values()[0xffff & error.getErrorCode()]; |
| 1305 | log.error("Error {} {} from {}", new Object[]{et, qofc, sw}); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1306 | break; |
| 1307 | default: |
| 1308 | break; |
| 1309 | } |
| 1310 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1311 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1312 | /** |
| 1313 | * Add a switch to the active switch list and call the switch listeners. |
| 1314 | * This happens either when a switch first connects (and the controller is |
| 1315 | * not in the slave role) or when the role of the controller changes from |
| 1316 | * slave to master. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1317 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1318 | * @param sw the switch that has been added |
| 1319 | */ |
| 1320 | // TODO: need to rethink locking and the synchronous switch update. |
| 1321 | // We can / should also handle duplicate DPIDs in connectedSwitches |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1322 | @LogMessageDoc(level = "ERROR", |
| 1323 | message = "New switch added {switch} for already-added switch {switch}", |
| 1324 | explanation = "A switch with the same DPID as another switch " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1325 | "connected to the controller. This can be caused by " + |
| 1326 | "multiple switches configured with the same DPID, or " + |
| 1327 | "by a switch reconnected very quickly after " + |
| 1328 | "disconnecting.", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1329 | recommendation = "If this happens repeatedly, it is likely there " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1330 | "are switches with duplicate DPIDs on the network. " + |
| 1331 | "Reconfigure the appropriate switches. If it happens " + |
| 1332 | "very rarely, then it is likely this is a transient " + |
| 1333 | "network problem that can be ignored." |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1334 | ) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1335 | protected void addSwitch(IOFSwitch sw) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1336 | // XXX Workaround to prevent race condition where a link is detected |
| 1337 | // and attempted to be written to the database before the port is in |
| 1338 | // the database. We now suppress link discovery on ports until we're |
| 1339 | // sure they're in the database. |
| 1340 | for (OFPhysicalPort port : sw.getPorts()) { |
Pavlin Radoslavov | 7d21c0a | 2014-04-10 10:32:59 -0700 | [diff] [blame] | 1341 | linkDiscovery.addToSuppressLLDPs(sw.getId(), port.getPortNumber()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1344 | // TODO: is it safe to modify the HashMap without holding |
| 1345 | // the old switch's lock? |
| 1346 | OFSwitchImpl oldSw = (OFSwitchImpl) this.activeSwitches.put(sw.getId(), sw); |
| 1347 | if (sw == oldSw) { |
| 1348 | // Note == for object equality, not .equals for value |
| 1349 | log.info("New add switch for pre-existing switch {}", sw); |
| 1350 | return; |
| 1351 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1352 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1353 | if (oldSw != null) { |
| 1354 | oldSw.getListenerWriteLock().lock(); |
| 1355 | try { |
| 1356 | log.error("New switch added {} for already-added switch {}", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1357 | sw, oldSw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1358 | // Set the connected flag to false to suppress calling |
| 1359 | // the listeners for this switch in processOFMessage |
| 1360 | oldSw.setConnected(false); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1361 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1362 | oldSw.cancelAllStatisticsReplies(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1363 | |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 1364 | //updateInactiveSwitchInfo(oldSw); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1365 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1366 | // we need to clean out old switch state definitively |
| 1367 | // before adding the new switch |
| 1368 | // FIXME: It seems not completely kosher to call the |
| 1369 | // switch listeners here. I thought one of the points of |
| 1370 | // having the asynchronous switch update mechanism was so |
| 1371 | // the addedSwitch and removedSwitch were always called |
| 1372 | // from a single thread to simplify concurrency issues |
| 1373 | // for the listener. |
| 1374 | if (switchListeners != null) { |
| 1375 | for (IOFSwitchListener listener : switchListeners) { |
| 1376 | listener.removedSwitch(oldSw); |
| 1377 | } |
| 1378 | } |
| 1379 | // will eventually trigger a removeSwitch(), which will cause |
| 1380 | // a "Not removing Switch ... already removed debug message. |
| 1381 | // TODO: Figure out a way to handle this that avoids the |
| 1382 | // spurious debug message. |
Jonathan Hart | 9e92c51 | 2013-03-20 16:24:44 -0700 | [diff] [blame] | 1383 | log.debug("Closing {} because a new IOFSwitch got added " + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1384 | "for this dpid", oldSw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1385 | oldSw.getChannel().close(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1386 | } finally { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1387 | oldSw.getListenerWriteLock().unlock(); |
| 1388 | } |
| 1389 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1390 | |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 1391 | //updateActiveSwitchInfo(sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1392 | SwitchUpdate update = new SwitchUpdate(sw, SwitchUpdateType.ADDED); |
| 1393 | try { |
| 1394 | this.updates.put(update); |
| 1395 | } catch (InterruptedException e) { |
| 1396 | log.error("Failure adding update to queue", e); |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | /** |
| 1401 | * Remove a switch from the active switch list and call the switch listeners. |
| 1402 | * This happens either when the switch is disconnected or when the |
| 1403 | * controller's role for the switch changes from master to slave. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1404 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1405 | * @param sw the switch that has been removed |
| 1406 | */ |
| 1407 | protected void removeSwitch(IOFSwitch sw) { |
| 1408 | // No need to acquire the listener lock, since |
| 1409 | // this method is only called after netty has processed all |
| 1410 | // pending messages |
| 1411 | log.debug("removeSwitch: {}", sw); |
| 1412 | if (!this.activeSwitches.remove(sw.getId(), sw) || !sw.isConnected()) { |
| 1413 | log.debug("Not removing switch {}; already removed", sw); |
| 1414 | return; |
| 1415 | } |
| 1416 | // We cancel all outstanding statistics replies if the switch transition |
| 1417 | // from active. In the future we might allow statistics requests |
| 1418 | // from slave controllers. Then we need to move this cancelation |
| 1419 | // to switch disconnect |
| 1420 | sw.cancelAllStatisticsReplies(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1421 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1422 | // FIXME: I think there's a race condition if we call updateInactiveSwitchInfo |
| 1423 | // here if role support is enabled. In that case if the switch is being |
| 1424 | // removed because we've been switched to being in the slave role, then I think |
| 1425 | // it's possible that the new master may have already been promoted to master |
| 1426 | // and written out the active switch state to storage. If we now execute |
| 1427 | // updateInactiveSwitchInfo we may wipe out all of the state that was |
| 1428 | // written out by the new master. Maybe need to revisit how we handle all |
| 1429 | // of the switch state that's written to storage. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1430 | |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 1431 | //updateInactiveSwitchInfo(sw); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1432 | SwitchUpdate update = new SwitchUpdate(sw, SwitchUpdateType.REMOVED); |
| 1433 | try { |
| 1434 | this.updates.put(update); |
| 1435 | } catch (InterruptedException e) { |
| 1436 | log.error("Failure adding update to queue", e); |
| 1437 | } |
| 1438 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1439 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1440 | // *************** |
| 1441 | // IFloodlightProvider |
| 1442 | // *************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1443 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1444 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1445 | public synchronized void addOFMessageListener(OFType type, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1446 | IOFMessageListener listener) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1447 | ListenerDispatcher<OFType, IOFMessageListener> ldd = |
| 1448 | messageListeners.get(type); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1449 | if (ldd == null) { |
| 1450 | ldd = new ListenerDispatcher<OFType, IOFMessageListener>(); |
| 1451 | messageListeners.put(type, ldd); |
| 1452 | } |
| 1453 | ldd.addListener(type, listener); |
| 1454 | } |
| 1455 | |
| 1456 | @Override |
| 1457 | public synchronized void removeOFMessageListener(OFType type, |
| 1458 | IOFMessageListener listener) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1459 | ListenerDispatcher<OFType, IOFMessageListener> ldd = |
| 1460 | messageListeners.get(type); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1461 | if (ldd != null) { |
| 1462 | ldd.removeListener(listener); |
| 1463 | } |
| 1464 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1465 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1466 | private void logListeners() { |
| 1467 | for (Map.Entry<OFType, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1468 | ListenerDispatcher<OFType, |
| 1469 | IOFMessageListener>> entry |
| 1470 | : messageListeners.entrySet()) { |
| 1471 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1472 | OFType type = entry.getKey(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1473 | ListenerDispatcher<OFType, IOFMessageListener> ldd = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1474 | entry.getValue(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1475 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1476 | StringBuffer sb = new StringBuffer(); |
| 1477 | sb.append("OFListeners for "); |
| 1478 | sb.append(type); |
| 1479 | sb.append(": "); |
| 1480 | for (IOFMessageListener l : ldd.getOrderedListeners()) { |
| 1481 | sb.append(l.getName()); |
| 1482 | sb.append(","); |
| 1483 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1484 | log.debug(sb.toString()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1485 | } |
| 1486 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1487 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1488 | public void removeOFMessageListeners(OFType type) { |
| 1489 | messageListeners.remove(type); |
| 1490 | } |
| 1491 | |
| 1492 | @Override |
| 1493 | public Map<Long, IOFSwitch> getSwitches() { |
| 1494 | return Collections.unmodifiableMap(this.activeSwitches); |
| 1495 | } |
| 1496 | |
| 1497 | @Override |
| 1498 | public void addOFSwitchListener(IOFSwitchListener listener) { |
| 1499 | this.switchListeners.add(listener); |
| 1500 | } |
| 1501 | |
| 1502 | @Override |
| 1503 | public void removeOFSwitchListener(IOFSwitchListener listener) { |
| 1504 | this.switchListeners.remove(listener); |
| 1505 | } |
| 1506 | |
| 1507 | @Override |
| 1508 | public Map<OFType, List<IOFMessageListener>> getListeners() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1509 | Map<OFType, List<IOFMessageListener>> lers = |
| 1510 | new HashMap<OFType, List<IOFMessageListener>>(); |
| 1511 | for (Entry<OFType, ListenerDispatcher<OFType, IOFMessageListener>> e : |
| 1512 | messageListeners.entrySet()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1513 | lers.put(e.getKey(), e.getValue().getOrderedListeners()); |
| 1514 | } |
| 1515 | return Collections.unmodifiableMap(lers); |
| 1516 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1517 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1518 | @Override |
| 1519 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1520 | @LogMessageDoc(message = "Failed to inject OFMessage {message} onto " + |
| 1521 | "a null switch", |
| 1522 | explanation = "Failed to process a message because the switch " + |
| 1523 | " is no longer connected."), |
| 1524 | @LogMessageDoc(level = "ERROR", |
| 1525 | message = "Error reinjecting OFMessage on switch {switch}", |
| 1526 | explanation = "An I/O error occured while attempting to " + |
| 1527 | "process an OpenFlow message", |
| 1528 | recommendation = LogMessageDoc.CHECK_SWITCH) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1529 | }) |
| 1530 | public boolean injectOfMessage(IOFSwitch sw, OFMessage msg, |
| 1531 | FloodlightContext bc) { |
| 1532 | if (sw == null) { |
| 1533 | log.info("Failed to inject OFMessage {} onto a null switch", msg); |
| 1534 | return false; |
| 1535 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1536 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1537 | // FIXME: Do we need to be able to inject messages to switches |
| 1538 | // where we're the slave controller (i.e. they're connected but |
| 1539 | // not active)? |
| 1540 | // FIXME: Don't we need synchronization logic here so we're holding |
| 1541 | // the listener read lock when we call handleMessage? After some |
| 1542 | // discussions it sounds like the right thing to do here would be to |
| 1543 | // inject the message as a netty upstream channel event so it goes |
| 1544 | // through the normal netty event processing, including being |
| 1545 | // handled |
| 1546 | if (!activeSwitches.containsKey(sw.getId())) return false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1547 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1548 | try { |
| 1549 | // Pass Floodlight context to the handleMessages() |
| 1550 | handleMessage(sw, msg, bc); |
| 1551 | } catch (IOException e) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1552 | log.error("Error reinjecting OFMessage on switch {}", |
| 1553 | HexString.toHexString(sw.getId())); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1554 | return false; |
| 1555 | } |
| 1556 | return true; |
| 1557 | } |
| 1558 | |
| 1559 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1560 | @LogMessageDoc(message = "Calling System.exit", |
| 1561 | explanation = "The controller is terminating") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1562 | public synchronized void terminate() { |
| 1563 | log.info("Calling System.exit"); |
| 1564 | System.exit(1); |
| 1565 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1566 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1567 | @Override |
| 1568 | public boolean injectOfMessage(IOFSwitch sw, OFMessage msg) { |
| 1569 | // call the overloaded version with floodlight context set to null |
| 1570 | return injectOfMessage(sw, msg, null); |
| 1571 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1572 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1573 | @Override |
| 1574 | public void handleOutgoingMessage(IOFSwitch sw, OFMessage m, |
| 1575 | FloodlightContext bc) { |
| 1576 | if (log.isTraceEnabled()) { |
| 1577 | String str = OFMessage.getDataAsString(sw, m, bc); |
| 1578 | log.trace("{}", str); |
| 1579 | } |
| 1580 | |
| 1581 | List<IOFMessageListener> listeners = null; |
| 1582 | if (messageListeners.containsKey(m.getType())) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1583 | listeners = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1584 | messageListeners.get(m.getType()).getOrderedListeners(); |
| 1585 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1586 | |
| 1587 | if (listeners != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1588 | for (IOFMessageListener listener : listeners) { |
| 1589 | if (listener instanceof IOFSwitchFilter) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1590 | if (!((IOFSwitchFilter) listener).isInterested(sw)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1591 | continue; |
| 1592 | } |
| 1593 | } |
| 1594 | if (Command.STOP.equals(listener.receive(sw, m, bc))) { |
| 1595 | break; |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | @Override |
| 1602 | public BasicFactory getOFMessageFactory() { |
| 1603 | return factory; |
| 1604 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1605 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1606 | @Override |
| 1607 | public String getControllerId() { |
| 1608 | return controllerId; |
| 1609 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1610 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1611 | // ************** |
| 1612 | // Initialization |
| 1613 | // ************** |
| 1614 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1615 | /** |
| 1616 | * Sets the initial role based on properties in the config params. |
| 1617 | * It looks for two different properties. |
| 1618 | * If the "role" property is specified then the value should be |
| 1619 | * either "EQUAL", "MASTER", or "SLAVE" and the role of the |
| 1620 | * controller is set to the specified value. If the "role" property |
| 1621 | * is not specified then it looks next for the "role.path" property. |
| 1622 | * In this case the value should be the path to a property file in |
| 1623 | * the file system that contains a property called "floodlight.role" |
| 1624 | * which can be one of the values listed above for the "role" property. |
| 1625 | * The idea behind the "role.path" mechanism is that you have some |
| 1626 | * separate heartbeat and master controller election algorithm that |
| 1627 | * determines the role of the controller. When a role transition happens, |
| 1628 | * it updates the current role in the file specified by the "role.path" |
| 1629 | * file. Then if floodlight restarts for some reason it can get the |
| 1630 | * correct current role of the controller from the file. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1631 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1632 | * @param configParams The config params for the FloodlightProvider service |
| 1633 | * @return A valid role if role information is specified in the |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1634 | * config params, otherwise null |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1635 | */ |
| 1636 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1637 | @LogMessageDoc(message = "Controller role set to {role}", |
| 1638 | explanation = "Setting the initial HA role to "), |
| 1639 | @LogMessageDoc(level = "ERROR", |
| 1640 | message = "Invalid current role value: {role}", |
| 1641 | explanation = "An invalid HA role value was read from the " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1642 | "properties file", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1643 | recommendation = LogMessageDoc.CHECK_CONTROLLER) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1644 | }) |
| 1645 | protected Role getInitialRole(Map<String, String> configParams) { |
| 1646 | Role role = null; |
| 1647 | String roleString = configParams.get("role"); |
| 1648 | if (roleString == null) { |
| 1649 | String rolePath = configParams.get("rolepath"); |
| 1650 | if (rolePath != null) { |
| 1651 | Properties properties = new Properties(); |
| 1652 | try { |
| 1653 | properties.load(new FileInputStream(rolePath)); |
| 1654 | roleString = properties.getProperty("floodlight.role"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1655 | } catch (IOException exc) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1656 | // Don't treat it as an error if the file specified by the |
| 1657 | // rolepath property doesn't exist. This lets us enable the |
| 1658 | // HA mechanism by just creating/setting the floodlight.role |
| 1659 | // property in that file without having to modify the |
| 1660 | // floodlight properties. |
| 1661 | } |
| 1662 | } |
| 1663 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1664 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1665 | if (roleString != null) { |
| 1666 | // Canonicalize the string to the form used for the enum constants |
| 1667 | roleString = roleString.trim().toUpperCase(); |
| 1668 | try { |
| 1669 | role = Role.valueOf(roleString); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1670 | } catch (IllegalArgumentException exc) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1671 | log.error("Invalid current role value: {}", roleString); |
| 1672 | } |
| 1673 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1674 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1675 | log.info("Controller role set to {}", role); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1676 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1677 | return role; |
| 1678 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1679 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1680 | /** |
| 1681 | * Tell controller that we're ready to accept switches loop |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1682 | * |
| 1683 | * @throws IOException |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1684 | */ |
| 1685 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1686 | @LogMessageDoc(message = "Listening for switch connections on {address}", |
| 1687 | explanation = "The controller is ready and listening for new" + |
| 1688 | " switch connections"), |
| 1689 | @LogMessageDoc(message = "Storage exception in controller " + |
| 1690 | "updates loop; terminating process", |
| 1691 | explanation = ERROR_DATABASE, |
| 1692 | recommendation = LogMessageDoc.CHECK_CONTROLLER), |
| 1693 | @LogMessageDoc(level = "ERROR", |
| 1694 | message = "Exception in controller updates loop", |
| 1695 | explanation = "Failed to dispatch controller event", |
| 1696 | recommendation = LogMessageDoc.GENERIC_ACTION) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1697 | }) |
| 1698 | public void run() { |
| 1699 | if (log.isDebugEnabled()) { |
| 1700 | logListeners(); |
| 1701 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1702 | |
| 1703 | try { |
| 1704 | final ServerBootstrap bootstrap = createServerBootStrap(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1705 | |
| 1706 | bootstrap.setOption("reuseAddr", true); |
| 1707 | bootstrap.setOption("child.keepAlive", true); |
| 1708 | bootstrap.setOption("child.tcpNoDelay", true); |
| 1709 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 1710 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1711 | ChannelPipelineFactory pfact = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1712 | new OpenflowPipelineFactory(this, null); |
| 1713 | bootstrap.setPipelineFactory(pfact); |
| 1714 | InetSocketAddress sa = new InetSocketAddress(openFlowPort); |
| 1715 | final ChannelGroup cg = new DefaultChannelGroup(); |
| 1716 | cg.add(bootstrap.bind(sa)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1717 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1718 | log.info("Listening for switch connections on {}", sa); |
| 1719 | } catch (Exception e) { |
| 1720 | throw new RuntimeException(e); |
| 1721 | } |
| 1722 | |
| 1723 | // main loop |
| 1724 | while (true) { |
| 1725 | try { |
| 1726 | IUpdate update = updates.take(); |
| 1727 | update.dispatch(); |
| 1728 | } catch (InterruptedException e) { |
| 1729 | return; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1730 | } catch (Exception e) { |
| 1731 | log.error("Exception in controller updates loop", e); |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | private ServerBootstrap createServerBootStrap() { |
| 1737 | if (workerThreads == 0) { |
| 1738 | return new ServerBootstrap( |
| 1739 | new NioServerSocketChannelFactory( |
| 1740 | Executors.newCachedThreadPool(), |
| 1741 | Executors.newCachedThreadPool())); |
| 1742 | } else { |
| 1743 | return new ServerBootstrap( |
| 1744 | new NioServerSocketChannelFactory( |
| 1745 | Executors.newCachedThreadPool(), |
| 1746 | Executors.newCachedThreadPool(), workerThreads)); |
| 1747 | } |
| 1748 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1749 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1750 | public void setConfigParams(Map<String, String> configParams) { |
| 1751 | String ofPort = configParams.get("openflowport"); |
| 1752 | if (ofPort != null) { |
| 1753 | this.openFlowPort = Integer.parseInt(ofPort); |
| 1754 | } |
| 1755 | log.debug("OpenFlow port set to {}", this.openFlowPort); |
| 1756 | String threads = configParams.get("workerthreads"); |
| 1757 | if (threads != null) { |
| 1758 | this.workerThreads = Integer.parseInt(threads); |
| 1759 | } |
| 1760 | log.debug("Number of worker threads set to {}", this.workerThreads); |
| 1761 | String controllerId = configParams.get("controllerid"); |
| 1762 | if (controllerId != null) { |
| 1763 | this.controllerId = controllerId; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1764 | } else { |
| 1765 | //Try to get the hostname of the machine and use that for controller ID |
| 1766 | try { |
| 1767 | String hostname = java.net.InetAddress.getLocalHost().getHostName(); |
| 1768 | this.controllerId = hostname; |
| 1769 | } catch (UnknownHostException e) { |
| 1770 | // Can't get hostname, we'll just use the default |
| 1771 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1772 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1773 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1774 | log.debug("ControllerId set to {}", this.controllerId); |
| 1775 | } |
| 1776 | |
| 1777 | private void initVendorMessages() { |
| 1778 | // Configure openflowj to be able to parse the role request/reply |
| 1779 | // vendor messages. |
| 1780 | OFBasicVendorId niciraVendorId = new OFBasicVendorId( |
| 1781 | OFNiciraVendorData.NX_VENDOR_ID, 4); |
| 1782 | OFVendorId.registerVendorId(niciraVendorId); |
| 1783 | OFBasicVendorDataType roleRequestVendorData = |
| 1784 | new OFBasicVendorDataType( |
| 1785 | OFRoleRequestVendorData.NXT_ROLE_REQUEST, |
| 1786 | OFRoleRequestVendorData.getInstantiable()); |
| 1787 | niciraVendorId.registerVendorDataType(roleRequestVendorData); |
| 1788 | OFBasicVendorDataType roleReplyVendorData = |
| 1789 | new OFBasicVendorDataType( |
| 1790 | OFRoleReplyVendorData.NXT_ROLE_REPLY, |
| 1791 | OFRoleReplyVendorData.getInstantiable()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1792 | niciraVendorId.registerVendorDataType(roleReplyVendorData); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1793 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1794 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1795 | /** |
| 1796 | * Initialize internal data structures |
| 1797 | */ |
| 1798 | public void init(Map<String, String> configParams) { |
| 1799 | // These data structures are initialized here because other |
| 1800 | // module's startUp() might be called before ours |
| 1801 | this.messageListeners = |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1802 | new ConcurrentHashMap<OFType, |
| 1803 | ListenerDispatcher<OFType, |
| 1804 | IOFMessageListener>>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1805 | this.switchListeners = new CopyOnWriteArraySet<IOFSwitchListener>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1806 | this.activeSwitches = new ConcurrentHashMap<Long, IOFSwitch>(); |
| 1807 | this.connectedSwitches = new HashSet<OFSwitchImpl>(); |
| 1808 | this.controllerNodeIPsCache = new HashMap<String, String>(); |
| 1809 | this.updates = new LinkedBlockingQueue<IUpdate>(); |
| 1810 | this.factory = new BasicFactory(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1811 | setConfigParams(configParams); |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 1812 | //Set the controller's role to MASTER so it always tries to do role requests. |
| 1813 | this.role = Role.MASTER; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1814 | this.roleChanger = new RoleChanger(); |
| 1815 | initVendorMessages(); |
| 1816 | this.systemStartTime = System.currentTimeMillis(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1817 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1818 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1819 | /** |
| 1820 | * Startup all of the controller's components |
| 1821 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1822 | @LogMessageDoc(message = "Waiting for storage source", |
| 1823 | explanation = "The system database is not yet ready", |
| 1824 | recommendation = "If this message persists, this indicates " + |
| 1825 | "that the system database has failed to start. " + |
| 1826 | LogMessageDoc.CHECK_CONTROLLER) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1827 | public void startupComponents() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1828 | try { |
| 1829 | registryService.registerController(controllerId); |
| 1830 | } catch (RegistryException e) { |
| 1831 | log.warn("Registry service error: {}", e.getMessage()); |
| 1832 | } |
| 1833 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1834 | // Add our REST API |
| 1835 | restApi.addRestletRoutable(new CoreWebRoutable()); |
| 1836 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1837 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1838 | @Override |
| 1839 | public Map<String, String> getControllerNodeIPs() { |
| 1840 | // We return a copy of the mapping so we can guarantee that |
| 1841 | // the mapping return is the same as one that will be (or was) |
| 1842 | // dispatched to IHAListeners |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1843 | HashMap<String, String> retval = new HashMap<String, String>(); |
| 1844 | synchronized (controllerNodeIPsCache) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1845 | retval.putAll(controllerNodeIPsCache); |
| 1846 | } |
| 1847 | return retval; |
| 1848 | } |
| 1849 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1850 | @Override |
| 1851 | public long getSystemStartTime() { |
| 1852 | return (this.systemStartTime); |
| 1853 | } |
| 1854 | |
| 1855 | @Override |
| 1856 | public void setAlwaysClearFlowsOnSwAdd(boolean value) { |
| 1857 | this.alwaysClearFlowsOnSwAdd = value; |
| 1858 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1859 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1860 | public boolean getAlwaysClearFlowsOnSwAdd() { |
| 1861 | return this.alwaysClearFlowsOnSwAdd; |
| 1862 | } |
| 1863 | } |