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; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 22 | import java.lang.management.ManagementFactory; |
| 23 | import java.lang.management.RuntimeMXBean; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | import java.net.InetSocketAddress; |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 25 | import java.net.UnknownHostException; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | import java.util.Collections; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 27 | import java.util.HashMap; |
| 28 | import java.util.HashSet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 29 | import java.util.List; |
| 30 | import java.util.Map; |
| 31 | import java.util.Map.Entry; |
| 32 | import java.util.Properties; |
| 33 | import java.util.Set; |
| 34 | import java.util.Stack; |
| 35 | import java.util.concurrent.BlockingQueue; |
| 36 | import java.util.concurrent.ConcurrentHashMap; |
| 37 | import java.util.concurrent.ConcurrentMap; |
| 38 | import java.util.concurrent.CopyOnWriteArraySet; |
| 39 | import java.util.concurrent.Executors; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 40 | import java.util.concurrent.LinkedBlockingQueue; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 41 | |
| 42 | import net.floodlightcontroller.core.FloodlightContext; |
| 43 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 44 | import net.floodlightcontroller.core.IListener.Command; |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 45 | import net.floodlightcontroller.core.IOFMessageListener; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 46 | import net.floodlightcontroller.core.IOFSwitch; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 47 | import net.floodlightcontroller.core.IOFSwitch.PortChangeType; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 48 | import net.floodlightcontroller.core.IOFSwitchFilter; |
| 49 | import net.floodlightcontroller.core.IOFSwitchListener; |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 50 | import net.floodlightcontroller.core.IUpdate; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 51 | import net.floodlightcontroller.core.annotations.LogMessageDoc; |
| 52 | import net.floodlightcontroller.core.annotations.LogMessageDocs; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 53 | import net.floodlightcontroller.core.internal.OFChannelHandler.RoleRecvStatus; |
| 54 | import net.floodlightcontroller.core.module.FloodlightModuleException; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 55 | import net.floodlightcontroller.core.util.ListenerDispatcher; |
| 56 | import net.floodlightcontroller.core.web.CoreWebRoutable; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 57 | import net.floodlightcontroller.debugcounter.IDebugCounter; |
| 58 | import net.floodlightcontroller.debugcounter.IDebugCounterService; |
| 59 | import net.floodlightcontroller.debugcounter.IDebugCounterService.CounterException; |
| 60 | import net.floodlightcontroller.debugcounter.IDebugCounterService.CounterType; |
| 61 | import net.floodlightcontroller.debugevent.IDebugEventService; |
| 62 | import net.floodlightcontroller.debugevent.IDebugEventService.EventColumn; |
| 63 | import net.floodlightcontroller.debugevent.IDebugEventService.EventFieldType; |
| 64 | import net.floodlightcontroller.debugevent.IDebugEventService.EventType; |
| 65 | import net.floodlightcontroller.debugevent.IDebugEventService.MaxEventsRegistered; |
| 66 | import net.floodlightcontroller.debugevent.IEventUpdater; |
| 67 | import net.floodlightcontroller.debugevent.NullDebugEvent; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 68 | import net.floodlightcontroller.restserver.IRestApiService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 69 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 70 | import net.floodlightcontroller.util.LoadMonitor; |
| 71 | import net.onrc.onos.core.drivermanager.DriverManager; |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 72 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 73 | import net.onrc.onos.core.packet.Ethernet; |
| 74 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 75 | import net.onrc.onos.core.registry.IControllerRegistryService.ControlChangeCallback; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 76 | import net.onrc.onos.core.registry.RegistryException; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 77 | import net.onrc.onos.core.util.Dpid; |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 78 | import net.onrc.onos.core.util.OnosInstanceId; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 79 | |
| 80 | import org.jboss.netty.bootstrap.ServerBootstrap; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 81 | import org.jboss.netty.channel.ChannelPipelineFactory; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 82 | import org.jboss.netty.channel.group.ChannelGroup; |
| 83 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 84 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 85 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 86 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 87 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 88 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 89 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 90 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 91 | import org.projectfloodlight.openflow.protocol.OFType; |
| 92 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 93 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 94 | import org.projectfloodlight.openflow.protocol.match.MatchFields; |
| 95 | import org.projectfloodlight.openflow.types.EthType; |
| 96 | import org.projectfloodlight.openflow.types.OFPort; |
| 97 | import org.projectfloodlight.openflow.util.HexString; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 98 | import org.slf4j.Logger; |
| 99 | import org.slf4j.LoggerFactory; |
| 100 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 101 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 102 | * The main controller class. Handles all setup and network listeners - |
| 103 | * Distributed ownership control of switch through IControllerRegistryService |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | */ |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 105 | public class Controller implements IFloodlightProviderService { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 106 | |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 107 | protected final static Logger log = LoggerFactory.getLogger(Controller.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 108 | static final String ERROR_DATABASE = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 109 | "The controller could not communicate with the system database."; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 110 | protected static OFFactory factory13 = OFFactories.getFactory(OFVersion.OF_13); |
| 111 | protected static OFFactory factory10 = OFFactories.getFactory(OFVersion.OF_10); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 113 | // connectedSwitches cache contains all connected switch's channelHandlers |
| 114 | // including ones where this controller is a master/equal/slave controller |
| 115 | // as well as ones that have not been activated yet |
| 116 | protected ConcurrentHashMap<Long, OFChannelHandler> connectedSwitches; |
| 117 | // These caches contains only those switches that are active |
| 118 | protected ConcurrentHashMap<Long, IOFSwitch> activeMasterSwitches; |
| 119 | protected ConcurrentHashMap<Long, IOFSwitch> activeEqualSwitches; |
| 120 | // lock to synchronize on, when manipulating multiple caches above |
| 121 | private Object multiCacheLock; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 123 | // The controllerNodeIPsCache maps Controller IDs to their IP address. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 124 | // It's only used by handleControllerNodeIPsChanged |
| 125 | protected HashMap<String, String> controllerNodeIPsCache; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 126 | protected BlockingQueue<IUpdate> updates; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 128 | protected ConcurrentMap<OFType, ListenerDispatcher<OFType, IOFMessageListener>> messageListeners; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 129 | protected Set<IOFSwitchListener> switchListeners; |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 130 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 131 | // Module dependencies |
| 132 | protected IRestApiService restApi; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 133 | protected IThreadPoolService threadPool; |
Jonathan Hart | d10008d | 2013-02-23 17:04:08 -0800 | [diff] [blame] | 134 | protected IControllerRegistryService registryService; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 135 | protected IDebugCounterService debugCounters; |
| 136 | protected IDebugEventService debugEvents; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 137 | |
Jonathan Hart | 8a5d097 | 2013-12-04 10:02:44 -0800 | [diff] [blame] | 138 | protected ILinkDiscoveryService linkDiscovery; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 139 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 140 | // Configuration options |
| 141 | protected int openFlowPort = 6633; |
| 142 | protected int workerThreads = 0; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 143 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 144 | // The id for this controller node. Should be unique for each controller |
| 145 | // node in a controller cluster. |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 146 | private OnosInstanceId onosInstanceId = new OnosInstanceId("localhost"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 148 | // defined counters |
| 149 | private Counters counters; |
| 150 | // Event IDs for debug events |
| 151 | protected IEventUpdater<SwitchEvent> evSwitch; |
| 152 | |
| 153 | // Load monitor for overload protection |
| 154 | protected final boolean overload_drop = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 155 | Boolean.parseBoolean(System.getProperty("overload_drop", "false")); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 156 | protected final LoadMonitor loadmonitor = new LoadMonitor(log); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 157 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 158 | // Start time of the controller |
| 159 | protected long systemStartTime; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 160 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 161 | // Flag to always flush flow table on switch reconnect (HA or otherwise) |
| 162 | protected boolean alwaysClearFlowsOnSwAdd = false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 163 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 164 | // Perf. related configuration |
| 165 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
| 166 | protected static final int BATCH_MAX_SIZE = 100; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 167 | protected static final boolean ALWAYS_DECODE_ETH = true; |
| 168 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 169 | // ****************************** |
| 170 | // Switch Management and Updates |
| 171 | // ****************************** |
| 172 | |
| 173 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 174 | * Switch updates are sent to all IOFSwitchListeners. A switch that is |
| 175 | * connected to this controller instance, but not activated, is not |
| 176 | * available for updates. |
| 177 | * |
| 178 | * In ONOS, each controller instance can simultaneously serve in a MASTER |
| 179 | * role for some connected switches, and in a EQUAL role for other connected |
| 180 | * switches. The EQUAL role can be treated as a SLAVE role, by ensuring that |
| 181 | * the controller instance never sends packets or commands out to the |
| 182 | * switch. Activated switches, either with Controller Role MASTER or EQUAL |
| 183 | * are announced as updates. We also support announcements of controller |
| 184 | * role transitions from MASTER --> EQUAL, and EQUAL --> MASTER, for an |
| 185 | * individual switch. |
| 186 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 187 | * Disconnection of only activated switches are announced. Finally, changes |
| 188 | * to switch ports are announced with a portChangeType (see @IOFSwitch) |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 189 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 190 | * @author saurav |
| 191 | */ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 192 | public enum SwitchUpdateType { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 193 | /** switch activated with this controller's role as MASTER */ |
| 194 | ACTIVATED_MASTER, |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 195 | /** |
| 196 | * switch activated with this controller's role as EQUAL. listener can |
| 197 | * treat this controller's role as SLAVE by not sending packets or |
| 198 | * commands to the switch |
| 199 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 200 | ACTIVATED_EQUAL, |
| 201 | /** this controller's role for this switch changed from Master to Equal */ |
| 202 | MASTER_TO_EQUAL, |
| 203 | /** this controller's role for this switch changed form Equal to Master */ |
| 204 | EQUAL_TO_MASTER, |
| 205 | /** A previously activated switch disconnected */ |
| 206 | DISCONNECTED, |
| 207 | /** Port changed on a previously activated switch */ |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 208 | PORTCHANGED, |
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 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 212 | * Update message indicating a switch was added or removed ONOS: This |
| 213 | * 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 { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 216 | public long getSwId() { |
| 217 | return swId; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 218 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 219 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 220 | public SwitchUpdateType getSwitchUpdateType() { |
| 221 | return switchUpdateType; |
| 222 | } |
| 223 | |
| 224 | public PortChangeType getPortChangeType() { |
| 225 | return changeType; |
| 226 | } |
| 227 | |
| 228 | private final long swId; |
| 229 | private final SwitchUpdateType switchUpdateType; |
| 230 | private final OFPortDesc port; |
| 231 | private final PortChangeType changeType; |
| 232 | |
| 233 | public SwitchUpdate(long swId, SwitchUpdateType switchUpdateType) { |
| 234 | this(swId, switchUpdateType, null, null); |
| 235 | } |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 236 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 237 | public SwitchUpdate(long swId, |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 238 | SwitchUpdateType switchUpdateType, |
| 239 | OFPortDesc port, |
| 240 | PortChangeType changeType) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 241 | if (switchUpdateType == SwitchUpdateType.PORTCHANGED) { |
| 242 | if (port == null) { |
| 243 | throw new NullPointerException("Port must not be null " + |
| 244 | "for PORTCHANGED updates"); |
| 245 | } |
| 246 | if (changeType == null) { |
| 247 | throw new NullPointerException("ChangeType must not be " + |
| 248 | "null for PORTCHANGED updates"); |
| 249 | } |
| 250 | } else { |
| 251 | if (port != null || changeType != null) { |
| 252 | throw new IllegalArgumentException("port and changeType " + |
| 253 | "must be null for " + switchUpdateType + |
| 254 | " updates"); |
| 255 | } |
| 256 | } |
| 257 | this.swId = swId; |
| 258 | this.switchUpdateType = switchUpdateType; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 259 | this.port = port; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 260 | this.changeType = changeType; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 261 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 262 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 263 | @Override |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 264 | public void dispatch() { |
| 265 | if (log.isTraceEnabled()) { |
| 266 | log.trace("Dispatching switch update {} {}", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 267 | HexString.toHexString(swId), switchUpdateType); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 268 | } |
| 269 | if (switchListeners != null) { |
| 270 | for (IOFSwitchListener listener : switchListeners) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 271 | switch (switchUpdateType) { |
| 272 | case ACTIVATED_MASTER: |
| 273 | // don't count here. We have more specific |
| 274 | // counters before the update is created |
| 275 | listener.switchActivatedMaster(swId); |
| 276 | break; |
| 277 | case ACTIVATED_EQUAL: |
| 278 | // don't count here. We have more specific |
| 279 | // counters before the update is created |
| 280 | listener.switchActivatedEqual(swId); |
| 281 | break; |
| 282 | case MASTER_TO_EQUAL: |
| 283 | listener.switchMasterToEqual(swId); |
| 284 | break; |
| 285 | case EQUAL_TO_MASTER: |
| 286 | listener.switchEqualToMaster(swId); |
| 287 | break; |
| 288 | case DISCONNECTED: |
| 289 | // don't count here. We have more specific |
| 290 | // counters before the update is created |
| 291 | listener.switchDisconnected(swId); |
| 292 | break; |
| 293 | case PORTCHANGED: |
| 294 | counters.switchPortChanged.updateCounterWithFlush(); |
| 295 | listener.switchPortChanged(swId, port, changeType); |
| 296 | break; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 301 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | protected boolean addConnectedSwitch(long dpid, OFChannelHandler h) { |
| 305 | if (connectedSwitches.get(dpid) != null) { |
| 306 | log.error("Trying to add connectedSwitch but found a previous " |
| 307 | + "value for dpid: {}", dpid); |
| 308 | return false; |
| 309 | } else { |
| 310 | connectedSwitches.put(dpid, h); |
| 311 | return true; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 316 | * Switch Events |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 317 | */ |
| 318 | @Override |
| 319 | public void addSwitchEvent(long dpid, String reason, boolean flushNow) { |
| 320 | if (flushNow) |
| 321 | evSwitch.updateEventWithFlush(new SwitchEvent(dpid, reason)); |
| 322 | else |
| 323 | evSwitch.updateEventNoFlush(new SwitchEvent(dpid, reason)); |
| 324 | } |
| 325 | |
| 326 | private boolean validActivation(long dpid) { |
| 327 | if (connectedSwitches.get(dpid) == null) { |
| 328 | log.error("Trying to activate switch but is not in " |
| 329 | + "connected switches: dpid {}. Aborting ..", |
| 330 | HexString.toHexString(dpid)); |
| 331 | return false; |
| 332 | } |
| 333 | if (activeMasterSwitches.get(dpid) != null || |
| 334 | activeEqualSwitches.get(dpid) != null) { |
| 335 | log.error("Trying to activate switch but it is already " |
| 336 | + "activated: dpid {}. Found in activeMaster: {} " |
| 337 | + "Found in activeEqual: {}. Aborting ..", new Object[] { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 338 | HexString.toHexString(dpid), |
| 339 | (activeMasterSwitches.get(dpid) == null) ? 'Y' : 'N', |
| 340 | (activeEqualSwitches.get(dpid) == null) ? 'Y' : 'N'}); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 341 | counters.switchWithSameDpidActivated.updateCounterWithFlush(); |
| 342 | return false; |
| 343 | } |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Called when a switch is activated, with this controller's role as MASTER |
| 349 | */ |
| 350 | protected boolean addActivatedMasterSwitch(long dpid, IOFSwitch sw) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 351 | synchronized (multiCacheLock) { |
| 352 | if (!validActivation(dpid)) |
| 353 | return false; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 354 | activeMasterSwitches.put(dpid, sw); |
| 355 | } |
| 356 | // XXX Workaround to prevent race condition where a link is detected |
| 357 | // and attempted to be written to the database before the port is in |
| 358 | // the database. We now suppress link discovery on ports until we're |
| 359 | // sure they're in the database. |
| 360 | for (OFPortDesc port : sw.getPorts()) { |
| 361 | linkDiscovery.disableDiscoveryOnPort(sw.getId(), |
| 362 | port.getPortNo().getShortPortNumber()); |
| 363 | } |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 364 | // update counters and events |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 365 | counters.switchActivated.updateCounterWithFlush(); |
| 366 | evSwitch.updateEventWithFlush(new SwitchEvent(dpid, "activeMaster")); |
| 367 | addUpdateToQueue(new SwitchUpdate(dpid, |
| 368 | SwitchUpdateType.ACTIVATED_MASTER)); |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Called when a switch is activated, with this controller's role as EQUAL |
| 374 | */ |
| 375 | protected boolean addActivatedEqualSwitch(long dpid, IOFSwitch sw) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 376 | synchronized (multiCacheLock) { |
| 377 | if (!validActivation(dpid)) |
| 378 | return false; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 379 | activeEqualSwitches.put(dpid, sw); |
| 380 | } |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 381 | // update counters and events |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 382 | counters.switchActivated.updateCounterWithFlush(); |
| 383 | evSwitch.updateEventWithFlush(new SwitchEvent(dpid, "activeEqual")); |
| 384 | addUpdateToQueue(new SwitchUpdate(dpid, |
| 385 | SwitchUpdateType.ACTIVATED_EQUAL)); |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 390 | * Called when this controller's role for a switch transitions from equal to |
| 391 | * master. For 1.0 switches, we internally refer to the role 'slave' as |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 392 | * 'equal' - so this transition is equivalent to 'addActivatedMasterSwitch'. |
| 393 | */ |
| 394 | protected void transitionToMasterSwitch(long dpid) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 395 | synchronized (multiCacheLock) { |
| 396 | IOFSwitch sw = activeEqualSwitches.remove(dpid); |
| 397 | if (sw == null) { |
| 398 | log.error("Transition to master called on sw {}, but switch " |
| 399 | + "was not found in controller-cache", dpid); |
| 400 | return; |
| 401 | } |
| 402 | activeMasterSwitches.put(dpid, sw); |
| 403 | } |
| 404 | addUpdateToQueue(new SwitchUpdate(dpid, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 405 | SwitchUpdateType.EQUAL_TO_MASTER)); |
| 406 | } |
| 407 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 408 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 409 | * Called when this controller's role for a switch transitions to equal. For |
| 410 | * 1.0 switches, we internally refer to the role 'slave' as 'equal'. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 411 | */ |
| 412 | protected void transitionToEqualSwitch(long dpid) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 413 | synchronized (multiCacheLock) { |
| 414 | IOFSwitch sw = activeMasterSwitches.remove(dpid); |
| 415 | if (sw == null) { |
| 416 | log.error("Transition to equal called on sw {}, but switch " |
| 417 | + "was not found in controller-cache", dpid); |
| 418 | return; |
| 419 | } |
| 420 | activeEqualSwitches.put(dpid, sw); |
| 421 | } |
| 422 | addUpdateToQueue(new SwitchUpdate(dpid, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 423 | SwitchUpdateType.MASTER_TO_EQUAL)); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Clear all state in controller switch maps for a switch that has |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 428 | * disconnected from the local controller. Also release control for that |
| 429 | * switch from the global repository. Notify switch listeners. |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 430 | */ |
| 431 | protected void removeConnectedSwitch(long dpid) { |
| 432 | releaseRegistryControl(dpid); |
| 433 | connectedSwitches.remove(dpid); |
| 434 | IOFSwitch sw = activeMasterSwitches.remove(dpid); |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 435 | if (sw == null) { |
| 436 | sw = activeEqualSwitches.remove(dpid); |
| 437 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 438 | if (sw != null) { |
| 439 | sw.cancelAllStatisticsReplies(); |
| 440 | sw.setConnected(false); // do we need this? |
| 441 | } |
| 442 | evSwitch.updateEventWithFlush(new SwitchEvent(dpid, "disconnected")); |
| 443 | counters.switchDisconnected.updateCounterWithFlush(); |
| 444 | addUpdateToQueue(new SwitchUpdate(dpid, SwitchUpdateType.DISCONNECTED)); |
| 445 | } |
| 446 | |
| 447 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 448 | * Indicates that ports on the given switch have changed. Enqueue a switch |
| 449 | * update. |
| 450 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 451 | * @param sw |
| 452 | */ |
| 453 | protected void notifyPortChanged(long dpid, OFPortDesc port, |
| 454 | PortChangeType changeType) { |
| 455 | if (port == null || changeType == null) { |
| 456 | String msg = String.format("Switch port or changetType must not " |
| 457 | + "be null in port change notification"); |
| 458 | throw new NullPointerException(msg); |
| 459 | } |
| 460 | if (connectedSwitches.get(dpid) == null || getSwitch(dpid) == null) { |
| 461 | log.warn("Port change update on switch {} not connected or activated " |
| 462 | + "... Aborting.", HexString.toHexString(dpid)); |
| 463 | return; |
| 464 | } |
| 465 | |
| 466 | if (changeType == PortChangeType.ADD) { |
| 467 | // XXX Workaround to prevent race condition where a link is detected |
| 468 | // and attempted to be written to the database before the port is in |
| 469 | // the database. We now suppress link discovery on ports until we're |
| 470 | // sure they're in the database. |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 471 | linkDiscovery.disableDiscoveryOnPort(dpid, port.getPortNo() |
| 472 | .getShortPortNumber()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | SwitchUpdate update = new SwitchUpdate(dpid, SwitchUpdateType.PORTCHANGED, |
| 476 | port, changeType); |
| 477 | addUpdateToQueue(update); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 478 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 479 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 480 | // *************** |
| 481 | // Getters/Setters |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 482 | // *************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 483 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 484 | public void setRestApiService(IRestApiService restApi) { |
| 485 | this.restApi = restApi; |
| 486 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 487 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 488 | public void setThreadPoolService(IThreadPoolService tp) { |
| 489 | this.threadPool = tp; |
| 490 | } |
| 491 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 492 | public void setMastershipService(IControllerRegistryService serviceImpl) { |
| 493 | this.registryService = serviceImpl; |
Pankaj Berde | dc73bb1 | 2013-08-14 13:46:38 -0700 | [diff] [blame] | 494 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 495 | |
| 496 | public void setLinkDiscoveryService(ILinkDiscoveryService linkDiscovery) { |
| 497 | this.linkDiscovery = linkDiscovery; |
| 498 | } |
| 499 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 500 | public void setDebugCounter(IDebugCounterService debugCounters) { |
| 501 | this.debugCounters = debugCounters; |
| 502 | } |
| 503 | |
| 504 | public void setDebugEvent(IDebugEventService debugEvents) { |
| 505 | this.debugEvents = debugEvents; |
| 506 | } |
| 507 | |
| 508 | IDebugCounterService getDebugCounter() { |
| 509 | return this.debugCounters; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 512 | // ********************** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 513 | // Role Handling |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 514 | // ********************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 515 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 516 | /** |
| 517 | * created by ONOS - works with registry service |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 518 | */ |
Jonathan Hart | cc957a0 | 2013-02-26 10:39:04 -0800 | [diff] [blame] | 519 | protected class RoleChangeCallback implements ControlChangeCallback { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 520 | @Override |
Pavlin Radoslavov | 695f895 | 2014-07-23 16:57:01 -0700 | [diff] [blame] | 521 | public void controlChanged(long dpidLong, boolean hasControl) { |
| 522 | Dpid dpid = new Dpid(dpidLong); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 523 | log.info("Role change callback for switch {}, hasControl {}", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 524 | dpid, hasControl); |
Pankaj Berde | 01939e9 | 2013-03-08 14:38:27 -0800 | [diff] [blame] | 525 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 526 | Role role = null; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 527 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 528 | /* |
| 529 | * issue #229 |
| 530 | * Cannot rely on sw.getRole() as it can be behind due to pending |
| 531 | * role changes in the queue. Just submit it and late the |
| 532 | * RoleChanger handle duplicates. |
| 533 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 534 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 535 | if (hasControl) { |
| 536 | role = Role.MASTER; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 537 | } else { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 538 | role = Role.EQUAL; // treat the same as Role.SLAVE |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 539 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 540 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 541 | OFChannelHandler swCh = connectedSwitches.get(dpid.value()); |
| 542 | if (swCh == null) { |
| 543 | log.warn("Switch {} not found in connected switches", dpid); |
| 544 | return; |
Pankaj Berde | 465ac7c | 2013-05-23 13:47:49 -0700 | [diff] [blame] | 545 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 546 | |
| 547 | log.debug("Sending role request {} msg to {}", role, dpid); |
| 548 | swCh.sendRoleRequest(role, RoleRecvStatus.MATCHED_SET_ROLE); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 549 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | public synchronized void submitRegistryRequest(long dpid) { |
| 553 | OFChannelHandler h = connectedSwitches.get(dpid); |
| 554 | if (h == null) { |
| 555 | log.error("Trying to request registry control for switch {} " |
| 556 | + "not in connected switches. Aborting.. ", |
| 557 | HexString.toHexString(dpid)); |
| 558 | // FIXME shouldn't we immediately return here? |
| 559 | } |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 560 | // Request control of the switch from the global registry |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 561 | try { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 562 | h.controlRequested = Boolean.TRUE; |
| 563 | registryService.requestControl(dpid, new RoleChangeCallback()); |
| 564 | } catch (RegistryException e) { |
| 565 | log.debug("Registry error: {}", e.getMessage()); |
| 566 | h.controlRequested = Boolean.FALSE; |
| 567 | } |
| 568 | if (!h.controlRequested) { // XXX what is being attempted here? |
| 569 | // yield to allow other thread(s) to release control |
| 570 | try { |
| 571 | Thread.sleep(10); |
| 572 | } catch (InterruptedException e) { |
| 573 | // Ignore interruptions |
| 574 | } |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 575 | // safer to bounce the switch to reconnect here than proceeding |
| 576 | // further |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 577 | // XXX S why? can't we just try again a little later? |
| 578 | log.debug("Closing sw:{} because we weren't able to request control " + |
| 579 | "successfully" + dpid); |
| 580 | connectedSwitches.get(dpid).disconnectSwitch(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 581 | } |
| 582 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 583 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 584 | public synchronized void releaseRegistryControl(long dpidLong) { |
| 585 | OFChannelHandler h = connectedSwitches.get(dpidLong); |
| 586 | if (h == null) { |
| 587 | log.error("Trying to release registry control for switch {} " |
| 588 | + "not in connected switches. Aborting.. ", |
| 589 | HexString.toHexString(dpidLong)); |
| 590 | return; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 591 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 592 | if (h.controlRequested) { |
| 593 | registryService.releaseControl(dpidLong); |
| 594 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 597 | // ******************* |
| 598 | // OF Message Handling |
| 599 | // ******************* |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 600 | |
| 601 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 602 | * Handle and dispatch a message to IOFMessageListeners. |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 603 | * |
| 604 | * We only dispatch messages to listeners if the controller's role is |
| 605 | * MASTER. |
| 606 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 607 | * @param sw The switch sending the message |
| 608 | * @param m The message the switch sent |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 609 | * @param flContext The floodlight context to use for this message. If null, |
| 610 | * a new context will be allocated. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 611 | * @throws IOException |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 612 | * |
| 613 | * FIXME: this method and the ChannelHandler disagree on which |
| 614 | * messages should be dispatched and which shouldn't |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 615 | */ |
| 616 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 617 | @LogMessageDoc(level = "ERROR", |
| 618 | message = "Ignoring PacketIn (Xid = {xid}) because the data" + |
| 619 | " field is empty.", |
| 620 | explanation = "The switch sent an improperly-formatted PacketIn" + |
| 621 | " message", |
| 622 | recommendation = LogMessageDoc.CHECK_SWITCH), |
| 623 | @LogMessageDoc(level = "WARN", |
| 624 | message = "Unhandled OF Message: {} from {}", |
| 625 | explanation = "The switch sent a message not handled by " + |
| 626 | "the controller") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 627 | }) |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 628 | @SuppressWarnings({"fallthrough", "unchecked"}) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 629 | protected void handleMessage(IOFSwitch sw, OFMessage m, |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 630 | FloodlightContext bContext) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 631 | throws IOException { |
| 632 | Ethernet eth = null; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 633 | short inport = -1; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 634 | |
| 635 | switch (m.getType()) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 636 | case PACKET_IN: |
| 637 | OFPacketIn pi = (OFPacketIn) m; |
| 638 | // log.info("saw packet in from sw {}", sw.getStringId()); |
| 639 | if (pi.getData().length <= 0) { |
| 640 | log.error("Ignoring PacketIn (Xid = " + pi.getXid() + |
| 641 | ") because/* the data field is empty."); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | // get incoming port to store in floodlight context |
| 646 | if (sw.getOFVersion() == OFVersion.OF_10) { |
| 647 | inport = pi.getInPort().getShortPortNumber(); |
| 648 | } else if (sw.getOFVersion() == OFVersion.OF_13) { |
| 649 | for (MatchField<?> mf : pi.getMatch().getMatchFields()) { |
| 650 | if (mf.id == MatchFields.IN_PORT) { |
| 651 | inport = pi.getMatch().get((MatchField<OFPort>) mf) |
| 652 | .getShortPortNumber(); |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | if (inport == -1) { |
| 657 | log.error("Match field for incoming port missing in " |
| 658 | + "packet-in from sw {}. Ignoring msg", |
| 659 | sw.getStringId()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 660 | return; |
| 661 | } |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 662 | } else { |
| 663 | // should have been taken care of earlier in handshake |
| 664 | log.error("OFVersion {} not supported for " |
| 665 | + "packet-in from sw {}. Ignoring msg", |
| 666 | sw.getOFVersion(), sw.getStringId()); |
| 667 | return; |
| 668 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 669 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 670 | // decode enclosed ethernet packet to store in floodlight context |
| 671 | if (Controller.ALWAYS_DECODE_ETH) { |
| 672 | eth = new Ethernet(); |
| 673 | eth.deserialize(pi.getData(), 0, |
| 674 | pi.getData().length); |
| 675 | } |
| 676 | // fall through to default case... |
| 677 | |
| 678 | /*log.debug("Sw:{} packet-in: {}", sw.getStringId(), |
| 679 | String.format("0x%x", eth.getEtherType()));*/ |
| 680 | if (eth.getEtherType() != (short) EthType.LLDP.getValue()) |
| 681 | log.trace("Sw:{} packet-in: {}", sw.getStringId(), pi); |
| 682 | |
| 683 | default: |
| 684 | |
| 685 | List<IOFMessageListener> listeners = null; |
| 686 | |
| 687 | if (messageListeners.containsKey(m.getType())) { |
| 688 | listeners = messageListeners.get(m.getType()). |
| 689 | getOrderedListeners(); |
| 690 | } |
| 691 | FloodlightContext bc = null; |
| 692 | if (listeners != null) { |
| 693 | // Check if floodlight context is passed from the calling |
| 694 | // function, if so use that floodlight context, otherwise |
| 695 | // allocate one |
| 696 | if (bContext == null) { |
| 697 | bc = flcontext_alloc(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 698 | } else { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 699 | bc = bContext; |
| 700 | } |
| 701 | if (eth != null) { |
| 702 | IFloodlightProviderService.bcStore.put(bc, |
| 703 | IFloodlightProviderService.CONTEXT_PI_PAYLOAD, |
| 704 | eth); |
| 705 | } |
| 706 | if (inport != -1) { |
| 707 | bc.getStorage().put( |
| 708 | IFloodlightProviderService.CONTEXT_PI_INPORT, |
| 709 | inport); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 712 | // Get the starting time (overall and per-component) of |
| 713 | // the processing chain for this packet if performance |
| 714 | // monitoring is turned on |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 715 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 716 | Command cmd = null; |
| 717 | for (IOFMessageListener listener : listeners) { |
| 718 | if (listener instanceof IOFSwitchFilter) { |
| 719 | if (!((IOFSwitchFilter) listener).isInterested(sw)) { |
| 720 | continue; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
mininet | 73e7fb7 | 2013-12-03 14:25:53 -0800 | [diff] [blame] | 723 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 724 | cmd = listener.receive(sw, m, bc); |
| 725 | |
| 726 | if (Command.STOP.equals(cmd)) { |
| 727 | break; |
| 728 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 729 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 730 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 731 | } else { |
| 732 | log.warn("Unhandled OF Message: {} from {}", m, sw); |
| 733 | } |
| 734 | |
| 735 | if ((bContext == null) && (bc != null)) |
| 736 | flcontext_free(bc); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 737 | } |
| 738 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 739 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 740 | // *************** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 741 | // IFloodlightProviderService |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 742 | // *************** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 743 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 744 | // FIXME: remove this method |
| 745 | @Override |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 746 | public Map<Long, IOFSwitch> getSwitches() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 747 | return getMasterSwitches(); |
| 748 | } |
| 749 | |
| 750 | // FIXME: remove this method |
| 751 | public Map<Long, IOFSwitch> getMasterSwitches() { |
| 752 | return Collections.unmodifiableMap(activeMasterSwitches); |
| 753 | } |
| 754 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 755 | @Override |
| 756 | public Set<Long> getAllSwitchDpids() { |
| 757 | Set<Long> dpids = new HashSet<Long>(); |
| 758 | dpids.addAll(activeMasterSwitches.keySet()); |
| 759 | dpids.addAll(activeEqualSwitches.keySet()); |
| 760 | return dpids; |
| 761 | } |
| 762 | |
| 763 | @Override |
| 764 | public Set<Long> getAllMasterSwitchDpids() { |
| 765 | Set<Long> dpids = new HashSet<Long>(); |
| 766 | dpids.addAll(activeMasterSwitches.keySet()); |
| 767 | return dpids; |
| 768 | } |
| 769 | |
| 770 | @Override |
| 771 | public Set<Long> getAllEqualSwitchDpids() { |
| 772 | Set<Long> dpids = new HashSet<Long>(); |
| 773 | dpids.addAll(activeEqualSwitches.keySet()); |
| 774 | return dpids; |
| 775 | } |
| 776 | |
| 777 | @Override |
| 778 | public IOFSwitch getSwitch(long dpid) { |
| 779 | IOFSwitch sw = null; |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 780 | if ((sw = activeMasterSwitches.get(dpid)) != null) |
| 781 | return sw; |
| 782 | if ((sw = activeEqualSwitches.get(dpid)) != null) |
| 783 | return sw; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 784 | return sw; |
| 785 | } |
| 786 | |
| 787 | @Override |
| 788 | public IOFSwitch getMasterSwitch(long dpid) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 789 | return activeMasterSwitches.get(dpid); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | @Override |
| 793 | public IOFSwitch getEqualSwitch(long dpid) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 794 | return activeEqualSwitches.get(dpid); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 797 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 798 | public synchronized void addOFMessageListener(OFType type, |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 799 | IOFMessageListener listener) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 800 | ListenerDispatcher<OFType, IOFMessageListener> ldd = |
| 801 | messageListeners.get(type); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 802 | if (ldd == null) { |
| 803 | ldd = new ListenerDispatcher<OFType, IOFMessageListener>(); |
| 804 | messageListeners.put(type, ldd); |
| 805 | } |
| 806 | ldd.addListener(type, listener); |
| 807 | } |
| 808 | |
| 809 | @Override |
| 810 | public synchronized void removeOFMessageListener(OFType type, |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 811 | IOFMessageListener listener) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 812 | ListenerDispatcher<OFType, IOFMessageListener> ldd = |
| 813 | messageListeners.get(type); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 814 | if (ldd != null) { |
| 815 | ldd.removeListener(listener); |
| 816 | } |
| 817 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 818 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 819 | public void removeOFMessageListeners(OFType type) { |
| 820 | messageListeners.remove(type); |
| 821 | } |
| 822 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 823 | private void logListeners() { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 824 | for (Map.Entry<OFType, ListenerDispatcher<OFType, IOFMessageListener>> entry : messageListeners |
| 825 | .entrySet()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 826 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 827 | OFType type = entry.getKey(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 828 | ListenerDispatcher<OFType, IOFMessageListener> ldd = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 829 | entry.getValue(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 830 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 831 | StringBuffer sb = new StringBuffer(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 832 | sb.append("OFMessageListeners for "); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 833 | sb.append(type); |
| 834 | sb.append(": "); |
| 835 | for (IOFMessageListener l : ldd.getOrderedListeners()) { |
| 836 | sb.append(l.getName()); |
| 837 | sb.append(","); |
| 838 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 839 | log.debug(sb.toString()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 840 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 841 | StringBuffer sl = new StringBuffer(); |
| 842 | sl.append("SwitchUpdate Listeners: "); |
| 843 | for (IOFSwitchListener swlistener : switchListeners) { |
| 844 | sl.append(swlistener.getName()); |
| 845 | sl.append(","); |
| 846 | } |
| 847 | log.debug(sl.toString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 848 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | @Override |
| 852 | public void addOFSwitchListener(IOFSwitchListener listener) { |
| 853 | this.switchListeners.add(listener); |
| 854 | } |
| 855 | |
| 856 | @Override |
| 857 | public void removeOFSwitchListener(IOFSwitchListener listener) { |
| 858 | this.switchListeners.remove(listener); |
| 859 | } |
| 860 | |
| 861 | @Override |
| 862 | public Map<OFType, List<IOFMessageListener>> getListeners() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 863 | Map<OFType, List<IOFMessageListener>> lers = |
| 864 | new HashMap<OFType, List<IOFMessageListener>>(); |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 865 | for (Entry<OFType, ListenerDispatcher<OFType, IOFMessageListener>> e : messageListeners |
| 866 | .entrySet()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 867 | lers.put(e.getKey(), e.getValue().getOrderedListeners()); |
| 868 | } |
| 869 | return Collections.unmodifiableMap(lers); |
| 870 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 871 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 872 | /*@Override |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 873 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 874 | @LogMessageDoc(message = "Failed to inject OFMessage {message} onto " + |
| 875 | "a null switch", |
| 876 | explanation = "Failed to process a message because the switch " + |
| 877 | " is no longer connected."), |
| 878 | @LogMessageDoc(level = "ERROR", |
| 879 | message = "Error reinjecting OFMessage on switch {switch}", |
| 880 | explanation = "An I/O error occured while attempting to " + |
| 881 | "process an OpenFlow message", |
| 882 | recommendation = LogMessageDoc.CHECK_SWITCH) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 883 | }) |
| 884 | public boolean injectOfMessage(IOFSwitch sw, OFMessage msg, |
| 885 | FloodlightContext bc) { |
| 886 | if (sw == null) { |
| 887 | log.info("Failed to inject OFMessage {} onto a null switch", msg); |
| 888 | return false; |
| 889 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 890 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 891 | // FIXME: Do we need to be able to inject messages to switches |
| 892 | // where we're the slave controller (i.e. they're connected but |
| 893 | // not active)? |
| 894 | // FIXME: Don't we need synchronization logic here so we're holding |
| 895 | // the listener read lock when we call handleMessage? After some |
| 896 | // discussions it sounds like the right thing to do here would be to |
| 897 | // inject the message as a netty upstream channel event so it goes |
| 898 | // through the normal netty event processing, including being |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 899 | // handled |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 900 | if (!activeSwitches.containsKey(sw.getId())) return false; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 901 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 902 | try { |
| 903 | // Pass Floodlight context to the handleMessages() |
| 904 | handleMessage(sw, msg, bc); |
| 905 | } catch (IOException e) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 906 | log.error("Error reinjecting OFMessage on switch {}", |
| 907 | HexString.toHexString(sw.getId())); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 908 | return false; |
| 909 | } |
| 910 | return true; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 911 | }*/ |
| 912 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 913 | // @Override |
| 914 | // public boolean injectOfMessage(IOFSwitch sw, OFMessage msg) { |
| 915 | // // call the overloaded version with floodlight context set to null |
| 916 | // return injectOfMessage(sw, msg, null); |
| 917 | // } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 918 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 919 | // @Override |
| 920 | // public void handleOutgoingMessage(IOFSwitch sw, OFMessage m, |
| 921 | // FloodlightContext bc) { |
| 922 | // |
| 923 | // List<IOFMessageListener> listeners = null; |
| 924 | // if (messageListeners.containsKey(m.getType())) { |
| 925 | // listeners = |
| 926 | // messageListeners.get(m.getType()).getOrderedListeners(); |
| 927 | // } |
| 928 | // |
| 929 | // if (listeners != null) { |
| 930 | // for (IOFMessageListener listener : listeners) { |
| 931 | // if (listener instanceof IOFSwitchFilter) { |
| 932 | // if (!((IOFSwitchFilter) listener).isInterested(sw)) { |
| 933 | // continue; |
| 934 | // } |
| 935 | // } |
| 936 | // if (Command.STOP.equals(listener.receive(sw, m, bc))) { |
| 937 | // break; |
| 938 | // } |
| 939 | // } |
| 940 | // } |
| 941 | // } |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 942 | |
| 943 | /** |
| 944 | * Gets an OpenFlow message factory for version 1.0. |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 945 | * |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 946 | * @return an OpenFlow 1.0 message factory |
| 947 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 948 | public OFFactory getOFMessageFactory_10() { |
| 949 | return factory10; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 950 | } |
| 951 | |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 952 | /** |
| 953 | * Gets an OpenFlow message factory for version 1.3. |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 954 | * |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 955 | * @return an OpenFlow 1.3 message factory |
| 956 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 957 | public OFFactory getOFMessageFactory_13() { |
| 958 | return factory13; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 959 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 960 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 961 | @Override |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 962 | public void publishUpdate(IUpdate update) { |
| 963 | try { |
| 964 | this.updates.put(update); |
| 965 | } catch (InterruptedException e) { |
| 966 | log.error("Failure adding update to queue", e); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | |
| 970 | @Override |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 971 | public Map<String, String> getControllerNodeIPs() { |
| 972 | // We return a copy of the mapping so we can guarantee that |
| 973 | // the mapping return is the same as one that will be (or was) |
| 974 | // dispatched to IHAListeners |
| 975 | HashMap<String, String> retval = new HashMap<String, String>(); |
| 976 | synchronized (controllerNodeIPsCache) { |
| 977 | retval.putAll(controllerNodeIPsCache); |
| 978 | } |
| 979 | return retval; |
| 980 | } |
| 981 | |
| 982 | @Override |
| 983 | public long getSystemStartTime() { |
| 984 | return (this.systemStartTime); |
| 985 | } |
| 986 | |
| 987 | @Override |
| 988 | public void setAlwaysClearFlowsOnSwAdd(boolean value) { |
| 989 | this.alwaysClearFlowsOnSwAdd = value; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 990 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 991 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 992 | @Override |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 993 | public OnosInstanceId getOnosInstanceId() { |
| 994 | return onosInstanceId; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 995 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 996 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 997 | /** |
| 998 | * FOR TESTING ONLY. Dispatch all updates in the update queue until queue is |
| 999 | * empty |
| 1000 | */ |
| 1001 | void processUpdateQueueForTesting() { |
| 1002 | while (!updates.isEmpty()) { |
| 1003 | IUpdate update = updates.poll(); |
| 1004 | if (update != null) |
| 1005 | update.dispatch(); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1009 | // ************** |
| 1010 | // Initialization |
| 1011 | // ************** |
| 1012 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1013 | // XXX S This should probably go away OR it should be edited to handle |
| 1014 | // controller roles per switch! Then it could be a way to |
| 1015 | // deterministically configure a switch to a MASTER controller instance |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1016 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1017 | * Sets the initial role based on properties in the config params. It looks |
| 1018 | * for two different properties. If the "role" property is specified then |
| 1019 | * the value should be either "EQUAL", "MASTER", or "SLAVE" and the role of |
| 1020 | * the controller is set to the specified value. If the "role" property is |
| 1021 | * not specified then it looks next for the "role.path" property. In this |
| 1022 | * case the value should be the path to a property file in the file system |
| 1023 | * that contains a property called "floodlight.role" which can be one of the |
| 1024 | * values listed above for the "role" property. The idea behind the |
| 1025 | * "role.path" mechanism is that you have some separate heartbeat and master |
| 1026 | * controller election algorithm that determines the role of the controller. |
| 1027 | * When a role transition happens, it updates the current role in the file |
| 1028 | * specified by the "role.path" file. Then if floodlight restarts for some |
| 1029 | * reason it can get the correct current role of the controller from the |
| 1030 | * file. |
| 1031 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1032 | * @param configParams The config params for the FloodlightProvider service |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1033 | * @return A valid role if role information is specified in the config |
| 1034 | * params, otherwise null |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1035 | */ |
| 1036 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1037 | @LogMessageDoc(message = "Controller role set to {role}", |
| 1038 | explanation = "Setting the initial HA role to "), |
| 1039 | @LogMessageDoc(level = "ERROR", |
| 1040 | message = "Invalid current role value: {role}", |
| 1041 | explanation = "An invalid HA role value was read from the " + |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1042 | "properties file", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1043 | recommendation = LogMessageDoc.CHECK_CONTROLLER) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1044 | }) |
| 1045 | protected Role getInitialRole(Map<String, String> configParams) { |
| 1046 | Role role = null; |
| 1047 | String roleString = configParams.get("role"); |
Praseed Balakrishnan | 59469e9 | 2014-06-20 11:55:13 -0700 | [diff] [blame] | 1048 | FileInputStream fs = null; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1049 | if (roleString == null) { |
| 1050 | String rolePath = configParams.get("rolepath"); |
| 1051 | if (rolePath != null) { |
| 1052 | Properties properties = new Properties(); |
| 1053 | try { |
Praseed Balakrishnan | 59469e9 | 2014-06-20 11:55:13 -0700 | [diff] [blame] | 1054 | fs = new FileInputStream(rolePath); |
| 1055 | properties.load(fs); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1056 | roleString = properties.getProperty("floodlight.role"); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1057 | } catch (IOException exc) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1058 | // Don't treat it as an error if the file specified by the |
| 1059 | // rolepath property doesn't exist. This lets us enable the |
| 1060 | // HA mechanism by just creating/setting the floodlight.role |
| 1061 | // property in that file without having to modify the |
| 1062 | // floodlight properties. |
Praseed Balakrishnan | 59469e9 | 2014-06-20 11:55:13 -0700 | [diff] [blame] | 1063 | } finally { |
| 1064 | if (fs != null) { |
| 1065 | try { |
| 1066 | fs.close(); |
| 1067 | } catch (IOException e) { |
| 1068 | log.error("Exception while closing resource ", e); |
| 1069 | } |
| 1070 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1074 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1075 | if (roleString != null) { |
| 1076 | // Canonicalize the string to the form used for the enum constants |
| 1077 | roleString = roleString.trim().toUpperCase(); |
| 1078 | try { |
| 1079 | role = Role.valueOf(roleString); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1080 | } catch (IllegalArgumentException exc) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1081 | log.error("Invalid current role value: {}", roleString); |
| 1082 | } |
| 1083 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1084 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1085 | log.info("Controller role set to {}", role); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1086 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1087 | return role; |
| 1088 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1089 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1090 | /** |
| 1091 | * Tell controller that we're ready to accept switches loop |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1092 | * |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1093 | * @throws IOException |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1094 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1095 | @Override |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1096 | @LogMessageDocs({ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1097 | @LogMessageDoc(message = "Listening for switch connections on {address}", |
| 1098 | explanation = "The controller is ready and listening for new" + |
| 1099 | " switch connections"), |
| 1100 | @LogMessageDoc(message = "Storage exception in controller " + |
| 1101 | "updates loop; terminating process", |
| 1102 | explanation = ERROR_DATABASE, |
| 1103 | recommendation = LogMessageDoc.CHECK_CONTROLLER), |
| 1104 | @LogMessageDoc(level = "ERROR", |
| 1105 | message = "Exception in controller updates loop", |
| 1106 | explanation = "Failed to dispatch controller event", |
| 1107 | recommendation = LogMessageDoc.GENERIC_ACTION) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1108 | }) |
| 1109 | public void run() { |
| 1110 | if (log.isDebugEnabled()) { |
| 1111 | logListeners(); |
| 1112 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1113 | |
| 1114 | try { |
| 1115 | final ServerBootstrap bootstrap = createServerBootStrap(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1116 | |
| 1117 | bootstrap.setOption("reuseAddr", true); |
| 1118 | bootstrap.setOption("child.keepAlive", true); |
| 1119 | bootstrap.setOption("child.tcpNoDelay", true); |
| 1120 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 1121 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1122 | ChannelPipelineFactory pfact = |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1123 | new OpenflowPipelineFactory(this, null); |
| 1124 | bootstrap.setPipelineFactory(pfact); |
| 1125 | InetSocketAddress sa = new InetSocketAddress(openFlowPort); |
| 1126 | final ChannelGroup cg = new DefaultChannelGroup(); |
| 1127 | cg.add(bootstrap.bind(sa)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1128 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1129 | log.info("Listening for switch connections on {}", sa); |
| 1130 | } catch (Exception e) { |
| 1131 | throw new RuntimeException(e); |
| 1132 | } |
| 1133 | |
| 1134 | // main loop |
| 1135 | while (true) { |
| 1136 | try { |
| 1137 | IUpdate update = updates.take(); |
| 1138 | update.dispatch(); |
| 1139 | } catch (InterruptedException e) { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1140 | log.error("Received interrupted exception in updates loop;" + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1141 | "terminating process"); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1142 | terminate(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1143 | } catch (Exception e) { |
| 1144 | log.error("Exception in controller updates loop", e); |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | private ServerBootstrap createServerBootStrap() { |
| 1150 | if (workerThreads == 0) { |
| 1151 | return new ServerBootstrap( |
| 1152 | new NioServerSocketChannelFactory( |
| 1153 | Executors.newCachedThreadPool(), |
| 1154 | Executors.newCachedThreadPool())); |
| 1155 | } else { |
| 1156 | return new ServerBootstrap( |
| 1157 | new NioServerSocketChannelFactory( |
| 1158 | Executors.newCachedThreadPool(), |
| 1159 | Executors.newCachedThreadPool(), workerThreads)); |
| 1160 | } |
| 1161 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1162 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1163 | public void setConfigParams(Map<String, String> configParams) { |
| 1164 | String ofPort = configParams.get("openflowport"); |
| 1165 | if (ofPort != null) { |
| 1166 | this.openFlowPort = Integer.parseInt(ofPort); |
| 1167 | } |
| 1168 | log.debug("OpenFlow port set to {}", this.openFlowPort); |
| 1169 | String threads = configParams.get("workerthreads"); |
| 1170 | if (threads != null) { |
| 1171 | this.workerThreads = Integer.parseInt(threads); |
| 1172 | } |
| 1173 | log.debug("Number of worker threads set to {}", this.workerThreads); |
| 1174 | String controllerId = configParams.get("controllerid"); |
| 1175 | if (controllerId != null) { |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 1176 | this.onosInstanceId = new OnosInstanceId(controllerId); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1177 | } else { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1178 | // Try to get the hostname of the machine and use that for |
| 1179 | // controller ID |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1180 | try { |
| 1181 | String hostname = java.net.InetAddress.getLocalHost().getHostName(); |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 1182 | this.onosInstanceId = new OnosInstanceId(hostname); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1183 | } catch (UnknownHostException e) { |
| 1184 | // Can't get hostname, we'll just use the default |
| 1185 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1186 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1187 | |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 1188 | log.debug("ControllerId set to {}", this.onosInstanceId); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1191 | /** |
| 1192 | * Initialize internal data structures |
| 1193 | */ |
| 1194 | public void init(Map<String, String> configParams) { |
| 1195 | // These data structures are initialized here because other |
| 1196 | // module's startUp() might be called before ours |
| 1197 | this.messageListeners = |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1198 | new ConcurrentHashMap<OFType, ListenerDispatcher<OFType, |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1199 | IOFMessageListener>>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1200 | this.switchListeners = new CopyOnWriteArraySet<IOFSwitchListener>(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1201 | this.activeMasterSwitches = new ConcurrentHashMap<Long, IOFSwitch>(); |
| 1202 | this.activeEqualSwitches = new ConcurrentHashMap<Long, IOFSwitch>(); |
| 1203 | this.connectedSwitches = new ConcurrentHashMap<Long, OFChannelHandler>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1204 | this.controllerNodeIPsCache = new HashMap<String, String>(); |
| 1205 | this.updates = new LinkedBlockingQueue<IUpdate>(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1206 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1207 | setConfigParams(configParams); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1208 | this.systemStartTime = System.currentTimeMillis(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1209 | this.counters = new Counters(); |
| 1210 | this.multiCacheLock = new Object(); |
| 1211 | |
| 1212 | String option = configParams.get("flushSwitchesOnReconnect"); |
| 1213 | if (option != null && option.equalsIgnoreCase("true")) { |
| 1214 | this.setAlwaysClearFlowsOnSwActivate(true); |
| 1215 | log.info("Flush switches on reconnect -- Enabled."); |
| 1216 | } else { |
| 1217 | this.setAlwaysClearFlowsOnSwActivate(false); |
| 1218 | log.info("Flush switches on reconnect -- Disabled"); |
| 1219 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1220 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1221 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1222 | /** |
| 1223 | * Startup all of the controller's components |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1224 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1225 | * @throws FloodlightModuleException |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1226 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1227 | @LogMessageDoc(message = "Waiting for storage source", |
| 1228 | explanation = "The system database is not yet ready", |
| 1229 | recommendation = "If this message persists, this indicates " + |
| 1230 | "that the system database has failed to start. " + |
| 1231 | LogMessageDoc.CHECK_CONTROLLER) |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1232 | public void startupComponents() throws FloodlightModuleException { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1233 | try { |
Pavlin Radoslavov | 53b208a | 2014-07-28 13:16:11 -0700 | [diff] [blame] | 1234 | registryService.registerController(onosInstanceId.toString()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1235 | } catch (RegistryException e) { |
| 1236 | log.warn("Registry service error: {}", e.getMessage()); |
| 1237 | } |
| 1238 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1239 | // Add our REST API |
| 1240 | restApi.addRestletRoutable(new CoreWebRoutable()); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1241 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1242 | // Startup load monitoring |
| 1243 | if (overload_drop) { |
| 1244 | this.loadmonitor.startMonitoring( |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1245 | this.threadPool.getScheduledExecutor()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1246 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1247 | |
| 1248 | // register counters and events |
| 1249 | try { |
| 1250 | this.counters.createCounters(debugCounters); |
| 1251 | } catch (CounterException e) { |
| 1252 | throw new FloodlightModuleException(e.getMessage()); |
| 1253 | } |
| 1254 | registerControllerDebugEvents(); |
| 1255 | } |
| 1256 | |
| 1257 | // ************** |
| 1258 | // debugCounter registrations |
| 1259 | // ************** |
| 1260 | |
| 1261 | public static class Counters { |
| 1262 | public static final String prefix = "controller"; |
| 1263 | public IDebugCounter setRoleEqual; |
| 1264 | public IDebugCounter setSameRole; |
| 1265 | public IDebugCounter setRoleMaster; |
| 1266 | public IDebugCounter remoteStoreNotification; |
| 1267 | public IDebugCounter invalidPortsChanged; |
| 1268 | public IDebugCounter invalidSwitchActivatedWhileSlave; |
| 1269 | public IDebugCounter invalidStoreEventWhileMaster; |
| 1270 | public IDebugCounter switchDisconnectedWhileSlave; |
| 1271 | public IDebugCounter switchActivated; |
| 1272 | public IDebugCounter errorSameSwitchReactivated; // err |
| 1273 | public IDebugCounter switchWithSameDpidActivated; // warn |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1274 | public IDebugCounter newSwitchActivated; // new switch |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1275 | public IDebugCounter syncedSwitchActivated; |
| 1276 | public IDebugCounter readyForReconcile; |
| 1277 | public IDebugCounter newSwitchFromStore; |
| 1278 | public IDebugCounter updatedSwitchFromStore; |
| 1279 | public IDebugCounter switchDisconnected; |
| 1280 | public IDebugCounter syncedSwitchRemoved; |
| 1281 | public IDebugCounter unknownSwitchRemovedFromStore; |
| 1282 | public IDebugCounter consolidateStoreRunCount; |
| 1283 | public IDebugCounter consolidateStoreInconsistencies; |
| 1284 | public IDebugCounter storeSyncError; |
| 1285 | public IDebugCounter switchesNotReconnectingToNewMaster; |
| 1286 | public IDebugCounter switchPortChanged; |
| 1287 | public IDebugCounter switchOtherChange; |
| 1288 | public IDebugCounter dispatchMessageWhileSlave; |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1289 | public IDebugCounter dispatchMessage; // does this cnt make sense? more |
| 1290 | // specific?? per type? count |
| 1291 | // stops? |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1292 | public IDebugCounter controllerNodeIpsChanged; |
| 1293 | public IDebugCounter messageReceived; |
| 1294 | public IDebugCounter messageInputThrottled; |
| 1295 | public IDebugCounter switchDisconnectReadTimeout; |
| 1296 | public IDebugCounter switchDisconnectHandshakeTimeout; |
| 1297 | public IDebugCounter switchDisconnectIOError; |
| 1298 | public IDebugCounter switchDisconnectParseError; |
| 1299 | public IDebugCounter switchDisconnectSwitchStateException; |
| 1300 | public IDebugCounter rejectedExecutionException; |
| 1301 | public IDebugCounter switchDisconnectOtherException; |
| 1302 | public IDebugCounter switchConnected; |
| 1303 | public IDebugCounter unhandledMessage; |
| 1304 | public IDebugCounter packetInWhileSwitchIsSlave; |
| 1305 | public IDebugCounter epermErrorWhileSwitchIsMaster; |
| 1306 | public IDebugCounter roleNotResentBecauseRolePending; |
| 1307 | public IDebugCounter roleRequestSent; |
| 1308 | public IDebugCounter roleReplyTimeout; |
| 1309 | public IDebugCounter roleReplyReceived; // expected RoleReply received |
| 1310 | public IDebugCounter roleReplyErrorUnsupported; |
| 1311 | public IDebugCounter switchCounterRegistrationFailed; |
| 1312 | public IDebugCounter packetParsingError; |
| 1313 | |
| 1314 | void createCounters(IDebugCounterService debugCounters) throws CounterException { |
| 1315 | setRoleEqual = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1316 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1317 | prefix, "set-role-equal", |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1318 | "Controller received a role request with role of " + |
| 1319 | "EQUAL which is unusual", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1320 | CounterType.ALWAYS_COUNT); |
| 1321 | setSameRole = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1322 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1323 | prefix, "set-same-role", |
| 1324 | "Controller received a role request for the same " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1325 | "role the controller already had", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1326 | CounterType.ALWAYS_COUNT, |
| 1327 | IDebugCounterService.CTR_MDATA_WARN); |
| 1328 | |
| 1329 | setRoleMaster = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1330 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1331 | prefix, "set-role-master", |
| 1332 | "Controller received a role request with role of " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1333 | "MASTER. This counter can be at most 1.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1334 | CounterType.ALWAYS_COUNT); |
| 1335 | |
| 1336 | remoteStoreNotification = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1337 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1338 | prefix, "remote-store-notification", |
| 1339 | "Received a notification from the sync service " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1340 | "indicating that switch information has changed", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1341 | CounterType.ALWAYS_COUNT); |
| 1342 | |
| 1343 | invalidPortsChanged = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1344 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1345 | prefix, "invalid-ports-changed", |
| 1346 | "Received an unexpected ports changed " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1347 | "notification while the controller was in " + |
| 1348 | "SLAVE role.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1349 | CounterType.ALWAYS_COUNT, |
| 1350 | IDebugCounterService.CTR_MDATA_WARN); |
| 1351 | |
| 1352 | invalidSwitchActivatedWhileSlave = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1353 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1354 | prefix, "invalid-switch-activated-while-slave", |
| 1355 | "Received an unexpected switchActivated " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1356 | "notification while the controller was in " + |
| 1357 | "SLAVE role.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1358 | CounterType.ALWAYS_COUNT, |
| 1359 | IDebugCounterService.CTR_MDATA_WARN); |
| 1360 | |
| 1361 | invalidStoreEventWhileMaster = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1362 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1363 | prefix, "invalid-store-event-while-master", |
| 1364 | "Received an unexpected notification from " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1365 | "the sync store while the controller was in " + |
| 1366 | "MASTER role.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1367 | CounterType.ALWAYS_COUNT, |
| 1368 | IDebugCounterService.CTR_MDATA_WARN); |
| 1369 | |
| 1370 | switchDisconnectedWhileSlave = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1371 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1372 | prefix, "switch-disconnected-while-slave", |
| 1373 | "A switch disconnected and the controller was " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1374 | "in SLAVE role.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1375 | CounterType.ALWAYS_COUNT, |
| 1376 | IDebugCounterService.CTR_MDATA_WARN); |
| 1377 | |
| 1378 | switchActivated = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1379 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1380 | prefix, "switch-activated", |
| 1381 | "A switch connected to this controller is now " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1382 | "in MASTER role", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1383 | CounterType.ALWAYS_COUNT); |
| 1384 | |
| 1385 | errorSameSwitchReactivated = // err |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1386 | debugCounters.registerCounter( |
| 1387 | prefix, "error-same-switch-reactivated", |
| 1388 | "A switch that was already in active state " + |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1389 | "was activated again. This indicates a " + |
| 1390 | "controller defect", |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1391 | CounterType.ALWAYS_COUNT, |
| 1392 | IDebugCounterService.CTR_MDATA_ERROR); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1393 | |
| 1394 | switchWithSameDpidActivated = // warn |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1395 | debugCounters.registerCounter( |
| 1396 | prefix, "switch-with-same-dpid-activated", |
| 1397 | "A switch with the same DPID as another switch " + |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1398 | "connected to the controller. This can be " + |
| 1399 | "caused by multiple switches configured with " + |
| 1400 | "the same DPID or by a switch reconnecting very " + |
| 1401 | "quickly.", |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1402 | CounterType.COUNT_ON_DEMAND, |
| 1403 | IDebugCounterService.CTR_MDATA_WARN); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1404 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1405 | newSwitchActivated = // new switch |
| 1406 | debugCounters.registerCounter( |
| 1407 | prefix, "new-switch-activated", |
| 1408 | "A new switch has completed the handshake as " + |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1409 | "MASTER. The switch was not known to any other " + |
| 1410 | "controller in the cluster", |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1411 | CounterType.ALWAYS_COUNT); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1412 | syncedSwitchActivated = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1413 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1414 | prefix, "synced-switch-activated", |
| 1415 | "A switch has completed the handshake as " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1416 | "MASTER. The switch was known to another " + |
| 1417 | "controller in the cluster", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1418 | CounterType.ALWAYS_COUNT); |
| 1419 | |
| 1420 | readyForReconcile = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1421 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1422 | prefix, "ready-for-reconcile", |
| 1423 | "Controller is ready for flow reconciliation " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1424 | "after Slave to Master transition. Either all " + |
| 1425 | "previously known switches are now active " + |
| 1426 | "or they have timed out and have been removed." + |
| 1427 | "This counter will be 0 or 1.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1428 | CounterType.ALWAYS_COUNT); |
| 1429 | |
| 1430 | newSwitchFromStore = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1431 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1432 | prefix, "new-switch-from-store", |
| 1433 | "A new switch has connected to another " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1434 | "another controller in the cluster. This " + |
| 1435 | "controller instance has received a sync store " + |
| 1436 | "notification for it.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1437 | CounterType.ALWAYS_COUNT); |
| 1438 | |
| 1439 | updatedSwitchFromStore = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1440 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1441 | prefix, "updated-switch-from-store", |
| 1442 | "Information about a switch connected to " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1443 | "another controller instance was updated in " + |
| 1444 | "the sync store. This controller instance has " + |
| 1445 | "received a notification for it", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1446 | CounterType.ALWAYS_COUNT); |
| 1447 | |
| 1448 | switchDisconnected = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1449 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1450 | prefix, "switch-disconnected", |
| 1451 | "FIXME: switch has disconnected", |
| 1452 | CounterType.ALWAYS_COUNT); |
| 1453 | |
| 1454 | syncedSwitchRemoved = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1455 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1456 | prefix, "synced-switch-removed", |
| 1457 | "A switch connected to another controller " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1458 | "instance has disconnected from the controller " + |
| 1459 | "cluster. This controller instance has " + |
| 1460 | "received a notification for it", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1461 | CounterType.ALWAYS_COUNT); |
| 1462 | |
| 1463 | unknownSwitchRemovedFromStore = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1464 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1465 | prefix, "unknown-switch-removed-from-store", |
| 1466 | "This controller instances has received a sync " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1467 | "store notification that a switch has " + |
| 1468 | "disconnected but this controller instance " + |
| 1469 | "did not have the any information about the " + |
| 1470 | "switch", // might be less than warning |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1471 | CounterType.ALWAYS_COUNT, |
| 1472 | IDebugCounterService.CTR_MDATA_WARN); |
| 1473 | |
| 1474 | consolidateStoreRunCount = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1475 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1476 | prefix, "consolidate-store-run-count", |
| 1477 | "This controller has transitioned from SLAVE " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1478 | "to MASTER and waited for switches to reconnect. " + |
| 1479 | "The controller has finished waiting and has " + |
| 1480 | "reconciled switch entries in the sync store " + |
| 1481 | "with live state", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1482 | CounterType.ALWAYS_COUNT); |
| 1483 | |
| 1484 | consolidateStoreInconsistencies = |
| 1485 | debugCounters.registerCounter( |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1486 | prefix, "consolidate-store-inconsistencies", |
| 1487 | "During switch sync store consolidation: " + |
| 1488 | "Number of switches that were in the store " + |
| 1489 | "but not otherwise known plus number of " + |
| 1490 | "switches that were in the store previously " + |
| 1491 | "but are now missing plus number of " + |
| 1492 | "connected switches that were absent from " + |
| 1493 | "the store although this controller has " + |
| 1494 | "written them. A non-zero count " + |
| 1495 | "indicates a brief split-brain dual MASTER " + |
| 1496 | "situation during fail-over", |
| 1497 | CounterType.ALWAYS_COUNT); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1498 | |
| 1499 | storeSyncError = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1500 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1501 | prefix, "store-sync-error", |
| 1502 | "Number of times a sync store operation failed " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1503 | "due to a store sync exception or an entry in " + |
| 1504 | "in the store had invalid data.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1505 | CounterType.ALWAYS_COUNT, |
| 1506 | IDebugCounterService.CTR_MDATA_ERROR); |
| 1507 | |
| 1508 | switchesNotReconnectingToNewMaster = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1509 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1510 | prefix, "switches-not-reconnecting-to-new-master", |
| 1511 | "Switches that were connected to another " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1512 | "controller instance in the cluster but that " + |
| 1513 | "did not reconnect to this controller after it " + |
| 1514 | "transitioned to MASTER", // might be less |
| 1515 | // than warning |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1516 | CounterType.ALWAYS_COUNT); |
| 1517 | |
| 1518 | switchPortChanged = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1519 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1520 | prefix, "switch-port-changed", |
| 1521 | "Number of times switch ports have changed", |
| 1522 | CounterType.ALWAYS_COUNT); |
| 1523 | switchOtherChange = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1524 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1525 | prefix, "switch-other-change", |
| 1526 | "Number of times other information of a switch " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1527 | "has changed.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1528 | CounterType.ALWAYS_COUNT); |
| 1529 | |
| 1530 | dispatchMessageWhileSlave = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1531 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1532 | prefix, "dispatch-message-while-slave", |
| 1533 | "Number of times an OF message was received " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1534 | "and supposed to be dispatched but the " + |
| 1535 | "controller was in SLAVE role and the message " + |
| 1536 | "was not dispatched", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1537 | CounterType.ALWAYS_COUNT); |
| 1538 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1539 | dispatchMessage = // does this cnt make sense? more specific?? per |
| 1540 | // type? count stops? |
| 1541 | debugCounters.registerCounter( |
| 1542 | prefix, "dispatch-message", |
| 1543 | "Number of times an OF message was dispatched " + |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1544 | "to registered modules", |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1545 | CounterType.ALWAYS_COUNT); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1546 | |
| 1547 | controllerNodeIpsChanged = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1548 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1549 | prefix, "controller-nodes-ips-changed", |
| 1550 | "IP addresses of controller nodes have changed", |
| 1551 | CounterType.ALWAYS_COUNT); |
| 1552 | |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1553 | // ------------------------ |
| 1554 | // channel handler counters. Factor them out ?? |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1555 | messageReceived = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1556 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1557 | prefix, "message-received", |
| 1558 | "Number of OpenFlow messages received. Some of " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1559 | "these might be throttled", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1560 | CounterType.ALWAYS_COUNT); |
| 1561 | messageInputThrottled = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1562 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1563 | prefix, "message-input-throttled", |
| 1564 | "Number of OpenFlow messages that were " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1565 | "throttled due to high load from the sender", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1566 | CounterType.ALWAYS_COUNT, |
| 1567 | IDebugCounterService.CTR_MDATA_WARN); |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1568 | // TODO: more counters in messageReceived ?? |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1569 | |
| 1570 | switchDisconnectReadTimeout = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1571 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1572 | prefix, "switch-disconnect-read-timeout", |
| 1573 | "Number of times a switch was disconnected due " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1574 | "due the switch failing to send OpenFlow " + |
| 1575 | "messages or responding to OpenFlow ECHOs", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1576 | CounterType.ALWAYS_COUNT, |
| 1577 | IDebugCounterService.CTR_MDATA_ERROR); |
| 1578 | switchDisconnectHandshakeTimeout = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1579 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1580 | prefix, "switch-disconnect-handshake-timeout", |
| 1581 | "Number of times a switch was disconnected " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1582 | "because it failed to complete the handshake " + |
| 1583 | "in time.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1584 | CounterType.ALWAYS_COUNT, |
| 1585 | IDebugCounterService.CTR_MDATA_ERROR); |
| 1586 | switchDisconnectIOError = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1587 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1588 | prefix, "switch-disconnect-io-error", |
| 1589 | "Number of times a switch was disconnected " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1590 | "due to IO errors on the switch connection.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1591 | CounterType.ALWAYS_COUNT, |
| 1592 | IDebugCounterService.CTR_MDATA_ERROR); |
| 1593 | switchDisconnectParseError = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1594 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1595 | prefix, "switch-disconnect-parse-error", |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1596 | "Number of times a switch was disconnected " + |
| 1597 | "because it sent an invalid packet that could " + |
| 1598 | "not be parsed", |
| 1599 | CounterType.ALWAYS_COUNT, |
| 1600 | IDebugCounterService.CTR_MDATA_ERROR); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1601 | |
| 1602 | switchDisconnectSwitchStateException = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1603 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1604 | prefix, "switch-disconnect-switch-state-exception", |
| 1605 | "Number of times a switch was disconnected " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1606 | "because it sent messages that were invalid " + |
| 1607 | "given the switch connection's state.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1608 | CounterType.ALWAYS_COUNT, |
| 1609 | IDebugCounterService.CTR_MDATA_ERROR); |
| 1610 | rejectedExecutionException = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1611 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1612 | prefix, "rejected-execution-exception", |
| 1613 | "TODO", |
| 1614 | CounterType.ALWAYS_COUNT, |
| 1615 | IDebugCounterService.CTR_MDATA_ERROR); |
| 1616 | |
| 1617 | switchDisconnectOtherException = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1618 | debugCounters.registerCounter( |
| 1619 | prefix, "switch-disconnect-other-exception", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1620 | "Number of times a switch was disconnected " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1621 | "due to an exceptional situation not covered " + |
| 1622 | "by other counters", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1623 | CounterType.ALWAYS_COUNT, |
| 1624 | IDebugCounterService.CTR_MDATA_ERROR); |
| 1625 | |
| 1626 | switchConnected = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1627 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1628 | prefix, "switch-connected", |
| 1629 | "Number of times a new switch connection was " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1630 | "established", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1631 | CounterType.ALWAYS_COUNT); |
| 1632 | |
| 1633 | unhandledMessage = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1634 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1635 | prefix, "unhandled-message", |
| 1636 | "Number of times an OpenFlow message was " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1637 | "received that the controller ignored because " + |
| 1638 | "it was inapproriate given the switch " + |
| 1639 | "connection's state.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1640 | CounterType.ALWAYS_COUNT, |
| 1641 | IDebugCounterService.CTR_MDATA_WARN); |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1642 | // might be less than warning |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1643 | |
| 1644 | packetInWhileSwitchIsSlave = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1645 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1646 | prefix, "packet-in-while-switch-is-slave", |
| 1647 | "Number of times a packet in was received " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1648 | "from a switch that was in SLAVE role. " + |
| 1649 | "Possibly inidicates inconsistent roles.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1650 | CounterType.ALWAYS_COUNT); |
| 1651 | epermErrorWhileSwitchIsMaster = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1652 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1653 | prefix, "eperm-error-while-switch-is-master", |
| 1654 | "Number of times a permission error was " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1655 | "received while the switch was in MASTER role. " + |
| 1656 | "Possibly inidicates inconsistent roles.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1657 | CounterType.ALWAYS_COUNT, |
| 1658 | IDebugCounterService.CTR_MDATA_WARN); |
| 1659 | |
| 1660 | roleNotResentBecauseRolePending = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1661 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1662 | prefix, "role-not-resent-because-role-pending", |
| 1663 | "The controller tried to reestablish a role " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1664 | "with a switch but did not do so because a " + |
| 1665 | "previous role request was still pending", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1666 | CounterType.ALWAYS_COUNT); |
| 1667 | roleRequestSent = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1668 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1669 | prefix, "role-request-sent", |
| 1670 | "Number of times the controller sent a role " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1671 | "request to a switch.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1672 | CounterType.ALWAYS_COUNT); |
| 1673 | roleReplyTimeout = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1674 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1675 | prefix, "role-reply-timeout", |
| 1676 | "Number of times a role request message did not " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1677 | "receive the expected reply from a switch", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1678 | CounterType.ALWAYS_COUNT, |
| 1679 | IDebugCounterService.CTR_MDATA_WARN); |
| 1680 | |
| 1681 | roleReplyReceived = // expected RoleReply received |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1682 | debugCounters.registerCounter( |
| 1683 | prefix, "role-reply-received", |
| 1684 | "Number of times the controller received the " + |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1685 | "expected role reply message from a switch", |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1686 | CounterType.ALWAYS_COUNT); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1687 | |
| 1688 | roleReplyErrorUnsupported = |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1689 | debugCounters.registerCounter( |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1690 | prefix, "role-reply-error-unsupported", |
| 1691 | "Number of times the controller received an " + |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1692 | "error from a switch in response to a role " + |
| 1693 | "request indicating that the switch does not " + |
| 1694 | "support roles.", |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1695 | CounterType.ALWAYS_COUNT); |
| 1696 | |
| 1697 | switchCounterRegistrationFailed = |
| 1698 | debugCounters.registerCounter(prefix, |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1699 | "switch-counter-registration-failed", |
| 1700 | "Number of times the controller failed to " + |
| 1701 | "register per-switch debug counters", |
| 1702 | CounterType.ALWAYS_COUNT, |
| 1703 | IDebugCounterService.CTR_MDATA_WARN); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1704 | |
| 1705 | packetParsingError = |
| 1706 | debugCounters.registerCounter(prefix, |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1707 | "packet-parsing-error", |
| 1708 | "Number of times the packet parsing " + |
| 1709 | "encountered an error", |
| 1710 | CounterType.ALWAYS_COUNT, |
| 1711 | IDebugCounterService.CTR_MDATA_ERROR); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1712 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1713 | } |
| 1714 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1715 | @Override |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1716 | public Counters getCounters() { |
| 1717 | return this.counters; |
| 1718 | } |
| 1719 | |
| 1720 | // ************** |
| 1721 | // debugEvent registrations |
| 1722 | // ************** |
| 1723 | |
| 1724 | private void registerControllerDebugEvents() throws FloodlightModuleException { |
| 1725 | if (debugEvents == null) { |
| 1726 | debugEvents = new NullDebugEvent(); |
| 1727 | } |
| 1728 | try { |
| 1729 | evSwitch = debugEvents.registerEvent( |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1730 | Counters.prefix, "switchevent", |
| 1731 | "Switch connected, disconnected or port changed", |
| 1732 | EventType.ALWAYS_LOG, SwitchEvent.class, 100); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1733 | } catch (MaxEventsRegistered e) { |
| 1734 | throw new FloodlightModuleException("Max events registered", e); |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | public class SwitchEvent { |
| 1739 | @EventColumn(name = "dpid", description = EventFieldType.DPID) |
| 1740 | long dpid; |
| 1741 | |
| 1742 | @EventColumn(name = "reason", description = EventFieldType.STRING) |
| 1743 | String reason; |
| 1744 | |
| 1745 | public SwitchEvent(long dpid, String reason) { |
| 1746 | this.dpid = dpid; |
| 1747 | this.reason = reason; |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | // ************** |
| 1752 | // Utility methods |
| 1753 | // ************** |
| 1754 | |
| 1755 | @Override |
| 1756 | public void setAlwaysClearFlowsOnSwActivate(boolean value) { |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1757 | // this.alwaysClearFlowsOnSwActivate = value; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1758 | // XXX S need to be a little more careful about this |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | @Override |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1762 | public Map<String, Long> getMemory() { |
| 1763 | Map<String, Long> m = new HashMap<String, Long>(); |
| 1764 | Runtime runtime = Runtime.getRuntime(); |
| 1765 | m.put("total", runtime.totalMemory()); |
| 1766 | m.put("free", runtime.freeMemory()); |
| 1767 | return m; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1768 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 1769 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1770 | @Override |
| 1771 | public Long getUptime() { |
| 1772 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 1773 | return rb.getUptime(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1774 | } |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1775 | |
| 1776 | /** |
| 1777 | * Forward to the driver-manager to get an IOFSwitch instance. |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1778 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1779 | * @param desc |
| 1780 | * @return |
| 1781 | */ |
| 1782 | protected IOFSwitch getOFSwitchInstance(OFDescStatsReply desc, OFVersion ofv) { |
| 1783 | return DriverManager.getOFSwitchImpl(desc, ofv); |
| 1784 | } |
| 1785 | |
| 1786 | protected IThreadPoolService getThreadPoolService() { |
| 1787 | return this.threadPool; |
| 1788 | } |
| 1789 | |
| 1790 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1791 | * Part of the controller updates framework (see 'run()' method) Use this |
| 1792 | * method to add an IUpdate. A thread-pool will serve the update by |
| 1793 | * dispatching it to all listeners for that update. |
| 1794 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1795 | * @param update |
| 1796 | */ |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1797 | @LogMessageDoc(level = "WARN", |
| 1798 | message = "Failure adding update {} to queue", |
| 1799 | explanation = "The controller tried to add an internal notification" + |
| 1800 | " to its message queue but the add failed.", |
| 1801 | recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG) |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1802 | private void addUpdateToQueue(IUpdate update) { |
| 1803 | try { |
| 1804 | this.updates.put(update); |
| 1805 | } catch (InterruptedException e) { |
| 1806 | // This should never happen |
| 1807 | log.error("Failure adding update {} to queue.", update); |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | void flushAll() { |
| 1812 | // Flush all flow-mods/packet-out/stats generated from this "train" |
| 1813 | OFSwitchImplBase.flush_all(); |
| 1814 | debugCounters.flushCounters(); |
| 1815 | debugEvents.flushEvents(); |
| 1816 | } |
| 1817 | |
| 1818 | /** |
| 1819 | * flcontext_free - Free the context to the current thread |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1820 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1821 | * @param flcontext |
| 1822 | */ |
| 1823 | protected void flcontext_free(FloodlightContext flcontext) { |
| 1824 | flcontext.getStorage().clear(); |
| 1825 | flcontext_cache.get().push(flcontext); |
| 1826 | } |
| 1827 | |
| 1828 | @LogMessageDoc(message = "Calling System.exit", |
| 1829 | explanation = "The controller is terminating") |
| 1830 | private synchronized void terminate() { |
| 1831 | log.info("Calling System.exit"); |
| 1832 | System.exit(1); |
| 1833 | } |
| 1834 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1835 | // *************** |
| 1836 | // Floodlight context related |
| 1837 | // *************** |
| 1838 | |
| 1839 | /** |
| 1840 | * flcontext_cache - Keep a thread local stack of contexts |
| 1841 | */ |
| 1842 | protected static final ThreadLocal<Stack<FloodlightContext>> flcontext_cache = |
| 1843 | new ThreadLocal<Stack<FloodlightContext>>() { |
| 1844 | @Override |
| 1845 | protected Stack<FloodlightContext> initialValue() { |
| 1846 | return new Stack<FloodlightContext>(); |
| 1847 | } |
| 1848 | }; |
| 1849 | |
| 1850 | /** |
Saurav Das | fcdad07 | 2014-08-13 14:15:21 -0700 | [diff] [blame] | 1851 | * flcontext_alloc - pop a context off the stack, if required create a new |
| 1852 | * one |
| 1853 | * |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 1854 | * @return FloodlightContext |
| 1855 | */ |
| 1856 | protected static FloodlightContext flcontext_alloc() { |
| 1857 | FloodlightContext flcontext = null; |
| 1858 | |
| 1859 | if (flcontext_cache.get().empty()) { |
| 1860 | flcontext = new FloodlightContext(); |
| 1861 | } else { |
| 1862 | flcontext = flcontext_cache.get().pop(); |
| 1863 | } |
| 1864 | |
| 1865 | return flcontext; |
| 1866 | } |
| 1867 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1868 | } |