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