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