blob: 92448b4de43916f3a1fb822d3938e6684b52440c [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart41349e92015-02-09 14:14:02 -080016package org.onosproject.routing.bgp;
Jonathan Hartab63aac2014-10-16 08:52:55 -070017
Jonathan Hartd24fafb2015-02-09 17:55:32 -080018import org.apache.felix.scr.annotations.Activate;
Jonathan Hart41349e92015-02-09 14:14:02 -080019import org.apache.felix.scr.annotations.Component;
Jonathan Hartd24fafb2015-02-09 17:55:32 -080020import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Modified;
Jonathan Hart1ad75f22016-04-13 21:24:13 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart41349e92015-02-09 14:14:02 -080024import org.apache.felix.scr.annotations.Service;
Jonathan Hartab63aac2014-10-16 08:52:55 -070025import org.jboss.netty.bootstrap.ServerBootstrap;
26import org.jboss.netty.channel.Channel;
27import org.jboss.netty.channel.ChannelException;
28import org.jboss.netty.channel.ChannelFactory;
29import org.jboss.netty.channel.ChannelPipeline;
30import org.jboss.netty.channel.ChannelPipelineFactory;
31import org.jboss.netty.channel.Channels;
Pavlin Radoslavov7e190942014-11-13 18:50:59 -080032import org.jboss.netty.channel.group.ChannelGroup;
33import org.jboss.netty.channel.group.DefaultChannelGroup;
Jonathan Hartab63aac2014-10-16 08:52:55 -070034import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080035import org.onlab.packet.Ip4Address;
36import org.onlab.packet.Ip4Prefix;
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080037import org.onlab.packet.Ip6Prefix;
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080038import org.onlab.packet.IpPrefix;
Jonathan Hart1ad75f22016-04-13 21:24:13 -070039import org.onosproject.incubator.net.routing.Route;
40import org.onosproject.incubator.net.routing.RouteAdminService;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080041import org.osgi.service.component.ComponentContext;
Jonathan Hartab63aac2014-10-16 08:52:55 -070042import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
44
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080045import java.net.InetAddress;
46import java.net.InetSocketAddress;
47import java.net.SocketAddress;
48import java.util.Collection;
Jonathan Hartd24fafb2015-02-09 17:55:32 -080049import java.util.Dictionary;
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080050import java.util.concurrent.ConcurrentHashMap;
51import java.util.concurrent.ConcurrentMap;
52
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080053import static java.util.concurrent.Executors.newCachedThreadPool;
Thomas Vachuska6f94ded2015-02-21 14:02:38 -080054import static org.onlab.util.Tools.groupedThreads;
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080055
Jonathan Hartab63aac2014-10-16 08:52:55 -070056/**
57 * BGP Session Manager class.
58 */
Jonathan Hartc22e8472015-11-17 18:25:45 -080059@Component(immediate = true, enabled = false)
Jonathan Hart41349e92015-02-09 14:14:02 -080060@Service
Jonathan Hart1ad75f22016-04-13 21:24:13 -070061public class BgpSessionManager implements BgpInfoService {
Jonathan Hartab63aac2014-10-16 08:52:55 -070062 private static final Logger log =
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080063 LoggerFactory.getLogger(BgpSessionManager.class);
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080064
Jonathan Hart1ad75f22016-04-13 21:24:13 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected RouteAdminService routeService;
67
Pavlin Radoslavov7e190942014-11-13 18:50:59 -080068 boolean isShutdown = true;
Jonathan Hartab63aac2014-10-16 08:52:55 -070069 private Channel serverChannel; // Listener for incoming BGP connections
Pavlin Radoslavov7e190942014-11-13 18:50:59 -080070 private ServerBootstrap serverBootstrap;
71 private ChannelGroup allChannels = new DefaultChannelGroup();
Jonathan Hartab63aac2014-10-16 08:52:55 -070072 private ConcurrentMap<SocketAddress, BgpSession> bgpSessions =
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080073 new ConcurrentHashMap<>();
Pavlin Radoslavov6b570732014-11-06 13:16:45 -080074 private Ip4Address myBgpId; // Same BGP ID for all peers
Jonathan Hartab63aac2014-10-16 08:52:55 -070075
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080076 private BgpRouteSelector bgpRouteSelector = new BgpRouteSelector(this);
77 private ConcurrentMap<Ip4Prefix, BgpRouteEntry> bgpRoutes4 =
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080078 new ConcurrentHashMap<>();
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -080079 private ConcurrentMap<Ip6Prefix, BgpRouteEntry> bgpRoutes6 =
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -080080 new ConcurrentHashMap<>();
Jonathan Hartab63aac2014-10-16 08:52:55 -070081
Jonathan Hartd24fafb2015-02-09 17:55:32 -080082 private static final int DEFAULT_BGP_PORT = 2000;
83 private int bgpPort;
84
85 @Activate
86 protected void activate(ComponentContext context) {
87 readComponentConfiguration(context);
Jonathan Hart1ad75f22016-04-13 21:24:13 -070088 start();
Jonathan Hartd24fafb2015-02-09 17:55:32 -080089 log.info("BgpSessionManager started");
90 }
91
92 @Deactivate
93 protected void deactivate() {
Jonathan Hart1ad75f22016-04-13 21:24:13 -070094 stop();
Jonathan Hartd24fafb2015-02-09 17:55:32 -080095 log.info("BgpSessionManager stopped");
96 }
97
98 /**
99 * Extracts properties from the component configuration context.
100 *
101 * @param context the component context
102 */
103 private void readComponentConfiguration(ComponentContext context) {
104 Dictionary<?, ?> properties = context.getProperties();
105 try {
106 String strPort = (String) properties.get("bgpPort");
107 if (strPort != null) {
108 bgpPort = Integer.parseInt(strPort);
109 } else {
110 bgpPort = DEFAULT_BGP_PORT;
111 }
Pavlin Radoslavovb9e50df2015-02-20 20:01:26 -0800112 } catch (NumberFormatException | ClassCastException e) {
Jonathan Hartd24fafb2015-02-09 17:55:32 -0800113 bgpPort = DEFAULT_BGP_PORT;
114 }
115 log.debug("BGP port is set to {}", bgpPort);
116 }
117
118 @Modified
119 public void modified(ComponentContext context) {
120 // Blank @Modified method to catch modifications to the context.
121 // If no @Modified method exists, it seems @Activate is called again
122 // when the context is modified.
123 }
124
Jonathan Hartab63aac2014-10-16 08:52:55 -0700125 /**
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800126 * Checks whether the BGP Session Manager is shutdown.
127 *
128 * @return true if the BGP Session Manager is shutdown, otherwise false
129 */
130 boolean isShutdown() {
131 return this.isShutdown;
132 }
133
134 /**
Jonathan Hartab63aac2014-10-16 08:52:55 -0700135 * Gets the BGP sessions.
136 *
137 * @return the BGP sessions
138 */
139 public Collection<BgpSession> getBgpSessions() {
140 return bgpSessions.values();
141 }
142
143 /**
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800144 * Gets the selected IPv4 BGP routes among all BGP sessions.
Jonathan Hartab63aac2014-10-16 08:52:55 -0700145 *
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800146 * @return the selected IPv4 BGP routes among all BGP sessions
Jonathan Hartab63aac2014-10-16 08:52:55 -0700147 */
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800148 public Collection<BgpRouteEntry> getBgpRoutes4() {
149 return bgpRoutes4.values();
150 }
151
152 /**
153 * Gets the selected IPv6 BGP routes among all BGP sessions.
154 *
155 * @return the selected IPv6 BGP routes among all BGP sessions
156 */
157 public Collection<BgpRouteEntry> getBgpRoutes6() {
158 return bgpRoutes6.values();
159 }
160
161 /**
162 * Finds a BGP route for a prefix. The prefix can be either IPv4 or IPv6.
163 *
164 * @param prefix the prefix to use
165 * @return the BGP route if found, otherwise null
166 */
167 BgpRouteEntry findBgpRoute(IpPrefix prefix) {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700168 if (prefix.isIp4()) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800169 return bgpRoutes4.get(prefix.getIp4Prefix()); // IPv4
170 }
171 return bgpRoutes6.get(prefix.getIp6Prefix()); // IPv6
172 }
173
174 /**
175 * Adds a BGP route. The route can be either IPv4 or IPv6.
176 *
177 * @param bgpRouteEntry the BGP route entry to use
178 */
179 void addBgpRoute(BgpRouteEntry bgpRouteEntry) {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700180 if (bgpRouteEntry.isIp4()) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800181 bgpRoutes4.put(bgpRouteEntry.prefix().getIp4Prefix(), // IPv4
182 bgpRouteEntry);
183 } else {
184 bgpRoutes6.put(bgpRouteEntry.prefix().getIp6Prefix(), // IPv6
185 bgpRouteEntry);
186 }
187 }
188
189 /**
190 * Removes a BGP route for a prefix. The prefix can be either IPv4 or IPv6.
191 *
192 * @param prefix the prefix to use
193 * @return true if the route was found and removed, otherwise false
194 */
195 boolean removeBgpRoute(IpPrefix prefix) {
Pavlin Radoslavov87dd9302015-03-10 13:53:24 -0700196 if (prefix.isIp4()) {
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800197 return (bgpRoutes4.remove(prefix.getIp4Prefix()) != null); // IPv4
198 }
199 return (bgpRoutes6.remove(prefix.getIp6Prefix()) != null); // IPv6
Jonathan Hartab63aac2014-10-16 08:52:55 -0700200 }
201
202 /**
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800203 * Adds the channel for a BGP session.
204 *
205 * @param channel the channel to add
206 */
207 void addSessionChannel(Channel channel) {
208 allChannels.add(channel);
209 }
210
211 /**
212 * Removes the channel for a BGP session.
213 *
214 * @param channel the channel to remove
215 */
216 void removeSessionChannel(Channel channel) {
217 allChannels.remove(channel);
218 }
219
220 /**
Jonathan Hartab63aac2014-10-16 08:52:55 -0700221 * Processes the connection from a BGP peer.
222 *
223 * @param bgpSession the BGP session for the peer
224 * @return true if the connection can be established, otherwise false
225 */
226 boolean peerConnected(BgpSession bgpSession) {
227
228 // Test whether there is already a session from the same remote
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800229 if (bgpSessions.get(bgpSession.remoteInfo().address()) != null) {
Jonathan Hartab63aac2014-10-16 08:52:55 -0700230 return false; // Duplicate BGP session
231 }
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800232 bgpSessions.put(bgpSession.remoteInfo().address(), bgpSession);
Jonathan Hartab63aac2014-10-16 08:52:55 -0700233
234 //
235 // If the first connection, set my BGP ID to the local address
236 // of the socket.
237 //
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800238 if (bgpSession.localInfo().address() instanceof InetSocketAddress) {
Jonathan Hartab63aac2014-10-16 08:52:55 -0700239 InetAddress inetAddr =
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800240 ((InetSocketAddress) bgpSession.localInfo().address()).getAddress();
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800241 Ip4Address ip4Address = Ip4Address.valueOf(inetAddr.getAddress());
Jonathan Hartab63aac2014-10-16 08:52:55 -0700242 updateMyBgpId(ip4Address);
243 }
244 return true;
245 }
246
247 /**
248 * Processes the disconnection from a BGP peer.
249 *
250 * @param bgpSession the BGP session for the peer
251 */
252 void peerDisconnected(BgpSession bgpSession) {
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800253 bgpSessions.remove(bgpSession.remoteInfo().address());
Jonathan Hartab63aac2014-10-16 08:52:55 -0700254 }
255
256 /**
257 * Conditionally updates the local BGP ID if it wasn't set already.
258 * <p/>
259 * NOTE: A BGP instance should use same BGP ID across all BGP sessions.
260 *
261 * @param ip4Address the IPv4 address to use as BGP ID
262 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800263 private synchronized void updateMyBgpId(Ip4Address ip4Address) {
Jonathan Hartab63aac2014-10-16 08:52:55 -0700264 if (myBgpId == null) {
265 myBgpId = ip4Address;
266 log.debug("BGP: My BGP ID is {}", myBgpId);
267 }
268 }
269
270 /**
271 * Gets the local BGP Identifier as an IPv4 address.
272 *
273 * @return the local BGP Identifier as an IPv4 address
274 */
Pavlin Radoslavov6b570732014-11-06 13:16:45 -0800275 Ip4Address getMyBgpId() {
Jonathan Hartab63aac2014-10-16 08:52:55 -0700276 return myBgpId;
277 }
278
279 /**
280 * Gets the BGP Route Selector.
281 *
282 * @return the BGP Route Selector
283 */
284 BgpRouteSelector getBgpRouteSelector() {
285 return bgpRouteSelector;
286 }
287
Jonathan Hart1ad75f22016-04-13 21:24:13 -0700288 /**
289 * Sends updates routes to the route service.
290 *
291 * @param updates routes to update
292 */
293 void update(Collection<Route> updates) {
294 routeService.update(updates);
295 }
296
297 /**
298 * Sends withdrawn routes to the routes service.
299 *
300 * @param withdraws routes to withdraw
301 */
302 void withdraw(Collection<Route> withdraws) {
303 routeService.withdraw(withdraws);
304 }
305
306
307 public void start() {
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800308 log.debug("BGP Session Manager start.");
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800309 isShutdown = false;
Jonathan Hartab63aac2014-10-16 08:52:55 -0700310
Pavlin Radoslavov3a0a52e2015-01-06 17:41:37 -0800311 ChannelFactory channelFactory = new NioServerSocketChannelFactory(
Thomas Vachuska6f94ded2015-02-21 14:02:38 -0800312 newCachedThreadPool(groupedThreads("onos/bgp", "sm-boss-%d")),
313 newCachedThreadPool(groupedThreads("onos/bgp", "sm-worker-%d")));
Sho SHIMIZU74626412015-09-11 11:46:27 -0700314 ChannelPipelineFactory pipelineFactory = () -> {
315 // Allocate a new session per connection
316 BgpSession bgpSessionHandler =
317 new BgpSession(BgpSessionManager.this);
318 BgpFrameDecoder bgpFrameDecoder =
319 new BgpFrameDecoder(bgpSessionHandler);
Jonathan Hartab63aac2014-10-16 08:52:55 -0700320
Sho SHIMIZU74626412015-09-11 11:46:27 -0700321 // Setup the processing pipeline
322 ChannelPipeline pipeline = Channels.pipeline();
323 pipeline.addLast("BgpFrameDecoder", bgpFrameDecoder);
324 pipeline.addLast("BgpSession", bgpSessionHandler);
325 return pipeline;
Thomas Vachuska9ea3e6f2015-01-23 16:34:22 -0800326 };
Jonathan Hartab63aac2014-10-16 08:52:55 -0700327 InetSocketAddress listenAddress =
Jonathan Hartd24fafb2015-02-09 17:55:32 -0800328 new InetSocketAddress(bgpPort);
Jonathan Hartab63aac2014-10-16 08:52:55 -0700329
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800330 serverBootstrap = new ServerBootstrap(channelFactory);
Jonathan Hartab63aac2014-10-16 08:52:55 -0700331 // serverBootstrap.setOptions("reuseAddr", true);
332 serverBootstrap.setOption("child.keepAlive", true);
333 serverBootstrap.setOption("child.tcpNoDelay", true);
334 serverBootstrap.setPipelineFactory(pipelineFactory);
335 try {
336 serverChannel = serverBootstrap.bind(listenAddress);
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800337 allChannels.add(serverChannel);
Jonathan Hartab63aac2014-10-16 08:52:55 -0700338 } catch (ChannelException e) {
339 log.debug("Exception binding to BGP port {}: ",
340 listenAddress.getPort(), e);
341 }
342 }
343
Pavlin Radoslavova071b1e2014-11-17 13:37:57 -0800344 public void stop() {
Pavlin Radoslavov7e190942014-11-13 18:50:59 -0800345 isShutdown = true;
346 allChannels.close().awaitUninterruptibly();
347 serverBootstrap.releaseExternalResources();
Jonathan Hartab63aac2014-10-16 08:52:55 -0700348 }
Jonathan Hartab63aac2014-10-16 08:52:55 -0700349}