blob: 69c0616553fd96ac60a5294414983150444f6f86 [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
Brian O'Connorff278502015-09-22 14:49:52 -070019import com.google.common.base.Strings;
20import com.google.common.collect.ImmutableList;
tom7ef8ff92014-09-17 13:08:06 -070021import org.jboss.netty.bootstrap.ServerBootstrap;
22import org.jboss.netty.channel.ChannelPipelineFactory;
23import org.jboss.netty.channel.group.ChannelGroup;
24import org.jboss.netty.channel.group.DefaultChannelGroup;
25import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
Jonathan Harta0d12492015-07-16 12:03:41 -070026import org.onlab.util.ItemNotFoundException;
27import org.onosproject.net.DeviceId;
alshabibb452fd72015-04-22 20:46:20 -070028import org.onosproject.net.driver.DefaultDriverData;
29import org.onosproject.net.driver.DefaultDriverHandler;
30import org.onosproject.net.driver.Driver;
31import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.openflow.controller.Dpid;
33import org.onosproject.openflow.controller.driver.OpenFlowAgent;
34import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
tom7ef8ff92014-09-17 13:08:06 -070035import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
36import org.projectfloodlight.openflow.protocol.OFFactories;
37import org.projectfloodlight.openflow.protocol.OFFactory;
38import org.projectfloodlight.openflow.protocol.OFVersion;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
alshabib9b6c19c2015-09-26 12:19:27 -070042import javax.net.ssl.KeyManagerFactory;
43import javax.net.ssl.SSLContext;
44import javax.net.ssl.SSLEngine;
45import javax.net.ssl.TrustManagerFactory;
46import java.io.FileInputStream;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080047import java.lang.management.ManagementFactory;
48import java.lang.management.RuntimeMXBean;
49import java.net.InetSocketAddress;
alshabib9b6c19c2015-09-26 12:19:27 -070050import java.security.KeyStore;
Brian O'Connorff278502015-09-22 14:49:52 -070051import java.util.Dictionary;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080052import java.util.HashMap;
Brian O'Connorff278502015-09-22 14:49:52 -070053import java.util.List;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080054import java.util.Map;
55import java.util.concurrent.Executors;
Brian O'Connorff278502015-09-22 14:49:52 -070056import java.util.stream.Collectors;
57import java.util.stream.Stream;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080058
Brian O'Connorff278502015-09-22 14:49:52 -070059import static org.onlab.util.Tools.get;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080060import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska80b0a802015-07-17 08:43:30 -070061import static org.onosproject.net.DeviceId.deviceId;
62import static org.onosproject.openflow.controller.Dpid.uri;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080063
tom7ef8ff92014-09-17 13:08:06 -070064
65/**
66 * The main controller class. Handles all setup and network listeners
67 * - Distributed ownership control of switch through IControllerRegistryService
68 */
69public class Controller {
70
71 protected static final Logger log = LoggerFactory.getLogger(Controller.class);
alshabib9eab22f2014-10-20 17:17:31 -070072
tom7ef8ff92014-09-17 13:08:06 -070073 protected static final OFFactory FACTORY13 = OFFactories.getFactory(OFVersion.OF_13);
74 protected static final OFFactory FACTORY10 = OFFactories.getFactory(OFVersion.OF_10);
alshabib9b6c19c2015-09-26 12:19:27 -070075 private static final boolean TLS_DISABLED = false;
76 private static final short MIN_KS_LENGTH = 6;
tom7ef8ff92014-09-17 13:08:06 -070077
tom7ef8ff92014-09-17 13:08:06 -070078 protected HashMap<String, String> controllerNodeIPsCache;
79
80 private ChannelGroup cg;
81
82 // Configuration options
Brian O'Connorff278502015-09-22 14:49:52 -070083 protected List<Integer> openFlowPorts = ImmutableList.of(6633, 6653);
84 protected int workerThreads = 16;
tom7ef8ff92014-09-17 13:08:06 -070085
86 // Start time of the controller
87 protected long systemStartTime;
88
89 private OpenFlowAgent agent;
90
91 private NioServerSocketChannelFactory execFactory;
92
alshabib9b6c19c2015-09-26 12:19:27 -070093 protected String ksLocation;
94 protected String tsLocation;
95 protected char[] ksPwd;
96 protected char[] tsPwd;
97 private SSLEngine serverSSLEngine;
98
tom7ef8ff92014-09-17 13:08:06 -070099 // Perf. related configuration
100 protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
alshabibb452fd72015-04-22 20:46:20 -0700101 private DriverService driverService;
alshabib9b6c19c2015-09-26 12:19:27 -0700102 private boolean enableOFTLS = TLS_DISABLED;
tom7ef8ff92014-09-17 13:08:06 -0700103
104 // ***************
105 // Getters/Setters
106 // ***************
107
108 public OFFactory getOFMessageFactory10() {
109 return FACTORY10;
110 }
111
112
113 public OFFactory getOFMessageFactory13() {
114 return FACTORY13;
115 }
116
tom7ef8ff92014-09-17 13:08:06 -0700117 // **************
118 // Initialization
119 // **************
120
121 /**
122 * Tell controller that we're ready to accept switches loop.
123 */
124 public void run() {
125
126 try {
127 final ServerBootstrap bootstrap = createServerBootStrap();
128
129 bootstrap.setOption("reuseAddr", true);
130 bootstrap.setOption("child.keepAlive", true);
131 bootstrap.setOption("child.tcpNoDelay", true);
132 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
133
134 ChannelPipelineFactory pfact =
alshabib9b6c19c2015-09-26 12:19:27 -0700135 new OpenflowPipelineFactory(this, null, serverSSLEngine);
tom7ef8ff92014-09-17 13:08:06 -0700136 bootstrap.setPipelineFactory(pfact);
tom7ef8ff92014-09-17 13:08:06 -0700137 cg = new DefaultChannelGroup();
Brian O'Connorff278502015-09-22 14:49:52 -0700138 openFlowPorts.forEach(port -> {
139 InetSocketAddress sa = new InetSocketAddress(port);
140 cg.add(bootstrap.bind(sa));
141 log.info("Listening for switch connections on {}", sa);
142 });
tom7ef8ff92014-09-17 13:08:06 -0700143
tom7ef8ff92014-09-17 13:08:06 -0700144 } catch (Exception e) {
145 throw new RuntimeException(e);
146 }
147
148 }
149
150 private ServerBootstrap createServerBootStrap() {
151
152 if (workerThreads == 0) {
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800153 execFactory = new NioServerSocketChannelFactory(
154 Executors.newCachedThreadPool(groupedThreads("onos/of", "boss-%d")),
155 Executors.newCachedThreadPool(groupedThreads("onos/of", "worker-%d")));
tom7ef8ff92014-09-17 13:08:06 -0700156 return new ServerBootstrap(execFactory);
157 } else {
158 execFactory = new NioServerSocketChannelFactory(
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800159 Executors.newCachedThreadPool(groupedThreads("onos/of", "boss-%d")),
160 Executors.newCachedThreadPool(groupedThreads("onos/of", "worker-%d")), workerThreads);
tom7ef8ff92014-09-17 13:08:06 -0700161 return new ServerBootstrap(execFactory);
162 }
163 }
164
Brian O'Connorff278502015-09-22 14:49:52 -0700165 public void setConfigParams(Dictionary<?, ?> properties) {
166 String ports = get(properties, "openflowPorts");
167 if (!Strings.isNullOrEmpty(ports)) {
168 this.openFlowPorts = Stream.of(ports.split(","))
169 .map(s -> Integer.parseInt(s))
170 .collect(Collectors.toList());
tom7ef8ff92014-09-17 13:08:06 -0700171 }
Brian O'Connorff278502015-09-22 14:49:52 -0700172 log.debug("OpenFlow ports set to {}", this.openFlowPorts);
Charles Chan45624b82015-08-24 00:29:20 +0800173
Brian O'Connorff278502015-09-22 14:49:52 -0700174 String threads = get(properties, "workerThreads");
175 if (!Strings.isNullOrEmpty(threads)) {
176 this.workerThreads = Integer.parseInt(threads);
177 }
tom7ef8ff92014-09-17 13:08:06 -0700178 log.debug("Number of worker threads set to {}", this.workerThreads);
179 }
180
tom7ef8ff92014-09-17 13:08:06 -0700181 /**
182 * Initialize internal data structures.
183 */
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800184 public void init() {
tom7ef8ff92014-09-17 13:08:06 -0700185 // These data structures are initialized here because other
186 // module's startUp() might be called before ours
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800187 this.controllerNodeIPsCache = new HashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700188
tom7ef8ff92014-09-17 13:08:06 -0700189 this.systemStartTime = System.currentTimeMillis();
alshabib9b6c19c2015-09-26 12:19:27 -0700190
191 try {
192 getTLSParameters();
193 if (enableOFTLS) {
194 initSSL();
195 }
196 } catch (Exception ex) {
197 log.error("SSL init failed: {}", ex.getMessage());
198 }
199
200 }
201
202 private void getTLSParameters() {
203 String tempString = System.getProperty("enableOFTLS");
204 enableOFTLS = Strings.isNullOrEmpty(tempString) ? TLS_DISABLED : Boolean.parseBoolean(tempString);
205 log.info("OpenFlow Security is {}", enableOFTLS ? "enabled" : "disabled");
206 if (enableOFTLS) {
207 ksLocation = System.getProperty("javax.net.ssl.keyStore");
208 if (Strings.isNullOrEmpty(ksLocation)) {
209 enableOFTLS = TLS_DISABLED;
210 return;
211 }
212 tsLocation = System.getProperty("javax.net.ssl.trustStore");
213 if (Strings.isNullOrEmpty(tsLocation)) {
214 enableOFTLS = TLS_DISABLED;
215 return;
216 }
217 ksPwd = System.getProperty("javax.net.ssl.keyStorePassword").toCharArray();
218 if (MIN_KS_LENGTH > ksPwd.length) {
219 enableOFTLS = TLS_DISABLED;
220 return;
221 }
222 tsPwd = System.getProperty("javax.net.ssl.trustStorePassword").toCharArray();
223 if (MIN_KS_LENGTH > tsPwd.length) {
224 enableOFTLS = TLS_DISABLED;
225 return;
226 }
227 }
228 }
229
230 private void initSSL() throws Exception {
231
232 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
233 KeyStore ts = KeyStore.getInstance("JKS");
234 ts.load(new FileInputStream(tsLocation), tsPwd);
235 tmFactory.init(ts);
236
237 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
238 KeyStore ks = KeyStore.getInstance("JKS");
239 ks.load(new FileInputStream(ksLocation), ksPwd);
240 kmf.init(ks, ksPwd);
241
242 SSLContext serverContext = SSLContext.getInstance("TLS");
243 serverContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null);
244
245 serverSSLEngine = serverContext.createSSLEngine();
246
247 serverSSLEngine.setNeedClientAuth(true);
248 serverSSLEngine.setUseClientMode(false);
249 serverSSLEngine.setEnabledProtocols(serverSSLEngine.getSupportedProtocols());
250 serverSSLEngine.setEnabledCipherSuites(serverSSLEngine.getSupportedCipherSuites());
251 serverSSLEngine.setEnableSessionCreation(true);
tom7ef8ff92014-09-17 13:08:06 -0700252 }
253
254 // **************
255 // Utility methods
256 // **************
257
258 public Map<String, Long> getMemory() {
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800259 Map<String, Long> m = new HashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700260 Runtime runtime = Runtime.getRuntime();
261 m.put("total", runtime.totalMemory());
262 m.put("free", runtime.freeMemory());
263 return m;
264 }
265
266
Ray Milkeydc0ff192015-11-04 13:49:52 -0800267 public Long getSystemUptime() {
tom7ef8ff92014-09-17 13:08:06 -0700268 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
269 return rb.getUptime();
270 }
271
Ray Milkeydc0ff192015-11-04 13:49:52 -0800272 public long getSystemStartTime() {
273 return (this.systemStartTime);
274 }
275
tom7ef8ff92014-09-17 13:08:06 -0700276 /**
277 * Forward to the driver-manager to get an IOFSwitch instance.
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800278 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800279 * @param dpid data path id
280 * @param desc switch description
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800281 * @param ofv OpenFlow version
tom7ef8ff92014-09-17 13:08:06 -0700282 * @return switch instance
283 */
284 protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid,
alshabibb452fd72015-04-22 20:46:20 -0700285 OFDescStatsReply desc,
286 OFVersion ofv) {
Jonathan Harta0d12492015-07-16 12:03:41 -0700287 Dpid dpidObj = new Dpid(dpid);
288
289 Driver driver;
290 try {
Sho SHIMIZUbc82ebb2015-08-25 10:15:21 -0700291 driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(dpidObj)));
Jonathan Harta0d12492015-07-16 12:03:41 -0700292 } catch (ItemNotFoundException e) {
293 driver = driverService.getDriver(desc.getMfrDesc(), desc.getHwDesc(), desc.getSwDesc());
294 }
alshabibb452fd72015-04-22 20:46:20 -0700295
Thomas Vachuska3358af22015-05-19 18:40:34 -0700296 if (driver != null && driver.hasBehaviour(OpenFlowSwitchDriver.class)) {
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700297 Dpid did = new Dpid(dpid);
298 DefaultDriverHandler handler =
299 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId(uri(did))));
300 OpenFlowSwitchDriver ofSwitchDriver =
301 driver.createBehaviour(handler, OpenFlowSwitchDriver.class);
302 ofSwitchDriver.init(did, desc, ofv);
alshabibb452fd72015-04-22 20:46:20 -0700303 ofSwitchDriver.setAgent(agent);
304 ofSwitchDriver.setRoleHandler(new RoleManager(ofSwitchDriver));
Saurav Das100e3b82015-04-30 11:12:10 -0700305 log.info("OpenFlow handshaker found for device {}: {}", dpid, ofSwitchDriver);
alshabibb452fd72015-04-22 20:46:20 -0700306 return ofSwitchDriver;
307 }
308 log.error("No OpenFlow driver for {} : {}", dpid, desc);
309 return null;
310
tom7ef8ff92014-09-17 13:08:06 -0700311 }
312
alshabibb452fd72015-04-22 20:46:20 -0700313 public void start(OpenFlowAgent ag, DriverService driverService) {
tom7ef8ff92014-09-17 13:08:06 -0700314 log.info("Starting OpenFlow IO");
315 this.agent = ag;
alshabibb452fd72015-04-22 20:46:20 -0700316 this.driverService = driverService;
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800317 this.init();
tom7ef8ff92014-09-17 13:08:06 -0700318 this.run();
319 }
320
321
322 public void stop() {
323 log.info("Stopping OpenFlow IO");
tom7ef8ff92014-09-17 13:08:06 -0700324 cg.close();
Thomas Vachuska1c681d72015-05-18 14:58:53 -0700325 execFactory.shutdown();
tom7ef8ff92014-09-17 13:08:06 -0700326 }
327
328}