From 5b35daaf2fe6c6fbe0b15740efbffe16ff278e6c Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 29 Dec 2018 16:16:57 -0800 Subject: IPC option rework (pipe/conn) to reduce code duplication. --- src/core/defs.h | 6 ++- src/core/listener.c | 14 +++++++ src/core/options.c | 32 +++++++++++++++ src/core/options.h | 44 ++++++++++++--------- src/core/pipe.c | 7 ++++ src/core/platform.h | 7 ++-- src/core/socket.c | 107 ++++++++++++++++++--------------------------------- src/core/transport.c | 10 +++++ src/core/transport.h | 19 ++++++++- 9 files changed, 151 insertions(+), 95 deletions(-) (limited to 'src/core') diff --git a/src/core/defs.h b/src/core/defs.h index 5c06cf53..32d6782a 100644 --- a/src/core/defs.h +++ b/src/core/defs.h @@ -139,7 +139,7 @@ typedef struct { // Types. These are used to provide more structured access to options // (and maybe later statistics). For now these are internal only. -typedef enum nni_opt_type { +typedef enum { NNI_TYPE_OPAQUE, NNI_TYPE_BOOL, NNI_TYPE_INT32, @@ -151,6 +151,8 @@ typedef enum nni_opt_type { NNI_TYPE_STRING, NNI_TYPE_SOCKADDR, NNI_TYPE_POINTER, -} nni_opt_type; +} nni_type; + +typedef nni_type nni_opt_type; #endif // CORE_DEFS_H diff --git a/src/core/listener.c b/src/core/listener.c index 84f8cafd..83f6ca41 100644 --- a/src/core/listener.c +++ b/src/core/listener.c @@ -370,6 +370,13 @@ nni_listener_setopt(nni_listener *l, const char *name, const void *val, return (NNG_EREADONLY); } + if (l->l_ops.l_setopt != NULL) { + int rv = l->l_ops.l_setopt(l->l_data, name, val, sz, t); + if (rv != NNG_ENOTSUP) { + return (rv); + } + } + for (o = l->l_ops.l_options; o && o->o_name; o++) { if (strcmp(o->o_name, name) != 0) { continue; @@ -390,6 +397,13 @@ nni_listener_getopt( { nni_option *o; + if (l->l_ops.l_getopt != NULL) { + int rv = l->l_ops.l_getopt(l->l_data, name, valp, szp, t); + if (rv != NNG_ENOTSUP) { + return (rv); + } + } + for (o = l->l_ops.l_options; o && o->o_name; o++) { if (strcmp(o->o_name, name) != 0) { continue; diff --git a/src/core/options.c b/src/core/options.c index d61b35ac..097b92b2 100644 --- a/src/core/options.c +++ b/src/core/options.c @@ -366,3 +366,35 @@ nni_copyout_str(const char *str, void *dst, size_t *szp, nni_opt_type t) return (NNG_EBADTYPE); } } + +int +nni_getopt(const nni_option *opts, const char *nm, void *arg, void *buf, + size_t *szp, nni_opt_type otype) +{ + while (opts->o_name != NULL) { + if (strcmp(opts->o_name, nm) == 0) { + if (opts->o_get == NULL) { + return (NNG_EWRITEONLY); + } + return (opts->o_get(arg, buf, szp, otype)); + } + opts++; + } + return (NNG_ENOTSUP); +} + +int +nni_setopt(const nni_option *opts, const char *nm, void *arg, const void *buf, + size_t sz, nni_opt_type otype) +{ + while (opts->o_name != NULL) { + if (strcmp(opts->o_name, nm) == 0) { + if (opts->o_set == NULL) { + return (NNG_EREADONLY); + } + return (opts->o_set(arg, buf, sz, otype)); + } + opts++; + } + return (NNG_ENOTSUP); +} \ No newline at end of file diff --git a/src/core/options.h b/src/core/options.h index f0ab9811..c0d5b23d 100644 --- a/src/core/options.h +++ b/src/core/options.h @@ -24,32 +24,31 @@ // in their own option handling, centralizing the logic for dealing with // variable sized options. -extern int nni_copyin_ms(nni_duration *, const void *, size_t, nni_opt_type); -extern int nni_copyin_bool(bool *, const void *, size_t, nni_opt_type); -extern int nni_copyin_int(int *, const void *, size_t, int, int, nni_opt_type); +extern int nni_copyin_ms(nni_duration *, const void *, size_t, nni_type); +extern int nni_copyin_bool(bool *, const void *, size_t, nni_type); +extern int nni_copyin_int(int *, const void *, size_t, int, int, nni_type); extern int nni_copyin_size( - size_t *, const void *, size_t, size_t, size_t, nni_opt_type); -extern int nni_copyin_str(char *, const void *, size_t, size_t, nni_opt_type); -extern int nni_copyin_ptr(void **, const void *, size_t, nni_opt_type); -extern int nni_copyin_u64(uint64_t *, const void *, size_t, nni_opt_type); -extern int nni_copyin_sockaddr( - nng_sockaddr *, const void *, size_t, nni_opt_type); + size_t *, const void *, size_t, size_t, size_t, nni_type); +extern int nni_copyin_str(char *, const void *, size_t, size_t, nni_type); +extern int nni_copyin_ptr(void **, const void *, size_t, nni_type); +extern int nni_copyin_u64(uint64_t *, const void *, size_t, nni_type); +extern int nni_copyin_sockaddr(nng_sockaddr *, const void *, size_t, nni_type); // nni_copyout_xxx copies out a type of the named value. It assumes that // the type is aligned and the size correct, unless NNI_TYPE_OPAQUE is passed. extern int nni_copyout(const void *, size_t, void *, size_t *); -extern int nni_copyout_bool(bool, void *, size_t *, nni_opt_type); -extern int nni_copyout_int(int, void *, size_t *, nni_opt_type); -extern int nni_copyout_ms(nng_duration, void *, size_t *, nni_opt_type); -extern int nni_copyout_ptr(void *, void *, size_t *, nni_opt_type); -extern int nni_copyout_size(size_t, void *, size_t *, nni_opt_type); +extern int nni_copyout_bool(bool, void *, size_t *, nni_type); +extern int nni_copyout_int(int, void *, size_t *, nni_type); +extern int nni_copyout_ms(nng_duration, void *, size_t *, nni_type); +extern int nni_copyout_ptr(void *, void *, size_t *, nni_type); +extern int nni_copyout_size(size_t, void *, size_t *, nni_type); extern int nni_copyout_sockaddr( - const nng_sockaddr *, void *, size_t *, nni_opt_type); -extern int nni_copyout_u64(uint64_t, void *, size_t *, nni_opt_type); + const nng_sockaddr *, void *, size_t *, nni_type); +extern int nni_copyout_u64(uint64_t, void *, size_t *, nni_type); // nni_copyout_str copies out a string. If the type is NNI_TYPE_STRING, // then it passes through a pointer, created by nni_strdup(). -extern int nni_copyout_str(const char *, void *, size_t *, nni_opt_type); +extern int nni_copyout_str(const char *, void *, size_t *, nni_type); // nni_option is used for socket, protocol, transport, and similar options. // Note that only for transports, the o_set member may be called with a NULL @@ -65,13 +64,20 @@ struct nni_option_s { // the actual size of the object that would have been copied // is supplied by the function in the size. If the object did // not fit, then NNG_EINVAL is returned. - int (*o_get)(void *, void *, size_t *, nni_opt_type); + int (*o_get)(void *, void *, size_t *, nni_type); // o_set is used to set the value of the option. For transport // endpoints only, the instance parameter (first argument) may be // NULL, in which case only a generic validation of the parameters // is performed. (This is used when setting socket options before - int (*o_set)(void *, const void *, size_t, nni_opt_type); + int (*o_set)(void *, const void *, size_t, nni_type); }; +// nni_getopt and nni_setopt are helper functions to implement options +// based on arrays of nni_option structures. +extern int nni_getopt( + const nni_option *, const char *, void *, void *, size_t *, nni_type); +extern int nni_setopt( + const nni_option *, const char *, void *, const void *, size_t, nni_type); + #endif // CORE_OPTIONS_H diff --git a/src/core/pipe.c b/src/core/pipe.c index 9357cee4..664dd3c2 100644 --- a/src/core/pipe.c +++ b/src/core/pipe.c @@ -291,6 +291,13 @@ nni_pipe_getopt( { nni_option *o; + if (p->p_tran_ops.p_getopt != NULL) { + int rv; + rv = p->p_tran_ops.p_getopt(p->p_tran_data, name, val, szp, t); + if (rv != NNG_ENOTSUP) { + return (rv); + } + } for (o = p->p_tran_ops.p_options; o && o->o_name; o++) { if (strcmp(o->o_name, name) != 0) { continue; diff --git a/src/core/platform.h b/src/core/platform.h index 2b7eb9f1..a512c39f 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -379,16 +379,15 @@ extern int nni_ipc_conn_get_peer_zoneid(nni_ipc_conn *, uint64_t *); // nni_ipc_conn_setopt is like setsockopt, but uses string names. These // are the same names from the IPC transport, generally. There are no // options that are generally settable on an IPC connection. -// NNG_OPT_REMADDR, NNG_OPT_LOCADDR, NNG_OPT_IPC_PERMISSIONS, -// NNG_OPT_ extern int nni_ipc_conn_setopt( - nni_ipc_conn *, const char *, const void *, size_t); + nni_ipc_conn *, const char *, const void *, size_t, nni_opt_type); // nni_ipc_conn_getopt is like getsockopt, but uses string names. // We support NNG_OPT_REMADDR and NNG_OPT_LOCADDR (with argument type // nng_sockaddr), and on some platforms NNG_OPT_IPC_PEER_[UID,GID,ZONEID] // (with type uint64_t.) -extern int nni_ipc_conn_getopt(nni_ipc_conn *, const char *, void *, size_t *); +extern int nni_ipc_conn_getopt( + nni_ipc_conn *, const char *, void *, size_t *, nni_opt_type); // nni_ipc_dialer_init creates a new dialer object. extern int nni_ipc_dialer_init(nni_ipc_dialer **); diff --git a/src/core/socket.c b/src/core/socket.c index 888200ef..387796a6 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -36,7 +36,7 @@ struct nni_ctx { typedef struct nni_sockopt { nni_list_node node; char * name; - nni_opt_type typ; + nni_type typ; size_t sz; void * data; } nni_sockopt; @@ -145,7 +145,7 @@ sock_get_fd(void *s, int flag, int *fdp) } static int -sock_get_sendfd(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_sendfd(void *s, void *buf, size_t *szp, nni_type t) { int fd; int rv; @@ -157,7 +157,7 @@ sock_get_sendfd(void *s, void *buf, size_t *szp, nni_opt_type t) } static int -sock_get_recvfd(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_recvfd(void *s, void *buf, size_t *szp, nni_type t) { int fd; int rv; @@ -169,38 +169,38 @@ sock_get_recvfd(void *s, void *buf, size_t *szp, nni_opt_type t) } static int -sock_get_raw(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_raw(void *s, void *buf, size_t *szp, nni_type t) { bool raw = ((nni_sock_flags(SOCK(s)) & NNI_PROTO_FLAG_RAW) != 0); return (nni_copyout_bool(raw, buf, szp, t)); } static int -sock_set_recvtimeo(void *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_recvtimeo(void *s, const void *buf, size_t sz, nni_type t) { return (nni_copyin_ms(&SOCK(s)->s_rcvtimeo, buf, sz, t)); } static int -sock_get_recvtimeo(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_recvtimeo(void *s, void *buf, size_t *szp, nni_type t) { return (nni_copyout_ms(SOCK(s)->s_rcvtimeo, buf, szp, t)); } static int -sock_set_sendtimeo(void *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_sendtimeo(void *s, const void *buf, size_t sz, nni_type t) { return (nni_copyin_ms(&SOCK(s)->s_sndtimeo, buf, sz, t)); } static int -sock_get_sendtimeo(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_sendtimeo(void *s, void *buf, size_t *szp, nni_type t) { return (nni_copyout_ms(SOCK(s)->s_sndtimeo, buf, szp, t)); } static int -sock_set_recvbuf(void *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_recvbuf(void *s, const void *buf, size_t sz, nni_type t) { int len; int rv; @@ -212,7 +212,7 @@ sock_set_recvbuf(void *s, const void *buf, size_t sz, nni_opt_type t) } static int -sock_get_recvbuf(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_recvbuf(void *s, void *buf, size_t *szp, nni_type t) { int len = nni_msgq_cap(SOCK(s)->s_urq); @@ -220,7 +220,7 @@ sock_get_recvbuf(void *s, void *buf, size_t *szp, nni_opt_type t) } static int -sock_set_sendbuf(void *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_sendbuf(void *s, const void *buf, size_t sz, nni_type t) { int len; int rv; @@ -232,7 +232,7 @@ sock_set_sendbuf(void *s, const void *buf, size_t sz, nni_opt_type t) } static int -sock_get_sendbuf(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_sendbuf(void *s, void *buf, size_t *szp, nni_type t) { int len = nni_msgq_cap(SOCK(s)->s_uwq); @@ -240,38 +240,38 @@ sock_get_sendbuf(void *s, void *buf, size_t *szp, nni_opt_type t) } static int -sock_get_sockname(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_sockname(void *s, void *buf, size_t *szp, nni_type t) { return (nni_copyout_str(SOCK(s)->s_name, buf, szp, t)); } static int -sock_set_sockname(void *s, const void *buf, size_t sz, nni_opt_type t) +sock_set_sockname(void *s, const void *buf, size_t sz, nni_type t) { return (nni_copyin_str( SOCK(s)->s_name, buf, sizeof(SOCK(s)->s_name), sz, t)); } static int -sock_get_proto(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_proto(void *s, void *buf, size_t *szp, nni_type t) { return (nni_copyout_int(nni_sock_proto_id(SOCK(s)), buf, szp, t)); } static int -sock_get_peer(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_peer(void *s, void *buf, size_t *szp, nni_type t) { return (nni_copyout_int(nni_sock_peer_id(SOCK(s)), buf, szp, t)); } static int -sock_get_protoname(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_protoname(void *s, void *buf, size_t *szp, nni_type t) { return (nni_copyout_str(nni_sock_proto_name(SOCK(s)), buf, szp, t)); } static int -sock_get_peername(void *s, void *buf, size_t *szp, nni_opt_type t) +sock_get_peername(void *s, void *buf, size_t *szp, nni_type t) { return (nni_copyout_str(nni_sock_peer_name(SOCK(s)), buf, szp, t)); } @@ -957,14 +957,13 @@ nni_sock_add_dialer(nni_sock *s, nni_dialer *d) int nni_sock_setopt( - nni_sock *s, const char *name, const void *v, size_t sz, nni_opt_type t) + nni_sock *s, const char *name, const void *v, size_t sz, nni_type t) { - int rv = NNG_ENOTSUP; - nni_dialer * d; - nni_listener * l; - nni_sockopt * optv; - nni_sockopt * oldv = NULL; - const nni_option *opt; + int rv; + nni_dialer * d; + nni_listener *l; + nni_sockopt * optv; + nni_sockopt * oldv = NULL; nni_mtx_lock(&s->s_mx); if (s->s_closing) { @@ -975,33 +974,18 @@ nni_sock_setopt( // Protocol options. The protocol can override options that // the socket framework would otherwise supply, like buffer // sizes. - for (opt = s->s_sock_ops.sock_options; opt->o_name != NULL; opt++) { - if (strcmp(opt->o_name, name) != 0) { - continue; - } - if (opt->o_set == NULL) { - nni_mtx_unlock(&s->s_mx); - return (NNG_EREADONLY); - } - rv = opt->o_set(s->s_data, v, sz, t); + rv = nni_setopt(s->s_sock_ops.sock_options, name, s->s_data, v, sz, t); + if (rv != NNG_ENOTSUP) { nni_mtx_unlock(&s->s_mx); return (rv); } // Some options do not go down to transports. Handle them directly. - for (opt = sock_options; opt->o_name != NULL; opt++) { - if (strcmp(opt->o_name, name) != 0) { - continue; - } - if (opt->o_set == NULL) { - nni_mtx_unlock(&s->s_mx); - return (NNG_EREADONLY); - } - rv = opt->o_set(s, v, sz, t); + rv = nni_setopt(sock_options, name, s, v, sz, t); + if (rv != NNG_ENOTSUP) { nni_mtx_unlock(&s->s_mx); return (rv); } - nni_mtx_unlock(&s->s_mx); // If the option was already handled one way or the other, @@ -1103,11 +1087,10 @@ nni_sock_setopt( int nni_sock_getopt( - nni_sock *s, const char *name, void *val, size_t *szp, nni_opt_type t) + nni_sock *s, const char *name, void *val, size_t *szp, nni_type t) { - int rv = NNG_ENOTSUP; - nni_sockopt * sopt; - const nni_option *opt; + int rv = NNG_ENOTSUP; + nni_sockopt *sopt; nni_mtx_lock(&s->s_mx); if (s->s_closing) { @@ -1118,29 +1101,16 @@ nni_sock_getopt( // Protocol specific options. The protocol can override // options like the send buffer or notification descriptors // this way. - for (opt = s->s_sock_ops.sock_options; opt->o_name != NULL; opt++) { - if (strcmp(name, opt->o_name) != 0) { - continue; - } - if (opt->o_get == NULL) { - nni_mtx_unlock(&s->s_mx); - return (NNG_EWRITEONLY); - } - rv = opt->o_get(s->s_data, val, szp, t); + rv = nni_getopt( + s->s_sock_ops.sock_options, name, s->s_data, val, szp, t); + if (rv != NNG_ENOTSUP) { nni_mtx_unlock(&s->s_mx); return (rv); } // Socket generic options. - for (opt = sock_options; opt->o_name != NULL; opt++) { - if (strcmp(name, opt->o_name) != 0) { - continue; - } - if (opt->o_get == NULL) { - nni_mtx_unlock(&s->s_mx); - return (NNG_EWRITEONLY); - } - rv = opt->o_get(s, val, szp, t); + rv = nni_getopt(sock_options, name, s, val, szp, t); + if (rv != NNG_ENOTSUP) { nni_mtx_unlock(&s->s_mx); return (rv); } @@ -1351,8 +1321,7 @@ nni_ctx_recv(nni_ctx *ctx, nni_aio *aio) } int -nni_ctx_getopt( - nni_ctx *ctx, const char *opt, void *v, size_t *szp, nni_opt_type t) +nni_ctx_getopt(nni_ctx *ctx, const char *opt, void *v, size_t *szp, nni_type t) { nni_sock * sock = ctx->c_sock; nni_option *o; @@ -1382,7 +1351,7 @@ nni_ctx_getopt( int nni_ctx_setopt( - nni_ctx *ctx, const char *opt, const void *v, size_t sz, nni_opt_type t) + nni_ctx *ctx, const char *opt, const void *v, size_t sz, nni_type t) { nni_sock * sock = ctx->c_sock; nni_option *o; diff --git a/src/core/transport.c b/src/core/transport.c index 185ab779..da192416 100644 --- a/src/core/transport.c +++ b/src/core/transport.c @@ -125,6 +125,11 @@ nni_tran_chkopt(const char *name, const void *v, size_t sz, int typ) // Generally we look for endpoint options. We check both // dialers and listeners. dops = t->t_tran.tran_dialer; + if ((dops->d_setopt != NULL) && + ((rv = dops->d_setopt(NULL, name, v, sz, typ)) != + NNG_ENOTSUP)) { + return (rv); + } for (o = dops->d_options; o && o->o_name != NULL; o++) { if (strcmp(name, o->o_name) != 0) { continue; @@ -138,6 +143,11 @@ nni_tran_chkopt(const char *name, const void *v, size_t sz, int typ) return (rv); } lops = t->t_tran.tran_listener; + if ((lops->l_setopt != NULL) && + ((rv = lops->l_setopt(NULL, name, v, sz, typ)) != + NNG_ENOTSUP)) { + return (rv); + } for (o = lops->l_options; o && o->o_name != NULL; o++) { if (strcmp(name, o->o_name) != 0) { continue; diff --git a/src/core/transport.h b/src/core/transport.h index 1736f8ea..338b3f67 100644 --- a/src/core/transport.h +++ b/src/core/transport.h @@ -27,7 +27,8 @@ #define NNI_TRANSPORT_V3 0x54520003 #define NNI_TRANSPORT_V4 0x54520004 #define NNI_TRANSPORT_V5 0x54520005 -#define NNI_TRANSPORT_VERSION NNI_TRANSPORT_V5 +#define NNI_TRANSPORT_V6 0x54220006 +#define NNI_TRANSPORT_VERSION NNI_TRANSPORT_V6 // Endpoint operations are called by the socket in a // protocol-independent fashion. The socket makes individual calls, @@ -58,6 +59,12 @@ struct nni_tran_dialer_ops { // nonblocking. void (*d_close)(void *); + // d_getopt is used to obtain an option. + int (*d_getopt)(void *, const char *, void *, size_t *, nni_type); + + // d_setopt is used to set or change an option. + int (*d_setopt)(void *, const char *, const void *, size_t, nni_type); + // d_options is an array of dialer options. The final // element must have a NULL name. If this member is NULL, then // no dialer specific options are available. @@ -88,6 +95,12 @@ struct nni_tran_listener_ops { // nonblocking. void (*l_close)(void *); + // l_getopt is used to obtain an option. + int (*l_getopt)(void *, const char *, void *, size_t *, nni_type); + + // l_setopt is used to set or change an option. + int (*l_setopt)(void *, const char *, const void *, size_t, nni_type); + // l_options is an array of listener options. The final // element must have a NULL name. If this member is NULL, then // no dialer specific options are available. @@ -137,6 +150,10 @@ struct nni_tran_pipe_ops { // whatever transport specific manner is appropriate. uint16_t (*p_peer)(void *); + // p_getopt is used to obtain an option. Pipes don't implement + // option setting. + int (*p_getopt)(void *, const char *, void *, size_t *, nni_type); + // p_options is an array of pipe options. The final element // must have a NULL name. If this member is NULL, then no // transport specific options are available. -- cgit v1.2.3-70-g09d2