blob: a4947c38f4e06c8c1a4a2cbc0026127a6dc6ba5a [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;
alshabib5162a9d2015-12-07 17:49:37 -080026
Jonathan Harta0d12492015-07-16 12:03:41 -070027import org.onlab.util.ItemNotFoundException;
28import org.onosproject.net.DeviceId;
alshabibb452fd72015-04-22 20:46:20 -070029import org.onosproject.net.driver.DefaultDriverData;
30import org.onosproject.net.driver.DefaultDriverHandler;
31import org.onosproject.net.driver.Driver;
32import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.openflow.controller.Dpid;
34import org.onosproject.openflow.controller.driver.OpenFlowAgent;
35import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
tom7ef8ff92014-09-17 13:08:06 -070036import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
37import org.projectfloodlight.openflow.protocol.OFFactories;
38import org.projectfloodlight.openflow.protocol.OFFactory;
39import org.projectfloodlight.openflow.protocol.OFVersion;
40import org.slf4j.Logger;
41import org.slf4j.LoggerFactory;
42
alshabib9b6c19c2015-09-26 12:19:27 -070043import javax.net.ssl.KeyManagerFactory;
44import javax.net.ssl.SSLContext;
alshabib9b6c19c2015-09-26 12:19:27 -070045import 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;
alshabib5162a9d2015-12-07 17:49:37 -080097 protected SSLContext sslContext;
alshabib9b6c19c2015-09-26 12:19:27 -070098
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;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800102 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 =
alshabib5162a9d2015-12-07 17:49:37 -0800135 new OpenflowPipelineFactory(this, null, sslContext);
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 {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800192 getTlsParameters();
193 if (enableOfTls) {
194 initSsl();
alshabib9b6c19c2015-09-26 12:19:27 -0700195 }
196 } catch (Exception ex) {
197 log.error("SSL init failed: {}", ex.getMessage());
198 }
199
200 }
201
Jonathan Hartb35540a2015-11-17 09:30:56 -0800202 private void getTlsParameters() {
alshabib9b6c19c2015-09-26 12:19:27 -0700203 String tempString = System.getProperty("enableOFTLS");
Jonathan Hartb35540a2015-11-17 09:30:56 -0800204 enableOfTls = Strings.isNullOrEmpty(tempString) ? TLS_DISABLED : Boolean.parseBoolean(tempString);
205 log.info("OpenFlow Security is {}", enableOfTls ? "enabled" : "disabled");
206 if (enableOfTls) {
alshabib9b6c19c2015-09-26 12:19:27 -0700207 ksLocation = System.getProperty("javax.net.ssl.keyStore");
208 if (Strings.isNullOrEmpty(ksLocation)) {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800209 enableOfTls = TLS_DISABLED;
alshabib9b6c19c2015-09-26 12:19:27 -0700210 return;
211 }
212 tsLocation = System.getProperty("javax.net.ssl.trustStore");
213 if (Strings.isNullOrEmpty(tsLocation)) {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800214 enableOfTls = TLS_DISABLED;
alshabib9b6c19c2015-09-26 12:19:27 -0700215 return;
216 }
217 ksPwd = System.getProperty("javax.net.ssl.keyStorePassword").toCharArray();
218 if (MIN_KS_LENGTH > ksPwd.length) {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800219 enableOfTls = TLS_DISABLED;
alshabib9b6c19c2015-09-26 12:19:27 -0700220 return;
221 }
222 tsPwd = System.getProperty("javax.net.ssl.trustStorePassword").toCharArray();
223 if (MIN_KS_LENGTH > tsPwd.length) {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800224 enableOfTls = TLS_DISABLED;
alshabib9b6c19c2015-09-26 12:19:27 -0700225 return;
226 }
227 }
228 }
229
Jonathan Hartb35540a2015-11-17 09:30:56 -0800230 private void initSsl() throws Exception {
alshabib9b6c19c2015-09-26 12:19:27 -0700231
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
alshabib5162a9d2015-12-07 17:49:37 -0800242 sslContext = SSLContext.getInstance("TLS");
243 sslContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null);
alshabib9b6c19c2015-09-26 12:19:27 -0700244
alshabib9b6c19c2015-09-26 12:19:27 -0700245
tom7ef8ff92014-09-17 13:08:06 -0700246 }
247
248 // **************
249 // Utility methods
250 // **************
251
252 public Map<String, Long> getMemory() {
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800253 Map<String, Long> m = new HashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700254 Runtime runtime = Runtime.getRuntime();
255 m.put("total", runtime.totalMemory());
256 m.put("free", runtime.freeMemory());
257 return m;
258 }
259
260
Ray Milkeydc0ff192015-11-04 13:49:52 -0800261 public Long getSystemUptime() {
tom7ef8ff92014-09-17 13:08:06 -0700262 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
263 return rb.getUptime();
264 }
265
Ray Milkeydc0ff192015-11-04 13:49:52 -0800266 public long getSystemStartTime() {
267 return (this.systemStartTime);
268 }
269
tom7ef8ff92014-09-17 13:08:06 -0700270 /**
271 * Forward to the driver-manager to get an IOFSwitch instance.
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800272 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800273 * @param dpid data path id
274 * @param desc switch description
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800275 * @param ofv OpenFlow version
tom7ef8ff92014-09-17 13:08:06 -0700276 * @return switch instance
277 */
278 protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid,
alshabibb452fd72015-04-22 20:46:20 -0700279 OFDescStatsReply desc,
280 OFVersion ofv) {
Jonathan Harta0d12492015-07-16 12:03:41 -0700281 Dpid dpidObj = new Dpid(dpid);
282
283 Driver driver;
284 try {
Sho SHIMIZUbc82ebb2015-08-25 10:15:21 -0700285 driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(dpidObj)));
Jonathan Harta0d12492015-07-16 12:03:41 -0700286 } catch (ItemNotFoundException e) {
287 driver = driverService.getDriver(desc.getMfrDesc(), desc.getHwDesc(), desc.getSwDesc());
288 }
alshabibb452fd72015-04-22 20:46:20 -0700289
Thomas Vachuska3358af22015-05-19 18:40:34 -0700290 if (driver != null && driver.hasBehaviour(OpenFlowSwitchDriver.class)) {
Thomas Vachuska80b0a802015-07-17 08:43:30 -0700291 Dpid did = new Dpid(dpid);
292 DefaultDriverHandler handler =
293 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId(uri(did))));
294 OpenFlowSwitchDriver ofSwitchDriver =
295 driver.createBehaviour(handler, OpenFlowSwitchDriver.class);
296 ofSwitchDriver.init(did, desc, ofv);
alshabibb452fd72015-04-22 20:46:20 -0700297 ofSwitchDriver.setAgent(agent);
298 ofSwitchDriver.setRoleHandler(new RoleManager(ofSwitchDriver));
Saurav Das100e3b82015-04-30 11:12:10 -0700299 log.info("OpenFlow handshaker found for device {}: {}", dpid, ofSwitchDriver);
alshabibb452fd72015-04-22 20:46:20 -0700300 return ofSwitchDriver;
301 }
302 log.error("No OpenFlow driver for {} : {}", dpid, desc);
303 return null;
304
tom7ef8ff92014-09-17 13:08:06 -0700305 }
306
alshabibb452fd72015-04-22 20:46:20 -0700307 public void start(OpenFlowAgent ag, DriverService driverService) {
tom7ef8ff92014-09-17 13:08:06 -0700308 log.info("Starting OpenFlow IO");
309 this.agent = ag;
alshabibb452fd72015-04-22 20:46:20 -0700310 this.driverService = driverService;
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800311 this.init();
tom7ef8ff92014-09-17 13:08:06 -0700312 this.run();
313 }
314
315
316 public void stop() {
317 log.info("Stopping OpenFlow IO");
tom7ef8ff92014-09-17 13:08:06 -0700318 cg.close();
Thomas Vachuska1c681d72015-05-18 14:58:53 -0700319 execFactory.shutdown();
tom7ef8ff92014-09-17 13:08:06 -0700320 }
321
322}