1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T 28 * All Rights Reserved 29 */ 30 31 /* 32 * Portions of this source code were derived from Berkeley 4.3 BSD 33 * under license from the Regents of the University of California. 34 */ 35 36 37 /* 38 * Implements a kernel based, client side RPC. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/types.h> 43 #include <sys/systm.h> 44 #include <sys/sysmacros.h> 45 #include <sys/stream.h> 46 #include <sys/strsubr.h> 47 #include <sys/ddi.h> 48 #include <sys/tiuser.h> 49 #include <sys/tihdr.h> 50 #include <sys/t_kuser.h> 51 #include <sys/errno.h> 52 #include <sys/kmem.h> 53 #include <sys/debug.h> 54 #include <sys/kstat.h> 55 #include <sys/t_lock.h> 56 #include <sys/cmn_err.h> 57 #include <sys/conf.h> 58 #include <sys/disp.h> 59 #include <sys/taskq.h> 60 #include <sys/list.h> 61 #include <sys/atomic.h> 62 #include <sys/zone.h> 63 #include <netinet/in.h> 64 #include <rpc/types.h> 65 #include <rpc/xdr.h> 66 #include <rpc/auth.h> 67 #include <rpc/clnt.h> 68 #include <rpc/rpc_msg.h> 69 70 #include <sys/sdt.h> 71 72 static enum clnt_stat clnt_clts_kcallit(CLIENT *, rpcproc_t, xdrproc_t, 73 caddr_t, xdrproc_t, caddr_t, struct timeval); 74 static void clnt_clts_kabort(CLIENT *); 75 static void clnt_clts_kerror(CLIENT *, struct rpc_err *); 76 static bool_t clnt_clts_kfreeres(CLIENT *, xdrproc_t, caddr_t); 77 static bool_t clnt_clts_kcontrol(CLIENT *, int, char *); 78 static void clnt_clts_kdestroy(CLIENT *); 79 static int clnt_clts_ksettimers(CLIENT *, struct rpc_timers *, 80 struct rpc_timers *, int, void (*)(), caddr_t, uint32_t); 81 82 /* 83 * Operations vector for CLTS based RPC 84 */ 85 static struct clnt_ops clts_ops = { 86 clnt_clts_kcallit, /* do rpc call */ 87 clnt_clts_kabort, /* abort call */ 88 clnt_clts_kerror, /* return error status */ 89 clnt_clts_kfreeres, /* free results */ 90 clnt_clts_kdestroy, /* destroy rpc handle */ 91 clnt_clts_kcontrol, /* the ioctl() of rpc */ 92 clnt_clts_ksettimers /* set retry timers */ 93 }; 94 95 /* 96 * Endpoint for CLTS (INET, INET6, loopback, etc.) 97 */ 98 typedef struct endpnt_type { 99 struct endpnt_type *e_next; /* pointer to next endpoint type */ 100 list_t e_pool; /* list of available endpoints */ 101 list_t e_ilist; /* list of idle endpoints */ 102 struct endpnt *e_pcurr; /* pointer to current endpoint */ 103 char e_protofmly[KNC_STRSIZE]; /* protocol family */ 104 dev_t e_rdev; /* device */ 105 kmutex_t e_plock; /* pool lock */ 106 kmutex_t e_ilock; /* idle list lock */ 107 timeout_id_t e_itimer; /* timer to dispatch the taskq */ 108 uint_t e_cnt; /* number of endpoints in the pool */ 109 zoneid_t e_zoneid; /* zoneid of endpoint type */ 110 kcondvar_t e_async_cv; /* cv for asynchronous reap threads */ 111 uint_t e_async_count; /* count of asynchronous reap threads */ 112 } endpnt_type_t; 113 114 typedef struct endpnt { 115 list_node_t e_node; /* link to the pool */ 116 list_node_t e_idle; /* link to the idle list */ 117 endpnt_type_t *e_type; /* back pointer to endpoint type */ 118 TIUSER *e_tiptr; /* pointer to transport endpoint */ 119 queue_t *e_wq; /* write queue */ 120 uint_t e_flags; /* endpoint flags */ 121 uint_t e_ref; /* ref count on endpoint */ 122 kcondvar_t e_cv; /* condition variable */ 123 kmutex_t e_lock; /* protects cv and flags */ 124 time_t e_itime; /* time when rele'd */ 125 } endpnt_t; 126 127 #define ENDPNT_ESTABLISHED 0x1 /* endpoint is established */ 128 #define ENDPNT_WAITING 0x2 /* thread waiting for endpoint */ 129 #define ENDPNT_BOUND 0x4 /* endpoint is bound */ 130 #define ENDPNT_STALE 0x8 /* endpoint is dead */ 131 #define ENDPNT_ONIDLE 0x10 /* endpoint is on the idle list */ 132 133 static krwlock_t endpnt_type_lock; /* protects endpnt_type_list */ 134 static endpnt_type_t *endpnt_type_list = NULL; /* list of CLTS endpoints */ 135 static struct kmem_cache *endpnt_cache; /* cache of endpnt_t's */ 136 static taskq_t *endpnt_taskq; /* endpnt_t reaper thread */ 137 static bool_t taskq_created; /* flag for endpnt_taskq */ 138 static kmutex_t endpnt_taskq_lock; /* taskq lock */ 139 static zone_key_t endpnt_destructor_key; 140 141 #define DEFAULT_ENDPOINT_REAP_INTERVAL 60 /* 1 minute */ 142 #define DEFAULT_INTERVAL_SHIFT 30 /* 30 seconds */ 143 144 /* 145 * Endpoint tunables 146 */ 147 static int clnt_clts_max_endpoints = -1; 148 static int clnt_clts_hash_size = DEFAULT_HASH_SIZE; 149 static time_t clnt_clts_endpoint_reap_interval = -1; 150 static clock_t clnt_clts_taskq_dispatch_interval; 151 152 /* 153 * Response completion hash queue 154 */ 155 static call_table_t *clts_call_ht; 156 157 /* 158 * Routines for the endpoint manager 159 */ 160 static struct endpnt_type *endpnt_type_create(struct knetconfig *); 161 static void endpnt_type_free(struct endpnt_type *); 162 static int check_endpnt(struct endpnt *, struct endpnt **); 163 static struct endpnt *endpnt_get(struct knetconfig *, int); 164 static void endpnt_rele(struct endpnt *); 165 static void endpnt_reap_settimer(endpnt_type_t *); 166 static void endpnt_reap(endpnt_type_t *); 167 static void endpnt_reap_dispatch(void *); 168 static void endpnt_reclaim(zoneid_t); 169 170 171 /* 172 * Request dipatching function. 173 */ 174 static int clnt_clts_dispatch_send(queue_t *q, mblk_t *, struct netbuf *addr, 175 calllist_t *, uint_t); 176 177 /* 178 * The size of the preserialized RPC header information. 179 */ 180 #define CKU_HDRSIZE 20 181 /* 182 * The initial allocation size. It is small to reduce space requirements. 183 */ 184 #define CKU_INITSIZE 2048 185 /* 186 * The size of additional allocations, if required. It is larger to 187 * reduce the number of actual allocations. 188 */ 189 #define CKU_ALLOCSIZE 8192 190 191 /* 192 * Private data per rpc handle. This structure is allocated by 193 * clnt_clts_kcreate, and freed by clnt_clts_kdestroy. 194 */ 195 struct cku_private { 196 CLIENT cku_client; /* client handle */ 197 int cku_retrys; /* request retrys */ 198 calllist_t cku_call; 199 struct endpnt *cku_endpnt; /* open end point */ 200 struct knetconfig cku_config; 201 struct netbuf cku_addr; /* remote address */ 202 struct rpc_err cku_err; /* error status */ 203 XDR cku_outxdr; /* xdr stream for output */ 204 XDR cku_inxdr; /* xdr stream for input */ 205 char cku_rpchdr[CKU_HDRSIZE + 4]; /* rpc header */ 206 struct cred *cku_cred; /* credentials */ 207 struct rpc_timers *cku_timers; /* for estimating RTT */ 208 struct rpc_timers *cku_timeall; /* for estimating RTT */ 209 void (*cku_feedback)(int, int, caddr_t); 210 /* ptr to feedback rtn */ 211 caddr_t cku_feedarg; /* argument for feedback func */ 212 uint32_t cku_xid; /* current XID */ 213 bool_t cku_bcast; /* RPC broadcast hint */ 214 int cku_useresvport; /* Use reserved port */ 215 struct rpc_clts_client *cku_stats; /* counters for the zone */ 216 }; 217 218 static const struct rpc_clts_client { 219 kstat_named_t rccalls; 220 kstat_named_t rcbadcalls; 221 kstat_named_t rcretrans; 222 kstat_named_t rcbadxids; 223 kstat_named_t rctimeouts; 224 kstat_named_t rcnewcreds; 225 kstat_named_t rcbadverfs; 226 kstat_named_t rctimers; 227 kstat_named_t rcnomem; 228 kstat_named_t rccantsend; 229 } clts_rcstat_tmpl = { 230 { "calls", KSTAT_DATA_UINT64 }, 231 { "badcalls", KSTAT_DATA_UINT64 }, 232 { "retrans", KSTAT_DATA_UINT64 }, 233 { "badxids", KSTAT_DATA_UINT64 }, 234 { "timeouts", KSTAT_DATA_UINT64 }, 235 { "newcreds", KSTAT_DATA_UINT64 }, 236 { "badverfs", KSTAT_DATA_UINT64 }, 237 { "timers", KSTAT_DATA_UINT64 }, 238 { "nomem", KSTAT_DATA_UINT64 }, 239 { "cantsend", KSTAT_DATA_UINT64 }, 240 }; 241 242 static uint_t clts_rcstat_ndata = 243 sizeof (clts_rcstat_tmpl) / sizeof (kstat_named_t); 244 245 #define RCSTAT_INCR(s, x) \ 246 atomic_add_64(&(s)->x.value.ui64, 1) 247 248 #define ptoh(p) (&((p)->cku_client)) 249 #define htop(h) ((struct cku_private *)((h)->cl_private)) 250 251 /* 252 * Times to retry 253 */ 254 #define SNDTRIES 4 255 #define REFRESHES 2 /* authentication refreshes */ 256 257 /* 258 * The following is used to determine the global default behavior for 259 * CLTS when binding to a local port. 260 * 261 * If the value is set to 1 the default will be to select a reserved 262 * (aka privileged) port, if the value is zero the default will be to 263 * use non-reserved ports. Users of kRPC may override this by using 264 * CLNT_CONTROL() and CLSET_BINDRESVPORT. 265 */ 266 static int clnt_clts_do_bindresvport = 1; 267 268 #define BINDRESVPORT_RETRIES 5 269 270 void 271 clnt_clts_stats_init(zoneid_t zoneid, struct rpc_clts_client **statsp) 272 { 273 kstat_t *ksp; 274 kstat_named_t *knp; 275 276 knp = rpcstat_zone_init_common(zoneid, "unix", "rpc_clts_client", 277 (const kstat_named_t *)&clts_rcstat_tmpl, 278 sizeof (clts_rcstat_tmpl)); 279 /* 280 * Backwards compatibility for old kstat clients 281 */ 282 ksp = kstat_create_zone("unix", 0, "rpc_client", "rpc", 283 KSTAT_TYPE_NAMED, clts_rcstat_ndata, 284 KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid); 285 if (ksp) { 286 ksp->ks_data = knp; 287 kstat_install(ksp); 288 } 289 *statsp = (struct rpc_clts_client *)knp; 290 } 291 292 void 293 clnt_clts_stats_fini(zoneid_t zoneid, struct rpc_clts_client **statsp) 294 { 295 rpcstat_zone_fini_common(zoneid, "unix", "rpc_clts_client"); 296 kstat_delete_byname_zone("unix", 0, "rpc_client", zoneid); 297 kmem_free(*statsp, sizeof (clts_rcstat_tmpl)); 298 } 299 300 /* 301 * Create an rpc handle for a clts rpc connection. 302 * Allocates space for the handle structure and the private data. 303 */ 304 /* ARGSUSED */ 305 int 306 clnt_clts_kcreate(struct knetconfig *config, struct netbuf *addr, 307 rpcprog_t pgm, rpcvers_t vers, int retrys, struct cred *cred, 308 CLIENT **cl) 309 { 310 CLIENT *h; 311 struct cku_private *p; 312 struct rpc_msg call_msg; 313 int error; 314 int plen; 315 316 if (cl == NULL) 317 return (EINVAL); 318 319 *cl = NULL; 320 error = 0; 321 322 p = kmem_zalloc(sizeof (*p), KM_SLEEP); 323 324 h = ptoh(p); 325 326 /* handle */ 327 h->cl_ops = &clts_ops; 328 h->cl_private = (caddr_t)p; 329 h->cl_auth = authkern_create(); 330 331 /* call message, just used to pre-serialize below */ 332 call_msg.rm_xid = 0; 333 call_msg.rm_direction = CALL; 334 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 335 call_msg.rm_call.cb_prog = pgm; 336 call_msg.rm_call.cb_vers = vers; 337 338 /* private */ 339 clnt_clts_kinit(h, addr, retrys, cred); 340 341 xdrmem_create(&p->cku_outxdr, p->cku_rpchdr, CKU_HDRSIZE, XDR_ENCODE); 342 343 /* pre-serialize call message header */ 344 if (!xdr_callhdr(&p->cku_outxdr, &call_msg)) { 345 error = EINVAL; /* XXX */ 346 goto bad; 347 } 348 349 p->cku_config.knc_rdev = config->knc_rdev; 350 p->cku_config.knc_semantics = config->knc_semantics; 351 plen = strlen(config->knc_protofmly) + 1; 352 p->cku_config.knc_protofmly = kmem_alloc(plen, KM_SLEEP); 353 bcopy(config->knc_protofmly, p->cku_config.knc_protofmly, plen); 354 p->cku_useresvport = -1; /* value is has not been set */ 355 356 cv_init(&p->cku_call.call_cv, NULL, CV_DEFAULT, NULL); 357 mutex_init(&p->cku_call.call_lock, NULL, MUTEX_DEFAULT, NULL); 358 359 *cl = h; 360 return (0); 361 362 bad: 363 auth_destroy(h->cl_auth); 364 kmem_free(p->cku_addr.buf, addr->maxlen); 365 kmem_free(p, sizeof (struct cku_private)); 366 367 return (error); 368 } 369 370 void 371 clnt_clts_kinit(CLIENT *h, struct netbuf *addr, int retrys, cred_t *cred) 372 { 373 /* LINTED pointer alignment */ 374 struct cku_private *p = htop(h); 375 struct rpcstat *rsp; 376 377 rsp = zone_getspecific(rpcstat_zone_key, rpc_zone()); 378 ASSERT(rsp != NULL); 379 380 p->cku_retrys = retrys; 381 382 if (p->cku_addr.maxlen < addr->len) { 383 if (p->cku_addr.maxlen != 0 && p->cku_addr.buf != NULL) 384 kmem_free(p->cku_addr.buf, p->cku_addr.maxlen); 385 386 p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP); 387 p->cku_addr.maxlen = addr->maxlen; 388 } 389 390 p->cku_addr.len = addr->len; 391 bcopy(addr->buf, p->cku_addr.buf, addr->len); 392 393 p->cku_cred = cred; 394 p->cku_xid = 0; 395 p->cku_timers = NULL; 396 p->cku_timeall = NULL; 397 p->cku_feedback = NULL; 398 p->cku_bcast = FALSE; 399 p->cku_call.call_xid = 0; 400 p->cku_call.call_hash = 0; 401 p->cku_call.call_notified = FALSE; 402 p->cku_call.call_next = NULL; 403 p->cku_call.call_prev = NULL; 404 p->cku_call.call_reply = NULL; 405 p->cku_call.call_wq = NULL; 406 p->cku_stats = rsp->rpc_clts_client; 407 } 408 409 /* 410 * set the timers. Return current retransmission timeout. 411 */ 412 static int 413 clnt_clts_ksettimers(CLIENT *h, struct rpc_timers *t, struct rpc_timers *all, 414 int minimum, void (*feedback)(int, int, caddr_t), caddr_t arg, 415 uint32_t xid) 416 { 417 /* LINTED pointer alignment */ 418 struct cku_private *p = htop(h); 419 int value; 420 421 p->cku_feedback = feedback; 422 p->cku_feedarg = arg; 423 p->cku_timers = t; 424 p->cku_timeall = all; 425 if (xid) 426 p->cku_xid = xid; 427 value = all->rt_rtxcur; 428 value += t->rt_rtxcur; 429 if (value < minimum) 430 return (minimum); 431 RCSTAT_INCR(p->cku_stats, rctimers); 432 return (value); 433 } 434 435 /* 436 * Time out back off function. tim is in HZ 437 */ 438 #define MAXTIMO (20 * hz) 439 #define backoff(tim) (((tim) < MAXTIMO) ? dobackoff(tim) : (tim)) 440 #define dobackoff(tim) ((((tim) << 1) > MAXTIMO) ? MAXTIMO : ((tim) << 1)) 441 442 #define RETRY_POLL_TIMO 30 443 444 /* 445 * Call remote procedure. 446 * Most of the work of rpc is done here. We serialize what is left 447 * of the header (some was pre-serialized in the handle), serialize 448 * the arguments, and send it off. We wait for a reply or a time out. 449 * Timeout causes an immediate return, other packet problems may cause 450 * a retry on the receive. When a good packet is received we deserialize 451 * it, and check verification. A bad reply code will cause one retry 452 * with full (longhand) credentials. 453 */ 454 enum clnt_stat 455 clnt_clts_kcallit_addr(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args, 456 caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp, 457 struct timeval wait, struct netbuf *sin) 458 { 459 /* LINTED pointer alignment */ 460 struct cku_private *p = htop(h); 461 XDR *xdrs; 462 int stries = p->cku_retrys; 463 int refreshes = REFRESHES; /* number of times to refresh cred */ 464 int round_trip; /* time the RPC */ 465 int error; 466 int hdrsz; 467 mblk_t *mp; 468 mblk_t *mpdup; 469 mblk_t *resp = NULL; 470 mblk_t *tmp; 471 calllist_t *call = &p->cku_call; 472 clock_t ori_timout, timout; 473 bool_t interrupted; 474 enum clnt_stat status; 475 struct rpc_msg reply_msg; 476 enum clnt_stat re_status; 477 endpnt_t *endpt; 478 479 RCSTAT_INCR(p->cku_stats, rccalls); 480 481 RPCLOG(2, "clnt_clts_kcallit_addr: wait.tv_sec: %ld\n", wait.tv_sec); 482 RPCLOG(2, "clnt_clts_kcallit_addr: wait.tv_usec: %ld\n", wait.tv_usec); 483 484 timout = TIMEVAL_TO_TICK(&wait); 485 ori_timout = timout; 486 487 if (p->cku_xid == 0) { 488 p->cku_xid = alloc_xid(); 489 if (p->cku_endpnt != NULL) 490 endpnt_rele(p->cku_endpnt); 491 p->cku_endpnt = NULL; 492 } 493 call->call_zoneid = rpc_zoneid(); 494 495 mpdup = NULL; 496 call_again: 497 498 if (mpdup == NULL) { 499 500 while ((mp = allocb(CKU_INITSIZE, BPRI_LO)) == NULL) { 501 if (strwaitbuf(CKU_INITSIZE, BPRI_LO)) { 502 p->cku_err.re_status = RPC_SYSTEMERROR; 503 p->cku_err.re_errno = ENOSR; 504 goto done; 505 } 506 } 507 508 xdrs = &p->cku_outxdr; 509 xdrmblk_init(xdrs, mp, XDR_ENCODE, CKU_ALLOCSIZE); 510 511 if (h->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { 512 /* 513 * Copy in the preserialized RPC header 514 * information. 515 */ 516 bcopy(p->cku_rpchdr, mp->b_rptr, CKU_HDRSIZE); 517 518 /* 519 * transaction id is the 1st thing in the output 520 * buffer. 521 */ 522 /* LINTED pointer alignment */ 523 (*(uint32_t *)(mp->b_rptr)) = p->cku_xid; 524 525 /* Skip the preserialized stuff. */ 526 XDR_SETPOS(xdrs, CKU_HDRSIZE); 527 528 /* Serialize dynamic stuff into the output buffer. */ 529 if ((!XDR_PUTINT32(xdrs, (int32_t *)&procnum)) || 530 (!AUTH_MARSHALL(h->cl_auth, xdrs, p->cku_cred)) || 531 (!(*xdr_args)(xdrs, argsp))) { 532 freemsg(mp); 533 p->cku_err.re_status = RPC_CANTENCODEARGS; 534 p->cku_err.re_errno = EIO; 535 goto done; 536 } 537 } else { 538 uint32_t *uproc = (uint32_t *) 539 &p->cku_rpchdr[CKU_HDRSIZE]; 540 IXDR_PUT_U_INT32(uproc, procnum); 541 542 (*(uint32_t *)(&p->cku_rpchdr[0])) = p->cku_xid; 543 XDR_SETPOS(xdrs, 0); 544 545 /* Serialize the procedure number and the arguments. */ 546 if (!AUTH_WRAP(h->cl_auth, (caddr_t)p->cku_rpchdr, 547 CKU_HDRSIZE+4, xdrs, xdr_args, argsp)) { 548 freemsg(mp); 549 p->cku_err.re_status = RPC_CANTENCODEARGS; 550 p->cku_err.re_errno = EIO; 551 goto done; 552 } 553 } 554 } else 555 mp = mpdup; 556 557 mpdup = dupmsg(mp); 558 if (mpdup == NULL) { 559 freemsg(mp); 560 p->cku_err.re_status = RPC_SYSTEMERROR; 561 p->cku_err.re_errno = ENOSR; 562 goto done; 563 } 564 565 /* 566 * Grab an endpnt only if the endpoint is NULL. We could be retrying 567 * the request and in this case we want to go through the same 568 * source port, so that the duplicate request cache may detect a 569 * retry. 570 */ 571 572 if (p->cku_endpnt == NULL) 573 p->cku_endpnt = endpnt_get(&p->cku_config, p->cku_useresvport); 574 575 if (p->cku_endpnt == NULL) { 576 freemsg(mp); 577 p->cku_err.re_status = RPC_SYSTEMERROR; 578 p->cku_err.re_errno = ENOSR; 579 goto done; 580 } 581 582 round_trip = lbolt; 583 584 error = clnt_clts_dispatch_send(p->cku_endpnt->e_wq, mp, 585 &p->cku_addr, call, p->cku_xid); 586 587 if (error != 0) { 588 freemsg(mp); 589 p->cku_err.re_status = RPC_CANTSEND; 590 p->cku_err.re_errno = error; 591 RCSTAT_INCR(p->cku_stats, rccantsend); 592 goto done1; 593 } 594 595 RPCLOG(64, "clnt_clts_kcallit_addr: sent call for xid 0x%x\n", 596 p->cku_xid); 597 598 /* 599 * There are two reasons for which we go back to to tryread. 600 * 601 * a) In case the status is RPC_PROCUNAVAIL and we sent out a 602 * broadcast we should not get any invalid messages with the 603 * RPC_PROCUNAVAIL error back. Some broken RPC implementations 604 * send them and for this we have to ignore them ( as we would 605 * have never received them ) and look for another message 606 * which might contain the valid response because we don't know 607 * how many broken implementations are in the network. So we are 608 * going to loop until 609 * - we received a valid response 610 * - we have processed all invalid responses and 611 * got a time out when we try to receive again a 612 * message. 613 * 614 * b) We will jump back to tryread also in case we failed 615 * within the AUTH_VALIDATE. In this case we should move 616 * on and loop until we received a valid response or we 617 * have processed all responses with broken authentication 618 * and we got a time out when we try to receive a message. 619 */ 620 tryread: 621 mutex_enter(&call->call_lock); 622 interrupted = FALSE; 623 if (call->call_notified == FALSE) { 624 klwp_t *lwp = ttolwp(curthread); 625 clock_t cv_wait_ret = 1; /* init to > 0 */ 626 clock_t cv_timout = timout; 627 628 if (lwp != NULL) 629 lwp->lwp_nostop++; 630 631 cv_timout += lbolt; 632 633 if (h->cl_nosignal) 634 while ((cv_wait_ret = 635 cv_timedwait(&call->call_cv, 636 &call->call_lock, cv_timout)) > 0 && 637 call->call_notified == FALSE) 638 ; 639 else 640 while ((cv_wait_ret = 641 cv_timedwait_sig(&call->call_cv, 642 &call->call_lock, cv_timout)) > 0 && 643 call->call_notified == FALSE) 644 ; 645 646 if (cv_wait_ret == 0) 647 interrupted = TRUE; 648 649 if (lwp != NULL) 650 lwp->lwp_nostop--; 651 } 652 resp = call->call_reply; 653 call->call_reply = NULL; 654 status = call->call_status; 655 /* 656 * We have to reset the call_notified here. In case we have 657 * to do a retry ( e.g. in case we got a RPC_PROCUNAVAIL 658 * error ) we need to set this to false to ensure that 659 * we will wait for the next message. When the next message 660 * is going to arrive the function clnt_clts_dispatch_notify 661 * will set this to true again. 662 */ 663 call->call_notified = FALSE; 664 mutex_exit(&call->call_lock); 665 666 if (status == RPC_TIMEDOUT) { 667 if (interrupted) { 668 /* 669 * We got interrupted, bail out 670 */ 671 p->cku_err.re_status = RPC_INTR; 672 p->cku_err.re_errno = EINTR; 673 goto done1; 674 } else { 675 /* 676 * It's possible that our response arrived 677 * right after we timed out. Check to see 678 * if it has arrived before we remove the 679 * calllist from the dispatch queue. 680 */ 681 mutex_enter(&call->call_lock); 682 if (call->call_notified == TRUE) { 683 resp = call->call_reply; 684 call->call_reply = NULL; 685 mutex_exit(&call->call_lock); 686 RPCLOG(8, "clnt_clts_kcallit_addr: " 687 "response received for request " 688 "w/xid 0x%x after timeout\n", 689 p->cku_xid); 690 goto getresponse; 691 } 692 mutex_exit(&call->call_lock); 693 694 RPCLOG(8, "clnt_clts_kcallit_addr: " 695 "request w/xid 0x%x timedout " 696 "waiting for reply\n", p->cku_xid); 697 #if 0 /* XXX not yet */ 698 /* 699 * Timeout may be due to a dead gateway. Send 700 * an ioctl downstream advising deletion of 701 * route when we reach the half-way point to 702 * timing out. 703 */ 704 if (stries == p->cku_retrys/2) { 705 t_kadvise(p->cku_endpnt->e_tiptr, 706 (uchar_t *)p->cku_addr.buf, 707 p->cku_addr.len); 708 } 709 #endif /* not yet */ 710 p->cku_err.re_status = RPC_TIMEDOUT; 711 p->cku_err.re_errno = ETIMEDOUT; 712 RCSTAT_INCR(p->cku_stats, rctimeouts); 713 goto done1; 714 } 715 } 716 717 getresponse: 718 /* 719 * Check to see if a response arrived. If it one is 720 * present then proceed to process the reponse. Otherwise 721 * fall through to retry or retransmit the request. This 722 * is probably not the optimal thing to do, but since we 723 * are most likely dealing with a unrealiable transport it 724 * is the safe thing to so. 725 */ 726 if (resp == NULL) { 727 p->cku_err.re_status = RPC_CANTRECV; 728 p->cku_err.re_errno = EIO; 729 goto done1; 730 } 731 732 /* 733 * Prepare the message for further processing. We need to remove 734 * the datagram header and copy the source address if necessary. No 735 * need to verify the header since rpcmod took care of that. 736 */ 737 /* 738 * Copy the source address if the caller has supplied a netbuf. 739 */ 740 if (sin != NULL) { 741 union T_primitives *pptr; 742 743 pptr = (union T_primitives *)resp->b_rptr; 744 bcopy(resp->b_rptr + pptr->unitdata_ind.SRC_offset, sin->buf, 745 pptr->unitdata_ind.SRC_length); 746 sin->len = pptr->unitdata_ind.SRC_length; 747 } 748 749 /* 750 * Pop off the datagram header. 751 */ 752 hdrsz = resp->b_wptr - resp->b_rptr; 753 if ((resp->b_wptr - (resp->b_rptr + hdrsz)) == 0) { 754 tmp = resp; 755 resp = resp->b_cont; 756 tmp->b_cont = NULL; 757 freeb(tmp); 758 } else { 759 unsigned char *ud_off = resp->b_rptr; 760 resp->b_rptr += hdrsz; 761 tmp = dupb(resp); 762 if (tmp == NULL) { 763 p->cku_err.re_status = RPC_SYSTEMERROR; 764 p->cku_err.re_errno = ENOSR; 765 freemsg(resp); 766 goto done1; 767 } 768 tmp->b_cont = resp->b_cont; 769 resp->b_rptr = ud_off; 770 freeb(resp); 771 resp = tmp; 772 } 773 774 round_trip = lbolt - round_trip; 775 /* 776 * Van Jacobson timer algorithm here, only if NOT a retransmission. 777 */ 778 if (p->cku_timers != NULL && stries == p->cku_retrys) { 779 int rt; 780 781 rt = round_trip; 782 rt -= (p->cku_timers->rt_srtt >> 3); 783 p->cku_timers->rt_srtt += rt; 784 if (rt < 0) 785 rt = - rt; 786 rt -= (p->cku_timers->rt_deviate >> 2); 787 p->cku_timers->rt_deviate += rt; 788 p->cku_timers->rt_rtxcur = 789 (clock_t)((p->cku_timers->rt_srtt >> 2) + 790 p->cku_timers->rt_deviate) >> 1; 791 792 rt = round_trip; 793 rt -= (p->cku_timeall->rt_srtt >> 3); 794 p->cku_timeall->rt_srtt += rt; 795 if (rt < 0) 796 rt = - rt; 797 rt -= (p->cku_timeall->rt_deviate >> 2); 798 p->cku_timeall->rt_deviate += rt; 799 p->cku_timeall->rt_rtxcur = 800 (clock_t)((p->cku_timeall->rt_srtt >> 2) + 801 p->cku_timeall->rt_deviate) >> 1; 802 if (p->cku_feedback != NULL) { 803 (*p->cku_feedback)(FEEDBACK_OK, procnum, 804 p->cku_feedarg); 805 } 806 } 807 808 /* 809 * Process reply 810 */ 811 xdrs = &(p->cku_inxdr); 812 xdrmblk_init(xdrs, resp, XDR_DECODE, 0); 813 814 reply_msg.rm_direction = REPLY; 815 reply_msg.rm_reply.rp_stat = MSG_ACCEPTED; 816 reply_msg.acpted_rply.ar_stat = SUCCESS; 817 reply_msg.acpted_rply.ar_verf = _null_auth; 818 /* 819 * xdr_results will be done in AUTH_UNWRAP. 820 */ 821 reply_msg.acpted_rply.ar_results.where = NULL; 822 reply_msg.acpted_rply.ar_results.proc = xdr_void; 823 824 /* 825 * Decode and validate the response. 826 */ 827 if (!xdr_replymsg(xdrs, &reply_msg)) { 828 p->cku_err.re_status = RPC_CANTDECODERES; 829 p->cku_err.re_errno = EIO; 830 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 831 goto done1; 832 } 833 834 _seterr_reply(&reply_msg, &(p->cku_err)); 835 836 re_status = p->cku_err.re_status; 837 if (re_status == RPC_SUCCESS) { 838 /* 839 * Reply is good, check auth. 840 */ 841 if (!AUTH_VALIDATE(h->cl_auth, 842 &reply_msg.acpted_rply.ar_verf)) { 843 p->cku_err.re_status = RPC_AUTHERROR; 844 p->cku_err.re_why = AUTH_INVALIDRESP; 845 RCSTAT_INCR(p->cku_stats, rcbadverfs); 846 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 847 goto tryread; 848 } 849 if (!AUTH_UNWRAP(h->cl_auth, xdrs, xdr_results, resultsp)) { 850 p->cku_err.re_status = RPC_CANTDECODERES; 851 p->cku_err.re_errno = EIO; 852 } 853 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 854 goto done1; 855 } 856 /* set errno in case we can't recover */ 857 if (re_status != RPC_VERSMISMATCH && 858 re_status != RPC_AUTHERROR && re_status != RPC_PROGVERSMISMATCH) 859 p->cku_err.re_errno = EIO; 860 /* 861 * Determine whether or not we're doing an RPC 862 * broadcast. Some server implementations don't 863 * follow RFC 1050, section 7.4.2 in that they 864 * don't remain silent when they see a proc 865 * they don't support. Therefore we keep trying 866 * to receive on RPC_PROCUNAVAIL, hoping to get 867 * a valid response from a compliant server. 868 */ 869 if (re_status == RPC_PROCUNAVAIL && p->cku_bcast) { 870 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 871 goto tryread; 872 } 873 if (re_status == RPC_AUTHERROR) { 874 /* 875 * Maybe our credential need to be refreshed 876 */ 877 if (refreshes > 0 && 878 AUTH_REFRESH(h->cl_auth, &reply_msg, p->cku_cred)) { 879 /* 880 * The credential is refreshed. Try the request again. 881 * Even if stries == 0, we still retry as long as 882 * refreshes > 0. This prevents a soft authentication 883 * error turning into a hard one at an upper level. 884 */ 885 refreshes--; 886 RCSTAT_INCR(p->cku_stats, rcbadcalls); 887 RCSTAT_INCR(p->cku_stats, rcnewcreds); 888 889 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 890 freemsg(mpdup); 891 call_table_remove(call); 892 mutex_enter(&call->call_lock); 893 if (call->call_reply != NULL) { 894 freemsg(call->call_reply); 895 call->call_reply = NULL; 896 } 897 mutex_exit(&call->call_lock); 898 899 freemsg(resp); 900 mpdup = NULL; 901 goto call_again; 902 } 903 /* 904 * We have used the client handle to do an AUTH_REFRESH 905 * and the RPC status may be set to RPC_SUCCESS; 906 * Let's make sure to set it to RPC_AUTHERROR. 907 */ 908 p->cku_err.re_status = RPC_CANTDECODERES; 909 910 /* 911 * Map recoverable and unrecoverable 912 * authentication errors to appropriate errno 913 */ 914 switch (p->cku_err.re_why) { 915 case AUTH_TOOWEAK: 916 /* 917 * Could be an nfsportmon failure, set 918 * useresvport and try again. 919 */ 920 if (p->cku_useresvport != 1) { 921 p->cku_useresvport = 1; 922 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 923 freemsg(mpdup); 924 925 call_table_remove(call); 926 mutex_enter(&call->call_lock); 927 if (call->call_reply != NULL) { 928 freemsg(call->call_reply); 929 call->call_reply = NULL; 930 } 931 mutex_exit(&call->call_lock); 932 933 freemsg(resp); 934 mpdup = NULL; 935 endpt = p->cku_endpnt; 936 if (endpt->e_tiptr != NULL) { 937 mutex_enter(&endpt->e_lock); 938 endpt->e_flags &= ~ENDPNT_BOUND; 939 (void) t_kclose(endpt->e_tiptr, 1); 940 endpt->e_tiptr = NULL; 941 mutex_exit(&endpt->e_lock); 942 943 } 944 945 p->cku_xid = alloc_xid(); 946 endpnt_rele(p->cku_endpnt); 947 p->cku_endpnt = NULL; 948 goto call_again; 949 } 950 /* FALLTHRU */ 951 case AUTH_BADCRED: 952 case AUTH_BADVERF: 953 case AUTH_INVALIDRESP: 954 case AUTH_FAILED: 955 case RPCSEC_GSS_NOCRED: 956 case RPCSEC_GSS_FAILED: 957 p->cku_err.re_errno = EACCES; 958 break; 959 case AUTH_REJECTEDCRED: 960 case AUTH_REJECTEDVERF: 961 default: 962 p->cku_err.re_errno = EIO; 963 break; 964 } 965 RPCLOG(1, "clnt_clts_kcallit : authentication failed " 966 "with RPC_AUTHERROR of type %d\n", 967 p->cku_err.re_why); 968 } 969 970 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 971 972 done1: 973 call_table_remove(call); 974 mutex_enter(&call->call_lock); 975 if (call->call_reply != NULL) { 976 freemsg(call->call_reply); 977 call->call_reply = NULL; 978 } 979 mutex_exit(&call->call_lock); 980 RPCLOG(64, "clnt_clts_kcallit_addr: xid 0x%x taken off dispatch list", 981 p->cku_xid); 982 983 done: 984 if (resp != NULL) { 985 freemsg(resp); 986 resp = NULL; 987 } 988 989 if ((p->cku_err.re_status != RPC_SUCCESS) && 990 (p->cku_err.re_status != RPC_INTR) && 991 (p->cku_err.re_status != RPC_UDERROR) && 992 !IS_UNRECOVERABLE_RPC(p->cku_err.re_status)) { 993 if (p->cku_feedback != NULL && stries == p->cku_retrys) { 994 (*p->cku_feedback)(FEEDBACK_REXMIT1, procnum, 995 p->cku_feedarg); 996 } 997 998 timout = backoff(timout); 999 if (p->cku_timeall != (struct rpc_timers *)0) 1000 p->cku_timeall->rt_rtxcur = timout; 1001 1002 if (p->cku_err.re_status == RPC_SYSTEMERROR || 1003 p->cku_err.re_status == RPC_CANTSEND) { 1004 /* 1005 * Errors due to lack of resources, wait a bit 1006 * and try again. 1007 */ 1008 (void) delay(hz/10); 1009 /* (void) sleep((caddr_t)&lbolt, PZERO-4); */ 1010 } 1011 if (stries-- > 0) { 1012 RCSTAT_INCR(p->cku_stats, rcretrans); 1013 goto call_again; 1014 } 1015 } 1016 1017 if (mpdup != NULL) 1018 freemsg(mpdup); 1019 1020 if (p->cku_err.re_status != RPC_SUCCESS) { 1021 RCSTAT_INCR(p->cku_stats, rcbadcalls); 1022 } 1023 1024 /* 1025 * Allow the endpoint to be held by the client handle in case this 1026 * RPC was not successful. A retry may occur at a higher level and 1027 * in this case we may want to send the request over the same 1028 * source port. 1029 * Endpoint is also released for one-way RPC: no reply, nor retransmit 1030 * is expected. 1031 */ 1032 if ((p->cku_err.re_status == RPC_SUCCESS || 1033 (p->cku_err.re_status == RPC_TIMEDOUT && ori_timout == 0)) && 1034 p->cku_endpnt != NULL) { 1035 endpnt_rele(p->cku_endpnt); 1036 p->cku_endpnt = NULL; 1037 } else { 1038 DTRACE_PROBE2(clnt_clts_kcallit_done, int, p->cku_err.re_status, 1039 struct endpnt *, p->cku_endpnt); 1040 } 1041 1042 return (p->cku_err.re_status); 1043 } 1044 1045 static enum clnt_stat 1046 clnt_clts_kcallit(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args, 1047 caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp, 1048 struct timeval wait) 1049 { 1050 return (clnt_clts_kcallit_addr(h, procnum, xdr_args, argsp, 1051 xdr_results, resultsp, wait, NULL)); 1052 } 1053 1054 /* 1055 * Return error info on this handle. 1056 */ 1057 static void 1058 clnt_clts_kerror(CLIENT *h, struct rpc_err *err) 1059 { 1060 /* LINTED pointer alignment */ 1061 struct cku_private *p = htop(h); 1062 1063 *err = p->cku_err; 1064 } 1065 1066 static bool_t 1067 clnt_clts_kfreeres(CLIENT *h, xdrproc_t xdr_res, caddr_t res_ptr) 1068 { 1069 /* LINTED pointer alignment */ 1070 struct cku_private *p = htop(h); 1071 XDR *xdrs; 1072 1073 xdrs = &(p->cku_outxdr); 1074 xdrs->x_op = XDR_FREE; 1075 return ((*xdr_res)(xdrs, res_ptr)); 1076 } 1077 1078 /*ARGSUSED*/ 1079 static void 1080 clnt_clts_kabort(CLIENT *h) 1081 { 1082 } 1083 1084 static bool_t 1085 clnt_clts_kcontrol(CLIENT *h, int cmd, char *arg) 1086 { 1087 /* LINTED pointer alignment */ 1088 struct cku_private *p = htop(h); 1089 1090 switch (cmd) { 1091 case CLSET_XID: 1092 p->cku_xid = *((uint32_t *)arg); 1093 return (TRUE); 1094 1095 case CLGET_XID: 1096 *((uint32_t *)arg) = p->cku_xid; 1097 return (TRUE); 1098 1099 case CLSET_BCAST: 1100 p->cku_bcast = *((uint32_t *)arg); 1101 return (TRUE); 1102 1103 case CLGET_BCAST: 1104 *((uint32_t *)arg) = p->cku_bcast; 1105 return (TRUE); 1106 case CLSET_BINDRESVPORT: 1107 if (arg == NULL) 1108 return (FALSE); 1109 1110 if (*(int *)arg != 1 && *(int *)arg != 0) 1111 return (FALSE); 1112 1113 p->cku_useresvport = *(int *)arg; 1114 1115 return (TRUE); 1116 1117 case CLGET_BINDRESVPORT: 1118 if (arg == NULL) 1119 return (FALSE); 1120 1121 *(int *)arg = p->cku_useresvport; 1122 1123 return (TRUE); 1124 1125 default: 1126 return (FALSE); 1127 } 1128 } 1129 1130 /* 1131 * Destroy rpc handle. 1132 * Frees the space used for output buffer, private data, and handle 1133 * structure, and the file pointer/TLI data on last reference. 1134 */ 1135 static void 1136 clnt_clts_kdestroy(CLIENT *h) 1137 { 1138 /* LINTED pointer alignment */ 1139 struct cku_private *p = htop(h); 1140 calllist_t *call = &p->cku_call; 1141 1142 int plen; 1143 1144 RPCLOG(8, "clnt_clts_kdestroy h: %p\n", (void *)h); 1145 RPCLOG(8, "clnt_clts_kdestroy h: xid=0x%x\n", p->cku_xid); 1146 1147 if (p->cku_endpnt != NULL) 1148 endpnt_rele(p->cku_endpnt); 1149 1150 cv_destroy(&call->call_cv); 1151 mutex_destroy(&call->call_lock); 1152 1153 plen = strlen(p->cku_config.knc_protofmly) + 1; 1154 kmem_free(p->cku_config.knc_protofmly, plen); 1155 kmem_free(p->cku_addr.buf, p->cku_addr.maxlen); 1156 kmem_free(p, sizeof (*p)); 1157 } 1158 1159 /* 1160 * The connectionless (CLTS) kRPC endpoint management subsystem. 1161 * 1162 * Because endpoints are potentially shared among threads making RPC calls, 1163 * they are managed in a pool according to type (endpnt_type_t). Each 1164 * endpnt_type_t points to a list of usable endpoints through the e_pool 1165 * field, which is of type list_t. list_t is a doubly-linked list. 1166 * The number of endpoints in the pool is stored in the e_cnt field of 1167 * endpnt_type_t and the endpoints are reference counted using the e_ref field 1168 * in the endpnt_t structure. 1169 * 1170 * As an optimization, endpoints that have no references are also linked 1171 * to an idle list via e_ilist which is also of type list_t. When a thread 1172 * calls endpnt_get() to obtain a transport endpoint, the idle list is first 1173 * consulted and if such an endpoint exists, it is removed from the idle list 1174 * and returned to the caller. 1175 * 1176 * If the idle list is empty, then a check is made to see if more endpoints 1177 * can be created. If so, we proceed and create a new endpoint which is added 1178 * to the pool and returned to the caller. If we have reached the limit and 1179 * cannot make a new endpoint then one is returned to the caller via round- 1180 * robin policy. 1181 * 1182 * When an endpoint is placed on the idle list by a thread calling 1183 * endpnt_rele(), it is timestamped and then a reaper taskq is scheduled to 1184 * be dispatched if one hasn't already been. When the timer fires, the 1185 * taskq traverses the idle list and checks to see which endpoints are 1186 * eligible to be closed. It determines this by checking if the timestamp 1187 * when the endpoint was released has exceeded the the threshold for how long 1188 * it should stay alive. 1189 * 1190 * endpnt_t structures remain persistent until the memory reclaim callback, 1191 * endpnt_reclaim(), is invoked. 1192 * 1193 * Here is an example of how the data structures would be laid out by the 1194 * subsystem: 1195 * 1196 * endpnt_type_t 1197 * 1198 * loopback inet 1199 * _______________ ______________ 1200 * | e_next |----------------------->| e_next |---->> 1201 * | e_pool |<---+ | e_pool |<----+ 1202 * | e_ilist |<---+--+ | e_ilist |<----+--+ 1203 * +->| e_pcurr |----+--+--+ +->| e_pcurr |-----+--+--+ 1204 * | | ... | | | | | | ... | | | | 1205 * | | e_itimer (90) | | | | | | e_itimer (0) | | | | 1206 * | | e_cnt (1) | | | | | | e_cnt (3) | | | | 1207 * | +---------------+ | | | | +--------------+ | | | 1208 * | | | | | | | | 1209 * | endpnt_t | | | | | | | 1210 * | ____________ | | | | ____________ | | | 1211 * | | e_node |<------+ | | | | e_node |<------+ | | 1212 * | | e_idle |<---------+ | | | e_idle | | | | 1213 * +--| e_type |<------------+ +--| e_type | | | | 1214 * | e_tiptr | | | e_tiptr | | | | 1215 * | ... | | | ... | | | | 1216 * | e_lock | | | e_lock | | | | 1217 * | ... | | | ... | | | | 1218 * | e_ref (0) | | | e_ref (2) | | | | 1219 * | e_itime | | | e_itime | | | | 1220 * +------------+ | +------------+ | | | 1221 * | | | | 1222 * | | | | 1223 * | ____________ | | | 1224 * | | e_node |<------+ | | 1225 * | | e_idle |<------+--+ | 1226 * +--| e_type | | | 1227 * | | e_tiptr | | | 1228 * | | ... | | | 1229 * | | e_lock | | | 1230 * | | ... | | | 1231 * | | e_ref (0) | | | 1232 * | | e_itime | | | 1233 * | +------------+ | | 1234 * | | | 1235 * | | | 1236 * | ____________ | | 1237 * | | e_node |<------+ | 1238 * | | e_idle | | 1239 * +--| e_type |<------------+ 1240 * | e_tiptr | 1241 * | ... | 1242 * | e_lock | 1243 * | ... | 1244 * | e_ref (1) | 1245 * | e_itime | 1246 * +------------+ 1247 * 1248 * Endpoint locking strategy: 1249 * 1250 * The following functions manipulate lists which hold the endpoint and the 1251 * endpoints themselves: 1252 * 1253 * endpnt_get()/check_endpnt()/endpnt_rele()/endpnt_reap()/do_endpnt_reclaim() 1254 * 1255 * Lock description follows: 1256 * 1257 * endpnt_type_lock: Global reader/writer lock which protects accesses to the 1258 * endpnt_type_list. 1259 * 1260 * e_plock: Lock defined in the endpnt_type_t. It is intended to 1261 * protect accesses to the pool of endopints (e_pool) for a given 1262 * endpnt_type_t. 1263 * 1264 * e_ilock: Lock defined in endpnt_type_t. It is intended to protect accesses 1265 * to the idle list (e_ilist) of available endpoints for a given 1266 * endpnt_type_t. It also protects access to the e_itimer, e_async_cv, 1267 * and e_async_count fields in endpnt_type_t. 1268 * 1269 * e_lock: Lock defined in the endpnt structure. It is intended to protect 1270 * flags, cv, and ref count. 1271 * 1272 * The order goes as follows so as not to induce deadlock. 1273 * 1274 * endpnt_type_lock -> e_plock -> e_ilock -> e_lock 1275 * 1276 * Interaction with Zones and shutting down: 1277 * 1278 * endpnt_type_ts are uniquely identified by the (e_zoneid, e_rdev, e_protofmly) 1279 * tuple, which means that a zone may not reuse another zone's idle endpoints 1280 * without first doing a t_kclose(). 1281 * 1282 * A zone's endpnt_type_ts are destroyed when a zone is shut down; e_async_cv 1283 * and e_async_count are used to keep track of the threads in endpnt_taskq 1284 * trying to reap endpnt_ts in the endpnt_type_t. 1285 */ 1286 1287 /* 1288 * Allocate and initialize an endpnt_type_t 1289 */ 1290 static struct endpnt_type * 1291 endpnt_type_create(struct knetconfig *config) 1292 { 1293 struct endpnt_type *etype; 1294 1295 /* 1296 * Allocate a new endpoint type to hang a list of 1297 * endpoints off of it. 1298 */ 1299 etype = kmem_alloc(sizeof (struct endpnt_type), KM_SLEEP); 1300 etype->e_next = NULL; 1301 etype->e_pcurr = NULL; 1302 etype->e_itimer = 0; 1303 etype->e_cnt = 0; 1304 1305 (void) strncpy(etype->e_protofmly, config->knc_protofmly, KNC_STRSIZE); 1306 mutex_init(&etype->e_plock, NULL, MUTEX_DEFAULT, NULL); 1307 mutex_init(&etype->e_ilock, NULL, MUTEX_DEFAULT, NULL); 1308 etype->e_rdev = config->knc_rdev; 1309 etype->e_zoneid = rpc_zoneid(); 1310 etype->e_async_count = 0; 1311 cv_init(&etype->e_async_cv, NULL, CV_DEFAULT, NULL); 1312 1313 list_create(&etype->e_pool, sizeof (endpnt_t), 1314 offsetof(endpnt_t, e_node)); 1315 list_create(&etype->e_ilist, sizeof (endpnt_t), 1316 offsetof(endpnt_t, e_idle)); 1317 1318 /* 1319 * Check to see if we need to create a taskq for endpoint 1320 * reaping 1321 */ 1322 mutex_enter(&endpnt_taskq_lock); 1323 if (taskq_created == FALSE) { 1324 taskq_created = TRUE; 1325 mutex_exit(&endpnt_taskq_lock); 1326 ASSERT(endpnt_taskq == NULL); 1327 endpnt_taskq = taskq_create("clts_endpnt_taskq", 1, 1328 minclsyspri, 200, INT_MAX, 0); 1329 } else 1330 mutex_exit(&endpnt_taskq_lock); 1331 1332 return (etype); 1333 } 1334 1335 /* 1336 * Free an endpnt_type_t 1337 */ 1338 static void 1339 endpnt_type_free(struct endpnt_type *etype) 1340 { 1341 mutex_destroy(&etype->e_plock); 1342 mutex_destroy(&etype->e_ilock); 1343 list_destroy(&etype->e_pool); 1344 list_destroy(&etype->e_ilist); 1345 kmem_free(etype, sizeof (endpnt_type_t)); 1346 } 1347 1348 /* 1349 * Check the endpoint to ensure that it is suitable for use. 1350 * 1351 * Possible return values: 1352 * 1353 * return (1) - Endpoint is established, but needs to be re-opened. 1354 * return (0) && *newp == NULL - Endpoint is established, but unusable. 1355 * return (0) && *newp != NULL - Endpoint is established and usable. 1356 */ 1357 static int 1358 check_endpnt(struct endpnt *endp, struct endpnt **newp) 1359 { 1360 *newp = endp; 1361 1362 mutex_enter(&endp->e_lock); 1363 ASSERT(endp->e_ref >= 1); 1364 1365 /* 1366 * The first condition we check for is if the endpoint has been 1367 * allocated, but is unusable either because it has been closed or 1368 * has been marked stale. Only *one* thread will be allowed to 1369 * execute the then clause. This is enforced because the first thread 1370 * to check this condition will clear the flags, so that subsequent 1371 * thread(s) checking this endpoint will move on. 1372 */ 1373 if ((endp->e_flags & ENDPNT_ESTABLISHED) && 1374 (!(endp->e_flags & ENDPNT_BOUND) || 1375 (endp->e_flags & ENDPNT_STALE))) { 1376 /* 1377 * Clear the flags here since they will be 1378 * set again by this thread. They need to be 1379 * individually cleared because we want to maintain 1380 * the state for ENDPNT_ONIDLE. 1381 */ 1382 endp->e_flags &= ~(ENDPNT_ESTABLISHED | 1383 ENDPNT_WAITING | ENDPNT_BOUND | ENDPNT_STALE); 1384 mutex_exit(&endp->e_lock); 1385 return (1); 1386 } 1387 1388 /* 1389 * The second condition is meant for any thread that is waiting for 1390 * an endpoint to become established. It will cv_wait() until 1391 * the condition for the endpoint has been changed to ENDPNT_BOUND or 1392 * ENDPNT_STALE. 1393 */ 1394 while (!(endp->e_flags & ENDPNT_BOUND) && 1395 !(endp->e_flags & ENDPNT_STALE)) { 1396 endp->e_flags |= ENDPNT_WAITING; 1397 cv_wait(&endp->e_cv, &endp->e_lock); 1398 } 1399 1400 ASSERT(endp->e_flags & ENDPNT_ESTABLISHED); 1401 1402 /* 1403 * The last case we check for is if the endpoint has been marked stale. 1404 * If this is the case then set *newp to NULL and return, so that the 1405 * caller is notified of the error and can take appropriate action. 1406 */ 1407 if (endp->e_flags & ENDPNT_STALE) { 1408 endp->e_ref--; 1409 *newp = NULL; 1410 } 1411 mutex_exit(&endp->e_lock); 1412 return (0); 1413 } 1414 1415 #ifdef DEBUG 1416 /* 1417 * Provide a fault injection setting to test error conditions. 1418 */ 1419 static int endpnt_get_return_null = 0; 1420 #endif 1421 1422 /* 1423 * Returns a handle (struct endpnt *) to an open and bound endpoint 1424 * specified by the knetconfig passed in. Returns NULL if no valid endpoint 1425 * can be obtained. 1426 */ 1427 static struct endpnt * 1428 endpnt_get(struct knetconfig *config, int useresvport) 1429 { 1430 struct endpnt_type *n_etype = NULL; 1431 struct endpnt_type *np = NULL; 1432 struct endpnt *new = NULL; 1433 struct endpnt *endp = NULL; 1434 struct endpnt *next = NULL; 1435 TIUSER *tiptr = NULL; 1436 int rtries = BINDRESVPORT_RETRIES; 1437 int i = 0; 1438 int error; 1439 int retval; 1440 zoneid_t zoneid = rpc_zoneid(); 1441 cred_t *cr; 1442 1443 RPCLOG(1, "endpnt_get: protofmly %s, ", config->knc_protofmly); 1444 RPCLOG(1, "rdev %ld\n", config->knc_rdev); 1445 1446 #ifdef DEBUG 1447 /* 1448 * Inject fault if desired. Pretend we have a stale endpoint 1449 * and return NULL. 1450 */ 1451 if (endpnt_get_return_null > 0) { 1452 endpnt_get_return_null--; 1453 return (NULL); 1454 } 1455 #endif 1456 rw_enter(&endpnt_type_lock, RW_READER); 1457 1458 top: 1459 for (np = endpnt_type_list; np != NULL; np = np->e_next) 1460 if ((np->e_zoneid == zoneid) && 1461 (np->e_rdev == config->knc_rdev) && 1462 (strcmp(np->e_protofmly, 1463 config->knc_protofmly) == 0)) 1464 break; 1465 1466 if (np == NULL && n_etype != NULL) { 1467 ASSERT(rw_write_held(&endpnt_type_lock)); 1468 1469 /* 1470 * Link the endpoint type onto the list 1471 */ 1472 n_etype->e_next = endpnt_type_list; 1473 endpnt_type_list = n_etype; 1474 np = n_etype; 1475 n_etype = NULL; 1476 } 1477 1478 if (np == NULL) { 1479 /* 1480 * The logic here is that we were unable to find an 1481 * endpnt_type_t that matched our criteria, so we allocate a 1482 * new one. Because kmem_alloc() needs to be called with 1483 * KM_SLEEP, we drop our locks so that we don't induce 1484 * deadlock. After allocating and initializing the 1485 * endpnt_type_t, we reaquire the lock and go back to check 1486 * if this entry needs to be added to the list. Since we do 1487 * some operations without any locking other threads may 1488 * have been looking for the same endpnt_type_t and gone 1489 * through this code path. We check for this case and allow 1490 * one thread to link its endpnt_type_t to the list and the 1491 * other threads will simply free theirs. 1492 */ 1493 rw_exit(&endpnt_type_lock); 1494 n_etype = endpnt_type_create(config); 1495 1496 /* 1497 * We need to reaquire the lock with RW_WRITER here so that 1498 * we can safely link the new endpoint type onto the list. 1499 */ 1500 rw_enter(&endpnt_type_lock, RW_WRITER); 1501 goto top; 1502 } 1503 1504 rw_exit(&endpnt_type_lock); 1505 /* 1506 * If n_etype is not NULL, then another thread was able to 1507 * insert an endpnt_type_t of this type onto the list before 1508 * we did. Go ahead and free ours. 1509 */ 1510 if (n_etype != NULL) 1511 endpnt_type_free(n_etype); 1512 1513 mutex_enter(&np->e_ilock); 1514 /* 1515 * The algorithm to hand out endpoints is to first 1516 * give out those that are idle if such endpoints 1517 * exist. Otherwise, create a new one if we haven't 1518 * reached the max threshold. Finally, we give out 1519 * endpoints in a pseudo LRU fashion (round-robin). 1520 * 1521 * Note: The idle list is merely a hint of those endpoints 1522 * that should be idle. There exists a window after the 1523 * endpoint is released and before it is linked back onto the 1524 * idle list where a thread could get a reference to it and 1525 * use it. This is okay, since the reference counts will 1526 * still be consistent. 1527 */ 1528 if ((endp = (endpnt_t *)list_head(&np->e_ilist)) != NULL) { 1529 timeout_id_t t_id = 0; 1530 1531 mutex_enter(&endp->e_lock); 1532 endp->e_ref++; 1533 endp->e_itime = 0; 1534 endp->e_flags &= ~ENDPNT_ONIDLE; 1535 mutex_exit(&endp->e_lock); 1536 1537 /* 1538 * Pop the endpoint off the idle list and hand it off 1539 */ 1540 list_remove(&np->e_ilist, endp); 1541 1542 if (np->e_itimer != 0) { 1543 t_id = np->e_itimer; 1544 np->e_itimer = 0; 1545 } 1546 mutex_exit(&np->e_ilock); 1547 /* 1548 * Reset the idle timer if it has been set 1549 */ 1550 if (t_id != (timeout_id_t)0) 1551 (void) untimeout(t_id); 1552 1553 if (check_endpnt(endp, &new) == 0) 1554 return (new); 1555 } else if (np->e_cnt >= clnt_clts_max_endpoints) { 1556 /* 1557 * There are no idle endpoints currently, so 1558 * create a new one if we have not reached the maximum or 1559 * hand one out in round-robin. 1560 */ 1561 mutex_exit(&np->e_ilock); 1562 mutex_enter(&np->e_plock); 1563 endp = np->e_pcurr; 1564 mutex_enter(&endp->e_lock); 1565 endp->e_ref++; 1566 mutex_exit(&endp->e_lock); 1567 1568 ASSERT(endp != NULL); 1569 /* 1570 * Advance the pointer to the next eligible endpoint, if 1571 * necessary. 1572 */ 1573 if (np->e_cnt > 1) { 1574 next = (endpnt_t *)list_next(&np->e_pool, np->e_pcurr); 1575 if (next == NULL) 1576 next = (endpnt_t *)list_head(&np->e_pool); 1577 np->e_pcurr = next; 1578 } 1579 1580 mutex_exit(&np->e_plock); 1581 1582 /* 1583 * We need to check to see if this endpoint is bound or 1584 * not. If it is in progress then just wait until 1585 * the set up is complete 1586 */ 1587 if (check_endpnt(endp, &new) == 0) 1588 return (new); 1589 } else { 1590 mutex_exit(&np->e_ilock); 1591 mutex_enter(&np->e_plock); 1592 1593 /* 1594 * Allocate a new endpoint to use. If we can't allocate any 1595 * more memory then use one that is already established if any 1596 * such endpoints exist. 1597 */ 1598 new = kmem_cache_alloc(endpnt_cache, KM_NOSLEEP); 1599 if (new == NULL) { 1600 RPCLOG0(1, "endpnt_get: kmem_cache_alloc failed\n"); 1601 /* 1602 * Try to recover by using an existing endpoint. 1603 */ 1604 if (np->e_cnt <= 0) { 1605 mutex_exit(&np->e_plock); 1606 return (NULL); 1607 } 1608 endp = np->e_pcurr; 1609 if ((next = list_next(&np->e_pool, np->e_pcurr)) != 1610 NULL) 1611 np->e_pcurr = next; 1612 ASSERT(endp != NULL); 1613 mutex_enter(&endp->e_lock); 1614 endp->e_ref++; 1615 mutex_exit(&endp->e_lock); 1616 mutex_exit(&np->e_plock); 1617 1618 if (check_endpnt(endp, &new) == 0) 1619 return (new); 1620 } else { 1621 /* 1622 * Partially init an endpoint structure and put 1623 * it on the list, so that other interested threads 1624 * know that one is being created 1625 */ 1626 bzero(new, sizeof (struct endpnt)); 1627 1628 cv_init(&new->e_cv, NULL, CV_DEFAULT, NULL); 1629 mutex_init(&new->e_lock, NULL, MUTEX_DEFAULT, NULL); 1630 new->e_ref = 1; 1631 new->e_type = np; 1632 1633 /* 1634 * Link the endpoint into the pool. 1635 */ 1636 list_insert_head(&np->e_pool, new); 1637 np->e_cnt++; 1638 if (np->e_pcurr == NULL) 1639 np->e_pcurr = new; 1640 mutex_exit(&np->e_plock); 1641 } 1642 } 1643 1644 /* 1645 * The transport should be opened with sufficient privs 1646 */ 1647 cr = zone_kcred(); 1648 error = t_kopen(NULL, config->knc_rdev, FREAD|FWRITE|FNDELAY, &tiptr, 1649 cr); 1650 if (error) { 1651 RPCLOG(1, "endpnt_get: t_kopen: %d\n", error); 1652 goto bad; 1653 } 1654 1655 new->e_tiptr = tiptr; 1656 rpc_poptimod(tiptr->fp->f_vnode); 1657 1658 /* 1659 * Allow the kernel to push the module on behalf of the user. 1660 */ 1661 error = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"rpcmod", 0, 1662 K_TO_K, cr, &retval); 1663 if (error) { 1664 RPCLOG(1, "endpnt_get: kstr_push on rpcmod failed %d\n", error); 1665 goto bad; 1666 } 1667 1668 error = strioctl(tiptr->fp->f_vnode, RPC_CLIENT, 0, 0, K_TO_K, 1669 cr, &retval); 1670 if (error) { 1671 RPCLOG(1, "endpnt_get: strioctl failed %d\n", error); 1672 goto bad; 1673 } 1674 1675 /* 1676 * Connectionless data flow should bypass the stream head. 1677 */ 1678 new->e_wq = tiptr->fp->f_vnode->v_stream->sd_wrq->q_next; 1679 1680 error = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"timod", 0, 1681 K_TO_K, cr, &retval); 1682 if (error) { 1683 RPCLOG(1, "endpnt_get: kstr_push on timod failed %d\n", error); 1684 goto bad; 1685 } 1686 1687 /* 1688 * Attempt to bind the endpoint. If we fail then propogate 1689 * error back to calling subsystem, so that it can be handled 1690 * appropriately. 1691 * If the caller has not specified reserved port usage then 1692 * take the system default. 1693 */ 1694 if (useresvport == -1) 1695 useresvport = clnt_clts_do_bindresvport; 1696 1697 if (useresvport && 1698 (strcmp(config->knc_protofmly, NC_INET) == 0 || 1699 strcmp(config->knc_protofmly, NC_INET6) == 0)) { 1700 1701 while ((error = 1702 bindresvport(new->e_tiptr, NULL, NULL, FALSE)) != 0) { 1703 RPCLOG(1, 1704 "endpnt_get: bindresvport error %d\n", error); 1705 if (error != EPROTO) { 1706 if (rtries-- <= 0) 1707 goto bad; 1708 1709 delay(hz << i++); 1710 continue; 1711 } 1712 1713 (void) t_kclose(new->e_tiptr, 1); 1714 /* 1715 * reopen with all privileges 1716 */ 1717 error = t_kopen(NULL, config->knc_rdev, 1718 FREAD|FWRITE|FNDELAY, 1719 &new->e_tiptr, cr); 1720 if (error) { 1721 RPCLOG(1, "endpnt_get: t_kopen: %d\n", error); 1722 new->e_tiptr = NULL; 1723 goto bad; 1724 } 1725 } 1726 } else if ((error = t_kbind(new->e_tiptr, NULL, NULL)) != 0) { 1727 RPCLOG(1, "endpnt_get: t_kbind failed: %d\n", error); 1728 goto bad; 1729 } 1730 1731 /* 1732 * Set the flags and notify and waiters that we have an established 1733 * endpoint. 1734 */ 1735 mutex_enter(&new->e_lock); 1736 new->e_flags |= ENDPNT_ESTABLISHED; 1737 new->e_flags |= ENDPNT_BOUND; 1738 if (new->e_flags & ENDPNT_WAITING) { 1739 cv_broadcast(&new->e_cv); 1740 new->e_flags &= ~ENDPNT_WAITING; 1741 } 1742 mutex_exit(&new->e_lock); 1743 1744 return (new); 1745 1746 bad: 1747 ASSERT(new != NULL); 1748 /* 1749 * mark this endpoint as stale and notify any threads waiting 1750 * on this endpoint that it will be going away. 1751 */ 1752 mutex_enter(&new->e_lock); 1753 if (new->e_ref > 0) { 1754 new->e_flags |= ENDPNT_ESTABLISHED; 1755 new->e_flags |= ENDPNT_STALE; 1756 if (new->e_flags & ENDPNT_WAITING) { 1757 cv_broadcast(&new->e_cv); 1758 new->e_flags &= ~ENDPNT_WAITING; 1759 } 1760 } 1761 new->e_ref--; 1762 new->e_tiptr = NULL; 1763 mutex_exit(&new->e_lock); 1764 1765 /* 1766 * If there was a transport endopoint opened, then close it. 1767 */ 1768 if (tiptr != NULL) 1769 (void) t_kclose(tiptr, 1); 1770 1771 return (NULL); 1772 } 1773 1774 /* 1775 * Release a referece to the endpoint 1776 */ 1777 static void 1778 endpnt_rele(struct endpnt *sp) 1779 { 1780 mutex_enter(&sp->e_lock); 1781 ASSERT(sp->e_ref > 0); 1782 sp->e_ref--; 1783 /* 1784 * If the ref count is zero, then start the idle timer and link 1785 * the endpoint onto the idle list. 1786 */ 1787 if (sp->e_ref == 0) { 1788 sp->e_itime = gethrestime_sec(); 1789 1790 /* 1791 * Check to see if the endpoint is already linked to the idle 1792 * list, so that we don't try to reinsert it. 1793 */ 1794 if (sp->e_flags & ENDPNT_ONIDLE) { 1795 mutex_exit(&sp->e_lock); 1796 mutex_enter(&sp->e_type->e_ilock); 1797 endpnt_reap_settimer(sp->e_type); 1798 mutex_exit(&sp->e_type->e_ilock); 1799 return; 1800 } 1801 1802 sp->e_flags |= ENDPNT_ONIDLE; 1803 mutex_exit(&sp->e_lock); 1804 mutex_enter(&sp->e_type->e_ilock); 1805 list_insert_tail(&sp->e_type->e_ilist, sp); 1806 endpnt_reap_settimer(sp->e_type); 1807 mutex_exit(&sp->e_type->e_ilock); 1808 } else 1809 mutex_exit(&sp->e_lock); 1810 } 1811 1812 static void 1813 endpnt_reap_settimer(endpnt_type_t *etp) 1814 { 1815 if (etp->e_itimer == (timeout_id_t)0) 1816 etp->e_itimer = timeout(endpnt_reap_dispatch, (void *)etp, 1817 clnt_clts_taskq_dispatch_interval); 1818 } 1819 1820 static void 1821 endpnt_reap_dispatch(void *a) 1822 { 1823 endpnt_type_t *etp = a; 1824 1825 /* 1826 * The idle timer has fired, so dispatch the taskq to close the 1827 * endpoint. 1828 */ 1829 if (taskq_dispatch(endpnt_taskq, (task_func_t *)endpnt_reap, etp, 1830 TQ_NOSLEEP) == NULL) 1831 return; 1832 mutex_enter(&etp->e_ilock); 1833 etp->e_async_count++; 1834 mutex_exit(&etp->e_ilock); 1835 } 1836 1837 /* 1838 * Traverse the idle list and close those endpoints that have reached their 1839 * timeout interval. 1840 */ 1841 static void 1842 endpnt_reap(endpnt_type_t *etp) 1843 { 1844 struct endpnt *e; 1845 struct endpnt *next_node = NULL; 1846 1847 mutex_enter(&etp->e_ilock); 1848 e = list_head(&etp->e_ilist); 1849 while (e != NULL) { 1850 next_node = list_next(&etp->e_ilist, e); 1851 1852 mutex_enter(&e->e_lock); 1853 if (e->e_ref > 0) { 1854 mutex_exit(&e->e_lock); 1855 e = next_node; 1856 continue; 1857 } 1858 1859 ASSERT(e->e_ref == 0); 1860 if (e->e_itime > 0 && 1861 (e->e_itime + clnt_clts_endpoint_reap_interval) < 1862 gethrestime_sec()) { 1863 e->e_flags &= ~ENDPNT_BOUND; 1864 (void) t_kclose(e->e_tiptr, 1); 1865 e->e_tiptr = NULL; 1866 e->e_itime = 0; 1867 } 1868 mutex_exit(&e->e_lock); 1869 e = next_node; 1870 } 1871 etp->e_itimer = 0; 1872 if (--etp->e_async_count == 0) 1873 cv_signal(&etp->e_async_cv); 1874 mutex_exit(&etp->e_ilock); 1875 } 1876 1877 static void 1878 endpnt_reclaim(zoneid_t zoneid) 1879 { 1880 struct endpnt_type *np; 1881 struct endpnt *e; 1882 struct endpnt *next_node = NULL; 1883 list_t free_list; 1884 int rcnt = 0; 1885 1886 list_create(&free_list, sizeof (endpnt_t), offsetof(endpnt_t, e_node)); 1887 1888 RPCLOG0(1, "endpnt_reclaim: reclaim callback started\n"); 1889 rw_enter(&endpnt_type_lock, RW_READER); 1890 for (np = endpnt_type_list; np != NULL; np = np->e_next) { 1891 if (zoneid != ALL_ZONES && zoneid != np->e_zoneid) 1892 continue; 1893 1894 mutex_enter(&np->e_plock); 1895 RPCLOG(1, "endpnt_reclaim: protofmly %s, ", 1896 np->e_protofmly); 1897 RPCLOG(1, "rdev %ld\n", np->e_rdev); 1898 RPCLOG(1, "endpnt_reclaim: found %d endpoint(s)\n", 1899 np->e_cnt); 1900 1901 if (np->e_cnt == 0) { 1902 mutex_exit(&np->e_plock); 1903 continue; 1904 } 1905 1906 /* 1907 * The nice thing about maintaining an idle list is that if 1908 * there are any endpoints to reclaim, they are going to be 1909 * on this list. Just go through and reap the one's that 1910 * have ref counts of zero. 1911 */ 1912 mutex_enter(&np->e_ilock); 1913 e = list_head(&np->e_ilist); 1914 while (e != NULL) { 1915 next_node = list_next(&np->e_ilist, e); 1916 mutex_enter(&e->e_lock); 1917 if (e->e_ref > 0) { 1918 mutex_exit(&e->e_lock); 1919 e = next_node; 1920 continue; 1921 } 1922 ASSERT(e->e_ref == 0); 1923 mutex_exit(&e->e_lock); 1924 1925 list_remove(&np->e_ilist, e); 1926 list_remove(&np->e_pool, e); 1927 list_insert_head(&free_list, e); 1928 1929 rcnt++; 1930 np->e_cnt--; 1931 e = next_node; 1932 } 1933 mutex_exit(&np->e_ilock); 1934 /* 1935 * Reset the current pointer to be safe 1936 */ 1937 if ((e = (struct endpnt *)list_head(&np->e_pool)) != NULL) 1938 np->e_pcurr = e; 1939 else { 1940 ASSERT(np->e_cnt == 0); 1941 np->e_pcurr = NULL; 1942 } 1943 1944 mutex_exit(&np->e_plock); 1945 } 1946 rw_exit(&endpnt_type_lock); 1947 1948 while ((e = list_head(&free_list)) != NULL) { 1949 list_remove(&free_list, e); 1950 if (e->e_tiptr != NULL) 1951 (void) t_kclose(e->e_tiptr, 1); 1952 1953 cv_destroy(&e->e_cv); 1954 mutex_destroy(&e->e_lock); 1955 kmem_cache_free(endpnt_cache, e); 1956 } 1957 list_destroy(&free_list); 1958 RPCLOG(1, "endpnt_reclaim: reclaimed %d endpoint(s)\n", rcnt); 1959 } 1960 1961 /* 1962 * Endpoint reclaim zones destructor callback routine. 1963 * 1964 * After reclaiming any cached entries, we basically go through the endpnt_type 1965 * list, canceling outstanding timeouts and free'ing data structures. 1966 */ 1967 /* ARGSUSED */ 1968 static void 1969 endpnt_destructor(zoneid_t zoneid, void *a) 1970 { 1971 struct endpnt_type **npp; 1972 struct endpnt_type *np; 1973 struct endpnt_type *free_list = NULL; 1974 timeout_id_t t_id = 0; 1975 extern void clcleanup_zone(zoneid_t); 1976 extern void clcleanup4_zone(zoneid_t); 1977 1978 /* Make sure NFS client handles are released. */ 1979 clcleanup_zone(zoneid); 1980 clcleanup4_zone(zoneid); 1981 1982 endpnt_reclaim(zoneid); 1983 /* 1984 * We don't need to be holding on to any locks across the call to 1985 * endpnt_reclaim() and the code below; we know that no-one can 1986 * be holding open connections for this zone (all processes and kernel 1987 * threads are gone), so nothing could be adding anything to the list. 1988 */ 1989 rw_enter(&endpnt_type_lock, RW_WRITER); 1990 npp = &endpnt_type_list; 1991 while ((np = *npp) != NULL) { 1992 if (np->e_zoneid != zoneid) { 1993 npp = &np->e_next; 1994 continue; 1995 } 1996 mutex_enter(&np->e_plock); 1997 mutex_enter(&np->e_ilock); 1998 if (np->e_itimer != 0) { 1999 t_id = np->e_itimer; 2000 np->e_itimer = 0; 2001 } 2002 ASSERT(np->e_cnt == 0); 2003 ASSERT(list_head(&np->e_pool) == NULL); 2004 ASSERT(list_head(&np->e_ilist) == NULL); 2005 2006 mutex_exit(&np->e_ilock); 2007 mutex_exit(&np->e_plock); 2008 2009 /* 2010 * untimeout() any outstanding timers that have not yet fired. 2011 */ 2012 if (t_id != (timeout_id_t)0) 2013 (void) untimeout(t_id); 2014 *npp = np->e_next; 2015 np->e_next = free_list; 2016 free_list = np; 2017 } 2018 rw_exit(&endpnt_type_lock); 2019 2020 while (free_list != NULL) { 2021 np = free_list; 2022 free_list = free_list->e_next; 2023 /* 2024 * Wait for threads in endpnt_taskq trying to reap endpnt_ts in 2025 * the endpnt_type_t. 2026 */ 2027 mutex_enter(&np->e_ilock); 2028 while (np->e_async_count > 0) 2029 cv_wait(&np->e_async_cv, &np->e_ilock); 2030 cv_destroy(&np->e_async_cv); 2031 mutex_destroy(&np->e_plock); 2032 mutex_destroy(&np->e_ilock); 2033 list_destroy(&np->e_pool); 2034 list_destroy(&np->e_ilist); 2035 kmem_free(np, sizeof (endpnt_type_t)); 2036 } 2037 } 2038 2039 /* 2040 * Endpoint reclaim kmem callback routine. 2041 */ 2042 /* ARGSUSED */ 2043 static void 2044 endpnt_repossess(void *a) 2045 { 2046 /* 2047 * Reclaim idle endpnt's from all zones. 2048 */ 2049 if (endpnt_taskq != NULL) 2050 (void) taskq_dispatch(endpnt_taskq, 2051 (task_func_t *)endpnt_reclaim, (void *)ALL_ZONES, 2052 TQ_NOSLEEP); 2053 } 2054 2055 /* 2056 * RPC request dispatch routine. Constructs a datagram message and wraps it 2057 * around the RPC request to pass downstream. 2058 */ 2059 static int 2060 clnt_clts_dispatch_send(queue_t *q, mblk_t *mp, struct netbuf *addr, 2061 calllist_t *cp, uint_t xid) 2062 { 2063 mblk_t *bp; 2064 int msgsz; 2065 struct T_unitdata_req *udreq; 2066 2067 /* 2068 * Set up the call record. 2069 */ 2070 cp->call_wq = q; 2071 cp->call_xid = xid; 2072 cp->call_status = RPC_TIMEDOUT; 2073 cp->call_notified = FALSE; 2074 RPCLOG(64, 2075 "clnt_clts_dispatch_send: putting xid 0x%x on " 2076 "dispatch list\n", xid); 2077 cp->call_hash = call_hash(xid, clnt_clts_hash_size); 2078 cp->call_bucket = &clts_call_ht[cp->call_hash]; 2079 call_table_enter(cp); 2080 2081 /* 2082 * Construct the datagram 2083 */ 2084 msgsz = (int)TUNITDATAREQSZ; 2085 while (!(bp = allocb(msgsz + addr->len, BPRI_LO))) { 2086 if (strwaitbuf(msgsz + addr->len, BPRI_LO)) 2087 return (ENOSR); 2088 } 2089 2090 udreq = (struct T_unitdata_req *)bp->b_wptr; 2091 udreq->PRIM_type = T_UNITDATA_REQ; 2092 udreq->DEST_length = addr->len; 2093 2094 if (addr->len) { 2095 bcopy(addr->buf, bp->b_wptr + msgsz, addr->len); 2096 udreq->DEST_offset = (t_scalar_t)msgsz; 2097 msgsz += addr->len; 2098 } else 2099 udreq->DEST_offset = 0; 2100 udreq->OPT_length = 0; 2101 udreq->OPT_offset = 0; 2102 2103 bp->b_datap->db_type = M_PROTO; 2104 bp->b_wptr += msgsz; 2105 2106 /* 2107 * Link the datagram header with the actual data 2108 */ 2109 linkb(bp, mp); 2110 2111 /* 2112 * Send downstream. 2113 */ 2114 if (canput(cp->call_wq)) { 2115 put(cp->call_wq, bp); 2116 return (0); 2117 } 2118 2119 return (EIO); 2120 } 2121 2122 /* 2123 * RPC response delivery routine. Deliver the response to the waiting 2124 * thread by matching the xid. 2125 */ 2126 void 2127 clnt_clts_dispatch_notify(mblk_t *mp, int resp_off, zoneid_t zoneid) 2128 { 2129 calllist_t *e = NULL; 2130 call_table_t *chtp; 2131 uint32_t xid; 2132 uint_t hash; 2133 unsigned char *hdr_offset; 2134 mblk_t *resp; 2135 2136 /* 2137 * If the RPC response is not contained in the same mblk as the 2138 * datagram header, then move to the next mblk. 2139 */ 2140 hdr_offset = mp->b_rptr; 2141 resp = mp; 2142 if ((mp->b_wptr - (mp->b_rptr + resp_off)) == 0) 2143 resp = mp->b_cont; 2144 else 2145 resp->b_rptr += resp_off; 2146 2147 ASSERT(resp != NULL); 2148 2149 if ((IS_P2ALIGNED(resp->b_rptr, sizeof (uint32_t))) && 2150 (resp->b_wptr - resp->b_rptr) >= sizeof (xid)) 2151 xid = *((uint32_t *)resp->b_rptr); 2152 else { 2153 int i = 0; 2154 unsigned char *p = (unsigned char *)&xid; 2155 unsigned char *rptr; 2156 mblk_t *tmp = resp; 2157 2158 /* 2159 * Copy the xid, byte-by-byte into xid. 2160 */ 2161 while (tmp) { 2162 rptr = tmp->b_rptr; 2163 while (rptr < tmp->b_wptr) { 2164 *p++ = *rptr++; 2165 if (++i >= sizeof (xid)) 2166 goto done_xid_copy; 2167 } 2168 tmp = tmp->b_cont; 2169 } 2170 2171 /* 2172 * If we got here, we ran out of mblk space before the 2173 * xid could be copied. 2174 */ 2175 ASSERT(tmp == NULL && i < sizeof (xid)); 2176 2177 RPCLOG0(1, 2178 "clnt_dispatch_notify(clts): message less than " 2179 "size of xid\n"); 2180 2181 freemsg(mp); 2182 return; 2183 } 2184 2185 done_xid_copy: 2186 2187 /* 2188 * Reset the read pointer back to the beginning of the protocol 2189 * header if we moved it. 2190 */ 2191 if (mp->b_rptr != hdr_offset) 2192 mp->b_rptr = hdr_offset; 2193 2194 hash = call_hash(xid, clnt_clts_hash_size); 2195 chtp = &clts_call_ht[hash]; 2196 /* call_table_find returns with the hash bucket locked */ 2197 call_table_find(chtp, xid, e); 2198 2199 if (e != NULL) { 2200 mutex_enter(&e->call_lock); 2201 2202 /* 2203 * verify that the reply is coming in on 2204 * the same zone that it was sent from. 2205 */ 2206 if (e->call_zoneid != zoneid) { 2207 mutex_exit(&e->call_lock); 2208 mutex_exit(&chtp->ct_lock); 2209 freemsg(mp); 2210 return; 2211 } 2212 2213 /* 2214 * found thread waiting for this reply. 2215 */ 2216 if (e->call_reply) { 2217 RPCLOG(8, 2218 "clnt_dispatch_notify (clts): discarding old " 2219 "reply for xid 0x%x\n", 2220 xid); 2221 freemsg(e->call_reply); 2222 } 2223 e->call_notified = TRUE; 2224 e->call_reply = mp; 2225 e->call_status = RPC_SUCCESS; 2226 cv_signal(&e->call_cv); 2227 mutex_exit(&e->call_lock); 2228 mutex_exit(&chtp->ct_lock); 2229 } else { 2230 zone_t *zone; 2231 struct rpcstat *rpcstat; 2232 2233 mutex_exit(&chtp->ct_lock); 2234 RPCLOG(8, "clnt_dispatch_notify (clts): no caller for reply " 2235 "0x%x\n", xid); 2236 freemsg(mp); 2237 /* 2238 * This is unfortunate, but we need to lookup the zone so we 2239 * can increment its "rcbadxids" counter. 2240 */ 2241 zone = zone_find_by_id(zoneid); 2242 if (zone == NULL) { 2243 /* 2244 * The zone went away... 2245 */ 2246 return; 2247 } 2248 rpcstat = zone_getspecific(rpcstat_zone_key, zone); 2249 if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 2250 /* 2251 * Not interested 2252 */ 2253 zone_rele(zone); 2254 return; 2255 } 2256 RCSTAT_INCR(rpcstat->rpc_clts_client, rcbadxids); 2257 zone_rele(zone); 2258 } 2259 } 2260 2261 /* 2262 * Init routine. Called when rpcmod is loaded. 2263 */ 2264 void 2265 clnt_clts_init(void) 2266 { 2267 endpnt_cache = kmem_cache_create("clnt_clts_endpnt_cache", 2268 sizeof (struct endpnt), 0, NULL, NULL, endpnt_repossess, NULL, 2269 NULL, 0); 2270 2271 rw_init(&endpnt_type_lock, NULL, RW_DEFAULT, NULL); 2272 2273 /* 2274 * Perform simple bounds checking to make sure that the setting is 2275 * reasonable 2276 */ 2277 if (clnt_clts_max_endpoints <= 0) { 2278 if (clnt_clts_do_bindresvport) 2279 clnt_clts_max_endpoints = RESERVED_PORTSPACE; 2280 else 2281 clnt_clts_max_endpoints = NONRESERVED_PORTSPACE; 2282 } 2283 2284 if (clnt_clts_do_bindresvport && 2285 clnt_clts_max_endpoints > RESERVED_PORTSPACE) 2286 clnt_clts_max_endpoints = RESERVED_PORTSPACE; 2287 else if (clnt_clts_max_endpoints > NONRESERVED_PORTSPACE) 2288 clnt_clts_max_endpoints = NONRESERVED_PORTSPACE; 2289 2290 if (clnt_clts_hash_size < DEFAULT_MIN_HASH_SIZE) 2291 clnt_clts_hash_size = DEFAULT_MIN_HASH_SIZE; 2292 2293 /* 2294 * Defer creating the taskq until rpcmod gets pushed. If we are 2295 * in diskless boot mode, rpcmod will get loaded early even before 2296 * thread_create() is available. 2297 */ 2298 endpnt_taskq = NULL; 2299 taskq_created = FALSE; 2300 mutex_init(&endpnt_taskq_lock, NULL, MUTEX_DEFAULT, NULL); 2301 2302 if (clnt_clts_endpoint_reap_interval < DEFAULT_ENDPOINT_REAP_INTERVAL) 2303 clnt_clts_endpoint_reap_interval = 2304 DEFAULT_ENDPOINT_REAP_INTERVAL; 2305 2306 /* 2307 * Dispatch the taskq at an interval which is offset from the 2308 * interval that the endpoints should be reaped. 2309 */ 2310 clnt_clts_taskq_dispatch_interval = 2311 (clnt_clts_endpoint_reap_interval + DEFAULT_INTERVAL_SHIFT) * hz; 2312 2313 /* 2314 * Initialize the completion queue 2315 */ 2316 clts_call_ht = call_table_init(clnt_clts_hash_size); 2317 /* 2318 * Initialize the zone destructor callback. 2319 */ 2320 zone_key_create(&endpnt_destructor_key, NULL, NULL, endpnt_destructor); 2321 } 2322 2323 void 2324 clnt_clts_fini(void) 2325 { 2326 (void) zone_key_delete(endpnt_destructor_key); 2327 } 2328