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