X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/eeab3e6cade87c1fe0a5f3d93522e12ccb9ec2ab..f2170465073de34adf89161d4287182b518352c4:/rpc/rpc.cc?ds=inline diff --git a/rpc/rpc.cc b/rpc/rpc.cc index f0c4b77..5e43547 100644 --- a/rpc/rpc.cc +++ b/rpc/rpc.cc @@ -64,12 +64,13 @@ #include "lock.h" #include "jsl_log.h" +#include "tprintf.h" #include "lang/verify.h" const rpcc::TO rpcc::to_max = { 120000 }; const rpcc::TO rpcc::to_min = { 1000 }; -rpcc::caller::caller(unsigned int xxid, unmarshall *xun) +rpcc::caller::caller(int xxid, unmarshall *xun) : xid(xxid), un(xun), done(false) { } @@ -82,7 +83,7 @@ inline void set_rand_seed() { auto now = std::chrono::time_point_cast(std::chrono::steady_clock::now()); - srandom((int)now.time_since_epoch().count()^((int)getpid())); + srandom((uint32_t)now.time_since_epoch().count()^(uint32_t)getpid()); } rpcc::rpcc(sockaddr_in d, bool retrans) : @@ -91,7 +92,7 @@ rpcc::rpcc(sockaddr_in d, bool retrans) : { if(retrans){ set_rand_seed(); - clt_nonce_ = random(); + clt_nonce_ = (unsigned int)random(); } else { // special client nonce 0 means this client does not // require at-most-once logic from the server @@ -127,7 +128,7 @@ rpcc::~rpcc() int rpcc::bind(TO to) { - int r; + unsigned int r; int ret = call_timeout(rpc_const::bind, to, r, 0); if(ret == 0){ lock ml(m_); @@ -145,10 +146,9 @@ rpcc::bind(TO to) rpcc::cancel(void) { lock ml(m_); - printf("rpcc::cancel: force callers to fail\n"); - std::map::iterator iter; - for(iter = calls_.begin(); iter != calls_.end(); iter++){ - caller *ca = iter->second; + tprintf("rpcc::cancel: force callers to fail"); + for(auto &p : calls_){ + caller *ca = p.second; jsl_log(JSL_DBG_2, "rpcc::cancel: force caller to fail\n"); { @@ -163,7 +163,7 @@ rpcc::cancel(void) destroy_wait_ = true; destroy_wait_c_.wait(ml); } - printf("rpcc::cancel: done\n"); + tprintf("rpcc::cancel: done"); } int @@ -172,7 +172,7 @@ rpcc::call1(unsigned int proc, marshall &req, unmarshall &rep, { caller ca(0, &rep); - int xid_rep; + int xid_rep; { lock ml(m_); @@ -189,7 +189,7 @@ rpcc::call1(unsigned int proc, marshall &req, unmarshall &rep, ca.xid = xid_++; calls_[ca.xid] = &ca; - req.pack_req_header({ca.xid, proc, clt_nonce_, srv_nonce_, xid_rep_window_.front()}); + req.pack_req_header({ca.xid, (int)proc, clt_nonce_, srv_nonce_, xid_rep_window_.front()}); xid_rep = xid_rep_window_.front(); } @@ -223,7 +223,7 @@ rpcc::call1(unsigned int proc, marshall &req, unmarshall &rep, } else jsl_log(JSL_DBG_1, "not reachable\n"); jsl_log(JSL_DBG_2, - "rpcc::call1 %u just sent req proc %x xid %u clt_nonce %d\n", + "rpcc::call1 %u just sent req proc %x xid %d clt_nonce %d\n", clt_nonce_, proc, ca.xid, clt_nonce_); } transmit = false; // only send once on a given channel @@ -289,7 +289,7 @@ rpcc::call1(unsigned int proc, marshall &req, unmarshall &rep, lock cal(ca.m); jsl_log(JSL_DBG_2, - "rpcc::call1 %u call done for req proc %x xid %u %s:%d done? %d ret %d \n", + "rpcc::call1 %u call done for req proc %x xid %d %s:%d done? %d ret %d \n", clt_nonce_, proc, ca.xid, inet_ntoa(dst_.sin_addr), ntohs(dst_.sin_port), ca.done, ca.intret); @@ -324,7 +324,7 @@ rpcc::get_refconn(connection **ch) // // this function keeps no reference for connection *c bool -rpcc::got_pdu(connection *c, char *b, size_t sz) +rpcc::got_pdu(connection *, char *b, size_t sz) { unmarshall rep(b, sz); reply_header h; @@ -361,15 +361,13 @@ rpcc::got_pdu(connection *c, char *b, size_t sz) // assumes thread holds mutex m void -rpcc::update_xid_rep(unsigned int xid) +rpcc::update_xid_rep(int xid) { - std::list::iterator it; - if(xid <= xid_rep_window_.front()){ return; } - for (it = xid_rep_window_.begin(); it != xid_rep_window_.end(); it++){ + for (auto it = xid_rep_window_.begin(); it != xid_rep_window_.end(); it++){ if(*it > xid){ xid_rep_window_.insert(it, xid); goto compress; @@ -378,18 +376,18 @@ rpcc::update_xid_rep(unsigned int xid) xid_rep_window_.push_back(xid); compress: - it = xid_rep_window_.begin(); + auto it = xid_rep_window_.begin(); for (it++; it != xid_rep_window_.end(); it++){ while (xid_rep_window_.front() + 1 == *it) xid_rep_window_.pop_front(); } } -rpcs::rpcs(unsigned int p1, int count) +rpcs::rpcs(unsigned int p1, size_t count) : port_(p1), counting_(count), curr_counts_(count), lossytest_(0), reachable_ (true) { set_rand_seed(); - nonce_ = random(); + nonce_ = (unsigned int)random(); jsl_log(JSL_DBG_2, "rpcs::rpcs created with nonce %d\n", nonce_); char *loss_env = getenv("RPC_LOSSY"); @@ -445,23 +443,20 @@ rpcs::updatestat(unsigned int proc) counts_[proc]++; curr_counts_--; if(curr_counts_ == 0){ - std::map::iterator i; - printf("RPC STATS: "); - for (i = counts_.begin(); i != counts_.end(); i++){ - printf("%x:%d ", i->first, i->second); - } - printf("\n"); + tprintf("RPC STATS: "); + for (auto i = counts_.begin(); i != counts_.end(); i++) + tprintf("%x:%lu ", i->first, i->second); lock rwl(reply_window_m_); std::map >::iterator clt; - unsigned int totalrep = 0, maxrep = 0; + size_t totalrep = 0, maxrep = 0; for (clt = reply_window_.begin(); clt != reply_window_.end(); clt++){ totalrep += clt->second.size(); if(clt->second.size() > maxrep) maxrep = clt->second.size(); } - jsl_log(JSL_DBG_1, "REPLY WINDOW: clients %d total reply %d max per client %d\n", + jsl_log(JSL_DBG_1, "REPLY WINDOW: clients %d total reply %lu max per client %lu\n", (int) reply_window_.size()-1, totalrep, maxrep); curr_counts_ = counting_; } @@ -476,7 +471,7 @@ rpcs::dispatch(djob_t *j) request_header h; req.unpack_req_header(&h); - int proc = h.proc; + unsigned int proc = (unsigned int)h.proc; if(!req.ok()){ jsl_log(JSL_DBG_1, "rpcs:dispatch unmarshall header failed!!!\n"); @@ -485,7 +480,7 @@ rpcs::dispatch(djob_t *j) } jsl_log(JSL_DBG_2, - "rpcs::dispatch: rpc %u (proc %x, last_rep %u) from clt %u for srv instance %u \n", + "rpcs::dispatch: rpc %d (proc %x, last_rep %d) from clt %u for srv instance %u \n", h.xid, proc, h.xid_rep, h.clt_nonce, h.srv_nonce); marshall rep; @@ -518,8 +513,8 @@ rpcs::dispatch(djob_t *j) } rpcs::rpcstate_t stat; - char *b1; - int sz1; + char *b1 = nullptr; + size_t sz1 = 0; if(h.clt_nonce){ // have i seen this client before? @@ -575,7 +570,7 @@ rpcs::dispatch(djob_t *j) rep.take_buf(&b1,&sz1); jsl_log(JSL_DBG_2, - "rpcs::dispatch: sending and saving reply of size %d for rpc %u, proc %x ret %d, clt %u\n", + "rpcs::dispatch: sending and saving reply of size %lu for rpc %d, proc %x ret %d, clt %u\n", sz1, h.xid, proc, rh.ret, h.clt_nonce); if(h.clt_nonce > 0){ @@ -605,7 +600,7 @@ rpcs::dispatch(djob_t *j) c->send(b1, sz1); break; case FORGOTTEN: // very old request and we don't have the response anymore - jsl_log(JSL_DBG_2, "rpcs::dispatch: very old request %u from %u\n", + jsl_log(JSL_DBG_2, "rpcs::dispatch: very old request %d from %u\n", h.xid, h.clt_nonce); rh.ret = rpc_const::atmostonce_failure; rep.pack_reply_header(rh); @@ -630,8 +625,8 @@ rpcs::dispatch(djob_t *j) // DONE: seen this xid, previous reply returned in *b and *sz. // FORGOTTEN: might have seen this xid, but deleted previous reply. rpcs::rpcstate_t -rpcs::checkduplicate_and_update(unsigned int clt_nonce, unsigned int xid, - unsigned int xid_rep, char **b, int *sz) +rpcs::checkduplicate_and_update(unsigned int clt_nonce, int xid, + int xid_rep, char **b, size_t *sz) { lock rwl(reply_window_m_); @@ -640,12 +635,12 @@ rpcs::checkduplicate_and_update(unsigned int clt_nonce, unsigned int xid, VERIFY(l.size() > 0); VERIFY(xid >= xid_rep); - unsigned int past_xid_rep = l.begin()->xid; + int past_xid_rep = l.begin()->xid; std::list::iterator start = l.begin(), it; it = ++start; - if (past_xid_rep < xid_rep || past_xid_rep == (unsigned int)-1) { + if (past_xid_rep < xid_rep || past_xid_rep == -1) { // scan for deletion candidates for (; it != l.end() && it->xid < xid_rep; it++) { if (it->cb_present) @@ -655,7 +650,7 @@ rpcs::checkduplicate_and_update(unsigned int clt_nonce, unsigned int xid, l.begin()->xid = xid_rep; } - if (xid < past_xid_rep && past_xid_rep != (unsigned int)-1) + if (xid < past_xid_rep && past_xid_rep != -1) return FORGOTTEN; // skip non-deletion candidates @@ -685,8 +680,8 @@ rpcs::checkduplicate_and_update(unsigned int clt_nonce, unsigned int xid, // free_reply_window() and checkduplicate_and_update is responsible for // calling free(b). void -rpcs::add_reply(unsigned int clt_nonce, unsigned int xid, - char *b, int sz) +rpcs::add_reply(unsigned int clt_nonce, int xid, + char *b, size_t sz) { lock rwl(reply_window_m_); // remember the RPC reply value @@ -706,12 +701,9 @@ rpcs::add_reply(unsigned int clt_nonce, unsigned int xid, void rpcs::free_reply_window(void) { - std::map >::iterator clt; - std::list::iterator it; - lock rwl(reply_window_m_); - for (clt = reply_window_.begin(); clt != reply_window_.end(); clt++){ - for (it = clt->second.begin(); it != clt->second.end(); it++){ + for (auto clt = reply_window_.begin(); clt != reply_window_.end(); clt++){ + for (auto it = clt->second.begin(); it != clt->second.end(); it++){ if (it->cb_present) free(it->buf); } @@ -722,7 +714,7 @@ rpcs::free_reply_window(void) // rpc handler int -rpcs::rpcbind(int &r, int a) +rpcs::rpcbind(unsigned int &r, int) { jsl_log(JSL_DBG_2, "rpcs::rpcbind called return nonce %u\n", nonce_); r = nonce_; @@ -737,22 +729,22 @@ operator<<(marshall &m, uint8_t x) { marshall & operator<<(marshall &m, uint16_t x) { - x = htons(x); + x = hton(x); m.rawbytes((char *)&x, 2); return m; } marshall & operator<<(marshall &m, uint32_t x) { - x = htonl(x); + x = hton(x); m.rawbytes((char *)&x, 4); return m; } -marshall & operator<<(marshall &m, int x) { return m << (unsigned int) x; } -marshall & operator<<(marshall &m, char x) { return m << (uint8_t)x; } +marshall & operator<<(marshall &m, int32_t x) { return m << (uint32_t) x; } +marshall & operator<<(marshall &m, int8_t x) { return m << (uint8_t)x; } marshall & operator<<(marshall &m, bool x) { return m << (uint8_t)x; } -marshall & operator<<(marshall &m, short x) { return m << (unsigned short) x; } +marshall & operator<<(marshall &m, int16_t x) { return m << (uint16_t)x; } marshall & operator<<(marshall &m, uint64_t x) { return m << (uint32_t)(x>>32) << (uint32_t)x; } marshall & @@ -763,7 +755,7 @@ operator<<(marshall &m, const std::string &s) { } void marshall::pack_req_header(const request_header &h) { - int saved_sz = index_; + size_t saved_sz = index_; //leave the first 4-byte empty for channel to fill size of pdu index_ = sizeof(rpc_sz_t); *this << h.xid << h.proc << h.clt_nonce << h.srv_nonce << h.xid_rep; @@ -771,22 +763,13 @@ void marshall::pack_req_header(const request_header &h) { } void marshall::pack_reply_header(const reply_header &h) { - int saved_sz = index_; + size_t saved_sz = index_; //leave the first 4-byte empty for channel to fill size of pdu index_ = sizeof(rpc_sz_t); *this << h.xid << h.ret; index_ = saved_sz; } -void -unmarshall::unpack(int *x) -{ - (*x) = (rawbyte() & 0xff) << 24; - (*x) |= (rawbyte() & 0xff) << 16; - (*x) |= (rawbyte() & 0xff) << 8; - (*x) |= rawbyte() & 0xff; -} - // take the contents from another unmarshall object void unmarshall::take_in(unmarshall &another) @@ -805,12 +788,12 @@ unmarshall::ensure(size_t n) { return ok_; } -unsigned int +inline uint8_t unmarshall::rawbyte() { if (!ensure(1)) return 0; - return buf_[index_++]; + return (uint8_t)buf_[index_++]; } void @@ -821,78 +804,29 @@ unmarshall::rawbytes(std::string &ss, size_t n) index_ += n; } -unmarshall & -operator>>(unmarshall &u, bool &x) -{ - x = (bool)u.rawbyte(); - return u; -} - -unmarshall & -operator>>(unmarshall &u, unsigned char &x) -{ - x = (unsigned char)u.rawbyte(); - return u; -} - -unmarshall & -operator>>(unmarshall &u, char &x) -{ - x = (char)u.rawbyte(); - return u; -} - -unmarshall & -operator>>(unmarshall &u, unsigned short &x) -{ - x = (u.rawbyte() & 0xff) << 8; - x |= u.rawbyte() & 0xff; - return u; -} - -unmarshall & -operator>>(unmarshall &u, short &x) -{ - x = (u.rawbyte() & 0xff) << 8; - x |= u.rawbyte() & 0xff; - return u; -} - -unmarshall & -operator>>(unmarshall &u, unsigned int &x) -{ - x = (u.rawbyte() & 0xff) << 24; - x |= (u.rawbyte() & 0xff) << 16; - x |= (u.rawbyte() & 0xff) << 8; - x |= u.rawbyte() & 0xff; - return u; -} - -unmarshall & -operator>>(unmarshall &u, int &x) -{ - x = (u.rawbyte() & 0xff) << 24; - x |= (u.rawbyte() & 0xff) << 16; - x |= (u.rawbyte() & 0xff) << 8; - x |= u.rawbyte() & 0xff; - return u; -} - -unmarshall & -operator>>(unmarshall &u, unsigned long long &x) +template +void +unmarshall::rawbytes(T &t) { - unsigned int h, l; - u >> h; - u >> l; - x = l | ((unsigned long long) h << 32); - return u; + const size_t n = sizeof(T); + VERIFY(ensure(n)); + memcpy(&t, buf_+index_, n); + t = ntoh(t); + index_ += n; } -unmarshall & -operator>>(unmarshall &u, std::string &s) -{ - unsigned sz; - u >> sz; +unmarshall & operator>>(unmarshall &u, bool &x) { x = (bool)u.rawbyte(); return u; } +unmarshall & operator>>(unmarshall &u, uint8_t &x) { x = u.rawbyte(); return u; } +unmarshall & operator>>(unmarshall &u, int8_t &x) { x = (int8_t)u.rawbyte(); return u; } +unmarshall & operator>>(unmarshall &u, uint16_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, int16_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, uint32_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, int32_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, size_t &x) { uint32_t xx; u.rawbytes(xx); x = xx; return u; } +unmarshall & operator>>(unmarshall &u, uint64_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, int64_t &x) { u.rawbytes(x); return u; } +unmarshall & operator>>(unmarshall &u, std::string &s) { + unsigned sz = u.grab(); if(u.ok()) u.rawbytes(s, sz); return u; @@ -906,42 +840,32 @@ bool operator<(const sockaddr_in &a, const sockaddr_in &b){ /*---------------auxilary function--------------*/ void -make_sockaddr(const char *hostandport, struct sockaddr_in *dst){ - - char host[200]; - const char *localhost = "127.0.0.1"; - const char *port = index(hostandport, ':'); - if(port == NULL){ - memcpy(host, localhost, strlen(localhost)+1); - port = hostandport; - } else { - memcpy(host, hostandport, port-hostandport); - host[port-hostandport] = '\0'; - port++; - } - - make_sockaddr(host, port, dst); - +make_sockaddr(const std::string &hostandport, struct sockaddr_in *dst) { + auto colon = hostandport.find(':'); + if (colon == std::string::npos) + make_sockaddr("127.0.0.1", hostandport, dst); + else + make_sockaddr(hostandport.substr(0, colon), hostandport.substr(colon+1), dst); } void -make_sockaddr(const char *host, const char *port, struct sockaddr_in *dst){ - - in_addr_t a; - +make_sockaddr(const std::string &host, const std::string &port, struct sockaddr_in *dst) { bzero(dst, sizeof(*dst)); dst->sin_family = AF_INET; - a = inet_addr(host); - if(a != INADDR_NONE){ - dst->sin_addr.s_addr = a; - } else { - struct hostent *hp = gethostbyname(host); - if(hp == 0 || hp->h_length != 4){ - fprintf(stderr, "cannot find host name %s\n", host); + struct in_addr a{inet_addr(host.c_str())}; + + if(a.s_addr != INADDR_NONE) + dst->sin_addr.s_addr = a.s_addr; + else { + struct hostent *hp = gethostbyname(host.c_str()); + + if (!hp || hp->h_length != 4 || hp->h_addrtype != AF_INET) { + fprintf(stderr, "cannot find host name %s\n", host.c_str()); exit(1); } - dst->sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; + memcpy(&a, hp->h_addr_list[0], sizeof(in_addr_t)); + dst->sin_addr.s_addr = a.s_addr; } - dst->sin_port = htons(atoi(port)); + dst->sin_port = hton((uint16_t)std::stoi(port)); }