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