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