blob: 23d61bb4213c73f3241dd7d40bb086cd7cebc021 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -080017
18package net.floodlightcontroller.core.internal;
19
20import java.io.FileInputStream;
21import java.io.IOException;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070022import java.lang.management.ManagementFactory;
23import java.lang.management.RuntimeMXBean;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024import java.net.InetSocketAddress;
Jonathan Hartd10008d2013-02-23 17:04:08 -080025import java.net.UnknownHostException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026import java.util.Collections;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027import java.util.HashMap;
28import java.util.HashSet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080029import java.util.List;
30import java.util.Map;
31import java.util.Map.Entry;
32import java.util.Properties;
33import java.util.Set;
34import java.util.Stack;
35import java.util.concurrent.BlockingQueue;
36import java.util.concurrent.ConcurrentHashMap;
37import java.util.concurrent.ConcurrentMap;
38import java.util.concurrent.CopyOnWriteArraySet;
39import java.util.concurrent.Executors;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080040import java.util.concurrent.LinkedBlockingQueue;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041
42import net.floodlightcontroller.core.FloodlightContext;
43import net.floodlightcontroller.core.IFloodlightProviderService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080044import net.floodlightcontroller.core.IListener.Command;
Jonathan Hartd10008d2013-02-23 17:04:08 -080045import net.floodlightcontroller.core.IOFMessageListener;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080046import net.floodlightcontroller.core.IOFSwitch;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070047import net.floodlightcontroller.core.IOFSwitch.PortChangeType;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048import net.floodlightcontroller.core.IOFSwitchFilter;
49import net.floodlightcontroller.core.IOFSwitchListener;
Pankaj Berdedc73bb12013-08-14 13:46:38 -070050import net.floodlightcontroller.core.IUpdate;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051import net.floodlightcontroller.core.annotations.LogMessageDoc;
52import net.floodlightcontroller.core.annotations.LogMessageDocs;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070053import net.floodlightcontroller.core.internal.OFChannelHandler.RoleRecvStatus;
54import net.floodlightcontroller.core.module.FloodlightModuleException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080055import net.floodlightcontroller.core.util.ListenerDispatcher;
56import net.floodlightcontroller.core.web.CoreWebRoutable;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070057import net.floodlightcontroller.debugcounter.IDebugCounter;
58import net.floodlightcontroller.debugcounter.IDebugCounterService;
59import net.floodlightcontroller.debugcounter.IDebugCounterService.CounterException;
60import net.floodlightcontroller.debugcounter.IDebugCounterService.CounterType;
61import net.floodlightcontroller.debugevent.IDebugEventService;
62import net.floodlightcontroller.debugevent.IDebugEventService.EventColumn;
63import net.floodlightcontroller.debugevent.IDebugEventService.EventFieldType;
64import net.floodlightcontroller.debugevent.IDebugEventService.EventType;
65import net.floodlightcontroller.debugevent.IDebugEventService.MaxEventsRegistered;
66import net.floodlightcontroller.debugevent.IEventUpdater;
67import net.floodlightcontroller.debugevent.NullDebugEvent;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068import net.floodlightcontroller.restserver.IRestApiService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080069import net.floodlightcontroller.threadpool.IThreadPoolService;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070070import net.floodlightcontroller.util.LoadMonitor;
71import net.onrc.onos.core.drivermanager.DriverManager;
Jonathan Hart23701d12014-04-03 10:45:48 -070072import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070073import net.onrc.onos.core.packet.Ethernet;
74import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070075import net.onrc.onos.core.registry.IControllerRegistryService.ControlChangeCallback;
Jonathan Harta99ec672014-04-03 11:30:34 -070076import net.onrc.onos.core.registry.RegistryException;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -070077import net.onrc.onos.core.util.Dpid;
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -070078import net.onrc.onos.core.util.OnosInstanceId;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080079
80import org.jboss.netty.bootstrap.ServerBootstrap;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080081import org.jboss.netty.channel.ChannelPipelineFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080082import org.jboss.netty.channel.group.ChannelGroup;
83import org.jboss.netty.channel.group.DefaultChannelGroup;
84import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070085import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
86import org.projectfloodlight.openflow.protocol.OFFactories;
87import org.projectfloodlight.openflow.protocol.OFFactory;
88import org.projectfloodlight.openflow.protocol.OFMessage;
89import org.projectfloodlight.openflow.protocol.OFPacketIn;
90import org.projectfloodlight.openflow.protocol.OFPortDesc;
91import org.projectfloodlight.openflow.protocol.OFType;
92import org.projectfloodlight.openflow.protocol.OFVersion;
93import org.projectfloodlight.openflow.protocol.match.MatchField;
94import org.projectfloodlight.openflow.protocol.match.MatchFields;
95import org.projectfloodlight.openflow.types.EthType;
96import org.projectfloodlight.openflow.types.OFPort;
97import org.projectfloodlight.openflow.util.HexString;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080098import org.slf4j.Logger;
99import org.slf4j.LoggerFactory;
100
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800101/**
Saurav Dasfcdad072014-08-13 14:15:21 -0700102 * The main controller class. Handles all setup and network listeners -
103 * Distributed ownership control of switch through IControllerRegistryService
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 */
Jonathan Hart2fa28062013-11-25 20:16:28 -0800105public class Controller implements IFloodlightProviderService {
Ray Milkey269ffb92014-04-03 14:43:30 -0700106
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -0700107 protected final static Logger log = LoggerFactory.getLogger(Controller.class);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700108 static final String ERROR_DATABASE =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800109 "The controller could not communicate with the system database.";
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700110 protected static OFFactory factory13 = OFFactories.getFactory(OFVersion.OF_13);
111 protected static OFFactory factory10 = OFFactories.getFactory(OFVersion.OF_10);
Ray Milkey269ffb92014-04-03 14:43:30 -0700112
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700113 // connectedSwitches cache contains all connected switch's channelHandlers
114 // including ones where this controller is a master/equal/slave controller
115 // as well as ones that have not been activated yet
116 protected ConcurrentHashMap<Long, OFChannelHandler> connectedSwitches;
117 // These caches contains only those switches that are active
118 protected ConcurrentHashMap<Long, IOFSwitch> activeMasterSwitches;
119 protected ConcurrentHashMap<Long, IOFSwitch> activeEqualSwitches;
120 // lock to synchronize on, when manipulating multiple caches above
121 private Object multiCacheLock;
Ray Milkey269ffb92014-04-03 14:43:30 -0700122
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700123 // The controllerNodeIPsCache maps Controller IDs to their IP address.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800124 // It's only used by handleControllerNodeIPsChanged
125 protected HashMap<String, String> controllerNodeIPsCache;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800126 protected BlockingQueue<IUpdate> updates;
Ray Milkey269ffb92014-04-03 14:43:30 -0700127
Saurav Dasfcdad072014-08-13 14:15:21 -0700128 protected ConcurrentMap<OFType, ListenerDispatcher<OFType, IOFMessageListener>> messageListeners;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700129 protected Set<IOFSwitchListener> switchListeners;
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700130
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800131 // Module dependencies
132 protected IRestApiService restApi;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800133 protected IThreadPoolService threadPool;
Jonathan Hartd10008d2013-02-23 17:04:08 -0800134 protected IControllerRegistryService registryService;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700135 protected IDebugCounterService debugCounters;
136 protected IDebugEventService debugEvents;
Ray Milkey269ffb92014-04-03 14:43:30 -0700137
Jonathan Hart8a5d0972013-12-04 10:02:44 -0800138 protected ILinkDiscoveryService linkDiscovery;
Ray Milkey269ffb92014-04-03 14:43:30 -0700139
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800140 // Configuration options
141 protected int openFlowPort = 6633;
142 protected int workerThreads = 0;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700143
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800144 // The id for this controller node. Should be unique for each controller
145 // node in a controller cluster.
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700146 private OnosInstanceId onosInstanceId = new OnosInstanceId("localhost");
Ray Milkey269ffb92014-04-03 14:43:30 -0700147
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700148 // defined counters
149 private Counters counters;
150 // Event IDs for debug events
151 protected IEventUpdater<SwitchEvent> evSwitch;
152
153 // Load monitor for overload protection
154 protected final boolean overload_drop =
Saurav Dasfcdad072014-08-13 14:15:21 -0700155 Boolean.parseBoolean(System.getProperty("overload_drop", "false"));
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700156 protected final LoadMonitor loadmonitor = new LoadMonitor(log);
Ray Milkey269ffb92014-04-03 14:43:30 -0700157
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800158 // Start time of the controller
159 protected long systemStartTime;
Ray Milkey269ffb92014-04-03 14:43:30 -0700160
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800161 // Flag to always flush flow table on switch reconnect (HA or otherwise)
162 protected boolean alwaysClearFlowsOnSwAdd = false;
Ray Milkey269ffb92014-04-03 14:43:30 -0700163
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800164 // Perf. related configuration
165 protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
166 protected static final int BATCH_MAX_SIZE = 100;
Ray Milkey269ffb92014-04-03 14:43:30 -0700167 protected static final boolean ALWAYS_DECODE_ETH = true;
168
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700169 // ******************************
170 // Switch Management and Updates
171 // ******************************
172
173 /**
Saurav Dasfcdad072014-08-13 14:15:21 -0700174 * Switch updates are sent to all IOFSwitchListeners. A switch that is
175 * connected to this controller instance, but not activated, is not
176 * available for updates.
Jonathan Hartcb34f382014-08-12 21:11:03 -0700177 *
Saurav Dasfcdad072014-08-13 14:15:21 -0700178 * In ONOS, each controller instance can simultaneously serve in a MASTER
179 * role for some connected switches, and in a EQUAL role for other connected
180 * switches. The EQUAL role can be treated as a SLAVE role, by ensuring that
181 * the controller instance never sends packets or commands out to the
182 * switch. Activated switches, either with Controller Role MASTER or EQUAL
183 * are announced as updates. We also support announcements of controller
184 * role transitions from MASTER --> EQUAL, and EQUAL --> MASTER, for an
185 * individual switch.
Jonathan Hartcb34f382014-08-12 21:11:03 -0700186 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700187 * Disconnection of only activated switches are announced. Finally, changes
188 * to switch ports are announced with a portChangeType (see @IOFSwitch)
Jonathan Hartcb34f382014-08-12 21:11:03 -0700189 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700190 * @author saurav
191 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800192 public enum SwitchUpdateType {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700193 /** switch activated with this controller's role as MASTER */
194 ACTIVATED_MASTER,
Saurav Dasfcdad072014-08-13 14:15:21 -0700195 /**
196 * switch activated with this controller's role as EQUAL. listener can
197 * treat this controller's role as SLAVE by not sending packets or
198 * commands to the switch
199 */
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700200 ACTIVATED_EQUAL,
201 /** this controller's role for this switch changed from Master to Equal */
202 MASTER_TO_EQUAL,
203 /** this controller's role for this switch changed form Equal to Master */
204 EQUAL_TO_MASTER,
205 /** A previously activated switch disconnected */
206 DISCONNECTED,
207 /** Port changed on a previously activated switch */
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700208 PORTCHANGED,
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800209 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700210
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800211 /**
Saurav Dasfcdad072014-08-13 14:15:21 -0700212 * Update message indicating a switch was added or removed ONOS: This
213 * message extended to indicate Port add or removed event.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800214 */
215 protected class SwitchUpdate implements IUpdate {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700216 public long getSwId() {
217 return swId;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800218 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700219
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700220 public SwitchUpdateType getSwitchUpdateType() {
221 return switchUpdateType;
222 }
223
224 public PortChangeType getPortChangeType() {
225 return changeType;
226 }
227
228 private final long swId;
229 private final SwitchUpdateType switchUpdateType;
230 private final OFPortDesc port;
231 private final PortChangeType changeType;
232
233 public SwitchUpdate(long swId, SwitchUpdateType switchUpdateType) {
234 this(swId, switchUpdateType, null, null);
235 }
Saurav Dasfcdad072014-08-13 14:15:21 -0700236
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700237 public SwitchUpdate(long swId,
Saurav Dasfcdad072014-08-13 14:15:21 -0700238 SwitchUpdateType switchUpdateType,
239 OFPortDesc port,
240 PortChangeType changeType) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700241 if (switchUpdateType == SwitchUpdateType.PORTCHANGED) {
242 if (port == null) {
243 throw new NullPointerException("Port must not be null " +
244 "for PORTCHANGED updates");
245 }
246 if (changeType == null) {
247 throw new NullPointerException("ChangeType must not be " +
248 "null for PORTCHANGED updates");
249 }
250 } else {
251 if (port != null || changeType != null) {
252 throw new IllegalArgumentException("port and changeType " +
253 "must be null for " + switchUpdateType +
254 " updates");
255 }
256 }
257 this.swId = swId;
258 this.switchUpdateType = switchUpdateType;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700259 this.port = port;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700260 this.changeType = changeType;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700261 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700262
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700263 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800264 public void dispatch() {
265 if (log.isTraceEnabled()) {
266 log.trace("Dispatching switch update {} {}",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700267 HexString.toHexString(swId), switchUpdateType);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800268 }
269 if (switchListeners != null) {
270 for (IOFSwitchListener listener : switchListeners) {
Saurav Dasfcdad072014-08-13 14:15:21 -0700271 switch (switchUpdateType) {
272 case ACTIVATED_MASTER:
273 // don't count here. We have more specific
274 // counters before the update is created
275 listener.switchActivatedMaster(swId);
276 break;
277 case ACTIVATED_EQUAL:
278 // don't count here. We have more specific
279 // counters before the update is created
280 listener.switchActivatedEqual(swId);
281 break;
282 case MASTER_TO_EQUAL:
283 listener.switchMasterToEqual(swId);
284 break;
285 case EQUAL_TO_MASTER:
286 listener.switchEqualToMaster(swId);
287 break;
288 case DISCONNECTED:
289 // don't count here. We have more specific
290 // counters before the update is created
291 listener.switchDisconnected(swId);
292 break;
293 case PORTCHANGED:
294 counters.switchPortChanged.updateCounterWithFlush();
295 listener.switchPortChanged(swId, port, changeType);
296 break;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800297 }
298 }
299 }
300 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700301
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700302 }
303
304 protected boolean addConnectedSwitch(long dpid, OFChannelHandler h) {
305 if (connectedSwitches.get(dpid) != null) {
306 log.error("Trying to add connectedSwitch but found a previous "
307 + "value for dpid: {}", dpid);
308 return false;
309 } else {
310 connectedSwitches.put(dpid, h);
311 return true;
312 }
313 }
314
315 /**
Saurav Dasfcdad072014-08-13 14:15:21 -0700316 * Switch Events
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700317 */
318 @Override
319 public void addSwitchEvent(long dpid, String reason, boolean flushNow) {
320 if (flushNow)
321 evSwitch.updateEventWithFlush(new SwitchEvent(dpid, reason));
322 else
323 evSwitch.updateEventNoFlush(new SwitchEvent(dpid, reason));
324 }
325
326 private boolean validActivation(long dpid) {
327 if (connectedSwitches.get(dpid) == null) {
328 log.error("Trying to activate switch but is not in "
329 + "connected switches: dpid {}. Aborting ..",
330 HexString.toHexString(dpid));
331 return false;
332 }
333 if (activeMasterSwitches.get(dpid) != null ||
334 activeEqualSwitches.get(dpid) != null) {
335 log.error("Trying to activate switch but it is already "
336 + "activated: dpid {}. Found in activeMaster: {} "
337 + "Found in activeEqual: {}. Aborting ..", new Object[] {
Saurav Dasfcdad072014-08-13 14:15:21 -0700338 HexString.toHexString(dpid),
339 (activeMasterSwitches.get(dpid) == null) ? 'Y' : 'N',
340 (activeEqualSwitches.get(dpid) == null) ? 'Y' : 'N'});
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700341 counters.switchWithSameDpidActivated.updateCounterWithFlush();
342 return false;
343 }
344 return true;
345 }
346
347 /**
348 * Called when a switch is activated, with this controller's role as MASTER
349 */
350 protected boolean addActivatedMasterSwitch(long dpid, IOFSwitch sw) {
Saurav Dasfcdad072014-08-13 14:15:21 -0700351 synchronized (multiCacheLock) {
352 if (!validActivation(dpid))
353 return false;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700354 activeMasterSwitches.put(dpid, sw);
355 }
Pavlin Radoslavov000fbae2014-08-19 19:42:58 -0700356
Saurav Dasfcdad072014-08-13 14:15:21 -0700357 // update counters and events
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700358 counters.switchActivated.updateCounterWithFlush();
359 evSwitch.updateEventWithFlush(new SwitchEvent(dpid, "activeMaster"));
360 addUpdateToQueue(new SwitchUpdate(dpid,
361 SwitchUpdateType.ACTIVATED_MASTER));
362 return true;
363 }
364
365 /**
366 * Called when a switch is activated, with this controller's role as EQUAL
367 */
368 protected boolean addActivatedEqualSwitch(long dpid, IOFSwitch sw) {
Saurav Dasfcdad072014-08-13 14:15:21 -0700369 synchronized (multiCacheLock) {
370 if (!validActivation(dpid))
371 return false;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700372 activeEqualSwitches.put(dpid, sw);
373 }
Saurav Dasfcdad072014-08-13 14:15:21 -0700374 // update counters and events
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700375 counters.switchActivated.updateCounterWithFlush();
376 evSwitch.updateEventWithFlush(new SwitchEvent(dpid, "activeEqual"));
377 addUpdateToQueue(new SwitchUpdate(dpid,
378 SwitchUpdateType.ACTIVATED_EQUAL));
379 return true;
380 }
381
382 /**
Saurav Dasfcdad072014-08-13 14:15:21 -0700383 * Called when this controller's role for a switch transitions from equal to
384 * master. For 1.0 switches, we internally refer to the role 'slave' as
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700385 * 'equal' - so this transition is equivalent to 'addActivatedMasterSwitch'.
386 */
387 protected void transitionToMasterSwitch(long dpid) {
Saurav Dasfcdad072014-08-13 14:15:21 -0700388 synchronized (multiCacheLock) {
389 IOFSwitch sw = activeEqualSwitches.remove(dpid);
390 if (sw == null) {
391 log.error("Transition to master called on sw {}, but switch "
392 + "was not found in controller-cache", dpid);
393 return;
394 }
395 activeMasterSwitches.put(dpid, sw);
396 }
397 addUpdateToQueue(new SwitchUpdate(dpid,
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700398 SwitchUpdateType.EQUAL_TO_MASTER));
399 }
400
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700401 /**
Saurav Dasfcdad072014-08-13 14:15:21 -0700402 * Called when this controller's role for a switch transitions to equal. For
403 * 1.0 switches, we internally refer to the role 'slave' as 'equal'.
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700404 */
405 protected void transitionToEqualSwitch(long dpid) {
Saurav Dasfcdad072014-08-13 14:15:21 -0700406 synchronized (multiCacheLock) {
407 IOFSwitch sw = activeMasterSwitches.remove(dpid);
408 if (sw == null) {
409 log.error("Transition to equal called on sw {}, but switch "
410 + "was not found in controller-cache", dpid);
411 return;
412 }
413 activeEqualSwitches.put(dpid, sw);
414 }
415 addUpdateToQueue(new SwitchUpdate(dpid,
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700416 SwitchUpdateType.MASTER_TO_EQUAL));
417 }
418
419 /**
420 * Clear all state in controller switch maps for a switch that has
Saurav Dasfcdad072014-08-13 14:15:21 -0700421 * disconnected from the local controller. Also release control for that
422 * switch from the global repository. Notify switch listeners.
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700423 */
424 protected void removeConnectedSwitch(long dpid) {
425 releaseRegistryControl(dpid);
426 connectedSwitches.remove(dpid);
427 IOFSwitch sw = activeMasterSwitches.remove(dpid);
Saurav Dasfcdad072014-08-13 14:15:21 -0700428 if (sw == null) {
429 sw = activeEqualSwitches.remove(dpid);
430 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700431 if (sw != null) {
432 sw.cancelAllStatisticsReplies();
433 sw.setConnected(false); // do we need this?
434 }
435 evSwitch.updateEventWithFlush(new SwitchEvent(dpid, "disconnected"));
436 counters.switchDisconnected.updateCounterWithFlush();
437 addUpdateToQueue(new SwitchUpdate(dpid, SwitchUpdateType.DISCONNECTED));
438 }
439
440 /**
Saurav Dasfcdad072014-08-13 14:15:21 -0700441 * Indicates that ports on the given switch have changed. Enqueue a switch
442 * update.
Jonathan Hartcb34f382014-08-12 21:11:03 -0700443 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700444 * @param sw
445 */
446 protected void notifyPortChanged(long dpid, OFPortDesc port,
447 PortChangeType changeType) {
448 if (port == null || changeType == null) {
449 String msg = String.format("Switch port or changetType must not "
450 + "be null in port change notification");
451 throw new NullPointerException(msg);
452 }
453 if (connectedSwitches.get(dpid) == null || getSwitch(dpid) == null) {
454 log.warn("Port change update on switch {} not connected or activated "
455 + "... Aborting.", HexString.toHexString(dpid));
456 return;
457 }
458
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700459 SwitchUpdate update = new SwitchUpdate(dpid, SwitchUpdateType.PORTCHANGED,
460 port, changeType);
461 addUpdateToQueue(update);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800462 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700463
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800464 // ***************
465 // Getters/Setters
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700466 // ***************
Ray Milkey269ffb92014-04-03 14:43:30 -0700467
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800468 public void setRestApiService(IRestApiService restApi) {
469 this.restApi = restApi;
470 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700471
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800472 public void setThreadPoolService(IThreadPoolService tp) {
473 this.threadPool = tp;
474 }
475
Ray Milkey269ffb92014-04-03 14:43:30 -0700476 public void setMastershipService(IControllerRegistryService serviceImpl) {
477 this.registryService = serviceImpl;
Pankaj Berdedc73bb12013-08-14 13:46:38 -0700478 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700479
480 public void setLinkDiscoveryService(ILinkDiscoveryService linkDiscovery) {
481 this.linkDiscovery = linkDiscovery;
482 }
483
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700484 public void setDebugCounter(IDebugCounterService debugCounters) {
485 this.debugCounters = debugCounters;
486 }
487
488 public void setDebugEvent(IDebugEventService debugEvents) {
489 this.debugEvents = debugEvents;
490 }
491
492 IDebugCounterService getDebugCounter() {
493 return this.debugCounters;
Ray Milkey269ffb92014-04-03 14:43:30 -0700494 }
495
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800496 // **********************
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700497 // Role Handling
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800498 // **********************
Ray Milkey269ffb92014-04-03 14:43:30 -0700499
Saurav Dasfcdad072014-08-13 14:15:21 -0700500 /**
501 * created by ONOS - works with registry service
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800502 */
Jonathan Hartcc957a02013-02-26 10:39:04 -0800503 protected class RoleChangeCallback implements ControlChangeCallback {
Ray Milkey269ffb92014-04-03 14:43:30 -0700504 @Override
Pavlin Radoslavov695f8952014-07-23 16:57:01 -0700505 public void controlChanged(long dpidLong, boolean hasControl) {
506 Dpid dpid = new Dpid(dpidLong);
Ray Milkey269ffb92014-04-03 14:43:30 -0700507 log.info("Role change callback for switch {}, hasControl {}",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700508 dpid, hasControl);
Pankaj Berde01939e92013-03-08 14:38:27 -0800509
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800510 Role role = null;
Ray Milkey269ffb92014-04-03 14:43:30 -0700511
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700512 /*
513 * issue #229
514 * Cannot rely on sw.getRole() as it can be behind due to pending
515 * role changes in the queue. Just submit it and late the
516 * RoleChanger handle duplicates.
517 */
Ray Milkey269ffb92014-04-03 14:43:30 -0700518
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700519 if (hasControl) {
520 role = Role.MASTER;
Ray Milkey269ffb92014-04-03 14:43:30 -0700521 } else {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700522 role = Role.EQUAL; // treat the same as Role.SLAVE
Ray Milkey269ffb92014-04-03 14:43:30 -0700523 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700524
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700525 OFChannelHandler swCh = connectedSwitches.get(dpid.value());
526 if (swCh == null) {
527 log.warn("Switch {} not found in connected switches", dpid);
528 return;
Pankaj Berde465ac7c2013-05-23 13:47:49 -0700529 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700530
531 log.debug("Sending role request {} msg to {}", role, dpid);
532 swCh.sendRoleRequest(role, RoleRecvStatus.MATCHED_SET_ROLE);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800533 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700534 }
535
536 public synchronized void submitRegistryRequest(long dpid) {
537 OFChannelHandler h = connectedSwitches.get(dpid);
538 if (h == null) {
539 log.error("Trying to request registry control for switch {} "
540 + "not in connected switches. Aborting.. ",
541 HexString.toHexString(dpid));
542 // FIXME shouldn't we immediately return here?
543 }
Saurav Dasfcdad072014-08-13 14:15:21 -0700544 // Request control of the switch from the global registry
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800545 try {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700546 h.controlRequested = Boolean.TRUE;
547 registryService.requestControl(dpid, new RoleChangeCallback());
548 } catch (RegistryException e) {
549 log.debug("Registry error: {}", e.getMessage());
550 h.controlRequested = Boolean.FALSE;
551 }
552 if (!h.controlRequested) { // XXX what is being attempted here?
553 // yield to allow other thread(s) to release control
554 try {
555 Thread.sleep(10);
556 } catch (InterruptedException e) {
557 // Ignore interruptions
558 }
Saurav Dasfcdad072014-08-13 14:15:21 -0700559 // safer to bounce the switch to reconnect here than proceeding
560 // further
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700561 // XXX S why? can't we just try again a little later?
562 log.debug("Closing sw:{} because we weren't able to request control " +
563 "successfully" + dpid);
564 connectedSwitches.get(dpid).disconnectSwitch();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800565 }
566 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700567
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700568 public synchronized void releaseRegistryControl(long dpidLong) {
569 OFChannelHandler h = connectedSwitches.get(dpidLong);
570 if (h == null) {
571 log.error("Trying to release registry control for switch {} "
572 + "not in connected switches. Aborting.. ",
573 HexString.toHexString(dpidLong));
574 return;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800575 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700576 if (h.controlRequested) {
577 registryService.releaseControl(dpidLong);
578 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800579 }
580
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700581 // *******************
582 // OF Message Handling
583 // *******************
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800584
585 /**
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700586 * Handle and dispatch a message to IOFMessageListeners.
Jonathan Hartcb34f382014-08-12 21:11:03 -0700587 *
Saurav Dasfcdad072014-08-13 14:15:21 -0700588 * We only dispatch messages to listeners if the controller's role is
589 * MASTER.
Jonathan Hartcb34f382014-08-12 21:11:03 -0700590 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700591 * @param sw The switch sending the message
592 * @param m The message the switch sent
Saurav Dasfcdad072014-08-13 14:15:21 -0700593 * @param flContext The floodlight context to use for this message. If null,
594 * a new context will be allocated.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800595 * @throws IOException
Jonathan Hartcb34f382014-08-12 21:11:03 -0700596 *
Saurav Dasfcdad072014-08-13 14:15:21 -0700597 * FIXME: this method and the ChannelHandler disagree on which
598 * messages should be dispatched and which shouldn't
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800599 */
600 @LogMessageDocs({
Ray Milkey269ffb92014-04-03 14:43:30 -0700601 @LogMessageDoc(level = "ERROR",
602 message = "Ignoring PacketIn (Xid = {xid}) because the data" +
603 " field is empty.",
604 explanation = "The switch sent an improperly-formatted PacketIn" +
605 " message",
606 recommendation = LogMessageDoc.CHECK_SWITCH),
607 @LogMessageDoc(level = "WARN",
608 message = "Unhandled OF Message: {} from {}",
609 explanation = "The switch sent a message not handled by " +
610 "the controller")
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800611 })
Saurav Dasfcdad072014-08-13 14:15:21 -0700612 @SuppressWarnings({"fallthrough", "unchecked"})
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800613 protected void handleMessage(IOFSwitch sw, OFMessage m,
Saurav Dasfcdad072014-08-13 14:15:21 -0700614 FloodlightContext bContext)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800615 throws IOException {
616 Ethernet eth = null;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700617 short inport = -1;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800618
619 switch (m.getType()) {
Saurav Dasfcdad072014-08-13 14:15:21 -0700620 case PACKET_IN:
621 OFPacketIn pi = (OFPacketIn) m;
622 // log.info("saw packet in from sw {}", sw.getStringId());
623 if (pi.getData().length <= 0) {
624 log.error("Ignoring PacketIn (Xid = " + pi.getXid() +
625 ") because/* the data field is empty.");
626 return;
627 }
628
629 // get incoming port to store in floodlight context
630 if (sw.getOFVersion() == OFVersion.OF_10) {
631 inport = pi.getInPort().getShortPortNumber();
632 } else if (sw.getOFVersion() == OFVersion.OF_13) {
633 for (MatchField<?> mf : pi.getMatch().getMatchFields()) {
634 if (mf.id == MatchFields.IN_PORT) {
635 inport = pi.getMatch().get((MatchField<OFPort>) mf)
636 .getShortPortNumber();
637 break;
638 }
639 }
640 if (inport == -1) {
641 log.error("Match field for incoming port missing in "
642 + "packet-in from sw {}. Ignoring msg",
643 sw.getStringId());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800644 return;
645 }
Saurav Dasfcdad072014-08-13 14:15:21 -0700646 } else {
647 // should have been taken care of earlier in handshake
648 log.error("OFVersion {} not supported for "
649 + "packet-in from sw {}. Ignoring msg",
650 sw.getOFVersion(), sw.getStringId());
651 return;
652 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700653
Saurav Dasfcdad072014-08-13 14:15:21 -0700654 // decode enclosed ethernet packet to store in floodlight context
655 if (Controller.ALWAYS_DECODE_ETH) {
656 eth = new Ethernet();
657 eth.deserialize(pi.getData(), 0,
658 pi.getData().length);
659 }
660 // fall through to default case...
661
662 /*log.debug("Sw:{} packet-in: {}", sw.getStringId(),
663 String.format("0x%x", eth.getEtherType()));*/
664 if (eth.getEtherType() != (short) EthType.LLDP.getValue())
665 log.trace("Sw:{} packet-in: {}", sw.getStringId(), pi);
666
667 default:
668
669 List<IOFMessageListener> listeners = null;
670
671 if (messageListeners.containsKey(m.getType())) {
672 listeners = messageListeners.get(m.getType()).
673 getOrderedListeners();
674 }
675 FloodlightContext bc = null;
676 if (listeners != null) {
677 // Check if floodlight context is passed from the calling
678 // function, if so use that floodlight context, otherwise
679 // allocate one
680 if (bContext == null) {
681 bc = flcontext_alloc();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700682 } else {
Saurav Dasfcdad072014-08-13 14:15:21 -0700683 bc = bContext;
684 }
685 if (eth != null) {
686 IFloodlightProviderService.bcStore.put(bc,
687 IFloodlightProviderService.CONTEXT_PI_PAYLOAD,
688 eth);
689 }
690 if (inport != -1) {
691 bc.getStorage().put(
692 IFloodlightProviderService.CONTEXT_PI_INPORT,
693 inport);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700694 }
695
Saurav Dasfcdad072014-08-13 14:15:21 -0700696 // Get the starting time (overall and per-component) of
697 // the processing chain for this packet if performance
698 // monitoring is turned on
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800699
Saurav Dasfcdad072014-08-13 14:15:21 -0700700 Command cmd = null;
701 for (IOFMessageListener listener : listeners) {
702 if (listener instanceof IOFSwitchFilter) {
703 if (!((IOFSwitchFilter) listener).isInterested(sw)) {
704 continue;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800705 }
706 }
mininet73e7fb72013-12-03 14:25:53 -0800707
Saurav Dasfcdad072014-08-13 14:15:21 -0700708 cmd = listener.receive(sw, m, bc);
709
710 if (Command.STOP.equals(cmd)) {
711 break;
712 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800713 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700714
Saurav Dasfcdad072014-08-13 14:15:21 -0700715 } else {
716 log.warn("Unhandled OF Message: {} from {}", m, sw);
717 }
718
719 if ((bContext == null) && (bc != null))
720 flcontext_free(bc);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800721 }
722 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700723
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800724 // ***************
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700725 // IFloodlightProviderService
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800726 // ***************
Ray Milkey269ffb92014-04-03 14:43:30 -0700727
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700728 // FIXME: remove this method
729 @Override
Saurav Dasfcdad072014-08-13 14:15:21 -0700730 public Map<Long, IOFSwitch> getSwitches() {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700731 return getMasterSwitches();
732 }
733
734 // FIXME: remove this method
735 public Map<Long, IOFSwitch> getMasterSwitches() {
736 return Collections.unmodifiableMap(activeMasterSwitches);
737 }
738
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700739 @Override
740 public Set<Long> getAllSwitchDpids() {
741 Set<Long> dpids = new HashSet<Long>();
742 dpids.addAll(activeMasterSwitches.keySet());
743 dpids.addAll(activeEqualSwitches.keySet());
744 return dpids;
745 }
746
747 @Override
748 public Set<Long> getAllMasterSwitchDpids() {
749 Set<Long> dpids = new HashSet<Long>();
750 dpids.addAll(activeMasterSwitches.keySet());
751 return dpids;
752 }
753
754 @Override
755 public Set<Long> getAllEqualSwitchDpids() {
756 Set<Long> dpids = new HashSet<Long>();
757 dpids.addAll(activeEqualSwitches.keySet());
758 return dpids;
759 }
760
761 @Override
762 public IOFSwitch getSwitch(long dpid) {
763 IOFSwitch sw = null;
Saurav Dasfcdad072014-08-13 14:15:21 -0700764 if ((sw = activeMasterSwitches.get(dpid)) != null)
765 return sw;
766 if ((sw = activeEqualSwitches.get(dpid)) != null)
767 return sw;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700768 return sw;
769 }
770
771 @Override
772 public IOFSwitch getMasterSwitch(long dpid) {
Saurav Dasfcdad072014-08-13 14:15:21 -0700773 return activeMasterSwitches.get(dpid);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700774 }
775
776 @Override
777 public IOFSwitch getEqualSwitch(long dpid) {
Saurav Dasfcdad072014-08-13 14:15:21 -0700778 return activeEqualSwitches.get(dpid);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700779 }
780
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800781 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700782 public synchronized void addOFMessageListener(OFType type,
Saurav Dasfcdad072014-08-13 14:15:21 -0700783 IOFMessageListener listener) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700784 ListenerDispatcher<OFType, IOFMessageListener> ldd =
785 messageListeners.get(type);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800786 if (ldd == null) {
787 ldd = new ListenerDispatcher<OFType, IOFMessageListener>();
788 messageListeners.put(type, ldd);
789 }
790 ldd.addListener(type, listener);
791 }
792
793 @Override
794 public synchronized void removeOFMessageListener(OFType type,
Saurav Dasfcdad072014-08-13 14:15:21 -0700795 IOFMessageListener listener) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700796 ListenerDispatcher<OFType, IOFMessageListener> ldd =
797 messageListeners.get(type);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800798 if (ldd != null) {
799 ldd.removeListener(listener);
800 }
801 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700802
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700803 public void removeOFMessageListeners(OFType type) {
804 messageListeners.remove(type);
805 }
806
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800807 private void logListeners() {
Saurav Dasfcdad072014-08-13 14:15:21 -0700808 for (Map.Entry<OFType, ListenerDispatcher<OFType, IOFMessageListener>> entry : messageListeners
809 .entrySet()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700810
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800811 OFType type = entry.getKey();
Ray Milkey269ffb92014-04-03 14:43:30 -0700812 ListenerDispatcher<OFType, IOFMessageListener> ldd =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800813 entry.getValue();
Ray Milkey269ffb92014-04-03 14:43:30 -0700814
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800815 StringBuffer sb = new StringBuffer();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700816 sb.append("OFMessageListeners for ");
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800817 sb.append(type);
818 sb.append(": ");
819 for (IOFMessageListener l : ldd.getOrderedListeners()) {
820 sb.append(l.getName());
821 sb.append(",");
822 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700823 log.debug(sb.toString());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800824 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700825 StringBuffer sl = new StringBuffer();
826 sl.append("SwitchUpdate Listeners: ");
827 for (IOFSwitchListener swlistener : switchListeners) {
828 sl.append(swlistener.getName());
829 sl.append(",");
830 }
831 log.debug(sl.toString());
Ray Milkey269ffb92014-04-03 14:43:30 -0700832
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800833 }
834
835 @Override
836 public void addOFSwitchListener(IOFSwitchListener listener) {
837 this.switchListeners.add(listener);
838 }
839
840 @Override
841 public void removeOFSwitchListener(IOFSwitchListener listener) {
842 this.switchListeners.remove(listener);
843 }
844
845 @Override
846 public Map<OFType, List<IOFMessageListener>> getListeners() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700847 Map<OFType, List<IOFMessageListener>> lers =
848 new HashMap<OFType, List<IOFMessageListener>>();
Saurav Dasfcdad072014-08-13 14:15:21 -0700849 for (Entry<OFType, ListenerDispatcher<OFType, IOFMessageListener>> e : messageListeners
850 .entrySet()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800851 lers.put(e.getKey(), e.getValue().getOrderedListeners());
852 }
853 return Collections.unmodifiableMap(lers);
854 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700855
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700856 /*@Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800857 @LogMessageDocs({
Ray Milkey269ffb92014-04-03 14:43:30 -0700858 @LogMessageDoc(message = "Failed to inject OFMessage {message} onto " +
859 "a null switch",
860 explanation = "Failed to process a message because the switch " +
861 " is no longer connected."),
862 @LogMessageDoc(level = "ERROR",
863 message = "Error reinjecting OFMessage on switch {switch}",
864 explanation = "An I/O error occured while attempting to " +
865 "process an OpenFlow message",
866 recommendation = LogMessageDoc.CHECK_SWITCH)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800867 })
868 public boolean injectOfMessage(IOFSwitch sw, OFMessage msg,
869 FloodlightContext bc) {
870 if (sw == null) {
871 log.info("Failed to inject OFMessage {} onto a null switch", msg);
872 return false;
873 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700874
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800875 // FIXME: Do we need to be able to inject messages to switches
876 // where we're the slave controller (i.e. they're connected but
877 // not active)?
878 // FIXME: Don't we need synchronization logic here so we're holding
879 // the listener read lock when we call handleMessage? After some
880 // discussions it sounds like the right thing to do here would be to
881 // inject the message as a netty upstream channel event so it goes
882 // through the normal netty event processing, including being
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700883 // handled
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800884 if (!activeSwitches.containsKey(sw.getId())) return false;
Ray Milkey269ffb92014-04-03 14:43:30 -0700885
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800886 try {
887 // Pass Floodlight context to the handleMessages()
888 handleMessage(sw, msg, bc);
889 } catch (IOException e) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700890 log.error("Error reinjecting OFMessage on switch {}",
891 HexString.toHexString(sw.getId()));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800892 return false;
893 }
894 return true;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700895 }*/
896
Saurav Dasfcdad072014-08-13 14:15:21 -0700897 // @Override
898 // public boolean injectOfMessage(IOFSwitch sw, OFMessage msg) {
899 // // call the overloaded version with floodlight context set to null
900 // return injectOfMessage(sw, msg, null);
901 // }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700902
Saurav Dasfcdad072014-08-13 14:15:21 -0700903 // @Override
904 // public void handleOutgoingMessage(IOFSwitch sw, OFMessage m,
905 // FloodlightContext bc) {
906 //
907 // List<IOFMessageListener> listeners = null;
908 // if (messageListeners.containsKey(m.getType())) {
909 // listeners =
910 // messageListeners.get(m.getType()).getOrderedListeners();
911 // }
912 //
913 // if (listeners != null) {
914 // for (IOFMessageListener listener : listeners) {
915 // if (listener instanceof IOFSwitchFilter) {
916 // if (!((IOFSwitchFilter) listener).isInterested(sw)) {
917 // continue;
918 // }
919 // }
920 // if (Command.STOP.equals(listener.receive(sw, m, bc))) {
921 // break;
922 // }
923 // }
924 // }
925 // }
Jonathan Harta213bce2014-08-11 15:44:07 -0700926
927 /**
928 * Gets an OpenFlow message factory for version 1.0.
Jonathan Hartcb34f382014-08-12 21:11:03 -0700929 *
Jonathan Harta213bce2014-08-11 15:44:07 -0700930 * @return an OpenFlow 1.0 message factory
931 */
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700932 public OFFactory getOFMessageFactory_10() {
933 return factory10;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800934 }
935
Jonathan Harta213bce2014-08-11 15:44:07 -0700936 /**
937 * Gets an OpenFlow message factory for version 1.3.
Jonathan Hartcb34f382014-08-12 21:11:03 -0700938 *
Jonathan Harta213bce2014-08-11 15:44:07 -0700939 * @return an OpenFlow 1.3 message factory
940 */
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700941 public OFFactory getOFMessageFactory_13() {
942 return factory13;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800943 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700944
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800945 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700946 public void publishUpdate(IUpdate update) {
947 try {
948 this.updates.put(update);
949 } catch (InterruptedException e) {
950 log.error("Failure adding update to queue", e);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800951 }
952 }
953
954 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700955 public Map<String, String> getControllerNodeIPs() {
956 // We return a copy of the mapping so we can guarantee that
957 // the mapping return is the same as one that will be (or was)
958 // dispatched to IHAListeners
959 HashMap<String, String> retval = new HashMap<String, String>();
960 synchronized (controllerNodeIPsCache) {
961 retval.putAll(controllerNodeIPsCache);
962 }
963 return retval;
964 }
965
966 @Override
967 public long getSystemStartTime() {
968 return (this.systemStartTime);
969 }
970
971 @Override
972 public void setAlwaysClearFlowsOnSwAdd(boolean value) {
973 this.alwaysClearFlowsOnSwAdd = value;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800974 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700975
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800976 @Override
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -0700977 public OnosInstanceId getOnosInstanceId() {
978 return onosInstanceId;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800979 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700980
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700981 /**
982 * FOR TESTING ONLY. Dispatch all updates in the update queue until queue is
983 * empty
984 */
985 void processUpdateQueueForTesting() {
986 while (!updates.isEmpty()) {
987 IUpdate update = updates.poll();
988 if (update != null)
989 update.dispatch();
990 }
991 }
992
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800993 // **************
994 // Initialization
995 // **************
996
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700997 // XXX S This should probably go away OR it should be edited to handle
998 // controller roles per switch! Then it could be a way to
999 // deterministically configure a switch to a MASTER controller instance
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001000 /**
Saurav Dasfcdad072014-08-13 14:15:21 -07001001 * Sets the initial role based on properties in the config params. It looks
1002 * for two different properties. If the "role" property is specified then
1003 * the value should be either "EQUAL", "MASTER", or "SLAVE" and the role of
1004 * the controller is set to the specified value. If the "role" property is
1005 * not specified then it looks next for the "role.path" property. In this
1006 * case the value should be the path to a property file in the file system
1007 * that contains a property called "floodlight.role" which can be one of the
1008 * values listed above for the "role" property. The idea behind the
1009 * "role.path" mechanism is that you have some separate heartbeat and master
1010 * controller election algorithm that determines the role of the controller.
1011 * When a role transition happens, it updates the current role in the file
1012 * specified by the "role.path" file. Then if floodlight restarts for some
1013 * reason it can get the correct current role of the controller from the
1014 * file.
Jonathan Hartcb34f382014-08-12 21:11:03 -07001015 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001016 * @param configParams The config params for the FloodlightProvider service
Saurav Dasfcdad072014-08-13 14:15:21 -07001017 * @return A valid role if role information is specified in the config
1018 * params, otherwise null
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001019 */
1020 @LogMessageDocs({
Ray Milkey269ffb92014-04-03 14:43:30 -07001021 @LogMessageDoc(message = "Controller role set to {role}",
1022 explanation = "Setting the initial HA role to "),
1023 @LogMessageDoc(level = "ERROR",
1024 message = "Invalid current role value: {role}",
1025 explanation = "An invalid HA role value was read from the " +
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001026 "properties file",
Ray Milkey269ffb92014-04-03 14:43:30 -07001027 recommendation = LogMessageDoc.CHECK_CONTROLLER)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001028 })
1029 protected Role getInitialRole(Map<String, String> configParams) {
1030 Role role = null;
1031 String roleString = configParams.get("role");
Praseed Balakrishnan59469e92014-06-20 11:55:13 -07001032 FileInputStream fs = null;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001033 if (roleString == null) {
1034 String rolePath = configParams.get("rolepath");
1035 if (rolePath != null) {
1036 Properties properties = new Properties();
1037 try {
Praseed Balakrishnan59469e92014-06-20 11:55:13 -07001038 fs = new FileInputStream(rolePath);
1039 properties.load(fs);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001040 roleString = properties.getProperty("floodlight.role");
Ray Milkey269ffb92014-04-03 14:43:30 -07001041 } catch (IOException exc) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001042 // Don't treat it as an error if the file specified by the
1043 // rolepath property doesn't exist. This lets us enable the
1044 // HA mechanism by just creating/setting the floodlight.role
1045 // property in that file without having to modify the
1046 // floodlight properties.
Praseed Balakrishnan59469e92014-06-20 11:55:13 -07001047 } finally {
1048 if (fs != null) {
1049 try {
1050 fs.close();
1051 } catch (IOException e) {
1052 log.error("Exception while closing resource ", e);
1053 }
1054 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001055 }
1056 }
1057 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001058
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001059 if (roleString != null) {
1060 // Canonicalize the string to the form used for the enum constants
1061 roleString = roleString.trim().toUpperCase();
1062 try {
1063 role = Role.valueOf(roleString);
Ray Milkey269ffb92014-04-03 14:43:30 -07001064 } catch (IllegalArgumentException exc) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001065 log.error("Invalid current role value: {}", roleString);
1066 }
1067 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001068
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001069 log.info("Controller role set to {}", role);
Ray Milkey269ffb92014-04-03 14:43:30 -07001070
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001071 return role;
1072 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001073
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001074 /**
1075 * Tell controller that we're ready to accept switches loop
Jonathan Hartcb34f382014-08-12 21:11:03 -07001076 *
Ray Milkey269ffb92014-04-03 14:43:30 -07001077 * @throws IOException
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001078 */
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001079 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001080 @LogMessageDocs({
Ray Milkey269ffb92014-04-03 14:43:30 -07001081 @LogMessageDoc(message = "Listening for switch connections on {address}",
1082 explanation = "The controller is ready and listening for new" +
1083 " switch connections"),
1084 @LogMessageDoc(message = "Storage exception in controller " +
1085 "updates loop; terminating process",
1086 explanation = ERROR_DATABASE,
1087 recommendation = LogMessageDoc.CHECK_CONTROLLER),
1088 @LogMessageDoc(level = "ERROR",
1089 message = "Exception in controller updates loop",
1090 explanation = "Failed to dispatch controller event",
1091 recommendation = LogMessageDoc.GENERIC_ACTION)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001092 })
1093 public void run() {
1094 if (log.isDebugEnabled()) {
1095 logListeners();
1096 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001097
1098 try {
1099 final ServerBootstrap bootstrap = createServerBootStrap();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001100
1101 bootstrap.setOption("reuseAddr", true);
1102 bootstrap.setOption("child.keepAlive", true);
1103 bootstrap.setOption("child.tcpNoDelay", true);
1104 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
1105
Ray Milkey269ffb92014-04-03 14:43:30 -07001106 ChannelPipelineFactory pfact =
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001107 new OpenflowPipelineFactory(this, null);
1108 bootstrap.setPipelineFactory(pfact);
1109 InetSocketAddress sa = new InetSocketAddress(openFlowPort);
1110 final ChannelGroup cg = new DefaultChannelGroup();
1111 cg.add(bootstrap.bind(sa));
Ray Milkey269ffb92014-04-03 14:43:30 -07001112
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001113 log.info("Listening for switch connections on {}", sa);
1114 } catch (Exception e) {
1115 throw new RuntimeException(e);
1116 }
1117
1118 // main loop
1119 while (true) {
1120 try {
1121 IUpdate update = updates.take();
1122 update.dispatch();
1123 } catch (InterruptedException e) {
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001124 log.error("Received interrupted exception in updates loop;" +
Saurav Dasfcdad072014-08-13 14:15:21 -07001125 "terminating process");
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001126 terminate();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001127 } catch (Exception e) {
1128 log.error("Exception in controller updates loop", e);
1129 }
1130 }
1131 }
1132
1133 private ServerBootstrap createServerBootStrap() {
1134 if (workerThreads == 0) {
1135 return new ServerBootstrap(
1136 new NioServerSocketChannelFactory(
1137 Executors.newCachedThreadPool(),
1138 Executors.newCachedThreadPool()));
1139 } else {
1140 return new ServerBootstrap(
1141 new NioServerSocketChannelFactory(
1142 Executors.newCachedThreadPool(),
1143 Executors.newCachedThreadPool(), workerThreads));
1144 }
1145 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001146
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001147 public void setConfigParams(Map<String, String> configParams) {
1148 String ofPort = configParams.get("openflowport");
1149 if (ofPort != null) {
1150 this.openFlowPort = Integer.parseInt(ofPort);
1151 }
1152 log.debug("OpenFlow port set to {}", this.openFlowPort);
1153 String threads = configParams.get("workerthreads");
1154 if (threads != null) {
1155 this.workerThreads = Integer.parseInt(threads);
1156 }
1157 log.debug("Number of worker threads set to {}", this.workerThreads);
1158 String controllerId = configParams.get("controllerid");
1159 if (controllerId != null) {
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -07001160 this.onosInstanceId = new OnosInstanceId(controllerId);
Ray Milkey269ffb92014-04-03 14:43:30 -07001161 } else {
Saurav Dasfcdad072014-08-13 14:15:21 -07001162 // Try to get the hostname of the machine and use that for
1163 // controller ID
Ray Milkey269ffb92014-04-03 14:43:30 -07001164 try {
1165 String hostname = java.net.InetAddress.getLocalHost().getHostName();
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -07001166 this.onosInstanceId = new OnosInstanceId(hostname);
Ray Milkey269ffb92014-04-03 14:43:30 -07001167 } catch (UnknownHostException e) {
1168 // Can't get hostname, we'll just use the default
1169 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001170 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001171
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -07001172 log.debug("ControllerId set to {}", this.onosInstanceId);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001173 }
1174
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001175 /**
1176 * Initialize internal data structures
1177 */
1178 public void init(Map<String, String> configParams) {
1179 // These data structures are initialized here because other
1180 // module's startUp() might be called before ours
1181 this.messageListeners =
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001182 new ConcurrentHashMap<OFType, ListenerDispatcher<OFType,
Saurav Dasfcdad072014-08-13 14:15:21 -07001183 IOFMessageListener>>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001184 this.switchListeners = new CopyOnWriteArraySet<IOFSwitchListener>();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001185 this.activeMasterSwitches = new ConcurrentHashMap<Long, IOFSwitch>();
1186 this.activeEqualSwitches = new ConcurrentHashMap<Long, IOFSwitch>();
1187 this.connectedSwitches = new ConcurrentHashMap<Long, OFChannelHandler>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001188 this.controllerNodeIPsCache = new HashMap<String, String>();
1189 this.updates = new LinkedBlockingQueue<IUpdate>();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001190
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001191 setConfigParams(configParams);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001192 this.systemStartTime = System.currentTimeMillis();
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001193 this.counters = new Counters();
1194 this.multiCacheLock = new Object();
1195
1196 String option = configParams.get("flushSwitchesOnReconnect");
1197 if (option != null && option.equalsIgnoreCase("true")) {
1198 this.setAlwaysClearFlowsOnSwActivate(true);
1199 log.info("Flush switches on reconnect -- Enabled.");
1200 } else {
1201 this.setAlwaysClearFlowsOnSwActivate(false);
1202 log.info("Flush switches on reconnect -- Disabled");
1203 }
Jonathan Hartcb34f382014-08-12 21:11:03 -07001204
1205 option = configParams.get("cpqdUsePipeline13");
1206 if (option != null && option.equalsIgnoreCase("true")) {
1207 DriverManager.setConfigForCpqd(true);
1208 log.info("Using OF1.3 pipeline for the CPqD software switch");
1209 } else {
1210 log.info("Using OF1.0 pipeline for the CPqD software switch");
1211 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001212 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001213
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001214 /**
1215 * Startup all of the controller's components
Jonathan Hartcb34f382014-08-12 21:11:03 -07001216 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001217 * @throws FloodlightModuleException
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001218 */
Ray Milkey269ffb92014-04-03 14:43:30 -07001219 @LogMessageDoc(message = "Waiting for storage source",
1220 explanation = "The system database is not yet ready",
1221 recommendation = "If this message persists, this indicates " +
1222 "that the system database has failed to start. " +
1223 LogMessageDoc.CHECK_CONTROLLER)
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001224 public void startupComponents() throws FloodlightModuleException {
Ray Milkey269ffb92014-04-03 14:43:30 -07001225 try {
Pavlin Radoslavov53b208a2014-07-28 13:16:11 -07001226 registryService.registerController(onosInstanceId.toString());
Ray Milkey269ffb92014-04-03 14:43:30 -07001227 } catch (RegistryException e) {
1228 log.warn("Registry service error: {}", e.getMessage());
1229 }
1230
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001231 // Add our REST API
1232 restApi.addRestletRoutable(new CoreWebRoutable());
Ray Milkey269ffb92014-04-03 14:43:30 -07001233
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001234 // Startup load monitoring
1235 if (overload_drop) {
1236 this.loadmonitor.startMonitoring(
Saurav Dasfcdad072014-08-13 14:15:21 -07001237 this.threadPool.getScheduledExecutor());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001238 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001239
1240 // register counters and events
1241 try {
1242 this.counters.createCounters(debugCounters);
1243 } catch (CounterException e) {
1244 throw new FloodlightModuleException(e.getMessage());
1245 }
1246 registerControllerDebugEvents();
1247 }
1248
1249 // **************
1250 // debugCounter registrations
1251 // **************
1252
1253 public static class Counters {
1254 public static final String prefix = "controller";
1255 public IDebugCounter setRoleEqual;
1256 public IDebugCounter setSameRole;
1257 public IDebugCounter setRoleMaster;
1258 public IDebugCounter remoteStoreNotification;
1259 public IDebugCounter invalidPortsChanged;
1260 public IDebugCounter invalidSwitchActivatedWhileSlave;
1261 public IDebugCounter invalidStoreEventWhileMaster;
1262 public IDebugCounter switchDisconnectedWhileSlave;
1263 public IDebugCounter switchActivated;
1264 public IDebugCounter errorSameSwitchReactivated; // err
1265 public IDebugCounter switchWithSameDpidActivated; // warn
Saurav Dasfcdad072014-08-13 14:15:21 -07001266 public IDebugCounter newSwitchActivated; // new switch
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001267 public IDebugCounter syncedSwitchActivated;
1268 public IDebugCounter readyForReconcile;
1269 public IDebugCounter newSwitchFromStore;
1270 public IDebugCounter updatedSwitchFromStore;
1271 public IDebugCounter switchDisconnected;
1272 public IDebugCounter syncedSwitchRemoved;
1273 public IDebugCounter unknownSwitchRemovedFromStore;
1274 public IDebugCounter consolidateStoreRunCount;
1275 public IDebugCounter consolidateStoreInconsistencies;
1276 public IDebugCounter storeSyncError;
1277 public IDebugCounter switchesNotReconnectingToNewMaster;
1278 public IDebugCounter switchPortChanged;
1279 public IDebugCounter switchOtherChange;
1280 public IDebugCounter dispatchMessageWhileSlave;
Saurav Dasfcdad072014-08-13 14:15:21 -07001281 public IDebugCounter dispatchMessage; // does this cnt make sense? more
1282 // specific?? per type? count
1283 // stops?
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001284 public IDebugCounter controllerNodeIpsChanged;
1285 public IDebugCounter messageReceived;
1286 public IDebugCounter messageInputThrottled;
1287 public IDebugCounter switchDisconnectReadTimeout;
1288 public IDebugCounter switchDisconnectHandshakeTimeout;
1289 public IDebugCounter switchDisconnectIOError;
1290 public IDebugCounter switchDisconnectParseError;
1291 public IDebugCounter switchDisconnectSwitchStateException;
1292 public IDebugCounter rejectedExecutionException;
1293 public IDebugCounter switchDisconnectOtherException;
1294 public IDebugCounter switchConnected;
1295 public IDebugCounter unhandledMessage;
1296 public IDebugCounter packetInWhileSwitchIsSlave;
1297 public IDebugCounter epermErrorWhileSwitchIsMaster;
1298 public IDebugCounter roleNotResentBecauseRolePending;
1299 public IDebugCounter roleRequestSent;
1300 public IDebugCounter roleReplyTimeout;
1301 public IDebugCounter roleReplyReceived; // expected RoleReply received
1302 public IDebugCounter roleReplyErrorUnsupported;
1303 public IDebugCounter switchCounterRegistrationFailed;
1304 public IDebugCounter packetParsingError;
1305
1306 void createCounters(IDebugCounterService debugCounters) throws CounterException {
1307 setRoleEqual =
Saurav Dasfcdad072014-08-13 14:15:21 -07001308 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001309 prefix, "set-role-equal",
Saurav Dasfcdad072014-08-13 14:15:21 -07001310 "Controller received a role request with role of " +
1311 "EQUAL which is unusual",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001312 CounterType.ALWAYS_COUNT);
1313 setSameRole =
Saurav Dasfcdad072014-08-13 14:15:21 -07001314 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001315 prefix, "set-same-role",
1316 "Controller received a role request for the same " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001317 "role the controller already had",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001318 CounterType.ALWAYS_COUNT,
1319 IDebugCounterService.CTR_MDATA_WARN);
1320
1321 setRoleMaster =
Saurav Dasfcdad072014-08-13 14:15:21 -07001322 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001323 prefix, "set-role-master",
1324 "Controller received a role request with role of " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001325 "MASTER. This counter can be at most 1.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001326 CounterType.ALWAYS_COUNT);
1327
1328 remoteStoreNotification =
Saurav Dasfcdad072014-08-13 14:15:21 -07001329 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001330 prefix, "remote-store-notification",
1331 "Received a notification from the sync service " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001332 "indicating that switch information has changed",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001333 CounterType.ALWAYS_COUNT);
1334
1335 invalidPortsChanged =
Saurav Dasfcdad072014-08-13 14:15:21 -07001336 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001337 prefix, "invalid-ports-changed",
1338 "Received an unexpected ports changed " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001339 "notification while the controller was in " +
1340 "SLAVE role.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001341 CounterType.ALWAYS_COUNT,
1342 IDebugCounterService.CTR_MDATA_WARN);
1343
1344 invalidSwitchActivatedWhileSlave =
Saurav Dasfcdad072014-08-13 14:15:21 -07001345 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001346 prefix, "invalid-switch-activated-while-slave",
1347 "Received an unexpected switchActivated " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001348 "notification while the controller was in " +
1349 "SLAVE role.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001350 CounterType.ALWAYS_COUNT,
1351 IDebugCounterService.CTR_MDATA_WARN);
1352
1353 invalidStoreEventWhileMaster =
Saurav Dasfcdad072014-08-13 14:15:21 -07001354 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001355 prefix, "invalid-store-event-while-master",
1356 "Received an unexpected notification from " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001357 "the sync store while the controller was in " +
1358 "MASTER role.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001359 CounterType.ALWAYS_COUNT,
1360 IDebugCounterService.CTR_MDATA_WARN);
1361
1362 switchDisconnectedWhileSlave =
Saurav Dasfcdad072014-08-13 14:15:21 -07001363 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001364 prefix, "switch-disconnected-while-slave",
1365 "A switch disconnected and the controller was " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001366 "in SLAVE role.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001367 CounterType.ALWAYS_COUNT,
1368 IDebugCounterService.CTR_MDATA_WARN);
1369
1370 switchActivated =
Saurav Dasfcdad072014-08-13 14:15:21 -07001371 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001372 prefix, "switch-activated",
1373 "A switch connected to this controller is now " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001374 "in MASTER role",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001375 CounterType.ALWAYS_COUNT);
1376
1377 errorSameSwitchReactivated = // err
Saurav Dasfcdad072014-08-13 14:15:21 -07001378 debugCounters.registerCounter(
1379 prefix, "error-same-switch-reactivated",
1380 "A switch that was already in active state " +
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001381 "was activated again. This indicates a " +
1382 "controller defect",
Saurav Dasfcdad072014-08-13 14:15:21 -07001383 CounterType.ALWAYS_COUNT,
1384 IDebugCounterService.CTR_MDATA_ERROR);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001385
1386 switchWithSameDpidActivated = // warn
Saurav Dasfcdad072014-08-13 14:15:21 -07001387 debugCounters.registerCounter(
1388 prefix, "switch-with-same-dpid-activated",
1389 "A switch with the same DPID as another switch " +
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001390 "connected to the controller. This can be " +
1391 "caused by multiple switches configured with " +
1392 "the same DPID or by a switch reconnecting very " +
1393 "quickly.",
Saurav Dasfcdad072014-08-13 14:15:21 -07001394 CounterType.COUNT_ON_DEMAND,
1395 IDebugCounterService.CTR_MDATA_WARN);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001396
Saurav Dasfcdad072014-08-13 14:15:21 -07001397 newSwitchActivated = // new switch
1398 debugCounters.registerCounter(
1399 prefix, "new-switch-activated",
1400 "A new switch has completed the handshake as " +
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001401 "MASTER. The switch was not known to any other " +
1402 "controller in the cluster",
Saurav Dasfcdad072014-08-13 14:15:21 -07001403 CounterType.ALWAYS_COUNT);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001404 syncedSwitchActivated =
Saurav Dasfcdad072014-08-13 14:15:21 -07001405 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001406 prefix, "synced-switch-activated",
1407 "A switch has completed the handshake as " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001408 "MASTER. The switch was known to another " +
1409 "controller in the cluster",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001410 CounterType.ALWAYS_COUNT);
1411
1412 readyForReconcile =
Saurav Dasfcdad072014-08-13 14:15:21 -07001413 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001414 prefix, "ready-for-reconcile",
1415 "Controller is ready for flow reconciliation " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001416 "after Slave to Master transition. Either all " +
1417 "previously known switches are now active " +
1418 "or they have timed out and have been removed." +
1419 "This counter will be 0 or 1.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001420 CounterType.ALWAYS_COUNT);
1421
1422 newSwitchFromStore =
Saurav Dasfcdad072014-08-13 14:15:21 -07001423 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001424 prefix, "new-switch-from-store",
1425 "A new switch has connected to another " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001426 "another controller in the cluster. This " +
1427 "controller instance has received a sync store " +
1428 "notification for it.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001429 CounterType.ALWAYS_COUNT);
1430
1431 updatedSwitchFromStore =
Saurav Dasfcdad072014-08-13 14:15:21 -07001432 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001433 prefix, "updated-switch-from-store",
1434 "Information about a switch connected to " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001435 "another controller instance was updated in " +
1436 "the sync store. This controller instance has " +
1437 "received a notification for it",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001438 CounterType.ALWAYS_COUNT);
1439
1440 switchDisconnected =
Saurav Dasfcdad072014-08-13 14:15:21 -07001441 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001442 prefix, "switch-disconnected",
1443 "FIXME: switch has disconnected",
1444 CounterType.ALWAYS_COUNT);
1445
1446 syncedSwitchRemoved =
Saurav Dasfcdad072014-08-13 14:15:21 -07001447 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001448 prefix, "synced-switch-removed",
1449 "A switch connected to another controller " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001450 "instance has disconnected from the controller " +
1451 "cluster. This controller instance has " +
1452 "received a notification for it",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001453 CounterType.ALWAYS_COUNT);
1454
1455 unknownSwitchRemovedFromStore =
Saurav Dasfcdad072014-08-13 14:15:21 -07001456 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001457 prefix, "unknown-switch-removed-from-store",
1458 "This controller instances has received a sync " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001459 "store notification that a switch has " +
1460 "disconnected but this controller instance " +
1461 "did not have the any information about the " +
1462 "switch", // might be less than warning
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001463 CounterType.ALWAYS_COUNT,
1464 IDebugCounterService.CTR_MDATA_WARN);
1465
1466 consolidateStoreRunCount =
Saurav Dasfcdad072014-08-13 14:15:21 -07001467 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001468 prefix, "consolidate-store-run-count",
1469 "This controller has transitioned from SLAVE " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001470 "to MASTER and waited for switches to reconnect. " +
1471 "The controller has finished waiting and has " +
1472 "reconciled switch entries in the sync store " +
1473 "with live state",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001474 CounterType.ALWAYS_COUNT);
1475
1476 consolidateStoreInconsistencies =
1477 debugCounters.registerCounter(
Saurav Dasfcdad072014-08-13 14:15:21 -07001478 prefix, "consolidate-store-inconsistencies",
1479 "During switch sync store consolidation: " +
1480 "Number of switches that were in the store " +
1481 "but not otherwise known plus number of " +
1482 "switches that were in the store previously " +
1483 "but are now missing plus number of " +
1484 "connected switches that were absent from " +
1485 "the store although this controller has " +
1486 "written them. A non-zero count " +
1487 "indicates a brief split-brain dual MASTER " +
1488 "situation during fail-over",
1489 CounterType.ALWAYS_COUNT);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001490
1491 storeSyncError =
Saurav Dasfcdad072014-08-13 14:15:21 -07001492 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001493 prefix, "store-sync-error",
1494 "Number of times a sync store operation failed " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001495 "due to a store sync exception or an entry in " +
1496 "in the store had invalid data.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001497 CounterType.ALWAYS_COUNT,
1498 IDebugCounterService.CTR_MDATA_ERROR);
1499
1500 switchesNotReconnectingToNewMaster =
Saurav Dasfcdad072014-08-13 14:15:21 -07001501 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001502 prefix, "switches-not-reconnecting-to-new-master",
1503 "Switches that were connected to another " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001504 "controller instance in the cluster but that " +
1505 "did not reconnect to this controller after it " +
1506 "transitioned to MASTER", // might be less
1507 // than warning
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001508 CounterType.ALWAYS_COUNT);
1509
1510 switchPortChanged =
Saurav Dasfcdad072014-08-13 14:15:21 -07001511 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001512 prefix, "switch-port-changed",
1513 "Number of times switch ports have changed",
1514 CounterType.ALWAYS_COUNT);
1515 switchOtherChange =
Saurav Dasfcdad072014-08-13 14:15:21 -07001516 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001517 prefix, "switch-other-change",
1518 "Number of times other information of a switch " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001519 "has changed.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001520 CounterType.ALWAYS_COUNT);
1521
1522 dispatchMessageWhileSlave =
Saurav Dasfcdad072014-08-13 14:15:21 -07001523 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001524 prefix, "dispatch-message-while-slave",
1525 "Number of times an OF message was received " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001526 "and supposed to be dispatched but the " +
1527 "controller was in SLAVE role and the message " +
1528 "was not dispatched",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001529 CounterType.ALWAYS_COUNT);
1530
Saurav Dasfcdad072014-08-13 14:15:21 -07001531 dispatchMessage = // does this cnt make sense? more specific?? per
1532 // type? count stops?
1533 debugCounters.registerCounter(
1534 prefix, "dispatch-message",
1535 "Number of times an OF message was dispatched " +
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001536 "to registered modules",
Saurav Dasfcdad072014-08-13 14:15:21 -07001537 CounterType.ALWAYS_COUNT);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001538
1539 controllerNodeIpsChanged =
Saurav Dasfcdad072014-08-13 14:15:21 -07001540 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001541 prefix, "controller-nodes-ips-changed",
1542 "IP addresses of controller nodes have changed",
1543 CounterType.ALWAYS_COUNT);
1544
Saurav Dasfcdad072014-08-13 14:15:21 -07001545 // ------------------------
1546 // channel handler counters. Factor them out ??
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001547 messageReceived =
Saurav Dasfcdad072014-08-13 14:15:21 -07001548 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001549 prefix, "message-received",
1550 "Number of OpenFlow messages received. Some of " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001551 "these might be throttled",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001552 CounterType.ALWAYS_COUNT);
1553 messageInputThrottled =
Saurav Dasfcdad072014-08-13 14:15:21 -07001554 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001555 prefix, "message-input-throttled",
1556 "Number of OpenFlow messages that were " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001557 "throttled due to high load from the sender",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001558 CounterType.ALWAYS_COUNT,
1559 IDebugCounterService.CTR_MDATA_WARN);
Saurav Dasfcdad072014-08-13 14:15:21 -07001560 // TODO: more counters in messageReceived ??
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001561
1562 switchDisconnectReadTimeout =
Saurav Dasfcdad072014-08-13 14:15:21 -07001563 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001564 prefix, "switch-disconnect-read-timeout",
1565 "Number of times a switch was disconnected due " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001566 "due the switch failing to send OpenFlow " +
1567 "messages or responding to OpenFlow ECHOs",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001568 CounterType.ALWAYS_COUNT,
1569 IDebugCounterService.CTR_MDATA_ERROR);
1570 switchDisconnectHandshakeTimeout =
Saurav Dasfcdad072014-08-13 14:15:21 -07001571 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001572 prefix, "switch-disconnect-handshake-timeout",
1573 "Number of times a switch was disconnected " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001574 "because it failed to complete the handshake " +
1575 "in time.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001576 CounterType.ALWAYS_COUNT,
1577 IDebugCounterService.CTR_MDATA_ERROR);
1578 switchDisconnectIOError =
Saurav Dasfcdad072014-08-13 14:15:21 -07001579 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001580 prefix, "switch-disconnect-io-error",
1581 "Number of times a switch was disconnected " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001582 "due to IO errors on the switch connection.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001583 CounterType.ALWAYS_COUNT,
1584 IDebugCounterService.CTR_MDATA_ERROR);
1585 switchDisconnectParseError =
Saurav Dasfcdad072014-08-13 14:15:21 -07001586 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001587 prefix, "switch-disconnect-parse-error",
Saurav Dasfcdad072014-08-13 14:15:21 -07001588 "Number of times a switch was disconnected " +
1589 "because it sent an invalid packet that could " +
1590 "not be parsed",
1591 CounterType.ALWAYS_COUNT,
1592 IDebugCounterService.CTR_MDATA_ERROR);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001593
1594 switchDisconnectSwitchStateException =
Saurav Dasfcdad072014-08-13 14:15:21 -07001595 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001596 prefix, "switch-disconnect-switch-state-exception",
1597 "Number of times a switch was disconnected " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001598 "because it sent messages that were invalid " +
1599 "given the switch connection's state.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001600 CounterType.ALWAYS_COUNT,
1601 IDebugCounterService.CTR_MDATA_ERROR);
1602 rejectedExecutionException =
Saurav Dasfcdad072014-08-13 14:15:21 -07001603 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001604 prefix, "rejected-execution-exception",
1605 "TODO",
1606 CounterType.ALWAYS_COUNT,
1607 IDebugCounterService.CTR_MDATA_ERROR);
1608
1609 switchDisconnectOtherException =
Saurav Dasfcdad072014-08-13 14:15:21 -07001610 debugCounters.registerCounter(
1611 prefix, "switch-disconnect-other-exception",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001612 "Number of times a switch was disconnected " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001613 "due to an exceptional situation not covered " +
1614 "by other counters",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001615 CounterType.ALWAYS_COUNT,
1616 IDebugCounterService.CTR_MDATA_ERROR);
1617
1618 switchConnected =
Saurav Dasfcdad072014-08-13 14:15:21 -07001619 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001620 prefix, "switch-connected",
1621 "Number of times a new switch connection was " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001622 "established",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001623 CounterType.ALWAYS_COUNT);
1624
1625 unhandledMessage =
Saurav Dasfcdad072014-08-13 14:15:21 -07001626 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001627 prefix, "unhandled-message",
1628 "Number of times an OpenFlow message was " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001629 "received that the controller ignored because " +
1630 "it was inapproriate given the switch " +
1631 "connection's state.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001632 CounterType.ALWAYS_COUNT,
1633 IDebugCounterService.CTR_MDATA_WARN);
Saurav Dasfcdad072014-08-13 14:15:21 -07001634 // might be less than warning
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001635
1636 packetInWhileSwitchIsSlave =
Saurav Dasfcdad072014-08-13 14:15:21 -07001637 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001638 prefix, "packet-in-while-switch-is-slave",
1639 "Number of times a packet in was received " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001640 "from a switch that was in SLAVE role. " +
1641 "Possibly inidicates inconsistent roles.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001642 CounterType.ALWAYS_COUNT);
1643 epermErrorWhileSwitchIsMaster =
Saurav Dasfcdad072014-08-13 14:15:21 -07001644 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001645 prefix, "eperm-error-while-switch-is-master",
1646 "Number of times a permission error was " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001647 "received while the switch was in MASTER role. " +
1648 "Possibly inidicates inconsistent roles.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001649 CounterType.ALWAYS_COUNT,
1650 IDebugCounterService.CTR_MDATA_WARN);
1651
1652 roleNotResentBecauseRolePending =
Saurav Dasfcdad072014-08-13 14:15:21 -07001653 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001654 prefix, "role-not-resent-because-role-pending",
1655 "The controller tried to reestablish a role " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001656 "with a switch but did not do so because a " +
1657 "previous role request was still pending",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001658 CounterType.ALWAYS_COUNT);
1659 roleRequestSent =
Saurav Dasfcdad072014-08-13 14:15:21 -07001660 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001661 prefix, "role-request-sent",
1662 "Number of times the controller sent a role " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001663 "request to a switch.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001664 CounterType.ALWAYS_COUNT);
1665 roleReplyTimeout =
Saurav Dasfcdad072014-08-13 14:15:21 -07001666 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001667 prefix, "role-reply-timeout",
1668 "Number of times a role request message did not " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001669 "receive the expected reply from a switch",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001670 CounterType.ALWAYS_COUNT,
1671 IDebugCounterService.CTR_MDATA_WARN);
1672
1673 roleReplyReceived = // expected RoleReply received
Saurav Dasfcdad072014-08-13 14:15:21 -07001674 debugCounters.registerCounter(
1675 prefix, "role-reply-received",
1676 "Number of times the controller received the " +
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001677 "expected role reply message from a switch",
Saurav Dasfcdad072014-08-13 14:15:21 -07001678 CounterType.ALWAYS_COUNT);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001679
1680 roleReplyErrorUnsupported =
Saurav Dasfcdad072014-08-13 14:15:21 -07001681 debugCounters.registerCounter(
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001682 prefix, "role-reply-error-unsupported",
1683 "Number of times the controller received an " +
Saurav Dasfcdad072014-08-13 14:15:21 -07001684 "error from a switch in response to a role " +
1685 "request indicating that the switch does not " +
1686 "support roles.",
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001687 CounterType.ALWAYS_COUNT);
1688
1689 switchCounterRegistrationFailed =
1690 debugCounters.registerCounter(prefix,
Saurav Dasfcdad072014-08-13 14:15:21 -07001691 "switch-counter-registration-failed",
1692 "Number of times the controller failed to " +
1693 "register per-switch debug counters",
1694 CounterType.ALWAYS_COUNT,
1695 IDebugCounterService.CTR_MDATA_WARN);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001696
1697 packetParsingError =
1698 debugCounters.registerCounter(prefix,
Saurav Dasfcdad072014-08-13 14:15:21 -07001699 "packet-parsing-error",
1700 "Number of times the packet parsing " +
1701 "encountered an error",
1702 CounterType.ALWAYS_COUNT,
1703 IDebugCounterService.CTR_MDATA_ERROR);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001704 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001705 }
1706
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001707 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001708 public Counters getCounters() {
1709 return this.counters;
1710 }
1711
1712 // **************
1713 // debugEvent registrations
1714 // **************
1715
1716 private void registerControllerDebugEvents() throws FloodlightModuleException {
1717 if (debugEvents == null) {
1718 debugEvents = new NullDebugEvent();
1719 }
1720 try {
1721 evSwitch = debugEvents.registerEvent(
Saurav Dasfcdad072014-08-13 14:15:21 -07001722 Counters.prefix, "switchevent",
1723 "Switch connected, disconnected or port changed",
1724 EventType.ALWAYS_LOG, SwitchEvent.class, 100);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001725 } catch (MaxEventsRegistered e) {
1726 throw new FloodlightModuleException("Max events registered", e);
1727 }
1728 }
1729
1730 public class SwitchEvent {
1731 @EventColumn(name = "dpid", description = EventFieldType.DPID)
1732 long dpid;
1733
1734 @EventColumn(name = "reason", description = EventFieldType.STRING)
1735 String reason;
1736
1737 public SwitchEvent(long dpid, String reason) {
1738 this.dpid = dpid;
1739 this.reason = reason;
1740 }
1741 }
1742
1743 // **************
1744 // Utility methods
1745 // **************
1746
1747 @Override
1748 public void setAlwaysClearFlowsOnSwActivate(boolean value) {
Saurav Dasfcdad072014-08-13 14:15:21 -07001749 // this.alwaysClearFlowsOnSwActivate = value;
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001750 // XXX S need to be a little more careful about this
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001751 }
1752
1753 @Override
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001754 public Map<String, Long> getMemory() {
1755 Map<String, Long> m = new HashMap<String, Long>();
1756 Runtime runtime = Runtime.getRuntime();
1757 m.put("total", runtime.totalMemory());
1758 m.put("free", runtime.freeMemory());
1759 return m;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001760 }
Ray Milkey269ffb92014-04-03 14:43:30 -07001761
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001762 @Override
1763 public Long getUptime() {
1764 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
1765 return rb.getUptime();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001766 }
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001767
1768 /**
1769 * Forward to the driver-manager to get an IOFSwitch instance.
Jonathan Hartcb34f382014-08-12 21:11:03 -07001770 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001771 * @param desc
1772 * @return
1773 */
1774 protected IOFSwitch getOFSwitchInstance(OFDescStatsReply desc, OFVersion ofv) {
1775 return DriverManager.getOFSwitchImpl(desc, ofv);
1776 }
1777
1778 protected IThreadPoolService getThreadPoolService() {
1779 return this.threadPool;
1780 }
1781
1782 /**
Saurav Dasfcdad072014-08-13 14:15:21 -07001783 * Part of the controller updates framework (see 'run()' method) Use this
1784 * method to add an IUpdate. A thread-pool will serve the update by
1785 * dispatching it to all listeners for that update.
Jonathan Hartcb34f382014-08-12 21:11:03 -07001786 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001787 * @param update
1788 */
Saurav Dasfcdad072014-08-13 14:15:21 -07001789 @LogMessageDoc(level = "WARN",
1790 message = "Failure adding update {} to queue",
1791 explanation = "The controller tried to add an internal notification" +
1792 " to its message queue but the add failed.",
1793 recommendation = LogMessageDoc.REPORT_CONTROLLER_BUG)
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001794 private void addUpdateToQueue(IUpdate update) {
1795 try {
1796 this.updates.put(update);
1797 } catch (InterruptedException e) {
1798 // This should never happen
1799 log.error("Failure adding update {} to queue.", update);
1800 }
1801 }
1802
1803 void flushAll() {
1804 // Flush all flow-mods/packet-out/stats generated from this "train"
1805 OFSwitchImplBase.flush_all();
1806 debugCounters.flushCounters();
1807 debugEvents.flushEvents();
1808 }
1809
1810 /**
1811 * flcontext_free - Free the context to the current thread
Jonathan Hartcb34f382014-08-12 21:11:03 -07001812 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001813 * @param flcontext
1814 */
1815 protected void flcontext_free(FloodlightContext flcontext) {
1816 flcontext.getStorage().clear();
1817 flcontext_cache.get().push(flcontext);
1818 }
1819
1820 @LogMessageDoc(message = "Calling System.exit",
1821 explanation = "The controller is terminating")
1822 private synchronized void terminate() {
1823 log.info("Calling System.exit");
1824 System.exit(1);
1825 }
1826
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001827 // ***************
1828 // Floodlight context related
1829 // ***************
1830
1831 /**
1832 * flcontext_cache - Keep a thread local stack of contexts
1833 */
1834 protected static final ThreadLocal<Stack<FloodlightContext>> flcontext_cache =
1835 new ThreadLocal<Stack<FloodlightContext>>() {
1836 @Override
1837 protected Stack<FloodlightContext> initialValue() {
1838 return new Stack<FloodlightContext>();
1839 }
1840 };
1841
1842 /**
Saurav Dasfcdad072014-08-13 14:15:21 -07001843 * flcontext_alloc - pop a context off the stack, if required create a new
1844 * one
Jonathan Hartcb34f382014-08-12 21:11:03 -07001845 *
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001846 * @return FloodlightContext
1847 */
1848 protected static FloodlightContext flcontext_alloc() {
1849 FloodlightContext flcontext = null;
1850
1851 if (flcontext_cache.get().empty()) {
1852 flcontext = new FloodlightContext();
1853 } else {
1854 flcontext = flcontext_cache.get().pop();
1855 }
1856
1857 return flcontext;
1858 }
1859
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001860}