7b65101d6e82ed7f9a27b54905b43dfba4165813
[invirt/third/libt4.git] / rpc / rpc.h
1 #ifndef rpc_h
2 #define rpc_h
3
4 #include "types.h"
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7
8 #include "rpc_protocol.h"
9 #include "thr_pool.h"
10 #include "marshall.h"
11 #include "marshall_wrap.h"
12 #include "connection.h"
13
14 namespace rpc {
15     static constexpr milliseconds to_max{12000};
16     static constexpr milliseconds to_min{100};
17 }
18
19 template<class P, class R, class ...Args> struct is_valid_call : false_type {};
20
21 template<class S, class R, class ...Args>
22 struct is_valid_call<S(R &, Args...), R, Args...> : true_type {};
23
24 template<class P, class F> struct is_valid_registration : false_type {};
25
26 template<class S, class R, class ...Args>
27 struct is_valid_registration<S(R &, typename decay<Args>::type...), S(R &, Args...)> : true_type {};
28
29 template<class P, class C, class S, class R, class ...Args>
30 struct is_valid_registration<P, S(C::*)(R &, Args...)> : is_valid_registration<P, S(R &, Args...)> {};
31
32 // rpc client endpoint.
33 // manages a xid space per destination socket
34 // threaded: multiple threads can be sending RPCs,
35 class rpcc : private connection_delegate {
36     private:
37         using proc_id_t = rpc_protocol::proc_id_t;
38         template <class S>
39         using proc_t = rpc_protocol::proc_t<S>;
40
41         // manages per rpc info
42         struct caller {
43             caller(int _xid, string *_rep) : xid(_xid), rep(_rep) {}
44
45             int xid;
46             string *rep;
47             int intret;
48             bool done = false;
49             mutex m;
50             cond c;
51         };
52
53         void get_refconn(shared_ptr<connection> & ch);
54         void update_xid_rep(int xid);
55
56
57         sockaddr_in dst_;
58         unsigned int clt_nonce_;
59         unsigned int srv_nonce_;
60         bool bind_done_;
61         int xid_;
62         int lossytest_;
63         bool retrans_;
64         bool reachable_;
65
66         shared_ptr<connection> chan_;
67
68         mutex m_; // protect insert/delete to calls[]
69         mutex chan_m_;
70
71         bool destroy_wait_;
72         cond destroy_wait_c_;
73
74         map<int, caller *> calls_;
75         list<int> xid_rep_window_;
76
77         struct request {
78             void clear() { buf.clear(); xid = -1; }
79             bool isvalid() { return xid != -1; }
80             string buf;
81             int xid = -1;
82         };
83         request dup_req_;
84         int xid_rep_done_;
85
86         int call1(proc_id_t proc, marshall &req, string &rep, milliseconds to);
87
88         template<class R>
89         int call_m(proc_id_t proc, marshall &req, R & r, milliseconds to) {
90             string rep;
91             int intret = call1(proc, req, rep, to);
92             unmarshall u(rep, true);
93             if (intret < 0) return intret;
94             u >> r;
95             if (u.okdone() != true) {
96                 LOG("rpcc::call_m: failed to unmarshall the reply.  You are probably " <<
97                     "calling RPC 0x" << hex << proc << " with the wrong return type.");
98                 VERIFY(0);
99                 return rpc_protocol::unmarshal_reply_failure;
100             }
101             return intret;
102         }
103
104         bool got_pdu(const shared_ptr<connection> & c, const string & b);
105
106     public:
107
108         rpcc(const string & d, bool retrans=true);
109         ~rpcc();
110
111         unsigned int id() { return clt_nonce_; }
112
113         int bind(milliseconds to = rpc::to_max);
114
115         void set_reachable(bool r) { reachable_ = r; }
116
117         void cancel();
118
119         template<class P, class R, typename ...Args>
120         inline int call(proc_t<P> proc, R & r, const Args&... args) {
121             return call_timeout(proc, rpc::to_max, r, args...);
122         }
123
124         template<class P, class R, typename ...Args>
125         inline int call_timeout(proc_t<P> proc, milliseconds to, R & r, const Args&... args) {
126             static_assert(is_valid_call<P, R, Args...>::value, "RPC called with incorrect argument types");
127             marshall m{args...};
128             return call_m(proc.id, m, r, to);
129         }
130 };
131
132 // rpc server endpoint.
133 class rpcs : private connection_delegate {
134     private:
135         using proc_id_t = rpc_protocol::proc_id_t;
136         template <class S>
137         using proc_t = rpc_protocol::proc_t<S>;
138
139         typedef enum {
140             NEW,  // new RPC, not a duplicate
141             INPROGRESS, // duplicate of an RPC we're still processing
142             DONE, // duplicate of an RPC we already replied to (have reply)
143             FORGOTTEN,  // duplicate of an old RPC whose reply we've forgotten
144         } rpcstate_t;
145
146         // state about an in-progress or completed RPC, for at-most-once.
147         // if cb_present is true, then the RPC is complete and a reply
148         // has been sent; in that case buf points to a copy of the reply,
149         // and sz holds the size of the reply.
150         struct reply_t {
151             reply_t (int _xid) : xid(_xid), cb_present(false) {}
152             reply_t (int _xid, const string & _buf) : xid(_xid), cb_present(true), buf(_buf) {}
153             int xid;
154             bool cb_present; // whether the reply buffer is valid
155             string buf;      // the reply buffer
156         };
157
158         in_port_t port_;
159         unsigned int nonce_;
160
161         // provide at most once semantics by maintaining a window of replies
162         // per client that that client hasn't acknowledged receiving yet.
163         // indexed by client nonce.
164         map<unsigned int, list<reply_t>> reply_window_;
165
166         void free_reply_window(void);
167         void add_reply(unsigned int clt_nonce, int xid, const string & b);
168
169         rpcstate_t checkduplicate_and_update(unsigned int clt_nonce, 
170                 int xid, int rep_xid, string & b);
171
172         void updatestat(proc_id_t proc);
173
174         // latest connection to the client
175         map<unsigned int, shared_ptr<connection>> conns_;
176
177         // counting
178         const size_t counting_;
179         size_t curr_counts_;
180         map<proc_id_t, size_t> counts_;
181
182         bool reachable_;
183
184         // map proc # to function
185         map<proc_id_t, handler *> procs_;
186
187         mutex procs_m_; // protect insert/delete to procs[]
188         mutex count_m_;  // protect modification of counts
189         mutex reply_window_m_; // protect reply window et al
190         mutex conns_m_; // protect conns_
191
192         void dispatch(shared_ptr<connection> c, const string & buf);
193
194         // internal handler registration
195         void reg1(proc_id_t proc, handler *);
196
197         unique_ptr<thread_pool> dispatchpool_;
198         unique_ptr<tcpsconn> listener_;
199
200         // RPC handler for clients binding
201         rpc_protocol::status rpcbind(unsigned int &r, int a);
202
203         bool got_pdu(const shared_ptr<connection> & c, const string & b);
204
205     public:
206
207         rpcs(in_port_t port, size_t counts=0);
208         ~rpcs();
209
210         void set_reachable(bool r) { reachable_ = r; }
211
212         template<class P, class F, class C=void> void reg(proc_t<P> proc, F f, C *c=nullptr) {
213             static_assert(is_valid_registration<P, F>::value, "RPC handler registered with incorrect argument types");
214             struct ReturnOnFailure {
215                 static inline int unmarshall_args_failure() {
216                     return rpc_protocol::unmarshal_args_failure;
217                 }
218             };
219             reg1(proc.id, marshalled_func<F, ReturnOnFailure>::wrap(f, c));
220         }
221
222         void start();
223 };
224
225 #endif