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