blob: c3d2b031ddb158bc804dfff3d8988e5a95f7022a [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright 2011, Big Switch Networks, Inc.
3* Originally created by David Erickson, Stanford University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package net.floodlightcontroller.core.internal;
19
20import java.io.FileInputStream;
21import java.io.IOException;
22import java.net.InetAddress;
23import java.net.InetSocketAddress;
24import java.net.SocketAddress;
Jonathan Hartd10008d2013-02-23 17:04:08 -080025import java.net.UnknownHostException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026import java.nio.channels.ClosedChannelException;
Jonathan Hartd10008d2013-02-23 17:04:08 -080027import java.util.ArrayList;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028import java.util.Collection;
29import java.util.Collections;
30import java.util.Date;
31import java.util.HashMap;
32import java.util.HashSet;
33import java.util.Iterator;
34import java.util.LinkedHashMap;
35import java.util.List;
36import java.util.Map;
37import java.util.Map.Entry;
38import java.util.Properties;
39import java.util.Set;
40import java.util.Stack;
41import java.util.concurrent.BlockingQueue;
42import java.util.concurrent.ConcurrentHashMap;
43import java.util.concurrent.ConcurrentMap;
44import java.util.concurrent.CopyOnWriteArraySet;
45import java.util.concurrent.Executors;
46import java.util.concurrent.Future;
47import java.util.concurrent.LinkedBlockingQueue;
48import java.util.concurrent.RejectedExecutionException;
49import java.util.concurrent.TimeUnit;
50import java.util.concurrent.TimeoutException;
51
52import net.floodlightcontroller.core.FloodlightContext;
53import net.floodlightcontroller.core.IFloodlightProviderService;
54import net.floodlightcontroller.core.IHAListener;
55import net.floodlightcontroller.core.IInfoProvider;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056import net.floodlightcontroller.core.IListener.Command;
Jonathan Hartd10008d2013-02-23 17:04:08 -080057import net.floodlightcontroller.core.INetMapStorage.DM_OPERATION;
58import net.floodlightcontroller.core.INetMapTopologyService.ITopoRouteService;
59import net.floodlightcontroller.core.IOFMessageListener;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080060import net.floodlightcontroller.core.IOFSwitch;
61import net.floodlightcontroller.core.IOFSwitchFilter;
62import net.floodlightcontroller.core.IOFSwitchListener;
Pankaj Berde8557a462013-01-07 08:59:31 -080063import net.floodlightcontroller.core.ISwitchStorage.SwitchState;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080064import net.floodlightcontroller.core.annotations.LogMessageDoc;
65import net.floodlightcontroller.core.annotations.LogMessageDocs;
66import net.floodlightcontroller.core.internal.OFChannelState.HandshakeState;
67import net.floodlightcontroller.core.util.ListenerDispatcher;
68import net.floodlightcontroller.core.web.CoreWebRoutable;
69import net.floodlightcontroller.counter.ICounterStoreService;
Pavlin Radoslavov19b0e122013-02-21 18:47:38 -080070import net.floodlightcontroller.flowcache.IFlowService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080071import net.floodlightcontroller.packet.Ethernet;
72import net.floodlightcontroller.perfmon.IPktInProcessingTimeService;
73import net.floodlightcontroller.restserver.IRestApiService;
74import net.floodlightcontroller.storage.IResultSet;
75import net.floodlightcontroller.storage.IStorageSourceListener;
76import net.floodlightcontroller.storage.IStorageSourceService;
77import net.floodlightcontroller.storage.OperatorPredicate;
78import net.floodlightcontroller.storage.StorageException;
79import net.floodlightcontroller.threadpool.IThreadPoolService;
Jonathan Hartd82f20d2013-02-21 18:04:24 -080080import net.onrc.onos.registry.controller.IControllerRegistryService;
Jonathan Hartcc957a02013-02-26 10:39:04 -080081import net.onrc.onos.registry.controller.IControllerRegistryService.ControlChangeCallback;
Jonathan Hartd10008d2013-02-23 17:04:08 -080082import net.onrc.onos.registry.controller.RegistryException;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080083
84import org.jboss.netty.bootstrap.ServerBootstrap;
85import org.jboss.netty.buffer.ChannelBuffer;
86import org.jboss.netty.buffer.ChannelBuffers;
87import org.jboss.netty.channel.Channel;
88import org.jboss.netty.channel.ChannelHandlerContext;
89import org.jboss.netty.channel.ChannelPipelineFactory;
90import org.jboss.netty.channel.ChannelStateEvent;
91import org.jboss.netty.channel.ChannelUpstreamHandler;
92import org.jboss.netty.channel.Channels;
93import org.jboss.netty.channel.ExceptionEvent;
94import org.jboss.netty.channel.MessageEvent;
95import org.jboss.netty.channel.group.ChannelGroup;
96import org.jboss.netty.channel.group.DefaultChannelGroup;
97import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
98import org.jboss.netty.handler.timeout.IdleStateAwareChannelUpstreamHandler;
99import org.jboss.netty.handler.timeout.IdleStateEvent;
100import org.jboss.netty.handler.timeout.ReadTimeoutException;
101import org.openflow.protocol.OFEchoReply;
102import org.openflow.protocol.OFError;
103import org.openflow.protocol.OFError.OFBadActionCode;
104import org.openflow.protocol.OFError.OFBadRequestCode;
105import org.openflow.protocol.OFError.OFErrorType;
106import org.openflow.protocol.OFError.OFFlowModFailedCode;
107import org.openflow.protocol.OFError.OFHelloFailedCode;
108import org.openflow.protocol.OFError.OFPortModFailedCode;
109import org.openflow.protocol.OFError.OFQueueOpFailedCode;
110import org.openflow.protocol.OFFeaturesReply;
111import org.openflow.protocol.OFGetConfigReply;
112import org.openflow.protocol.OFMessage;
113import org.openflow.protocol.OFPacketIn;
114import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -0800115import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
Pankaj Berde6debb042013-01-16 18:04:32 -0800116import org.openflow.protocol.OFPhysicalPort.OFPortState;
Jonathan Hartd10008d2013-02-23 17:04:08 -0800117import org.openflow.protocol.OFPortStatus;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800118import org.openflow.protocol.OFPortStatus.OFPortReason;
119import org.openflow.protocol.OFSetConfig;
120import org.openflow.protocol.OFStatisticsRequest;
121import org.openflow.protocol.OFSwitchConfig;
122import org.openflow.protocol.OFType;
123import org.openflow.protocol.OFVendor;
124import org.openflow.protocol.factory.BasicFactory;
125import org.openflow.protocol.factory.MessageParseException;
126import org.openflow.protocol.statistics.OFDescriptionStatistics;
127import org.openflow.protocol.statistics.OFStatistics;
128import org.openflow.protocol.statistics.OFStatisticsType;
129import org.openflow.protocol.vendor.OFBasicVendorDataType;
130import org.openflow.protocol.vendor.OFBasicVendorId;
131import org.openflow.protocol.vendor.OFVendorId;
132import org.openflow.util.HexString;
133import org.openflow.util.U16;
134import org.openflow.util.U32;
135import org.openflow.vendor.nicira.OFNiciraVendorData;
136import org.openflow.vendor.nicira.OFRoleReplyVendorData;
137import org.openflow.vendor.nicira.OFRoleRequestVendorData;
138import org.openflow.vendor.nicira.OFRoleVendorData;
139import org.slf4j.Logger;
140import org.slf4j.LoggerFactory;
141
142
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800143
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800144/**
145 * The main controller class. Handles all setup and network listeners
146 */
147public class Controller implements IFloodlightProviderService,
148 IStorageSourceListener {
Pankaj Berde29ab7fc2013-01-25 06:17:52 -0800149
150 ThreadLocal<SwitchStorageImpl> store = new ThreadLocal<SwitchStorageImpl>() {
151 @Override
152 protected SwitchStorageImpl initialValue() {
153 SwitchStorageImpl swStore = new SwitchStorageImpl();
154 //TODO: Get the file path from global properties
155 swStore.init("/tmp/cassandra.titan");
156 return swStore;
157 }
158 };
159
160 protected SwitchStorageImpl swStore = store.get();
Pankaj Berde8557a462013-01-07 08:59:31 -0800161
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800162 protected static Logger log = LoggerFactory.getLogger(Controller.class);
163
164 private static final String ERROR_DATABASE =
165 "The controller could not communicate with the system database.";
166
167 protected BasicFactory factory;
168 protected ConcurrentMap<OFType,
169 ListenerDispatcher<OFType,IOFMessageListener>>
170 messageListeners;
171 // The activeSwitches map contains only those switches that are actively
172 // being controlled by us -- it doesn't contain switches that are
173 // in the slave role
174 protected ConcurrentHashMap<Long, IOFSwitch> activeSwitches;
175 // connectedSwitches contains all connected switches, including ones where
176 // we're a slave controller. We need to keep track of them so that we can
177 // send role request messages to switches when our role changes to master
178 // We add a switch to this set after it successfully completes the
179 // handshake. Access to this Set needs to be synchronized with roleChanger
180 protected HashSet<OFSwitchImpl> connectedSwitches;
181
182 // The controllerNodeIPsCache maps Controller IDs to their IP address.
183 // It's only used by handleControllerNodeIPsChanged
184 protected HashMap<String, String> controllerNodeIPsCache;
185
186 protected Set<IOFSwitchListener> switchListeners;
187 protected Set<IHAListener> haListeners;
188 protected Map<String, List<IInfoProvider>> providerMap;
189 protected BlockingQueue<IUpdate> updates;
190
191 // Module dependencies
192 protected IRestApiService restApi;
193 protected ICounterStoreService counterStore = null;
194 protected IStorageSourceService storageSource;
195 protected IPktInProcessingTimeService pktinProcTime;
196 protected IThreadPoolService threadPool;
Pavlin Radoslavov19b0e122013-02-21 18:47:38 -0800197 protected IFlowService flowService;
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -0800198 protected ITopoRouteService topoRouteService;
Jonathan Hartd10008d2013-02-23 17:04:08 -0800199 protected IControllerRegistryService registryService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800200
201 // Configuration options
202 protected int openFlowPort = 6633;
203 protected int workerThreads = 0;
204 // The id for this controller node. Should be unique for each controller
205 // node in a controller cluster.
206 protected String controllerId = "localhost";
207
208 // The current role of the controller.
209 // If the controller isn't configured to support roles, then this is null.
210 protected Role role;
211 // A helper that handles sending and timeout handling for role requests
212 protected RoleChanger roleChanger;
213
214 // Start time of the controller
215 protected long systemStartTime;
216
217 // Flag to always flush flow table on switch reconnect (HA or otherwise)
218 protected boolean alwaysClearFlowsOnSwAdd = false;
219
220 // Storage table names
221 protected static final String CONTROLLER_TABLE_NAME = "controller_controller";
222 protected static final String CONTROLLER_ID = "id";
223
224 protected static final String SWITCH_TABLE_NAME = "controller_switch";
225 protected static final String SWITCH_DATAPATH_ID = "dpid";
226 protected static final String SWITCH_SOCKET_ADDRESS = "socket_address";
227 protected static final String SWITCH_IP = "ip";
228 protected static final String SWITCH_CONTROLLER_ID = "controller_id";
229 protected static final String SWITCH_ACTIVE = "active";
230 protected static final String SWITCH_CONNECTED_SINCE = "connected_since";
231 protected static final String SWITCH_CAPABILITIES = "capabilities";
232 protected static final String SWITCH_BUFFERS = "buffers";
233 protected static final String SWITCH_TABLES = "tables";
234 protected static final String SWITCH_ACTIONS = "actions";
235
236 protected static final String SWITCH_CONFIG_TABLE_NAME = "controller_switchconfig";
237 protected static final String SWITCH_CONFIG_CORE_SWITCH = "core_switch";
238
239 protected static final String PORT_TABLE_NAME = "controller_port";
240 protected static final String PORT_ID = "id";
241 protected static final String PORT_SWITCH = "switch_id";
242 protected static final String PORT_NUMBER = "number";
243 protected static final String PORT_HARDWARE_ADDRESS = "hardware_address";
244 protected static final String PORT_NAME = "name";
245 protected static final String PORT_CONFIG = "config";
246 protected static final String PORT_STATE = "state";
247 protected static final String PORT_CURRENT_FEATURES = "current_features";
248 protected static final String PORT_ADVERTISED_FEATURES = "advertised_features";
249 protected static final String PORT_SUPPORTED_FEATURES = "supported_features";
250 protected static final String PORT_PEER_FEATURES = "peer_features";
251
252 protected static final String CONTROLLER_INTERFACE_TABLE_NAME = "controller_controllerinterface";
253 protected static final String CONTROLLER_INTERFACE_ID = "id";
254 protected static final String CONTROLLER_INTERFACE_CONTROLLER_ID = "controller_id";
255 protected static final String CONTROLLER_INTERFACE_TYPE = "type";
256 protected static final String CONTROLLER_INTERFACE_NUMBER = "number";
257 protected static final String CONTROLLER_INTERFACE_DISCOVERED_IP = "discovered_ip";
258
259
260
261 // Perf. related configuration
262 protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
263 protected static final int BATCH_MAX_SIZE = 100;
264 protected static final boolean ALWAYS_DECODE_ETH = true;
265
266 /**
267 * Updates handled by the main loop
268 */
269 protected interface IUpdate {
270 /**
271 * Calls the appropriate listeners
272 */
273 public void dispatch();
274 }
275 public enum SwitchUpdateType {
276 ADDED,
277 REMOVED,
278 PORTCHANGED
279 }
280 /**
281 * Update message indicating a switch was added or removed
282 */
283 protected class SwitchUpdate implements IUpdate {
284 public IOFSwitch sw;
285 public SwitchUpdateType switchUpdateType;
286 public SwitchUpdate(IOFSwitch sw, SwitchUpdateType switchUpdateType) {
287 this.sw = sw;
288 this.switchUpdateType = switchUpdateType;
289 }
290 public void dispatch() {
291 if (log.isTraceEnabled()) {
292 log.trace("Dispatching switch update {} {}",
293 sw, switchUpdateType);
294 }
295 if (switchListeners != null) {
296 for (IOFSwitchListener listener : switchListeners) {
297 switch(switchUpdateType) {
298 case ADDED:
299 listener.addedSwitch(sw);
300 break;
301 case REMOVED:
302 listener.removedSwitch(sw);
303 break;
304 case PORTCHANGED:
305 listener.switchPortChanged(sw.getId());
306 break;
307 }
308 }
309 }
310 }
311 }
312
313 /**
314 * Update message indicating controller's role has changed
315 */
316 protected class HARoleUpdate implements IUpdate {
317 public Role oldRole;
318 public Role newRole;
319 public HARoleUpdate(Role newRole, Role oldRole) {
320 this.oldRole = oldRole;
321 this.newRole = newRole;
322 }
323 public void dispatch() {
324 // Make sure that old and new roles are different.
325 if (oldRole == newRole) {
326 if (log.isTraceEnabled()) {
327 log.trace("HA role update ignored as the old and " +
328 "new roles are the same. newRole = {}" +
329 "oldRole = {}", newRole, oldRole);
330 }
331 return;
332 }
333 if (log.isTraceEnabled()) {
334 log.trace("Dispatching HA Role update newRole = {}, oldRole = {}",
335 newRole, oldRole);
336 }
337 if (haListeners != null) {
338 for (IHAListener listener : haListeners) {
339 listener.roleChanged(oldRole, newRole);
340 }
341 }
342 }
343 }
344
345 /**
346 * Update message indicating
347 * IPs of controllers in controller cluster have changed.
348 */
349 protected class HAControllerNodeIPUpdate implements IUpdate {
350 public Map<String,String> curControllerNodeIPs;
351 public Map<String,String> addedControllerNodeIPs;
352 public Map<String,String> removedControllerNodeIPs;
353 public HAControllerNodeIPUpdate(
354 HashMap<String,String> curControllerNodeIPs,
355 HashMap<String,String> addedControllerNodeIPs,
356 HashMap<String,String> removedControllerNodeIPs) {
357 this.curControllerNodeIPs = curControllerNodeIPs;
358 this.addedControllerNodeIPs = addedControllerNodeIPs;
359 this.removedControllerNodeIPs = removedControllerNodeIPs;
360 }
361 public void dispatch() {
362 if (log.isTraceEnabled()) {
363 log.trace("Dispatching HA Controller Node IP update "
364 + "curIPs = {}, addedIPs = {}, removedIPs = {}",
365 new Object[] { curControllerNodeIPs, addedControllerNodeIPs,
366 removedControllerNodeIPs }
367 );
368 }
369 if (haListeners != null) {
370 for (IHAListener listener: haListeners) {
371 listener.controllerNodeIPsChanged(curControllerNodeIPs,
372 addedControllerNodeIPs, removedControllerNodeIPs);
373 }
374 }
375 }
376 }
377
378 // ***************
379 // Getters/Setters
380 // ***************
381
382 public void setStorageSourceService(IStorageSourceService storageSource) {
383 this.storageSource = storageSource;
384 }
385
386 public void setCounterStore(ICounterStoreService counterStore) {
387 this.counterStore = counterStore;
388 }
389
390 public void setPktInProcessingService(IPktInProcessingTimeService pits) {
391 this.pktinProcTime = pits;
392 }
393
394 public void setRestApiService(IRestApiService restApi) {
395 this.restApi = restApi;
396 }
397
398 public void setThreadPoolService(IThreadPoolService tp) {
399 this.threadPool = tp;
400 }
401
Pavlin Radoslavov19b0e122013-02-21 18:47:38 -0800402 public void setFlowService(IFlowService serviceImpl) {
403 this.flowService = serviceImpl;
404 }
Pavlin Radoslavovd7d8b792013-02-22 10:24:38 -0800405
406 public void setTopoRouteService(ITopoRouteService serviceImpl) {
407 this.topoRouteService = serviceImpl;
408 }
Jonathan Hartc2e95ee2013-02-22 15:25:11 -0800409
Jonathan Hartd82f20d2013-02-21 18:04:24 -0800410 public void setMastershipService(IControllerRegistryService serviceImpl) {
Jonathan Hartd10008d2013-02-23 17:04:08 -0800411 this.registryService = serviceImpl;
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800412 }
413
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800414 @Override
415 public Role getRole() {
416 synchronized(roleChanger) {
417 return role;
418 }
419 }
420
421 @Override
422 public void setRole(Role role) {
423 if (role == null) throw new NullPointerException("Role can not be null.");
424 if (role == Role.MASTER && this.role == Role.SLAVE) {
425 // Reset db state to Inactive for all switches.
426 updateAllInactiveSwitchInfo();
427 }
428
429 // Need to synchronize to ensure a reliable ordering on role request
430 // messages send and to ensure the list of connected switches is stable
431 // RoleChanger will handle the actual sending of the message and
432 // timeout handling
433 // @see RoleChanger
434 synchronized(roleChanger) {
435 if (role.equals(this.role)) {
436 log.debug("Ignoring role change: role is already {}", role);
437 return;
438 }
439
440 Role oldRole = this.role;
441 this.role = role;
442
443 log.debug("Submitting role change request to role {}", role);
444 roleChanger.submitRequest(connectedSwitches, role);
445
446 // Enqueue an update for our listeners.
447 try {
448 this.updates.put(new HARoleUpdate(role, oldRole));
449 } catch (InterruptedException e) {
450 log.error("Failure adding update to queue", e);
451 }
452 }
453 }
454
455
456
457 // **********************
458 // ChannelUpstreamHandler
459 // **********************
460
461 /**
462 * Return a new channel handler for processing a switch connections
463 * @param state The channel state object for the connection
464 * @return the new channel handler
465 */
466 protected ChannelUpstreamHandler getChannelHandler(OFChannelState state) {
467 return new OFChannelHandler(state);
468 }
469
Jonathan Hartcc957a02013-02-26 10:39:04 -0800470 protected class RoleChangeCallback implements ControlChangeCallback {
471 @Override
472 public void controlChanged(long dpid, boolean hasControl) {
473 log.info("Role change callback for switch {}, hasControl {}",
474 HexString.toHexString(dpid), hasControl);
475
476 synchronized(roleChanger){
477 OFSwitchImpl sw = null;
478 for (OFSwitchImpl connectedSw : connectedSwitches){
479 if (connectedSw.getId() == dpid){
480 sw = connectedSw;
481 break;
482 }
483 }
484 if (sw == null){
485 log.warn("Switch {} not found in connected switches",
486 HexString.toHexString(dpid));
487 return;
488 }
489
490 Role role = null;
491
Pankaj Berde01939e92013-03-08 14:38:27 -0800492 /*
493 * issue #229
494 * Cannot rely on sw.getRole() as it can be behind due to pending
495 * role changes in the queue. Just submit it and late the RoleChanger
496 * handle duplicates.
497 */
498
499 if (hasControl){
Jonathan Hartcc957a02013-02-26 10:39:04 -0800500 role = Role.MASTER;
501 }
Pankaj Berde01939e92013-03-08 14:38:27 -0800502 else {
Jonathan Hartcc957a02013-02-26 10:39:04 -0800503 role = Role.SLAVE;
504 }
Pankaj Berde01939e92013-03-08 14:38:27 -0800505
506 log.debug("Sending role request {} msg to {}", role, sw);
507 Collection<OFSwitchImpl> swList = new ArrayList<OFSwitchImpl>(1);
508 swList.add(sw);
509 roleChanger.submitRequest(swList, role);
510
Jonathan Hartcc957a02013-02-26 10:39:04 -0800511 }
512
513 }
514 }
515
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800516 /**
517 * Channel handler deals with the switch connection and dispatches
518 * switch messages to the appropriate locations.
519 * @author readams
520 */
521 protected class OFChannelHandler
522 extends IdleStateAwareChannelUpstreamHandler {
523 protected OFSwitchImpl sw;
524 protected OFChannelState state;
525
526 public OFChannelHandler(OFChannelState state) {
527 this.state = state;
528 }
529
530 @Override
531 @LogMessageDoc(message="New switch connection from {ip address}",
532 explanation="A new switch has connected from the " +
533 "specified IP address")
534 public void channelConnected(ChannelHandlerContext ctx,
535 ChannelStateEvent e) throws Exception {
536 log.info("New switch connection from {}",
537 e.getChannel().getRemoteAddress());
538
539 sw = new OFSwitchImpl();
540 sw.setChannel(e.getChannel());
541 sw.setFloodlightProvider(Controller.this);
542 sw.setThreadPoolService(threadPool);
543
544 List<OFMessage> msglist = new ArrayList<OFMessage>(1);
545 msglist.add(factory.getMessage(OFType.HELLO));
546 e.getChannel().write(msglist);
547
548 }
549
550 @Override
551 @LogMessageDoc(message="Disconnected switch {switch information}",
552 explanation="The specified switch has disconnected.")
553 public void channelDisconnected(ChannelHandlerContext ctx,
554 ChannelStateEvent e) throws Exception {
555 if (sw != null && state.hsState == HandshakeState.READY) {
556 if (activeSwitches.containsKey(sw.getId())) {
557 // It's safe to call removeSwitch even though the map might
558 // not contain this particular switch but another with the
559 // same DPID
560 removeSwitch(sw);
561 }
562 synchronized(roleChanger) {
Pankaj Berdeda7187b2013-03-18 15:24:59 -0700563 if (controlRequested) {
564 registryService.releaseControl(sw.getId());
565 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800566 connectedSwitches.remove(sw);
567 }
568 sw.setConnected(false);
569 }
570 log.info("Disconnected switch {}", sw);
571 }
572
573 @Override
574 @LogMessageDocs({
575 @LogMessageDoc(level="ERROR",
576 message="Disconnecting switch {switch} due to read timeout",
577 explanation="The connected switch has failed to send any " +
578 "messages or respond to echo requests",
579 recommendation=LogMessageDoc.CHECK_SWITCH),
580 @LogMessageDoc(level="ERROR",
581 message="Disconnecting switch {switch}: failed to " +
582 "complete handshake",
583 explanation="The switch did not respond correctly " +
584 "to handshake messages",
585 recommendation=LogMessageDoc.CHECK_SWITCH),
586 @LogMessageDoc(level="ERROR",
587 message="Disconnecting switch {switch} due to IO Error: {}",
588 explanation="There was an error communicating with the switch",
589 recommendation=LogMessageDoc.CHECK_SWITCH),
590 @LogMessageDoc(level="ERROR",
591 message="Disconnecting switch {switch} due to switch " +
592 "state error: {error}",
593 explanation="The switch sent an unexpected message",
594 recommendation=LogMessageDoc.CHECK_SWITCH),
595 @LogMessageDoc(level="ERROR",
596 message="Disconnecting switch {switch} due to " +
597 "message parse failure",
598 explanation="Could not parse a message from the switch",
599 recommendation=LogMessageDoc.CHECK_SWITCH),
600 @LogMessageDoc(level="ERROR",
601 message="Terminating controller due to storage exception",
602 explanation=ERROR_DATABASE,
603 recommendation=LogMessageDoc.CHECK_CONTROLLER),
604 @LogMessageDoc(level="ERROR",
605 message="Could not process message: queue full",
606 explanation="OpenFlow messages are arriving faster than " +
607 " the controller can process them.",
608 recommendation=LogMessageDoc.CHECK_CONTROLLER),
609 @LogMessageDoc(level="ERROR",
610 message="Error while processing message " +
611 "from switch {switch} {cause}",
612 explanation="An error occurred processing the switch message",
613 recommendation=LogMessageDoc.GENERIC_ACTION)
614 })
615 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
616 throws Exception {
617 if (e.getCause() instanceof ReadTimeoutException) {
618 // switch timeout
619 log.error("Disconnecting switch {} due to read timeout", sw);
620 ctx.getChannel().close();
621 } else if (e.getCause() instanceof HandshakeTimeoutException) {
622 log.error("Disconnecting switch {}: failed to complete handshake",
623 sw);
624 ctx.getChannel().close();
625 } else if (e.getCause() instanceof ClosedChannelException) {
626 //log.warn("Channel for sw {} already closed", sw);
627 } else if (e.getCause() instanceof IOException) {
628 log.error("Disconnecting switch {} due to IO Error: {}",
629 sw, e.getCause().getMessage());
630 ctx.getChannel().close();
631 } else if (e.getCause() instanceof SwitchStateException) {
632 log.error("Disconnecting switch {} due to switch state error: {}",
633 sw, e.getCause().getMessage());
634 ctx.getChannel().close();
635 } else if (e.getCause() instanceof MessageParseException) {
636 log.error("Disconnecting switch " + sw +
637 " due to message parse failure",
638 e.getCause());
639 ctx.getChannel().close();
640 } else if (e.getCause() instanceof StorageException) {
641 log.error("Terminating controller due to storage exception",
642 e.getCause());
643 terminate();
644 } else if (e.getCause() instanceof RejectedExecutionException) {
645 log.warn("Could not process message: queue full");
646 } else {
647 log.error("Error while processing message from switch " + sw,
648 e.getCause());
649 ctx.getChannel().close();
650 }
651 }
652
653 @Override
654 public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e)
655 throws Exception {
656 List<OFMessage> msglist = new ArrayList<OFMessage>(1);
657 msglist.add(factory.getMessage(OFType.ECHO_REQUEST));
658 e.getChannel().write(msglist);
659 }
660
661 @Override
662 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
663 throws Exception {
664 if (e.getMessage() instanceof List) {
665 @SuppressWarnings("unchecked")
666 List<OFMessage> msglist = (List<OFMessage>)e.getMessage();
667
668 for (OFMessage ofm : msglist) {
669 try {
670 processOFMessage(ofm);
671 }
672 catch (Exception ex) {
673 // We are the last handler in the stream, so run the
674 // exception through the channel again by passing in
675 // ctx.getChannel().
676 Channels.fireExceptionCaught(ctx.getChannel(), ex);
677 }
678 }
679
680 // Flush all flow-mods/packet-out generated from this "train"
681 OFSwitchImpl.flush_all();
682 }
683 }
684
685 /**
686 * Process the request for the switch description
687 */
688 @LogMessageDoc(level="ERROR",
689 message="Exception in reading description " +
690 " during handshake {exception}",
691 explanation="Could not process the switch description string",
692 recommendation=LogMessageDoc.CHECK_SWITCH)
693 void processSwitchDescReply() {
694 try {
695 // Read description, if it has been updated
696 @SuppressWarnings("unchecked")
697 Future<List<OFStatistics>> desc_future =
698 (Future<List<OFStatistics>>)sw.
699 getAttribute(IOFSwitch.SWITCH_DESCRIPTION_FUTURE);
700 List<OFStatistics> values =
701 desc_future.get(0, TimeUnit.MILLISECONDS);
702 if (values != null) {
703 OFDescriptionStatistics description =
704 new OFDescriptionStatistics();
705 ChannelBuffer data =
706 ChannelBuffers.buffer(description.getLength());
707 for (OFStatistics f : values) {
708 f.writeTo(data);
709 description.readFrom(data);
710 break; // SHOULD be a list of length 1
711 }
712 sw.setAttribute(IOFSwitch.SWITCH_DESCRIPTION_DATA,
713 description);
714 sw.setSwitchProperties(description);
715 data = null;
716
717 // At this time, also set other switch properties from storage
718 boolean is_core_switch = false;
719 IResultSet resultSet = null;
720 try {
721 String swid = sw.getStringId();
722 resultSet =
723 storageSource.getRow(SWITCH_CONFIG_TABLE_NAME, swid);
724 for (Iterator<IResultSet> it =
725 resultSet.iterator(); it.hasNext();) {
726 // In case of multiple rows, use the status
727 // in last row?
728 Map<String, Object> row = it.next().getRow();
729 if (row.containsKey(SWITCH_CONFIG_CORE_SWITCH)) {
730 if (log.isDebugEnabled()) {
731 log.debug("Reading SWITCH_IS_CORE_SWITCH " +
732 "config for switch={}, is-core={}",
733 sw, row.get(SWITCH_CONFIG_CORE_SWITCH));
734 }
735 String ics =
736 (String)row.get(SWITCH_CONFIG_CORE_SWITCH);
737 is_core_switch = ics.equals("true");
738 }
739 }
740 }
741 finally {
742 if (resultSet != null)
743 resultSet.close();
744 }
745 if (is_core_switch) {
746 sw.setAttribute(IOFSwitch.SWITCH_IS_CORE_SWITCH,
747 new Boolean(true));
748 }
749 }
750 sw.removeAttribute(IOFSwitch.SWITCH_DESCRIPTION_FUTURE);
751 state.hasDescription = true;
752 checkSwitchReady();
753 }
754 catch (InterruptedException ex) {
755 // Ignore
756 }
757 catch (TimeoutException ex) {
758 // Ignore
759 } catch (Exception ex) {
760 log.error("Exception in reading description " +
761 " during handshake", ex);
762 }
763 }
764
765 /**
766 * Send initial switch setup information that we need before adding
767 * the switch
768 * @throws IOException
769 */
770 void sendHelloConfiguration() throws IOException {
771 // Send initial Features Request
Jonathan Hart9e92c512013-03-20 16:24:44 -0700772 log.debug("Sending FEATURES_REQUEST to {}", sw);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800773 sw.write(factory.getMessage(OFType.FEATURES_REQUEST), null);
774 }
775
776 /**
777 * Send the configuration requests we can only do after we have
778 * the features reply
779 * @throws IOException
780 */
781 void sendFeatureReplyConfiguration() throws IOException {
Jonathan Hart9e92c512013-03-20 16:24:44 -0700782 log.debug("Sending CONFIG_REQUEST to {}", sw);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800783 // Ensure we receive the full packet via PacketIn
784 OFSetConfig config = (OFSetConfig) factory
785 .getMessage(OFType.SET_CONFIG);
786 config.setMissSendLength((short) 0xffff)
787 .setLengthU(OFSwitchConfig.MINIMUM_LENGTH);
788 sw.write(config, null);
789 sw.write(factory.getMessage(OFType.GET_CONFIG_REQUEST),
790 null);
791
792 // Get Description to set switch-specific flags
793 OFStatisticsRequest req = new OFStatisticsRequest();
794 req.setStatisticType(OFStatisticsType.DESC);
795 req.setLengthU(req.getLengthU());
796 Future<List<OFStatistics>> dfuture =
797 sw.getStatistics(req);
798 sw.setAttribute(IOFSwitch.SWITCH_DESCRIPTION_FUTURE,
799 dfuture);
800
801 }
Pankaj Berdeda7187b2013-03-18 15:24:59 -0700802
803 volatile Boolean controlRequested = Boolean.FALSE;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800804 protected void checkSwitchReady() {
Pankaj Berdeda7187b2013-03-18 15:24:59 -0700805
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800806 if (state.hsState == HandshakeState.FEATURES_REPLY &&
807 state.hasDescription && state.hasGetConfigReply) {
808
809 state.hsState = HandshakeState.READY;
Jonathan Hart9e92c512013-03-20 16:24:44 -0700810 log.debug("Handshake with {} complete", sw);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800811
812 synchronized(roleChanger) {
813 // We need to keep track of all of the switches that are connected
814 // to the controller, in any role, so that we can later send the
815 // role request messages when the controller role changes.
816 // We need to be synchronized while doing this: we must not
817 // send a another role request to the connectedSwitches until
818 // we were able to add this new switch to connectedSwitches
819 // *and* send the current role to the new switch.
820 connectedSwitches.add(sw);
821
822 if (role != null) {
Jonathan Hart97801ac2013-02-26 14:29:16 -0800823 //Put the switch in SLAVE mode until we know we have control
824 log.debug("Setting new switch {} to SLAVE", sw.getStringId());
825 Collection<OFSwitchImpl> swList = new ArrayList<OFSwitchImpl>(1);
826 swList.add(sw);
827 roleChanger.submitRequest(swList, Role.SLAVE);
828
Jonathan Hartcc957a02013-02-26 10:39:04 -0800829 //Request control of the switch from the global registry
830 try {
Pankaj Berdeda7187b2013-03-18 15:24:59 -0700831 controlRequested = Boolean.TRUE;
Jonathan Hartcc957a02013-02-26 10:39:04 -0800832 registryService.requestControl(sw.getId(),
833 new RoleChangeCallback());
834 } catch (RegistryException e) {
835 log.debug("Registry error: {}", e.getMessage());
Pankaj Berde99fcee12013-03-18 09:41:53 -0700836 controlRequested = Boolean.FALSE;
Jonathan Hartcc957a02013-02-26 10:39:04 -0800837 }
838
Jonathan Hart97801ac2013-02-26 14:29:16 -0800839
Jonathan Hartcc957a02013-02-26 10:39:04 -0800840
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800841 // Send a role request if role support is enabled for the controller
842 // This is a probe that we'll use to determine if the switch
843 // actually supports the role request message. If it does we'll
844 // get back a role reply message. If it doesn't we'll get back an
845 // OFError message.
846 // If role is MASTER we will promote switch to active
847 // list when we receive the switch's role reply messages
Jonathan Hartcc957a02013-02-26 10:39:04 -0800848 /*
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800849 log.debug("This controller's role is {}, " +
850 "sending initial role request msg to {}",
851 role, sw);
852 Collection<OFSwitchImpl> swList = new ArrayList<OFSwitchImpl>(1);
853 swList.add(sw);
854 roleChanger.submitRequest(swList, role);
Jonathan Hartcc957a02013-02-26 10:39:04 -0800855 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800856 }
857 else {
858 // Role supported not enabled on controller (for now)
859 // automatically promote switch to active state.
Umesh Krishnaswamyb56bb292013-02-12 20:28:27 -0800860 log.debug("This controller's role is {}, " +
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800861 "not sending role request msg to {}",
862 role, sw);
863 // Need to clear FlowMods before we add the switch
864 // and dispatch updates otherwise we have a race condition.
865 sw.clearAllFlowMods();
866 addSwitch(sw);
867 state.firstRoleReplyReceived = true;
868 }
869 }
Pankaj Berde99fcee12013-03-18 09:41:53 -0700870 if (!controlRequested) {
871 // yield to allow other thread(s) to release control
872 try {
873 Thread.sleep(10);
874 } catch (InterruptedException e) {
875 // Ignore interruptions
876 }
877 // safer to bounce the switch to reconnect here than proceeding further
Jonathan Hart9e92c512013-03-20 16:24:44 -0700878 log.debug("Closing {} because we weren't able to request control " +
879 "successfully" + sw);
Pankaj Berde99fcee12013-03-18 09:41:53 -0700880 sw.channel.close();
881 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800882 }
883 }
884
885 /* Handle a role reply message we received from the switch. Since
886 * netty serializes message dispatch we don't need to synchronize
887 * against other receive operations from the same switch, so no need
888 * to synchronize addSwitch(), removeSwitch() operations from the same
889 * connection.
890 * FIXME: However, when a switch with the same DPID connects we do
891 * need some synchronization. However, handling switches with same
892 * DPID needs to be revisited anyways (get rid of r/w-lock and synchronous
893 * removedSwitch notification):1
894 *
895 */
896 @LogMessageDoc(level="ERROR",
897 message="Invalid role value in role reply message",
898 explanation="Was unable to set the HA role (master or slave) " +
899 "for the controller.",
900 recommendation=LogMessageDoc.CHECK_CONTROLLER)
901 protected void handleRoleReplyMessage(OFVendor vendorMessage,
902 OFRoleReplyVendorData roleReplyVendorData) {
903 // Map from the role code in the message to our role enum
904 int nxRole = roleReplyVendorData.getRole();
905 Role role = null;
906 switch (nxRole) {
907 case OFRoleVendorData.NX_ROLE_OTHER:
908 role = Role.EQUAL;
909 break;
910 case OFRoleVendorData.NX_ROLE_MASTER:
911 role = Role.MASTER;
912 break;
913 case OFRoleVendorData.NX_ROLE_SLAVE:
914 role = Role.SLAVE;
915 break;
916 default:
917 log.error("Invalid role value in role reply message");
918 sw.getChannel().close();
919 return;
920 }
921
922 log.debug("Handling role reply for role {} from {}. " +
923 "Controller's role is {} ",
924 new Object[] { role, sw, Controller.this.role}
925 );
926
927 sw.deliverRoleReply(vendorMessage.getXid(), role);
928
929 boolean isActive = activeSwitches.containsKey(sw.getId());
930 if (!isActive && sw.isActive()) {
931 // Transition from SLAVE to MASTER.
932
933 if (!state.firstRoleReplyReceived ||
934 getAlwaysClearFlowsOnSwAdd()) {
935 // This is the first role-reply message we receive from
936 // this switch or roles were disabled when the switch
937 // connected:
938 // Delete all pre-existing flows for new connections to
939 // the master
940 //
941 // FIXME: Need to think more about what the test should
942 // be for when we flush the flow-table? For example,
943 // if all the controllers are temporarily in the backup
944 // role (e.g. right after a failure of the master
945 // controller) at the point the switch connects, then
946 // all of the controllers will initially connect as
947 // backup controllers and not flush the flow-table.
948 // Then when one of them is promoted to master following
949 // the master controller election the flow-table
950 // will still not be flushed because that's treated as
951 // a failover event where we don't want to flush the
952 // flow-table. The end result would be that the flow
953 // table for a newly connected switch is never
954 // flushed. Not sure how to handle that case though...
955 sw.clearAllFlowMods();
956 log.debug("First role reply from master switch {}, " +
957 "clear FlowTable to active switch list",
958 HexString.toHexString(sw.getId()));
959 }
960
961 // Some switches don't seem to update us with port
962 // status messages while in slave role.
963 readSwitchPortStateFromStorage(sw);
964
965 // Only add the switch to the active switch list if
966 // we're not in the slave role. Note that if the role
967 // attribute is null, then that means that the switch
968 // doesn't support the role request messages, so in that
969 // case we're effectively in the EQUAL role and the
970 // switch should be included in the active switch list.
971 addSwitch(sw);
972 log.debug("Added master switch {} to active switch list",
973 HexString.toHexString(sw.getId()));
974
975 }
976 else if (isActive && !sw.isActive()) {
977 // Transition from MASTER to SLAVE: remove switch
978 // from active switch list.
979 log.debug("Removed slave switch {} from active switch" +
980 " list", HexString.toHexString(sw.getId()));
981 removeSwitch(sw);
982 }
983
984 // Indicate that we have received a role reply message.
985 state.firstRoleReplyReceived = true;
986 }
987
988 protected boolean handleVendorMessage(OFVendor vendorMessage) {
989 boolean shouldHandleMessage = false;
990 int vendor = vendorMessage.getVendor();
991 switch (vendor) {
992 case OFNiciraVendorData.NX_VENDOR_ID:
993 OFNiciraVendorData niciraVendorData =
994 (OFNiciraVendorData)vendorMessage.getVendorData();
995 int dataType = niciraVendorData.getDataType();
996 switch (dataType) {
997 case OFRoleReplyVendorData.NXT_ROLE_REPLY:
998 OFRoleReplyVendorData roleReplyVendorData =
999 (OFRoleReplyVendorData) niciraVendorData;
1000 handleRoleReplyMessage(vendorMessage,
1001 roleReplyVendorData);
1002 break;
1003 default:
1004 log.warn("Unhandled Nicira VENDOR message; " +
1005 "data type = {}", dataType);
1006 break;
1007 }
1008 break;
1009 default:
1010 log.warn("Unhandled VENDOR message; vendor id = {}", vendor);
1011 break;
1012 }
1013
1014 return shouldHandleMessage;
1015 }
1016
1017 /**
1018 * Dispatch an Openflow message from a switch to the appropriate
1019 * handler.
1020 * @param m The message to process
1021 * @throws IOException
1022 * @throws SwitchStateException
1023 */
1024 @LogMessageDocs({
1025 @LogMessageDoc(level="WARN",
1026 message="Config Reply from {switch} has " +
1027 "miss length set to {length}",
1028 explanation="The controller requires that the switch " +
1029 "use a miss length of 0xffff for correct " +
1030 "function",
1031 recommendation="Use a different switch to ensure " +
1032 "correct function"),
1033 @LogMessageDoc(level="WARN",
1034 message="Received ERROR from sw {switch} that "
1035 +"indicates roles are not supported "
1036 +"but we have received a valid "
1037 +"role reply earlier",
1038 explanation="The switch sent a confusing message to the" +
1039 "controller")
1040 })
1041 protected void processOFMessage(OFMessage m)
1042 throws IOException, SwitchStateException {
1043 boolean shouldHandleMessage = false;
1044
1045 switch (m.getType()) {
1046 case HELLO:
1047 if (log.isTraceEnabled())
1048 log.trace("HELLO from {}", sw);
1049
1050 if (state.hsState.equals(HandshakeState.START)) {
1051 state.hsState = HandshakeState.HELLO;
1052 sendHelloConfiguration();
1053 } else {
1054 throw new SwitchStateException("Unexpected HELLO from "
1055 + sw);
1056 }
1057 break;
1058 case ECHO_REQUEST:
1059 OFEchoReply reply =
1060 (OFEchoReply) factory.getMessage(OFType.ECHO_REPLY);
1061 reply.setXid(m.getXid());
1062 sw.write(reply, null);
1063 break;
1064 case ECHO_REPLY:
1065 break;
1066 case FEATURES_REPLY:
1067 if (log.isTraceEnabled())
1068 log.trace("Features Reply from {}", sw);
1069
1070 sw.setFeaturesReply((OFFeaturesReply) m);
1071 if (state.hsState.equals(HandshakeState.HELLO)) {
1072 sendFeatureReplyConfiguration();
1073 state.hsState = HandshakeState.FEATURES_REPLY;
1074 // uncomment to enable "dumb" switches like cbench
1075 // state.hsState = HandshakeState.READY;
1076 // addSwitch(sw);
1077 } else {
1078 // return results to rest api caller
1079 sw.deliverOFFeaturesReply(m);
1080 // update database */
1081 updateActiveSwitchInfo(sw);
1082 }
1083 break;
1084 case GET_CONFIG_REPLY:
1085 if (log.isTraceEnabled())
1086 log.trace("Get config reply from {}", sw);
1087
1088 if (!state.hsState.equals(HandshakeState.FEATURES_REPLY)) {
1089 String em = "Unexpected GET_CONFIG_REPLY from " + sw;
1090 throw new SwitchStateException(em);
1091 }
1092 OFGetConfigReply cr = (OFGetConfigReply) m;
1093 if (cr.getMissSendLength() == (short)0xffff) {
1094 log.trace("Config Reply from {} confirms " +
1095 "miss length set to 0xffff", sw);
1096 } else {
1097 log.warn("Config Reply from {} has " +
1098 "miss length set to {}",
1099 sw, cr.getMissSendLength() & 0xffff);
1100 }
1101 state.hasGetConfigReply = true;
1102 checkSwitchReady();
1103 break;
1104 case VENDOR:
1105 shouldHandleMessage = handleVendorMessage((OFVendor)m);
1106 break;
1107 case ERROR:
Jonathan Hart3525df92013-03-19 14:09:13 -07001108 log.debug("Recieved ERROR message from switch {}: {}", sw, m);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001109 // TODO: we need better error handling. Especially for
1110 // request/reply style message (stats, roles) we should have
1111 // a unified way to lookup the xid in the error message.
1112 // This will probable involve rewriting the way we handle
1113 // request/reply style messages.
1114 OFError error = (OFError) m;
1115 boolean shouldLogError = true;
1116 // TODO: should we check that firstRoleReplyReceived is false,
1117 // i.e., check only whether the first request fails?
1118 if (sw.checkFirstPendingRoleRequestXid(error.getXid())) {
1119 boolean isBadVendorError =
1120 (error.getErrorType() == OFError.OFErrorType.
1121 OFPET_BAD_REQUEST.getValue());
1122 // We expect to receive a bad vendor error when
1123 // we're connected to a switch that doesn't support
1124 // the Nicira vendor extensions (i.e. not OVS or
1125 // derived from OVS). By protocol, it should also be
1126 // BAD_VENDOR, but too many switch implementations
1127 // get it wrong and we can already check the xid()
1128 // so we can ignore the type with confidence that this
1129 // is not a spurious error
1130 shouldLogError = !isBadVendorError;
1131 if (isBadVendorError) {
Jonathan Hart3525df92013-03-19 14:09:13 -07001132 log.debug("Handling bad vendor error for {}", sw);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001133 if (state.firstRoleReplyReceived && (role != null)) {
1134 log.warn("Received ERROR from sw {} that "
1135 +"indicates roles are not supported "
1136 +"but we have received a valid "
1137 +"role reply earlier", sw);
1138 }
1139 state.firstRoleReplyReceived = true;
Jonathan Harta95c6d92013-03-18 16:12:27 -07001140 Role requestedRole =
1141 sw.deliverRoleRequestNotSupported(error.getXid());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001142 synchronized(roleChanger) {
1143 if (sw.role == null && Controller.this.role==Role.SLAVE) {
Jonathan Harta95c6d92013-03-18 16:12:27 -07001144 //This will now never happen. The Controller's role
1145 //is now never SLAVE, always MASTER.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001146 // the switch doesn't understand role request
1147 // messages and the current controller role is
1148 // slave. We need to disconnect the switch.
1149 // @see RoleChanger for rationale
Jonathan Hart9e92c512013-03-20 16:24:44 -07001150 log.warn("Closing {} channel because controller's role " +
1151 "is SLAVE", sw);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001152 sw.getChannel().close();
1153 }
Jonathan Harta95c6d92013-03-18 16:12:27 -07001154 else if (sw.role == null && requestedRole == Role.MASTER) {
Jonathan Hart3525df92013-03-19 14:09:13 -07001155 log.debug("Adding switch {} because we got an error" +
1156 " returned from a MASTER role request", sw);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001157 // Controller's role is master: add to
1158 // active
1159 // TODO: check if clearing flow table is
1160 // right choice here.
1161 // Need to clear FlowMods before we add the switch
1162 // and dispatch updates otherwise we have a race condition.
1163 // TODO: switch update is async. Won't we still have a potential
1164 // race condition?
1165 sw.clearAllFlowMods();
1166 addSwitch(sw);
1167 }
1168 }
1169 }
1170 else {
1171 // TODO: Is this the right thing to do if we receive
1172 // some other error besides a bad vendor error?
1173 // Presumably that means the switch did actually
1174 // understand the role request message, but there
1175 // was some other error from processing the message.
1176 // OF 1.2 specifies a OFPET_ROLE_REQUEST_FAILED
1177 // error code, but it doesn't look like the Nicira
1178 // role request has that. Should check OVS source
1179 // code to see if it's possible for any other errors
1180 // to be returned.
1181 // If we received an error the switch is not
1182 // in the correct role, so we need to disconnect it.
1183 // We could also resend the request but then we need to
1184 // check if there are other pending request in which
1185 // case we shouldn't resend. If we do resend we need
1186 // to make sure that the switch eventually accepts one
1187 // of our requests or disconnect the switch. This feels
1188 // cumbersome.
Jonathan Hart9e92c512013-03-20 16:24:44 -07001189 log.debug("Closing {} channel because we recieved an " +
1190 "error other than BAD_VENDOR", sw);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001191 sw.getChannel().close();
1192 }
1193 }
1194 // Once we support OF 1.2, we'd add code to handle it here.
1195 //if (error.getXid() == state.ofRoleRequestXid) {
1196 //}
1197 if (shouldLogError)
1198 logError(sw, error);
1199 break;
1200 case STATS_REPLY:
1201 if (state.hsState.ordinal() <
1202 HandshakeState.FEATURES_REPLY.ordinal()) {
1203 String em = "Unexpected STATS_REPLY from " + sw;
1204 throw new SwitchStateException(em);
1205 }
1206 sw.deliverStatisticsReply(m);
1207 if (sw.hasAttribute(IOFSwitch.SWITCH_DESCRIPTION_FUTURE)) {
1208 processSwitchDescReply();
1209 }
1210 break;
1211 case PORT_STATUS:
1212 // We want to update our port state info even if we're in
1213 // the slave role, but we only want to update storage if
1214 // we're the master (or equal).
1215 boolean updateStorage = state.hsState.
1216 equals(HandshakeState.READY) &&
1217 (sw.getRole() != Role.SLAVE);
1218 handlePortStatusMessage(sw, (OFPortStatus)m, updateStorage);
1219 shouldHandleMessage = true;
1220 break;
1221
1222 default:
1223 shouldHandleMessage = true;
1224 break;
1225 }
1226
1227 if (shouldHandleMessage) {
1228 sw.getListenerReadLock().lock();
1229 try {
1230 if (sw.isConnected()) {
1231 if (!state.hsState.equals(HandshakeState.READY)) {
1232 log.debug("Ignoring message type {} received " +
1233 "from switch {} before switch is " +
1234 "fully configured.", m.getType(), sw);
1235 }
1236 // Check if the controller is in the slave role for the
1237 // switch. If it is, then don't dispatch the message to
1238 // the listeners.
1239 // TODO: Should we dispatch messages that we expect to
1240 // receive when we're in the slave role, e.g. port
1241 // status messages? Since we're "hiding" switches from
1242 // the listeners when we're in the slave role, then it
1243 // seems a little weird to dispatch port status messages
1244 // to them. On the other hand there might be special
1245 // modules that care about all of the connected switches
1246 // and would like to receive port status notifications.
1247 else if (sw.getRole() == Role.SLAVE) {
1248 // Don't log message if it's a port status message
1249 // since we expect to receive those from the switch
1250 // and don't want to emit spurious messages.
1251 if (m.getType() != OFType.PORT_STATUS) {
1252 log.debug("Ignoring message type {} received " +
1253 "from switch {} while in the slave role.",
1254 m.getType(), sw);
1255 }
1256 } else {
1257 handleMessage(sw, m, null);
1258 }
1259 }
1260 }
1261 finally {
1262 sw.getListenerReadLock().unlock();
1263 }
1264 }
1265 }
1266 }
1267
1268 // ****************
1269 // Message handlers
1270 // ****************
1271
1272 protected void handlePortStatusMessage(IOFSwitch sw,
1273 OFPortStatus m,
1274 boolean updateStorage) {
1275 short portNumber = m.getDesc().getPortNumber();
1276 OFPhysicalPort port = m.getDesc();
1277 if (m.getReason() == (byte)OFPortReason.OFPPR_MODIFY.ordinal()) {
Pankaj Berde6a4075d2013-01-22 16:42:54 -08001278 boolean portDown = ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
1279 ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001280 sw.setPort(port);
Pankaj Berde6a4075d2013-01-22 16:42:54 -08001281 if (!portDown) {
Pankaj Berde6debb042013-01-16 18:04:32 -08001282 swStore.addPort(sw.getStringId(), port);
1283 } else {
1284 swStore.deletePort(sw.getStringId(), port.getPortNumber());
1285 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001286 if (updateStorage)
1287 updatePortInfo(sw, port);
1288 log.debug("Port #{} modified for {}", portNumber, sw);
1289 } else if (m.getReason() == (byte)OFPortReason.OFPPR_ADD.ordinal()) {
1290 sw.setPort(port);
Pankaj Berde8557a462013-01-07 08:59:31 -08001291 swStore.addPort(sw.getStringId(), port);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001292 if (updateStorage)
1293 updatePortInfo(sw, port);
1294 log.debug("Port #{} added for {}", portNumber, sw);
1295 } else if (m.getReason() ==
1296 (byte)OFPortReason.OFPPR_DELETE.ordinal()) {
1297 sw.deletePort(portNumber);
Pankaj Berde8557a462013-01-07 08:59:31 -08001298 swStore.deletePort(sw.getStringId(), portNumber);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001299 if (updateStorage)
1300 removePortInfo(sw, portNumber);
1301 log.debug("Port #{} deleted for {}", portNumber, sw);
1302 }
1303 SwitchUpdate update = new SwitchUpdate(sw, SwitchUpdateType.PORTCHANGED);
1304 try {
1305 this.updates.put(update);
1306 } catch (InterruptedException e) {
1307 log.error("Failure adding update to queue", e);
1308 }
1309 }
1310
1311 /**
1312 * flcontext_cache - Keep a thread local stack of contexts
1313 */
1314 protected static final ThreadLocal<Stack<FloodlightContext>> flcontext_cache =
1315 new ThreadLocal <Stack<FloodlightContext>> () {
1316 @Override
1317 protected Stack<FloodlightContext> initialValue() {
1318 return new Stack<FloodlightContext>();
1319 }
1320 };
1321
1322 /**
1323 * flcontext_alloc - pop a context off the stack, if required create a new one
1324 * @return FloodlightContext
1325 */
1326 protected static FloodlightContext flcontext_alloc() {
1327 FloodlightContext flcontext = null;
1328
1329 if (flcontext_cache.get().empty()) {
1330 flcontext = new FloodlightContext();
1331 }
1332 else {
1333 flcontext = flcontext_cache.get().pop();
1334 }
1335
1336 return flcontext;
1337 }
1338
1339 /**
1340 * flcontext_free - Free the context to the current thread
1341 * @param flcontext
1342 */
1343 protected void flcontext_free(FloodlightContext flcontext) {
1344 flcontext.getStorage().clear();
1345 flcontext_cache.get().push(flcontext);
1346 }
1347
1348 /**
1349 * Handle replies to certain OFMessages, and pass others off to listeners
1350 * @param sw The switch for the message
1351 * @param m The message
1352 * @param bContext The floodlight context. If null then floodlight context would
1353 * be allocated in this function
1354 * @throws IOException
1355 */
1356 @LogMessageDocs({
1357 @LogMessageDoc(level="ERROR",
1358 message="Ignoring PacketIn (Xid = {xid}) because the data" +
1359 " field is empty.",
1360 explanation="The switch sent an improperly-formatted PacketIn" +
1361 " message",
1362 recommendation=LogMessageDoc.CHECK_SWITCH),
1363 @LogMessageDoc(level="WARN",
1364 message="Unhandled OF Message: {} from {}",
1365 explanation="The switch sent a message not handled by " +
1366 "the controller")
1367 })
1368 protected void handleMessage(IOFSwitch sw, OFMessage m,
1369 FloodlightContext bContext)
1370 throws IOException {
1371 Ethernet eth = null;
1372
1373 switch (m.getType()) {
1374 case PACKET_IN:
1375 OFPacketIn pi = (OFPacketIn)m;
1376
1377 if (pi.getPacketData().length <= 0) {
1378 log.error("Ignoring PacketIn (Xid = " + pi.getXid() +
1379 ") because the data field is empty.");
1380 return;
1381 }
1382
1383 if (Controller.ALWAYS_DECODE_ETH) {
1384 eth = new Ethernet();
1385 eth.deserialize(pi.getPacketData(), 0,
1386 pi.getPacketData().length);
1387 counterStore.updatePacketInCounters(sw, m, eth);
1388 }
1389 // fall through to default case...
1390
1391 default:
1392
1393 List<IOFMessageListener> listeners = null;
1394 if (messageListeners.containsKey(m.getType())) {
1395 listeners = messageListeners.get(m.getType()).
1396 getOrderedListeners();
1397 }
1398
1399 FloodlightContext bc = null;
1400 if (listeners != null) {
1401 // Check if floodlight context is passed from the calling
1402 // function, if so use that floodlight context, otherwise
1403 // allocate one
1404 if (bContext == null) {
1405 bc = flcontext_alloc();
1406 } else {
1407 bc = bContext;
1408 }
1409 if (eth != null) {
1410 IFloodlightProviderService.bcStore.put(bc,
1411 IFloodlightProviderService.CONTEXT_PI_PAYLOAD,
1412 eth);
1413 }
1414
1415 // Get the starting time (overall and per-component) of
1416 // the processing chain for this packet if performance
1417 // monitoring is turned on
1418 pktinProcTime.bootstrap(listeners);
1419 pktinProcTime.recordStartTimePktIn();
1420 Command cmd;
1421 for (IOFMessageListener listener : listeners) {
1422 if (listener instanceof IOFSwitchFilter) {
1423 if (!((IOFSwitchFilter)listener).isInterested(sw)) {
1424 continue;
1425 }
1426 }
1427
1428 pktinProcTime.recordStartTimeComp(listener);
1429 cmd = listener.receive(sw, m, bc);
1430 pktinProcTime.recordEndTimeComp(listener);
1431
1432 if (Command.STOP.equals(cmd)) {
1433 break;
1434 }
1435 }
1436 pktinProcTime.recordEndTimePktIn(sw, m, bc);
1437 } else {
1438 log.warn("Unhandled OF Message: {} from {}", m, sw);
1439 }
1440
1441 if ((bContext == null) && (bc != null)) flcontext_free(bc);
1442 }
1443 }
1444
1445 /**
1446 * Log an OpenFlow error message from a switch
1447 * @param sw The switch that sent the error
1448 * @param error The error message
1449 */
1450 @LogMessageDoc(level="ERROR",
1451 message="Error {error type} {error code} from {switch}",
1452 explanation="The switch responded with an unexpected error" +
1453 "to an OpenFlow message from the controller",
1454 recommendation="This could indicate improper network operation. " +
1455 "If the problem persists restarting the switch and " +
1456 "controller may help."
1457 )
1458 protected void logError(IOFSwitch sw, OFError error) {
1459 int etint = 0xffff & error.getErrorType();
1460 if (etint < 0 || etint >= OFErrorType.values().length) {
1461 log.error("Unknown error code {} from sw {}", etint, sw);
1462 }
1463 OFErrorType et = OFErrorType.values()[etint];
1464 switch (et) {
1465 case OFPET_HELLO_FAILED:
1466 OFHelloFailedCode hfc =
1467 OFHelloFailedCode.values()[0xffff & error.getErrorCode()];
1468 log.error("Error {} {} from {}", new Object[] {et, hfc, sw});
1469 break;
1470 case OFPET_BAD_REQUEST:
1471 OFBadRequestCode brc =
1472 OFBadRequestCode.values()[0xffff & error.getErrorCode()];
1473 log.error("Error {} {} from {}", new Object[] {et, brc, sw});
1474 break;
1475 case OFPET_BAD_ACTION:
1476 OFBadActionCode bac =
1477 OFBadActionCode.values()[0xffff & error.getErrorCode()];
1478 log.error("Error {} {} from {}", new Object[] {et, bac, sw});
1479 break;
1480 case OFPET_FLOW_MOD_FAILED:
1481 OFFlowModFailedCode fmfc =
1482 OFFlowModFailedCode.values()[0xffff & error.getErrorCode()];
1483 log.error("Error {} {} from {}", new Object[] {et, fmfc, sw});
1484 break;
1485 case OFPET_PORT_MOD_FAILED:
1486 OFPortModFailedCode pmfc =
1487 OFPortModFailedCode.values()[0xffff & error.getErrorCode()];
1488 log.error("Error {} {} from {}", new Object[] {et, pmfc, sw});
1489 break;
1490 case OFPET_QUEUE_OP_FAILED:
1491 OFQueueOpFailedCode qofc =
1492 OFQueueOpFailedCode.values()[0xffff & error.getErrorCode()];
1493 log.error("Error {} {} from {}", new Object[] {et, qofc, sw});
1494 break;
1495 default:
1496 break;
1497 }
1498 }
1499
1500 /**
1501 * Add a switch to the active switch list and call the switch listeners.
1502 * This happens either when a switch first connects (and the controller is
1503 * not in the slave role) or when the role of the controller changes from
1504 * slave to master.
1505 * @param sw the switch that has been added
1506 */
1507 // TODO: need to rethink locking and the synchronous switch update.
1508 // We can / should also handle duplicate DPIDs in connectedSwitches
1509 @LogMessageDoc(level="ERROR",
1510 message="New switch added {switch} for already-added switch {switch}",
1511 explanation="A switch with the same DPID as another switch " +
1512 "connected to the controller. This can be caused by " +
1513 "multiple switches configured with the same DPID, or " +
1514 "by a switch reconnected very quickly after " +
1515 "disconnecting.",
1516 recommendation="If this happens repeatedly, it is likely there " +
1517 "are switches with duplicate DPIDs on the network. " +
1518 "Reconfigure the appropriate switches. If it happens " +
1519 "very rarely, then it is likely this is a transient " +
1520 "network problem that can be ignored."
1521 )
1522 protected void addSwitch(IOFSwitch sw) {
1523 // TODO: is it safe to modify the HashMap without holding
1524 // the old switch's lock?
1525 OFSwitchImpl oldSw = (OFSwitchImpl) this.activeSwitches.put(sw.getId(), sw);
1526 if (sw == oldSw) {
1527 // Note == for object equality, not .equals for value
1528 log.info("New add switch for pre-existing switch {}", sw);
1529 return;
1530 }
1531
1532 if (oldSw != null) {
1533 oldSw.getListenerWriteLock().lock();
1534 try {
1535 log.error("New switch added {} for already-added switch {}",
1536 sw, oldSw);
1537 // Set the connected flag to false to suppress calling
1538 // the listeners for this switch in processOFMessage
1539 oldSw.setConnected(false);
1540
1541 oldSw.cancelAllStatisticsReplies();
1542
1543 updateInactiveSwitchInfo(oldSw);
1544
1545 // we need to clean out old switch state definitively
1546 // before adding the new switch
1547 // FIXME: It seems not completely kosher to call the
1548 // switch listeners here. I thought one of the points of
1549 // having the asynchronous switch update mechanism was so
1550 // the addedSwitch and removedSwitch were always called
1551 // from a single thread to simplify concurrency issues
1552 // for the listener.
1553 if (switchListeners != null) {
1554 for (IOFSwitchListener listener : switchListeners) {
1555 listener.removedSwitch(oldSw);
1556 }
1557 }
1558 // will eventually trigger a removeSwitch(), which will cause
1559 // a "Not removing Switch ... already removed debug message.
1560 // TODO: Figure out a way to handle this that avoids the
1561 // spurious debug message.
Jonathan Hart9e92c512013-03-20 16:24:44 -07001562 log.debug("Closing {} because a new IOFSwitch got added " +
1563 "for this dpid", oldSw);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001564 oldSw.getChannel().close();
1565 }
1566 finally {
1567 oldSw.getListenerWriteLock().unlock();
1568 }
1569 }
1570
1571 updateActiveSwitchInfo(sw);
Pankaj Berdef8ad2852013-02-27 17:06:14 -08001572 if (registryService.hasControl(sw.getId())) {
1573 swStore.update(sw.getStringId(), SwitchState.ACTIVE, DM_OPERATION.UPDATE);
1574 for (OFPhysicalPort port: sw.getPorts()) {
1575 swStore.addPort(sw.getStringId(), port);
1576 }
Pankaj Berde0fc4e432013-01-12 09:47:22 -08001577 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001578 SwitchUpdate update = new SwitchUpdate(sw, SwitchUpdateType.ADDED);
1579 try {
1580 this.updates.put(update);
1581 } catch (InterruptedException e) {
1582 log.error("Failure adding update to queue", e);
1583 }
1584 }
1585
1586 /**
1587 * Remove a switch from the active switch list and call the switch listeners.
1588 * This happens either when the switch is disconnected or when the
1589 * controller's role for the switch changes from master to slave.
1590 * @param sw the switch that has been removed
1591 */
1592 protected void removeSwitch(IOFSwitch sw) {
1593 // No need to acquire the listener lock, since
1594 // this method is only called after netty has processed all
1595 // pending messages
1596 log.debug("removeSwitch: {}", sw);
Pankaj Berdefa4d0f72013-03-13 17:59:37 -07001597 //
1598 // Cannot set sw to inactive in network map due to race condition
1599 // Need a cleanup thread to periodically check switches not active in registry
1600 // and acquire control to set to inactive state in network map and release it
1601 //
1602 // if (registryService.hasControl(sw.getId())) {
1603 // swStore.update(sw.getStringId(), SwitchState.INACTIVE, DM_OPERATION.UPDATE);
1604 // }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001605 if (!this.activeSwitches.remove(sw.getId(), sw) || !sw.isConnected()) {
1606 log.debug("Not removing switch {}; already removed", sw);
1607 return;
1608 }
1609 // We cancel all outstanding statistics replies if the switch transition
1610 // from active. In the future we might allow statistics requests
1611 // from slave controllers. Then we need to move this cancelation
1612 // to switch disconnect
1613 sw.cancelAllStatisticsReplies();
Pankaj Berdeafb20532013-01-08 15:05:24 -08001614
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001615
1616 // FIXME: I think there's a race condition if we call updateInactiveSwitchInfo
1617 // here if role support is enabled. In that case if the switch is being
1618 // removed because we've been switched to being in the slave role, then I think
1619 // it's possible that the new master may have already been promoted to master
1620 // and written out the active switch state to storage. If we now execute
1621 // updateInactiveSwitchInfo we may wipe out all of the state that was
1622 // written out by the new master. Maybe need to revisit how we handle all
1623 // of the switch state that's written to storage.
1624
1625 updateInactiveSwitchInfo(sw);
Pankaj Berdeafb20532013-01-08 15:05:24 -08001626
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001627 SwitchUpdate update = new SwitchUpdate(sw, SwitchUpdateType.REMOVED);
1628 try {
1629 this.updates.put(update);
1630 } catch (InterruptedException e) {
1631 log.error("Failure adding update to queue", e);
1632 }
1633 }
1634
1635 // ***************
1636 // IFloodlightProvider
1637 // ***************
1638
1639 @Override
1640 public synchronized void addOFMessageListener(OFType type,
1641 IOFMessageListener listener) {
1642 ListenerDispatcher<OFType, IOFMessageListener> ldd =
1643 messageListeners.get(type);
1644 if (ldd == null) {
1645 ldd = new ListenerDispatcher<OFType, IOFMessageListener>();
1646 messageListeners.put(type, ldd);
1647 }
1648 ldd.addListener(type, listener);
1649 }
1650
1651 @Override
1652 public synchronized void removeOFMessageListener(OFType type,
1653 IOFMessageListener listener) {
1654 ListenerDispatcher<OFType, IOFMessageListener> ldd =
1655 messageListeners.get(type);
1656 if (ldd != null) {
1657 ldd.removeListener(listener);
1658 }
1659 }
1660
1661 private void logListeners() {
1662 for (Map.Entry<OFType,
1663 ListenerDispatcher<OFType,
1664 IOFMessageListener>> entry
1665 : messageListeners.entrySet()) {
1666
1667 OFType type = entry.getKey();
1668 ListenerDispatcher<OFType, IOFMessageListener> ldd =
1669 entry.getValue();
1670
1671 StringBuffer sb = new StringBuffer();
1672 sb.append("OFListeners for ");
1673 sb.append(type);
1674 sb.append(": ");
1675 for (IOFMessageListener l : ldd.getOrderedListeners()) {
1676 sb.append(l.getName());
1677 sb.append(",");
1678 }
1679 log.debug(sb.toString());
1680 }
1681 }
1682
1683 public void removeOFMessageListeners(OFType type) {
1684 messageListeners.remove(type);
1685 }
1686
1687 @Override
1688 public Map<Long, IOFSwitch> getSwitches() {
1689 return Collections.unmodifiableMap(this.activeSwitches);
1690 }
1691
1692 @Override
1693 public void addOFSwitchListener(IOFSwitchListener listener) {
1694 this.switchListeners.add(listener);
1695 }
1696
1697 @Override
1698 public void removeOFSwitchListener(IOFSwitchListener listener) {
1699 this.switchListeners.remove(listener);
1700 }
1701
1702 @Override
1703 public Map<OFType, List<IOFMessageListener>> getListeners() {
1704 Map<OFType, List<IOFMessageListener>> lers =
1705 new HashMap<OFType, List<IOFMessageListener>>();
1706 for(Entry<OFType, ListenerDispatcher<OFType, IOFMessageListener>> e :
1707 messageListeners.entrySet()) {
1708 lers.put(e.getKey(), e.getValue().getOrderedListeners());
1709 }
1710 return Collections.unmodifiableMap(lers);
1711 }
1712
1713 @Override
1714 @LogMessageDocs({
1715 @LogMessageDoc(message="Failed to inject OFMessage {message} onto " +
1716 "a null switch",
1717 explanation="Failed to process a message because the switch " +
1718 " is no longer connected."),
1719 @LogMessageDoc(level="ERROR",
1720 message="Error reinjecting OFMessage on switch {switch}",
1721 explanation="An I/O error occured while attempting to " +
1722 "process an OpenFlow message",
1723 recommendation=LogMessageDoc.CHECK_SWITCH)
1724 })
1725 public boolean injectOfMessage(IOFSwitch sw, OFMessage msg,
1726 FloodlightContext bc) {
1727 if (sw == null) {
1728 log.info("Failed to inject OFMessage {} onto a null switch", msg);
1729 return false;
1730 }
1731
1732 // FIXME: Do we need to be able to inject messages to switches
1733 // where we're the slave controller (i.e. they're connected but
1734 // not active)?
1735 // FIXME: Don't we need synchronization logic here so we're holding
1736 // the listener read lock when we call handleMessage? After some
1737 // discussions it sounds like the right thing to do here would be to
1738 // inject the message as a netty upstream channel event so it goes
1739 // through the normal netty event processing, including being
1740 // handled
1741 if (!activeSwitches.containsKey(sw.getId())) return false;
1742
1743 try {
1744 // Pass Floodlight context to the handleMessages()
1745 handleMessage(sw, msg, bc);
1746 } catch (IOException e) {
1747 log.error("Error reinjecting OFMessage on switch {}",
1748 HexString.toHexString(sw.getId()));
1749 return false;
1750 }
1751 return true;
1752 }
1753
1754 @Override
1755 @LogMessageDoc(message="Calling System.exit",
1756 explanation="The controller is terminating")
1757 public synchronized void terminate() {
1758 log.info("Calling System.exit");
1759 System.exit(1);
1760 }
1761
1762 @Override
1763 public boolean injectOfMessage(IOFSwitch sw, OFMessage msg) {
1764 // call the overloaded version with floodlight context set to null
1765 return injectOfMessage(sw, msg, null);
1766 }
1767
1768 @Override
1769 public void handleOutgoingMessage(IOFSwitch sw, OFMessage m,
1770 FloodlightContext bc) {
1771 if (log.isTraceEnabled()) {
1772 String str = OFMessage.getDataAsString(sw, m, bc);
1773 log.trace("{}", str);
1774 }
1775
1776 List<IOFMessageListener> listeners = null;
1777 if (messageListeners.containsKey(m.getType())) {
1778 listeners =
1779 messageListeners.get(m.getType()).getOrderedListeners();
1780 }
1781
1782 if (listeners != null) {
1783 for (IOFMessageListener listener : listeners) {
1784 if (listener instanceof IOFSwitchFilter) {
1785 if (!((IOFSwitchFilter)listener).isInterested(sw)) {
1786 continue;
1787 }
1788 }
1789 if (Command.STOP.equals(listener.receive(sw, m, bc))) {
1790 break;
1791 }
1792 }
1793 }
1794 }
1795
1796 @Override
1797 public BasicFactory getOFMessageFactory() {
1798 return factory;
1799 }
1800
1801 @Override
1802 public String getControllerId() {
1803 return controllerId;
1804 }
1805
1806 // **************
1807 // Initialization
1808 // **************
1809
1810 protected void updateAllInactiveSwitchInfo() {
1811 if (role == Role.SLAVE) {
1812 return;
1813 }
1814 String controllerId = getControllerId();
1815 String[] switchColumns = { SWITCH_DATAPATH_ID,
1816 SWITCH_CONTROLLER_ID,
1817 SWITCH_ACTIVE };
1818 String[] portColumns = { PORT_ID, PORT_SWITCH };
1819 IResultSet switchResultSet = null;
1820 try {
1821 OperatorPredicate op =
1822 new OperatorPredicate(SWITCH_CONTROLLER_ID,
1823 OperatorPredicate.Operator.EQ,
1824 controllerId);
1825 switchResultSet =
1826 storageSource.executeQuery(SWITCH_TABLE_NAME,
1827 switchColumns,
1828 op, null);
1829 while (switchResultSet.next()) {
1830 IResultSet portResultSet = null;
1831 try {
1832 String datapathId =
1833 switchResultSet.getString(SWITCH_DATAPATH_ID);
1834 switchResultSet.setBoolean(SWITCH_ACTIVE, Boolean.FALSE);
1835 op = new OperatorPredicate(PORT_SWITCH,
1836 OperatorPredicate.Operator.EQ,
1837 datapathId);
1838 portResultSet =
1839 storageSource.executeQuery(PORT_TABLE_NAME,
1840 portColumns,
1841 op, null);
1842 while (portResultSet.next()) {
1843 portResultSet.deleteRow();
1844 }
1845 portResultSet.save();
1846 }
1847 finally {
1848 if (portResultSet != null)
1849 portResultSet.close();
1850 }
1851 }
1852 switchResultSet.save();
1853 }
1854 finally {
1855 if (switchResultSet != null)
1856 switchResultSet.close();
1857 }
1858 }
1859
1860 protected void updateControllerInfo() {
1861 updateAllInactiveSwitchInfo();
1862
1863 // Write out the controller info to the storage source
1864 Map<String, Object> controllerInfo = new HashMap<String, Object>();
1865 String id = getControllerId();
1866 controllerInfo.put(CONTROLLER_ID, id);
1867 storageSource.updateRow(CONTROLLER_TABLE_NAME, controllerInfo);
1868 }
1869
1870 protected void updateActiveSwitchInfo(IOFSwitch sw) {
1871 if (role == Role.SLAVE) {
1872 return;
1873 }
1874 // Obtain the row info for the switch
1875 Map<String, Object> switchInfo = new HashMap<String, Object>();
1876 String datapathIdString = sw.getStringId();
1877 switchInfo.put(SWITCH_DATAPATH_ID, datapathIdString);
1878 String controllerId = getControllerId();
1879 switchInfo.put(SWITCH_CONTROLLER_ID, controllerId);
1880 Date connectedSince = sw.getConnectedSince();
1881 switchInfo.put(SWITCH_CONNECTED_SINCE, connectedSince);
1882 Channel channel = sw.getChannel();
1883 SocketAddress socketAddress = channel.getRemoteAddress();
1884 if (socketAddress != null) {
1885 String socketAddressString = socketAddress.toString();
1886 switchInfo.put(SWITCH_SOCKET_ADDRESS, socketAddressString);
1887 if (socketAddress instanceof InetSocketAddress) {
1888 InetSocketAddress inetSocketAddress =
1889 (InetSocketAddress)socketAddress;
1890 InetAddress inetAddress = inetSocketAddress.getAddress();
1891 String ip = inetAddress.getHostAddress();
1892 switchInfo.put(SWITCH_IP, ip);
1893 }
1894 }
1895
1896 // Write out the switch features info
1897 long capabilities = U32.f(sw.getCapabilities());
1898 switchInfo.put(SWITCH_CAPABILITIES, capabilities);
1899 long buffers = U32.f(sw.getBuffers());
1900 switchInfo.put(SWITCH_BUFFERS, buffers);
1901 long tables = U32.f(sw.getTables());
1902 switchInfo.put(SWITCH_TABLES, tables);
1903 long actions = U32.f(sw.getActions());
1904 switchInfo.put(SWITCH_ACTIONS, actions);
1905 switchInfo.put(SWITCH_ACTIVE, Boolean.TRUE);
1906
1907 // Update the switch
1908 storageSource.updateRowAsync(SWITCH_TABLE_NAME, switchInfo);
1909
1910 // Update the ports
1911 for (OFPhysicalPort port: sw.getPorts()) {
1912 updatePortInfo(sw, port);
1913 }
1914 }
1915
1916 protected void updateInactiveSwitchInfo(IOFSwitch sw) {
1917 if (role == Role.SLAVE) {
1918 return;
1919 }
1920 log.debug("Update DB with inactiveSW {}", sw);
1921 // Update the controller info in the storage source to be inactive
1922 Map<String, Object> switchInfo = new HashMap<String, Object>();
1923 String datapathIdString = sw.getStringId();
1924 switchInfo.put(SWITCH_DATAPATH_ID, datapathIdString);
1925 //switchInfo.put(SWITCH_CONNECTED_SINCE, null);
1926 switchInfo.put(SWITCH_ACTIVE, Boolean.FALSE);
1927 storageSource.updateRowAsync(SWITCH_TABLE_NAME, switchInfo);
1928 }
1929
1930 protected void updatePortInfo(IOFSwitch sw, OFPhysicalPort port) {
1931 if (role == Role.SLAVE) {
1932 return;
1933 }
1934 String datapathIdString = sw.getStringId();
1935 Map<String, Object> portInfo = new HashMap<String, Object>();
1936 int portNumber = U16.f(port.getPortNumber());
1937 String id = datapathIdString + "|" + portNumber;
1938 portInfo.put(PORT_ID, id);
1939 portInfo.put(PORT_SWITCH, datapathIdString);
1940 portInfo.put(PORT_NUMBER, portNumber);
1941 byte[] hardwareAddress = port.getHardwareAddress();
1942 String hardwareAddressString = HexString.toHexString(hardwareAddress);
1943 portInfo.put(PORT_HARDWARE_ADDRESS, hardwareAddressString);
1944 String name = port.getName();
1945 portInfo.put(PORT_NAME, name);
1946 long config = U32.f(port.getConfig());
1947 portInfo.put(PORT_CONFIG, config);
1948 long state = U32.f(port.getState());
1949 portInfo.put(PORT_STATE, state);
1950 long currentFeatures = U32.f(port.getCurrentFeatures());
1951 portInfo.put(PORT_CURRENT_FEATURES, currentFeatures);
1952 long advertisedFeatures = U32.f(port.getAdvertisedFeatures());
1953 portInfo.put(PORT_ADVERTISED_FEATURES, advertisedFeatures);
1954 long supportedFeatures = U32.f(port.getSupportedFeatures());
1955 portInfo.put(PORT_SUPPORTED_FEATURES, supportedFeatures);
1956 long peerFeatures = U32.f(port.getPeerFeatures());
1957 portInfo.put(PORT_PEER_FEATURES, peerFeatures);
1958 storageSource.updateRowAsync(PORT_TABLE_NAME, portInfo);
1959 }
1960
1961 /**
1962 * Read switch port data from storage and write it into a switch object
1963 * @param sw the switch to update
1964 */
1965 protected void readSwitchPortStateFromStorage(OFSwitchImpl sw) {
1966 OperatorPredicate op =
1967 new OperatorPredicate(PORT_SWITCH,
1968 OperatorPredicate.Operator.EQ,
1969 sw.getStringId());
1970 IResultSet portResultSet =
1971 storageSource.executeQuery(PORT_TABLE_NAME,
1972 null, op, null);
1973 //Map<Short, OFPhysicalPort> oldports =
1974 // new HashMap<Short, OFPhysicalPort>();
1975 //oldports.putAll(sw.getPorts());
1976
1977 while (portResultSet.next()) {
1978 try {
1979 OFPhysicalPort p = new OFPhysicalPort();
1980 p.setPortNumber((short)portResultSet.getInt(PORT_NUMBER));
1981 p.setName(portResultSet.getString(PORT_NAME));
1982 p.setConfig((int)portResultSet.getLong(PORT_CONFIG));
1983 p.setState((int)portResultSet.getLong(PORT_STATE));
1984 String portMac = portResultSet.getString(PORT_HARDWARE_ADDRESS);
1985 p.setHardwareAddress(HexString.fromHexString(portMac));
1986 p.setCurrentFeatures((int)portResultSet.
1987 getLong(PORT_CURRENT_FEATURES));
1988 p.setAdvertisedFeatures((int)portResultSet.
1989 getLong(PORT_ADVERTISED_FEATURES));
1990 p.setSupportedFeatures((int)portResultSet.
1991 getLong(PORT_SUPPORTED_FEATURES));
1992 p.setPeerFeatures((int)portResultSet.
1993 getLong(PORT_PEER_FEATURES));
1994 //oldports.remove(Short.valueOf(p.getPortNumber()));
1995 sw.setPort(p);
1996 } catch (NullPointerException e) {
1997 // ignore
1998 }
1999 }
2000 SwitchUpdate update = new SwitchUpdate(sw, SwitchUpdateType.PORTCHANGED);
2001 try {
2002 this.updates.put(update);
2003 } catch (InterruptedException e) {
2004 log.error("Failure adding update to queue", e);
2005 }
2006 }
2007
2008 protected void removePortInfo(IOFSwitch sw, short portNumber) {
2009 if (role == Role.SLAVE) {
2010 return;
2011 }
2012 String datapathIdString = sw.getStringId();
2013 String id = datapathIdString + "|" + portNumber;
2014 storageSource.deleteRowAsync(PORT_TABLE_NAME, id);
2015 }
2016
2017 /**
2018 * Sets the initial role based on properties in the config params.
2019 * It looks for two different properties.
2020 * If the "role" property is specified then the value should be
2021 * either "EQUAL", "MASTER", or "SLAVE" and the role of the
2022 * controller is set to the specified value. If the "role" property
2023 * is not specified then it looks next for the "role.path" property.
2024 * In this case the value should be the path to a property file in
2025 * the file system that contains a property called "floodlight.role"
2026 * which can be one of the values listed above for the "role" property.
2027 * The idea behind the "role.path" mechanism is that you have some
2028 * separate heartbeat and master controller election algorithm that
2029 * determines the role of the controller. When a role transition happens,
2030 * it updates the current role in the file specified by the "role.path"
2031 * file. Then if floodlight restarts for some reason it can get the
2032 * correct current role of the controller from the file.
2033 * @param configParams The config params for the FloodlightProvider service
2034 * @return A valid role if role information is specified in the
2035 * config params, otherwise null
2036 */
2037 @LogMessageDocs({
2038 @LogMessageDoc(message="Controller role set to {role}",
2039 explanation="Setting the initial HA role to "),
2040 @LogMessageDoc(level="ERROR",
2041 message="Invalid current role value: {role}",
2042 explanation="An invalid HA role value was read from the " +
2043 "properties file",
2044 recommendation=LogMessageDoc.CHECK_CONTROLLER)
2045 })
2046 protected Role getInitialRole(Map<String, String> configParams) {
2047 Role role = null;
2048 String roleString = configParams.get("role");
2049 if (roleString == null) {
2050 String rolePath = configParams.get("rolepath");
2051 if (rolePath != null) {
2052 Properties properties = new Properties();
2053 try {
2054 properties.load(new FileInputStream(rolePath));
2055 roleString = properties.getProperty("floodlight.role");
2056 }
2057 catch (IOException exc) {
2058 // Don't treat it as an error if the file specified by the
2059 // rolepath property doesn't exist. This lets us enable the
2060 // HA mechanism by just creating/setting the floodlight.role
2061 // property in that file without having to modify the
2062 // floodlight properties.
2063 }
2064 }
2065 }
2066
2067 if (roleString != null) {
2068 // Canonicalize the string to the form used for the enum constants
2069 roleString = roleString.trim().toUpperCase();
2070 try {
2071 role = Role.valueOf(roleString);
2072 }
2073 catch (IllegalArgumentException exc) {
2074 log.error("Invalid current role value: {}", roleString);
2075 }
2076 }
2077
2078 log.info("Controller role set to {}", role);
2079
2080 return role;
2081 }
2082
2083 /**
2084 * Tell controller that we're ready to accept switches loop
2085 * @throws IOException
2086 */
2087 @LogMessageDocs({
2088 @LogMessageDoc(message="Listening for switch connections on {address}",
2089 explanation="The controller is ready and listening for new" +
2090 " switch connections"),
2091 @LogMessageDoc(message="Storage exception in controller " +
2092 "updates loop; terminating process",
2093 explanation=ERROR_DATABASE,
2094 recommendation=LogMessageDoc.CHECK_CONTROLLER),
2095 @LogMessageDoc(level="ERROR",
2096 message="Exception in controller updates loop",
2097 explanation="Failed to dispatch controller event",
2098 recommendation=LogMessageDoc.GENERIC_ACTION)
2099 })
2100 public void run() {
2101 if (log.isDebugEnabled()) {
2102 logListeners();
2103 }
2104
2105 try {
2106 final ServerBootstrap bootstrap = createServerBootStrap();
2107
2108 bootstrap.setOption("reuseAddr", true);
2109 bootstrap.setOption("child.keepAlive", true);
2110 bootstrap.setOption("child.tcpNoDelay", true);
2111 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
2112
2113 ChannelPipelineFactory pfact =
2114 new OpenflowPipelineFactory(this, null);
2115 bootstrap.setPipelineFactory(pfact);
2116 InetSocketAddress sa = new InetSocketAddress(openFlowPort);
2117 final ChannelGroup cg = new DefaultChannelGroup();
2118 cg.add(bootstrap.bind(sa));
2119
2120 log.info("Listening for switch connections on {}", sa);
2121 } catch (Exception e) {
2122 throw new RuntimeException(e);
2123 }
2124
2125 // main loop
2126 while (true) {
2127 try {
2128 IUpdate update = updates.take();
2129 update.dispatch();
2130 } catch (InterruptedException e) {
2131 return;
2132 } catch (StorageException e) {
2133 log.error("Storage exception in controller " +
2134 "updates loop; terminating process", e);
2135 return;
2136 } catch (Exception e) {
2137 log.error("Exception in controller updates loop", e);
2138 }
2139 }
2140 }
2141
2142 private ServerBootstrap createServerBootStrap() {
2143 if (workerThreads == 0) {
2144 return new ServerBootstrap(
2145 new NioServerSocketChannelFactory(
2146 Executors.newCachedThreadPool(),
2147 Executors.newCachedThreadPool()));
2148 } else {
2149 return new ServerBootstrap(
2150 new NioServerSocketChannelFactory(
2151 Executors.newCachedThreadPool(),
2152 Executors.newCachedThreadPool(), workerThreads));
2153 }
2154 }
2155
2156 public void setConfigParams(Map<String, String> configParams) {
2157 String ofPort = configParams.get("openflowport");
2158 if (ofPort != null) {
2159 this.openFlowPort = Integer.parseInt(ofPort);
2160 }
2161 log.debug("OpenFlow port set to {}", this.openFlowPort);
2162 String threads = configParams.get("workerthreads");
2163 if (threads != null) {
2164 this.workerThreads = Integer.parseInt(threads);
2165 }
2166 log.debug("Number of worker threads set to {}", this.workerThreads);
2167 String controllerId = configParams.get("controllerid");
2168 if (controllerId != null) {
2169 this.controllerId = controllerId;
2170 }
Jonathan Hartd10008d2013-02-23 17:04:08 -08002171 else {
2172 //Try to get the hostname of the machine and use that for controller ID
2173 try {
2174 String hostname = java.net.InetAddress.getLocalHost().getHostName();
2175 this.controllerId = hostname;
2176 } catch (UnknownHostException e) {
2177 // Can't get hostname, we'll just use the default
2178 }
2179 }
2180
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08002181 log.debug("ControllerId set to {}", this.controllerId);
2182 }
2183
2184 private void initVendorMessages() {
2185 // Configure openflowj to be able to parse the role request/reply
2186 // vendor messages.
2187 OFBasicVendorId niciraVendorId = new OFBasicVendorId(
2188 OFNiciraVendorData.NX_VENDOR_ID, 4);
2189 OFVendorId.registerVendorId(niciraVendorId);
2190 OFBasicVendorDataType roleRequestVendorData =
2191 new OFBasicVendorDataType(
2192 OFRoleRequestVendorData.NXT_ROLE_REQUEST,
2193 OFRoleRequestVendorData.getInstantiable());
2194 niciraVendorId.registerVendorDataType(roleRequestVendorData);
2195 OFBasicVendorDataType roleReplyVendorData =
2196 new OFBasicVendorDataType(
2197 OFRoleReplyVendorData.NXT_ROLE_REPLY,
2198 OFRoleReplyVendorData.getInstantiable());
2199 niciraVendorId.registerVendorDataType(roleReplyVendorData);
2200 }
2201
2202 /**
2203 * Initialize internal data structures
2204 */
2205 public void init(Map<String, String> configParams) {
2206 // These data structures are initialized here because other
2207 // module's startUp() might be called before ours
2208 this.messageListeners =
2209 new ConcurrentHashMap<OFType,
2210 ListenerDispatcher<OFType,
2211 IOFMessageListener>>();
2212 this.switchListeners = new CopyOnWriteArraySet<IOFSwitchListener>();
2213 this.haListeners = new CopyOnWriteArraySet<IHAListener>();
2214 this.activeSwitches = new ConcurrentHashMap<Long, IOFSwitch>();
2215 this.connectedSwitches = new HashSet<OFSwitchImpl>();
2216 this.controllerNodeIPsCache = new HashMap<String, String>();
2217 this.updates = new LinkedBlockingQueue<IUpdate>();
2218 this.factory = new BasicFactory();
2219 this.providerMap = new HashMap<String, List<IInfoProvider>>();
2220 setConfigParams(configParams);
Jonathan Hartcc957a02013-02-26 10:39:04 -08002221 //this.role = getInitialRole(configParams);
2222 //Set the controller's role to MASTER so it always tries to do role requests.
2223 this.role = Role.MASTER;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08002224 this.roleChanger = new RoleChanger();
2225 initVendorMessages();
2226 this.systemStartTime = System.currentTimeMillis();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08002227 }
2228
2229 /**
2230 * Startup all of the controller's components
2231 */
2232 @LogMessageDoc(message="Waiting for storage source",
2233 explanation="The system database is not yet ready",
2234 recommendation="If this message persists, this indicates " +
2235 "that the system database has failed to start. " +
2236 LogMessageDoc.CHECK_CONTROLLER)
2237 public void startupComponents() {
Jonathan Hartd10008d2013-02-23 17:04:08 -08002238 try {
2239 registryService.registerController(controllerId);
2240 } catch (RegistryException e2) {
2241 log.warn("Registry service error: {}", e2.getMessage());
2242 }
2243
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08002244 // Create the table names we use
2245 storageSource.createTable(CONTROLLER_TABLE_NAME, null);
2246 storageSource.createTable(SWITCH_TABLE_NAME, null);
2247 storageSource.createTable(PORT_TABLE_NAME, null);
2248 storageSource.createTable(CONTROLLER_INTERFACE_TABLE_NAME, null);
2249 storageSource.createTable(SWITCH_CONFIG_TABLE_NAME, null);
2250 storageSource.setTablePrimaryKeyName(CONTROLLER_TABLE_NAME,
2251 CONTROLLER_ID);
2252 storageSource.setTablePrimaryKeyName(SWITCH_TABLE_NAME,
2253 SWITCH_DATAPATH_ID);
2254 storageSource.setTablePrimaryKeyName(PORT_TABLE_NAME, PORT_ID);
2255 storageSource.setTablePrimaryKeyName(CONTROLLER_INTERFACE_TABLE_NAME,
2256 CONTROLLER_INTERFACE_ID);
2257 storageSource.addListener(CONTROLLER_INTERFACE_TABLE_NAME, this);
2258
2259 while (true) {
2260 try {
2261 updateControllerInfo();
2262 break;
2263 }
2264 catch (StorageException e) {
2265 log.info("Waiting for storage source");
2266 try {
2267 Thread.sleep(1000);
2268 } catch (InterruptedException e1) {
2269 }
2270 }
2271 }
2272
2273 // Add our REST API
2274 restApi.addRestletRoutable(new CoreWebRoutable());
2275 }
2276
2277 @Override
2278 public void addInfoProvider(String type, IInfoProvider provider) {
2279 if (!providerMap.containsKey(type)) {
2280 providerMap.put(type, new ArrayList<IInfoProvider>());
2281 }
2282 providerMap.get(type).add(provider);
2283 }
2284
2285 @Override
2286 public void removeInfoProvider(String type, IInfoProvider provider) {
2287 if (!providerMap.containsKey(type)) {
2288 log.debug("Provider type {} doesn't exist.", type);
2289 return;
2290 }
2291
2292 providerMap.get(type).remove(provider);
2293 }
2294
2295 public Map<String, Object> getControllerInfo(String type) {
2296 if (!providerMap.containsKey(type)) return null;
2297
2298 Map<String, Object> result = new LinkedHashMap<String, Object>();
2299 for (IInfoProvider provider : providerMap.get(type)) {
2300 result.putAll(provider.getInfo(type));
2301 }
2302
2303 return result;
2304 }
2305
2306 @Override
2307 public void addHAListener(IHAListener listener) {
2308 this.haListeners.add(listener);
2309 }
2310
2311 @Override
2312 public void removeHAListener(IHAListener listener) {
2313 this.haListeners.remove(listener);
2314 }
2315
2316
2317 /**
2318 * Handle changes to the controller nodes IPs and dispatch update.
2319 */
2320 @SuppressWarnings("unchecked")
2321 protected void handleControllerNodeIPChanges() {
2322 HashMap<String,String> curControllerNodeIPs = new HashMap<String,String>();
2323 HashMap<String,String> addedControllerNodeIPs = new HashMap<String,String>();
2324 HashMap<String,String> removedControllerNodeIPs =new HashMap<String,String>();
2325 String[] colNames = { CONTROLLER_INTERFACE_CONTROLLER_ID,
2326 CONTROLLER_INTERFACE_TYPE,
2327 CONTROLLER_INTERFACE_NUMBER,
2328 CONTROLLER_INTERFACE_DISCOVERED_IP };
2329 synchronized(controllerNodeIPsCache) {
2330 // We currently assume that interface Ethernet0 is the relevant
2331 // controller interface. Might change.
2332 // We could (should?) implement this using
2333 // predicates, but creating the individual and compound predicate
2334 // seems more overhead then just checking every row. Particularly,
2335 // since the number of rows is small and changes infrequent
2336 IResultSet res = storageSource.executeQuery(CONTROLLER_INTERFACE_TABLE_NAME,
2337 colNames,null, null);
2338 while (res.next()) {
2339 if (res.getString(CONTROLLER_INTERFACE_TYPE).equals("Ethernet") &&
2340 res.getInt(CONTROLLER_INTERFACE_NUMBER) == 0) {
2341 String controllerID = res.getString(CONTROLLER_INTERFACE_CONTROLLER_ID);
2342 String discoveredIP = res.getString(CONTROLLER_INTERFACE_DISCOVERED_IP);
2343 String curIP = controllerNodeIPsCache.get(controllerID);
2344
2345 curControllerNodeIPs.put(controllerID, discoveredIP);
2346 if (curIP == null) {
2347 // new controller node IP
2348 addedControllerNodeIPs.put(controllerID, discoveredIP);
2349 }
2350 else if (curIP != discoveredIP) {
2351 // IP changed
2352 removedControllerNodeIPs.put(controllerID, curIP);
2353 addedControllerNodeIPs.put(controllerID, discoveredIP);
2354 }
2355 }
2356 }
2357 // Now figure out if rows have been deleted. We can't use the
2358 // rowKeys from rowsDeleted directly, since the tables primary
2359 // key is a compound that we can't disassemble
2360 Set<String> curEntries = curControllerNodeIPs.keySet();
2361 Set<String> removedEntries = controllerNodeIPsCache.keySet();
2362 removedEntries.removeAll(curEntries);
2363 for (String removedControllerID : removedEntries)
2364 removedControllerNodeIPs.put(removedControllerID, controllerNodeIPsCache.get(removedControllerID));
2365 controllerNodeIPsCache = (HashMap<String, String>) curControllerNodeIPs.clone();
2366 HAControllerNodeIPUpdate update = new HAControllerNodeIPUpdate(
2367 curControllerNodeIPs, addedControllerNodeIPs,
2368 removedControllerNodeIPs);
2369 if (!removedControllerNodeIPs.isEmpty() || !addedControllerNodeIPs.isEmpty()) {
2370 try {
2371 this.updates.put(update);
2372 } catch (InterruptedException e) {
2373 log.error("Failure adding update to queue", e);
2374 }
2375 }
2376 }
2377 }
2378
2379 @Override
2380 public Map<String, String> getControllerNodeIPs() {
2381 // We return a copy of the mapping so we can guarantee that
2382 // the mapping return is the same as one that will be (or was)
2383 // dispatched to IHAListeners
2384 HashMap<String,String> retval = new HashMap<String,String>();
2385 synchronized(controllerNodeIPsCache) {
2386 retval.putAll(controllerNodeIPsCache);
2387 }
2388 return retval;
2389 }
2390
2391 @Override
2392 public void rowsModified(String tableName, Set<Object> rowKeys) {
2393 if (tableName.equals(CONTROLLER_INTERFACE_TABLE_NAME)) {
2394 handleControllerNodeIPChanges();
2395 }
2396
2397 }
2398
2399 @Override
2400 public void rowsDeleted(String tableName, Set<Object> rowKeys) {
2401 if (tableName.equals(CONTROLLER_INTERFACE_TABLE_NAME)) {
2402 handleControllerNodeIPChanges();
2403 }
2404 }
2405
2406 @Override
2407 public long getSystemStartTime() {
2408 return (this.systemStartTime);
2409 }
2410
2411 @Override
2412 public void setAlwaysClearFlowsOnSwAdd(boolean value) {
2413 this.alwaysClearFlowsOnSwAdd = value;
2414 }
2415
2416 public boolean getAlwaysClearFlowsOnSwAdd() {
2417 return this.alwaysClearFlowsOnSwAdd;
2418 }
2419}