Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | */ |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 16 | package org.onosproject.lisp.ctl.impl; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 20 | import io.netty.bootstrap.Bootstrap; |
| 21 | import io.netty.buffer.PooledByteBufAllocator; |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 22 | import io.netty.channel.AbstractChannel; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 23 | import io.netty.channel.ChannelFuture; |
| 24 | import io.netty.channel.ChannelOption; |
| 25 | import io.netty.channel.EventLoopGroup; |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 26 | import io.netty.channel.epoll.EpollDatagramChannel; |
| 27 | import io.netty.channel.epoll.EpollEventLoopGroup; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 28 | import io.netty.channel.nio.NioEventLoopGroup; |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 29 | import io.netty.channel.socket.nio.NioDatagramChannel; |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import java.net.InetSocketAddress; |
| 34 | import java.util.List; |
| 35 | |
| 36 | /** |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 37 | * Bootstraps LISP netty channel, handles all setup and network listeners. |
| 38 | */ |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 39 | public class LispControllerBootstrap { |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 40 | |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 41 | protected static final Logger log = LoggerFactory.getLogger(LispControllerBootstrap.class); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 42 | |
| 43 | private static final int LISP_DATA_PORT = 4341; |
| 44 | private static final int LISP_CONTROL_PORT = 4342; |
| 45 | |
| 46 | // Configuration options |
| 47 | protected List<Integer> lispPorts = ImmutableList.of(LISP_DATA_PORT, LISP_CONTROL_PORT); |
| 48 | |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 49 | private EventLoopGroup eventLoopGroup; |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 50 | private Class<? extends AbstractChannel> channelClass; |
Jian Li | 834ff72 | 2016-12-13 19:43:02 +0900 | [diff] [blame] | 51 | private List<ChannelFuture> channelFutures = Lists.newArrayList(); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Stitches all channel handlers into server bootstrap. |
| 55 | */ |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 56 | private void run() { |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 57 | |
| 58 | try { |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 59 | final Bootstrap bootstrap = createServerBootstrap(); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 60 | |
| 61 | configBootstrapOptions(bootstrap); |
| 62 | |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 63 | lispPorts.forEach(p -> { |
| 64 | InetSocketAddress sa = new InetSocketAddress(p); |
| 65 | channelFutures.add(bootstrap.bind(sa)); |
| 66 | log.info("Listening for LISP router connections on {}", sa); |
| 67 | }); |
| 68 | |
| 69 | for (ChannelFuture f : channelFutures) { |
| 70 | f.sync(); |
| 71 | } |
| 72 | |
| 73 | } catch (Exception e) { |
| 74 | throw new RuntimeException(e); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Initializes server bootstrap with given LISP channel initializer. |
| 80 | * |
| 81 | * @return initialized server bootstrap |
| 82 | */ |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 83 | private Bootstrap createServerBootstrap() { |
| 84 | Bootstrap bootstrap = new Bootstrap(); |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 85 | |
| 86 | initEventLoopGroup(); |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 87 | bootstrap.group(eventLoopGroup) |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 88 | .channel(channelClass) |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 89 | .handler(new LispChannelInitializer()); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 90 | |
| 91 | return bootstrap; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Configures bootstrap options to tune the communication performance. |
| 96 | * |
| 97 | * @param bootstrap LISP server bootstrap |
| 98 | */ |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 99 | private void configBootstrapOptions(Bootstrap bootstrap) { |
| 100 | bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Closes all open channels. |
| 105 | * |
| 106 | * @param channelFutures a collection of channel futures |
| 107 | */ |
| 108 | private void closeChannels(List<ChannelFuture> channelFutures) { |
| 109 | try { |
| 110 | for (ChannelFuture f : channelFutures) { |
| 111 | f.channel().closeFuture().sync(); |
| 112 | } |
| 113 | } catch (InterruptedException e) { |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 114 | log.warn("Failed to close channels. Reasons: {}.", e.getMessage()); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 119 | * Initializes event loop group. |
| 120 | */ |
| 121 | private void initEventLoopGroup() { |
| 122 | |
| 123 | // try to use EpollEventLoopGroup if possible, |
| 124 | // if OS does not support native Epoll, fallback to use netty NIO |
| 125 | try { |
| 126 | eventLoopGroup = new EpollEventLoopGroup(); |
| 127 | channelClass = EpollDatagramChannel.class; |
Jian Li | 47828a7 | 2017-02-14 20:56:31 +0900 | [diff] [blame^] | 128 | } catch (NoClassDefFoundError e) { |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 129 | log.debug("Failed to initialize native (epoll) transport. " |
Jian Li | 347c51e | 2017-01-18 14:08:50 -0800 | [diff] [blame] | 130 | + "Reason: {}. Proceeding with NIO event group.", e); |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 131 | } |
| 132 | eventLoopGroup = new NioEventLoopGroup(); |
| 133 | channelClass = NioDatagramChannel.class; |
| 134 | } |
| 135 | |
| 136 | /** |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 137 | * Launches LISP controller to listen control channel. |
| 138 | */ |
| 139 | public void start() { |
| 140 | log.info("Starting LISP control I/O"); |
| 141 | |
| 142 | this.run(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Terminates LISP controller and lease all occupied resources. |
| 147 | */ |
| 148 | public void stop() { |
| 149 | log.info("Stopping LISP control I/O"); |
| 150 | |
| 151 | try { |
| 152 | // try to shutdown all open event groups |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 153 | eventLoopGroup.shutdownGracefully().sync(); |
Jian Li | 834ff72 | 2016-12-13 19:43:02 +0900 | [diff] [blame] | 154 | closeChannels(channelFutures); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 155 | } catch (InterruptedException e) { |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 156 | log.warn("Failed to stop LISP controller. Reasons: {}.", e.getMessage()); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |