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