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