Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.bgp.controller.impl; |
| 17 | |
| 18 | import static org.onlab.util.Tools.groupedThreads; |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 19 | import io.netty.util.internal.PlatformDependent; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 20 | import java.lang.management.ManagementFactory; |
| 21 | import java.lang.management.RuntimeMXBean; |
| 22 | import java.net.InetSocketAddress; |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | import java.util.concurrent.Executors; |
| 26 | |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 27 | import org.jboss.netty.bootstrap.ClientBootstrap; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 28 | import org.jboss.netty.bootstrap.ServerBootstrap; |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 29 | import org.jboss.netty.channel.Channel; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 30 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 31 | import org.jboss.netty.channel.group.ChannelGroup; |
| 32 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 33 | import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 34 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 35 | import org.onosproject.bgp.controller.BgpController; |
| 36 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 37 | import org.onosproject.bgpio.protocol.BgpFactory; |
| 38 | import org.onosproject.bgpio.protocol.BgpVersion; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 39 | import org.slf4j.Logger; |
| 40 | import org.slf4j.LoggerFactory; |
| 41 | |
| 42 | /** |
| 43 | * The main controller class. Handles all setup and network listeners - Distributed ownership control of bgp peer |
| 44 | * through IControllerRegistryService |
| 45 | */ |
| 46 | public class Controller { |
| 47 | |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 48 | private static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 49 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 50 | private static final BgpFactory FACTORY4 = BgpFactories.getFactory(BgpVersion.BGP_4); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 51 | |
| 52 | private ChannelGroup cg; |
Ray Milkey | e4afdb5 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 53 | private Channel serverChannel; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 54 | |
| 55 | // Configuration options |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 56 | protected static final short BGP_PORT_NUM = 179; |
| 57 | private static final short BGP_PRIVILEGED_PORT = 1790; // server port used for non root users in linux |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 58 | private static final short PORT_NUM_ZERO = 0; |
| 59 | private static boolean isPortNumSet = false; |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 60 | private static short portNumber = BGP_PORT_NUM; |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 61 | private final int workerThreads = 16; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 62 | private final int peerWorkerThreads = 16; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 63 | |
| 64 | // Start time of the controller |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 65 | private long systemStartTime; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 66 | |
| 67 | private NioServerSocketChannelFactory serverExecFactory; |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 68 | private NioClientSocketChannelFactory peerExecFactory; |
| 69 | private static ClientBootstrap peerBootstrap; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 70 | private BgpController bgpController; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 71 | |
| 72 | // Perf. related configuration |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 73 | private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 74 | |
| 75 | /** |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 76 | * Constructor to initialize the values. |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 77 | * |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 78 | * @param bgpController bgp controller instance |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 79 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 80 | public Controller(BgpController bgpController) { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 81 | this.bgpController = bgpController; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns factory version for processing BGP messages. |
| 86 | * |
| 87 | * @return instance of factory version |
| 88 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 89 | static BgpFactory getBgpMessageFactory4() { |
Shashikanth VH | 9f8afb4 | 2015-11-04 18:00:30 +0530 | [diff] [blame] | 90 | return FACTORY4; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 91 | } |
| 92 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 93 | /** |
| 94 | * To get system start time. |
| 95 | * |
| 96 | * @return system start time in milliseconds |
| 97 | */ |
| 98 | public long getSystemStartTime() { |
| 99 | return (this.systemStartTime); |
| 100 | } |
| 101 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 102 | /** |
| 103 | * Tell controller that we're ready to accept bgp peer connections. |
| 104 | */ |
| 105 | public void run() { |
| 106 | |
| 107 | try { |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 108 | |
| 109 | peerBootstrap = createPeerBootStrap(); |
| 110 | |
| 111 | peerBootstrap.setOption("reuseAddr", true); |
| 112 | peerBootstrap.setOption("child.keepAlive", true); |
| 113 | peerBootstrap.setOption("child.tcpNoDelay", true); |
| 114 | peerBootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 115 | |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 116 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 117 | |
| 118 | bootstrap.setOption("reuseAddr", true); |
| 119 | bootstrap.setOption("child.keepAlive", true); |
| 120 | bootstrap.setOption("child.tcpNoDelay", true); |
| 121 | bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE); |
| 122 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 123 | ChannelPipelineFactory pfact = new BgpPipelineFactory(bgpController, true); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 124 | |
| 125 | bootstrap.setPipelineFactory(pfact); |
| 126 | InetSocketAddress sa = new InetSocketAddress(getBgpPortNum()); |
| 127 | cg = new DefaultChannelGroup(); |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 128 | serverChannel = bootstrap.bind(sa); |
| 129 | cg.add(serverChannel); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 130 | log.info("Listening for Peer connection on {}", sa); |
| 131 | } catch (Exception e) { |
| 132 | throw new RuntimeException(e); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Creates server boot strap. |
| 138 | * |
| 139 | * @return ServerBootStrap |
| 140 | */ |
| 141 | private ServerBootstrap createServerBootStrap() { |
| 142 | |
| 143 | if (workerThreads == 0) { |
| 144 | serverExecFactory = new NioServerSocketChannelFactory( |
| 145 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 146 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d"))); |
| 147 | return new ServerBootstrap(serverExecFactory); |
| 148 | } else { |
| 149 | serverExecFactory = new NioServerSocketChannelFactory( |
| 150 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 151 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")), |
| 152 | workerThreads); |
| 153 | return new ServerBootstrap(serverExecFactory); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 158 | * Creates peer boot strap. |
| 159 | * |
| 160 | * @return ClientBootstrap |
| 161 | */ |
| 162 | private ClientBootstrap createPeerBootStrap() { |
| 163 | |
| 164 | if (peerWorkerThreads == 0) { |
| 165 | peerExecFactory = new NioClientSocketChannelFactory( |
| 166 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 167 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d"))); |
| 168 | return new ClientBootstrap(peerExecFactory); |
| 169 | } else { |
| 170 | peerExecFactory = new NioClientSocketChannelFactory( |
| 171 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "boss-%d")), |
| 172 | Executors.newCachedThreadPool(groupedThreads("onos/bgp", "worker-%d")), |
| 173 | peerWorkerThreads); |
| 174 | return new ClientBootstrap(peerExecFactory); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets peer bootstrap. |
| 180 | * |
| 181 | * @return peer bootstrap |
| 182 | */ |
| 183 | public static ClientBootstrap peerBootstrap() { |
| 184 | return peerBootstrap; |
| 185 | } |
| 186 | |
| 187 | /** |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 188 | * Initialize internal data structures. |
| 189 | */ |
| 190 | public void init() { |
| 191 | // These data structures are initialized here because other |
| 192 | // module's startUp() might be called before ours |
| 193 | this.systemStartTime = System.currentTimeMillis(); |
| 194 | } |
| 195 | |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 196 | /** |
| 197 | * Gets run time memory. |
| 198 | * |
| 199 | * @return m run time memory |
| 200 | */ |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 201 | public Map<String, Long> getMemory() { |
| 202 | Map<String, Long> m = new HashMap<>(); |
| 203 | Runtime runtime = Runtime.getRuntime(); |
| 204 | m.put("total", runtime.totalMemory()); |
| 205 | m.put("free", runtime.freeMemory()); |
| 206 | return m; |
| 207 | } |
| 208 | |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 209 | /** |
| 210 | * Gets UP time. |
| 211 | * |
| 212 | * @return UP time |
| 213 | */ |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 214 | public Long getUptime() { |
| 215 | RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean(); |
| 216 | return rb.getUptime(); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Starts the BGP controller. |
| 221 | */ |
| 222 | public void start() { |
| 223 | log.info("Started"); |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 224 | if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot()) { |
| 225 | portNumber = BGP_PRIVILEGED_PORT; |
| 226 | } else { |
| 227 | portNumber = BGP_PORT_NUM; |
| 228 | } |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 229 | this.init(); |
| 230 | this.run(); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Stops the BGP controller. |
| 235 | */ |
| 236 | public void stop() { |
| 237 | log.info("Stopped"); |
| 238 | serverExecFactory.shutdown(); |
Shashikanth VH | fd75b84 | 2015-11-18 19:10:13 +0530 | [diff] [blame] | 239 | peerExecFactory.shutdown(); |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 240 | cg.close(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Returns port number. |
| 245 | * |
| 246 | * @return port number |
| 247 | */ |
| 248 | public static short getBgpPortNum() { |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 249 | if (isPortNumSet) { |
| 250 | return PORT_NUM_ZERO; |
| 251 | } |
Shashikanth VH | 83f77e4 | 2016-05-26 20:34:56 +0530 | [diff] [blame] | 252 | |
| 253 | return portNumber; |
Satish K | e107e66 | 2015-09-21 19:00:17 +0530 | [diff] [blame] | 254 | } |
Vidyashree Rama | 7bd3d78 | 2015-11-23 08:46:33 +0530 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * sets the isPortNumSet as true. |
| 258 | */ |
| 259 | public void setBgpPortNum() { |
| 260 | isPortNumSet = true; |
| 261 | } |
Ray Milkey | e4afdb5 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 262 | } |