blob: 324e06d5671272826d294664332fce08eaecd466 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070021import io.netty.bootstrap.ServerBootstrap;
22import io.netty.channel.ChannelOption;
23import io.netty.channel.EventLoopGroup;
24import io.netty.channel.epoll.EpollEventLoopGroup;
25import io.netty.channel.epoll.EpollServerSocketChannel;
26import io.netty.channel.group.ChannelGroup;
27import io.netty.channel.group.DefaultChannelGroup;
28import io.netty.channel.nio.NioEventLoopGroup;
29import io.netty.channel.socket.nio.NioServerSocketChannel;
30import io.netty.util.concurrent.GlobalEventExecutor;
Jonathan Harta0d12492015-07-16 12:03:41 -070031import org.onlab.util.ItemNotFoundException;
32import org.onosproject.net.DeviceId;
alshabibb452fd72015-04-22 20:46:20 -070033import org.onosproject.net.driver.DefaultDriverData;
34import org.onosproject.net.driver.DefaultDriverHandler;
35import org.onosproject.net.driver.Driver;
36import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.openflow.controller.Dpid;
38import org.onosproject.openflow.controller.driver.OpenFlowAgent;
39import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver;
tom7ef8ff92014-09-17 13:08:06 -070040import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
tom7ef8ff92014-09-17 13:08:06 -070041import org.projectfloodlight.openflow.protocol.OFVersion;
42import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
alshabib9b6c19c2015-09-26 12:19:27 -070045import javax.net.ssl.KeyManagerFactory;
46import javax.net.ssl.SSLContext;
alshabib9b6c19c2015-09-26 12:19:27 -070047import javax.net.ssl.TrustManagerFactory;
48import java.io.FileInputStream;
Ray Milkey986a47a2018-01-25 11:38:51 -080049import java.io.IOException;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080050import java.lang.management.ManagementFactory;
51import java.lang.management.RuntimeMXBean;
Ray Milkey986a47a2018-01-25 11:38:51 -080052import java.security.KeyManagementException;
alshabib9b6c19c2015-09-26 12:19:27 -070053import java.security.KeyStore;
Ray Milkey986a47a2018-01-25 11:38:51 -080054import java.security.KeyStoreException;
55import java.security.NoSuchAlgorithmException;
56import java.security.UnrecoverableKeyException;
57import java.security.cert.CertificateException;
Brian O'Connorff278502015-09-22 14:49:52 -070058import java.util.Dictionary;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080059import java.util.HashMap;
Brian O'Connorff278502015-09-22 14:49:52 -070060import java.util.List;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080061import java.util.Map;
Brian O'Connorff278502015-09-22 14:49:52 -070062import java.util.stream.Collectors;
63import java.util.stream.Stream;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080064
Brian O'Connorff278502015-09-22 14:49:52 -070065import static org.onlab.util.Tools.get;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080066import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska80b0a802015-07-17 08:43:30 -070067import static org.onosproject.net.DeviceId.deviceId;
68import static org.onosproject.openflow.controller.Dpid.uri;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080069
tom7ef8ff92014-09-17 13:08:06 -070070
71/**
72 * The main controller class. Handles all setup and network listeners
tom7ef8ff92014-09-17 13:08:06 -070073 */
74public class Controller {
75
Ray Milkey9c9cde42018-01-12 14:22:06 -080076 private static final Logger log = LoggerFactory.getLogger(Controller.class);
alshabib9eab22f2014-10-20 17:17:31 -070077
alshabib9b6c19c2015-09-26 12:19:27 -070078 private static final boolean TLS_DISABLED = false;
79 private static final short MIN_KS_LENGTH = 6;
tom7ef8ff92014-09-17 13:08:06 -070080
tom7ef8ff92014-09-17 13:08:06 -070081 protected HashMap<String, String> controllerNodeIPsCache;
82
83 private ChannelGroup cg;
84
85 // Configuration options
Brian O'Connorff278502015-09-22 14:49:52 -070086 protected List<Integer> openFlowPorts = ImmutableList.of(6633, 6653);
Yuta HIGUCHI8552b172016-07-25 12:10:08 -070087 protected int workerThreads = 0;
tom7ef8ff92014-09-17 13:08:06 -070088
89 // Start time of the controller
90 protected long systemStartTime;
91
92 private OpenFlowAgent agent;
93
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070094 private EventLoopGroup bossGroup;
95 private EventLoopGroup workerGroup;
tom7ef8ff92014-09-17 13:08:06 -070096
alshabib9b6c19c2015-09-26 12:19:27 -070097 protected String ksLocation;
98 protected String tsLocation;
99 protected char[] ksPwd;
100 protected char[] tsPwd;
alshabib5162a9d2015-12-07 17:49:37 -0800101 protected SSLContext sslContext;
alshabib9b6c19c2015-09-26 12:19:27 -0700102
tom7ef8ff92014-09-17 13:08:06 -0700103 // Perf. related configuration
104 protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
Yuta HIGUCHI2341e602017-03-08 20:10:08 -0800105
alshabibb452fd72015-04-22 20:46:20 -0700106 private DriverService driverService;
Jonathan Hartb35540a2015-11-17 09:30:56 -0800107 private boolean enableOfTls = TLS_DISABLED;
tom7ef8ff92014-09-17 13:08:06 -0700108
tom7ef8ff92014-09-17 13:08:06 -0700109 // **************
110 // Initialization
111 // **************
112
113 /**
114 * Tell controller that we're ready to accept switches loop.
115 */
116 public void run() {
117
tom7ef8ff92014-09-17 13:08:06 -0700118 final ServerBootstrap bootstrap = createServerBootStrap();
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700119 bootstrap.option(ChannelOption.SO_REUSEADDR, true);
120 bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
121 bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
122 bootstrap.childOption(ChannelOption.SO_SNDBUF, Controller.SEND_BUFFER_SIZE);
123// bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK,
124// new WriteBufferWaterMark(8 * 1024, 32 * 1024));
tom7ef8ff92014-09-17 13:08:06 -0700125
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700126 bootstrap.childHandler(new OFChannelInitializer(this, null, sslContext));
tom7ef8ff92014-09-17 13:08:06 -0700127
Brian O'Connorff278502015-09-22 14:49:52 -0700128 openFlowPorts.forEach(port -> {
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700129 // TODO revisit if this is best way to listen to multiple ports
130 cg.add(bootstrap.bind(port).syncUninterruptibly().channel());
131 log.info("Listening for switch connections on {}", port);
Brian O'Connorff278502015-09-22 14:49:52 -0700132 });
tom7ef8ff92014-09-17 13:08:06 -0700133
tom7ef8ff92014-09-17 13:08:06 -0700134 }
135
136 private ServerBootstrap createServerBootStrap() {
137
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700138 int bossThreads = Math.max(1, openFlowPorts.size());
139 try {
140 bossGroup = new EpollEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log));
141 workerGroup = new EpollEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log));
142 ServerBootstrap bs = new ServerBootstrap()
143 .group(bossGroup, workerGroup)
144 .channel(EpollServerSocketChannel.class);
145 log.info("Using Epoll transport");
146 return bs;
147 } catch (Throwable e) {
148 log.debug("Failed to initialize native (epoll) transport: {}", e.getMessage());
tom7ef8ff92014-09-17 13:08:06 -0700149 }
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700150
151// Requires 4.1.11 or later
152// try {
153// bossGroup = new KQueueEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log));
154// workerGroup = new KQueueEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log));
155// ServerBootstrap bs = new ServerBootstrap()
156// .group(bossGroup, workerGroup)
157// .channel(KQueueServerSocketChannel.class);
158// log.info("Using Kqueue transport");
159// return bs;
160// } catch (Throwable e) {
161// log.debug("Failed to initialize native (kqueue) transport. ", e.getMessage());
162// }
163
164 bossGroup = new NioEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log));
165 workerGroup = new NioEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log));
166 log.info("Using Nio transport");
167 return new ServerBootstrap()
168 .group(bossGroup, workerGroup)
169 .channel(NioServerSocketChannel.class);
tom7ef8ff92014-09-17 13:08:06 -0700170 }
171
Brian O'Connorff278502015-09-22 14:49:52 -0700172 public void setConfigParams(Dictionary<?, ?> properties) {
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700173 // TODO should be possible to reconfigure ports without restart,
174 // by updating ChannelGroup
Brian O'Connorff278502015-09-22 14:49:52 -0700175 String ports = get(properties, "openflowPorts");
176 if (!Strings.isNullOrEmpty(ports)) {
177 this.openFlowPorts = Stream.of(ports.split(","))
178 .map(s -> Integer.parseInt(s))
179 .collect(Collectors.toList());
tom7ef8ff92014-09-17 13:08:06 -0700180 }
Brian O'Connorff278502015-09-22 14:49:52 -0700181 log.debug("OpenFlow ports set to {}", this.openFlowPorts);
Charles Chan45624b82015-08-24 00:29:20 +0800182
Brian O'Connorff278502015-09-22 14:49:52 -0700183 String threads = get(properties, "workerThreads");
184 if (!Strings.isNullOrEmpty(threads)) {
185 this.workerThreads = Integer.parseInt(threads);
186 }
tom7ef8ff92014-09-17 13:08:06 -0700187 log.debug("Number of worker threads set to {}", this.workerThreads);
188 }
189
tom7ef8ff92014-09-17 13:08:06 -0700190 /**
191 * Initialize internal data structures.
192 */
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800193 public void init() {
tom7ef8ff92014-09-17 13:08:06 -0700194 // These data structures are initialized here because other
195 // module's startUp() might be called before ours
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800196 this.controllerNodeIPsCache = new HashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700197
tom7ef8ff92014-09-17 13:08:06 -0700198 this.systemStartTime = System.currentTimeMillis();
alshabib9b6c19c2015-09-26 12:19:27 -0700199
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700200 cg = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
201
Ray Milkey986a47a2018-01-25 11:38:51 -0800202 getTlsParameters();
203 if (enableOfTls) {
204 initSsl();
alshabib9b6c19c2015-09-26 12:19:27 -0700205 }
alshabib9b6c19c2015-09-26 12:19:27 -0700206 }
207
Jonathan Hartb35540a2015-11-17 09:30:56 -0800208 private void getTlsParameters() {
alshabib9b6c19c2015-09-26 12:19:27 -0700209 String tempString = System.getProperty("enableOFTLS");
Jonathan Hartb35540a2015-11-17 09:30:56 -0800210 enableOfTls = Strings.isNullOrEmpty(tempString) ? TLS_DISABLED : Boolean.parseBoolean(tempString);
211 log.info("OpenFlow Security is {}", enableOfTls ? "enabled" : "disabled");
212 if (enableOfTls) {
alshabib9b6c19c2015-09-26 12:19:27 -0700213 ksLocation = System.getProperty("javax.net.ssl.keyStore");
214 if (Strings.isNullOrEmpty(ksLocation)) {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800215 enableOfTls = TLS_DISABLED;
alshabib9b6c19c2015-09-26 12:19:27 -0700216 return;
217 }
218 tsLocation = System.getProperty("javax.net.ssl.trustStore");
219 if (Strings.isNullOrEmpty(tsLocation)) {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800220 enableOfTls = TLS_DISABLED;
alshabib9b6c19c2015-09-26 12:19:27 -0700221 return;
222 }
223 ksPwd = System.getProperty("javax.net.ssl.keyStorePassword").toCharArray();
224 if (MIN_KS_LENGTH > ksPwd.length) {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800225 enableOfTls = TLS_DISABLED;
alshabib9b6c19c2015-09-26 12:19:27 -0700226 return;
227 }
228 tsPwd = System.getProperty("javax.net.ssl.trustStorePassword").toCharArray();
229 if (MIN_KS_LENGTH > tsPwd.length) {
Jonathan Hartb35540a2015-11-17 09:30:56 -0800230 enableOfTls = TLS_DISABLED;
alshabib9b6c19c2015-09-26 12:19:27 -0700231 return;
232 }
233 }
234 }
235
Ray Milkey986a47a2018-01-25 11:38:51 -0800236 private void initSsl() {
237 try {
238 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
239 KeyStore ts = KeyStore.getInstance("JKS");
240 ts.load(new FileInputStream(tsLocation), tsPwd);
241 tmFactory.init(ts);
alshabib9b6c19c2015-09-26 12:19:27 -0700242
Ray Milkey986a47a2018-01-25 11:38:51 -0800243 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
244 KeyStore ks = KeyStore.getInstance("JKS");
245 ks.load(new FileInputStream(ksLocation), ksPwd);
246 kmf.init(ks, ksPwd);
alshabib9b6c19c2015-09-26 12:19:27 -0700247
Ray Milkey986a47a2018-01-25 11:38:51 -0800248 sslContext = SSLContext.getInstance("TLS");
249 sslContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null);
250 } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException |
251 IOException | KeyManagementException | UnrecoverableKeyException ex) {
252 log.error("SSL init failed: {}", ex.getMessage());
253 }
tom7ef8ff92014-09-17 13:08:06 -0700254 }
255
256 // **************
257 // Utility methods
258 // **************
259
260 public Map<String, Long> getMemory() {
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800261 Map<String, Long> m = new HashMap<>();
tom7ef8ff92014-09-17 13:08:06 -0700262 Runtime runtime = Runtime.getRuntime();
263 m.put("total", runtime.totalMemory());
264 m.put("free", runtime.freeMemory());
265 return m;
266 }
267
268
Ray Milkeydc0ff192015-11-04 13:49:52 -0800269 public Long getSystemUptime() {
tom7ef8ff92014-09-17 13:08:06 -0700270 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
271 return rb.getUptime();
272 }
273
Ray Milkeydc0ff192015-11-04 13:49:52 -0800274 public long getSystemStartTime() {
275 return (this.systemStartTime);
276 }
277
tom7ef8ff92014-09-17 13:08:06 -0700278 /**
279 * Forward to the driver-manager to get an IOFSwitch instance.
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800280 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800281 * @param dpid data path id
282 * @param desc switch description
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800283 * @param ofv OpenFlow version
tom7ef8ff92014-09-17 13:08:06 -0700284 * @return switch instance
285 */
286 protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid,
alshabibb452fd72015-04-22 20:46:20 -0700287 OFDescStatsReply desc,
288 OFVersion ofv) {
Jonathan Harta0d12492015-07-16 12:03:41 -0700289 Dpid dpidObj = new Dpid(dpid);
290
291 Driver driver;
292 try {
Sho SHIMIZUbc82ebb2015-08-25 10:15:21 -0700293 driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(dpidObj)));
Jonathan Harta0d12492015-07-16 12:03:41 -0700294 } catch (ItemNotFoundException e) {
295 driver = driverService.getDriver(desc.getMfrDesc(), desc.getHwDesc(), desc.getSwDesc());
296 }
alshabibb452fd72015-04-22 20:46:20 -0700297
Jonathan Harta33134e2016-07-27 17:33:35 -0700298 if (driver == null) {
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700299 log.error("No OpenFlow driver for {} : {}", dpidObj, desc);
Jonathan Harta33134e2016-07-27 17:33:35 -0700300 return null;
alshabibb452fd72015-04-22 20:46:20 -0700301 }
alshabibb452fd72015-04-22 20:46:20 -0700302
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700303 log.info("Driver '{}' assigned to device {}", driver.name(), dpidObj);
Jonathan Harta33134e2016-07-27 17:33:35 -0700304
305 if (!driver.hasBehaviour(OpenFlowSwitchDriver.class)) {
306 log.error("Driver {} does not support OpenFlowSwitchDriver behaviour", driver.name());
307 return null;
308 }
309
310 DefaultDriverHandler handler =
311 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId(uri(dpidObj))));
312 OpenFlowSwitchDriver ofSwitchDriver =
313 driver.createBehaviour(handler, OpenFlowSwitchDriver.class);
314 ofSwitchDriver.init(dpidObj, desc, ofv);
315 ofSwitchDriver.setAgent(agent);
316 ofSwitchDriver.setRoleHandler(new RoleManager(ofSwitchDriver));
Jonathan Harta33134e2016-07-27 17:33:35 -0700317 return ofSwitchDriver;
tom7ef8ff92014-09-17 13:08:06 -0700318 }
319
alshabibb452fd72015-04-22 20:46:20 -0700320 public void start(OpenFlowAgent ag, DriverService driverService) {
tom7ef8ff92014-09-17 13:08:06 -0700321 log.info("Starting OpenFlow IO");
322 this.agent = ag;
alshabibb452fd72015-04-22 20:46:20 -0700323 this.driverService = driverService;
Jonathan Hartbbd91d42015-02-27 11:18:04 -0800324 this.init();
tom7ef8ff92014-09-17 13:08:06 -0700325 this.run();
326 }
327
328
329 public void stop() {
330 log.info("Stopping OpenFlow IO");
tom7ef8ff92014-09-17 13:08:06 -0700331 cg.close();
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -0700332
333 // Shut down all event loops to terminate all threads.
334 bossGroup.shutdownGracefully();
335 workerGroup.shutdownGracefully();
336
337 // Wait until all threads are terminated.
338 try {
339 bossGroup.terminationFuture().sync();
340 workerGroup.terminationFuture().sync();
341 } catch (InterruptedException e) {
342 log.warn("Interrupted while stopping", e);
343 Thread.currentThread().interrupt();
344 }
tom7ef8ff92014-09-17 13:08:06 -0700345 }
346
347}