Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present 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 | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 18 | import org.jboss.netty.buffer.ChannelBuffer; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 19 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 20 | import org.jboss.netty.channel.ChannelStateEvent; |
Pavlin Radoslavov | 0a71472 | 2014-12-03 13:44:08 -0800 | [diff] [blame] | 21 | import org.jboss.netty.channel.ExceptionEvent; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 22 | import org.jboss.netty.channel.SimpleChannelHandler; |
| 23 | import org.jboss.netty.util.HashedWheelTimer; |
| 24 | import org.jboss.netty.util.Timeout; |
| 25 | import org.jboss.netty.util.Timer; |
| 26 | import org.jboss.netty.util.TimerTask; |
Jonathan Hart | e9ef4f3 | 2014-12-04 17:49:22 -0800 | [diff] [blame] | 27 | import org.onlab.packet.Ip4Address; |
| 28 | import org.onlab.packet.Ip4Prefix; |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 29 | import org.onlab.packet.Ip6Prefix; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 30 | import org.onlab.packet.IpPrefix; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 34 | import static org.onlab.util.Tools.groupedThreads; |
| 35 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 36 | import java.net.InetAddress; |
| 37 | import java.net.InetSocketAddress; |
| 38 | import java.util.Collection; |
| 39 | import java.util.Collections; |
| 40 | import java.util.concurrent.ConcurrentHashMap; |
| 41 | import java.util.concurrent.ConcurrentMap; |
| 42 | import java.util.concurrent.TimeUnit; |
| 43 | |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Class for handling the BGP peer sessions. |
| 46 | * There is one instance per each BGP peer session. |
| 47 | */ |
| 48 | public class BgpSession extends SimpleChannelHandler { |
| 49 | private static final Logger log = |
| 50 | LoggerFactory.getLogger(BgpSession.class); |
| 51 | |
| 52 | private final BgpSessionManager bgpSessionManager; |
| 53 | |
| 54 | // Local flag to indicate the session is closed. |
| 55 | // It is used to avoid the Netty's asynchronous closing of a channel. |
| 56 | private boolean isClosed = false; |
| 57 | |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 58 | // BGP session info: local and remote |
| 59 | private final BgpSessionInfo localInfo; // BGP session local info |
| 60 | private final BgpSessionInfo remoteInfo; // BGP session remote info |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 61 | |
| 62 | // Timers state |
Yuta HIGUCHI | 1624df1 | 2016-07-21 16:54:33 -0700 | [diff] [blame] | 63 | private Timer timer = new HashedWheelTimer(groupedThreads("BgpSession", "timer-%d", log)); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 64 | private volatile Timeout keepaliveTimeout; // Periodic KEEPALIVE |
| 65 | private volatile Timeout sessionTimeout; // Session timeout |
| 66 | |
| 67 | // BGP RIB-IN routing entries from this peer |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 68 | private ConcurrentMap<Ip4Prefix, BgpRouteEntry> bgpRibIn4 = |
| 69 | new ConcurrentHashMap<>(); |
| 70 | private ConcurrentMap<Ip6Prefix, BgpRouteEntry> bgpRibIn6 = |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 71 | new ConcurrentHashMap<>(); |
| 72 | |
| 73 | /** |
| 74 | * Constructor for a given BGP Session Manager. |
| 75 | * |
| 76 | * @param bgpSessionManager the BGP Session Manager to use |
| 77 | */ |
| 78 | BgpSession(BgpSessionManager bgpSessionManager) { |
| 79 | this.bgpSessionManager = bgpSessionManager; |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 80 | this.localInfo = new BgpSessionInfo(); |
| 81 | this.remoteInfo = new BgpSessionInfo(); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 82 | |
| 83 | // NOTE: We support only BGP4 |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 84 | this.localInfo.setBgpVersion(BgpConstants.BGP_VERSION); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Gets the BGP Session Manager. |
| 89 | * |
| 90 | * @return the BGP Session Manager |
| 91 | */ |
| 92 | BgpSessionManager getBgpSessionManager() { |
| 93 | return bgpSessionManager; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 97 | * Gets the BGP Session local information. |
| 98 | * |
| 99 | * @return the BGP Session local information. |
| 100 | */ |
| 101 | public BgpSessionInfo localInfo() { |
| 102 | return localInfo; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Gets the BGP Session remote information. |
| 107 | * |
| 108 | * @return the BGP Session remote information. |
| 109 | */ |
| 110 | public BgpSessionInfo remoteInfo() { |
| 111 | return remoteInfo; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Gets the BGP Multiprotocol Extensions for the session. |
| 116 | * |
| 117 | * @return true if the BGP Multiprotocol Extensions are enabled for the |
| 118 | * session, otherwise false |
| 119 | */ |
| 120 | public boolean mpExtensions() { |
| 121 | return remoteInfo.mpExtensions() && localInfo.mpExtensions(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Gets the BGP session 4 octet AS path capability. |
| 126 | * |
| 127 | * @return true when the BGP session is 4 octet AS path capable |
| 128 | */ |
| 129 | public boolean isAs4OctetCapable() { |
| 130 | return remoteInfo.as4OctetCapability() && |
| 131 | localInfo.as4OctetCapability(); |
| 132 | } |
| 133 | |
| 134 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 135 | * Gets the IPv4 BGP RIB-IN routing entries. |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 136 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 137 | * @return the IPv4 BGP RIB-IN routing entries |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 138 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 139 | public Collection<BgpRouteEntry> getBgpRibIn4() { |
| 140 | return bgpRibIn4.values(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 144 | * Gets the IPv6 BGP RIB-IN routing entries. |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 145 | * |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 146 | * @return the IPv6 BGP RIB-IN routing entries |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 147 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 148 | public Collection<BgpRouteEntry> getBgpRibIn6() { |
| 149 | return bgpRibIn6.values(); |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 153 | * Finds an IPv4 BGP routing entry for a prefix in the IPv4 BGP RIB-IN. |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 154 | * |
| 155 | * @param prefix the IPv4 prefix of the route to search for |
| 156 | * @return the IPv4 BGP routing entry if found, otherwise null |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 157 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 158 | public BgpRouteEntry findBgpRoute(Ip4Prefix prefix) { |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 159 | return bgpRibIn4.get(prefix); |
| 160 | } |
| 161 | |
| 162 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 163 | * Finds an IPv6 BGP routing entry for a prefix in the IPv6 BGP RIB-IN. |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 164 | * |
| 165 | * @param prefix the IPv6 prefix of the route to search for |
| 166 | * @return the IPv6 BGP routing entry if found, otherwise null |
| 167 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 168 | public BgpRouteEntry findBgpRoute(Ip6Prefix prefix) { |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 169 | return bgpRibIn6.get(prefix); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 173 | * Finds a BGP routing entry for a prefix in the BGP RIB-IN. The prefix |
| 174 | * can be either IPv4 or IPv6. |
| 175 | * |
| 176 | * @param prefix the IP prefix of the route to search for |
| 177 | * @return the BGP routing entry if found, otherwise null |
| 178 | */ |
| 179 | public BgpRouteEntry findBgpRoute(IpPrefix prefix) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 180 | if (prefix.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 181 | // IPv4 prefix |
| 182 | Ip4Prefix ip4Prefix = prefix.getIp4Prefix(); |
| 183 | return bgpRibIn4.get(ip4Prefix); |
| 184 | } |
| 185 | |
| 186 | // IPv6 prefix |
| 187 | Ip6Prefix ip6Prefix = prefix.getIp6Prefix(); |
| 188 | return bgpRibIn6.get(ip6Prefix); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Adds a BGP route. The route can be either IPv4 or IPv6. |
| 193 | * |
| 194 | * @param bgpRouteEntry the BGP route entry to use |
| 195 | */ |
| 196 | void addBgpRoute(BgpRouteEntry bgpRouteEntry) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 197 | if (bgpRouteEntry.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 198 | // IPv4 route |
| 199 | Ip4Prefix ip4Prefix = bgpRouteEntry.prefix().getIp4Prefix(); |
| 200 | bgpRibIn4.put(ip4Prefix, bgpRouteEntry); |
| 201 | } else { |
| 202 | // IPv6 route |
| 203 | Ip6Prefix ip6Prefix = bgpRouteEntry.prefix().getIp6Prefix(); |
| 204 | bgpRibIn6.put(ip6Prefix, bgpRouteEntry); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Removes an IPv4 BGP route for a prefix. |
| 210 | * |
| 211 | * @param prefix the prefix to use |
| 212 | * @return true if the route was found and removed, otherwise false |
| 213 | */ |
| 214 | boolean removeBgpRoute(Ip4Prefix prefix) { |
| 215 | return (bgpRibIn4.remove(prefix) != null); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Removes an IPv6 BGP route for a prefix. |
| 220 | * |
| 221 | * @param prefix the prefix to use |
| 222 | * @return true if the route was found and removed, otherwise false |
| 223 | */ |
| 224 | boolean removeBgpRoute(Ip6Prefix prefix) { |
| 225 | return (bgpRibIn6.remove(prefix) != null); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Removes a BGP route for a prefix. The prefix can be either IPv4 or IPv6. |
| 230 | * |
| 231 | * @param prefix the prefix to use |
| 232 | * @return true if the route was found and removed, otherwise false |
| 233 | */ |
| 234 | boolean removeBgpRoute(IpPrefix prefix) { |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 235 | if (prefix.isIp4()) { |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 236 | return (bgpRibIn4.remove(prefix.getIp4Prefix()) != null); // IPv4 |
| 237 | } |
| 238 | return (bgpRibIn6.remove(prefix.getIp6Prefix()) != null); // IPv6 |
| 239 | } |
| 240 | |
| 241 | /** |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 242 | * Tests whether the session is closed. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 243 | * <p> |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 244 | * NOTE: We use this method to avoid the Netty's asynchronous closing |
| 245 | * of a channel. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 246 | * </p> |
| 247 | * @return true if the session is closed |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 248 | */ |
| 249 | boolean isClosed() { |
| 250 | return isClosed; |
| 251 | } |
| 252 | |
| 253 | /** |
Jonathan Hart | ff071a2 | 2014-12-03 12:10:00 -0800 | [diff] [blame] | 254 | * Closes the session. |
| 255 | * |
| 256 | * @param ctx the Channel Handler Context |
| 257 | */ |
| 258 | void closeSession(ChannelHandlerContext ctx) { |
| 259 | timer.stop(); |
| 260 | closeChannel(ctx); |
| 261 | } |
| 262 | |
| 263 | /** |
Pavlin Radoslavov | 0a71472 | 2014-12-03 13:44:08 -0800 | [diff] [blame] | 264 | * Closes the Netty channel. |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 265 | * |
| 266 | * @param ctx the Channel Handler Context |
| 267 | */ |
| 268 | void closeChannel(ChannelHandlerContext ctx) { |
| 269 | isClosed = true; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 270 | ctx.getChannel().close(); |
| 271 | } |
| 272 | |
| 273 | @Override |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 274 | public void channelOpen(ChannelHandlerContext ctx, |
| 275 | ChannelStateEvent channelEvent) { |
| 276 | bgpSessionManager.addSessionChannel(channelEvent.getChannel()); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void channelClosed(ChannelHandlerContext ctx, |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 281 | ChannelStateEvent channelEvent) { |
Pavlin Radoslavov | 7e19094 | 2014-11-13 18:50:59 -0800 | [diff] [blame] | 282 | bgpSessionManager.removeSessionChannel(channelEvent.getChannel()); |
| 283 | } |
| 284 | |
| 285 | @Override |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 286 | public void channelConnected(ChannelHandlerContext ctx, |
| 287 | ChannelStateEvent channelEvent) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 288 | localInfo.setAddress(ctx.getChannel().getLocalAddress()); |
| 289 | remoteInfo.setAddress(ctx.getChannel().getRemoteAddress()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 290 | |
| 291 | // Assign the local and remote IPv4 addresses |
| 292 | InetAddress inetAddr; |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 293 | if (localInfo.address() instanceof InetSocketAddress) { |
| 294 | inetAddr = ((InetSocketAddress) localInfo.address()).getAddress(); |
| 295 | localInfo.setIp4Address(Ip4Address.valueOf(inetAddr.getAddress())); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 296 | } |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 297 | if (remoteInfo.address() instanceof InetSocketAddress) { |
| 298 | inetAddr = ((InetSocketAddress) remoteInfo.address()).getAddress(); |
| 299 | remoteInfo.setIp4Address(Ip4Address.valueOf(inetAddr.getAddress())); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | log.debug("BGP Session Connected from {} on {}", |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 303 | remoteInfo.address(), localInfo.address()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 304 | if (!bgpSessionManager.peerConnected(this)) { |
| 305 | log.debug("Cannot setup BGP Session Connection from {}. Closing...", |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 306 | remoteInfo.address()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 307 | ctx.getChannel().close(); |
| 308 | } |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 309 | |
| 310 | // |
| 311 | // Assign the local BGP ID |
| 312 | // NOTE: This should be configuration-based |
| 313 | // |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 314 | localInfo.setBgpId(bgpSessionManager.getMyBgpId()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void channelDisconnected(ChannelHandlerContext ctx, |
| 319 | ChannelStateEvent channelEvent) { |
| 320 | log.debug("BGP Session Disconnected from {} on {}", |
| 321 | ctx.getChannel().getRemoteAddress(), |
| 322 | ctx.getChannel().getLocalAddress()); |
Pavlin Radoslavov | 0a71472 | 2014-12-03 13:44:08 -0800 | [diff] [blame] | 323 | processChannelDisconnected(); |
| 324 | } |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 325 | |
Pavlin Radoslavov | 0a71472 | 2014-12-03 13:44:08 -0800 | [diff] [blame] | 326 | @Override |
| 327 | public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { |
| 328 | log.debug("BGP Session Exception Caught from {} on {}: {}", |
| 329 | ctx.getChannel().getRemoteAddress(), |
| 330 | ctx.getChannel().getLocalAddress(), |
| 331 | e); |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 332 | log.debug("Exception:", e.getCause()); |
Pavlin Radoslavov | 0a71472 | 2014-12-03 13:44:08 -0800 | [diff] [blame] | 333 | processChannelDisconnected(); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Processes the channel being disconnected. |
| 338 | */ |
| 339 | private void processChannelDisconnected() { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 340 | // |
| 341 | // Withdraw the routes advertised by this BGP peer |
| 342 | // |
| 343 | // NOTE: We must initialize the RIB-IN before propagating the withdraws |
| 344 | // for further processing. Otherwise, the BGP Decision Process |
| 345 | // will use those routes again. |
| 346 | // |
Pavlin Radoslavov | c7648ee | 2014-12-19 16:20:33 -0800 | [diff] [blame] | 347 | Collection<BgpRouteEntry> deletedRoutes4 = bgpRibIn4.values(); |
| 348 | Collection<BgpRouteEntry> deletedRoutes6 = bgpRibIn6.values(); |
| 349 | bgpRibIn4 = new ConcurrentHashMap<>(); |
| 350 | bgpRibIn6 = new ConcurrentHashMap<>(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 351 | |
| 352 | // Push the updates to the BGP Merged RIB |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 353 | BgpRouteSelector bgpRouteSelector = |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 354 | bgpSessionManager.getBgpRouteSelector(); |
| 355 | Collection<BgpRouteEntry> addedRoutes = Collections.emptyList(); |
Jonathan Hart | 1ad75f2 | 2016-04-13 21:24:13 -0700 | [diff] [blame] | 356 | bgpRouteSelector.routeUpdates(addedRoutes, deletedRoutes4); |
| 357 | bgpRouteSelector.routeUpdates(addedRoutes, deletedRoutes6); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 358 | |
| 359 | bgpSessionManager.peerDisconnected(this); |
| 360 | } |
| 361 | |
| 362 | /** |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 363 | * Restarts the BGP KeepaliveTimer. |
Charles M.C. Chan | bc0e84d | 2015-01-13 23:53:09 +0800 | [diff] [blame] | 364 | * |
| 365 | * @param ctx the Channel Handler Context to use |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 366 | */ |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 367 | void restartKeepaliveTimer(ChannelHandlerContext ctx) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 368 | long localKeepaliveInterval = 0; |
| 369 | |
| 370 | // |
| 371 | // Compute the local Keepalive interval |
| 372 | // |
| 373 | if (localInfo.holdtime() != 0) { |
| 374 | localKeepaliveInterval = Math.max(localInfo.holdtime() / |
| 375 | BgpConstants.BGP_KEEPALIVE_PER_HOLD_INTERVAL, |
| 376 | BgpConstants.BGP_KEEPALIVE_MIN_INTERVAL); |
| 377 | } |
| 378 | |
| 379 | // Restart the Keepalive timer |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 380 | if (localKeepaliveInterval == 0) { |
| 381 | return; // Nothing to do |
| 382 | } |
| 383 | keepaliveTimeout = timer.newTimeout(new TransmitKeepaliveTask(ctx), |
| 384 | localKeepaliveInterval, |
| 385 | TimeUnit.SECONDS); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Task class for transmitting KEEPALIVE messages. |
| 390 | */ |
| 391 | private final class TransmitKeepaliveTask implements TimerTask { |
| 392 | private final ChannelHandlerContext ctx; |
| 393 | |
| 394 | /** |
| 395 | * Constructor for given Channel Handler Context. |
| 396 | * |
| 397 | * @param ctx the Channel Handler Context to use |
| 398 | */ |
| 399 | TransmitKeepaliveTask(ChannelHandlerContext ctx) { |
| 400 | this.ctx = ctx; |
| 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public void run(Timeout timeout) throws Exception { |
| 405 | if (timeout.isCancelled()) { |
| 406 | return; |
| 407 | } |
| 408 | if (!ctx.getChannel().isOpen()) { |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | // Transmit the KEEPALIVE |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 413 | ChannelBuffer txMessage = BgpKeepalive.prepareBgpKeepalive(); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 414 | ctx.getChannel().write(txMessage); |
| 415 | |
| 416 | // Restart the KEEPALIVE timer |
| 417 | restartKeepaliveTimer(ctx); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Restarts the BGP Session Timeout Timer. |
Charles M.C. Chan | bc0e84d | 2015-01-13 23:53:09 +0800 | [diff] [blame] | 423 | * |
| 424 | * @param ctx the Channel Handler Context to use |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 425 | */ |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 426 | void restartSessionTimeoutTimer(ChannelHandlerContext ctx) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 427 | if (remoteInfo.holdtime() == 0) { |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 428 | return; // Nothing to do |
| 429 | } |
| 430 | if (sessionTimeout != null) { |
| 431 | sessionTimeout.cancel(); |
| 432 | } |
| 433 | sessionTimeout = timer.newTimeout(new SessionTimeoutTask(ctx), |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 434 | remoteInfo.holdtime(), |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 435 | TimeUnit.SECONDS); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Task class for BGP Session timeout. |
| 440 | */ |
| 441 | private final class SessionTimeoutTask implements TimerTask { |
| 442 | private final ChannelHandlerContext ctx; |
| 443 | |
| 444 | /** |
| 445 | * Constructor for given Channel Handler Context. |
| 446 | * |
| 447 | * @param ctx the Channel Handler Context to use |
| 448 | */ |
| 449 | SessionTimeoutTask(ChannelHandlerContext ctx) { |
| 450 | this.ctx = ctx; |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public void run(Timeout timeout) throws Exception { |
| 455 | if (timeout.isCancelled()) { |
| 456 | return; |
| 457 | } |
| 458 | if (!ctx.getChannel().isOpen()) { |
| 459 | return; |
| 460 | } |
| 461 | |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 462 | log.debug("BGP Session Timeout: peer {}", remoteInfo.address()); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 463 | // |
| 464 | // ERROR: Invalid Optional Parameter Length field: Unspecific |
| 465 | // |
| 466 | // Send NOTIFICATION and close the connection |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 467 | int errorCode = BgpConstants.Notifications.HoldTimerExpired.ERROR_CODE; |
| 468 | int errorSubcode = BgpConstants.Notifications.ERROR_SUBCODE_UNSPECIFIC; |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 469 | ChannelBuffer txMessage = |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 470 | BgpNotification.prepareBgpNotification(errorCode, errorSubcode, |
| 471 | null); |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 472 | ctx.getChannel().write(txMessage); |
| 473 | closeChannel(ctx); |
| 474 | } |
| 475 | } |
Jonathan Hart | ab63aac | 2014-10-16 08:52:55 -0700 | [diff] [blame] | 476 | } |