4 #include <sys/socket.h>
5 #include <netinet/in.h>
12 #include "connection.h"
20 static const unsigned int bind = 1; // handler number reserved for bind
21 static const int timeout_failure = -1;
22 static const int unmarshal_args_failure = -2;
23 static const int unmarshal_reply_failure = -3;
24 static const int atmostonce_failure = -4;
25 static const int oldsrv_failure = -5;
26 static const int bind_failure = -6;
27 static const int cancel_failure = -7;
30 struct ReturnOnFailure {
31 static inline int unmarshall_args_failure() {
32 return rpc_const::unmarshal_args_failure;
36 // rpc client endpoint.
37 // manages a xid space per destination socket
38 // threaded: multiple threads can be sending RPCs,
39 class rpcc : public chanmgr {
43 //manages per rpc info
45 caller(unsigned int xxid, unmarshall *un);
53 std::condition_variable c;
56 void get_refconn(connection **ch);
57 void update_xid_rep(unsigned int xid);
61 unsigned int clt_nonce_;
62 unsigned int srv_nonce_;
71 std::mutex m_; // protect insert/delete to calls[]
75 std::condition_variable destroy_wait_c_;
77 std::map<int, caller *> calls_;
78 std::list<unsigned int> xid_rep_window_;
81 request() { clear(); }
82 void clear() { buf.clear(); xid = -1; }
83 bool isvalid() { return xid != -1; }
87 struct request dup_req_;
91 rpcc(sockaddr_in d, bool retrans=true);
97 static const TO to_max;
98 static const TO to_min;
99 static TO to(int x) { TO t; t.to = x; return t;}
101 unsigned int id() { return clt_nonce_; }
103 int bind(TO to = to_max);
105 void set_reachable(bool r) { reachable_ = r; }
109 int islossy() { return lossytest_ > 0; }
111 int call1(unsigned int proc,
112 marshall &req, unmarshall &rep, TO to);
114 bool got_pdu(connection *c, char *b, int sz);
118 int call_m(unsigned int proc, marshall &req, R & r, TO to);
120 template<class R, typename ...Args>
121 inline int call(unsigned int proc, R & r, const Args&... args);
123 template<class R, typename ...Args>
124 inline int call_timeout(unsigned int proc, TO to, R & r, const Args&... args);
127 template<class R> int
128 rpcc::call_m(unsigned int proc, marshall &req, R & r, TO to)
131 int intret = call1(proc, req, u, to);
132 if (intret < 0) return intret;
134 if (u.okdone() != true) {
135 fprintf(stderr, "rpcc::call_m: failed to unmarshall the reply."
136 "You are probably calling RPC 0x%x with wrong return "
139 return rpc_const::unmarshal_reply_failure;
144 template<class R, typename... Args> inline int
145 rpcc::call(unsigned int proc, R & r, const Args&... args)
147 return call_timeout(proc, rpcc::to_max, r, args...);
150 template<class R, typename... Args> inline int
151 rpcc::call_timeout(unsigned int proc, const rpcc::TO to, R & r, const Args&... args)
154 return call_m(proc, m, r, to);
157 bool operator<(const sockaddr_in &a, const sockaddr_in &b);
159 // rpc server endpoint.
160 class rpcs : public chanmgr {
163 NEW, // new RPC, not a duplicate
164 INPROGRESS, // duplicate of an RPC we're still processing
165 DONE, // duplicate of an RPC we already replied to (have reply)
166 FORGOTTEN, // duplicate of an old RPC whose reply we've forgotten
171 // state about an in-progress or completed RPC, for at-most-once.
172 // if cb_present is true, then the RPC is complete and a reply
173 // has been sent; in that case buf points to a copy of the reply,
174 // and sz holds the size of the reply.
176 reply_t (unsigned int _xid) {
182 reply_t (unsigned int _xid, char *_buf, int _sz) {
189 bool cb_present; // whether the reply buffer is valid
190 char *buf; // the reply buffer
191 int sz; // the size of reply buffer
197 // provide at most once semantics by maintaining a window of replies
198 // per client that that client hasn't acknowledged receiving yet.
199 // indexed by client nonce.
200 std::map<unsigned int, std::list<reply_t> > reply_window_;
202 void free_reply_window(void);
203 void add_reply(unsigned int clt_nonce, unsigned int xid, char *b, int sz);
205 rpcstate_t checkduplicate_and_update(unsigned int clt_nonce,
206 unsigned int xid, unsigned int rep_xid,
209 void updatestat(unsigned int proc);
211 // latest connection to the client
212 std::map<unsigned int, connection *> conns_;
217 std::map<int, int> counts_;
222 // map proc # to function
223 std::map<int, handler *> procs_;
225 std::mutex procs_m_; // protect insert/delete to procs[]
226 std::mutex count_m_; //protect modification of counts
227 std::mutex reply_window_m_; // protect reply window et al
228 std::mutex conss_m_; // protect conns_
234 djob_t (connection *c, char *b, int bsz):buf(b),sz(bsz),conn(c) {}
239 void dispatch(djob_t *);
241 // internal handler registration
242 void reg1(unsigned int proc, handler *);
244 ThrPool* dispatchpool_;
248 rpcs(unsigned int port, int counts=0);
250 inline int port() { return listener_->port(); }
251 //RPC handler for clients binding
252 int rpcbind(int &r, int a);
254 void set_reachable(bool r) { reachable_ = r; }
256 bool got_pdu(connection *c, char *b, int sz);
258 template<class F, class C=void> void reg(unsigned int proc, F f, C *c=nullptr);
261 template<class F, class C> void rpcs::reg(unsigned int proc, F f, C *c) {
262 reg1(proc, marshalled_func<F, F, ReturnOnFailure>::wrap(f, c));
265 void make_sockaddr(const char *hostandport, struct sockaddr_in *dst);
266 void make_sockaddr(const char *host, const char *port,
267 struct sockaddr_in *dst);