blob: 6cbd991280194884dff6ea354df3967998df13d2 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
tom7ef8ff92014-09-17 13:08:06 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
tom7ef8ff92014-09-17 13:08:06 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
tom7ef8ff92014-09-17 13:08:06 -070016
Brian O'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.openflow.controller.impl;
tom7ef8ff92014-09-17 13:08:06 -070018
tom7ef8ff92014-09-17 13:08:06 -070019import org.jboss.netty.bootstrap.ServerBootstrap;
20import org.jboss.netty.channel.ChannelPipelineFactory;
21import org.jboss.netty.channel.group.ChannelGroup;
22import org.jboss.netty.channel.group.DefaultChannelGroup;
23import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
alshabibb452fd72015-04-22 20:46:20 -070024import org.onosproject.net.driver.DefaultDriverData;
25import org.onosproject.net.driver.DefaultDriverHandler;
26import org.onosproject.net.driver.Driver;
27import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.openflow.controller.Dpid;
29import org.onosproject.openflow.controller.driver.OpenFlowAgent;
30import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
tom7ef8ff92014-09-17 13:08:06 -070031import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
32import org.projectfloodlight.openflow.protocol.OFFactories;
33import org.projectfloodlight.openflow.protocol.OFFactory;
34import org.projectfloodlight.openflow.protocol.OFVersion;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080038import java.lang.management.ManagementFactory;
39import java.lang.management.RuntimeMXBean;
40import java.net.InetSocketAddress;
41import java.util.HashMap;
42import java.util.Map;
43import java.util.concurrent.Executors;
44
45import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska80b0a802015-07-17 08:43:30 -070046import static org.onosproject.net.DeviceId.deviceId;
47import static org.onosproject.openflow.controller.Dpid.uri;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080048
tom7ef8ff92014-09-17 13:08:06 -070049
50/**
51 * The main controller class. Handles all setup and network listeners
52 * - Distributed ownership control of switch through IControllerRegistryService
53 */
54public class Controller {
55
56 protected static final Logger log = LoggerFactory.getLogger(Controller.class);
alshabib9eab22f2014-10-20 17:17:31 -070057
tom7ef8ff92014-09-17 13:08:06 -070058 protected static final OFFactory FACTORY13 = OFFactories.getFactory(OFVersion.OF_13);
59 protected static final OFFactory FACTORY10 = OFFactories.getFactory(OFVersion.OF_10);
60
tom7ef8ff92014-09-17 13:08:06 -070061 protected HashMap<String, String> controllerNodeIPsCache;
62
63 private ChannelGroup cg;
64
65 // Configuration options
66 protected int openFlowPort = 6633;
67 protected int workerThreads = 0;
68
69 // Start time of the controller
70 protected long systemStartTime;
71
72 private OpenFlowAgent agent;
73
74 private NioServerSocketChannelFactory execFactory;
75
76 // Perf. related configuration
77 protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
alshabibb452fd72015-04-22 20:46:20 -070078 private DriverService driverService;
tom7ef8ff92014-09-17 13:08:06 -070079
80 // ***************
81 // Getters/Setters
82 // ***************
83
84 public OFFactory getOFMessageFactory10() {
85 return FACTORY10;
86 }
87
88
89 public OFFactory getOFMessageFactory13() {
90 return FACTORY13;
91 }
92
93
tom7ef8ff92014-09-17 13:08:06 -070094 public Map<String, String> getControllerNodeIPs() {
95 // We return a copy of the mapping so we can guarantee that
96 // the mapping return is the same as one that will be (or was)
97 // dispatched to IHAListeners
Jonathan Hartbbd91d42015-02-27 11:18:04 -080098 HashMap<String, String> retval = new HashMap<>();
tom7ef8ff92014-09-17 13:08:06 -070099 synchronized (controllerNodeIPsCache) {
100 retval.putAll(controllerNodeIPsCache);
101 }
102 return retval;
103 }
104
105
106 public long getSystemStartTime() {
107 return (this.systemStartTime);
108 }
109
110 // **************
111 // Initialization
112 // **************
113
114 /**
115 * Tell controller that we're ready to accept switches loop.
116 */
117 public void run() {
118
119 try {
120 final ServerBootstrap bootstrap = createServerBootStrap();
121
122 bootstrap.setOption("reuseAddr", true);
123 bootstrap.setOption("child.keepAlive", true);
124 bootstrap.setOption("child.tcpNoDelay", true);
125 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
126
127 ChannelPipelineFactory pfact =
128 new OpenflowPipelineFactory(this, null);
129 bootstrap.setPipelineFactory(pfact);
130 InetSocketAddress sa = new InetSocketAddress(openFlowPort);
131 cg = new DefaultChannelGroup();
132 cg.add(bootstrap.bind(sa));
133
134 log.info("Listening for switch connections on {}", sa);
135 } catch (Exception e) {
136 throw new RuntimeException(e);
137 }
138
139 }
140
141 private ServerBootstrap createServerBootStrap() {
142
143 if (workerThreads == 0) {
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800144 execFactory = new NioServerSocketChannelFactory(
145 Executors.newCachedThreadPool(groupedThreads("onos/of", "boss-%d")),
146 Executors.newCachedThreadPool(groupedThreads("onos/of", "worker-%d")));
tom7ef8ff92014-09-17 13:08:06 -0700147 return new ServerBootstrap(execFactory);
148 } else {
149 execFactory = new NioServerSocketChannelFactory(
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800150 Executors.newCachedThreadPool(groupedThreads("onos/of", "boss-%d")),
151 Executors.newCachedThreadPool(groupedThreads("onos/of", "worker-%d")), workerThreads);
tom7ef8ff92014-09-17 13:08:06 -0700152 return new ServerBootstrap(execFactory);
153 }
154 }
155
156 public void setConfigParams(Map<String, String> configParams) {
157 String ofPort = configParams.get("openflowport");
158 if (ofPort != null) {
159 this.openFlowPort = Integer.parseInt(ofPort);
160 }
alshabibb452fd72015-04-22 20:46:20 -0700161
tom7ef8ff92014-09-17 13:08:06 -0700162 log.debug("OpenFlow port set to {}", this.openFlowPort);
163 String threads = configParams.get("workerthreads");
tom1679e182014-10-09 13:50:45 -0700164 this.workerThreads = threads != null ? Integer.parseInt(threads) : 16;
tom7ef8ff92014-09-17 13:08:06 -0700165 log.debug("Number of worker threads set to {}", this.workerThreads);
166 }
167
168
169 /**
170 * Initialize internal data structures.
171 */
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800172 public void init() {
tom7ef8ff92014-09-17 13:08:06 -0700173 // These data structures are initialized here because other
174 // module's startUp() might be called before ours
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800175 this.controllerNodeIPsCache = new HashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700176
tom7ef8ff92014-09-17 13:08:06 -0700177 this.systemStartTime = System.currentTimeMillis();
tom7ef8ff92014-09-17 13:08:06 -0700178 }
179
180 // **************
181 // Utility methods
182 // **************
183
184 public Map<String, Long> getMemory() {
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800185 Map<String, Long> m = new HashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700186 Runtime runtime = Runtime.getRuntime();
187 m.put("total", runtime.totalMemory());
188 m.put("free", runtime.freeMemory());
189 return m;
190 }
191
192
193 public Long getUptime() {
194 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
195 return rb.getUptime();
196 }
197
198 /**
199 * Forward to the driver-manager to get an IOFSwitch instance.
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800200 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800201 * @param dpid data path id
202 * @param desc switch description
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800203 * @param ofv OpenFlow version
tom7ef8ff92014-09-17 13:08:06 -0700204 * @return switch instance
205 */
206 protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid,
alshabibb452fd72015-04-22 20:46:20 -0700207 OFDescStatsReply desc,
208 OFVersion ofv) {
209 Driver driver = driverService
210 .getDriver(desc.getMfrDesc(), desc.getHwDesc(), desc.getSwDesc());
211
Thomas Vachuska3358af22015-05-19 18:40:34 -0700212 if (driver != null && driver.hasBehaviour(OpenFlowSwitchDriver.class)) {
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700213 Dpid did = new Dpid(dpid);
214 DefaultDriverHandler handler =
215 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId(uri(did))));
216 OpenFlowSwitchDriver ofSwitchDriver =
217 driver.createBehaviour(handler, OpenFlowSwitchDriver.class);
218 ofSwitchDriver.init(did, desc, ofv);
alshabibb452fd72015-04-22 20:46:20 -0700219 ofSwitchDriver.setAgent(agent);
220 ofSwitchDriver.setRoleHandler(new RoleManager(ofSwitchDriver));
Saurav Das100e3b82015-04-30 11:12:10 -0700221 log.info("OpenFlow handshaker found for device {}: {}", dpid, ofSwitchDriver);
alshabibb452fd72015-04-22 20:46:20 -0700222 return ofSwitchDriver;
223 }
224 log.error("No OpenFlow driver for {} : {}", dpid, desc);
225 return null;
226
tom7ef8ff92014-09-17 13:08:06 -0700227 }
228
alshabibb452fd72015-04-22 20:46:20 -0700229 public void start(OpenFlowAgent ag, DriverService driverService) {
tom7ef8ff92014-09-17 13:08:06 -0700230 log.info("Starting OpenFlow IO");
231 this.agent = ag;
alshabibb452fd72015-04-22 20:46:20 -0700232 this.driverService = driverService;
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800233 this.init();
tom7ef8ff92014-09-17 13:08:06 -0700234 this.run();
235 }
236
237
238 public void stop() {
239 log.info("Stopping OpenFlow IO");
tom7ef8ff92014-09-17 13:08:06 -0700240 cg.close();
Thomas Vachuska1c681d72015-05-18 14:58:53 -0700241 execFactory.shutdown();
tom7ef8ff92014-09-17 13:08:06 -0700242 }
243
244}