Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.openflow.controller.impl; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 22 | import io.netty.bootstrap.ServerBootstrap; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 23 | import io.netty.channel.Channel; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 24 | import io.netty.channel.ChannelOption; |
| 25 | import io.netty.channel.EventLoopGroup; |
| 26 | import io.netty.channel.epoll.EpollEventLoopGroup; |
| 27 | import io.netty.channel.epoll.EpollServerSocketChannel; |
| 28 | import io.netty.channel.group.ChannelGroup; |
| 29 | import io.netty.channel.group.DefaultChannelGroup; |
| 30 | import io.netty.channel.nio.NioEventLoopGroup; |
| 31 | import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 32 | import io.netty.util.concurrent.GlobalEventExecutor; |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 33 | import org.onlab.util.ItemNotFoundException; |
| 34 | import org.onosproject.net.DeviceId; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 35 | import org.onosproject.net.config.NetworkConfigRegistry; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 36 | import org.onosproject.net.driver.DefaultDriverData; |
| 37 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 38 | import org.onosproject.net.driver.Driver; |
| 39 | import org.onosproject.net.driver.DriverService; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 40 | import org.onosproject.openflow.config.OpenFlowDeviceConfig; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.openflow.controller.Dpid; |
| 42 | import org.onosproject.openflow.controller.driver.OpenFlowAgent; |
| 43 | import org.onosproject.openflow.controller.driver.OpenFlowSwitchDriver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 44 | import org.projectfloodlight.openflow.protocol.OFDescStatsReply; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 45 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 49 | import javax.net.ssl.KeyManagerFactory; |
| 50 | import javax.net.ssl.SSLContext; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 51 | import javax.net.ssl.TrustManagerFactory; |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 52 | import java.io.File; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 53 | import java.io.FileInputStream; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 54 | import java.io.IOException; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 55 | import java.lang.management.ManagementFactory; |
| 56 | import java.lang.management.RuntimeMXBean; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 57 | import java.net.InetSocketAddress; |
| 58 | import java.net.SocketAddress; |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 59 | import java.security.DigestInputStream; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 60 | import java.security.KeyManagementException; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 61 | import java.security.KeyStore; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 62 | import java.security.KeyStoreException; |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 63 | import java.security.MessageDigest; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 64 | import java.security.NoSuchAlgorithmException; |
| 65 | import java.security.UnrecoverableKeyException; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 66 | import java.security.cert.Certificate; |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 67 | import java.security.cert.CertificateException; |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 68 | import java.util.Arrays; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 69 | import java.util.Collection; |
| 70 | import java.util.Collections; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 71 | import java.util.Dictionary; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 72 | import java.util.EnumSet; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 73 | import java.util.HashMap; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 74 | import java.util.Iterator; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 75 | import java.util.List; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 76 | import java.util.Map; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 77 | import java.util.Objects; |
| 78 | import java.util.Optional; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 79 | import java.util.Set; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 80 | import java.util.stream.Collectors; |
| 81 | import java.util.stream.Stream; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 82 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 83 | import static org.onlab.util.Tools.get; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 84 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 80b0a80 | 2015-07-17 08:43:30 -0700 | [diff] [blame] | 85 | import static org.onosproject.net.DeviceId.deviceId; |
| 86 | import static org.onosproject.openflow.controller.Dpid.uri; |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 87 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * The main controller class. Handles all setup and network listeners |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 91 | */ |
| 92 | public class Controller { |
| 93 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 94 | private static final Logger log = LoggerFactory.getLogger(Controller.class); |
alshabib | 9eab22f | 2014-10-20 17:17:31 -0700 | [diff] [blame] | 95 | |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 96 | private static final short MIN_KS_LENGTH = 6; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 97 | |
Anton Chigrin | 4af4f87 | 2019-01-14 17:29:56 +0200 | [diff] [blame^] | 98 | //Default queues settings |
| 99 | private static final short DEFAULT_QUEUE_SIZE = 5000; |
| 100 | private static final short FIRST_QUEUE_SIZE = 1000; |
| 101 | private static final short DEFAULT_BULK_SIZE = 100; |
| 102 | private static final short DEFAULT_QUEUE_ID = 7; |
| 103 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 104 | protected HashMap<String, String> controllerNodeIPsCache; |
| 105 | |
| 106 | private ChannelGroup cg; |
| 107 | |
| 108 | // Configuration options |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 109 | protected List<Integer> openFlowPorts = ImmutableList.of(6633, 6653); |
Yuta HIGUCHI | 8552b17 | 2016-07-25 12:10:08 -0700 | [diff] [blame] | 110 | protected int workerThreads = 0; |
Anton Chigrin | 4af4f87 | 2019-01-14 17:29:56 +0200 | [diff] [blame^] | 111 | protected int[] cfgQueueSizes = {FIRST_QUEUE_SIZE, 0, 0, 0, 0, 0, 0, DEFAULT_QUEUE_SIZE}; |
| 112 | protected int[] cfgBulkSizes = new int[8]; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 113 | |
| 114 | // Start time of the controller |
| 115 | protected long systemStartTime; |
| 116 | |
| 117 | private OpenFlowAgent agent; |
| 118 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 119 | private EventLoopGroup bossGroup; |
| 120 | private EventLoopGroup workerGroup; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 121 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 122 | enum TlsMode { |
| 123 | DISABLED, // TLS is not used for OpenFlow connections |
| 124 | ENABLED, // Clients are required use TLS and present a client certificate |
| 125 | STRICT, // Clients must use TLS, and certificate must match the one specified in netcfg |
| 126 | } |
| 127 | private static final EnumSet<TlsMode> TLS_ENABLED = EnumSet.of(TlsMode.ENABLED, TlsMode.STRICT); |
| 128 | |
| 129 | protected TlsParams tlsParams; |
alshabib | 5162a9d | 2015-12-07 17:49:37 -0800 | [diff] [blame] | 130 | protected SSLContext sslContext; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 131 | protected KeyStore keyStore; |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 132 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 133 | // Perf. related configuration |
| 134 | protected static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 135 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 136 | private DriverService driverService; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 137 | private NetworkConfigRegistry netCfgService; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 138 | |
Anton Chigrin | 4af4f87 | 2019-01-14 17:29:56 +0200 | [diff] [blame^] | 139 | public Controller() { |
| 140 | Arrays.fill(cfgBulkSizes, DEFAULT_BULK_SIZE); |
| 141 | } |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 142 | |
Anton Chigrin | 4af4f87 | 2019-01-14 17:29:56 +0200 | [diff] [blame^] | 143 | public int getQueueSize(int queueId) { |
| 144 | return cfgQueueSizes[queueId]; |
| 145 | } |
| 146 | |
| 147 | public int getBulkSize(int queueId) { |
| 148 | return cfgBulkSizes[queueId]; |
| 149 | } |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 150 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 151 | // ************** |
| 152 | // Initialization |
| 153 | // ************** |
| 154 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 155 | private void addListeningPorts(Collection<Integer> ports) { |
| 156 | if (cg == null) { |
| 157 | return; |
| 158 | } |
| 159 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 160 | bootstrap.option(ChannelOption.SO_REUSEADDR, true); |
| 161 | bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); |
| 162 | bootstrap.childOption(ChannelOption.TCP_NODELAY, true); |
| 163 | bootstrap.childOption(ChannelOption.SO_SNDBUF, Controller.SEND_BUFFER_SIZE); |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 164 | // bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, |
| 165 | // new WriteBufferWaterMark(8 * 1024, 32 * 1024)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 166 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 167 | bootstrap.childHandler(new OFChannelInitializer(this, null, sslContext)); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 168 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 169 | Set<Integer> existingPorts = cg.stream() |
| 170 | .map(Channel::localAddress) |
| 171 | .filter(InetSocketAddress.class::isInstance) |
| 172 | .map(InetSocketAddress.class::cast) |
| 173 | .map(InetSocketAddress::getPort) |
| 174 | .collect(Collectors.toSet()); |
| 175 | ports.removeAll(existingPorts); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 176 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 177 | ports.forEach(port -> { |
| 178 | // TODO revisit if this is best way to listen to multiple ports |
| 179 | cg.add(bootstrap.bind(port).syncUninterruptibly().channel()); |
| 180 | log.info("Listening for OF switch connections on {}", port); |
| 181 | }); |
| 182 | } |
| 183 | |
| 184 | private void removeListeningPorts(Collection<Integer> ports) { |
| 185 | if (cg == null) { |
| 186 | return; |
| 187 | } |
| 188 | Iterator<Channel> itr = cg.iterator(); |
| 189 | while (itr.hasNext()) { |
| 190 | Channel c = itr.next(); |
| 191 | SocketAddress addr = c.localAddress(); |
| 192 | if (addr instanceof InetSocketAddress) { |
| 193 | InetSocketAddress inetAddr = (InetSocketAddress) addr; |
| 194 | Integer port = inetAddr.getPort(); |
| 195 | if (ports.contains(port)) { |
| 196 | log.info("No longer listening for OF switch connections on {}", port); |
| 197 | c.close(); |
| 198 | itr.remove(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | private ServerBootstrap createServerBootStrap() { |
| 206 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 207 | int bossThreads = Math.max(1, openFlowPorts.size()); |
| 208 | try { |
| 209 | bossGroup = new EpollEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 210 | workerGroup = new EpollEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 211 | ServerBootstrap bs = new ServerBootstrap() |
| 212 | .group(bossGroup, workerGroup) |
| 213 | .channel(EpollServerSocketChannel.class); |
| 214 | log.info("Using Epoll transport"); |
| 215 | return bs; |
| 216 | } catch (Throwable e) { |
| 217 | log.debug("Failed to initialize native (epoll) transport: {}", e.getMessage()); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 218 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 219 | |
| 220 | // Requires 4.1.11 or later |
| 221 | // try { |
| 222 | // bossGroup = new KQueueEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 223 | // workerGroup = new KQueueEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 224 | // ServerBootstrap bs = new ServerBootstrap() |
| 225 | // .group(bossGroup, workerGroup) |
| 226 | // .channel(KQueueServerSocketChannel.class); |
| 227 | // log.info("Using Kqueue transport"); |
| 228 | // return bs; |
| 229 | // } catch (Throwable e) { |
| 230 | // log.debug("Failed to initialize native (kqueue) transport. ", e.getMessage()); |
| 231 | // } |
| 232 | |
| 233 | bossGroup = new NioEventLoopGroup(bossThreads, groupedThreads("onos/of", "boss-%d", log)); |
| 234 | workerGroup = new NioEventLoopGroup(workerThreads, groupedThreads("onos/of", "worker-%d", log)); |
| 235 | log.info("Using Nio transport"); |
| 236 | return new ServerBootstrap() |
| 237 | .group(bossGroup, workerGroup) |
| 238 | .channel(NioServerSocketChannel.class); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Anton Chigrin | 4af4f87 | 2019-01-14 17:29:56 +0200 | [diff] [blame^] | 241 | public void setQueueParams(Dictionary<?, ?> properties, String sizeParamName, String bulkParamName, int queueId) { |
| 242 | String queueSize = get(properties, sizeParamName); |
| 243 | if (!Strings.isNullOrEmpty(queueSize)) { |
| 244 | int size = Integer.parseInt(queueSize); |
| 245 | if (size > 0) { |
| 246 | this.cfgQueueSizes[queueId] = size; |
| 247 | } else { |
| 248 | throw new IllegalArgumentException( |
| 249 | String.format("%s value must be either a positive integer value", sizeParamName)); |
| 250 | } |
| 251 | } |
| 252 | String bulkSize = get(properties, bulkParamName); |
| 253 | if (!Strings.isNullOrEmpty(bulkSize)) { |
| 254 | int bulk = Integer.parseInt(bulkSize); |
| 255 | if (bulk > 0) { |
| 256 | this.cfgBulkSizes[queueId] = bulk; |
| 257 | } else { |
| 258 | throw new IllegalArgumentException( |
| 259 | String.format("%s value must be either a positive integer value", bulkParamName)); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 264 | public void setConfigParams(Dictionary<?, ?> properties) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 265 | boolean restartRequired = setOpenFlowPorts(properties); |
| 266 | restartRequired |= setWorkerThreads(properties); |
| 267 | restartRequired |= setTlsParameters(properties); |
| 268 | if (restartRequired) { |
| 269 | restart(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 270 | } |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Gets the list of listening ports from property dict. |
| 275 | * |
| 276 | * @param properties dictionary |
| 277 | * @return true if restart is required |
| 278 | */ |
| 279 | private boolean setWorkerThreads(Dictionary<?, ?> properties) { |
| 280 | List<Integer> oldPorts = this.openFlowPorts; |
| 281 | String ports = get(properties, "openflowPorts"); |
| 282 | List<Integer> newPorts = Collections.emptyList(); |
| 283 | if (!Strings.isNullOrEmpty(ports)) { |
| 284 | newPorts = Stream.of(ports.split(",")) |
| 285 | .map(s -> Integer.parseInt(s)) |
| 286 | .collect(Collectors.toList()); |
| 287 | } |
| 288 | |
| 289 | Set<Integer> portsToAdd = Sets.newHashSet(newPorts); |
| 290 | portsToAdd.removeAll(oldPorts); |
| 291 | addListeningPorts(portsToAdd); |
| 292 | |
| 293 | Set<Integer> portsToRemove = Sets.newHashSet(oldPorts); |
| 294 | portsToRemove.removeAll(newPorts); |
| 295 | removeListeningPorts(portsToRemove); |
| 296 | |
| 297 | this.openFlowPorts = newPorts; |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 298 | log.debug("OpenFlow ports set to {}", this.openFlowPorts); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 299 | return false; // restart is never required |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Gets the number of worker threads from property dict. |
| 304 | * |
| 305 | * @param properties dictionary |
| 306 | * @return true if restart is required |
| 307 | */ |
| 308 | private boolean setOpenFlowPorts(Dictionary<?, ?> properties) { |
| 309 | Integer oldValue = this.workerThreads; |
Charles Chan | 45624b8 | 2015-08-24 00:29:20 +0800 | [diff] [blame] | 310 | |
Brian O'Connor | ff27850 | 2015-09-22 14:49:52 -0700 | [diff] [blame] | 311 | String threads = get(properties, "workerThreads"); |
| 312 | if (!Strings.isNullOrEmpty(threads)) { |
| 313 | this.workerThreads = Integer.parseInt(threads); |
| 314 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 315 | log.debug("Number of worker threads set to {}", this.workerThreads); |
Anton Chigrin | 4af4f87 | 2019-01-14 17:29:56 +0200 | [diff] [blame^] | 316 | |
| 317 | setQueueParams(properties, "defaultQueueSize", "defaultBulkSize", DEFAULT_QUEUE_ID); |
| 318 | setQueueParams(properties, "queueSizeN0", "bulkSizeN0", 0); |
| 319 | setQueueParams(properties, "queueSizeN1", "bulkSizeN1", 1); |
| 320 | setQueueParams(properties, "queueSizeN2", "bulkSizeN2", 2); |
| 321 | setQueueParams(properties, "queueSizeN3", "bulkSizeN3", 3); |
| 322 | setQueueParams(properties, "queueSizeN4", "bulkSizeN4", 4); |
| 323 | setQueueParams(properties, "queueSizeN5", "bulkSizeN5", 5); |
| 324 | setQueueParams(properties, "queueSizeN6", "bulkSizeN6", 6); |
| 325 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 326 | return oldValue != this.workerThreads; // restart if number of threads has changed |
| 327 | } |
| 328 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 329 | static class TlsParams { |
| 330 | final TlsMode mode; |
| 331 | final String ksLocation; |
| 332 | final String tsLocation; |
| 333 | final String ksPwd; |
| 334 | final String tsPwd; |
| 335 | final byte[] ksSignature; |
| 336 | final byte[] tsSignature; |
| 337 | |
| 338 | TlsParams(TlsMode mode, String ksLocation, String tsLocation, |
| 339 | String ksPwd, String tsPwd) { |
| 340 | this.mode = mode; |
| 341 | this.ksLocation = ksLocation; |
| 342 | this.tsLocation = tsLocation; |
| 343 | this.ksPwd = ksPwd; |
| 344 | this.tsPwd = tsPwd; |
| 345 | this.ksSignature = getSha1Checksum(ksLocation); |
| 346 | this.tsSignature = getSha1Checksum(tsLocation); |
| 347 | } |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 348 | |
| 349 | public char[] ksPwd() { |
| 350 | return ksPwd.toCharArray(); |
| 351 | } |
| 352 | |
| 353 | public char[] tsPwd() { |
| 354 | return tsPwd.toCharArray(); |
| 355 | } |
| 356 | |
| 357 | public boolean isTlsEnabled() { |
| 358 | return TLS_ENABLED.contains(mode); |
| 359 | } |
| 360 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 361 | public byte[] getSha1Checksum(String filepath) { |
| 362 | if (filepath == null) { |
| 363 | return new byte[0]; |
| 364 | } |
| 365 | try { |
| 366 | MessageDigest digest = MessageDigest.getInstance("SHA1"); |
| 367 | File f = new File(filepath); |
| 368 | FileInputStream is = new FileInputStream(f); |
| 369 | DigestInputStream dis = new DigestInputStream(is, digest); |
| 370 | byte[] buffer = new byte[1024]; |
| 371 | while (dis.read(buffer) > 0) { |
| 372 | // nothing to do :) |
| 373 | } |
柯志勇10068695 | 55f80b6 | 2018-10-11 17:28:31 +0800 | [diff] [blame] | 374 | is.close(); |
| 375 | return dis.getMessageDigest().digest(); |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 376 | } catch (NoSuchAlgorithmException ignored) { |
| 377 | } catch (IOException e) { |
| 378 | log.info("Error reading file file: {}", filepath); |
| 379 | } |
| 380 | return new byte[0]; |
| 381 | } |
| 382 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 383 | @Override |
| 384 | public int hashCode() { |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 385 | if (mode == TlsMode.DISABLED) { |
| 386 | return Objects.hash(mode); |
| 387 | } |
| 388 | return Objects.hash(mode, ksLocation, tsLocation, |
Yuta HIGUCHI | cf03a0f | 2018-05-15 19:25:28 -0700 | [diff] [blame] | 389 | ksPwd, tsPwd, |
| 390 | Arrays.hashCode(ksSignature), |
| 391 | Arrays.hashCode(tsSignature)); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public boolean equals(Object obj) { |
| 396 | if (this == obj) { |
| 397 | return true; |
| 398 | } |
| 399 | if (obj instanceof TlsParams) { |
| 400 | final TlsParams that = (TlsParams) obj; |
| 401 | if (this.getClass() != that.getClass()) { |
| 402 | return false; |
| 403 | } else if (this.mode == that.mode && this.mode == TlsMode.DISABLED) { |
| 404 | // All disabled objects should be equal regardless of other params |
| 405 | return true; |
| 406 | } |
| 407 | return this.mode == that.mode && |
| 408 | Objects.equals(this.ksLocation, that.ksLocation) && |
| 409 | Objects.equals(this.tsLocation, that.tsLocation) && |
| 410 | Objects.equals(this.ksPwd, that.ksPwd) && |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 411 | Objects.equals(this.tsPwd, that.tsPwd) && |
| 412 | Arrays.equals(this.ksSignature, that.ksSignature) && |
| 413 | Arrays.equals(this.tsSignature, that.tsSignature); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | @Override |
| 419 | public String toString() { |
| 420 | return MoreObjects.toStringHelper(this) |
| 421 | .add("tlsMode", mode.toString().toLowerCase()) |
| 422 | .add("ksLocation", ksLocation) |
| 423 | .add("tsLocation", tsLocation) |
| 424 | .toString(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Gets the TLS parameters from the properties dict, but fallback to the old approach of |
| 430 | * system properties if the parameters are missing from the dict. |
| 431 | * |
| 432 | * @param properties dictionary |
| 433 | * @return true if restart is required |
| 434 | */ |
| 435 | private boolean setTlsParameters(Dictionary<?, ?> properties) { |
| 436 | TlsParams oldParams = this.tlsParams; |
| 437 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 438 | TlsMode mode = null; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 439 | String tlsString = get(properties, "tlsMode"); |
| 440 | if (!Strings.isNullOrEmpty(tlsString)) { |
| 441 | try { |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 442 | mode = TlsMode.valueOf(tlsString.toUpperCase()); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 443 | } catch (IllegalArgumentException e) { |
| 444 | log.info("Invalid TLS mode {}. TLS is disabled.", tlsString); |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 445 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 446 | } |
| 447 | } else { |
| 448 | // Fallback to system properties |
| 449 | // TODO this method of configuring TLS is deprecated and should be removed eventually |
| 450 | tlsString = System.getProperty("enableOFTLS"); |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 451 | mode = !Strings.isNullOrEmpty(tlsString) && Boolean.parseBoolean(tlsString) ? |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 452 | TlsMode.ENABLED : TlsMode.DISABLED; |
| 453 | } |
| 454 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 455 | String ksLocation = null, tsLocation = null, ksPwd = null, tsPwd = null; |
| 456 | if (TLS_ENABLED.contains(mode)) { |
| 457 | ksLocation = get(properties, "keyStore"); |
| 458 | if (Strings.isNullOrEmpty(ksLocation)) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 459 | // Fallback to system properties |
| 460 | // TODO remove this eventually |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 461 | ksLocation = System.getProperty("javax.net.ssl.keyStore"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 462 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 463 | if (Strings.isNullOrEmpty(ksLocation)) { |
| 464 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 467 | tsLocation = get(properties, "trustStore"); |
| 468 | if (Strings.isNullOrEmpty(tsLocation)) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 469 | // Fallback to system properties |
| 470 | // TODO remove this eventually |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 471 | tsLocation = System.getProperty("javax.net.ssl.trustStore"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 472 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 473 | if (Strings.isNullOrEmpty(tsLocation)) { |
| 474 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 477 | ksPwd = get(properties, "keyStorePassword"); |
| 478 | if (Strings.isNullOrEmpty(ksPwd)) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 479 | // Fallback to system properties |
| 480 | // TODO remove this eventually |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 481 | ksPwd = System.getProperty("javax.net.ssl.keyStorePassword"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 482 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 483 | if (Strings.isNullOrEmpty(ksPwd) || MIN_KS_LENGTH > ksPwd.length()) { |
| 484 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 487 | tsPwd = get(properties, "trustStorePassword"); |
| 488 | if (Strings.isNullOrEmpty(tsPwd)) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 489 | // Fallback to system properties |
| 490 | // TODO remove this eventually |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 491 | tsPwd = System.getProperty("javax.net.ssl.trustStorePassword"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 492 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 493 | if (Strings.isNullOrEmpty(tsPwd) || MIN_KS_LENGTH > tsPwd.length()) { |
| 494 | mode = TlsMode.DISABLED; |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 497 | this.tlsParams = new TlsParams(mode, ksLocation, tsLocation, ksPwd, tsPwd); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 498 | log.info("OpenFlow TLS Params: {}", tlsParams); |
Brian O'Connor | 1bd4a9f | 2018-05-10 22:40:41 -0700 | [diff] [blame] | 499 | return !Objects.equals(this.tlsParams, oldParams); // restart if TLS params change |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 500 | } |
| 501 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 502 | /** |
| 503 | * Initialize internal data structures. |
| 504 | */ |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 505 | public void init() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 506 | // These data structures are initialized here because other |
| 507 | // module's startUp() might be called before ours |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 508 | this.controllerNodeIPsCache = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 509 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 510 | this.systemStartTime = System.currentTimeMillis(); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 511 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 512 | cg = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); |
| 513 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 514 | if (tlsParams.isTlsEnabled()) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 515 | initSsl(); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 516 | } |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 519 | private void initSsl() { |
| 520 | try { |
| 521 | TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 522 | KeyStore ts = KeyStore.getInstance("JKS"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 523 | ts.load(new FileInputStream(tlsParams.tsLocation), tlsParams.tsPwd()); |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 524 | tmFactory.init(ts); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 525 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 526 | KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 527 | keyStore = KeyStore.getInstance("JKS"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 528 | keyStore.load(new FileInputStream(tlsParams.ksLocation), tlsParams.ksPwd()); |
| 529 | kmf.init(keyStore, tlsParams.ksPwd()); |
alshabib | 9b6c19c | 2015-09-26 12:19:27 -0700 | [diff] [blame] | 530 | |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 531 | sslContext = SSLContext.getInstance("TLS"); |
| 532 | sslContext.init(kmf.getKeyManagers(), tmFactory.getTrustManagers(), null); |
| 533 | } catch (NoSuchAlgorithmException | KeyStoreException | CertificateException | |
| 534 | IOException | KeyManagementException | UnrecoverableKeyException ex) { |
| 535 | log.error("SSL init failed: {}", ex.getMessage()); |
| 536 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // ************** |
| 540 | // Utility methods |
| 541 | // ************** |
| 542 | |
| 543 | public Map<String, Long> getMemory() { |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 544 | Map<String, Long> m = new HashMap<>(); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 545 | Runtime runtime = Runtime.getRuntime(); |
| 546 | m.put("total", runtime.totalMemory()); |
| 547 | m.put("free", runtime.freeMemory()); |
| 548 | return m; |
| 549 | } |
| 550 | |
| 551 | |
Ray Milkey | dc0ff19 | 2015-11-04 13:49:52 -0800 | [diff] [blame] | 552 | public Long getSystemUptime() { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 553 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 554 | return rb.getUptime(); |
| 555 | } |
| 556 | |
Ray Milkey | dc0ff19 | 2015-11-04 13:49:52 -0800 | [diff] [blame] | 557 | public long getSystemStartTime() { |
| 558 | return (this.systemStartTime); |
| 559 | } |
| 560 | |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 561 | public boolean isValidCertificate(Long dpid, Certificate peerCert) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 562 | if (!tlsParams.isTlsEnabled()) { |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 563 | return true; |
| 564 | } |
| 565 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 566 | if (netCfgService == null) { |
| 567 | // netcfg service not available; accept any cert if not in strict mode |
| 568 | return tlsParams.mode == TlsMode.ENABLED; |
| 569 | } |
| 570 | |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 571 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(new Dpid(dpid))); |
| 572 | OpenFlowDeviceConfig config = |
| 573 | netCfgService.getConfig(deviceId, OpenFlowDeviceConfig.class); |
| 574 | if (config == null) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 575 | // Config not set for device, accept any cert if not in strict mode |
| 576 | return tlsParams.mode == TlsMode.ENABLED; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | Optional<String> alias = config.keyAlias(); |
| 580 | if (!alias.isPresent()) { |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 581 | // Config for device does not specify a certificate chain, accept any cert if not in strict mode |
| 582 | return tlsParams.mode == TlsMode.ENABLED; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | try { |
| 586 | Certificate configuredCert = keyStore.getCertificate(alias.get()); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 587 | //TODO there's probably a better way to compare these |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 588 | return Objects.deepEquals(peerCert, configuredCert); |
| 589 | } catch (KeyStoreException e) { |
| 590 | log.info("failed to load key", e); |
| 591 | } |
| 592 | return false; |
| 593 | } |
| 594 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 595 | /** |
| 596 | * Forward to the driver-manager to get an IOFSwitch instance. |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 597 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 598 | * @param dpid data path id |
| 599 | * @param desc switch description |
Thomas Vachuska | 6f94ded | 2015-02-21 14:02:38 -0800 | [diff] [blame] | 600 | * @param ofv OpenFlow version |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 601 | * @return switch instance |
| 602 | */ |
| 603 | protected OpenFlowSwitchDriver getOFSwitchInstance(long dpid, |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 604 | OFDescStatsReply desc, |
| 605 | OFVersion ofv) { |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 606 | Dpid dpidObj = new Dpid(dpid); |
| 607 | |
| 608 | Driver driver; |
| 609 | try { |
Sho SHIMIZU | bc82ebb | 2015-08-25 10:15:21 -0700 | [diff] [blame] | 610 | driver = driverService.getDriver(DeviceId.deviceId(Dpid.uri(dpidObj))); |
Jonathan Hart | a0d1249 | 2015-07-16 12:03:41 -0700 | [diff] [blame] | 611 | } catch (ItemNotFoundException e) { |
| 612 | driver = driverService.getDriver(desc.getMfrDesc(), desc.getHwDesc(), desc.getSwDesc()); |
| 613 | } |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 614 | |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 615 | if (driver == null) { |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 616 | log.error("No OpenFlow driver for {} : {}", dpidObj, desc); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 617 | return null; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 618 | } |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 619 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 620 | log.info("Driver '{}' assigned to device {}", driver.name(), dpidObj); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 621 | |
| 622 | if (!driver.hasBehaviour(OpenFlowSwitchDriver.class)) { |
| 623 | log.error("Driver {} does not support OpenFlowSwitchDriver behaviour", driver.name()); |
| 624 | return null; |
| 625 | } |
| 626 | |
| 627 | DefaultDriverHandler handler = |
| 628 | new DefaultDriverHandler(new DefaultDriverData(driver, deviceId(uri(dpidObj)))); |
| 629 | OpenFlowSwitchDriver ofSwitchDriver = |
| 630 | driver.createBehaviour(handler, OpenFlowSwitchDriver.class); |
| 631 | ofSwitchDriver.init(dpidObj, desc, ofv); |
| 632 | ofSwitchDriver.setAgent(agent); |
| 633 | ofSwitchDriver.setRoleHandler(new RoleManager(ofSwitchDriver)); |
Jonathan Hart | a33134e | 2016-07-27 17:33:35 -0700 | [diff] [blame] | 634 | return ofSwitchDriver; |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 637 | public void start(OpenFlowAgent ag, DriverService driverService, |
| 638 | NetworkConfigRegistry netCfgService) { |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 639 | log.info("Starting OpenFlow IO"); |
| 640 | this.agent = ag; |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 641 | this.driverService = driverService; |
Brian O'Connor | f69e3e3 | 2018-05-10 02:25:09 -0700 | [diff] [blame] | 642 | this.netCfgService = netCfgService; |
Jonathan Hart | bbd91d4 | 2015-02-27 11:18:04 -0800 | [diff] [blame] | 643 | this.init(); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 644 | this.addListeningPorts(this.openFlowPorts); |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | |
| 648 | public void stop() { |
| 649 | log.info("Stopping OpenFlow IO"); |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 650 | if (cg != null) { |
| 651 | cg.close(); |
| 652 | cg = null; |
| 653 | } |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame] | 654 | |
| 655 | // Shut down all event loops to terminate all threads. |
| 656 | bossGroup.shutdownGracefully(); |
| 657 | workerGroup.shutdownGracefully(); |
| 658 | |
| 659 | // Wait until all threads are terminated. |
| 660 | try { |
| 661 | bossGroup.terminationFuture().sync(); |
| 662 | workerGroup.terminationFuture().sync(); |
| 663 | } catch (InterruptedException e) { |
| 664 | log.warn("Interrupted while stopping", e); |
| 665 | Thread.currentThread().interrupt(); |
| 666 | } |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Brian O'Connor | f7215b8 | 2018-05-10 19:12:44 -0700 | [diff] [blame] | 669 | private void restart() { |
| 670 | // only restart if we are already running |
| 671 | if (cg != null) { |
| 672 | stop(); |
| 673 | start(this.agent, this.driverService, this.netCfgService); |
| 674 | } |
| 675 | } |
| 676 | |
tom | 7ef8ff9 | 2014-09-17 13:08:06 -0700 | [diff] [blame] | 677 | } |