OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tcp_server.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef SRC_IO_TCP_SERVER_H_
6 #define SRC_IO_TCP_SERVER_H_
7 
8 #include <tbb/compat/condition_variable>
9 #include <tbb/mutex.h>
10 
11 #include <map>
12 #include <set>
13 #include <string>
14 #include <boost/asio/ip/tcp.hpp>
15 #include <boost/intrusive_ptr.hpp>
16 #include <boost/scoped_ptr.hpp>
17 
18 #include "base/util.h"
19 #include "base/address.h"
20 #include "io/server_manager.h"
21 #include "io/io_utils.h"
23 
24 class EventManager;
25 class TcpSession;
26 class SocketIOStats;
27 
28 class TcpServer {
29 public:
30  typedef boost::asio::ip::tcp::endpoint Endpoint;
31  typedef boost::asio::ip::tcp::socket Socket;
32  typedef boost::asio::ip::tcp::socket::native_handle_type NativeSocketType;
33 
34  explicit TcpServer(EventManager *evm);
35  virtual ~TcpServer();
36 
37  // Bind a listening socket and register it with the event manager.
38  virtual bool Initialize(unsigned short port);
39  virtual bool Initialize(unsigned short port, const IpAddress &host_ip,
40  int intf_id = -1);
41  bool InitializeInternal(boost::asio::ip::tcp::endpoint localaddr);
42 
43  const std::string ToString() const { return name_; }
44  void SetAcceptor();
45  void ResetAcceptor();
46 
47  // shutdown the listening socket.
48  void Shutdown();
49 
50  // close all existing sessions and delete them.
51  void ClearSessions();
52  void UpdateSessionsDscp(uint8_t dscp);
53 
54  // Helper function that allocates a socket and calls the virtual method
55  // AllocSession. The session object is owned by the TcpServer and must
56  // be deallocated via DeleteSession.
57  virtual TcpSession *CreateSession();
58 
59  // Delete a session object.
60  virtual void DeleteSession(TcpSession *session);
61 
62  virtual void Connect(TcpSession *session, Endpoint remote);
63 
64  virtual bool DisableSandeshLogMessages() const { return false; }
65 
66  int GetPort() const;
67  const io::SocketStats &GetSocketStats() const { return stats_; }
68 
69  //
70  // Return the number of tcp sessions in the map
71  //
72  size_t GetSessionCount() const {
73  return session_ref_.size();
74  }
75 
77 
78  // Returns true if any of the sessions on this server has read available
79  // data.
80  bool HasSessionReadAvailable() const;
81  bool HasSessions() const;
82 
84 
85  // wait until the server has deleted all sessions.
86  void WaitForEmpty();
87 
88  void GetRxSocketStats(SocketIOStats *socket_stats) const;
89  void GetTxSocketStats(SocketIOStats *socket_stats) const;
90 
91  void GetRxSocketStats(SocketIOStats &socket_stats) const {
92  GetRxSocketStats(&socket_stats);
93  }
94 
95  void GetTxSocketStats(SocketIOStats &socket_stats) const {
96  GetTxSocketStats(&socket_stats);
97  }
98 
99  int SetMd5SocketOption(NativeSocketType fd, uint32_t peer_ip,
100  const std::string &md5_password);
101  int SetListenSocketMd5Option(uint32_t peer_ip,
102  const std::string &md5_password);
103  int SetDscpSocketOption(NativeSocketType fd, uint8_t value);
104  uint8_t GetDscpValue(NativeSocketType fd) const;
105  int SetListenSocketDscp(uint8_t value);
106  int SetSocketOptions(const SandeshConfig &sandesh_config);
107  int SetKeepAliveSocketOption(int fd, const SandeshConfig &sandesh_config);
108 
109 protected:
110  typedef boost::intrusive_ptr<TcpServer> TcpServerPtr;
111  typedef boost::intrusive_ptr<TcpSession> TcpSessionPtr;
112 
113  // Create a session object.
114  virtual TcpSession *AllocSession(Socket *socket) = 0;
115 
116  // Only SslServer overrides this method, to manage server with SSL
117  // socket instead of TCP socket
118  virtual TcpSession *AllocSession(bool server_session);
119 
120  virtual Socket *accept_socket() const;
121  virtual void set_accept_socket();
122 
123  //
124  // Passively accepted a new session. Returns true if the session is
125  // accepted, false otherwise.
126  //
127  // If the session is not accepted, tcp_server.cc deletes the newly
128  // created session.
129  //
130  virtual bool AcceptSession(TcpSession *session);
131 
132  // For testing - will typically be used by derived class.
133  void set_socket_open_failure(bool flag) { socket_open_failure_ = flag; }
134  bool socket_open_failure() const { return socket_open_failure_; }
135 
136  Endpoint LocalEndpoint() const;
137 
138  virtual void AcceptHandlerComplete(TcpSessionPtr session);
139  virtual void ConnectHandlerComplete(TcpSessionPtr session);
140 
141 private:
142  friend class TcpSession;
143  friend class SslSession;
144  friend class TcpMessageWriter;
145  friend class BgpServerUnitTest;
146  friend void intrusive_ptr_add_ref(TcpServer *server);
147  friend void intrusive_ptr_release(TcpServer *server);
148 
150  bool operator()(const TcpSessionPtr &lhs,
151  const TcpSessionPtr &rhs) const {
152  return lhs.get() < rhs.get();
153  }
154  };
155  typedef std::set<TcpSessionPtr, TcpSessionPtrCmp> SessionSet;
156  typedef std::multimap<Endpoint, TcpSession *> SessionMap;
157 
158  void InsertSessionToMap(Endpoint remote, TcpSession *session);
159  bool RemoveSessionFromMap(Endpoint remote, TcpSession *session);
160 
161  // Called by the asio service.
163  const boost::system::error_code &error);
164 
165  void ConnectHandler(TcpServerPtr server, TcpSessionPtr session,
166  const boost::system::error_code &error);
167 
168  // Trigger the async accept operation.
169  void AsyncAccept();
170 
171  void OnSessionClose(TcpSession *session);
172  void SetName(Endpoint local_endpoint);
173 
176  // mutex protects the session maps
177  mutable tbb::mutex mutex_;
178  tbb::interface5::condition_variable cond_var_;
181  std::unique_ptr<Socket> so_accept_; // socket used in async_accept
182  boost::scoped_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
183  tbb::atomic<int> refcount_;
184  std::string name_;
186  int intf_id_;
187 
189 };
190 
191 typedef boost::intrusive_ptr<TcpServer> TcpServerPtr;
192 
193 inline void intrusive_ptr_add_ref(TcpServer *server) {
194  server->refcount_.fetch_and_increment();
195 }
196 
197 inline void intrusive_ptr_release(TcpServer *server) {
198  int prev = server->refcount_.fetch_and_decrement();
199  if (prev == 1) {
200  delete server;
201  }
202 }
203 
205 public:
206  static void AddServer(TcpServer *server);
207  static void DeleteServer(TcpServer *server);
208  static size_t GetServerCount();
209 
210 private:
212 };
213 
214 #endif // SRC_IO_TCP_SERVER_H_
virtual bool DisableSandeshLogMessages() const
Definition: tcp_server.h:64
std::string name_
Definition: tcp_server.h:184
int intrusive_ptr_add_ref(const AsPath *cpath)
Definition: bgp_aspath.h:147
virtual ~TcpServer()
Definition: tcp_server.cc:42
static void AddServer(TcpServer *server)
Definition: tcp_server.cc:652
void UpdateSessionsDscp(uint8_t dscp)
Definition: tcp_server.cc:177
bool HasSessions() const
Definition: tcp_server.cc:285
virtual void DeleteSession(TcpSession *session)
Definition: tcp_server.cc:197
boost::asio::ip::tcp::socket Socket
Definition: tcp_server.h:31
tbb::atomic< int > refcount_
Definition: tcp_server.h:183
void GetRxSocketStats(SocketIOStats &socket_stats) const
Definition: tcp_server.h:91
tbb::interface5::condition_variable cond_var_
Definition: tcp_server.h:178
virtual void Connect(TcpSession *session, Endpoint remote)
Definition: tcp_server.cc:474
friend void intrusive_ptr_add_ref(TcpServer *server)
Definition: tcp_server.h:193
std::set< TcpSessionPtr, TcpSessionPtrCmp > SessionSet
Definition: tcp_server.h:155
size_t GetSessionCount() const
Definition: tcp_server.h:72
SessionMap session_map_
Definition: tcp_server.h:180
boost::asio::ip::address IpAddress
Definition: address.h:13
TcpSession * GetSession(Endpoint remote)
Definition: tcp_server.cc:430
virtual TcpSession * CreateSession()
Definition: tcp_server.cc:188
void GetTxSocketStats(SocketIOStats *socket_stats) const
Definition: tcp_server.cc:643
std::unique_ptr< Socket > so_accept_
Definition: tcp_server.h:181
friend void intrusive_ptr_release(TcpServer *server)
Definition: tcp_server.h:197
boost::intrusive_ptr< HttpClientSession > TcpSessionPtr
Definition: http_curl.cc:105
EventManager * event_manager()
Definition: tcp_server.h:76
static ServerManager< TcpServer, TcpServerPtr > impl_
Definition: tcp_server.h:211
std::multimap< Endpoint, TcpSession * > SessionMap
Definition: tcp_server.h:156
int SetKeepAliveSocketOption(int fd, const SandeshConfig &sandesh_config)
Definition: tcp_server.cc:574
void GetRxSocketStats(SocketIOStats *socket_stats) const
Definition: tcp_server.cc:639
void AcceptHandlerInternal(TcpServerPtr server, const boost::system::error_code &error)
Definition: tcp_server.cc:354
bool operator()(const TcpSessionPtr &lhs, const TcpSessionPtr &rhs) const
Definition: tcp_server.h:150
Endpoint LocalEndpoint() const
Definition: tcp_server.cc:306
virtual void AcceptHandlerComplete(TcpSessionPtr session)
Definition: tcp_server.cc:405
void ConnectHandler(TcpServerPtr server, TcpSessionPtr session, const boost::system::error_code &error)
Definition: tcp_server.cc:439
const std::string ToString() const
Definition: tcp_server.h:43
boost::intrusive_ptr< TcpServer > TcpServerPtr
Definition: tcp_server.h:110
void Shutdown()
Definition: tcp_server.cc:143
int SetDscpSocketOption(NativeSocketType fd, uint8_t value)
Definition: tcp_server.cc:535
virtual TcpSession * AllocSession(Socket *socket)=0
bool HasSessionReadAvailable() const
Definition: tcp_server.cc:290
void AsyncAccept()
Definition: tcp_server.cc:261
int SetSocketOptions(const SandeshConfig &sandesh_config)
Definition: tcp_server.cc:566
friend class BgpServerUnitTest
Definition: tcp_server.h:145
boost::scoped_ptr< boost::asio::ip::tcp::acceptor > acceptor_
Definition: tcp_server.h:182
EventManager * evm_
Definition: tcp_server.h:175
int GetPort() const
Definition: tcp_server.cc:272
int intf_id_
Definition: tcp_server.h:186
static void DeleteServer(TcpServer *server)
Definition: tcp_server.cc:656
boost::intrusive_ptr< TcpServer > TcpServerPtr
Definition: tcp_server.h:191
TcpServer(EventManager *evm)
Definition: tcp_server.cc:31
void ResetAcceptor()
Definition: tcp_server.cc:54
bool socket_open_failure_
Definition: tcp_server.h:185
static size_t GetServerCount()
Definition: tcp_server.cc:662
bool InitializeInternal(boost::asio::ip::tcp::endpoint localaddr)
Definition: tcp_server.cc:81
void OnSessionClose(TcpSession *session)
Definition: tcp_server.cc:235
uint8_t GetDscpValue(NativeSocketType fd) const
Definition: tcp_server.cc:552
void InsertSessionToMap(Endpoint remote, TcpSession *session)
Definition: tcp_server.cc:215
void set_socket_open_failure(bool flag)
Definition: tcp_server.h:133
int SetListenSocketMd5Option(uint32_t peer_ip, const std::string &md5_password)
Definition: tcp_server.cc:517
void SetAcceptor()
const io::SocketStats & GetSocketStats() const
Definition: tcp_server.h:67
virtual bool AcceptSession(TcpSession *session)
Definition: tcp_server.cc:344
DISALLOW_COPY_AND_ASSIGN(TcpServer)
io::SocketStats stats_
Definition: tcp_server.h:174
int SetListenSocketDscp(uint8_t value)
Definition: tcp_server.cc:527
void GetTxSocketStats(SocketIOStats &socket_stats) const
Definition: tcp_server.h:95
void intrusive_ptr_release(const AsPath *cpath)
Definition: bgp_aspath.h:155
virtual bool Initialize(unsigned short port)
Definition: tcp_server.cc:59
void ClearSessions()
Definition: tcp_server.cc:159
virtual void ConnectHandlerComplete(TcpSessionPtr session)
Definition: tcp_server.cc:451
bool RemoveSessionFromMap(Endpoint remote, TcpSession *session)
Definition: tcp_server.cc:224
virtual Socket * accept_socket() const
Definition: tcp_server.cc:336
boost::asio::ip::tcp::socket::native_handle_type NativeSocketType
Definition: tcp_server.h:32
boost::asio::ip::tcp::endpoint Endpoint
Definition: tcp_server.h:30
int SetMd5SocketOption(NativeSocketType fd, uint32_t peer_ip, const std::string &md5_password)
Definition: tcp_server.cc:482
void SetName(Endpoint local_endpoint)
Definition: tcp_server.cc:48
bool socket_open_failure() const
Definition: tcp_server.h:134
SessionSet session_ref_
Definition: tcp_server.h:179
void WaitForEmpty()
Definition: tcp_server.cc:254
virtual void set_accept_socket()
Definition: tcp_server.cc:340
boost::intrusive_ptr< TcpSession > TcpSessionPtr
Definition: tcp_server.h:111
static EventManager evm
tbb::mutex mutex_
Definition: tcp_server.h:177