clnt_clts.c revision de8c4a14ec9a49bad5e62b2cfa6c1ba21de1c708
1N/A/*
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License (the "License").
1N/A * You may not use this file except in compliance with the License.
1N/A *
1N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * When distributing Covered Code, include this CDDL HEADER in each
1N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A * If applicable, add the following below this CDDL HEADER, with the
1N/A * fields enclosed by brackets "[]" replaced with your own identifying
1N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1N/A *
1N/A * CDDL HEADER END
1N/A */
1N/A/*
1N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A */
1N/A
1N/A/*
1N/A * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
1N/A * All Rights Reserved
1N/A */
1N/A
1N/A/*
1N/A * Portions of this source code were derived from Berkeley 4.3 BSD
1N/A * under license from the Regents of the University of California.
1N/A */
1N/A
1N/A
1N/A/*
1N/A * Implements a kernel based, client side RPC.
1N/A */
1N/A
1N/A#include <sys/param.h>
1N/A#include <sys/types.h>
1N/A#include <sys/systm.h>
1N/A#include <sys/sysmacros.h>
1N/A#include <sys/stream.h>
1N/A#include <sys/strsubr.h>
1N/A#include <sys/ddi.h>
1N/A#include <sys/tiuser.h>
1N/A#include <sys/tihdr.h>
1N/A#include <sys/t_kuser.h>
1N/A#include <sys/errno.h>
1N/A#include <sys/kmem.h>
1N/A#include <sys/debug.h>
1N/A#include <sys/kstat.h>
1N/A#include <sys/t_lock.h>
1N/A#include <sys/cmn_err.h>
1N/A#include <sys/conf.h>
1N/A#include <sys/disp.h>
1N/A#include <sys/taskq.h>
1N/A#include <sys/list.h>
1N/A#include <sys/atomic.h>
1N/A#include <sys/zone.h>
1N/A#include <netinet/in.h>
1N/A#include <rpc/types.h>
1N/A#include <rpc/xdr.h>
1N/A#include <rpc/auth.h>
1N/A#include <rpc/clnt.h>
1N/A#include <rpc/rpc_msg.h>
1N/A
1N/A#include <sys/sdt.h>
1N/A
1N/Astatic enum clnt_stat clnt_clts_kcallit(CLIENT *, rpcproc_t, xdrproc_t,
1N/A caddr_t, xdrproc_t, caddr_t, struct timeval);
1N/Astatic void clnt_clts_kabort(CLIENT *);
1N/Astatic void clnt_clts_kerror(CLIENT *, struct rpc_err *);
1N/Astatic bool_t clnt_clts_kfreeres(CLIENT *, xdrproc_t, caddr_t);
1N/Astatic bool_t clnt_clts_kcontrol(CLIENT *, int, char *);
1N/Astatic void clnt_clts_kdestroy(CLIENT *);
1N/Astatic int clnt_clts_ksettimers(CLIENT *, struct rpc_timers *,
1N/A struct rpc_timers *, int, void (*)(), caddr_t, uint32_t);
1N/A
1N/A/*
1N/A * Operations vector for CLTS based RPC
1N/A */
1N/Astatic struct clnt_ops clts_ops = {
1N/A clnt_clts_kcallit, /* do rpc call */
1N/A clnt_clts_kabort, /* abort call */
1N/A clnt_clts_kerror, /* return error status */
1N/A clnt_clts_kfreeres, /* free results */
1N/A clnt_clts_kdestroy, /* destroy rpc handle */
1N/A clnt_clts_kcontrol, /* the ioctl() of rpc */
1N/A clnt_clts_ksettimers /* set retry timers */
1N/A};
1N/A
1N/A/*
1N/A * Endpoint for CLTS (INET, INET6, loopback, etc.)
1N/A */
1N/Atypedef struct endpnt_type {
1N/A struct endpnt_type *e_next; /* pointer to next endpoint type */
1N/A list_t e_pool; /* list of available endpoints */
1N/A list_t e_ilist; /* list of idle endpoints */
1N/A struct endpnt *e_pcurr; /* pointer to current endpoint */
1N/A char e_protofmly[KNC_STRSIZE]; /* protocol family */
1N/A dev_t e_rdev; /* device */
1N/A kmutex_t e_plock; /* pool lock */
1N/A kmutex_t e_ilock; /* idle list lock */
1N/A timeout_id_t e_itimer; /* timer to dispatch the taskq */
1N/A uint_t e_cnt; /* number of endpoints in the pool */
1N/A zoneid_t e_zoneid; /* zoneid of endpoint type */
1N/A kcondvar_t e_async_cv; /* cv for asynchronous reap threads */
1N/A uint_t e_async_count; /* count of asynchronous reap threads */
1N/A} endpnt_type_t;
1N/A
1N/Atypedef struct endpnt {
1N/A list_node_t e_node; /* link to the pool */
1N/A list_node_t e_idle; /* link to the idle list */
1N/A endpnt_type_t *e_type; /* back pointer to endpoint type */
1N/A TIUSER *e_tiptr; /* pointer to transport endpoint */
1N/A queue_t *e_wq; /* write queue */
1N/A uint_t e_flags; /* endpoint flags */
1N/A uint_t e_ref; /* ref count on endpoint */
1N/A kcondvar_t e_cv; /* condition variable */
1N/A kmutex_t e_lock; /* protects cv and flags */
1N/A time_t e_itime; /* time when rele'd */
1N/A} endpnt_t;
1N/A
1N/A#define ENDPNT_ESTABLISHED 0x1 /* endpoint is established */
1N/A#define ENDPNT_WAITING 0x2 /* thread waiting for endpoint */
1N/A#define ENDPNT_BOUND 0x4 /* endpoint is bound */
1N/A#define ENDPNT_STALE 0x8 /* endpoint is dead */
1N/A#define ENDPNT_ONIDLE 0x10 /* endpoint is on the idle list */
1N/A
1N/Astatic krwlock_t endpnt_type_lock; /* protects endpnt_type_list */
1N/Astatic endpnt_type_t *endpnt_type_list = NULL; /* list of CLTS endpoints */
1N/Astatic struct kmem_cache *endpnt_cache; /* cache of endpnt_t's */
1N/Astatic taskq_t *endpnt_taskq; /* endpnt_t reaper thread */
1N/Astatic bool_t taskq_created; /* flag for endpnt_taskq */
1N/Astatic kmutex_t endpnt_taskq_lock; /* taskq lock */
1N/Astatic zone_key_t endpnt_destructor_key;
1N/A
1N/A#define DEFAULT_ENDPOINT_REAP_INTERVAL 60 /* 1 minute */
1N/A#define DEFAULT_INTERVAL_SHIFT 30 /* 30 seconds */
1N/A
1N/A/*
1N/A * Endpoint tunables
1N/A */
1N/Astatic int clnt_clts_max_endpoints = -1;
1N/Astatic int clnt_clts_hash_size = DEFAULT_HASH_SIZE;
1N/Astatic time_t clnt_clts_endpoint_reap_interval = -1;
1N/Astatic clock_t clnt_clts_taskq_dispatch_interval;
1N/A
1N/A/*
1N/A * Response completion hash queue
1N/A */
1N/Astatic call_table_t *clts_call_ht;
1N/A
1N/A/*
1N/A * Routines for the endpoint manager
1N/A */
1N/Astatic struct endpnt_type *endpnt_type_create(struct knetconfig *);
1N/Astatic void endpnt_type_free(struct endpnt_type *);
1N/Astatic int check_endpnt(struct endpnt *, struct endpnt **);
1N/Astatic struct endpnt *endpnt_get(struct knetconfig *, int);
1N/Astatic void endpnt_rele(struct endpnt *);
1N/Astatic void endpnt_reap_settimer(endpnt_type_t *);
1N/Astatic void endpnt_reap(endpnt_type_t *);
1N/Astatic void endpnt_reap_dispatch(void *);
1N/Astatic void endpnt_reclaim(zoneid_t);
1N/A
1N/A
1N/A/*
1N/A * Request dipatching function.
1N/A */
1N/Astatic int clnt_clts_dispatch_send(queue_t *q, mblk_t *, struct netbuf *addr,
1N/A calllist_t *, uint_t, cred_t *);
1N/A
1N/A/*
1N/A * The size of the preserialized RPC header information.
1N/A */
1N/A#define CKU_HDRSIZE 20
1N/A/*
1N/A * The initial allocation size. It is small to reduce space requirements.
1N/A */
1N/A#define CKU_INITSIZE 2048
1N/A/*
1N/A * The size of additional allocations, if required. It is larger to
1N/A * reduce the number of actual allocations.
1N/A */
1N/A#define CKU_ALLOCSIZE 8192
1N/A
1N/A/*
1N/A * Private data per rpc handle. This structure is allocated by
1N/A * clnt_clts_kcreate, and freed by clnt_clts_kdestroy.
1N/A */
1N/Astruct cku_private {
1N/A CLIENT cku_client; /* client handle */
1N/A int cku_retrys; /* request retrys */
1N/A calllist_t cku_call;
1N/A struct endpnt *cku_endpnt; /* open end point */
1N/A struct knetconfig cku_config;
1N/A struct netbuf cku_addr; /* remote address */
1N/A struct rpc_err cku_err; /* error status */
1N/A XDR cku_outxdr; /* xdr stream for output */
1N/A XDR cku_inxdr; /* xdr stream for input */
1N/A char cku_rpchdr[CKU_HDRSIZE + 4]; /* rpc header */
1N/A struct cred *cku_cred; /* credentials */
1N/A struct rpc_timers *cku_timers; /* for estimating RTT */
1N/A struct rpc_timers *cku_timeall; /* for estimating RTT */
1N/A void (*cku_feedback)(int, int, caddr_t);
1N/A /* ptr to feedback rtn */
1N/A caddr_t cku_feedarg; /* argument for feedback func */
1N/A uint32_t cku_xid; /* current XID */
1N/A bool_t cku_bcast; /* RPC broadcast hint */
1N/A int cku_useresvport; /* Use reserved port */
1N/A struct rpc_clts_client *cku_stats; /* counters for the zone */
1N/A};
1N/A
1N/Astatic const struct rpc_clts_client {
1N/A kstat_named_t rccalls;
1N/A kstat_named_t rcbadcalls;
1N/A kstat_named_t rcretrans;
1N/A kstat_named_t rcbadxids;
1N/A kstat_named_t rctimeouts;
1N/A kstat_named_t rcnewcreds;
1N/A kstat_named_t rcbadverfs;
1N/A kstat_named_t rctimers;
1N/A kstat_named_t rcnomem;
1N/A kstat_named_t rccantsend;
1N/A} clts_rcstat_tmpl = {
1N/A { "calls", KSTAT_DATA_UINT64 },
1N/A { "badcalls", KSTAT_DATA_UINT64 },
1N/A { "retrans", KSTAT_DATA_UINT64 },
1N/A { "badxids", KSTAT_DATA_UINT64 },
1N/A { "timeouts", KSTAT_DATA_UINT64 },
1N/A { "newcreds", KSTAT_DATA_UINT64 },
1N/A { "badverfs", KSTAT_DATA_UINT64 },
1N/A { "timers", KSTAT_DATA_UINT64 },
1N/A { "nomem", KSTAT_DATA_UINT64 },
1N/A { "cantsend", KSTAT_DATA_UINT64 },
1N/A};
1N/A
1N/Astatic uint_t clts_rcstat_ndata =
1N/A sizeof (clts_rcstat_tmpl) / sizeof (kstat_named_t);
1N/A
1N/A#define RCSTAT_INCR(s, x) \
1N/A atomic_add_64(&(s)->x.value.ui64, 1)
1N/A
1N/A#define ptoh(p) (&((p)->cku_client))
1N/A#define htop(h) ((struct cku_private *)((h)->cl_private))
1N/A
1N/A/*
1N/A * Times to retry
1N/A */
1N/A#define SNDTRIES 4
1N/A#define REFRESHES 2 /* authentication refreshes */
1N/A
1N/A/*
1N/A * The following is used to determine the global default behavior for
1N/A * CLTS when binding to a local port.
1N/A *
1N/A * If the value is set to 1 the default will be to select a reserved
1N/A * (aka privileged) port, if the value is zero the default will be to
1N/A * use non-reserved ports. Users of kRPC may override this by using
1N/A * CLNT_CONTROL() and CLSET_BINDRESVPORT.
1N/A */
1N/Astatic int clnt_clts_do_bindresvport = 1;
1N/A
1N/A#define BINDRESVPORT_RETRIES 5
1N/A
1N/Avoid
1N/Aclnt_clts_stats_init(zoneid_t zoneid, struct rpc_clts_client **statsp)
1N/A{
1N/A kstat_t *ksp;
1N/A kstat_named_t *knp;
1N/A
1N/A knp = rpcstat_zone_init_common(zoneid, "unix", "rpc_clts_client",
1N/A (const kstat_named_t *)&clts_rcstat_tmpl,
1N/A sizeof (clts_rcstat_tmpl));
1N/A /*
1N/A * Backwards compatibility for old kstat clients
1N/A */
1N/A ksp = kstat_create_zone("unix", 0, "rpc_client", "rpc",
1N/A KSTAT_TYPE_NAMED, clts_rcstat_ndata,
1N/A KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid);
1N/A if (ksp) {
1N/A ksp->ks_data = knp;
1N/A kstat_install(ksp);
1N/A }
1N/A *statsp = (struct rpc_clts_client *)knp;
1N/A}
1N/A
1N/Avoid
1N/Aclnt_clts_stats_fini(zoneid_t zoneid, struct rpc_clts_client **statsp)
1N/A{
1N/A rpcstat_zone_fini_common(zoneid, "unix", "rpc_clts_client");
1N/A kstat_delete_byname_zone("unix", 0, "rpc_client", zoneid);
1N/A kmem_free(*statsp, sizeof (clts_rcstat_tmpl));
1N/A}
1N/A
1N/A/*
1N/A * Create an rpc handle for a clts rpc connection.
1N/A * Allocates space for the handle structure and the private data.
1N/A */
1N/A/* ARGSUSED */
1N/Aint
1N/Aclnt_clts_kcreate(struct knetconfig *config, struct netbuf *addr,
1N/A rpcprog_t pgm, rpcvers_t vers, int retrys, struct cred *cred,
1N/A CLIENT **cl)
1N/A{
1N/A CLIENT *h;
1N/A struct cku_private *p;
1N/A struct rpc_msg call_msg;
1N/A int error;
1N/A int plen;
1N/A
1N/A if (cl == NULL)
1N/A return (EINVAL);
1N/A
1N/A *cl = NULL;
1N/A error = 0;
1N/A
1N/A p = kmem_zalloc(sizeof (*p), KM_SLEEP);
1N/A
1N/A h = ptoh(p);
1N/A
1N/A /* handle */
1N/A h->cl_ops = &clts_ops;
1N/A h->cl_private = (caddr_t)p;
1N/A h->cl_auth = authkern_create();
1N/A
1N/A /* call message, just used to pre-serialize below */
1N/A call_msg.rm_xid = 0;
1N/A call_msg.rm_direction = CALL;
1N/A call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
1N/A call_msg.rm_call.cb_prog = pgm;
1N/A call_msg.rm_call.cb_vers = vers;
1N/A
1N/A /* private */
1N/A clnt_clts_kinit(h, addr, retrys, cred);
1N/A
1N/A xdrmem_create(&p->cku_outxdr, p->cku_rpchdr, CKU_HDRSIZE, XDR_ENCODE);
1N/A
1N/A /* pre-serialize call message header */
1N/A if (!xdr_callhdr(&p->cku_outxdr, &call_msg)) {
1N/A error = EINVAL; /* XXX */
1N/A goto bad;
1N/A }
1N/A
1N/A p->cku_config.knc_rdev = config->knc_rdev;
1N/A p->cku_config.knc_semantics = config->knc_semantics;
1N/A plen = strlen(config->knc_protofmly) + 1;
1N/A p->cku_config.knc_protofmly = kmem_alloc(plen, KM_SLEEP);
1N/A bcopy(config->knc_protofmly, p->cku_config.knc_protofmly, plen);
1N/A p->cku_useresvport = -1; /* value is has not been set */
1N/A
1N/A cv_init(&p->cku_call.call_cv, NULL, CV_DEFAULT, NULL);
1N/A mutex_init(&p->cku_call.call_lock, NULL, MUTEX_DEFAULT, NULL);
1N/A
1N/A *cl = h;
1N/A return (0);
1N/A
1N/Abad:
1N/A auth_destroy(h->cl_auth);
1N/A kmem_free(p->cku_addr.buf, addr->maxlen);
1N/A kmem_free(p, sizeof (struct cku_private));
1N/A
1N/A return (error);
1N/A}
1N/A
1N/Avoid
1N/Aclnt_clts_kinit(CLIENT *h, struct netbuf *addr, int retrys, cred_t *cred)
1N/A{
1N/A /* LINTED pointer alignment */
1N/A struct cku_private *p = htop(h);
1N/A struct rpcstat *rsp;
1N/A
1N/A rsp = zone_getspecific(rpcstat_zone_key, rpc_zone());
1N/A ASSERT(rsp != NULL);
1N/A
1N/A p->cku_retrys = retrys;
1N/A
1N/A if (p->cku_addr.maxlen < addr->len) {
1N/A if (p->cku_addr.maxlen != 0 && p->cku_addr.buf != NULL)
1N/A kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
1N/A
1N/A p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP);
1N/A p->cku_addr.maxlen = addr->maxlen;
1N/A }
1N/A
1N/A p->cku_addr.len = addr->len;
1N/A bcopy(addr->buf, p->cku_addr.buf, addr->len);
1N/A
1N/A p->cku_cred = cred;
1N/A p->cku_xid = 0;
1N/A p->cku_timers = NULL;
1N/A p->cku_timeall = NULL;
1N/A p->cku_feedback = NULL;
1N/A p->cku_bcast = FALSE;
1N/A p->cku_call.call_xid = 0;
1N/A p->cku_call.call_hash = 0;
1N/A p->cku_call.call_notified = FALSE;
1N/A p->cku_call.call_next = NULL;
1N/A p->cku_call.call_prev = NULL;
1N/A p->cku_call.call_reply = NULL;
1N/A p->cku_call.call_wq = NULL;
1N/A p->cku_stats = rsp->rpc_clts_client;
1N/A}
1N/A
1N/A/*
1N/A * set the timers. Return current retransmission timeout.
1N/A */
1N/Astatic int
1N/Aclnt_clts_ksettimers(CLIENT *h, struct rpc_timers *t, struct rpc_timers *all,
1N/A int minimum, void (*feedback)(int, int, caddr_t), caddr_t arg,
1N/A uint32_t xid)
1N/A{
1N/A /* LINTED pointer alignment */
1N/A struct cku_private *p = htop(h);
1N/A int value;
1N/A
1N/A p->cku_feedback = feedback;
1N/A p->cku_feedarg = arg;
1N/A p->cku_timers = t;
1N/A p->cku_timeall = all;
1N/A if (xid)
1N/A p->cku_xid = xid;
1N/A value = all->rt_rtxcur;
1N/A value += t->rt_rtxcur;
1N/A if (value < minimum)
1N/A return (minimum);
1N/A RCSTAT_INCR(p->cku_stats, rctimers);
1N/A return (value);
1N/A}
1N/A
1N/A/*
1N/A * Time out back off function. tim is in HZ
1N/A */
1N/A#define MAXTIMO (20 * hz)
1N/A#define backoff(tim) (((tim) < MAXTIMO) ? dobackoff(tim) : (tim))
1N/A#define dobackoff(tim) ((((tim) << 1) > MAXTIMO) ? MAXTIMO : ((tim) << 1))
1N/A
1N/A#define RETRY_POLL_TIMO 30
1N/A
1N/A/*
1N/A * Call remote procedure.
1N/A * Most of the work of rpc is done here. We serialize what is left
1N/A * of the header (some was pre-serialized in the handle), serialize
1N/A * the arguments, and send it off. We wait for a reply or a time out.
1N/A * Timeout causes an immediate return, other packet problems may cause
1N/A * a retry on the receive. When a good packet is received we deserialize
1N/A * it, and check verification. A bad reply code will cause one retry
1N/A * with full (longhand) credentials.
1N/A */
1N/Aenum clnt_stat
1N/Aclnt_clts_kcallit_addr(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args,
1N/A caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp,
1N/A struct timeval wait, struct netbuf *sin)
1N/A{
1N/A /* LINTED pointer alignment */
1N/A struct cku_private *p = htop(h);
1N/A XDR *xdrs;
1N/A int stries = p->cku_retrys;
1N/A int refreshes = REFRESHES; /* number of times to refresh cred */
1N/A int round_trip; /* time the RPC */
1N/A int error;
1N/A int hdrsz;
1N/A mblk_t *mp;
1N/A mblk_t *mpdup;
1N/A mblk_t *resp = NULL;
1N/A mblk_t *tmp;
1N/A calllist_t *call = &p->cku_call;
1N/A clock_t ori_timout, timout;
1N/A bool_t interrupted;
1N/A enum clnt_stat status;
1N/A struct rpc_msg reply_msg;
1N/A enum clnt_stat re_status;
1N/A endpnt_t *endpt;
1N/A
1N/A RCSTAT_INCR(p->cku_stats, rccalls);
1N/A
1N/A RPCLOG(2, "clnt_clts_kcallit_addr: wait.tv_sec: %ld\n", wait.tv_sec);
1N/A RPCLOG(2, "clnt_clts_kcallit_addr: wait.tv_usec: %ld\n", wait.tv_usec);
1N/A
1N/A timout = TIMEVAL_TO_TICK(&wait);
1N/A ori_timout = timout;
1N/A
1N/A if (p->cku_xid == 0) {
1N/A p->cku_xid = alloc_xid();
1N/A if (p->cku_endpnt != NULL)
1N/A endpnt_rele(p->cku_endpnt);
1N/A p->cku_endpnt = NULL;
1N/A }
1N/A call->call_zoneid = rpc_zoneid();
1N/A
1N/A mpdup = NULL;
1N/Acall_again:
1N/A
1N/A if (mpdup == NULL) {
1N/A
1N/A while ((mp = allocb(CKU_INITSIZE, BPRI_LO)) == NULL) {
1N/A if (strwaitbuf(CKU_INITSIZE, BPRI_LO)) {
1N/A p->cku_err.re_status = RPC_SYSTEMERROR;
1N/A p->cku_err.re_errno = ENOSR;
1N/A goto done;
1N/A }
1N/A }
1N/A
1N/A xdrs = &p->cku_outxdr;
1N/A xdrmblk_init(xdrs, mp, XDR_ENCODE, CKU_ALLOCSIZE);
1N/A
1N/A if (h->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) {
1N/A /*
1N/A * Copy in the preserialized RPC header
1N/A * information.
1N/A */
1N/A bcopy(p->cku_rpchdr, mp->b_rptr, CKU_HDRSIZE);
1N/A
1N/A /*
1N/A * transaction id is the 1st thing in the output
1N/A * buffer.
1N/A */
1N/A /* LINTED pointer alignment */
1N/A (*(uint32_t *)(mp->b_rptr)) = p->cku_xid;
1N/A
1N/A /* Skip the preserialized stuff. */
1N/A XDR_SETPOS(xdrs, CKU_HDRSIZE);
1N/A
1N/A /* Serialize dynamic stuff into the output buffer. */
1N/A if ((!XDR_PUTINT32(xdrs, (int32_t *)&procnum)) ||
1N/A (!AUTH_MARSHALL(h->cl_auth, xdrs, p->cku_cred)) ||
1N/A (!(*xdr_args)(xdrs, argsp))) {
1N/A freemsg(mp);
1N/A p->cku_err.re_status = RPC_CANTENCODEARGS;
1N/A p->cku_err.re_errno = EIO;
1N/A goto done;
1N/A }
1N/A } else {
1N/A uint32_t *uproc = (uint32_t *)
1N/A &p->cku_rpchdr[CKU_HDRSIZE];
1N/A IXDR_PUT_U_INT32(uproc, procnum);
1N/A
1N/A (*(uint32_t *)(&p->cku_rpchdr[0])) = p->cku_xid;
1N/A XDR_SETPOS(xdrs, 0);
1N/A
1N/A /* Serialize the procedure number and the arguments. */
1N/A if (!AUTH_WRAP(h->cl_auth, (caddr_t)p->cku_rpchdr,
1N/A CKU_HDRSIZE+4, xdrs, xdr_args, argsp)) {
1N/A freemsg(mp);
1N/A p->cku_err.re_status = RPC_CANTENCODEARGS;
1N/A p->cku_err.re_errno = EIO;
1N/A goto done;
1N/A }
1N/A }
1N/A } else
1N/A mp = mpdup;
1N/A
1N/A mpdup = dupmsg(mp);
1N/A if (mpdup == NULL) {
1N/A freemsg(mp);
1N/A p->cku_err.re_status = RPC_SYSTEMERROR;
1N/A p->cku_err.re_errno = ENOSR;
1N/A goto done;
1N/A }
1N/A
1N/A /*
1N/A * Grab an endpnt only if the endpoint is NULL. We could be retrying
1N/A * the request and in this case we want to go through the same
1N/A * source port, so that the duplicate request cache may detect a
1N/A * retry.
1N/A */
1N/A
1N/A if (p->cku_endpnt == NULL)
1N/A p->cku_endpnt = endpnt_get(&p->cku_config, p->cku_useresvport);
1N/A
1N/A if (p->cku_endpnt == NULL) {
1N/A freemsg(mp);
1N/A p->cku_err.re_status = RPC_SYSTEMERROR;
1N/A p->cku_err.re_errno = ENOSR;
1N/A goto done;
1N/A }
1N/A
1N/A round_trip = lbolt;
1N/A
1N/A error = clnt_clts_dispatch_send(p->cku_endpnt->e_wq, mp,
1N/A &p->cku_addr, call, p->cku_xid, p->cku_cred);
1N/A
1N/A if (error != 0) {
1N/A freemsg(mp);
1N/A p->cku_err.re_status = RPC_CANTSEND;
1N/A p->cku_err.re_errno = error;
1N/A RCSTAT_INCR(p->cku_stats, rccantsend);
1N/A goto done1;
1N/A }
1N/A
1N/A RPCLOG(64, "clnt_clts_kcallit_addr: sent call for xid 0x%x\n",
1N/A p->cku_xid);
1N/A
1N/A /*
1N/A * There are two reasons for which we go back to to tryread.
1N/A *
1N/A * a) In case the status is RPC_PROCUNAVAIL and we sent out a
1N/A * broadcast we should not get any invalid messages with the
1N/A * RPC_PROCUNAVAIL error back. Some broken RPC implementations
1N/A * send them and for this we have to ignore them ( as we would
1N/A * have never received them ) and look for another message
1N/A * which might contain the valid response because we don't know
1N/A * how many broken implementations are in the network. So we are
1N/A * going to loop until
1N/A * - we received a valid response
1N/A * - we have processed all invalid responses and
1N/A * got a time out when we try to receive again a
1N/A * message.
1N/A *
1N/A * b) We will jump back to tryread also in case we failed
1N/A * within the AUTH_VALIDATE. In this case we should move
1N/A * on and loop until we received a valid response or we
1N/A * have processed all responses with broken authentication
1N/A * and we got a time out when we try to receive a message.
1N/A */
1N/Atryread:
1N/A mutex_enter(&call->call_lock);
1N/A interrupted = FALSE;
1N/A if (call->call_notified == FALSE) {
1N/A klwp_t *lwp = ttolwp(curthread);
1N/A clock_t cv_wait_ret = 1; /* init to > 0 */
1N/A clock_t cv_timout = timout;
1N/A
1N/A if (lwp != NULL)
1N/A lwp->lwp_nostop++;
1N/A
1N/A cv_timout += lbolt;
1N/A
1N/A if (h->cl_nosignal)
1N/A while ((cv_wait_ret =
1N/A cv_timedwait(&call->call_cv,
1N/A &call->call_lock, cv_timout)) > 0 &&
1N/A call->call_notified == FALSE)
1N/A ;
1N/A else
1N/A while ((cv_wait_ret =
1N/A cv_timedwait_sig(&call->call_cv,
1N/A &call->call_lock, cv_timout)) > 0 &&
1N/A call->call_notified == FALSE)
1N/A ;
1N/A
1N/A if (cv_wait_ret == 0)
1N/A interrupted = TRUE;
1N/A
1N/A if (lwp != NULL)
1N/A lwp->lwp_nostop--;
1N/A }
1N/A resp = call->call_reply;
1N/A call->call_reply = NULL;
1N/A status = call->call_status;
1N/A /*
1N/A * We have to reset the call_notified here. In case we have
1N/A * to do a retry ( e.g. in case we got a RPC_PROCUNAVAIL
1N/A * error ) we need to set this to false to ensure that
1N/A * we will wait for the next message. When the next message
1N/A * is going to arrive the function clnt_clts_dispatch_notify
1N/A * will set this to true again.
1N/A */
1N/A call->call_notified = FALSE;
1N/A mutex_exit(&call->call_lock);
1N/A
1N/A if (status == RPC_TIMEDOUT) {
1N/A if (interrupted) {
1N/A /*
1N/A * We got interrupted, bail out
1N/A */
1N/A p->cku_err.re_status = RPC_INTR;
1N/A p->cku_err.re_errno = EINTR;
1N/A goto done1;
1N/A } else {
1N/A /*
1N/A * It's possible that our response arrived
1N/A * right after we timed out. Check to see
1N/A * if it has arrived before we remove the
1N/A * calllist from the dispatch queue.
1N/A */
1N/A mutex_enter(&call->call_lock);
1N/A if (call->call_notified == TRUE) {
1N/A resp = call->call_reply;
1N/A call->call_reply = NULL;
1N/A mutex_exit(&call->call_lock);
1N/A RPCLOG(8, "clnt_clts_kcallit_addr: "
1N/A "response received for request "
1N/A "w/xid 0x%x after timeout\n",
1N/A p->cku_xid);
1N/A goto getresponse;
1N/A }
1N/A mutex_exit(&call->call_lock);
1N/A
1N/A RPCLOG(8, "clnt_clts_kcallit_addr: "
1N/A "request w/xid 0x%x timedout "
1N/A "waiting for reply\n", p->cku_xid);
1N/A#if 0 /* XXX not yet */
1N/A /*
1N/A * Timeout may be due to a dead gateway. Send
1N/A * an ioctl downstream advising deletion of
1N/A * route when we reach the half-way point to
1N/A * timing out.
1N/A */
1N/A if (stries == p->cku_retrys/2) {
1N/A t_kadvise(p->cku_endpnt->e_tiptr,
1N/A (uchar_t *)p->cku_addr.buf,
1N/A p->cku_addr.len);
1N/A }
1N/A#endif /* not yet */
1N/A p->cku_err.re_status = RPC_TIMEDOUT;
1N/A p->cku_err.re_errno = ETIMEDOUT;
1N/A RCSTAT_INCR(p->cku_stats, rctimeouts);
1N/A goto done1;
1N/A }
1N/A }
1N/A
1N/Agetresponse:
1N/A /*
1N/A * Check to see if a response arrived. If it one is
1N/A * present then proceed to process the reponse. Otherwise
1N/A * fall through to retry or retransmit the request. This
1N/A * is probably not the optimal thing to do, but since we
1N/A * are most likely dealing with a unrealiable transport it
1N/A * is the safe thing to so.
1N/A */
1N/A if (resp == NULL) {
1N/A p->cku_err.re_status = RPC_CANTRECV;
1N/A p->cku_err.re_errno = EIO;
1N/A goto done1;
1N/A }
1N/A
1N/A /*
1N/A * Prepare the message for further processing. We need to remove
1N/A * the datagram header and copy the source address if necessary. No
1N/A * need to verify the header since rpcmod took care of that.
1N/A */
1N/A /*
1N/A * Copy the source address if the caller has supplied a netbuf.
1N/A */
1N/A if (sin != NULL) {
1N/A union T_primitives *pptr;
1N/A
1N/A pptr = (union T_primitives *)resp->b_rptr;
1N/A bcopy(resp->b_rptr + pptr->unitdata_ind.SRC_offset, sin->buf,
1N/A pptr->unitdata_ind.SRC_length);
1N/A sin->len = pptr->unitdata_ind.SRC_length;
1N/A }
1N/A
1N/A /*
1N/A * Pop off the datagram header.
1N/A */
1N/A hdrsz = resp->b_wptr - resp->b_rptr;
1N/A if ((resp->b_wptr - (resp->b_rptr + hdrsz)) == 0) {
1N/A tmp = resp;
1N/A resp = resp->b_cont;
1N/A tmp->b_cont = NULL;
1N/A freeb(tmp);
1N/A } else {
1N/A unsigned char *ud_off = resp->b_rptr;
1N/A resp->b_rptr += hdrsz;
1N/A tmp = dupb(resp);
1N/A if (tmp == NULL) {
1N/A p->cku_err.re_status = RPC_SYSTEMERROR;
1N/A p->cku_err.re_errno = ENOSR;
1N/A freemsg(resp);
1N/A goto done1;
1N/A }
1N/A tmp->b_cont = resp->b_cont;
1N/A resp->b_rptr = ud_off;
1N/A freeb(resp);
1N/A resp = tmp;
1N/A }
1N/A
1N/A round_trip = lbolt - round_trip;
1N/A /*
1N/A * Van Jacobson timer algorithm here, only if NOT a retransmission.
1N/A */
1N/A if (p->cku_timers != NULL && stries == p->cku_retrys) {
1N/A int rt;
1N/A
1N/A rt = round_trip;
1N/A rt -= (p->cku_timers->rt_srtt >> 3);
1N/A p->cku_timers->rt_srtt += rt;
1N/A if (rt < 0)
1N/A rt = - rt;
1N/A rt -= (p->cku_timers->rt_deviate >> 2);
1N/A p->cku_timers->rt_deviate += rt;
1N/A p->cku_timers->rt_rtxcur =
1N/A (clock_t)((p->cku_timers->rt_srtt >> 2) +
1N/A p->cku_timers->rt_deviate) >> 1;
1N/A
1N/A rt = round_trip;
1N/A rt -= (p->cku_timeall->rt_srtt >> 3);
1N/A p->cku_timeall->rt_srtt += rt;
1N/A if (rt < 0)
1N/A rt = - rt;
1N/A rt -= (p->cku_timeall->rt_deviate >> 2);
1N/A p->cku_timeall->rt_deviate += rt;
1N/A p->cku_timeall->rt_rtxcur =
1N/A (clock_t)((p->cku_timeall->rt_srtt >> 2) +
1N/A p->cku_timeall->rt_deviate) >> 1;
1N/A if (p->cku_feedback != NULL) {
1N/A (*p->cku_feedback)(FEEDBACK_OK, procnum,
1N/A p->cku_feedarg);
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * Process reply
1N/A */
1N/A xdrs = &(p->cku_inxdr);
1N/A xdrmblk_init(xdrs, resp, XDR_DECODE, 0);
1N/A
1N/A reply_msg.rm_direction = REPLY;
1N/A reply_msg.rm_reply.rp_stat = MSG_ACCEPTED;
1N/A reply_msg.acpted_rply.ar_stat = SUCCESS;
1N/A reply_msg.acpted_rply.ar_verf = _null_auth;
1N/A /*
1N/A * xdr_results will be done in AUTH_UNWRAP.
1N/A */
1N/A reply_msg.acpted_rply.ar_results.where = NULL;
1N/A reply_msg.acpted_rply.ar_results.proc = xdr_void;
1N/A
1N/A /*
1N/A * Decode and validate the response.
1N/A */
1N/A if (!xdr_replymsg(xdrs, &reply_msg)) {
1N/A p->cku_err.re_status = RPC_CANTDECODERES;
1N/A p->cku_err.re_errno = EIO;
1N/A (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1N/A goto done1;
1N/A }
1N/A
1N/A _seterr_reply(&reply_msg, &(p->cku_err));
1N/A
1N/A re_status = p->cku_err.re_status;
1N/A if (re_status == RPC_SUCCESS) {
1N/A /*
1N/A * Reply is good, check auth.
1N/A */
1N/A if (!AUTH_VALIDATE(h->cl_auth,
1N/A &reply_msg.acpted_rply.ar_verf)) {
1N/A p->cku_err.re_status = RPC_AUTHERROR;
1N/A p->cku_err.re_why = AUTH_INVALIDRESP;
1N/A RCSTAT_INCR(p->cku_stats, rcbadverfs);
1N/A (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1N/A goto tryread;
1N/A }
1N/A if (!AUTH_UNWRAP(h->cl_auth, xdrs, xdr_results, resultsp)) {
1N/A p->cku_err.re_status = RPC_CANTDECODERES;
1N/A p->cku_err.re_errno = EIO;
1N/A }
1N/A (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1N/A goto done1;
1N/A }
1N/A /* set errno in case we can't recover */
1N/A if (re_status != RPC_VERSMISMATCH &&
1N/A re_status != RPC_AUTHERROR && re_status != RPC_PROGVERSMISMATCH)
1N/A p->cku_err.re_errno = EIO;
1N/A /*
1N/A * Determine whether or not we're doing an RPC
1N/A * broadcast. Some server implementations don't
1N/A * follow RFC 1050, section 7.4.2 in that they
1N/A * don't remain silent when they see a proc
1N/A * they don't support. Therefore we keep trying
1N/A * to receive on RPC_PROCUNAVAIL, hoping to get
1N/A * a valid response from a compliant server.
1N/A */
1N/A if (re_status == RPC_PROCUNAVAIL && p->cku_bcast) {
1N/A (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1N/A goto tryread;
1N/A }
1N/A if (re_status == RPC_AUTHERROR) {
1N/A /*
1N/A * Maybe our credential need to be refreshed
1N/A */
1N/A if (refreshes > 0 &&
1N/A AUTH_REFRESH(h->cl_auth, &reply_msg, p->cku_cred)) {
1N/A /*
1N/A * The credential is refreshed. Try the request again.
1N/A * Even if stries == 0, we still retry as long as
1N/A * refreshes > 0. This prevents a soft authentication
1N/A * error turning into a hard one at an upper level.
1N/A */
1N/A refreshes--;
1N/A RCSTAT_INCR(p->cku_stats, rcbadcalls);
1N/A RCSTAT_INCR(p->cku_stats, rcnewcreds);
1N/A
1N/A (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1N/A freemsg(mpdup);
1N/A call_table_remove(call);
1N/A mutex_enter(&call->call_lock);
1N/A if (call->call_reply != NULL) {
1N/A freemsg(call->call_reply);
1N/A call->call_reply = NULL;
1N/A }
1N/A mutex_exit(&call->call_lock);
1N/A
1N/A freemsg(resp);
1N/A mpdup = NULL;
1N/A goto call_again;
1N/A }
1N/A /*
1N/A * We have used the client handle to do an AUTH_REFRESH
1N/A * and the RPC status may be set to RPC_SUCCESS;
1N/A * Let's make sure to set it to RPC_AUTHERROR.
1N/A */
1N/A p->cku_err.re_status = RPC_CANTDECODERES;
1N/A
1N/A /*
1N/A * Map recoverable and unrecoverable
1N/A * authentication errors to appropriate errno
1N/A */
1N/A switch (p->cku_err.re_why) {
1N/A case AUTH_TOOWEAK:
1N/A /*
1N/A * Could be an nfsportmon failure, set
1N/A * useresvport and try again.
1N/A */
1N/A if (p->cku_useresvport != 1) {
1N/A p->cku_useresvport = 1;
1N/A (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1N/A freemsg(mpdup);
1N/A
1N/A call_table_remove(call);
1N/A mutex_enter(&call->call_lock);
1N/A if (call->call_reply != NULL) {
1N/A freemsg(call->call_reply);
1N/A call->call_reply = NULL;
1N/A }
1N/A mutex_exit(&call->call_lock);
1N/A
1N/A freemsg(resp);
1N/A mpdup = NULL;
1N/A endpt = p->cku_endpnt;
1N/A if (endpt->e_tiptr != NULL) {
1N/A mutex_enter(&endpt->e_lock);
1N/A endpt->e_flags &= ~ENDPNT_BOUND;
1N/A (void) t_kclose(endpt->e_tiptr, 1);
1N/A endpt->e_tiptr = NULL;
1N/A mutex_exit(&endpt->e_lock);
1N/A
1N/A }
1N/A
1N/A p->cku_xid = alloc_xid();
1N/A endpnt_rele(p->cku_endpnt);
1N/A p->cku_endpnt = NULL;
1N/A goto call_again;
1N/A }
1N/A /* FALLTHRU */
1N/A case AUTH_BADCRED:
1N/A case AUTH_BADVERF:
1N/A case AUTH_INVALIDRESP:
1N/A case AUTH_FAILED:
1N/A case RPCSEC_GSS_NOCRED:
1N/A case RPCSEC_GSS_FAILED:
1N/A p->cku_err.re_errno = EACCES;
1N/A break;
1N/A case AUTH_REJECTEDCRED:
1N/A case AUTH_REJECTEDVERF:
1N/A default:
1N/A p->cku_err.re_errno = EIO;
1N/A break;
1N/A }
1N/A RPCLOG(1, "clnt_clts_kcallit : authentication failed "
1N/A "with RPC_AUTHERROR of type %d\n",
1N/A p->cku_err.re_why);
1N/A }
1N/A
1N/A (void) xdr_rpc_free_verifier(xdrs, &reply_msg);
1N/A
1N/Adone1:
1N/A call_table_remove(call);
1N/A mutex_enter(&call->call_lock);
1N/A if (call->call_reply != NULL) {
1N/A freemsg(call->call_reply);
1N/A call->call_reply = NULL;
1N/A }
1N/A mutex_exit(&call->call_lock);
1N/A RPCLOG(64, "clnt_clts_kcallit_addr: xid 0x%x taken off dispatch list",
1N/A p->cku_xid);
1N/A
1N/Adone:
1N/A if (resp != NULL) {
1N/A freemsg(resp);
1N/A resp = NULL;
1N/A }
1N/A
1N/A if ((p->cku_err.re_status != RPC_SUCCESS) &&
1N/A (p->cku_err.re_status != RPC_INTR) &&
1N/A (p->cku_err.re_status != RPC_UDERROR) &&
1N/A !IS_UNRECOVERABLE_RPC(p->cku_err.re_status)) {
1N/A if (p->cku_feedback != NULL && stries == p->cku_retrys) {
1N/A (*p->cku_feedback)(FEEDBACK_REXMIT1, procnum,
1N/A p->cku_feedarg);
1N/A }
1N/A
1N/A timout = backoff(timout);
1N/A if (p->cku_timeall != (struct rpc_timers *)0)
1N/A p->cku_timeall->rt_rtxcur = timout;
1N/A
1N/A if (p->cku_err.re_status == RPC_SYSTEMERROR ||
1N/A p->cku_err.re_status == RPC_CANTSEND) {
1N/A /*
1N/A * Errors due to lack of resources, wait a bit
1N/A * and try again.
1N/A */
1N/A (void) delay(hz/10);
1N/A /* (void) sleep((caddr_t)&lbolt, PZERO-4); */
1N/A }
1N/A if (stries-- > 0) {
1N/A RCSTAT_INCR(p->cku_stats, rcretrans);
1N/A goto call_again;
1N/A }
1N/A }
1N/A
1N/A if (mpdup != NULL)
1N/A freemsg(mpdup);
1N/A
1N/A if (p->cku_err.re_status != RPC_SUCCESS) {
1N/A RCSTAT_INCR(p->cku_stats, rcbadcalls);
1N/A }
1N/A
1N/A /*
1N/A * Allow the endpoint to be held by the client handle in case this
1N/A * RPC was not successful. A retry may occur at a higher level and
1N/A * in this case we may want to send the request over the same
1N/A * source port.
1N/A * Endpoint is also released for one-way RPC: no reply, nor retransmit
1N/A * is expected.
1N/A */
1N/A if ((p->cku_err.re_status == RPC_SUCCESS ||
1N/A (p->cku_err.re_status == RPC_TIMEDOUT && ori_timout == 0)) &&
1N/A p->cku_endpnt != NULL) {
1N/A endpnt_rele(p->cku_endpnt);
1N/A p->cku_endpnt = NULL;
1N/A } else {
1N/A DTRACE_PROBE2(clnt_clts_kcallit_done, int, p->cku_err.re_status,
1N/A struct endpnt *, p->cku_endpnt);
1N/A }
1N/A
1N/A return (p->cku_err.re_status);
1N/A}
1N/A
1N/Astatic enum clnt_stat
1N/Aclnt_clts_kcallit(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args,
1N/A caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp,
1N/A struct timeval wait)
1N/A{
1N/A return (clnt_clts_kcallit_addr(h, procnum, xdr_args, argsp,
1N/A xdr_results, resultsp, wait, NULL));
1N/A}
1N/A
1N/A/*
1N/A * Return error info on this handle.
1N/A */
1N/Astatic void
1N/Aclnt_clts_kerror(CLIENT *h, struct rpc_err *err)
1N/A{
1N/A /* LINTED pointer alignment */
1N/A struct cku_private *p = htop(h);
1N/A
1N/A *err = p->cku_err;
1N/A}
1N/A
1N/Astatic bool_t
1N/Aclnt_clts_kfreeres(CLIENT *h, xdrproc_t xdr_res, caddr_t res_ptr)
1N/A{
1N/A /* LINTED pointer alignment */
1N/A struct cku_private *p = htop(h);
1N/A XDR *xdrs;
1N/A
1N/A xdrs = &(p->cku_outxdr);
1N/A xdrs->x_op = XDR_FREE;
1N/A return ((*xdr_res)(xdrs, res_ptr));
1N/A}
1N/A
1N/A/*ARGSUSED*/
1N/Astatic void
1N/Aclnt_clts_kabort(CLIENT *h)
1N/A{
1N/A}
1N/A
1N/Astatic bool_t
1N/Aclnt_clts_kcontrol(CLIENT *h, int cmd, char *arg)
1N/A{
1N/A /* LINTED pointer alignment */
1N/A struct cku_private *p = htop(h);
1N/A
1N/A switch (cmd) {
1N/A case CLSET_XID:
1N/A p->cku_xid = *((uint32_t *)arg);
1N/A return (TRUE);
1N/A
1N/A case CLGET_XID:
1N/A *((uint32_t *)arg) = p->cku_xid;
1N/A return (TRUE);
1N/A
1N/A case CLSET_BCAST:
1N/A p->cku_bcast = *((uint32_t *)arg);
1N/A return (TRUE);
1N/A
1N/A case CLGET_BCAST:
1N/A *((uint32_t *)arg) = p->cku_bcast;
1N/A return (TRUE);
1N/A case CLSET_BINDRESVPORT:
1N/A if (arg == NULL)
1N/A return (FALSE);
1N/A
1N/A if (*(int *)arg != 1 && *(int *)arg != 0)
1N/A return (FALSE);
1N/A
1N/A p->cku_useresvport = *(int *)arg;
1N/A
1N/A return (TRUE);
1N/A
1N/A case CLGET_BINDRESVPORT:
1N/A if (arg == NULL)
1N/A return (FALSE);
1N/A
1N/A *(int *)arg = p->cku_useresvport;
1N/A
1N/A return (TRUE);
1N/A
1N/A default:
1N/A return (FALSE);
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * Destroy rpc handle.
1N/A * Frees the space used for output buffer, private data, and handle
1N/A * structure, and the file pointer/TLI data on last reference.
1N/A */
1N/Astatic void
1N/Aclnt_clts_kdestroy(CLIENT *h)
1N/A{
1N/A /* LINTED pointer alignment */
1N/A struct cku_private *p = htop(h);
1N/A calllist_t *call = &p->cku_call;
1N/A
1N/A int plen;
1N/A
1N/A RPCLOG(8, "clnt_clts_kdestroy h: %p\n", (void *)h);
1N/A RPCLOG(8, "clnt_clts_kdestroy h: xid=0x%x\n", p->cku_xid);
1N/A
1N/A if (p->cku_endpnt != NULL)
1N/A endpnt_rele(p->cku_endpnt);
1N/A
1N/A cv_destroy(&call->call_cv);
1N/A mutex_destroy(&call->call_lock);
1N/A
1N/A plen = strlen(p->cku_config.knc_protofmly) + 1;
1N/A kmem_free(p->cku_config.knc_protofmly, plen);
1N/A kmem_free(p->cku_addr.buf, p->cku_addr.maxlen);
1N/A kmem_free(p, sizeof (*p));
1N/A}
1N/A
1N/A/*
1N/A * The connectionless (CLTS) kRPC endpoint management subsystem.
1N/A *
1N/A * Because endpoints are potentially shared among threads making RPC calls,
1N/A * they are managed in a pool according to type (endpnt_type_t). Each
1N/A * endpnt_type_t points to a list of usable endpoints through the e_pool
1N/A * field, which is of type list_t. list_t is a doubly-linked list.
1N/A * The number of endpoints in the pool is stored in the e_cnt field of
1N/A * endpnt_type_t and the endpoints are reference counted using the e_ref field
1N/A * in the endpnt_t structure.
1N/A *
1N/A * As an optimization, endpoints that have no references are also linked
1N/A * to an idle list via e_ilist which is also of type list_t. When a thread
1N/A * calls endpnt_get() to obtain a transport endpoint, the idle list is first
1N/A * consulted and if such an endpoint exists, it is removed from the idle list
1N/A * and returned to the caller.
1N/A *
1N/A * If the idle list is empty, then a check is made to see if more endpoints
1N/A * can be created. If so, we proceed and create a new endpoint which is added
1N/A * to the pool and returned to the caller. If we have reached the limit and
1N/A * cannot make a new endpoint then one is returned to the caller via round-
1N/A * robin policy.
1N/A *
1N/A * When an endpoint is placed on the idle list by a thread calling
1N/A * endpnt_rele(), it is timestamped and then a reaper taskq is scheduled to
1N/A * be dispatched if one hasn't already been. When the timer fires, the
1N/A * taskq traverses the idle list and checks to see which endpoints are
1N/A * eligible to be closed. It determines this by checking if the timestamp
1N/A * when the endpoint was released has exceeded the the threshold for how long
1N/A * it should stay alive.
1N/A *
1N/A * endpnt_t structures remain persistent until the memory reclaim callback,
1N/A * endpnt_reclaim(), is invoked.
1N/A *
1N/A * Here is an example of how the data structures would be laid out by the
1N/A * subsystem:
1N/A *
1N/A * endpnt_type_t
1N/A *
1N/A * loopback inet
1N/A * _______________ ______________
1N/A * | e_next |----------------------->| e_next |---->>
1N/A * | e_pool |<---+ | e_pool |<----+
1N/A * | e_ilist |<---+--+ | e_ilist |<----+--+
1N/A * +->| e_pcurr |----+--+--+ +->| e_pcurr |-----+--+--+
1N/A * | | ... | | | | | | ... | | | |
1N/A * | | e_itimer (90) | | | | | | e_itimer (0) | | | |
1N/A * | | e_cnt (1) | | | | | | e_cnt (3) | | | |
1N/A * | +---------------+ | | | | +--------------+ | | |
1N/A * | | | | | | | |
1N/A * | endpnt_t | | | | | | |
1N/A * | ____________ | | | | ____________ | | |
1N/A * | | e_node |<------+ | | | | e_node |<------+ | |
1N/A * | | e_idle |<---------+ | | | e_idle | | | |
1N/A * +--| e_type |<------------+ +--| e_type | | | |
1N/A * | e_tiptr | | | e_tiptr | | | |
1N/A * | ... | | | ... | | | |
1N/A * | e_lock | | | e_lock | | | |
1N/A * | ... | | | ... | | | |
1N/A * | e_ref (0) | | | e_ref (2) | | | |
1N/A * | e_itime | | | e_itime | | | |
1N/A * +------------+ | +------------+ | | |
1N/A * | | | |
1N/A * | | | |
1N/A * | ____________ | | |
1N/A * | | e_node |<------+ | |
1N/A * | | e_idle |<------+--+ |
1N/A * +--| e_type | | |
1N/A * | | e_tiptr | | |
1N/A * | | ... | | |
1N/A * | | e_lock | | |
1N/A * | | ... | | |
1N/A * | | e_ref (0) | | |
1N/A * | | e_itime | | |
1N/A * | +------------+ | |
1N/A * | | |
1N/A * | | |
1N/A * | ____________ | |
1N/A * | | e_node |<------+ |
1N/A * | | e_idle | |
1N/A * +--| e_type |<------------+
1N/A * | e_tiptr |
1N/A * | ... |
1N/A * | e_lock |
1N/A * | ... |
1N/A * | e_ref (1) |
1N/A * | e_itime |
1N/A * +------------+
1N/A *
1N/A * Endpoint locking strategy:
1N/A *
1N/A * The following functions manipulate lists which hold the endpoint and the
1N/A * endpoints themselves:
1N/A *
1N/A * endpnt_get()/check_endpnt()/endpnt_rele()/endpnt_reap()/do_endpnt_reclaim()
1N/A *
1N/A * Lock description follows:
1N/A *
1N/A * endpnt_type_lock: Global reader/writer lock which protects accesses to the
1N/A * endpnt_type_list.
1N/A *
1N/A * e_plock: Lock defined in the endpnt_type_t. It is intended to
1N/A * protect accesses to the pool of endopints (e_pool) for a given
1N/A * endpnt_type_t.
1N/A *
1N/A * e_ilock: Lock defined in endpnt_type_t. It is intended to protect accesses
1N/A * to the idle list (e_ilist) of available endpoints for a given
1N/A * endpnt_type_t. It also protects access to the e_itimer, e_async_cv,
1N/A * and e_async_count fields in endpnt_type_t.
1N/A *
1N/A * e_lock: Lock defined in the endpnt structure. It is intended to protect
1N/A * flags, cv, and ref count.
1N/A *
1N/A * The order goes as follows so as not to induce deadlock.
1N/A *
1N/A * endpnt_type_lock -> e_plock -> e_ilock -> e_lock
1N/A *
1N/A * Interaction with Zones and shutting down:
1N/A *
1N/A * endpnt_type_ts are uniquely identified by the (e_zoneid, e_rdev, e_protofmly)
1N/A * tuple, which means that a zone may not reuse another zone's idle endpoints
1N/A * without first doing a t_kclose().
1N/A *
1N/A * A zone's endpnt_type_ts are destroyed when a zone is shut down; e_async_cv
1N/A * and e_async_count are used to keep track of the threads in endpnt_taskq
1N/A * trying to reap endpnt_ts in the endpnt_type_t.
1N/A */
1N/A
1N/A/*
1N/A * Allocate and initialize an endpnt_type_t
1N/A */
1N/Astatic struct endpnt_type *
1N/Aendpnt_type_create(struct knetconfig *config)
1N/A{
1N/A struct endpnt_type *etype;
1N/A
1N/A /*
1N/A * Allocate a new endpoint type to hang a list of
1N/A * endpoints off of it.
1N/A */
1N/A etype = kmem_alloc(sizeof (struct endpnt_type), KM_SLEEP);
1N/A etype->e_next = NULL;
1N/A etype->e_pcurr = NULL;
1N/A etype->e_itimer = 0;
1N/A etype->e_cnt = 0;
1N/A
1N/A (void) strncpy(etype->e_protofmly, config->knc_protofmly, KNC_STRSIZE);
1N/A mutex_init(&etype->e_plock, NULL, MUTEX_DEFAULT, NULL);
1N/A mutex_init(&etype->e_ilock, NULL, MUTEX_DEFAULT, NULL);
1N/A etype->e_rdev = config->knc_rdev;
1N/A etype->e_zoneid = rpc_zoneid();
1N/A etype->e_async_count = 0;
1N/A cv_init(&etype->e_async_cv, NULL, CV_DEFAULT, NULL);
1N/A
1N/A list_create(&etype->e_pool, sizeof (endpnt_t),
1N/A offsetof(endpnt_t, e_node));
1N/A list_create(&etype->e_ilist, sizeof (endpnt_t),
1N/A offsetof(endpnt_t, e_idle));
1N/A
1N/A /*
1N/A * Check to see if we need to create a taskq for endpoint
1N/A * reaping
1N/A */
1N/A mutex_enter(&endpnt_taskq_lock);
1N/A if (taskq_created == FALSE) {
1N/A taskq_created = TRUE;
1N/A mutex_exit(&endpnt_taskq_lock);
1N/A ASSERT(endpnt_taskq == NULL);
1N/A endpnt_taskq = taskq_create("clts_endpnt_taskq", 1,
1N/A minclsyspri, 200, INT_MAX, 0);
1N/A } else
1N/A mutex_exit(&endpnt_taskq_lock);
1N/A
1N/A return (etype);
1N/A}
1N/A
1N/A/*
1N/A * Free an endpnt_type_t
1N/A */
1N/Astatic void
1N/Aendpnt_type_free(struct endpnt_type *etype)
1N/A{
1N/A mutex_destroy(&etype->e_plock);
1N/A mutex_destroy(&etype->e_ilock);
1N/A list_destroy(&etype->e_pool);
1N/A list_destroy(&etype->e_ilist);
1N/A kmem_free(etype, sizeof (endpnt_type_t));
1N/A}
1N/A
1N/A/*
1N/A * Check the endpoint to ensure that it is suitable for use.
1N/A *
1N/A * Possible return values:
1N/A *
1N/A * return (1) - Endpoint is established, but needs to be re-opened.
1N/A * return (0) && *newp == NULL - Endpoint is established, but unusable.
1N/A * return (0) && *newp != NULL - Endpoint is established and usable.
1N/A */
1N/Astatic int
1N/Acheck_endpnt(struct endpnt *endp, struct endpnt **newp)
1N/A{
1N/A *newp = endp;
1N/A
1N/A mutex_enter(&endp->e_lock);
1N/A ASSERT(endp->e_ref >= 1);
1N/A
1N/A /*
1N/A * The first condition we check for is if the endpoint has been
1N/A * allocated, but is unusable either because it has been closed or
1N/A * has been marked stale. Only *one* thread will be allowed to
1N/A * execute the then clause. This is enforced because the first thread
1N/A * to check this condition will clear the flags, so that subsequent
1N/A * thread(s) checking this endpoint will move on.
1N/A */
1N/A if ((endp->e_flags & ENDPNT_ESTABLISHED) &&
1N/A (!(endp->e_flags & ENDPNT_BOUND) ||
1N/A (endp->e_flags & ENDPNT_STALE))) {
1N/A /*
1N/A * Clear the flags here since they will be
1N/A * set again by this thread. They need to be
1N/A * individually cleared because we want to maintain
1N/A * the state for ENDPNT_ONIDLE.
1N/A */
1N/A endp->e_flags &= ~(ENDPNT_ESTABLISHED |
1N/A ENDPNT_WAITING | ENDPNT_BOUND | ENDPNT_STALE);
1N/A mutex_exit(&endp->e_lock);
1N/A return (1);
1N/A }
1N/A
1N/A /*
1N/A * The second condition is meant for any thread that is waiting for
1N/A * an endpoint to become established. It will cv_wait() until
1N/A * the condition for the endpoint has been changed to ENDPNT_BOUND or
1N/A * ENDPNT_STALE.
1N/A */
1N/A while (!(endp->e_flags & ENDPNT_BOUND) &&
1N/A !(endp->e_flags & ENDPNT_STALE)) {
1N/A endp->e_flags |= ENDPNT_WAITING;
1N/A cv_wait(&endp->e_cv, &endp->e_lock);
1N/A }
1N/A
1N/A ASSERT(endp->e_flags & ENDPNT_ESTABLISHED);
1N/A
1N/A /*
1N/A * The last case we check for is if the endpoint has been marked stale.
1N/A * If this is the case then set *newp to NULL and return, so that the
1N/A * caller is notified of the error and can take appropriate action.
1N/A */
1N/A if (endp->e_flags & ENDPNT_STALE) {
1N/A endp->e_ref--;
1N/A *newp = NULL;
1N/A }
1N/A mutex_exit(&endp->e_lock);
1N/A return (0);
1N/A}
1N/A
1N/A#ifdef DEBUG
1N/A/*
1N/A * Provide a fault injection setting to test error conditions.
1N/A */
1N/Astatic int endpnt_get_return_null = 0;
1N/A#endif
1N/A
1N/A/*
1N/A * Returns a handle (struct endpnt *) to an open and bound endpoint
1N/A * specified by the knetconfig passed in. Returns NULL if no valid endpoint
1N/A * can be obtained.
1N/A */
1N/Astatic struct endpnt *
1N/Aendpnt_get(struct knetconfig *config, int useresvport)
1N/A{
1N/A struct endpnt_type *n_etype = NULL;
1N/A struct endpnt_type *np = NULL;
1N/A struct endpnt *new = NULL;
1N/A struct endpnt *endp = NULL;
1N/A struct endpnt *next = NULL;
1N/A TIUSER *tiptr = NULL;
1N/A int rtries = BINDRESVPORT_RETRIES;
1N/A int i = 0;
1N/A int error;
1N/A int retval;
1N/A zoneid_t zoneid = rpc_zoneid();
1N/A cred_t *cr;
1N/A
1N/A RPCLOG(1, "endpnt_get: protofmly %s, ", config->knc_protofmly);
1N/A RPCLOG(1, "rdev %ld\n", config->knc_rdev);
1N/A
1N/A#ifdef DEBUG
1N/A /*
1N/A * Inject fault if desired. Pretend we have a stale endpoint
1N/A * and return NULL.
1N/A */
1N/A if (endpnt_get_return_null > 0) {
1N/A endpnt_get_return_null--;
1N/A return (NULL);
1N/A }
1N/A#endif
1N/A rw_enter(&endpnt_type_lock, RW_READER);
1N/A
1N/Atop:
1N/A for (np = endpnt_type_list; np != NULL; np = np->e_next)
1N/A if ((np->e_zoneid == zoneid) &&
1N/A (np->e_rdev == config->knc_rdev) &&
1N/A (strcmp(np->e_protofmly,
1N/A config->knc_protofmly) == 0))
1N/A break;
1N/A
1N/A if (np == NULL && n_etype != NULL) {
1N/A ASSERT(rw_write_held(&endpnt_type_lock));
1N/A
1N/A /*
1N/A * Link the endpoint type onto the list
1N/A */
1N/A n_etype->e_next = endpnt_type_list;
1N/A endpnt_type_list = n_etype;
1N/A np = n_etype;
1N/A n_etype = NULL;
1N/A }
1N/A
1N/A if (np == NULL) {
1N/A /*
1N/A * The logic here is that we were unable to find an
1N/A * endpnt_type_t that matched our criteria, so we allocate a
1N/A * new one. Because kmem_alloc() needs to be called with
1N/A * KM_SLEEP, we drop our locks so that we don't induce
1N/A * deadlock. After allocating and initializing the
1N/A * endpnt_type_t, we reaquire the lock and go back to check
1N/A * if this entry needs to be added to the list. Since we do
1N/A * some operations without any locking other threads may
1N/A * have been looking for the same endpnt_type_t and gone
1N/A * through this code path. We check for this case and allow
1N/A * one thread to link its endpnt_type_t to the list and the
1N/A * other threads will simply free theirs.
1N/A */
1N/A rw_exit(&endpnt_type_lock);
1N/A n_etype = endpnt_type_create(config);
1N/A
1N/A /*
1N/A * We need to reaquire the lock with RW_WRITER here so that
1N/A * we can safely link the new endpoint type onto the list.
1N/A */
1N/A rw_enter(&endpnt_type_lock, RW_WRITER);
1N/A goto top;
1N/A }
1N/A
1N/A rw_exit(&endpnt_type_lock);
1N/A /*
1N/A * If n_etype is not NULL, then another thread was able to
1N/A * insert an endpnt_type_t of this type onto the list before
1N/A * we did. Go ahead and free ours.
1N/A */
1N/A if (n_etype != NULL)
1N/A endpnt_type_free(n_etype);
1N/A
1N/A mutex_enter(&np->e_ilock);
1N/A /*
1N/A * The algorithm to hand out endpoints is to first
1N/A * give out those that are idle if such endpoints
1N/A * exist. Otherwise, create a new one if we haven't
1N/A * reached the max threshold. Finally, we give out
1N/A * endpoints in a pseudo LRU fashion (round-robin).
1N/A *
1N/A * Note: The idle list is merely a hint of those endpoints
1N/A * that should be idle. There exists a window after the
1N/A * endpoint is released and before it is linked back onto the
1N/A * idle list where a thread could get a reference to it and
1N/A * use it. This is okay, since the reference counts will
1N/A * still be consistent.
1N/A */
1N/A if ((endp = (endpnt_t *)list_head(&np->e_ilist)) != NULL) {
1N/A timeout_id_t t_id = 0;
1N/A
1N/A mutex_enter(&endp->e_lock);
1N/A endp->e_ref++;
1N/A endp->e_itime = 0;
1N/A endp->e_flags &= ~ENDPNT_ONIDLE;
1N/A mutex_exit(&endp->e_lock);
1N/A
1N/A /*
1N/A * Pop the endpoint off the idle list and hand it off
1N/A */
1N/A list_remove(&np->e_ilist, endp);
1N/A
1N/A if (np->e_itimer != 0) {
1N/A t_id = np->e_itimer;
1N/A np->e_itimer = 0;
1N/A }
1N/A mutex_exit(&np->e_ilock);
1N/A /*
1N/A * Reset the idle timer if it has been set
1N/A */
1N/A if (t_id != (timeout_id_t)0)
1N/A (void) untimeout(t_id);
1N/A
1N/A if (check_endpnt(endp, &new) == 0)
1N/A return (new);
1N/A } else if (np->e_cnt >= clnt_clts_max_endpoints) {
1N/A /*
1N/A * There are no idle endpoints currently, so
1N/A * create a new one if we have not reached the maximum or
1N/A * hand one out in round-robin.
1N/A */
1N/A mutex_exit(&np->e_ilock);
1N/A mutex_enter(&np->e_plock);
1N/A endp = np->e_pcurr;
1N/A mutex_enter(&endp->e_lock);
1N/A endp->e_ref++;
1N/A mutex_exit(&endp->e_lock);
1N/A
1N/A ASSERT(endp != NULL);
1N/A /*
1N/A * Advance the pointer to the next eligible endpoint, if
1N/A * necessary.
1N/A */
1N/A if (np->e_cnt > 1) {
1N/A next = (endpnt_t *)list_next(&np->e_pool, np->e_pcurr);
1N/A if (next == NULL)
1N/A next = (endpnt_t *)list_head(&np->e_pool);
1N/A np->e_pcurr = next;
1N/A }
1N/A
1N/A mutex_exit(&np->e_plock);
1N/A
1N/A /*
1N/A * We need to check to see if this endpoint is bound or
1N/A * not. If it is in progress then just wait until
1N/A * the set up is complete
1N/A */
1N/A if (check_endpnt(endp, &new) == 0)
1N/A return (new);
1N/A } else {
1N/A mutex_exit(&np->e_ilock);
1N/A mutex_enter(&np->e_plock);
1N/A
/*
* Allocate a new endpoint to use. If we can't allocate any
* more memory then use one that is already established if any
* such endpoints exist.
*/
new = kmem_cache_alloc(endpnt_cache, KM_NOSLEEP);
if (new == NULL) {
RPCLOG0(1, "endpnt_get: kmem_cache_alloc failed\n");
/*
* Try to recover by using an existing endpoint.
*/
if (np->e_cnt <= 0) {
mutex_exit(&np->e_plock);
return (NULL);
}
endp = np->e_pcurr;
if ((next = list_next(&np->e_pool, np->e_pcurr)) !=
NULL)
np->e_pcurr = next;
ASSERT(endp != NULL);
mutex_enter(&endp->e_lock);
endp->e_ref++;
mutex_exit(&endp->e_lock);
mutex_exit(&np->e_plock);
if (check_endpnt(endp, &new) == 0)
return (new);
} else {
/*
* Partially init an endpoint structure and put
* it on the list, so that other interested threads
* know that one is being created
*/
bzero(new, sizeof (struct endpnt));
cv_init(&new->e_cv, NULL, CV_DEFAULT, NULL);
mutex_init(&new->e_lock, NULL, MUTEX_DEFAULT, NULL);
new->e_ref = 1;
new->e_type = np;
/*
* Link the endpoint into the pool.
*/
list_insert_head(&np->e_pool, new);
np->e_cnt++;
if (np->e_pcurr == NULL)
np->e_pcurr = new;
mutex_exit(&np->e_plock);
}
}
/*
* The transport should be opened with sufficient privs
*/
cr = zone_kcred();
error = t_kopen(NULL, config->knc_rdev, FREAD|FWRITE|FNDELAY, &tiptr,
cr);
if (error) {
RPCLOG(1, "endpnt_get: t_kopen: %d\n", error);
goto bad;
}
new->e_tiptr = tiptr;
rpc_poptimod(tiptr->fp->f_vnode);
/*
* Allow the kernel to push the module on behalf of the user.
*/
error = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"rpcmod", 0,
K_TO_K, cr, &retval);
if (error) {
RPCLOG(1, "endpnt_get: kstr_push on rpcmod failed %d\n", error);
goto bad;
}
error = strioctl(tiptr->fp->f_vnode, RPC_CLIENT, 0, 0, K_TO_K,
cr, &retval);
if (error) {
RPCLOG(1, "endpnt_get: strioctl failed %d\n", error);
goto bad;
}
/*
* Connectionless data flow should bypass the stream head.
*/
new->e_wq = tiptr->fp->f_vnode->v_stream->sd_wrq->q_next;
error = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"timod", 0,
K_TO_K, cr, &retval);
if (error) {
RPCLOG(1, "endpnt_get: kstr_push on timod failed %d\n", error);
goto bad;
}
/*
* Attempt to bind the endpoint. If we fail then propogate
* error back to calling subsystem, so that it can be handled
* appropriately.
* If the caller has not specified reserved port usage then
* take the system default.
*/
if (useresvport == -1)
useresvport = clnt_clts_do_bindresvport;
if (useresvport &&
(strcmp(config->knc_protofmly, NC_INET) == 0 ||
strcmp(config->knc_protofmly, NC_INET6) == 0)) {
while ((error =
bindresvport(new->e_tiptr, NULL, NULL, FALSE)) != 0) {
RPCLOG(1,
"endpnt_get: bindresvport error %d\n", error);
if (error != EPROTO) {
if (rtries-- <= 0)
goto bad;
delay(hz << i++);
continue;
}
(void) t_kclose(new->e_tiptr, 1);
/*
* reopen with all privileges
*/
error = t_kopen(NULL, config->knc_rdev,
FREAD|FWRITE|FNDELAY,
&new->e_tiptr, cr);
if (error) {
RPCLOG(1, "endpnt_get: t_kopen: %d\n", error);
new->e_tiptr = NULL;
goto bad;
}
}
} else if ((error = t_kbind(new->e_tiptr, NULL, NULL)) != 0) {
RPCLOG(1, "endpnt_get: t_kbind failed: %d\n", error);
goto bad;
}
/*
* Set the flags and notify and waiters that we have an established
* endpoint.
*/
mutex_enter(&new->e_lock);
new->e_flags |= ENDPNT_ESTABLISHED;
new->e_flags |= ENDPNT_BOUND;
if (new->e_flags & ENDPNT_WAITING) {
cv_broadcast(&new->e_cv);
new->e_flags &= ~ENDPNT_WAITING;
}
mutex_exit(&new->e_lock);
return (new);
bad:
ASSERT(new != NULL);
/*
* mark this endpoint as stale and notify any threads waiting
* on this endpoint that it will be going away.
*/
mutex_enter(&new->e_lock);
if (new->e_ref > 0) {
new->e_flags |= ENDPNT_ESTABLISHED;
new->e_flags |= ENDPNT_STALE;
if (new->e_flags & ENDPNT_WAITING) {
cv_broadcast(&new->e_cv);
new->e_flags &= ~ENDPNT_WAITING;
}
}
new->e_ref--;
new->e_tiptr = NULL;
mutex_exit(&new->e_lock);
/*
* If there was a transport endopoint opened, then close it.
*/
if (tiptr != NULL)
(void) t_kclose(tiptr, 1);
return (NULL);
}
/*
* Release a referece to the endpoint
*/
static void
endpnt_rele(struct endpnt *sp)
{
mutex_enter(&sp->e_lock);
ASSERT(sp->e_ref > 0);
sp->e_ref--;
/*
* If the ref count is zero, then start the idle timer and link
* the endpoint onto the idle list.
*/
if (sp->e_ref == 0) {
sp->e_itime = gethrestime_sec();
/*
* Check to see if the endpoint is already linked to the idle
* list, so that we don't try to reinsert it.
*/
if (sp->e_flags & ENDPNT_ONIDLE) {
mutex_exit(&sp->e_lock);
mutex_enter(&sp->e_type->e_ilock);
endpnt_reap_settimer(sp->e_type);
mutex_exit(&sp->e_type->e_ilock);
return;
}
sp->e_flags |= ENDPNT_ONIDLE;
mutex_exit(&sp->e_lock);
mutex_enter(&sp->e_type->e_ilock);
list_insert_tail(&sp->e_type->e_ilist, sp);
endpnt_reap_settimer(sp->e_type);
mutex_exit(&sp->e_type->e_ilock);
} else
mutex_exit(&sp->e_lock);
}
static void
endpnt_reap_settimer(endpnt_type_t *etp)
{
if (etp->e_itimer == (timeout_id_t)0)
etp->e_itimer = timeout(endpnt_reap_dispatch, (void *)etp,
clnt_clts_taskq_dispatch_interval);
}
static void
endpnt_reap_dispatch(void *a)
{
endpnt_type_t *etp = a;
/*
* The idle timer has fired, so dispatch the taskq to close the
* endpoint.
*/
if (taskq_dispatch(endpnt_taskq, (task_func_t *)endpnt_reap, etp,
TQ_NOSLEEP) == NULL)
return;
mutex_enter(&etp->e_ilock);
etp->e_async_count++;
mutex_exit(&etp->e_ilock);
}
/*
* Traverse the idle list and close those endpoints that have reached their
* timeout interval.
*/
static void
endpnt_reap(endpnt_type_t *etp)
{
struct endpnt *e;
struct endpnt *next_node = NULL;
mutex_enter(&etp->e_ilock);
e = list_head(&etp->e_ilist);
while (e != NULL) {
next_node = list_next(&etp->e_ilist, e);
mutex_enter(&e->e_lock);
if (e->e_ref > 0) {
mutex_exit(&e->e_lock);
e = next_node;
continue;
}
ASSERT(e->e_ref == 0);
if (e->e_itime > 0 &&
(e->e_itime + clnt_clts_endpoint_reap_interval) <
gethrestime_sec()) {
e->e_flags &= ~ENDPNT_BOUND;
(void) t_kclose(e->e_tiptr, 1);
e->e_tiptr = NULL;
e->e_itime = 0;
}
mutex_exit(&e->e_lock);
e = next_node;
}
etp->e_itimer = 0;
if (--etp->e_async_count == 0)
cv_signal(&etp->e_async_cv);
mutex_exit(&etp->e_ilock);
}
static void
endpnt_reclaim(zoneid_t zoneid)
{
struct endpnt_type *np;
struct endpnt *e;
struct endpnt *next_node = NULL;
list_t free_list;
int rcnt = 0;
list_create(&free_list, sizeof (endpnt_t), offsetof(endpnt_t, e_node));
RPCLOG0(1, "endpnt_reclaim: reclaim callback started\n");
rw_enter(&endpnt_type_lock, RW_READER);
for (np = endpnt_type_list; np != NULL; np = np->e_next) {
if (zoneid != ALL_ZONES && zoneid != np->e_zoneid)
continue;
mutex_enter(&np->e_plock);
RPCLOG(1, "endpnt_reclaim: protofmly %s, ",
np->e_protofmly);
RPCLOG(1, "rdev %ld\n", np->e_rdev);
RPCLOG(1, "endpnt_reclaim: found %d endpoint(s)\n",
np->e_cnt);
if (np->e_cnt == 0) {
mutex_exit(&np->e_plock);
continue;
}
/*
* The nice thing about maintaining an idle list is that if
* there are any endpoints to reclaim, they are going to be
* on this list. Just go through and reap the one's that
* have ref counts of zero.
*/
mutex_enter(&np->e_ilock);
e = list_head(&np->e_ilist);
while (e != NULL) {
next_node = list_next(&np->e_ilist, e);
mutex_enter(&e->e_lock);
if (e->e_ref > 0) {
mutex_exit(&e->e_lock);
e = next_node;
continue;
}
ASSERT(e->e_ref == 0);
mutex_exit(&e->e_lock);
list_remove(&np->e_ilist, e);
list_remove(&np->e_pool, e);
list_insert_head(&free_list, e);
rcnt++;
np->e_cnt--;
e = next_node;
}
mutex_exit(&np->e_ilock);
/*
* Reset the current pointer to be safe
*/
if ((e = (struct endpnt *)list_head(&np->e_pool)) != NULL)
np->e_pcurr = e;
else {
ASSERT(np->e_cnt == 0);
np->e_pcurr = NULL;
}
mutex_exit(&np->e_plock);
}
rw_exit(&endpnt_type_lock);
while ((e = list_head(&free_list)) != NULL) {
list_remove(&free_list, e);
if (e->e_tiptr != NULL)
(void) t_kclose(e->e_tiptr, 1);
cv_destroy(&e->e_cv);
mutex_destroy(&e->e_lock);
kmem_cache_free(endpnt_cache, e);
}
list_destroy(&free_list);
RPCLOG(1, "endpnt_reclaim: reclaimed %d endpoint(s)\n", rcnt);
}
/*
* Endpoint reclaim zones destructor callback routine.
*
* After reclaiming any cached entries, we basically go through the endpnt_type
* list, canceling outstanding timeouts and free'ing data structures.
*/
/* ARGSUSED */
static void
endpnt_destructor(zoneid_t zoneid, void *a)
{
struct endpnt_type **npp;
struct endpnt_type *np;
struct endpnt_type *free_list = NULL;
timeout_id_t t_id = 0;
extern void clcleanup_zone(zoneid_t);
extern void clcleanup4_zone(zoneid_t);
/* Make sure NFS client handles are released. */
clcleanup_zone(zoneid);
clcleanup4_zone(zoneid);
endpnt_reclaim(zoneid);
/*
* We don't need to be holding on to any locks across the call to
* endpnt_reclaim() and the code below; we know that no-one can
* be holding open connections for this zone (all processes and kernel
* threads are gone), so nothing could be adding anything to the list.
*/
rw_enter(&endpnt_type_lock, RW_WRITER);
npp = &endpnt_type_list;
while ((np = *npp) != NULL) {
if (np->e_zoneid != zoneid) {
npp = &np->e_next;
continue;
}
mutex_enter(&np->e_plock);
mutex_enter(&np->e_ilock);
if (np->e_itimer != 0) {
t_id = np->e_itimer;
np->e_itimer = 0;
}
ASSERT(np->e_cnt == 0);
ASSERT(list_head(&np->e_pool) == NULL);
ASSERT(list_head(&np->e_ilist) == NULL);
mutex_exit(&np->e_ilock);
mutex_exit(&np->e_plock);
/*
* untimeout() any outstanding timers that have not yet fired.
*/
if (t_id != (timeout_id_t)0)
(void) untimeout(t_id);
*npp = np->e_next;
np->e_next = free_list;
free_list = np;
}
rw_exit(&endpnt_type_lock);
while (free_list != NULL) {
np = free_list;
free_list = free_list->e_next;
/*
* Wait for threads in endpnt_taskq trying to reap endpnt_ts in
* the endpnt_type_t.
*/
mutex_enter(&np->e_ilock);
while (np->e_async_count > 0)
cv_wait(&np->e_async_cv, &np->e_ilock);
cv_destroy(&np->e_async_cv);
mutex_destroy(&np->e_plock);
mutex_destroy(&np->e_ilock);
list_destroy(&np->e_pool);
list_destroy(&np->e_ilist);
kmem_free(np, sizeof (endpnt_type_t));
}
}
/*
* Endpoint reclaim kmem callback routine.
*/
/* ARGSUSED */
static void
endpnt_repossess(void *a)
{
/*
* Reclaim idle endpnt's from all zones.
*/
if (endpnt_taskq != NULL)
(void) taskq_dispatch(endpnt_taskq,
(task_func_t *)endpnt_reclaim, (void *)ALL_ZONES,
TQ_NOSLEEP);
}
/*
* RPC request dispatch routine. Constructs a datagram message and wraps it
* around the RPC request to pass downstream.
*/
static int
clnt_clts_dispatch_send(queue_t *q, mblk_t *mp, struct netbuf *addr,
calllist_t *cp, uint_t xid, cred_t *cr)
{
mblk_t *bp;
int msgsz;
struct T_unitdata_req *udreq;
/*
* Set up the call record.
*/
cp->call_wq = q;
cp->call_xid = xid;
cp->call_status = RPC_TIMEDOUT;
cp->call_notified = FALSE;
RPCLOG(64,
"clnt_clts_dispatch_send: putting xid 0x%x on "
"dispatch list\n", xid);
cp->call_hash = call_hash(xid, clnt_clts_hash_size);
cp->call_bucket = &clts_call_ht[cp->call_hash];
call_table_enter(cp);
/*
* Construct the datagram
*/
msgsz = (int)TUNITDATAREQSZ;
/*
* Note: if the receiver uses SCM_UCRED/getpeerucred the pid will
* appear as -1.
*/
while (!(bp = allocb_cred(msgsz + addr->len, cr, NOPID))) {
if (strwaitbuf(msgsz + addr->len, BPRI_LO))
return (ENOSR);
}
udreq = (struct T_unitdata_req *)bp->b_wptr;
udreq->PRIM_type = T_UNITDATA_REQ;
udreq->DEST_length = addr->len;
if (addr->len) {
bcopy(addr->buf, bp->b_wptr + msgsz, addr->len);
udreq->DEST_offset = (t_scalar_t)msgsz;
msgsz += addr->len;
} else
udreq->DEST_offset = 0;
udreq->OPT_length = 0;
udreq->OPT_offset = 0;
bp->b_datap->db_type = M_PROTO;
bp->b_wptr += msgsz;
/*
* Link the datagram header with the actual data
*/
linkb(bp, mp);
/*
* Send downstream.
*/
if (canput(cp->call_wq)) {
put(cp->call_wq, bp);
return (0);
}
return (EIO);
}
/*
* RPC response delivery routine. Deliver the response to the waiting
* thread by matching the xid.
*/
void
clnt_clts_dispatch_notify(mblk_t *mp, int resp_off, zoneid_t zoneid)
{
calllist_t *e = NULL;
call_table_t *chtp;
uint32_t xid;
uint_t hash;
unsigned char *hdr_offset;
mblk_t *resp;
/*
* If the RPC response is not contained in the same mblk as the
* datagram header, then move to the next mblk.
*/
hdr_offset = mp->b_rptr;
resp = mp;
if ((mp->b_wptr - (mp->b_rptr + resp_off)) == 0)
resp = mp->b_cont;
else
resp->b_rptr += resp_off;
ASSERT(resp != NULL);
if ((IS_P2ALIGNED(resp->b_rptr, sizeof (uint32_t))) &&
(resp->b_wptr - resp->b_rptr) >= sizeof (xid))
xid = *((uint32_t *)resp->b_rptr);
else {
int i = 0;
unsigned char *p = (unsigned char *)&xid;
unsigned char *rptr;
mblk_t *tmp = resp;
/*
* Copy the xid, byte-by-byte into xid.
*/
while (tmp) {
rptr = tmp->b_rptr;
while (rptr < tmp->b_wptr) {
*p++ = *rptr++;
if (++i >= sizeof (xid))
goto done_xid_copy;
}
tmp = tmp->b_cont;
}
/*
* If we got here, we ran out of mblk space before the
* xid could be copied.
*/
ASSERT(tmp == NULL && i < sizeof (xid));
RPCLOG0(1,
"clnt_dispatch_notify(clts): message less than "
"size of xid\n");
freemsg(mp);
return;
}
done_xid_copy:
/*
* Reset the read pointer back to the beginning of the protocol
* header if we moved it.
*/
if (mp->b_rptr != hdr_offset)
mp->b_rptr = hdr_offset;
hash = call_hash(xid, clnt_clts_hash_size);
chtp = &clts_call_ht[hash];
/* call_table_find returns with the hash bucket locked */
call_table_find(chtp, xid, e);
if (e != NULL) {
mutex_enter(&e->call_lock);
/*
* verify that the reply is coming in on
* the same zone that it was sent from.
*/
if (e->call_zoneid != zoneid) {
mutex_exit(&e->call_lock);
mutex_exit(&chtp->ct_lock);
freemsg(mp);
return;
}
/*
* found thread waiting for this reply.
*/
if (e->call_reply) {
RPCLOG(8,
"clnt_dispatch_notify (clts): discarding old "
"reply for xid 0x%x\n",
xid);
freemsg(e->call_reply);
}
e->call_notified = TRUE;
e->call_reply = mp;
e->call_status = RPC_SUCCESS;
cv_signal(&e->call_cv);
mutex_exit(&e->call_lock);
mutex_exit(&chtp->ct_lock);
} else {
zone_t *zone;
struct rpcstat *rpcstat;
mutex_exit(&chtp->ct_lock);
RPCLOG(8, "clnt_dispatch_notify (clts): no caller for reply "
"0x%x\n", xid);
freemsg(mp);
/*
* This is unfortunate, but we need to lookup the zone so we
* can increment its "rcbadxids" counter.
*/
zone = zone_find_by_id(zoneid);
if (zone == NULL) {
/*
* The zone went away...
*/
return;
}
rpcstat = zone_getspecific(rpcstat_zone_key, zone);
if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) {
/*
* Not interested
*/
zone_rele(zone);
return;
}
RCSTAT_INCR(rpcstat->rpc_clts_client, rcbadxids);
zone_rele(zone);
}
}
/*
* Init routine. Called when rpcmod is loaded.
*/
void
clnt_clts_init(void)
{
endpnt_cache = kmem_cache_create("clnt_clts_endpnt_cache",
sizeof (struct endpnt), 0, NULL, NULL, endpnt_repossess, NULL,
NULL, 0);
rw_init(&endpnt_type_lock, NULL, RW_DEFAULT, NULL);
/*
* Perform simple bounds checking to make sure that the setting is
* reasonable
*/
if (clnt_clts_max_endpoints <= 0) {
if (clnt_clts_do_bindresvport)
clnt_clts_max_endpoints = RESERVED_PORTSPACE;
else
clnt_clts_max_endpoints = NONRESERVED_PORTSPACE;
}
if (clnt_clts_do_bindresvport &&
clnt_clts_max_endpoints > RESERVED_PORTSPACE)
clnt_clts_max_endpoints = RESERVED_PORTSPACE;
else if (clnt_clts_max_endpoints > NONRESERVED_PORTSPACE)
clnt_clts_max_endpoints = NONRESERVED_PORTSPACE;
if (clnt_clts_hash_size < DEFAULT_MIN_HASH_SIZE)
clnt_clts_hash_size = DEFAULT_MIN_HASH_SIZE;
/*
* Defer creating the taskq until rpcmod gets pushed. If we are
* in diskless boot mode, rpcmod will get loaded early even before
* thread_create() is available.
*/
endpnt_taskq = NULL;
taskq_created = FALSE;
mutex_init(&endpnt_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
if (clnt_clts_endpoint_reap_interval < DEFAULT_ENDPOINT_REAP_INTERVAL)
clnt_clts_endpoint_reap_interval =
DEFAULT_ENDPOINT_REAP_INTERVAL;
/*
* Dispatch the taskq at an interval which is offset from the
* interval that the endpoints should be reaped.
*/
clnt_clts_taskq_dispatch_interval =
(clnt_clts_endpoint_reap_interval + DEFAULT_INTERVAL_SHIFT) * hz;
/*
* Initialize the completion queue
*/
clts_call_ht = call_table_init(clnt_clts_hash_size);
/*
* Initialize the zone destructor callback.
*/
zone_key_create(&endpnt_destructor_key, NULL, NULL, endpnt_destructor);
}
void
clnt_clts_fini(void)
{
(void) zone_key_delete(endpnt_destructor_key);
}