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