Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 16 | |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 17 | package org.onlab.onos.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
Yuta HIGUCHI | f845cc3 | 2014-11-25 19:28:12 -0800 | [diff] [blame] | 19 | import static org.onlab.util.Tools.namedThreads; |
| 20 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 21 | import java.lang.management.ManagementFactory; |
| 22 | import java.lang.management.RuntimeMXBean; |
| 23 | import java.net.InetSocketAddress; |
| 24 | import java.util.HashMap; |
| 25 | import java.util.Map; |
| 26 | import java.util.concurrent.Executors; |
| 27 | |
| 28 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 29 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 30 | import org.jboss.netty.channel.group.ChannelGroup; |
| 31 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 32 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 33 | import org.onlab.onos.openflow.controller.Dpid; |
| 34 | import org.onlab.onos.openflow.controller.driver.OpenFlowAgent; |
| 35 | import org.onlab.onos.openflow.controller.driver.OpenFlowSwitchDriver; |
alshabib | 452234e | 2014-11-25 00:03:49 -0500 | [diff] [blame] | 36 | import org.onlab.onos.openflow.drivers.DriverManager; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 38 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 39 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 40 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 41 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * The main controller class. Handles all setup and network listeners |
| 47 | * - Distributed ownership control of switch through IControllerRegistryService |
| 48 | */ |
| 49 | public class Controller { |
| 50 | |
| 51 | protected static final Logger log = LoggerFactory.getLogger(Controller.class); |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 52 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 53 | protected static final OFFactory FACTORY13 = OFFactories.getFactory(OFVersion.OF_13); |
| 54 | protected static final OFFactory FACTORY10 = OFFactories.getFactory(OFVersion.OF_10); |
| 55 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 56 | protected HashMap<String, String> controllerNodeIPsCache; |
| 57 | |
| 58 | private ChannelGroup cg; |
| 59 | |
| 60 | // Configuration options |
| 61 | protected int openFlowPort = 6633; |
| 62 | protected int workerThreads = 0; |
| 63 | |
| 64 | // Start time of the controller |
| 65 | protected long systemStartTime; |
| 66 | |
| 67 | private OpenFlowAgent agent; |
| 68 | |
| 69 | private NioServerSocketChannelFactory execFactory; |
| 70 | |
| 71 | // Perf. related configuration |
| 72 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
| 73 | |
| 74 | // *************** |
| 75 | // Getters/Setters |
| 76 | // *************** |
| 77 | |
| 78 | public OFFactory getOFMessageFactory10() { |
| 79 | return FACTORY10; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | public OFFactory getOFMessageFactory13() { |
| 84 | return FACTORY13; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | public Map<String, String> getControllerNodeIPs() { |
| 90 | // We return a copy of the mapping so we can guarantee that |
| 91 | // the mapping return is the same as one that will be (or was) |
| 92 | // dispatched to IHAListeners |
| 93 | HashMap<String, String> retval = new HashMap<String, String>(); |
| 94 | synchronized (controllerNodeIPsCache) { |
| 95 | retval.putAll(controllerNodeIPsCache); |
| 96 | } |
| 97 | return retval; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | public long getSystemStartTime() { |
| 102 | return (this.systemStartTime); |
| 103 | } |
| 104 | |
| 105 | // ************** |
| 106 | // Initialization |
| 107 | // ************** |
| 108 | |
| 109 | /** |
| 110 | * Tell controller that we're ready to accept switches loop. |
| 111 | */ |
| 112 | public void run() { |
| 113 | |
| 114 | try { |
| 115 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 116 | |
| 117 | bootstrap.setOption("reuseAddr", true); |
| 118 | bootstrap.setOption("child.keepAlive", true); |
| 119 | bootstrap.setOption("child.tcpNoDelay", true); |
| 120 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 121 | |
| 122 | ChannelPipelineFactory pfact = |
| 123 | new OpenflowPipelineFactory(this, null); |
| 124 | bootstrap.setPipelineFactory(pfact); |
| 125 | InetSocketAddress sa = new InetSocketAddress(openFlowPort); |
| 126 | cg = new DefaultChannelGroup(); |
| 127 | cg.add(bootstrap.bind(sa)); |
| 128 | |
| 129 | log.info("Listening for switch connections on {}", sa); |
| 130 | } catch (Exception e) { |
| 131 | throw new RuntimeException(e); |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |
| 136 | private ServerBootstrap createServerBootStrap() { |
| 137 | |
| 138 | if (workerThreads == 0) { |
| 139 | execFactory = new NioServerSocketChannelFactory( |
Yuta HIGUCHI | f845cc3 | 2014-11-25 19:28:12 -0800 | [diff] [blame] | 140 | Executors.newCachedThreadPool(namedThreads("Controller-boss-%d")), |
| 141 | Executors.newCachedThreadPool(namedThreads("Controller-worker-%d"))); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 142 | return new ServerBootstrap(execFactory); |
| 143 | } else { |
| 144 | execFactory = new NioServerSocketChannelFactory( |
Yuta HIGUCHI | f845cc3 | 2014-11-25 19:28:12 -0800 | [diff] [blame] | 145 | Executors.newCachedThreadPool(namedThreads("Controller-boss-%d")), |
| 146 | Executors.newCachedThreadPool(namedThreads("Controller-worker-%d")), workerThreads); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 147 | return new ServerBootstrap(execFactory); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public void setConfigParams(Map<String, String> configParams) { |
| 152 | String ofPort = configParams.get("openflowport"); |
| 153 | if (ofPort != null) { |
| 154 | this.openFlowPort = Integer.parseInt(ofPort); |
| 155 | } |
| 156 | log.debug("OpenFlow port set to {}", this.openFlowPort); |
| 157 | String threads = configParams.get("workerthreads"); |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 158 | this.workerThreads = threads != null ? Integer.parseInt(threads) : 16; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 159 | log.debug("Number of worker threads set to {}", this.workerThreads); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * Initialize internal data structures. |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 165 | * @param configParams configuration parameters |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 166 | */ |
| 167 | public void init(Map<String, String> configParams) { |
| 168 | // These data structures are initialized here because other |
| 169 | // module's startUp() might be called before ours |
| 170 | this.controllerNodeIPsCache = new HashMap<String, String>(); |
| 171 | |
| 172 | setConfigParams(configParams); |
| 173 | this.systemStartTime = System.currentTimeMillis(); |
| 174 | |
| 175 | |
| 176 | } |
| 177 | |
| 178 | // ************** |
| 179 | // Utility methods |
| 180 | // ************** |
| 181 | |
| 182 | public Map<String, Long> getMemory() { |
| 183 | Map<String, Long> m = new HashMap<String, Long>(); |
| 184 | Runtime runtime = Runtime.getRuntime(); |
| 185 | m.put("total", runtime.totalMemory()); |
| 186 | m.put("free", runtime.freeMemory()); |
| 187 | return m; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | public Long getUptime() { |
| 192 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 193 | return rb.getUptime(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Forward to the driver-manager to get an IOFSwitch instance. |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 198 | * @param dpid data path id |
| 199 | * @param desc switch description |
| 200 | * @param ofv OpenFlow version |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 201 | * @return switch instance |
| 202 | */ |
| 203 | protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid, |
| 204 | OFDescStatsReply desc, OFVersion ofv) { |
| 205 | OpenFlowSwitchDriver sw = DriverManager.getSwitch(new Dpid(dpid), |
alshabib | 452234e | 2014-11-25 00:03:49 -0500 | [diff] [blame] | 206 | desc, ofv); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 207 | sw.setAgent(agent); |
| 208 | sw.setRoleHandler(new RoleManager(sw)); |
| 209 | return sw; |
| 210 | } |
| 211 | |
| 212 | public void start(OpenFlowAgent ag) { |
| 213 | log.info("Starting OpenFlow IO"); |
| 214 | this.agent = ag; |
| 215 | this.init(new HashMap<String, String>()); |
| 216 | this.run(); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | public void stop() { |
| 221 | log.info("Stopping OpenFlow IO"); |
| 222 | execFactory.shutdown(); |
| 223 | cg.close(); |
| 224 | } |
| 225 | |
| 226 | } |