X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/dfe8486473094c0769fd1922329c3f0dfd8f43c0..5d99dbf06a14904944f5593c63705934bdfdcfb7:/rpc/marshall.h?ds=sidebyside diff --git a/rpc/marshall.h b/rpc/marshall.h index fcb5bab..d7f1dff 100644 --- a/rpc/marshall.h +++ b/rpc/marshall.h @@ -1,271 +1,351 @@ #ifndef marshall_h #define marshall_h -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "lang/verify.h" - -struct req_header { - req_header(int x=0, int p=0, int c = 0, int s = 0, int xi = 0): - xid(x), proc(p), clt_nonce(c), srv_nonce(s), xid_rep(xi) {} - int xid; - int proc; - unsigned int clt_nonce; - unsigned int srv_nonce; - int xid_rep; +#include "types.h" + +// for structs or classes containing a MEMBERS declaration +class marshall; +class unmarshall; +#define FORWARD_MARSHALLABLE(_c_) \ +extern unmarshall & operator>>(unmarshall &u, typename remove_reference<_c_>::type &a); \ +extern marshall & operator<<(marshall &m, const _c_ a); +#define MARSHALLABLE(_c_) \ +inline unmarshall & operator>>(unmarshall &u, _c_ &a) { return u >> a._tuple_(); } \ +inline marshall & operator<<(marshall &m, const _c_ a) { return m << a._tuple_(); } + +// for plain old data +#define MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _d_) \ +marshall & operator<<(marshall &m, _c_ x) { _d_ y = hton((_d_)x); m.rawbytes(&y, sizeof(_d_)); return m; } \ +unmarshall & operator>>(unmarshall &u, _c_ &x) { _d_ y; u.rawbytes(&y, sizeof(_d_)); x = (_c_)ntoh(y); return u; } + +#define MARSHALL_RAW_NETWORK_ORDER(_c_) MARSHALL_RAW_NETWORK_ORDER_AS(_c_, _c_) + +using proc_t = uint32_t; +using status_t = int32_t; + +struct request_header { + int xid; + proc_t proc; + unsigned int clt_nonce; + unsigned int srv_nonce; + int xid_rep; + + MEMBERS(xid, proc, clt_nonce, srv_nonce, xid_rep) }; +FORWARD_MARSHALLABLE(request_header) +ENDIAN_SWAPPABLE(request_header) + struct reply_header { - reply_header(int x=0, int r=0): xid(x), ret(r) {} - int xid; - int ret; + int xid; + int ret; + + MEMBERS(xid, ret) }; +FORWARD_MARSHALLABLE(reply_header) +ENDIAN_SWAPPABLE(reply_header) + typedef int rpc_sz_t; -//size of initial buffer allocation -#define DEFAULT_RPC_SZ 1024 -#define RPC_HEADER_SZ (std::max(sizeof(req_header), sizeof(reply_header)) + sizeof(rpc_sz_t)) +const size_t RPC_HEADER_SZ = max(sizeof(request_header), sizeof(reply_header)) + sizeof(rpc_sz_t); +const size_t DEFAULT_RPC_SZ = 1024; // size of initial buffer allocation + +// Template parameter pack expansion is not allowed in certain contexts, but +// brace initializers (for instance, calls to constructors of empty structs) +// are fair game. +struct pass { template inline pass(Args&&...) {} }; class marshall { - private: - char *_buf; // Base of the raw bytes buffer (dynamically readjusted) - int _capa; // Capacity of the buffer - int _ind; // Read/write head position - - public: - marshall() { - _buf = (char *) malloc(sizeof(char)*DEFAULT_RPC_SZ); - VERIFY(_buf); - _capa = DEFAULT_RPC_SZ; - _ind = RPC_HEADER_SZ; - } - - ~marshall() { - if (_buf) - free(_buf); - } - - int size() { return _ind;} - char *cstr() { return _buf;} - - void rawbyte(unsigned char); - void rawbytes(const char *, int); - - // Return the current content (excluding header) as a string - std::string get_content() { - return std::string(_buf+RPC_HEADER_SZ,_ind-RPC_HEADER_SZ); - } - - // Return the current content (excluding header) as a string - std::string str() { - return get_content(); - } - - void pack(int i); - - void pack_req_header(const req_header &h) { - int saved_sz = _ind; - //leave the first 4-byte empty for channel to fill size of pdu - _ind = sizeof(rpc_sz_t); - pack(h.xid); - pack(h.proc); - pack((int)h.clt_nonce); - pack((int)h.srv_nonce); - pack(h.xid_rep); - _ind = saved_sz; - } - - void pack_reply_header(const reply_header &h) { - int saved_sz = _ind; - //leave the first 4-byte empty for channel to fill size of pdu - _ind = sizeof(rpc_sz_t); - pack(h.xid); - pack(h.ret); - _ind = saved_sz; - } - - void take_buf(char **b, int *s) { - *b = _buf; - *s = _ind; - _buf = NULL; - _ind = 0; - return; - } + private: + string buf_ = string(DEFAULT_RPC_SZ, 0); // Raw bytes buffer + size_t index_ = RPC_HEADER_SZ; // Read/write head position + + inline void reserve(size_t n) { + if (index_+n > buf_.size()) + buf_.resize(index_+n); + } + public: + template + marshall(const Args&... args) { + (void)pass{(*this << args)...}; + } + + void rawbytes(const void *p, size_t n) { + reserve(n); + copy((char *)p, (char *)p+n, &buf_[index_]); + index_ += n; + } + + // with header + operator string () const { return buf_.substr(0,index_); } + // without header + string content() { return buf_.substr(RPC_HEADER_SZ,index_-RPC_HEADER_SZ); } + + template + void pack_header(const T &h) { + VERIFY(sizeof(T)+sizeof(rpc_sz_t) <= RPC_HEADER_SZ); + size_t saved_sz = index_; + index_ = sizeof(rpc_sz_t); // first 4 bytes hold length field + *this << h; + index_ = saved_sz; + } }; -marshall& operator<<(marshall &, bool); -marshall& operator<<(marshall &, unsigned int); -marshall& operator<<(marshall &, int); -marshall& operator<<(marshall &, unsigned char); -marshall& operator<<(marshall &, char); -marshall& operator<<(marshall &, unsigned short); -marshall& operator<<(marshall &, short); -marshall& operator<<(marshall &, unsigned long long); -marshall& operator<<(marshall &, const std::string &); - -template marshall & -operator<<(marshall &m, std::vector v) -{ - m << (unsigned int) v.size(); - for(unsigned i = 0; i < v.size(); i++) - m << v[i]; - return m; +FORWARD_MARSHALLABLE(bool); +FORWARD_MARSHALLABLE(uint8_t); +FORWARD_MARSHALLABLE(int8_t); +FORWARD_MARSHALLABLE(uint16_t); +FORWARD_MARSHALLABLE(int16_t); +FORWARD_MARSHALLABLE(uint32_t); +FORWARD_MARSHALLABLE(int32_t); +FORWARD_MARSHALLABLE(size_t); +FORWARD_MARSHALLABLE(uint64_t); +FORWARD_MARSHALLABLE(int64_t); +FORWARD_MARSHALLABLE(string &); + +template typename enable_if::value, marshall>::type & +operator<<(marshall &m, const A &x) { + m << (unsigned int)x.size(); + for (const auto &a : x) + m << a; + return m; } template marshall & -operator<<(marshall &m, const std::map &d) { - typename std::map::const_iterator i; - - m << (unsigned int) d.size(); - - for (i = d.begin(); i != d.end(); i++) { - m << i->first << i->second; - } - return m; +operator<<(marshall &m, const pair &d) { + return m << d.first << d.second; } -template marshall & -operator<<(marshall &m, const std::list &d) { - m << std::vector(d.begin(), d.end()); - return m; -} +template +using enum_type_t = typename enable_if::value, typename underlying_type::type>::type; +template constexpr inline enum_type_t from_enum(E e) noexcept { return (enum_type_t)e; } +template constexpr inline E to_enum(enum_type_t value) noexcept { return (E)value; } -template marshall & -operator<<(marshall &m, const std::pair &d) { - m << d.first; - m << d.second; - return m; +template typename enable_if::value, marshall>::type & +operator<<(marshall &m, E e) { + return m << from_enum(e); } +template typename enable_if::value, unmarshall>::type & +operator>>(unmarshall &u, E &e); + class unmarshall { - private: - char *_buf; - int _sz; - int _ind; - bool _ok; - public: - unmarshall(): _buf(NULL),_sz(0),_ind(0),_ok(false) {} - unmarshall(char *b, int sz): _buf(b),_sz(sz),_ind(),_ok(true) {} - unmarshall(const std::string &s) : _buf(NULL),_sz(0),_ind(0),_ok(false) - { - //take the content which does not exclude a RPC header from a string - take_content(s); - } - ~unmarshall() { - if (_buf) free(_buf); - } - - //take contents from another unmarshall object - void take_in(unmarshall &another); - - //take the content which does not exclude a RPC header from a string - void take_content(const std::string &s) { - _sz = s.size()+RPC_HEADER_SZ; - _buf = (char *)realloc(_buf,_sz); - VERIFY(_buf); - _ind = RPC_HEADER_SZ; - memcpy(_buf+_ind, s.data(), s.size()); - _ok = true; - } - - bool ok() { return _ok; } - char *cstr() { return _buf;} - bool okdone(); - unsigned int rawbyte(); - void rawbytes(std::string &s, unsigned int n); - - int ind() { return _ind;} - int size() { return _sz;} - void unpack(int *); //non-const ref - void take_buf(char **b, int *sz) { - *b = _buf; - *sz = _sz; - _sz = _ind = 0; - _buf = NULL; - } - - void unpack_req_header(req_header *h) { - //the first 4-byte is for channel to fill size of pdu - _ind = sizeof(rpc_sz_t); - unpack(&h->xid); - unpack(&h->proc); - unpack((int *)&h->clt_nonce); - unpack((int *)&h->srv_nonce); - unpack(&h->xid_rep); - _ind = RPC_HEADER_SZ; - } - - void unpack_reply_header(reply_header *h) { - //the first 4-byte is for channel to fill size of pdu - _ind = sizeof(rpc_sz_t); - unpack(&h->xid); - unpack(&h->ret); - _ind = RPC_HEADER_SZ; - } + private: + string buf_; + size_t index_ = 0; + bool ok_ = false; + + inline bool ensure(size_t n) { + if (index_+n > buf_.size()) + ok_ = false; + return ok_; + } + public: + unmarshall() {} + unmarshall(const string &s, bool has_header) + : buf_(s),index_(RPC_HEADER_SZ) { + if (!has_header) + buf_.insert(0, RPC_HEADER_SZ, 0); + ok_ = (buf_.size() >= RPC_HEADER_SZ); + } + + bool ok() const { return ok_; } + bool okdone() const { return ok_ && index_ == buf_.size(); } + + void rawbytes(void * t, size_t n) { + VERIFY(ensure(n)); + copy(&buf_[index_], &buf_[index_+n], (char *)t); + index_ += n; + } + + template + void unpack_header(T & h) { + // first 4 bytes hold length field + VERIFY(sizeof(T)+sizeof(rpc_sz_t) <= RPC_HEADER_SZ); + index_ = sizeof(rpc_sz_t); + *this >> h; + index_ = RPC_HEADER_SZ; + } + + template inline T grab() { T t; *this >> t; return t; } }; -unmarshall& operator>>(unmarshall &, bool &); -unmarshall& operator>>(unmarshall &, unsigned char &); -unmarshall& operator>>(unmarshall &, char &); -unmarshall& operator>>(unmarshall &, unsigned short &); -unmarshall& operator>>(unmarshall &, short &); -unmarshall& operator>>(unmarshall &, unsigned int &); -unmarshall& operator>>(unmarshall &, int &); -unmarshall& operator>>(unmarshall &, unsigned long long &); -unmarshall& operator>>(unmarshall &, std::string &); - -template unmarshall & -operator>>(unmarshall &u, std::vector &v) -{ - unsigned n; - u >> n; - v.clear(); - while (n--) { - C c; - u >> c; - v.push_back(c); - } - return u; +template typename enable_if::value, unmarshall>::type & +operator>>(unmarshall &u, A &x) { + unsigned n = u.grab(); + x.clear(); + while (n--) + x.emplace_back(u.grab()); + return u; +} + +template unmarshall & +operator>>(unmarshall &u, map &x) { + unsigned n = u.grab(); + x.clear(); + while (n--) + x.emplace(u.grab>()); + return u; } template unmarshall & -operator>>(unmarshall &u, std::map &d) { - unsigned n; - u >> n; - d.clear(); - while (n--) { - A a; - B b; - u >> a >> b; - d[a] = b; +operator>>(unmarshall &u, pair &d) { + return u >> d.first >> d.second; +} + +template typename enable_if::value, unmarshall>::type & +operator>>(unmarshall &u, E &e) { + e = to_enum(u.grab>()); + return u; +} + +typedef function handler; + +// +// Automatic marshalling wrappers for RPC handlers +// + +// PAI 2013/09/19 +// C++11 does neither of these two things for us: +// 1) Declare variables using a parameter pack expansion, like so +// Args... args; +// 2) Call a function with a tuple of the arguments it expects +// +// We implement an 'invoke' function for functions of the RPC handler +// signature, i.e. int(R & r, const Args...) +// +// One thing we need in order to accomplish this is a way to cause the compiler +// to specialize 'invoke' with a parameter pack containing a list of indices +// for the elements of the tuple. This will allow us to call the underlying +// function with the exploded contents of the tuple. The empty type +// tuple_indices accomplishes this. It will be passed in to +// 'invoke' as a parameter which will be ignored, but its type will force the +// compiler to specialize 'invoke' appropriately. + +// This class encapsulates the default response to runtime unmarshalling +// failures. The templated wrappers below may optionally use a different +// class. + +struct VerifyOnFailure { + static inline int unmarshall_args_failure() { + VERIFY(0); + return 0; } - return u; +}; + +// Here's the implementation of 'invoke'. It could be more general, but this +// meets our needs. + +// One for function pointers... + +template +typename enable_if::value, RV>::type +invoke(RV, F f, void *, R & r, args_type & t, tuple_indices) { + return f(r, move(get(t))...); +} + +// And one for pointers to member functions... + +template +typename enable_if::value, RV>::type +invoke(RV, F f, C *c, R & r, args_type & t, tuple_indices) { + return (c->*f)(r, move(get(t))...); } -template unmarshall & -operator>>(unmarshall &u, std::list &l) { - unsigned n; - u >> n; - l.clear(); - while (n--) { - C c; - u >> c; - l.push_back(c); +// The class marshalled_func_imp uses partial template specialization to +// implement the ::wrap static function. ::wrap takes a function pointer or a +// pointer to a member function and returns a handler * object which +// unmarshalls arguments, verifies successful unmarshalling, calls the supplied +// function, and marshalls the response. + +template struct marshalled_func_imp; + +// Here we specialize on the Signature template parameter to obtain the list of +// argument types. Note that we do not assume that the Functor parameter has +// the same pattern as Signature; this allows us to ignore the distinctions +// between various types of callable objects at this level of abstraction. + +template +struct marshalled_func_imp { + static inline handler *wrap(F f, C *c=nullptr) { + // This type definition corresponds to an empty struct with + // template parameters running from 0 up to (# args) - 1. + using Indices = typename make_tuple_indices::type; + // This type definition represents storage for f's unmarshalled + // arguments. decay is (most notably) stripping off const + // qualifiers. + using ArgsStorage = tuple::type...>; + // Allocate a handler (i.e. function) to hold the lambda + // which will unmarshall RPCs and call f. + return new handler([=](unmarshall &u, marshall &m) -> RV { + // Unmarshall each argument with the correct type and store the + // result in a tuple. + ArgsStorage t = {u.grab::type>()...}; + // Verify successful unmarshalling of the entire input stream. + if (!u.okdone()) + return (RV)ErrorHandler::unmarshall_args_failure(); + // Allocate space for the RPC response -- will be passed into the + // function as an lvalue reference. + R r; + // Perform the invocation. Note that Indices() calls the default + // constructor of the empty struct with the special template + // parameters. + RV b = invoke(RV(), f, c, r, t, Indices()); + // Marshall the response. + m << r; + // Make like a tree. + return b; + }); } +}; + +// More partial template specialization shenanigans to reduce the number of +// parameters which must be provided explicitly and to support a few common +// callable types. C++11 doesn't allow partial function template +// specialization, so we use classes (structs). + +template struct marshalled_func; + +template +struct marshalled_func : + public marshalled_func_imp {}; + +template +struct marshalled_func : + public marshalled_func_imp {}; + +template +struct marshalled_func> : + public marshalled_func_imp {}; + +template unmarshall & +tuple_unmarshall_imp(unmarshall & u, tuple t, tuple_indices) { + (void)pass{(u >> get(t))...}; return u; } -template unmarshall & -operator>>(unmarshall &u, std::pair &d) { - return u >> d.first >> d.second; +template unmarshall & +operator>>(unmarshall & u, tuple && t) { + using Indices = typename make_tuple_indices::type; + return tuple_unmarshall_imp(u, t, Indices()); } +template marshall & +tuple_marshall_imp(marshall & m, tuple & t, tuple_indices) { + (void)pass{(m << get(t))...}; + return m; +} + +template marshall & +operator<<(marshall & m, tuple && t) { + using Indices = typename make_tuple_indices::type; + return tuple_marshall_imp(m, t, Indices()); +} + +MARSHALLABLE(request_header) +MARSHALLABLE(reply_header) + #endif