OpenSDN source code
bgp_session_manager.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
10 #include "base/address_util.h"
11 #include "bgp/bgp_log.h"
12 #include "bgp/bgp_peer.h"
13 #include "bgp/bgp_server.h"
14 #include "bgp/bgp_session.h"
17 
18 using namespace boost::asio::ip;
19 
20 static const std::string kDefaultBgpSessionIp = "0.0.0.0";
21 
23  : TcpServer(evm),
24  server_(server),
25  session_queue_(
26  TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0,
27  boost::bind(&BgpSessionManager::ProcessSession, this, _1)),
28  write_ready_queue_(
29  TaskScheduler::GetInstance()->GetTaskId("bgp::Config"), 0,
30  boost::bind(&BgpSessionManager::ProcessWriteReady, this, _1)) {
31 
32  boost::system::error_code ec;
34 }
35 
37 }
38 
39 //
40 // Start listening at the given port.
41 //
42 bool BgpSessionManager::Initialize(unsigned short port) {
43  // Changes to already bound bgp listen port number is not supported.
44  Endpoint local_endpoint = LocalEndpoint();
45  if (local_endpoint != Endpoint()) {
46  Endpoint new_endpoint(session_ip_, port);
47  if (local_endpoint != new_endpoint) {
49  "Cannot change already bound " << local_endpoint <<
50  " to " << new_endpoint);
51  return false;
52  }
53  return true;
54  }
55 
56  LOG(DEBUG, "Starting Bgp Server at " << session_ip_ << ":" << port);
57  bool r = TcpServer::Initialize(port, session_ip_);
58  if (!r) {
60  "Cannot bind to bgp/tcp server ip:port " <<
61  session_ip_ << ":" << port);
62  }
63  return r;
64 }
65 
66 //
67 // Start listening at the given ip:port.
68 //
69 bool BgpSessionManager::Initialize(unsigned short port, const IpAddress& ip) {
70  session_ip_ = ip;
71  return true;
72 }
73 
74 //
75 // Called from BgpServer::DeleteActor's Shutdown method.
76 // Shutdown the TcpServer.
77 // Register an exit callback to the WorkQueues so that we can ask BgpServer
78 // to retry deletion when a WorkQueue becomes empty.
79 //
81  CHECK_CONCURRENCY("bgp::Config");
84  boost::bind(&BgpSessionManager::WorkQueueExitCallback, this, _1));
86  boost::bind(&BgpSessionManager::WorkQueueExitCallback, this, _1));
87 }
88 
89 //
90 // Called when the BgpServer is being destroyed.
91 //
92 // The WorkQueues need to be shutdown as the last step to ensure that all
93 // entries get deleted. Note that there's no need to call DeleteSession on
94 // the sessions in the WorkQueues since ClearSessions does the same thing.
95 //
97  CHECK_CONCURRENCY("bgp::Config");
98  server_ = NULL;
99  ClearSessions();
102 }
103 
104 //
105 // Return true if all WorkQueues are empty.
106 //
109  return false;
111  return false;
112  return true;
113 }
114 
115 //
116 // Add a BgpSession to the write ready WorkQueue.
117 // Take a reference to make sure that BgpSession doesn't get deleted before
118 // it's processed.
119 //
121  if (!server_ || server_->IsDeleted())
122  return;
124 }
125 
126 //
127 // Handler for BgpSessions that are dequeued from the write ready WorkQueue.
128 //
129 // The BgpServer does not get destroyed if the WorkQueue is non-empty.
130 //
132  BgpSession *session = static_cast<BgpSession *>(tcp_session.get());
133  session->ProcessWriteReady();
134  return true;
135 }
136 
137 //
138 // Search for a matching BgpPeer.
139 // First look for a matching address in the master instance.
140 // Then look for a matching port in the EndpointPeerList in BgpServer.
141 //
143  BgpPeer *peer = NULL;
144  const RoutingInstance *instance =
146  if (instance && !instance->deleted()) {
147  peer = instance->peer_manager()->PeerLookup(remote);
148  }
149  if (!peer) {
150  uint16_t port = BGPaaSUtils::DecodeBgpaasServicePort(remote.port(),
154  }
155  return peer;
156 }
157 
158 //
159 // Create an active BgpSession.
160 //
163  Socket *socket = session->socket();
164 
165  boost::system::error_code ec;
166  socket->open(boost::asio::ip::tcp::v4(), ec);
167  if (ec || (ec = session->SetSocketOptions()) || socket_open_failure()) {
169  "Failed to open bgp socket, error: " << ec.message());
170  DeleteSession(session);
171  return NULL;
172  }
173 
174  if (session_ip_ != address::from_string(kDefaultBgpSessionIp, ec)) {
175  tcp::endpoint localaddr(session_ip_, 0);
176  socket->bind(localaddr, ec);
177  if (ec) {
179  "Failed to bind bgp socket to:" << session_ip_ <<
180  ", error: " << ec.message());
181  DeleteSession(session);
182  return NULL;
183  }
184  }
185  return session;
186 }
187 
188 //
189 // Allocate a new BgpSession.
190 // Called via CreateSession or when the TcpServer accepts a passive session.
191 //
193  TcpSession *session = new BgpSession(this, socket);
194  return session;
195 }
196 
197 //
198 // Accept incoming BgpSession and add to session WorkQueue for processing.
199 // This ensures that we don't try to access the BgpServer data structures
200 // from the IO thread while they are being modified from bgp::Config task.
201 //
202 // Stop accepting sessions after delete of the BgpServer gets triggered.
203 // Note that the BgpServer, and hence the BgpSessionManager will not get
204 // destroyed if the WorkQueue is non-empty.
205 //
207  if (!server_ || server_->IsDeleted())
208  return false;
209  BgpSession *session = dynamic_cast<BgpSession *>(tcp_session);
210  session->set_read_on_connect(false);
211  session_queue_.Enqueue(session);
212  return true;
213 }
214 
215 //
216 // Handler for BgpSessions that are dequeued from the session WorkQueue.
217 //
218 // The BgpServer does not get destroyed if the WorkQueue is non-empty.
219 //
221  CHECK_CONCURRENCY("bgp::Config");
222 
223  BgpPeer *peer = FindPeer(session->remote_endpoint());
224 
225  // Ignore if server is being deleted.
226  if (!server_ || server_->IsDeleted()) {
229  DeleteSession(session);
230  return true;
231  }
232 
233  // Ignore if this server is being held administratively down.
234  if (server_->admin_down()) {
237  DeleteSession(session);
238  return true;
239  }
240 
241  // Ignore if this peer is not configured or is being deleted.
242  if (peer == NULL || peer->deleter()->IsDeleted()) {
246  "Remote end-point not found");
247  DeleteSession(session);
248  return true;
249  }
250 
251  // Ignore if this peer is being held administratively down.
252  if (peer->IsAdminDown()) {
255  DeleteSession(session);
256  return true;
257  }
258 
259  // Ignore if the peer's prefix limit idle timer is running.
260  if (peer->PrefixLimitIdleTimerRunning()) {
263  DeleteSession(session);
264  return true;
265  }
266 
267  // Ignore if this peer is being closed.
268  if (peer->IsCloseInProgress()) {
271  DeleteSession(session);
272  return true;
273  }
274 
275  if (!peer->ProcessSession()) {
278  DeleteSession(session);
279  return true;
280  }
281 
282  peer->AcceptSession(session);
283  return true;
284 }
285 
286 //
287 // Exit callback for the session and write ready WorkQueues.
288 //
290  server_->RetryDelete();
291 }
292 
294  return session_queue_.Length();
295 }
296 
298  session_queue_.set_disable(disabled);
299 }
boost::asio::ip::address IpAddress
Definition: address.h:14
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:15
IpAddress AddressFromString(const std::string &ip_address_str, boost::system::error_code *ec)
#define BGP_LOG_FLAG_ALL
Definition: bgp_log.h:45
#define BGP_LOG_WARNING_STR(obj, flags, arg)
Definition: bgp_log.h:108
#define BGP_LOG_FLAG_TRACE
Definition: bgp_log.h:44
static const std::string kDefaultBgpSessionIp
static BgpAsServicePortIndexPair DecodeBgpaasServicePort(const uint32_t sport, const uint16_t port_range_start, const uint16_t port_range_end)
uint16_t bgpaas_port_start() const
Definition: bgp_config.h:677
uint16_t bgpaas_port_end() const
Definition: bgp_config.h:681
bool PrefixLimitIdleTimerRunning() const
Definition: bgp_peer.cc:2204
virtual bool ProcessSession() const
Definition: bgp_peer.cc:2860
bool IsCloseInProgress() const
Definition: bgp_peer.cc:1322
bool IsAdminDown() const
Definition: bgp_peer.h:223
bool AcceptSession(BgpSession *session)
Definition: bgp_peer.cc:1369
LifetimeActor * deleter()
Definition: bgp_peer.cc:1167
RoutingInstanceMgr * routing_instance_mgr()
Definition: bgp_server.h:107
void RetryDelete()
Definition: bgp_server.cc:427
bool IsDeleted() const
Definition: bgp_server.cc:423
BgpPeer * FindPeer(const std::string &name)
BgpGlobalSystemConfig * global_config()
Definition: bgp_server.h:298
bool admin_down() const
Definition: bgp_server.h:210
WorkQueue< TcpSessionPtr > write_ready_queue_
WorkQueue< BgpSession * > session_queue_
virtual TcpSession * AllocSession(Socket *socket)
bool ProcessWriteReady(TcpSessionPtr tcp_session)
void WorkQueueExitCallback(bool done)
virtual TcpSession * CreateSession()
BgpSessionManager(EventManager *evm, BgpServer *server)
virtual bool Initialize(unsigned short port)
size_t GetSessionQueueSize() const
bool ProcessSession(BgpSession *session)
void SetSessionQueueDisable(bool disabled)
void EnqueueWriteReady(BgpSession *session)
BgpPeer * FindPeer(Endpoint remote)
virtual bool AcceptSession(TcpSession *session)
void SendNotification(int code, int subcode, const std::string &data=std::string())
Definition: bgp_session.cc:113
void ProcessWriteReady()
Definition: bgp_session.cc:72
bool IsDeleted() const
Definition: lifetime.h:132
virtual BgpPeer * PeerLookup(std::string name) const
RoutingInstance * GetDefaultRoutingInstance()
bool deleted() const
PeerManager * peer_manager()
The TaskScheduler keeps track of what tasks are currently schedulable. When a task is enqueued it is ...
Definition: task.h:307
void Shutdown()
Definition: tcp_server.cc:145
bool socket_open_failure() const
Definition: tcp_server.h:135
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp_server.h:31
Endpoint LocalEndpoint() const
Definition: tcp_server.cc:309
virtual bool Initialize(unsigned short port)
Definition: tcp_server.cc:61
virtual TcpSession * CreateSession()
Definition: tcp_server.cc:191
boost::intrusive_ptr< TcpSession > TcpSessionPtr
Definition: tcp_server.h:112
virtual void DeleteSession(TcpSession *session)
Definition: tcp_server.cc:200
void ClearSessions()
Definition: tcp_server.cc:161
boost::asio::ip::tcp::socket Socket
Definition: tcp_server.h:32
virtual Socket * socket() const
Definition: tcp_session.h:83
void set_read_on_connect(bool read)
Definition: tcp_session.h:148
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp_session.h:59
Endpoint remote_endpoint() const
Definition: tcp_session.h:132
virtual boost::system::error_code SetSocketOptions()
Definition: tcp_session.cc:870
bool Enqueue(QueueEntryT entry)
Definition: queue_task.h:249
void set_disable(bool disabled)
Definition: queue_task.h:320
size_t Length() const
Definition: queue_task.h:357
void Shutdown(bool delete_entries=true)
Definition: queue_task.h:153
void SetExitCallback(TaskExitCallback on_exit)
Definition: queue_task.h:304
bool IsQueueEmpty() const
Definition: queue_task.h:353
static EventManager evm
#define LOG(_Level, _Msg)
Definition: logging.h:35
#define CHECK_CONCURRENCY(...)