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 | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Stitches all channel handlers into server bootstrap. |
| 54 | */ |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 55 | private void run() { |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 56 | |
| 57 | try { |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 58 | final Bootstrap bootstrap = createServerBootstrap(); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 59 | |
| 60 | configBootstrapOptions(bootstrap); |
| 61 | |
| 62 | List<ChannelFuture> channelFutures = Lists.newArrayList(); |
| 63 | |
| 64 | lispPorts.forEach(p -> { |
| 65 | InetSocketAddress sa = new InetSocketAddress(p); |
| 66 | channelFutures.add(bootstrap.bind(sa)); |
| 67 | log.info("Listening for LISP router connections on {}", sa); |
| 68 | }); |
| 69 | |
| 70 | for (ChannelFuture f : channelFutures) { |
| 71 | f.sync(); |
| 72 | } |
| 73 | |
| 74 | } catch (Exception e) { |
| 75 | throw new RuntimeException(e); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Initializes server bootstrap with given LISP channel initializer. |
| 81 | * |
| 82 | * @return initialized server bootstrap |
| 83 | */ |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 84 | private Bootstrap createServerBootstrap() { |
| 85 | Bootstrap bootstrap = new Bootstrap(); |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 86 | |
| 87 | initEventLoopGroup(); |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 88 | bootstrap.group(eventLoopGroup) |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 89 | .channel(channelClass) |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 90 | .handler(new LispChannelInitializer()); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 91 | |
| 92 | return bootstrap; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Configures bootstrap options to tune the communication performance. |
| 97 | * |
| 98 | * @param bootstrap LISP server bootstrap |
| 99 | */ |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 100 | private void configBootstrapOptions(Bootstrap bootstrap) { |
| 101 | bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Closes all open channels. |
| 106 | * |
| 107 | * @param channelFutures a collection of channel futures |
| 108 | */ |
| 109 | private void closeChannels(List<ChannelFuture> channelFutures) { |
| 110 | try { |
| 111 | for (ChannelFuture f : channelFutures) { |
| 112 | f.channel().closeFuture().sync(); |
| 113 | } |
| 114 | } catch (InterruptedException e) { |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 115 | log.warn("Failed to close channels. Reasons: {}.", e.getMessage()); |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
Jian Li | 5ba6ec1 | 2016-11-18 02:38:58 +0900 | [diff] [blame] | 120 | * Initializes event loop group. |
| 121 | */ |
| 122 | private void initEventLoopGroup() { |
| 123 | |
| 124 | // try to use EpollEventLoopGroup if possible, |
| 125 | // if OS does not support native Epoll, fallback to use netty NIO |
| 126 | try { |
| 127 | eventLoopGroup = new EpollEventLoopGroup(); |
| 128 | channelClass = EpollDatagramChannel.class; |
| 129 | } catch (Throwable e) { |
| 130 | log.debug("Failed to initialize native (epoll) transport. " |
| 131 | + "Reason: {}. Proceeding with NIO event group.", e.getMessage()); |
| 132 | } |
| 133 | eventLoopGroup = new NioEventLoopGroup(); |
| 134 | channelClass = NioDatagramChannel.class; |
| 135 | } |
| 136 | |
| 137 | /** |
Jian Li | 451cea3 | 2016-10-04 15:27:50 +0900 | [diff] [blame] | 138 | * Launches LISP controller to listen control channel. |
| 139 | */ |
| 140 | public void start() { |
| 141 | log.info("Starting LISP control I/O"); |
| 142 | |
| 143 | this.run(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Terminates LISP controller and lease all occupied resources. |
| 148 | */ |
| 149 | public void stop() { |
| 150 | log.info("Stopping LISP control I/O"); |
| 151 | |
| 152 | try { |
| 153 | // try to shutdown all open event groups |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 154 | eventLoopGroup.shutdownGracefully().sync(); |
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 | } |