Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -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 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame^] | 16 | package org.onosproject.routing.bgp; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame^] | 18 | import org.apache.felix.scr.annotations.Component; |
| 19 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 20 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 21 | import org.jboss.netty.channel.Channel; |
| 22 | import org.jboss.netty.channel.ChannelException; |
| 23 | import org.jboss.netty.channel.ChannelFactory; |
| 24 | import org.jboss.netty.channel.ChannelPipeline; |
| 25 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 26 | import org.jboss.netty.channel.Channels; |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 27 | import org.jboss.netty.channel.group.ChannelGroup; |
| 28 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 29 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 30 | import org.onlab.packet.Ip4Address; |
| 31 | import org.onlab.packet.Ip4Prefix; |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 32 | import org.onlab.packet.Ip6Prefix; |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 33 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame^] | 34 | import org.onosproject.routingapi.BgpService; |
| 35 | import org.onosproject.routingapi.RouteListener; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 39 | import java.net.InetAddress; |
| 40 | import java.net.InetSocketAddress; |
| 41 | import java.net.SocketAddress; |
| 42 | import java.util.Collection; |
| 43 | import java.util.concurrent.ConcurrentHashMap; |
| 44 | import java.util.concurrent.ConcurrentMap; |
| 45 | |
| 46 | import static com.google.common.base.Preconditions.checkNotNull; |
| 47 | import static java.util.concurrent.Executors.newCachedThreadPool; |
| 48 | import static org.onlab.util.Tools.namedThreads; |
| 49 | |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 50 | /** |
| 51 | * BGP Session Manager class. |
| 52 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame^] | 53 | @Component(immediate = true) |
| 54 | @Service |
| 55 | public class BgpSessionManager implements BgpInfoService, BgpService { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 56 | private static final Logger log = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 57 | LoggerFactory.getLogger(BgpSessionManager.class); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 58 | |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 59 | boolean isShutdown = true; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 60 | private Channel serverChannel; // Listener for incoming BGP connections |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 61 | private ServerBootstrap serverBootstrap; |
| 62 | private ChannelGroup allChannels = new DefaultChannelGroup(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 63 | private ConcurrentMap<SocketAddress, BgpSession> bgpSessions = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 64 | new ConcurrentHashMap<>(); |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 65 | private Ip4Address myBgpId; // Same BGP ID for all peers |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 66 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 67 | private BgpRouteSelector bgpRouteSelector = new BgpRouteSelector(this); |
| 68 | private ConcurrentMap<Ip4Prefix, BgpRouteEntry> bgpRoutes4 = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 69 | new ConcurrentHashMap<>(); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 70 | private ConcurrentMap<Ip6Prefix, BgpRouteEntry> bgpRoutes6 = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 71 | new ConcurrentHashMap<>(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 72 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame^] | 73 | private RouteListener routeListener; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 76 | * Checks whether the BGP Session Manager is shutdown. |
| 77 | * |
| 78 | * @return true if the BGP Session Manager is shutdown, otherwise false |
| 79 | */ |
| 80 | boolean isShutdown() { |
| 81 | return this.isShutdown; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets the route listener. |
| 86 | * |
| 87 | * @return the route listener to use |
| 88 | */ |
| 89 | RouteListener getRouteListener() { |
| 90 | return routeListener; |
| 91 | } |
| 92 | |
| 93 | /** |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 94 | * Gets the BGP sessions. |
| 95 | * |
| 96 | * @return the BGP sessions |
| 97 | */ |
| 98 | public Collection<BgpSession> getBgpSessions() { |
| 99 | return bgpSessions.values(); |
| 100 | } |
| 101 | |
| 102 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 103 | * Gets the selected IPv4 BGP routes among all BGP sessions. |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 104 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 105 | * @return the selected IPv4 BGP routes among all BGP sessions |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 106 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 107 | public Collection<BgpRouteEntry> getBgpRoutes4() { |
| 108 | return bgpRoutes4.values(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Gets the selected IPv6 BGP routes among all BGP sessions. |
| 113 | * |
| 114 | * @return the selected IPv6 BGP routes among all BGP sessions |
| 115 | */ |
| 116 | public Collection<BgpRouteEntry> getBgpRoutes6() { |
| 117 | return bgpRoutes6.values(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Finds a BGP route for a prefix. The prefix can be either IPv4 or IPv6. |
| 122 | * |
| 123 | * @param prefix the prefix to use |
| 124 | * @return the BGP route if found, otherwise null |
| 125 | */ |
| 126 | BgpRouteEntry findBgpRoute(IpPrefix prefix) { |
| 127 | if (prefix.version() == Ip4Address.VERSION) { |
| 128 | return bgpRoutes4.get(prefix.getIp4Prefix()); // IPv4 |
| 129 | } |
| 130 | return bgpRoutes6.get(prefix.getIp6Prefix()); // IPv6 |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Adds a BGP route. The route can be either IPv4 or IPv6. |
| 135 | * |
| 136 | * @param bgpRouteEntry the BGP route entry to use |
| 137 | */ |
| 138 | void addBgpRoute(BgpRouteEntry bgpRouteEntry) { |
| 139 | if (bgpRouteEntry.version() == Ip4Address.VERSION) { |
| 140 | bgpRoutes4.put(bgpRouteEntry.prefix().getIp4Prefix(), // IPv4 |
| 141 | bgpRouteEntry); |
| 142 | } else { |
| 143 | bgpRoutes6.put(bgpRouteEntry.prefix().getIp6Prefix(), // IPv6 |
| 144 | bgpRouteEntry); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Removes a BGP route for a prefix. The prefix can be either IPv4 or IPv6. |
| 150 | * |
| 151 | * @param prefix the prefix to use |
| 152 | * @return true if the route was found and removed, otherwise false |
| 153 | */ |
| 154 | boolean removeBgpRoute(IpPrefix prefix) { |
| 155 | if (prefix.version() == Ip4Address.VERSION) { |
| 156 | return (bgpRoutes4.remove(prefix.getIp4Prefix()) != null); // IPv4 |
| 157 | } |
| 158 | return (bgpRoutes6.remove(prefix.getIp6Prefix()) != null); // IPv6 |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 162 | * Adds the channel for a BGP session. |
| 163 | * |
| 164 | * @param channel the channel to add |
| 165 | */ |
| 166 | void addSessionChannel(Channel channel) { |
| 167 | allChannels.add(channel); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Removes the channel for a BGP session. |
| 172 | * |
| 173 | * @param channel the channel to remove |
| 174 | */ |
| 175 | void removeSessionChannel(Channel channel) { |
| 176 | allChannels.remove(channel); |
| 177 | } |
| 178 | |
| 179 | /** |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 180 | * Processes the connection from a BGP peer. |
| 181 | * |
| 182 | * @param bgpSession the BGP session for the peer |
| 183 | * @return true if the connection can be established, otherwise false |
| 184 | */ |
| 185 | boolean peerConnected(BgpSession bgpSession) { |
| 186 | |
| 187 | // Test whether there is already a session from the same remote |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 188 | if (bgpSessions.get(bgpSession.remoteInfo().address()) != null) { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 189 | return false; // Duplicate BGP session |
| 190 | } |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 191 | bgpSessions.put(bgpSession.remoteInfo().address(), bgpSession); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 192 | |
| 193 | // |
| 194 | // If the first connection, set my BGP ID to the local address |
| 195 | // of the socket. |
| 196 | // |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 197 | if (bgpSession.localInfo().address() instanceof InetSocketAddress) { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 198 | InetAddress inetAddr = |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 199 | ((InetSocketAddress) bgpSession.localInfo().address()).getAddress(); |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 200 | Ip4Address ip4Address = Ip4Address.valueOf(inetAddr.getAddress()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 201 | updateMyBgpId(ip4Address); |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Processes the disconnection from a BGP peer. |
| 208 | * |
| 209 | * @param bgpSession the BGP session for the peer |
| 210 | */ |
| 211 | void peerDisconnected(BgpSession bgpSession) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 212 | bgpSessions.remove(bgpSession.remoteInfo().address()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Conditionally updates the local BGP ID if it wasn't set already. |
| 217 | * <p/> |
| 218 | * NOTE: A BGP instance should use same BGP ID across all BGP sessions. |
| 219 | * |
| 220 | * @param ip4Address the IPv4 address to use as BGP ID |
| 221 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 222 | private synchronized void updateMyBgpId(Ip4Address ip4Address) { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 223 | if (myBgpId == null) { |
| 224 | myBgpId = ip4Address; |
| 225 | log.debug("BGP: My BGP ID is {}", myBgpId); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Gets the local BGP Identifier as an IPv4 address. |
| 231 | * |
| 232 | * @return the local BGP Identifier as an IPv4 address |
| 233 | */ |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 234 | Ip4Address getMyBgpId() { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 235 | return myBgpId; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Gets the BGP Route Selector. |
| 240 | * |
| 241 | * @return the BGP Route Selector |
| 242 | */ |
| 243 | BgpRouteSelector getBgpRouteSelector() { |
| 244 | return bgpRouteSelector; |
| 245 | } |
| 246 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame^] | 247 | @Override |
| 248 | public void start(RouteListener routeListener, int listenPortNumber) { |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 249 | log.debug("BGP Session Manager start."); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 250 | isShutdown = false; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 251 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame^] | 252 | this.routeListener = checkNotNull(routeListener); |
| 253 | |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 254 | ChannelFactory channelFactory = new NioServerSocketChannelFactory( |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 255 | newCachedThreadPool(namedThreads("onos-bgp-sm-boss-%d")), |
| 256 | newCachedThreadPool(namedThreads("onos-bgp-sm-worker-%d"))); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 257 | ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() { |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 258 | @Override |
| 259 | public ChannelPipeline getPipeline() throws Exception { |
| 260 | // Allocate a new session per connection |
| 261 | BgpSession bgpSessionHandler = |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 262 | new BgpSession(BgpSessionManager.this); |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 263 | BgpFrameDecoder bgpFrameDecoder = |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 264 | new BgpFrameDecoder(bgpSessionHandler); |
| 265 | |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 266 | // Setup the processing pipeline |
| 267 | ChannelPipeline pipeline = Channels.pipeline(); |
| 268 | pipeline.addLast("BgpFrameDecoder", bgpFrameDecoder); |
| 269 | pipeline.addLast("BgpSession", bgpSessionHandler); |
| 270 | return pipeline; |
| 271 | } |
| 272 | }; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 273 | InetSocketAddress listenAddress = |
Thomas Vachuska | 9ea3e6f | 2015-01-23 16:34:22 -0800 | [diff] [blame] | 274 | new InetSocketAddress(listenPortNumber); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 275 | |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 276 | serverBootstrap = new ServerBootstrap(channelFactory); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 277 | // serverBootstrap.setOptions("reuseAddr", true); |
| 278 | serverBootstrap.setOption("child.keepAlive", true); |
| 279 | serverBootstrap.setOption("child.tcpNoDelay", true); |
| 280 | serverBootstrap.setPipelineFactory(pipelineFactory); |
| 281 | try { |
| 282 | serverChannel = serverBootstrap.bind(listenAddress); |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 283 | allChannels.add(serverChannel); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 284 | } catch (ChannelException e) { |
| 285 | log.debug("Exception binding to BGP port {}: ", |
| 286 | listenAddress.getPort(), e); |
| 287 | } |
| 288 | } |
| 289 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame^] | 290 | @Override |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 291 | public void stop() { |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 292 | isShutdown = true; |
| 293 | allChannels.close().awaitUninterruptibly(); |
| 294 | serverBootstrap.releaseExternalResources(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 295 | } |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 296 | } |