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