Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 18 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 19 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 20 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 21 | import org.jboss.netty.channel.group.ChannelGroup; |
| 22 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 23 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 24 | import org.onosproject.net.driver.DefaultDriverData; |
| 25 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 26 | import org.onosproject.net.driver.Driver; |
| 27 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.openflow.controller.Dpid; |
| 29 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
| 30 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 31 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
| 32 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 33 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 34 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 38 | import java.lang.management.ManagementFactory; |
| 39 | import java.lang.management.RuntimeMXBean; |
| 40 | import java.net.InetSocketAddress; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.Map; |
| 43 | import java.util.concurrent.Executors; |
| 44 | |
| 45 | import static org.onlab.util.Tools.groupedThreads; |
| 46 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * The main controller class. Handles all setup and network listeners |
| 50 | * - Distributed ownership control of switch through IControllerRegistryService |
| 51 | */ |
| 52 | public class Controller { |
| 53 | |
| 54 | protected static final Logger log = LoggerFactory.getLogger(Controller.class); |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 55 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 56 | protected static final OFFactory FACTORY13 = OFFactories.getFactory(OFVersion.OF_13); |
| 57 | protected static final OFFactory FACTORY10 = OFFactories.getFactory(OFVersion.OF_10); |
| 58 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 59 | protected HashMap<String, String> controllerNodeIPsCache; |
| 60 | |
| 61 | private ChannelGroup cg; |
| 62 | |
| 63 | // Configuration options |
| 64 | protected int openFlowPort = 6633; |
| 65 | protected int workerThreads = 0; |
| 66 | |
| 67 | // Start time of the controller |
| 68 | protected long systemStartTime; |
| 69 | |
| 70 | private OpenFlowAgent agent; |
| 71 | |
| 72 | private NioServerSocketChannelFactory execFactory; |
| 73 | |
| 74 | // Perf. related configuration |
| 75 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 76 | private DriverService driverService; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 77 | |
| 78 | // *************** |
| 79 | // Getters/Setters |
| 80 | // *************** |
| 81 | |
| 82 | public OFFactory getOFMessageFactory10() { |
| 83 | return FACTORY10; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | public OFFactory getOFMessageFactory13() { |
| 88 | return FACTORY13; |
| 89 | } |
| 90 | |
| 91 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 92 | public Map<String, String> getControllerNodeIPs() { |
| 93 | // We return a copy of the mapping so we can guarantee that |
| 94 | // the mapping return is the same as one that will be (or was) |
| 95 | // dispatched to IHAListeners |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 96 | HashMap<String, String> retval = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 97 | synchronized (controllerNodeIPsCache) { |
| 98 | retval.putAll(controllerNodeIPsCache); |
| 99 | } |
| 100 | return retval; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | public long getSystemStartTime() { |
| 105 | return (this.systemStartTime); |
| 106 | } |
| 107 | |
| 108 | // ************** |
| 109 | // Initialization |
| 110 | // ************** |
| 111 | |
| 112 | /** |
| 113 | * Tell controller that we're ready to accept switches loop. |
| 114 | */ |
| 115 | public void run() { |
| 116 | |
| 117 | try { |
| 118 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 119 | |
| 120 | bootstrap.setOption("reuseAddr", true); |
| 121 | bootstrap.setOption("child.keepAlive", true); |
| 122 | bootstrap.setOption("child.tcpNoDelay", true); |
| 123 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 124 | |
| 125 | ChannelPipelineFactory pfact = |
| 126 | new OpenflowPipelineFactory(this, null); |
| 127 | bootstrap.setPipelineFactory(pfact); |
| 128 | InetSocketAddress sa = new InetSocketAddress(openFlowPort); |
| 129 | cg = new DefaultChannelGroup(); |
| 130 | cg.add(bootstrap.bind(sa)); |
| 131 | |
| 132 | log.info("Listening for switch connections on {}", sa); |
| 133 | } catch (Exception e) { |
| 134 | throw new RuntimeException(e); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | |
| 139 | private ServerBootstrap createServerBootStrap() { |
| 140 | |
| 141 | if (workerThreads == 0) { |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 142 | execFactory = new NioServerSocketChannelFactory( |
| 143 | Executors.newCachedThreadPool(groupedThreads("onos/of", "boss-%d")), |
| 144 | Executors.newCachedThreadPool(groupedThreads("onos/of", "worker-%d"))); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 145 | return new ServerBootstrap(execFactory); |
| 146 | } else { |
| 147 | execFactory = new NioServerSocketChannelFactory( |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 148 | Executors.newCachedThreadPool(groupedThreads("onos/of", "boss-%d")), |
| 149 | Executors.newCachedThreadPool(groupedThreads("onos/of", "worker-%d")), workerThreads); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 150 | return new ServerBootstrap(execFactory); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | public void setConfigParams(Map<String, String> configParams) { |
| 155 | String ofPort = configParams.get("openflowport"); |
| 156 | if (ofPort != null) { |
| 157 | this.openFlowPort = Integer.parseInt(ofPort); |
| 158 | } |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 159 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 160 | log.debug("OpenFlow port set to {}", this.openFlowPort); |
| 161 | String threads = configParams.get("workerthreads"); |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 162 | this.workerThreads = threads != null ? Integer.parseInt(threads) : 16; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 163 | log.debug("Number of worker threads set to {}", this.workerThreads); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Initialize internal data structures. |
| 169 | */ |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 170 | public void init() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 171 | // These data structures are initialized here because other |
| 172 | // module's startUp() might be called before ours |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 173 | this.controllerNodeIPsCache = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 174 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 175 | this.systemStartTime = System.currentTimeMillis(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // ************** |
| 179 | // Utility methods |
| 180 | // ************** |
| 181 | |
| 182 | public Map<String, Long> getMemory() { |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 183 | Map<String, Long> m = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 198 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 199 | * @param dpid data path id |
| 200 | * @param desc switch description |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 201 | * @param ofv OpenFlow version |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 202 | * @return switch instance |
| 203 | */ |
| 204 | protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid, |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 205 | OFDescStatsReply desc, |
| 206 | OFVersion ofv) { |
| 207 | Driver driver = driverService |
| 208 | .getDriver(desc.getMfrDesc(), desc.getHwDesc(), desc.getSwDesc()); |
| 209 | |
Thomas Vachuska | 3358af2 | 2015-05-19 18:40:34 -0700 | [diff] [blame^] | 210 | if (driver != null && driver.hasBehaviour(OpenFlowSwitchDriver.class)) { |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 211 | OpenFlowSwitchDriver ofSwitchDriver = driver.createBehaviour(new DefaultDriverHandler( |
| 212 | new DefaultDriverData(driver)), OpenFlowSwitchDriver.class); |
| 213 | ofSwitchDriver.init(new Dpid(dpid), desc, ofv); |
| 214 | ofSwitchDriver.setAgent(agent); |
| 215 | ofSwitchDriver.setRoleHandler(new RoleManager(ofSwitchDriver)); |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 216 | log.info("OpenFlow handshaker found for device {}: {}", dpid, ofSwitchDriver); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 217 | return ofSwitchDriver; |
| 218 | } |
| 219 | log.error("No OpenFlow driver for {} : {}", dpid, desc); |
| 220 | return null; |
| 221 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 222 | } |
| 223 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 224 | public void start(OpenFlowAgent ag, DriverService driverService) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 225 | log.info("Starting OpenFlow IO"); |
| 226 | this.agent = ag; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 227 | this.driverService = driverService; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 228 | this.init(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 229 | this.run(); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | public void stop() { |
| 234 | log.info("Stopping OpenFlow IO"); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 235 | cg.close(); |
Thomas Vachuska | 1c681d7 | 2015-05-18 14:58:53 -0700 | [diff] [blame] | 236 | execFactory.shutdown(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | } |