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 | |
tom | 9ccd781 | 2014-08-25 22:43:19 -0700 | [diff] [blame] | 18 | package org.onlab.onos.of.controller.impl.internal; |
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 | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 33 | import org.onlab.onos.of.controller.impl.annotations.LogMessageDoc; |
| 34 | import org.onlab.onos.of.controller.impl.annotations.LogMessageDocs; |
| 35 | import org.onlab.onos.of.controller.impl.internal.OpenFlowControllerImpl.OpenFlowSwitchAgent; |
alshabib | d777b20 | 2014-08-28 17:52:55 -0700 | [diff] [blame^] | 36 | import org.onlab.onos.of.drivers.DriverManager; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 37 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 38 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 39 | import org.projectfloodlight.openflow.protocol.OFFactory; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 40 | import org.projectfloodlight.openflow.protocol.OFVersion; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 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 | */ |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 49 | public class Controller { |
| 50 | |
| 51 | protected static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 52 | static final String ERROR_DATABASE = |
| 53 | "The controller could not communicate with the system database."; |
| 54 | protected static final OFFactory FACTORY13 = OFFactories.getFactory(OFVersion.OF_13); |
| 55 | protected static final OFFactory FACTORY10 = OFFactories.getFactory(OFVersion.OF_10); |
| 56 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 57 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 58 | |
| 59 | // The controllerNodeIPsCache maps Controller IDs to their IP address. |
| 60 | // It's only used by handleControllerNodeIPsChanged |
| 61 | protected HashMap<String, String> controllerNodeIPsCache; |
| 62 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 63 | |
| 64 | private IOFSwitchManager switchManager; |
| 65 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 66 | private ChannelGroup cg; |
| 67 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 68 | // Configuration options |
| 69 | protected int openFlowPort = 6633; |
| 70 | protected int workerThreads = 0; |
| 71 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 72 | // Start time of the controller |
| 73 | protected long systemStartTime; |
| 74 | |
| 75 | // Flag to always flush flow table on switch reconnect (HA or otherwise) |
| 76 | protected boolean alwaysClearFlowsOnSwAdd = false; |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 77 | private OpenFlowSwitchAgent agent; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 78 | |
| 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({ |
| 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) |
| 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() { |
| 160 | if (workerThreads == 0) { |
| 161 | return new ServerBootstrap( |
| 162 | new NioServerSocketChannelFactory( |
| 163 | Executors.newCachedThreadPool(), |
| 164 | Executors.newCachedThreadPool())); |
| 165 | } else { |
| 166 | return new ServerBootstrap( |
| 167 | new NioServerSocketChannelFactory( |
| 168 | Executors.newCachedThreadPool(), |
| 169 | Executors.newCachedThreadPool(), workerThreads)); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | public void setConfigParams(Map<String, String> configParams) { |
| 174 | String ofPort = configParams.get("openflowport"); |
| 175 | if (ofPort != null) { |
| 176 | this.openFlowPort = Integer.parseInt(ofPort); |
| 177 | } |
| 178 | log.debug("OpenFlow port set to {}", this.openFlowPort); |
| 179 | String threads = configParams.get("workerthreads"); |
| 180 | if (threads != null) { |
| 181 | this.workerThreads = Integer.parseInt(threads); |
| 182 | } |
| 183 | log.debug("Number of worker threads set to {}", this.workerThreads); |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * Initialize internal data structures. |
| 189 | */ |
| 190 | public void init(Map<String, String> configParams) { |
| 191 | // These data structures are initialized here because other |
| 192 | // module's startUp() might be called before ours |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 193 | this.controllerNodeIPsCache = new HashMap<String, String>(); |
| 194 | |
| 195 | setConfigParams(configParams); |
| 196 | this.systemStartTime = System.currentTimeMillis(); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 197 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 198 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 199 | } |
| 200 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 201 | // ************** |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 202 | // Utility methods |
| 203 | // ************** |
| 204 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 205 | public Map<String, Long> getMemory() { |
| 206 | Map<String, Long> m = new HashMap<String, Long>(); |
| 207 | Runtime runtime = Runtime.getRuntime(); |
| 208 | m.put("total", runtime.totalMemory()); |
| 209 | m.put("free", runtime.freeMemory()); |
| 210 | return m; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | public Long getUptime() { |
| 215 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 216 | return rb.getUptime(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Forward to the driver-manager to get an IOFSwitch instance. |
| 221 | * @param desc |
tom | 9ccd781 | 2014-08-25 22:43:19 -0700 | [diff] [blame] | 222 | * @return switch instance |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 223 | */ |
alshabib | d777b20 | 2014-08-28 17:52:55 -0700 | [diff] [blame^] | 224 | protected AbstractOpenFlowSwitch getOFSwitchInstance(long dpid, |
| 225 | OFDescStatsReply desc, OFVersion ofv) { |
| 226 | AbstractOpenFlowSwitch sw = DriverManager.getOFSwitchImpl(new Dpid(dpid), |
| 227 | desc, ofv); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 228 | sw.setAgent(agent); |
| 229 | return sw; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 230 | } |
| 231 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 232 | public void start(OpenFlowSwitchAgent ag) { |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 233 | log.info("Initialising OpenFlow Lib and IO"); |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 234 | this.agent = ag; |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 235 | this.init(new HashMap<String, String>()); |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 236 | this.run(); |
| 237 | } |
| 238 | |
alshabib | 54ebd9c | 2014-08-27 18:38:41 -0700 | [diff] [blame] | 239 | |
| 240 | public void stop() { |
| 241 | cg.close(); |
| 242 | } |
| 243 | |
alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 244 | } |