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