alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 1 | /** |
| 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 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 18 | package org.onlab.onos.of.controller.impl; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 19 | |
| 20 | import java.lang.management.ManagementFactory; |
| 21 | import java.lang.management.RuntimeMXBean; |
| 22 | import java.net.InetSocketAddress; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 23 | import java.util.HashMap; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 24 | import java.util.Map; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 25 | import java.util.concurrent.Executors; |
| 26 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 27 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 28 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 29 | import org.jboss.netty.channel.group.ChannelGroup; |
| 30 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 31 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
alshabib | d777b20 | 2014-08-28 17:52:55 -0700 | [diff] [blame] | 32 | import org.onlab.onos.of.controller.Dpid; |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 33 | import org.onlab.onos.of.controller.driver.OpenFlowAgent; |
| 34 | import org.onlab.onos.of.controller.driver.OpenFlowSwitchDriver; |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 35 | import org.onlab.onos.of.controller.impl.annotations.LogMessageDoc; |
| 36 | import org.onlab.onos.of.controller.impl.annotations.LogMessageDocs; |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 37 | import org.onlab.onos.of.drivers.impl.DriverManager; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 38 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 39 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 40 | import org.projectfloodlight.openflow.protocol.OFFactory; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.protocol.OFVersion; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * The main controller class. Handles all setup and network listeners |
| 48 | * - Distributed ownership control of switch through IControllerRegistryService |
| 49 | */ |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 50 | public class Controller { |
| 51 | |
| 52 | protected static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 53 | static final String ERROR_DATABASE = |
| 54 | "The controller could not communicate with the system database."; |
| 55 | protected static final OFFactory FACTORY13 = OFFactories.getFactory(OFVersion.OF_13); |
| 56 | protected static final OFFactory FACTORY10 = OFFactories.getFactory(OFVersion.OF_10); |
| 57 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 58 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 59 | |
| 60 | // The controllerNodeIPsCache maps Controller IDs to their IP address. |
| 61 | // It's only used by handleControllerNodeIPsChanged |
| 62 | protected HashMap<String, String> controllerNodeIPsCache; |
| 63 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 64 | private ChannelGroup cg; |
| 65 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 66 | // Configuration options |
| 67 | protected int openFlowPort = 6633; |
| 68 | protected int workerThreads = 0; |
| 69 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 70 | // Start time of the controller |
| 71 | protected long systemStartTime; |
| 72 | |
| 73 | // Flag to always flush flow table on switch reconnect (HA or otherwise) |
| 74 | protected boolean alwaysClearFlowsOnSwAdd = false; |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 75 | private OpenFlowAgent agent; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 76 | |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 77 | private NioServerSocketChannelFactory execFactory; |
| 78 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 79 | // Perf. related configuration |
| 80 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
| 81 | protected static final int BATCH_MAX_SIZE = 100; |
| 82 | protected static final boolean ALWAYS_DECODE_ETH = true; |
| 83 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 84 | // *************** |
| 85 | // Getters/Setters |
| 86 | // *************** |
| 87 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 88 | public OFFactory getOFMessageFactory10() { |
| 89 | return FACTORY10; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | public OFFactory getOFMessageFactory13() { |
| 94 | return FACTORY13; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | public Map<String, String> getControllerNodeIPs() { |
| 100 | // We return a copy of the mapping so we can guarantee that |
| 101 | // the mapping return is the same as one that will be (or was) |
| 102 | // dispatched to IHAListeners |
| 103 | HashMap<String, String> retval = new HashMap<String, String>(); |
| 104 | synchronized (controllerNodeIPsCache) { |
| 105 | retval.putAll(controllerNodeIPsCache); |
| 106 | } |
| 107 | return retval; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | public long getSystemStartTime() { |
| 112 | return (this.systemStartTime); |
| 113 | } |
| 114 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 115 | // ************** |
| 116 | // Initialization |
| 117 | // ************** |
| 118 | |
| 119 | /** |
| 120 | * Tell controller that we're ready to accept switches loop. |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 121 | */ |
| 122 | @LogMessageDocs({ |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 123 | @LogMessageDoc(message = "Listening for switch connections on {address}", |
| 124 | explanation = "The controller is ready and listening for new" + |
| 125 | " switch connections"), |
| 126 | @LogMessageDoc(message = "Storage exception in controller " + |
| 127 | "updates loop; terminating process", |
| 128 | explanation = ERROR_DATABASE, |
| 129 | recommendation = LogMessageDoc.CHECK_CONTROLLER), |
| 130 | @LogMessageDoc(level = "ERROR", |
| 131 | message = "Exception in controller updates loop", |
| 132 | explanation = "Failed to dispatch controller event", |
| 133 | recommendation = LogMessageDoc.GENERIC_ACTION) |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 134 | }) |
| 135 | public void run() { |
| 136 | |
| 137 | try { |
| 138 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 139 | |
| 140 | bootstrap.setOption("reuseAddr", true); |
| 141 | bootstrap.setOption("child.keepAlive", true); |
| 142 | bootstrap.setOption("child.tcpNoDelay", true); |
| 143 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 144 | |
| 145 | ChannelPipelineFactory pfact = |
| 146 | new OpenflowPipelineFactory(this, null); |
| 147 | bootstrap.setPipelineFactory(pfact); |
| 148 | InetSocketAddress sa = new InetSocketAddress(openFlowPort); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 149 | cg = new DefaultChannelGroup(); |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 150 | cg.add(bootstrap.bind(sa)); |
| 151 | |
| 152 | log.info("Listening for switch connections on {}", sa); |
| 153 | } catch (Exception e) { |
| 154 | throw new RuntimeException(e); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | private ServerBootstrap createServerBootStrap() { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 160 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 161 | if (workerThreads == 0) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 162 | execFactory = new NioServerSocketChannelFactory( |
| 163 | Executors.newCachedThreadPool(), |
| 164 | Executors.newCachedThreadPool()); |
| 165 | return new ServerBootstrap(execFactory); |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 166 | } else { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 167 | execFactory = new NioServerSocketChannelFactory( |
| 168 | Executors.newCachedThreadPool(), |
| 169 | Executors.newCachedThreadPool(), workerThreads); |
| 170 | return new ServerBootstrap(execFactory); |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | public void setConfigParams(Map<String, String> configParams) { |
| 175 | String ofPort = configParams.get("openflowport"); |
| 176 | if (ofPort != null) { |
| 177 | this.openFlowPort = Integer.parseInt(ofPort); |
| 178 | } |
| 179 | log.debug("OpenFlow port set to {}", this.openFlowPort); |
| 180 | String threads = configParams.get("workerthreads"); |
| 181 | if (threads != null) { |
| 182 | this.workerThreads = Integer.parseInt(threads); |
| 183 | } |
| 184 | log.debug("Number of worker threads set to {}", this.workerThreads); |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
| 188 | /** |
| 189 | * Initialize internal data structures. |
| 190 | */ |
| 191 | public void init(Map<String, String> configParams) { |
| 192 | // These data structures are initialized here because other |
| 193 | // module's startUp() might be called before ours |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 194 | this.controllerNodeIPsCache = new HashMap<String, String>(); |
| 195 | |
| 196 | setConfigParams(configParams); |
| 197 | this.systemStartTime = System.currentTimeMillis(); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 198 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 199 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 200 | } |
| 201 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 202 | // ************** |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 203 | // Utility methods |
| 204 | // ************** |
| 205 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 206 | public Map<String, Long> getMemory() { |
| 207 | Map<String, Long> m = new HashMap<String, Long>(); |
| 208 | Runtime runtime = Runtime.getRuntime(); |
| 209 | m.put("total", runtime.totalMemory()); |
| 210 | m.put("free", runtime.freeMemory()); |
| 211 | return m; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | public Long getUptime() { |
| 216 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 217 | return rb.getUptime(); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Forward to the driver-manager to get an IOFSwitch instance. |
| 222 | * @param desc |
tom | 9ccd781 | 2014-08-25 22:43:19 -0700 | [diff] [blame] | 223 | * @return switch instance |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 224 | */ |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 225 | protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid, |
alshabib | d777b20 | 2014-08-28 17:52:55 -0700 | [diff] [blame] | 226 | OFDescStatsReply desc, OFVersion ofv) { |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 227 | OpenFlowSwitchDriver sw = DriverManager.getSwitch(new Dpid(dpid), |
alshabib | d777b20 | 2014-08-28 17:52:55 -0700 | [diff] [blame] | 228 | desc, ofv); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 229 | sw.setAgent(agent); |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 230 | sw.setRoleHandler(new RoleManager(sw)); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 231 | return sw; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 232 | } |
| 233 | |
alshabib | 6171f18 | 2014-09-02 19:00:32 -0700 | [diff] [blame] | 234 | public void start(OpenFlowAgent ag) { |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 235 | log.info("Initialising OpenFlow Lib and IO"); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 236 | this.agent = ag; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 237 | this.init(new HashMap<String, String>()); |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 238 | this.run(); |
| 239 | } |
| 240 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 241 | |
| 242 | public void stop() { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 243 | execFactory.shutdown(); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 244 | cg.close(); |
| 245 | } |
| 246 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 247 | } |