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 over Connection Oriented 39 * Transports (COTS). 40 */ 41 42 /* 43 * Much of this file has been re-written to let NFS work better over slow 44 * transports. A description follows. 45 * 46 * One of the annoying things about kRPC/COTS is that it will temporarily 47 * create more than one connection between a client and server. This 48 * happens because when a connection is made, the end-points entry in the 49 * linked list of connections (headed by cm_hd), is removed so that other 50 * threads don't mess with it. Went ahead and bit the bullet by keeping 51 * the endpoint on the connection list and introducing state bits, 52 * condition variables etc. to the connection entry data structure (struct 53 * cm_xprt). 54 * 55 * Here is a summary of the changes to cm-xprt: 56 * 57 * x_ctime is the timestamp of when the endpoint was last 58 * connected or disconnected. If an end-point is ever disconnected 59 * or re-connected, then any outstanding RPC request is presumed 60 * lost, telling clnt_cots_kcallit that it needs to re-send the 61 * request, not just wait for the original request's reply to 62 * arrive. 63 * 64 * x_thread flag which tells us if a thread is doing a connection attempt. 65 * 66 * x_waitdis flag which tells us we are waiting a disconnect ACK. 67 * 68 * x_needdis flag which tells us we need to send a T_DISCONN_REQ 69 * to kill the connection. 70 * 71 * x_needrel flag which tells us we need to send a T_ORDREL_REQ to 72 * gracefully close the connection. 73 * 74 * #defined bitmasks for the all the b_* bits so that more 75 * efficient (and at times less clumsy) masks can be used to 76 * manipulated state in cases where multiple bits have to 77 * set/cleared/checked in the same critical section. 78 * 79 * x_conn_cv and x_dis-_cv are new condition variables to let 80 * threads knows when the connection attempt is done, and to let 81 * the connecting thread know when the disconnect handshake is 82 * done. 83 * 84 * Added the CONN_HOLD() macro so that all reference holds have the same 85 * look and feel. 86 * 87 * In the private (cku_private) portion of the client handle, 88 * 89 * cku_flags replaces the cku_sent a boolean. cku_flags keeps 90 * track of whether a request as been sent, and whether the 91 * client's handles call record is on the dispatch list (so that 92 * the reply can be matched by XID to the right client handle). 93 * The idea of CKU_ONQUEUE is that we can exit clnt_cots_kcallit() 94 * and still have the response find the right client handle so 95 * that the retry of CLNT_CALL() gets the result. Testing, found 96 * situations where if the timeout was increased, performance 97 * degraded. This was due to us hitting a window where the thread 98 * was back in rfscall() (probably printing server not responding) 99 * while the response came back but no place to put it. 100 * 101 * cku_ctime is just a cache of x_ctime. If they match, 102 * clnt_cots_kcallit() won't to send a retry (unless the maximum 103 * receive count limit as been reached). If the don't match, then 104 * we assume the request has been lost, and a retry of the request 105 * is needed. 106 * 107 * cku_recv_attempts counts the number of receive count attempts 108 * after one try is sent on the wire. 109 * 110 * Added the clnt_delay() routine so that interruptible and 111 * noninterruptible delays are possible. 112 * 113 * CLNT_MIN_TIMEOUT has been bumped to 10 seconds from 3. This is used to 114 * control how long the client delays before returned after getting 115 * ECONNREFUSED. At 3 seconds, 8 client threads per mount really does bash 116 * a server that may be booting and not yet started nfsd. 117 * 118 * CLNT_MAXRECV_WITHOUT_RETRY is a new macro (value of 3) (with a tunable) 119 * Why don't we just wait forever (receive an infinite # of times)? 120 * Because the server may have rebooted. More insidious is that some 121 * servers (ours) will drop NFS/TCP requests in some cases. This is bad, 122 * but it is a reality. 123 * 124 * The case of a server doing orderly release really messes up the 125 * client's recovery, especially if the server's TCP implementation is 126 * buggy. It was found was that the kRPC/COTS client was breaking some 127 * TPI rules, such as not waiting for the acknowledgement of a 128 * T_DISCON_REQ (hence the added case statements T_ERROR_ACK, T_OK_ACK and 129 * T_DISCON_REQ in clnt_dispatch_notifyall()). 130 * 131 * One of things that we've seen is that a kRPC TCP endpoint goes into 132 * TIMEWAIT and a thus a reconnect takes a long time to satisfy because 133 * that the TIMEWAIT state takes a while to finish. If a server sends a 134 * T_ORDREL_IND, there is little point in an RPC client doing a 135 * T_ORDREL_REQ, because the RPC request isn't going to make it (the 136 * server is saying that it won't accept any more data). So kRPC was 137 * changed to send a T_DISCON_REQ when we get a T_ORDREL_IND. So now the 138 * connection skips the TIMEWAIT state and goes straight to a bound state 139 * that kRPC can quickly switch to connected. 140 * 141 * Code that issues TPI request must use waitforack() to wait for the 142 * corresponding ack (assuming there is one) in any future modifications. 143 * This works around problems that may be introduced by breaking TPI rules 144 * (by submitting new calls before earlier requests have been acked) in the 145 * case of a signal or other early return. waitforack() depends on 146 * clnt_dispatch_notifyconn() to issue the wakeup when the ack 147 * arrives, so adding new TPI calls may require corresponding changes 148 * to clnt_dispatch_notifyconn(). Presently, the timeout period is based on 149 * CLNT_MIN_TIMEOUT which is 10 seconds. If you modify this value, be sure 150 * not to set it too low or TPI ACKS will be lost. 151 */ 152 153 #include <sys/param.h> 154 #include <sys/types.h> 155 #include <sys/user.h> 156 #include <sys/systm.h> 157 #include <sys/sysmacros.h> 158 #include <sys/proc.h> 159 #include <sys/socket.h> 160 #include <sys/file.h> 161 #include <sys/stream.h> 162 #include <sys/strsubr.h> 163 #include <sys/stropts.h> 164 #include <sys/strsun.h> 165 #include <sys/timod.h> 166 #include <sys/tiuser.h> 167 #include <sys/tihdr.h> 168 #include <sys/t_kuser.h> 169 #include <sys/fcntl.h> 170 #include <sys/errno.h> 171 #include <sys/kmem.h> 172 #include <sys/debug.h> 173 #include <sys/systm.h> 174 #include <sys/kstat.h> 175 #include <sys/t_lock.h> 176 #include <sys/ddi.h> 177 #include <sys/cmn_err.h> 178 #include <sys/time.h> 179 #include <sys/isa_defs.h> 180 #include <sys/callb.h> 181 #include <sys/sunddi.h> 182 #include <sys/atomic.h> 183 #include <sys/sdt.h> 184 185 #include <netinet/in.h> 186 #include <netinet/tcp.h> 187 188 #include <rpc/types.h> 189 #include <rpc/xdr.h> 190 #include <rpc/auth.h> 191 #include <rpc/clnt.h> 192 #include <rpc/rpc_msg.h> 193 194 #define COTS_DEFAULT_ALLOCSIZE 2048 195 196 #define WIRE_HDR_SIZE 20 /* serialized call header, sans proc number */ 197 #define MSG_OFFSET 128 /* offset of call into the mblk */ 198 199 const char *kinet_ntop6(uchar_t *, char *, size_t); 200 201 static int clnt_cots_ksettimers(CLIENT *, struct rpc_timers *, 202 struct rpc_timers *, int, void(*)(int, int, caddr_t), caddr_t, uint32_t); 203 static enum clnt_stat clnt_cots_kcallit(CLIENT *, rpcproc_t, xdrproc_t, 204 caddr_t, xdrproc_t, caddr_t, struct timeval); 205 static void clnt_cots_kabort(CLIENT *); 206 static void clnt_cots_kerror(CLIENT *, struct rpc_err *); 207 static bool_t clnt_cots_kfreeres(CLIENT *, xdrproc_t, caddr_t); 208 static void clnt_cots_kdestroy(CLIENT *); 209 static bool_t clnt_cots_kcontrol(CLIENT *, int, char *); 210 211 212 /* List of transports managed by the connection manager. */ 213 struct cm_xprt { 214 TIUSER *x_tiptr; /* transport handle */ 215 queue_t *x_wq; /* send queue */ 216 clock_t x_time; /* last time we handed this xprt out */ 217 clock_t x_ctime; /* time we went to CONNECTED */ 218 int x_tidu_size; /* TIDU size of this transport */ 219 union { 220 struct { 221 unsigned int 222 #ifdef _BIT_FIELDS_HTOL 223 b_closing: 1, /* we've sent a ord rel on this conn */ 224 b_dead: 1, /* transport is closed or disconn */ 225 b_doomed: 1, /* too many conns, let this go idle */ 226 b_connected: 1, /* this connection is connected */ 227 228 b_ordrel: 1, /* do an orderly release? */ 229 b_thread: 1, /* thread doing connect */ 230 b_waitdis: 1, /* waiting for disconnect ACK */ 231 b_needdis: 1, /* need T_DISCON_REQ */ 232 233 b_needrel: 1, /* need T_ORDREL_REQ */ 234 b_early_disc: 1, /* got a T_ORDREL_IND or T_DISCON_IND */ 235 /* disconnect during connect */ 236 237 b_pad: 22; 238 239 #endif 240 241 #ifdef _BIT_FIELDS_LTOH 242 b_pad: 22, 243 244 b_early_disc: 1, /* got a T_ORDREL_IND or T_DISCON_IND */ 245 /* disconnect during connect */ 246 b_needrel: 1, /* need T_ORDREL_REQ */ 247 248 b_needdis: 1, /* need T_DISCON_REQ */ 249 b_waitdis: 1, /* waiting for disconnect ACK */ 250 b_thread: 1, /* thread doing connect */ 251 b_ordrel: 1, /* do an orderly release? */ 252 253 b_connected: 1, /* this connection is connected */ 254 b_doomed: 1, /* too many conns, let this go idle */ 255 b_dead: 1, /* transport is closed or disconn */ 256 b_closing: 1; /* we've sent a ord rel on this conn */ 257 #endif 258 } bit; unsigned int word; 259 260 #define x_closing x_state.bit.b_closing 261 #define x_dead x_state.bit.b_dead 262 #define x_doomed x_state.bit.b_doomed 263 #define x_connected x_state.bit.b_connected 264 265 #define x_ordrel x_state.bit.b_ordrel 266 #define x_thread x_state.bit.b_thread 267 #define x_waitdis x_state.bit.b_waitdis 268 #define x_needdis x_state.bit.b_needdis 269 270 #define x_needrel x_state.bit.b_needrel 271 #define x_early_disc x_state.bit.b_early_disc 272 273 #define x_state_flags x_state.word 274 275 #define X_CLOSING 0x80000000 276 #define X_DEAD 0x40000000 277 #define X_DOOMED 0x20000000 278 #define X_CONNECTED 0x10000000 279 280 #define X_ORDREL 0x08000000 281 #define X_THREAD 0x04000000 282 #define X_WAITDIS 0x02000000 283 #define X_NEEDDIS 0x01000000 284 285 #define X_NEEDREL 0x00800000 286 #define X_EARLYDISC 0x00400000 287 288 #define X_BADSTATES (X_CLOSING | X_DEAD | X_DOOMED) 289 290 } x_state; 291 int x_ref; /* number of users of this xprt */ 292 int x_family; /* address family of transport */ 293 dev_t x_rdev; /* device number of transport */ 294 struct cm_xprt *x_next; 295 296 struct netbuf x_server; /* destination address */ 297 struct netbuf x_src; /* src address (for retries) */ 298 kmutex_t x_lock; /* lock on this entry */ 299 kcondvar_t x_cv; /* to signal when can be closed */ 300 kcondvar_t x_conn_cv; /* to signal when connection attempt */ 301 /* is complete */ 302 kstat_t *x_ksp; 303 304 kcondvar_t x_dis_cv; /* to signal when disconnect attempt */ 305 /* is complete */ 306 zoneid_t x_zoneid; /* zone this xprt belongs to */ 307 }; 308 309 typedef struct cm_kstat_xprt { 310 kstat_named_t x_wq; 311 kstat_named_t x_server; 312 kstat_named_t x_family; 313 kstat_named_t x_rdev; 314 kstat_named_t x_time; 315 kstat_named_t x_state; 316 kstat_named_t x_ref; 317 kstat_named_t x_port; 318 } cm_kstat_xprt_t; 319 320 static cm_kstat_xprt_t cm_kstat_template = { 321 { "write_queue", KSTAT_DATA_UINT32 }, 322 { "server", KSTAT_DATA_STRING }, 323 { "addr_family", KSTAT_DATA_UINT32 }, 324 { "device", KSTAT_DATA_UINT32 }, 325 { "time_stamp", KSTAT_DATA_UINT32 }, 326 { "status", KSTAT_DATA_UINT32 }, 327 { "ref_count", KSTAT_DATA_INT32 }, 328 { "port", KSTAT_DATA_UINT32 }, 329 }; 330 331 /* 332 * The inverse of this is connmgr_release(). 333 */ 334 #define CONN_HOLD(Cm_entry) {\ 335 mutex_enter(&(Cm_entry)->x_lock); \ 336 (Cm_entry)->x_ref++; \ 337 mutex_exit(&(Cm_entry)->x_lock); \ 338 } 339 340 341 /* 342 * Private data per rpc handle. This structure is allocated by 343 * clnt_cots_kcreate, and freed by clnt_cots_kdestroy. 344 */ 345 typedef struct cku_private_s { 346 CLIENT cku_client; /* client handle */ 347 calllist_t cku_call; /* for dispatching calls */ 348 struct rpc_err cku_err; /* error status */ 349 350 struct netbuf cku_srcaddr; /* source address for retries */ 351 int cku_addrfmly; /* for binding port */ 352 struct netbuf cku_addr; /* remote address */ 353 dev_t cku_device; /* device to use */ 354 uint_t cku_flags; 355 #define CKU_ONQUEUE 0x1 356 #define CKU_SENT 0x2 357 358 bool_t cku_progress; /* for CLSET_PROGRESS */ 359 uint32_t cku_xid; /* current XID */ 360 clock_t cku_ctime; /* time stamp of when */ 361 /* connection was created */ 362 uint_t cku_recv_attempts; 363 XDR cku_outxdr; /* xdr routine for output */ 364 XDR cku_inxdr; /* xdr routine for input */ 365 char cku_rpchdr[WIRE_HDR_SIZE + 4]; 366 /* pre-serialized rpc header */ 367 368 uint_t cku_outbuflen; /* default output mblk length */ 369 struct cred *cku_cred; /* credentials */ 370 bool_t cku_nodelayonerr; 371 /* for CLSET_NODELAYONERR */ 372 int cku_useresvport; /* Use reserved port */ 373 struct rpc_cots_client *cku_stats; /* stats for zone */ 374 } cku_private_t; 375 376 static struct cm_xprt *connmgr_wrapconnect(struct cm_xprt *, 377 const struct timeval *, struct netbuf *, int, struct netbuf *, 378 struct rpc_err *, bool_t, bool_t, cred_t *); 379 380 static bool_t connmgr_connect(struct cm_xprt *, queue_t *, struct netbuf *, 381 int, calllist_t *, int *, bool_t reconnect, 382 const struct timeval *, bool_t, cred_t *); 383 384 static void *connmgr_opt_getoff(mblk_t *mp, t_uscalar_t offset, 385 t_uscalar_t length, uint_t align_size); 386 static bool_t connmgr_setbufsz(calllist_t *e, queue_t *wq, cred_t *cr); 387 static bool_t connmgr_getopt_int(queue_t *wq, int level, int name, int *val, 388 calllist_t *e, cred_t *cr); 389 static bool_t connmgr_setopt_int(queue_t *wq, int level, int name, int val, 390 calllist_t *e, cred_t *cr); 391 static bool_t connmgr_setopt(queue_t *, int, int, calllist_t *, cred_t *cr); 392 static void connmgr_sndrel(struct cm_xprt *); 393 static void connmgr_snddis(struct cm_xprt *); 394 static void connmgr_close(struct cm_xprt *); 395 static void connmgr_release(struct cm_xprt *); 396 static struct cm_xprt *connmgr_wrapget(struct netbuf *, const struct timeval *, 397 cku_private_t *); 398 399 static struct cm_xprt *connmgr_get(struct netbuf *, const struct timeval *, 400 struct netbuf *, int, struct netbuf *, struct rpc_err *, dev_t, 401 bool_t, int, cred_t *); 402 403 static void connmgr_cancelconn(struct cm_xprt *); 404 static enum clnt_stat connmgr_cwait(struct cm_xprt *, const struct timeval *, 405 bool_t); 406 static void connmgr_dis_and_wait(struct cm_xprt *); 407 408 static int clnt_dispatch_send(queue_t *, mblk_t *, calllist_t *, uint_t, 409 uint_t); 410 411 static int clnt_delay(clock_t, bool_t); 412 413 static int waitforack(calllist_t *, t_scalar_t, const struct timeval *, bool_t); 414 415 /* 416 * Operations vector for TCP/IP based RPC 417 */ 418 static struct clnt_ops tcp_ops = { 419 clnt_cots_kcallit, /* do rpc call */ 420 clnt_cots_kabort, /* abort call */ 421 clnt_cots_kerror, /* return error status */ 422 clnt_cots_kfreeres, /* free results */ 423 clnt_cots_kdestroy, /* destroy rpc handle */ 424 clnt_cots_kcontrol, /* the ioctl() of rpc */ 425 clnt_cots_ksettimers, /* set retry timers */ 426 }; 427 428 static int rpc_kstat_instance = 0; /* keeps the current instance */ 429 /* number for the next kstat_create */ 430 431 static struct cm_xprt *cm_hd = NULL; 432 static kmutex_t connmgr_lock; /* for connection mngr's list of transports */ 433 434 extern kmutex_t clnt_max_msg_lock; 435 436 static calllist_t *clnt_pending = NULL; 437 extern kmutex_t clnt_pending_lock; 438 439 static int clnt_cots_hash_size = DEFAULT_HASH_SIZE; 440 441 static call_table_t *cots_call_ht; 442 443 static const struct rpc_cots_client { 444 kstat_named_t rccalls; 445 kstat_named_t rcbadcalls; 446 kstat_named_t rcbadxids; 447 kstat_named_t rctimeouts; 448 kstat_named_t rcnewcreds; 449 kstat_named_t rcbadverfs; 450 kstat_named_t rctimers; 451 kstat_named_t rccantconn; 452 kstat_named_t rcnomem; 453 kstat_named_t rcintrs; 454 } cots_rcstat_tmpl = { 455 { "calls", KSTAT_DATA_UINT64 }, 456 { "badcalls", KSTAT_DATA_UINT64 }, 457 { "badxids", KSTAT_DATA_UINT64 }, 458 { "timeouts", KSTAT_DATA_UINT64 }, 459 { "newcreds", KSTAT_DATA_UINT64 }, 460 { "badverfs", KSTAT_DATA_UINT64 }, 461 { "timers", KSTAT_DATA_UINT64 }, 462 { "cantconn", KSTAT_DATA_UINT64 }, 463 { "nomem", KSTAT_DATA_UINT64 }, 464 { "interrupts", KSTAT_DATA_UINT64 } 465 }; 466 467 #define COTSRCSTAT_INCR(p, x) \ 468 atomic_inc_64(&(p)->x.value.ui64) 469 470 #define CLNT_MAX_CONNS 1 /* concurrent connections between clnt/srvr */ 471 int clnt_max_conns = CLNT_MAX_CONNS; 472 473 #define CLNT_MIN_TIMEOUT 10 /* seconds to wait after we get a */ 474 /* connection reset */ 475 #define CLNT_MIN_CONNTIMEOUT 5 /* seconds to wait for a connection */ 476 477 478 int clnt_cots_min_tout = CLNT_MIN_TIMEOUT; 479 int clnt_cots_min_conntout = CLNT_MIN_CONNTIMEOUT; 480 481 /* 482 * Limit the number of times we will attempt to receive a reply without 483 * re-sending a response. 484 */ 485 #define CLNT_MAXRECV_WITHOUT_RETRY 3 486 uint_t clnt_cots_maxrecv = CLNT_MAXRECV_WITHOUT_RETRY; 487 488 uint_t *clnt_max_msg_sizep; 489 void (*clnt_stop_idle)(queue_t *wq); 490 491 #define ptoh(p) (&((p)->cku_client)) 492 #define htop(h) ((cku_private_t *)((h)->cl_private)) 493 494 /* 495 * Times to retry 496 */ 497 #define REFRESHES 2 /* authentication refreshes */ 498 499 /* 500 * The following is used to determine the global default behavior for 501 * COTS when binding to a local port. 502 * 503 * If the value is set to 1 the default will be to select a reserved 504 * (aka privileged) port, if the value is zero the default will be to 505 * use non-reserved ports. Users of kRPC may override this by using 506 * CLNT_CONTROL() and CLSET_BINDRESVPORT. 507 */ 508 int clnt_cots_do_bindresvport = 1; 509 510 static zone_key_t zone_cots_key; 511 512 /* 513 * Defaults TCP send and receive buffer size for RPC connections. 514 * These values can be tuned by /etc/system. 515 */ 516 int rpc_send_bufsz = 1024*1024; 517 int rpc_recv_bufsz = 1024*1024; 518 /* 519 * To use system-wide default for TCP send and receive buffer size, 520 * use /etc/system to set rpc_default_tcp_bufsz to 1: 521 * 522 * set rpcmod:rpc_default_tcp_bufsz=1 523 */ 524 int rpc_default_tcp_bufsz = 0; 525 526 /* 527 * We need to do this after all kernel threads in the zone have exited. 528 */ 529 /* ARGSUSED */ 530 static void 531 clnt_zone_destroy(zoneid_t zoneid, void *unused) 532 { 533 struct cm_xprt **cmp; 534 struct cm_xprt *cm_entry; 535 struct cm_xprt *freelist = NULL; 536 537 mutex_enter(&connmgr_lock); 538 cmp = &cm_hd; 539 while ((cm_entry = *cmp) != NULL) { 540 if (cm_entry->x_zoneid == zoneid) { 541 *cmp = cm_entry->x_next; 542 cm_entry->x_next = freelist; 543 freelist = cm_entry; 544 } else { 545 cmp = &cm_entry->x_next; 546 } 547 } 548 mutex_exit(&connmgr_lock); 549 while ((cm_entry = freelist) != NULL) { 550 freelist = cm_entry->x_next; 551 connmgr_close(cm_entry); 552 } 553 } 554 555 int 556 clnt_cots_kcreate(dev_t dev, struct netbuf *addr, int family, rpcprog_t prog, 557 rpcvers_t vers, uint_t max_msgsize, cred_t *cred, CLIENT **ncl) 558 { 559 CLIENT *h; 560 cku_private_t *p; 561 struct rpc_msg call_msg; 562 struct rpcstat *rpcstat; 563 564 RPCLOG(8, "clnt_cots_kcreate: prog %u\n", prog); 565 566 rpcstat = zone_getspecific(rpcstat_zone_key, rpc_zone()); 567 ASSERT(rpcstat != NULL); 568 569 /* Allocate and intialize the client handle. */ 570 p = kmem_zalloc(sizeof (*p), KM_SLEEP); 571 572 h = ptoh(p); 573 574 h->cl_private = (caddr_t)p; 575 h->cl_auth = authkern_create(); 576 h->cl_ops = &tcp_ops; 577 578 cv_init(&p->cku_call.call_cv, NULL, CV_DEFAULT, NULL); 579 mutex_init(&p->cku_call.call_lock, NULL, MUTEX_DEFAULT, NULL); 580 581 /* 582 * If the current sanity check size in rpcmod is smaller 583 * than the size needed, then increase the sanity check. 584 */ 585 if (max_msgsize != 0 && clnt_max_msg_sizep != NULL && 586 max_msgsize > *clnt_max_msg_sizep) { 587 mutex_enter(&clnt_max_msg_lock); 588 if (max_msgsize > *clnt_max_msg_sizep) 589 *clnt_max_msg_sizep = max_msgsize; 590 mutex_exit(&clnt_max_msg_lock); 591 } 592 593 p->cku_outbuflen = COTS_DEFAULT_ALLOCSIZE; 594 595 /* Preserialize the call message header */ 596 597 call_msg.rm_xid = 0; 598 call_msg.rm_direction = CALL; 599 call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; 600 call_msg.rm_call.cb_prog = prog; 601 call_msg.rm_call.cb_vers = vers; 602 603 xdrmem_create(&p->cku_outxdr, p->cku_rpchdr, WIRE_HDR_SIZE, XDR_ENCODE); 604 605 if (!xdr_callhdr(&p->cku_outxdr, &call_msg)) { 606 RPCLOG0(1, "clnt_cots_kcreate - Fatal header serialization " 607 "error\n"); 608 auth_destroy(h->cl_auth); 609 kmem_free(p, sizeof (cku_private_t)); 610 RPCLOG0(1, "clnt_cots_kcreate: create failed error EINVAL\n"); 611 return (EINVAL); /* XXX */ 612 } 613 614 /* 615 * The zalloc initialized the fields below. 616 * p->cku_xid = 0; 617 * p->cku_flags = 0; 618 * p->cku_srcaddr.len = 0; 619 * p->cku_srcaddr.maxlen = 0; 620 */ 621 622 p->cku_cred = cred; 623 p->cku_device = dev; 624 p->cku_addrfmly = family; 625 p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP); 626 p->cku_addr.maxlen = addr->maxlen; 627 p->cku_addr.len = addr->len; 628 bcopy(addr->buf, p->cku_addr.buf, addr->len); 629 p->cku_stats = rpcstat->rpc_cots_client; 630 p->cku_useresvport = -1; /* value is has not been set */ 631 632 *ncl = h; 633 return (0); 634 } 635 636 /*ARGSUSED*/ 637 static void 638 clnt_cots_kabort(CLIENT *h) 639 { 640 } 641 642 /* 643 * Return error info on this handle. 644 */ 645 static void 646 clnt_cots_kerror(CLIENT *h, struct rpc_err *err) 647 { 648 /* LINTED pointer alignment */ 649 cku_private_t *p = htop(h); 650 651 *err = p->cku_err; 652 } 653 654 static bool_t 655 clnt_cots_kfreeres(CLIENT *h, xdrproc_t xdr_res, caddr_t res_ptr) 656 { 657 /* LINTED pointer alignment */ 658 cku_private_t *p = htop(h); 659 XDR *xdrs; 660 661 xdrs = &(p->cku_outxdr); 662 xdrs->x_op = XDR_FREE; 663 return ((*xdr_res)(xdrs, res_ptr)); 664 } 665 666 static bool_t 667 clnt_cots_kcontrol(CLIENT *h, int cmd, char *arg) 668 { 669 cku_private_t *p = htop(h); 670 671 switch (cmd) { 672 case CLSET_PROGRESS: 673 p->cku_progress = TRUE; 674 return (TRUE); 675 676 case CLSET_XID: 677 if (arg == NULL) 678 return (FALSE); 679 680 p->cku_xid = *((uint32_t *)arg); 681 return (TRUE); 682 683 case CLGET_XID: 684 if (arg == NULL) 685 return (FALSE); 686 687 *((uint32_t *)arg) = p->cku_xid; 688 return (TRUE); 689 690 case CLSET_NODELAYONERR: 691 if (arg == NULL) 692 return (FALSE); 693 694 if (*((bool_t *)arg) == TRUE) { 695 p->cku_nodelayonerr = TRUE; 696 return (TRUE); 697 } 698 if (*((bool_t *)arg) == FALSE) { 699 p->cku_nodelayonerr = FALSE; 700 return (TRUE); 701 } 702 return (FALSE); 703 704 case CLGET_NODELAYONERR: 705 if (arg == NULL) 706 return (FALSE); 707 708 *((bool_t *)arg) = p->cku_nodelayonerr; 709 return (TRUE); 710 711 case CLSET_BINDRESVPORT: 712 if (arg == NULL) 713 return (FALSE); 714 715 if (*(int *)arg != 1 && *(int *)arg != 0) 716 return (FALSE); 717 718 p->cku_useresvport = *(int *)arg; 719 720 return (TRUE); 721 722 case CLGET_BINDRESVPORT: 723 if (arg == NULL) 724 return (FALSE); 725 726 *(int *)arg = p->cku_useresvport; 727 728 return (TRUE); 729 730 default: 731 return (FALSE); 732 } 733 } 734 735 /* 736 * Destroy rpc handle. Frees the space used for output buffer, 737 * private data, and handle structure. 738 */ 739 static void 740 clnt_cots_kdestroy(CLIENT *h) 741 { 742 /* LINTED pointer alignment */ 743 cku_private_t *p = htop(h); 744 calllist_t *call = &p->cku_call; 745 746 RPCLOG(8, "clnt_cots_kdestroy h: %p\n", (void *)h); 747 RPCLOG(8, "clnt_cots_kdestroy h: xid=0x%x\n", p->cku_xid); 748 749 if (p->cku_flags & CKU_ONQUEUE) { 750 RPCLOG(64, "clnt_cots_kdestroy h: removing call for xid 0x%x " 751 "from dispatch list\n", p->cku_xid); 752 call_table_remove(call); 753 } 754 755 if (call->call_reply) 756 freemsg(call->call_reply); 757 cv_destroy(&call->call_cv); 758 mutex_destroy(&call->call_lock); 759 760 kmem_free(p->cku_srcaddr.buf, p->cku_srcaddr.maxlen); 761 kmem_free(p->cku_addr.buf, p->cku_addr.maxlen); 762 kmem_free(p, sizeof (*p)); 763 } 764 765 static int clnt_cots_pulls; 766 #define RM_HDR_SIZE 4 /* record mark header size */ 767 768 /* 769 * Call remote procedure. 770 */ 771 static enum clnt_stat 772 clnt_cots_kcallit(CLIENT *h, rpcproc_t procnum, xdrproc_t xdr_args, 773 caddr_t argsp, xdrproc_t xdr_results, caddr_t resultsp, struct timeval wait) 774 { 775 /* LINTED pointer alignment */ 776 cku_private_t *p = htop(h); 777 calllist_t *call = &p->cku_call; 778 XDR *xdrs; 779 struct rpc_msg reply_msg; 780 mblk_t *mp; 781 #ifdef RPCDEBUG 782 clock_t time_sent; 783 #endif 784 struct netbuf *retryaddr; 785 struct cm_xprt *cm_entry = NULL; 786 queue_t *wq; 787 int len, waitsecs, max_waitsecs; 788 int mpsize; 789 int refreshes = REFRESHES; 790 int interrupted; 791 int tidu_size; 792 enum clnt_stat status; 793 struct timeval cwait; 794 bool_t delay_first = FALSE; 795 clock_t ticks, now; 796 797 RPCLOG(2, "clnt_cots_kcallit, procnum %u\n", procnum); 798 COTSRCSTAT_INCR(p->cku_stats, rccalls); 799 800 RPCLOG(2, "clnt_cots_kcallit: wait.tv_sec: %ld\n", wait.tv_sec); 801 RPCLOG(2, "clnt_cots_kcallit: wait.tv_usec: %ld\n", wait.tv_usec); 802 /* 803 * Bug ID 1240234: 804 * Look out for zero length timeouts. We don't want to 805 * wait zero seconds for a connection to be established. 806 */ 807 if (wait.tv_sec < clnt_cots_min_conntout) { 808 cwait.tv_sec = clnt_cots_min_conntout; 809 cwait.tv_usec = 0; 810 RPCLOG(8, "clnt_cots_kcallit: wait.tv_sec (%ld) too low,", 811 wait.tv_sec); 812 RPCLOG(8, " setting to: %d\n", clnt_cots_min_conntout); 813 } else { 814 cwait = wait; 815 } 816 817 call_again: 818 if (cm_entry) { 819 connmgr_release(cm_entry); 820 cm_entry = NULL; 821 } 822 823 mp = NULL; 824 825 /* 826 * If the call is not a retry, allocate a new xid and cache it 827 * for future retries. 828 * Bug ID 1246045: 829 * Treat call as a retry for purposes of binding the source 830 * port only if we actually attempted to send anything on 831 * the previous call. 832 */ 833 if (p->cku_xid == 0) { 834 p->cku_xid = alloc_xid(); 835 call->call_zoneid = rpc_zoneid(); 836 837 /* 838 * We need to ASSERT here that our xid != 0 because this 839 * determines whether or not our call record gets placed on 840 * the hash table or the linked list. By design, we mandate 841 * that RPC calls over cots must have xid's != 0, so we can 842 * ensure proper management of the hash table. 843 */ 844 ASSERT(p->cku_xid != 0); 845 846 retryaddr = NULL; 847 p->cku_flags &= ~CKU_SENT; 848 849 if (p->cku_flags & CKU_ONQUEUE) { 850 RPCLOG(8, "clnt_cots_kcallit: new call, dequeuing old" 851 " one (%p)\n", (void *)call); 852 call_table_remove(call); 853 p->cku_flags &= ~CKU_ONQUEUE; 854 RPCLOG(64, "clnt_cots_kcallit: removing call from " 855 "dispatch list because xid was zero (now 0x%x)\n", 856 p->cku_xid); 857 } 858 859 if (call->call_reply != NULL) { 860 freemsg(call->call_reply); 861 call->call_reply = NULL; 862 } 863 } else if (p->cku_srcaddr.buf == NULL || p->cku_srcaddr.len == 0) { 864 retryaddr = NULL; 865 866 } else if (p->cku_flags & CKU_SENT) { 867 retryaddr = &p->cku_srcaddr; 868 869 } else { 870 /* 871 * Bug ID 1246045: Nothing was sent, so set retryaddr to 872 * NULL and let connmgr_get() bind to any source port it 873 * can get. 874 */ 875 retryaddr = NULL; 876 } 877 878 RPCLOG(64, "clnt_cots_kcallit: xid = 0x%x", p->cku_xid); 879 RPCLOG(64, " flags = 0x%x\n", p->cku_flags); 880 881 p->cku_err.re_status = RPC_TIMEDOUT; 882 p->cku_err.re_errno = p->cku_err.re_terrno = 0; 883 884 cm_entry = connmgr_wrapget(retryaddr, &cwait, p); 885 886 if (cm_entry == NULL) { 887 RPCLOG(1, "clnt_cots_kcallit: can't connect status %s\n", 888 clnt_sperrno(p->cku_err.re_status)); 889 890 /* 891 * The reasons why we fail to create a connection are 892 * varied. In most cases we don't want the caller to 893 * immediately retry. This could have one or more 894 * bad effects. This includes flooding the net with 895 * connect requests to ports with no listener; a hard 896 * kernel loop due to all the "reserved" TCP ports being 897 * in use. 898 */ 899 delay_first = TRUE; 900 901 /* 902 * Even if we end up returning EINTR, we still count a 903 * a "can't connect", because the connection manager 904 * might have been committed to waiting for or timing out on 905 * a connection. 906 */ 907 COTSRCSTAT_INCR(p->cku_stats, rccantconn); 908 switch (p->cku_err.re_status) { 909 case RPC_INTR: 910 p->cku_err.re_errno = EINTR; 911 912 /* 913 * No need to delay because a UNIX signal(2) 914 * interrupted us. The caller likely won't 915 * retry the CLNT_CALL() and even if it does, 916 * we assume the caller knows what it is doing. 917 */ 918 delay_first = FALSE; 919 break; 920 921 case RPC_TIMEDOUT: 922 p->cku_err.re_errno = ETIMEDOUT; 923 924 /* 925 * No need to delay because timed out already 926 * on the connection request and assume that the 927 * transport time out is longer than our minimum 928 * timeout, or least not too much smaller. 929 */ 930 delay_first = FALSE; 931 break; 932 933 case RPC_SYSTEMERROR: 934 case RPC_TLIERROR: 935 /* 936 * We want to delay here because a transient 937 * system error has a better chance of going away 938 * if we delay a bit. If it's not transient, then 939 * we don't want end up in a hard kernel loop 940 * due to retries. 941 */ 942 ASSERT(p->cku_err.re_errno != 0); 943 break; 944 945 946 case RPC_CANTCONNECT: 947 /* 948 * RPC_CANTCONNECT is set on T_ERROR_ACK which 949 * implies some error down in the TCP layer or 950 * below. If cku_nodelayonerror is set then we 951 * assume the caller knows not to try too hard. 952 */ 953 RPCLOG0(8, "clnt_cots_kcallit: connection failed,"); 954 RPCLOG0(8, " re_status=RPC_CANTCONNECT,"); 955 RPCLOG(8, " re_errno=%d,", p->cku_err.re_errno); 956 RPCLOG(8, " cku_nodelayonerr=%d", p->cku_nodelayonerr); 957 if (p->cku_nodelayonerr == TRUE) 958 delay_first = FALSE; 959 960 p->cku_err.re_errno = EIO; 961 962 break; 963 964 case RPC_XPRTFAILED: 965 /* 966 * We want to delay here because we likely 967 * got a refused connection. 968 */ 969 if (p->cku_err.re_errno == 0) 970 p->cku_err.re_errno = EIO; 971 972 RPCLOG(1, "clnt_cots_kcallit: transport failed: %d\n", 973 p->cku_err.re_errno); 974 975 break; 976 977 default: 978 /* 979 * We delay here because it is better to err 980 * on the side of caution. If we got here then 981 * status could have been RPC_SUCCESS, but we 982 * know that we did not get a connection, so 983 * force the rpc status to RPC_CANTCONNECT. 984 */ 985 p->cku_err.re_status = RPC_CANTCONNECT; 986 p->cku_err.re_errno = EIO; 987 break; 988 } 989 if (delay_first == TRUE) 990 ticks = drv_sectohz(clnt_cots_min_tout); 991 goto cots_done; 992 } 993 994 /* 995 * If we've never sent any request on this connection (send count 996 * is zero, or the connection has been reset), cache the 997 * the connection's create time and send a request (possibly a retry) 998 */ 999 if ((p->cku_flags & CKU_SENT) == 0 || 1000 p->cku_ctime != cm_entry->x_ctime) { 1001 p->cku_ctime = cm_entry->x_ctime; 1002 1003 } else if ((p->cku_flags & CKU_SENT) && (p->cku_flags & CKU_ONQUEUE) && 1004 (call->call_reply != NULL || 1005 p->cku_recv_attempts < clnt_cots_maxrecv)) { 1006 1007 /* 1008 * If we've sent a request and our call is on the dispatch 1009 * queue and we haven't made too many receive attempts, then 1010 * don't re-send, just receive. 1011 */ 1012 p->cku_recv_attempts++; 1013 goto read_again; 1014 } 1015 1016 /* 1017 * Now we create the RPC request in a STREAMS message. We have to do 1018 * this after the call to connmgr_get so that we have the correct 1019 * TIDU size for the transport. 1020 */ 1021 tidu_size = cm_entry->x_tidu_size; 1022 len = MSG_OFFSET + MAX(tidu_size, RM_HDR_SIZE + WIRE_HDR_SIZE); 1023 1024 while ((mp = allocb(len, BPRI_MED)) == NULL) { 1025 if (strwaitbuf(len, BPRI_MED)) { 1026 p->cku_err.re_status = RPC_SYSTEMERROR; 1027 p->cku_err.re_errno = ENOSR; 1028 COTSRCSTAT_INCR(p->cku_stats, rcnomem); 1029 goto cots_done; 1030 } 1031 } 1032 xdrs = &p->cku_outxdr; 1033 xdrmblk_init(xdrs, mp, XDR_ENCODE, tidu_size); 1034 mpsize = MBLKSIZE(mp); 1035 ASSERT(mpsize >= len); 1036 ASSERT(mp->b_rptr == mp->b_datap->db_base); 1037 1038 /* 1039 * If the size of mblk is not appreciably larger than what we 1040 * asked, then resize the mblk to exactly len bytes. The reason for 1041 * this: suppose len is 1600 bytes, the tidu is 1460 bytes 1042 * (from TCP over ethernet), and the arguments to the RPC require 1043 * 2800 bytes. Ideally we want the protocol to render two 1044 * ~1400 byte segments over the wire. However if allocb() gives us a 2k 1045 * mblk, and we allocate a second mblk for the remainder, the protocol 1046 * module may generate 3 segments over the wire: 1047 * 1460 bytes for the first, 448 (2048 - 1600) for the second, and 1048 * 892 for the third. If we "waste" 448 bytes in the first mblk, 1049 * the XDR encoding will generate two ~1400 byte mblks, and the 1050 * protocol module is more likely to produce properly sized segments. 1051 */ 1052 if ((mpsize >> 1) <= len) 1053 mp->b_rptr += (mpsize - len); 1054 1055 /* 1056 * Adjust b_rptr to reserve space for the non-data protocol headers 1057 * any downstream modules might like to add, and for the 1058 * record marking header. 1059 */ 1060 mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE); 1061 1062 if (h->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { 1063 /* Copy in the preserialized RPC header information. */ 1064 bcopy(p->cku_rpchdr, mp->b_rptr, WIRE_HDR_SIZE); 1065 1066 /* Use XDR_SETPOS() to set the b_wptr to past the RPC header. */ 1067 XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base + 1068 WIRE_HDR_SIZE)); 1069 1070 ASSERT((mp->b_wptr - mp->b_rptr) == WIRE_HDR_SIZE); 1071 1072 /* Serialize the procedure number and the arguments. */ 1073 if ((!XDR_PUTINT32(xdrs, (int32_t *)&procnum)) || 1074 (!AUTH_MARSHALL(h->cl_auth, xdrs, p->cku_cred)) || 1075 (!(*xdr_args)(xdrs, argsp))) { 1076 p->cku_err.re_status = RPC_CANTENCODEARGS; 1077 p->cku_err.re_errno = EIO; 1078 goto cots_done; 1079 } 1080 1081 (*(uint32_t *)(mp->b_rptr)) = p->cku_xid; 1082 } else { 1083 uint32_t *uproc = (uint32_t *)&p->cku_rpchdr[WIRE_HDR_SIZE]; 1084 IXDR_PUT_U_INT32(uproc, procnum); 1085 1086 (*(uint32_t *)(&p->cku_rpchdr[0])) = p->cku_xid; 1087 1088 /* Use XDR_SETPOS() to set the b_wptr. */ 1089 XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base)); 1090 1091 /* Serialize the procedure number and the arguments. */ 1092 if (!AUTH_WRAP(h->cl_auth, p->cku_rpchdr, WIRE_HDR_SIZE+4, 1093 xdrs, xdr_args, argsp)) { 1094 p->cku_err.re_status = RPC_CANTENCODEARGS; 1095 p->cku_err.re_errno = EIO; 1096 goto cots_done; 1097 } 1098 } 1099 1100 RPCLOG(2, "clnt_cots_kcallit: connected, sending call, tidu_size %d\n", 1101 tidu_size); 1102 1103 wq = cm_entry->x_wq; 1104 waitsecs = 0; 1105 1106 dispatch_again: 1107 status = clnt_dispatch_send(wq, mp, call, p->cku_xid, 1108 (p->cku_flags & CKU_ONQUEUE)); 1109 1110 if ((status == RPC_CANTSEND) && (call->call_reason == ENOBUFS)) { 1111 /* 1112 * QFULL condition, allow some time for queue to drain 1113 * and try again. Give up after waiting for all timeout 1114 * specified for the call, or zone is going away. 1115 */ 1116 max_waitsecs = wait.tv_sec ? wait.tv_sec : clnt_cots_min_tout; 1117 if ((waitsecs++ < max_waitsecs) && 1118 !(zone_status_get(curproc->p_zone) >= 1119 ZONE_IS_SHUTTING_DOWN)) { 1120 1121 /* wait 1 sec for queue to drain */ 1122 if (clnt_delay(drv_sectohz(1), 1123 h->cl_nosignal) == EINTR) { 1124 p->cku_err.re_errno = EINTR; 1125 p->cku_err.re_status = RPC_INTR; 1126 1127 goto cots_done; 1128 } 1129 1130 /* and try again */ 1131 goto dispatch_again; 1132 } 1133 p->cku_err.re_status = status; 1134 p->cku_err.re_errno = call->call_reason; 1135 DTRACE_PROBE(krpc__e__clntcots__kcallit__cantsend); 1136 1137 goto cots_done; 1138 } 1139 1140 if (waitsecs) { 1141 /* adjust timeout to account for time wait to send */ 1142 wait.tv_sec -= waitsecs; 1143 if (wait.tv_sec < 0) { 1144 /* pick up reply on next retry */ 1145 wait.tv_sec = 0; 1146 } 1147 DTRACE_PROBE2(clnt_cots__sendwait, CLIENT *, h, 1148 int, waitsecs); 1149 } 1150 1151 RPCLOG(64, "clnt_cots_kcallit: sent call for xid 0x%x\n", 1152 (uint_t)p->cku_xid); 1153 p->cku_flags = (CKU_ONQUEUE|CKU_SENT); 1154 p->cku_recv_attempts = 1; 1155 1156 #ifdef RPCDEBUG 1157 time_sent = ddi_get_lbolt(); 1158 #endif 1159 1160 /* 1161 * Wait for a reply or a timeout. If there is no error or timeout, 1162 * (both indicated by call_status), call->call_reply will contain 1163 * the RPC reply message. 1164 */ 1165 read_again: 1166 mutex_enter(&call->call_lock); 1167 interrupted = 0; 1168 if (call->call_status == RPC_TIMEDOUT) { 1169 /* 1170 * Indicate that the lwp is not to be stopped while waiting 1171 * for this network traffic. This is to avoid deadlock while 1172 * debugging a process via /proc and also to avoid recursive 1173 * mutex_enter()s due to NFS page faults while stopping 1174 * (NFS holds locks when it calls here). 1175 */ 1176 clock_t cv_wait_ret; 1177 clock_t timout; 1178 clock_t oldlbolt; 1179 1180 klwp_t *lwp = ttolwp(curthread); 1181 1182 if (lwp != NULL) 1183 lwp->lwp_nostop++; 1184 1185 oldlbolt = ddi_get_lbolt(); 1186 timout = drv_sectohz(wait.tv_sec) + 1187 drv_usectohz(wait.tv_usec) + oldlbolt; 1188 /* 1189 * Iterate until the call_status is changed to something 1190 * other that RPC_TIMEDOUT, or if cv_timedwait_sig() returns 1191 * something <=0 zero. The latter means that we timed 1192 * out. 1193 */ 1194 if (h->cl_nosignal) 1195 while ((cv_wait_ret = cv_timedwait(&call->call_cv, 1196 &call->call_lock, timout)) > 0 && 1197 call->call_status == RPC_TIMEDOUT) 1198 ; 1199 else 1200 while ((cv_wait_ret = cv_timedwait_sig( 1201 &call->call_cv, 1202 &call->call_lock, timout)) > 0 && 1203 call->call_status == RPC_TIMEDOUT) 1204 ; 1205 1206 switch (cv_wait_ret) { 1207 case 0: 1208 /* 1209 * If we got out of the above loop with 1210 * cv_timedwait_sig() returning 0, then we were 1211 * interrupted regardless what call_status is. 1212 */ 1213 interrupted = 1; 1214 break; 1215 case -1: 1216 /* cv_timedwait_sig() timed out */ 1217 break; 1218 default: 1219 1220 /* 1221 * We were cv_signaled(). If we didn't 1222 * get a successful call_status and returned 1223 * before time expired, delay up to clnt_cots_min_tout 1224 * seconds so that the caller doesn't immediately 1225 * try to call us again and thus force the 1226 * same condition that got us here (such 1227 * as a RPC_XPRTFAILED due to the server not 1228 * listening on the end-point. 1229 */ 1230 if (call->call_status != RPC_SUCCESS) { 1231 clock_t curlbolt; 1232 clock_t diff; 1233 1234 curlbolt = ddi_get_lbolt(); 1235 ticks = drv_sectohz(clnt_cots_min_tout); 1236 diff = curlbolt - oldlbolt; 1237 if (diff < ticks) { 1238 delay_first = TRUE; 1239 if (diff > 0) 1240 ticks -= diff; 1241 } 1242 } 1243 break; 1244 } 1245 1246 if (lwp != NULL) 1247 lwp->lwp_nostop--; 1248 } 1249 /* 1250 * Get the reply message, if any. This will be freed at the end 1251 * whether or not an error occurred. 1252 */ 1253 mp = call->call_reply; 1254 call->call_reply = NULL; 1255 1256 /* 1257 * call_err is the error info when the call is on dispatch queue. 1258 * cku_err is the error info returned to the caller. 1259 * Sync cku_err with call_err for local message processing. 1260 */ 1261 1262 status = call->call_status; 1263 p->cku_err = call->call_err; 1264 mutex_exit(&call->call_lock); 1265 1266 if (status != RPC_SUCCESS) { 1267 switch (status) { 1268 case RPC_TIMEDOUT: 1269 now = ddi_get_lbolt(); 1270 if (interrupted) { 1271 COTSRCSTAT_INCR(p->cku_stats, rcintrs); 1272 p->cku_err.re_status = RPC_INTR; 1273 p->cku_err.re_errno = EINTR; 1274 RPCLOG(1, "clnt_cots_kcallit: xid 0x%x", 1275 p->cku_xid); 1276 RPCLOG(1, "signal interrupted at %ld", now); 1277 RPCLOG(1, ", was sent at %ld\n", time_sent); 1278 } else { 1279 COTSRCSTAT_INCR(p->cku_stats, rctimeouts); 1280 p->cku_err.re_errno = ETIMEDOUT; 1281 RPCLOG(1, "clnt_cots_kcallit: timed out at %ld", 1282 now); 1283 RPCLOG(1, ", was sent at %ld\n", time_sent); 1284 } 1285 break; 1286 1287 case RPC_XPRTFAILED: 1288 if (p->cku_err.re_errno == 0) 1289 p->cku_err.re_errno = EIO; 1290 1291 RPCLOG(1, "clnt_cots_kcallit: transport failed: %d\n", 1292 p->cku_err.re_errno); 1293 break; 1294 1295 case RPC_SYSTEMERROR: 1296 ASSERT(p->cku_err.re_errno); 1297 RPCLOG(1, "clnt_cots_kcallit: system error: %d\n", 1298 p->cku_err.re_errno); 1299 break; 1300 1301 default: 1302 p->cku_err.re_status = RPC_SYSTEMERROR; 1303 p->cku_err.re_errno = EIO; 1304 RPCLOG(1, "clnt_cots_kcallit: error: %s\n", 1305 clnt_sperrno(status)); 1306 break; 1307 } 1308 if (p->cku_err.re_status != RPC_TIMEDOUT) { 1309 1310 if (p->cku_flags & CKU_ONQUEUE) { 1311 call_table_remove(call); 1312 p->cku_flags &= ~CKU_ONQUEUE; 1313 } 1314 1315 RPCLOG(64, "clnt_cots_kcallit: non TIMEOUT so xid 0x%x " 1316 "taken off dispatch list\n", p->cku_xid); 1317 if (call->call_reply) { 1318 freemsg(call->call_reply); 1319 call->call_reply = NULL; 1320 } 1321 } else if (wait.tv_sec != 0) { 1322 /* 1323 * We've sent the request over TCP and so we have 1324 * every reason to believe it will get 1325 * delivered. In which case returning a timeout is not 1326 * appropriate. 1327 */ 1328 if (p->cku_progress == TRUE && 1329 p->cku_recv_attempts < clnt_cots_maxrecv) { 1330 p->cku_err.re_status = RPC_INPROGRESS; 1331 } 1332 } 1333 goto cots_done; 1334 } 1335 1336 xdrs = &p->cku_inxdr; 1337 xdrmblk_init(xdrs, mp, XDR_DECODE, 0); 1338 1339 reply_msg.rm_direction = REPLY; 1340 reply_msg.rm_reply.rp_stat = MSG_ACCEPTED; 1341 reply_msg.acpted_rply.ar_stat = SUCCESS; 1342 1343 reply_msg.acpted_rply.ar_verf = _null_auth; 1344 /* 1345 * xdr_results will be done in AUTH_UNWRAP. 1346 */ 1347 reply_msg.acpted_rply.ar_results.where = NULL; 1348 reply_msg.acpted_rply.ar_results.proc = xdr_void; 1349 1350 if (xdr_replymsg(xdrs, &reply_msg)) { 1351 enum clnt_stat re_status; 1352 1353 _seterr_reply(&reply_msg, &p->cku_err); 1354 1355 re_status = p->cku_err.re_status; 1356 if (re_status == RPC_SUCCESS) { 1357 /* 1358 * Reply is good, check auth. 1359 */ 1360 if (!AUTH_VALIDATE(h->cl_auth, 1361 &reply_msg.acpted_rply.ar_verf)) { 1362 COTSRCSTAT_INCR(p->cku_stats, rcbadverfs); 1363 RPCLOG0(1, "clnt_cots_kcallit: validation " 1364 "failure\n"); 1365 freemsg(mp); 1366 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 1367 mutex_enter(&call->call_lock); 1368 if (call->call_reply == NULL) 1369 call->call_status = RPC_TIMEDOUT; 1370 mutex_exit(&call->call_lock); 1371 goto read_again; 1372 } else if (!AUTH_UNWRAP(h->cl_auth, xdrs, 1373 xdr_results, resultsp)) { 1374 RPCLOG0(1, "clnt_cots_kcallit: validation " 1375 "failure (unwrap)\n"); 1376 p->cku_err.re_status = RPC_CANTDECODERES; 1377 p->cku_err.re_errno = EIO; 1378 } 1379 } else { 1380 /* set errno in case we can't recover */ 1381 if (re_status != RPC_VERSMISMATCH && 1382 re_status != RPC_AUTHERROR && 1383 re_status != RPC_PROGVERSMISMATCH) 1384 p->cku_err.re_errno = EIO; 1385 1386 if (re_status == RPC_AUTHERROR) { 1387 /* 1388 * Maybe our credential need to be refreshed 1389 */ 1390 if (cm_entry) { 1391 /* 1392 * There is the potential that the 1393 * cm_entry has/will be marked dead, 1394 * so drop the connection altogether, 1395 * force REFRESH to establish new 1396 * connection. 1397 */ 1398 connmgr_cancelconn(cm_entry); 1399 cm_entry = NULL; 1400 } 1401 1402 (void) xdr_rpc_free_verifier(xdrs, 1403 &reply_msg); 1404 1405 if (p->cku_flags & CKU_ONQUEUE) { 1406 call_table_remove(call); 1407 p->cku_flags &= ~CKU_ONQUEUE; 1408 } 1409 RPCLOG(64, 1410 "clnt_cots_kcallit: AUTH_ERROR, xid" 1411 " 0x%x removed off dispatch list\n", 1412 p->cku_xid); 1413 if (call->call_reply) { 1414 freemsg(call->call_reply); 1415 call->call_reply = NULL; 1416 } 1417 1418 if ((refreshes > 0) && 1419 AUTH_REFRESH(h->cl_auth, &reply_msg, 1420 p->cku_cred)) { 1421 refreshes--; 1422 freemsg(mp); 1423 mp = NULL; 1424 1425 COTSRCSTAT_INCR(p->cku_stats, 1426 rcbadcalls); 1427 COTSRCSTAT_INCR(p->cku_stats, 1428 rcnewcreds); 1429 goto call_again; 1430 } 1431 1432 /* 1433 * We have used the client handle to 1434 * do an AUTH_REFRESH and the RPC status may 1435 * be set to RPC_SUCCESS; Let's make sure to 1436 * set it to RPC_AUTHERROR. 1437 */ 1438 p->cku_err.re_status = RPC_AUTHERROR; 1439 1440 /* 1441 * Map recoverable and unrecoverable 1442 * authentication errors to appropriate errno 1443 */ 1444 switch (p->cku_err.re_why) { 1445 case AUTH_TOOWEAK: 1446 /* 1447 * This could be a failure where the 1448 * server requires use of a reserved 1449 * port, check and optionally set the 1450 * client handle useresvport trying 1451 * one more time. Next go round we 1452 * fall out with the tooweak error. 1453 */ 1454 if (p->cku_useresvport != 1) { 1455 p->cku_useresvport = 1; 1456 p->cku_xid = 0; 1457 freemsg(mp); 1458 mp = NULL; 1459 goto call_again; 1460 } 1461 /* FALLTHRU */ 1462 case AUTH_BADCRED: 1463 case AUTH_BADVERF: 1464 case AUTH_INVALIDRESP: 1465 case AUTH_FAILED: 1466 case RPCSEC_GSS_NOCRED: 1467 case RPCSEC_GSS_FAILED: 1468 p->cku_err.re_errno = EACCES; 1469 break; 1470 case AUTH_REJECTEDCRED: 1471 case AUTH_REJECTEDVERF: 1472 default: p->cku_err.re_errno = EIO; 1473 break; 1474 } 1475 RPCLOG(1, "clnt_cots_kcallit : authentication" 1476 " failed with RPC_AUTHERROR of type %d\n", 1477 (int)p->cku_err.re_why); 1478 goto cots_done; 1479 } 1480 } 1481 } else { 1482 /* reply didn't decode properly. */ 1483 p->cku_err.re_status = RPC_CANTDECODERES; 1484 p->cku_err.re_errno = EIO; 1485 RPCLOG0(1, "clnt_cots_kcallit: decode failure\n"); 1486 } 1487 1488 (void) xdr_rpc_free_verifier(xdrs, &reply_msg); 1489 1490 if (p->cku_flags & CKU_ONQUEUE) { 1491 call_table_remove(call); 1492 p->cku_flags &= ~CKU_ONQUEUE; 1493 } 1494 1495 RPCLOG(64, "clnt_cots_kcallit: xid 0x%x taken off dispatch list", 1496 p->cku_xid); 1497 RPCLOG(64, " status is %s\n", clnt_sperrno(p->cku_err.re_status)); 1498 cots_done: 1499 if (cm_entry) 1500 connmgr_release(cm_entry); 1501 1502 if (mp != NULL) 1503 freemsg(mp); 1504 if ((p->cku_flags & CKU_ONQUEUE) == 0 && call->call_reply) { 1505 freemsg(call->call_reply); 1506 call->call_reply = NULL; 1507 } 1508 if (p->cku_err.re_status != RPC_SUCCESS) { 1509 RPCLOG0(1, "clnt_cots_kcallit: tail-end failure\n"); 1510 COTSRCSTAT_INCR(p->cku_stats, rcbadcalls); 1511 } 1512 1513 /* 1514 * No point in delaying if the zone is going away. 1515 */ 1516 if (delay_first == TRUE && 1517 !(zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN)) { 1518 if (clnt_delay(ticks, h->cl_nosignal) == EINTR) { 1519 p->cku_err.re_errno = EINTR; 1520 p->cku_err.re_status = RPC_INTR; 1521 } 1522 } 1523 return (p->cku_err.re_status); 1524 } 1525 1526 /* 1527 * Kinit routine for cots. This sets up the correct operations in 1528 * the client handle, as the handle may have previously been a clts 1529 * handle, and clears the xid field so there is no way a new call 1530 * could be mistaken for a retry. It also sets in the handle the 1531 * information that is passed at create/kinit time but needed at 1532 * call time, as cots creates the transport at call time - device, 1533 * address of the server, protocol family. 1534 */ 1535 void 1536 clnt_cots_kinit(CLIENT *h, dev_t dev, int family, struct netbuf *addr, 1537 int max_msgsize, cred_t *cred) 1538 { 1539 /* LINTED pointer alignment */ 1540 cku_private_t *p = htop(h); 1541 calllist_t *call = &p->cku_call; 1542 1543 h->cl_ops = &tcp_ops; 1544 if (p->cku_flags & CKU_ONQUEUE) { 1545 call_table_remove(call); 1546 p->cku_flags &= ~CKU_ONQUEUE; 1547 RPCLOG(64, "clnt_cots_kinit: removing call for xid 0x%x from" 1548 " dispatch list\n", p->cku_xid); 1549 } 1550 1551 if (call->call_reply != NULL) { 1552 freemsg(call->call_reply); 1553 call->call_reply = NULL; 1554 } 1555 1556 call->call_bucket = NULL; 1557 call->call_hash = 0; 1558 1559 /* 1560 * We don't clear cku_flags here, because clnt_cots_kcallit() 1561 * takes care of handling the cku_flags reset. 1562 */ 1563 p->cku_xid = 0; 1564 p->cku_device = dev; 1565 p->cku_addrfmly = family; 1566 p->cku_cred = cred; 1567 1568 if (p->cku_addr.maxlen < addr->len) { 1569 if (p->cku_addr.maxlen != 0 && p->cku_addr.buf != NULL) 1570 kmem_free(p->cku_addr.buf, p->cku_addr.maxlen); 1571 p->cku_addr.buf = kmem_zalloc(addr->maxlen, KM_SLEEP); 1572 p->cku_addr.maxlen = addr->maxlen; 1573 } 1574 1575 p->cku_addr.len = addr->len; 1576 bcopy(addr->buf, p->cku_addr.buf, addr->len); 1577 1578 /* 1579 * If the current sanity check size in rpcmod is smaller 1580 * than the size needed, then increase the sanity check. 1581 */ 1582 if (max_msgsize != 0 && clnt_max_msg_sizep != NULL && 1583 max_msgsize > *clnt_max_msg_sizep) { 1584 mutex_enter(&clnt_max_msg_lock); 1585 if (max_msgsize > *clnt_max_msg_sizep) 1586 *clnt_max_msg_sizep = max_msgsize; 1587 mutex_exit(&clnt_max_msg_lock); 1588 } 1589 } 1590 1591 /* 1592 * ksettimers is a no-op for cots, with the exception of setting the xid. 1593 */ 1594 /* ARGSUSED */ 1595 static int 1596 clnt_cots_ksettimers(CLIENT *h, struct rpc_timers *t, struct rpc_timers *all, 1597 int minimum, void (*feedback)(int, int, caddr_t), caddr_t arg, 1598 uint32_t xid) 1599 { 1600 /* LINTED pointer alignment */ 1601 cku_private_t *p = htop(h); 1602 1603 if (xid) 1604 p->cku_xid = xid; 1605 COTSRCSTAT_INCR(p->cku_stats, rctimers); 1606 return (0); 1607 } 1608 1609 extern void rpc_poptimod(struct vnode *); 1610 extern int kstr_push(struct vnode *, char *); 1611 1612 int 1613 conn_kstat_update(kstat_t *ksp, int rw) 1614 { 1615 struct cm_xprt *cm_entry; 1616 struct cm_kstat_xprt *cm_ksp_data; 1617 uchar_t *b; 1618 char *fbuf; 1619 1620 if (rw == KSTAT_WRITE) 1621 return (EACCES); 1622 if (ksp == NULL || ksp->ks_private == NULL) 1623 return (EIO); 1624 cm_entry = (struct cm_xprt *)ksp->ks_private; 1625 cm_ksp_data = (struct cm_kstat_xprt *)ksp->ks_data; 1626 1627 cm_ksp_data->x_wq.value.ui32 = (uint32_t)(uintptr_t)cm_entry->x_wq; 1628 cm_ksp_data->x_family.value.ui32 = cm_entry->x_family; 1629 cm_ksp_data->x_rdev.value.ui32 = (uint32_t)cm_entry->x_rdev; 1630 cm_ksp_data->x_time.value.ui32 = cm_entry->x_time; 1631 cm_ksp_data->x_ref.value.ui32 = cm_entry->x_ref; 1632 cm_ksp_data->x_state.value.ui32 = cm_entry->x_state_flags; 1633 1634 if (cm_entry->x_server.buf) { 1635 fbuf = cm_ksp_data->x_server.value.str.addr.ptr; 1636 if (cm_entry->x_family == AF_INET && 1637 cm_entry->x_server.len == 1638 sizeof (struct sockaddr_in)) { 1639 struct sockaddr_in *sa; 1640 sa = (struct sockaddr_in *) 1641 cm_entry->x_server.buf; 1642 b = (uchar_t *)&sa->sin_addr; 1643 (void) sprintf(fbuf, 1644 "%03d.%03d.%03d.%03d", b[0] & 0xFF, b[1] & 0xFF, 1645 b[2] & 0xFF, b[3] & 0xFF); 1646 cm_ksp_data->x_port.value.ui32 = 1647 (uint32_t)sa->sin_port; 1648 } else if (cm_entry->x_family == AF_INET6 && 1649 cm_entry->x_server.len >= 1650 sizeof (struct sockaddr_in6)) { 1651 /* extract server IP address & port */ 1652 struct sockaddr_in6 *sin6; 1653 sin6 = (struct sockaddr_in6 *)cm_entry->x_server.buf; 1654 (void) kinet_ntop6((uchar_t *)&sin6->sin6_addr, fbuf, 1655 INET6_ADDRSTRLEN); 1656 cm_ksp_data->x_port.value.ui32 = sin6->sin6_port; 1657 } else { 1658 struct sockaddr_in *sa; 1659 1660 sa = (struct sockaddr_in *)cm_entry->x_server.buf; 1661 b = (uchar_t *)&sa->sin_addr; 1662 (void) sprintf(fbuf, 1663 "%03d.%03d.%03d.%03d", b[0] & 0xFF, b[1] & 0xFF, 1664 b[2] & 0xFF, b[3] & 0xFF); 1665 } 1666 KSTAT_NAMED_STR_BUFLEN(&cm_ksp_data->x_server) = 1667 strlen(fbuf) + 1; 1668 } 1669 1670 return (0); 1671 } 1672 1673 1674 /* 1675 * We want a version of delay which is interruptible by a UNIX signal 1676 * Return EINTR if an interrupt occured. 1677 */ 1678 static int 1679 clnt_delay(clock_t ticks, bool_t nosignal) 1680 { 1681 if (nosignal == TRUE) { 1682 delay(ticks); 1683 return (0); 1684 } 1685 return (delay_sig(ticks)); 1686 } 1687 1688 /* 1689 * Wait for a connection until a timeout, or until we are 1690 * signalled that there has been a connection state change. 1691 */ 1692 static enum clnt_stat 1693 connmgr_cwait(struct cm_xprt *cm_entry, const struct timeval *waitp, 1694 bool_t nosignal) 1695 { 1696 bool_t interrupted; 1697 clock_t timout, cv_stat; 1698 enum clnt_stat clstat; 1699 unsigned int old_state; 1700 1701 ASSERT(MUTEX_HELD(&connmgr_lock)); 1702 /* 1703 * We wait for the transport connection to be made, or an 1704 * indication that it could not be made. 1705 */ 1706 clstat = RPC_TIMEDOUT; 1707 interrupted = FALSE; 1708 1709 old_state = cm_entry->x_state_flags; 1710 /* 1711 * Now loop until cv_timedwait{_sig} returns because of 1712 * a signal(0) or timeout(-1) or cv_signal(>0). But it may be 1713 * cv_signalled for various other reasons too. So loop 1714 * until there is a state change on the connection. 1715 */ 1716 1717 timout = drv_sectohz(waitp->tv_sec) + 1718 drv_usectohz(waitp->tv_usec) + ddi_get_lbolt(); 1719 1720 if (nosignal) { 1721 while ((cv_stat = cv_timedwait(&cm_entry->x_conn_cv, 1722 &connmgr_lock, timout)) > 0 && 1723 cm_entry->x_state_flags == old_state) 1724 ; 1725 } else { 1726 while ((cv_stat = cv_timedwait_sig(&cm_entry->x_conn_cv, 1727 &connmgr_lock, timout)) > 0 && 1728 cm_entry->x_state_flags == old_state) 1729 ; 1730 1731 if (cv_stat == 0) /* got intr signal? */ 1732 interrupted = TRUE; 1733 } 1734 1735 if ((cm_entry->x_state_flags & (X_BADSTATES|X_CONNECTED)) == 1736 X_CONNECTED) { 1737 clstat = RPC_SUCCESS; 1738 } else { 1739 if (interrupted == TRUE) 1740 clstat = RPC_INTR; 1741 RPCLOG(1, "connmgr_cwait: can't connect, error: %s\n", 1742 clnt_sperrno(clstat)); 1743 } 1744 1745 return (clstat); 1746 } 1747 1748 /* 1749 * Primary interface for how RPC grabs a connection. 1750 */ 1751 static struct cm_xprt * 1752 connmgr_wrapget( 1753 struct netbuf *retryaddr, 1754 const struct timeval *waitp, 1755 cku_private_t *p) 1756 { 1757 struct cm_xprt *cm_entry; 1758 1759 cm_entry = connmgr_get(retryaddr, waitp, &p->cku_addr, p->cku_addrfmly, 1760 &p->cku_srcaddr, &p->cku_err, p->cku_device, 1761 p->cku_client.cl_nosignal, p->cku_useresvport, p->cku_cred); 1762 1763 if (cm_entry == NULL) { 1764 /* 1765 * Re-map the call status to RPC_INTR if the err code is 1766 * EINTR. This can happen if calls status is RPC_TLIERROR. 1767 * However, don't re-map if signalling has been turned off. 1768 * XXX Really need to create a separate thread whenever 1769 * there isn't an existing connection. 1770 */ 1771 if (p->cku_err.re_errno == EINTR) { 1772 if (p->cku_client.cl_nosignal == TRUE) 1773 p->cku_err.re_errno = EIO; 1774 else 1775 p->cku_err.re_status = RPC_INTR; 1776 } 1777 } 1778 1779 return (cm_entry); 1780 } 1781 1782 /* 1783 * Obtains a transport to the server specified in addr. If a suitable transport 1784 * does not already exist in the list of cached transports, a new connection 1785 * is created, connected, and added to the list. The connection is for sending 1786 * only - the reply message may come back on another transport connection. 1787 * 1788 * To implement round-robin load balancing with multiple client connections, 1789 * the last entry on the list is always selected. Once the entry is selected 1790 * it's re-inserted to the head of the list. 1791 */ 1792 static struct cm_xprt * 1793 connmgr_get( 1794 struct netbuf *retryaddr, 1795 const struct timeval *waitp, /* changed to a ptr to converse stack */ 1796 struct netbuf *destaddr, 1797 int addrfmly, 1798 struct netbuf *srcaddr, 1799 struct rpc_err *rpcerr, 1800 dev_t device, 1801 bool_t nosignal, 1802 int useresvport, 1803 cred_t *cr) 1804 { 1805 struct cm_xprt *cm_entry; 1806 struct cm_xprt *lru_entry; 1807 struct cm_xprt **cmp, **prev; 1808 queue_t *wq; 1809 TIUSER *tiptr; 1810 int i; 1811 int retval; 1812 int tidu_size; 1813 bool_t connected; 1814 zoneid_t zoneid = rpc_zoneid(); 1815 1816 /* 1817 * If the call is not a retry, look for a transport entry that 1818 * goes to the server of interest. 1819 */ 1820 mutex_enter(&connmgr_lock); 1821 1822 if (retryaddr == NULL) { 1823 use_new_conn: 1824 i = 0; 1825 cm_entry = lru_entry = NULL; 1826 1827 prev = cmp = &cm_hd; 1828 while ((cm_entry = *cmp) != NULL) { 1829 ASSERT(cm_entry != cm_entry->x_next); 1830 /* 1831 * Garbage collect conections that are marked 1832 * for needs disconnect. 1833 */ 1834 if (cm_entry->x_needdis) { 1835 CONN_HOLD(cm_entry); 1836 connmgr_dis_and_wait(cm_entry); 1837 connmgr_release(cm_entry); 1838 /* 1839 * connmgr_lock could have been 1840 * dropped for the disconnect 1841 * processing so start over. 1842 */ 1843 goto use_new_conn; 1844 } 1845 1846 /* 1847 * Garbage collect the dead connections that have 1848 * no threads working on them. 1849 */ 1850 if ((cm_entry->x_state_flags & (X_DEAD|X_THREAD)) == 1851 X_DEAD) { 1852 mutex_enter(&cm_entry->x_lock); 1853 if (cm_entry->x_ref != 0) { 1854 /* 1855 * Currently in use. 1856 * Cleanup later. 1857 */ 1858 cmp = &cm_entry->x_next; 1859 mutex_exit(&cm_entry->x_lock); 1860 continue; 1861 } 1862 mutex_exit(&cm_entry->x_lock); 1863 *cmp = cm_entry->x_next; 1864 mutex_exit(&connmgr_lock); 1865 connmgr_close(cm_entry); 1866 mutex_enter(&connmgr_lock); 1867 goto use_new_conn; 1868 } 1869 1870 1871 if ((cm_entry->x_state_flags & X_BADSTATES) == 0 && 1872 cm_entry->x_zoneid == zoneid && 1873 cm_entry->x_rdev == device && 1874 destaddr->len == cm_entry->x_server.len && 1875 bcmp(destaddr->buf, cm_entry->x_server.buf, 1876 destaddr->len) == 0) { 1877 /* 1878 * If the matching entry isn't connected, 1879 * attempt to reconnect it. 1880 */ 1881 if (cm_entry->x_connected == FALSE) { 1882 /* 1883 * We don't go through trying 1884 * to find the least recently 1885 * used connected because 1886 * connmgr_reconnect() briefly 1887 * dropped the connmgr_lock, 1888 * allowing a window for our 1889 * accounting to be messed up. 1890 * In any case, a re-connected 1891 * connection is as good as 1892 * a LRU connection. 1893 */ 1894 return (connmgr_wrapconnect(cm_entry, 1895 waitp, destaddr, addrfmly, srcaddr, 1896 rpcerr, TRUE, nosignal, cr)); 1897 } 1898 i++; 1899 1900 /* keep track of the last entry */ 1901 lru_entry = cm_entry; 1902 prev = cmp; 1903 } 1904 cmp = &cm_entry->x_next; 1905 } 1906 1907 if (i > clnt_max_conns) { 1908 RPCLOG(8, "connmgr_get: too many conns, dooming entry" 1909 " %p\n", (void *)lru_entry->x_tiptr); 1910 lru_entry->x_doomed = TRUE; 1911 goto use_new_conn; 1912 } 1913 1914 /* 1915 * If we are at the maximum number of connections to 1916 * the server, hand back the least recently used one. 1917 */ 1918 if (i == clnt_max_conns) { 1919 /* 1920 * Copy into the handle the source address of 1921 * the connection, which we will use in case of 1922 * a later retry. 1923 */ 1924 if (srcaddr->len != lru_entry->x_src.len) { 1925 if (srcaddr->len > 0) 1926 kmem_free(srcaddr->buf, 1927 srcaddr->maxlen); 1928 srcaddr->buf = kmem_zalloc( 1929 lru_entry->x_src.len, KM_SLEEP); 1930 srcaddr->maxlen = srcaddr->len = 1931 lru_entry->x_src.len; 1932 } 1933 bcopy(lru_entry->x_src.buf, srcaddr->buf, srcaddr->len); 1934 RPCLOG(2, "connmgr_get: call going out on %p\n", 1935 (void *)lru_entry); 1936 lru_entry->x_time = ddi_get_lbolt(); 1937 CONN_HOLD(lru_entry); 1938 1939 if ((i > 1) && (prev != &cm_hd)) { 1940 /* 1941 * remove and re-insert entry at head of list. 1942 */ 1943 *prev = lru_entry->x_next; 1944 lru_entry->x_next = cm_hd; 1945 cm_hd = lru_entry; 1946 } 1947 1948 mutex_exit(&connmgr_lock); 1949 return (lru_entry); 1950 } 1951 1952 } else { 1953 /* 1954 * This is the retry case (retryaddr != NULL). Retries must 1955 * be sent on the same source port as the original call. 1956 */ 1957 1958 /* 1959 * Walk the list looking for a connection with a source address 1960 * that matches the retry address. 1961 */ 1962 start_retry_loop: 1963 cmp = &cm_hd; 1964 while ((cm_entry = *cmp) != NULL) { 1965 ASSERT(cm_entry != cm_entry->x_next); 1966 1967 /* 1968 * determine if this connection matches the passed 1969 * in retry address. If it does not match, advance 1970 * to the next element on the list. 1971 */ 1972 if (zoneid != cm_entry->x_zoneid || 1973 device != cm_entry->x_rdev || 1974 retryaddr->len != cm_entry->x_src.len || 1975 bcmp(retryaddr->buf, cm_entry->x_src.buf, 1976 retryaddr->len) != 0) { 1977 cmp = &cm_entry->x_next; 1978 continue; 1979 } 1980 /* 1981 * Garbage collect conections that are marked 1982 * for needs disconnect. 1983 */ 1984 if (cm_entry->x_needdis) { 1985 CONN_HOLD(cm_entry); 1986 connmgr_dis_and_wait(cm_entry); 1987 connmgr_release(cm_entry); 1988 /* 1989 * connmgr_lock could have been 1990 * dropped for the disconnect 1991 * processing so start over. 1992 */ 1993 goto start_retry_loop; 1994 } 1995 /* 1996 * Garbage collect the dead connections that have 1997 * no threads working on them. 1998 */ 1999 if ((cm_entry->x_state_flags & (X_DEAD|X_THREAD)) == 2000 X_DEAD) { 2001 mutex_enter(&cm_entry->x_lock); 2002 if (cm_entry->x_ref != 0) { 2003 /* 2004 * Currently in use. 2005 * Cleanup later. 2006 */ 2007 cmp = &cm_entry->x_next; 2008 mutex_exit(&cm_entry->x_lock); 2009 continue; 2010 } 2011 mutex_exit(&cm_entry->x_lock); 2012 *cmp = cm_entry->x_next; 2013 mutex_exit(&connmgr_lock); 2014 connmgr_close(cm_entry); 2015 mutex_enter(&connmgr_lock); 2016 goto start_retry_loop; 2017 } 2018 2019 /* 2020 * Sanity check: if the connection with our source 2021 * port is going to some other server, something went 2022 * wrong, as we never delete connections (i.e. release 2023 * ports) unless they have been idle. In this case, 2024 * it is probably better to send the call out using 2025 * a new source address than to fail it altogether, 2026 * since that port may never be released. 2027 */ 2028 if (destaddr->len != cm_entry->x_server.len || 2029 bcmp(destaddr->buf, cm_entry->x_server.buf, 2030 destaddr->len) != 0) { 2031 RPCLOG(1, "connmgr_get: tiptr %p" 2032 " is going to a different server" 2033 " with the port that belongs" 2034 " to us!\n", (void *)cm_entry->x_tiptr); 2035 retryaddr = NULL; 2036 goto use_new_conn; 2037 } 2038 2039 /* 2040 * If the connection of interest is not connected and we 2041 * can't reconnect it, then the server is probably 2042 * still down. Return NULL to the caller and let it 2043 * retry later if it wants to. We have a delay so the 2044 * machine doesn't go into a tight retry loop. If the 2045 * entry was already connected, or the reconnected was 2046 * successful, return this entry. 2047 */ 2048 if (cm_entry->x_connected == FALSE) { 2049 return (connmgr_wrapconnect(cm_entry, 2050 waitp, destaddr, addrfmly, NULL, 2051 rpcerr, TRUE, nosignal, cr)); 2052 } else { 2053 CONN_HOLD(cm_entry); 2054 2055 cm_entry->x_time = ddi_get_lbolt(); 2056 mutex_exit(&connmgr_lock); 2057 RPCLOG(2, "connmgr_get: found old " 2058 "transport %p for retry\n", 2059 (void *)cm_entry); 2060 return (cm_entry); 2061 } 2062 } 2063 2064 /* 2065 * We cannot find an entry in the list for this retry. 2066 * Either the entry has been removed temporarily to be 2067 * reconnected by another thread, or the original call 2068 * got a port but never got connected, 2069 * and hence the transport never got put in the 2070 * list. Fall through to the "create new connection" code - 2071 * the former case will fail there trying to rebind the port, 2072 * and the later case (and any other pathological cases) will 2073 * rebind and reconnect and not hang the client machine. 2074 */ 2075 RPCLOG0(8, "connmgr_get: no entry in list for retry\n"); 2076 } 2077 /* 2078 * Set up a transport entry in the connection manager's list. 2079 */ 2080 cm_entry = (struct cm_xprt *) 2081 kmem_zalloc(sizeof (struct cm_xprt), KM_SLEEP); 2082 2083 cm_entry->x_server.buf = kmem_zalloc(destaddr->len, KM_SLEEP); 2084 bcopy(destaddr->buf, cm_entry->x_server.buf, destaddr->len); 2085 cm_entry->x_server.len = cm_entry->x_server.maxlen = destaddr->len; 2086 2087 cm_entry->x_state_flags = X_THREAD; 2088 cm_entry->x_ref = 1; 2089 cm_entry->x_family = addrfmly; 2090 cm_entry->x_rdev = device; 2091 cm_entry->x_zoneid = zoneid; 2092 mutex_init(&cm_entry->x_lock, NULL, MUTEX_DEFAULT, NULL); 2093 cv_init(&cm_entry->x_cv, NULL, CV_DEFAULT, NULL); 2094 cv_init(&cm_entry->x_conn_cv, NULL, CV_DEFAULT, NULL); 2095 cv_init(&cm_entry->x_dis_cv, NULL, CV_DEFAULT, NULL); 2096 2097 /* 2098 * Note that we add this partially initialized entry to the 2099 * connection list. This is so that we don't have connections to 2100 * the same server. 2101 * 2102 * Note that x_src is not initialized at this point. This is because 2103 * retryaddr might be NULL in which case x_src is whatever 2104 * t_kbind/bindresvport gives us. If another thread wants a 2105 * connection to the same server, seemingly we have an issue, but we 2106 * don't. If the other thread comes in with retryaddr == NULL, then it 2107 * will never look at x_src, and it will end up waiting in 2108 * connmgr_cwait() for the first thread to finish the connection 2109 * attempt. If the other thread comes in with retryaddr != NULL, then 2110 * that means there was a request sent on a connection, in which case 2111 * the the connection should already exist. Thus the first thread 2112 * never gets here ... it finds the connection it its server in the 2113 * connection list. 2114 * 2115 * But even if theory is wrong, in the retryaddr != NULL case, the 2nd 2116 * thread will skip us because x_src.len == 0. 2117 */ 2118 cm_entry->x_next = cm_hd; 2119 cm_hd = cm_entry; 2120 mutex_exit(&connmgr_lock); 2121 2122 /* 2123 * Either we didn't find an entry to the server of interest, or we 2124 * don't have the maximum number of connections to that server - 2125 * create a new connection. 2126 */ 2127 RPCLOG0(8, "connmgr_get: creating new connection\n"); 2128 rpcerr->re_status = RPC_TLIERROR; 2129 2130 i = t_kopen(NULL, device, FREAD|FWRITE|FNDELAY, &tiptr, zone_kcred()); 2131 if (i) { 2132 RPCLOG(1, "connmgr_get: can't open cots device, error %d\n", i); 2133 rpcerr->re_errno = i; 2134 connmgr_cancelconn(cm_entry); 2135 return (NULL); 2136 } 2137 rpc_poptimod(tiptr->fp->f_vnode); 2138 2139 if (i = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"rpcmod", 0, 2140 K_TO_K, kcred, &retval)) { 2141 RPCLOG(1, "connmgr_get: can't push cots module, %d\n", i); 2142 (void) t_kclose(tiptr, 1); 2143 rpcerr->re_errno = i; 2144 connmgr_cancelconn(cm_entry); 2145 return (NULL); 2146 } 2147 2148 if (i = strioctl(tiptr->fp->f_vnode, RPC_CLIENT, 0, 0, K_TO_K, 2149 kcred, &retval)) { 2150 RPCLOG(1, "connmgr_get: can't set client status with cots " 2151 "module, %d\n", i); 2152 (void) t_kclose(tiptr, 1); 2153 rpcerr->re_errno = i; 2154 connmgr_cancelconn(cm_entry); 2155 return (NULL); 2156 } 2157 2158 mutex_enter(&connmgr_lock); 2159 2160 wq = tiptr->fp->f_vnode->v_stream->sd_wrq->q_next; 2161 cm_entry->x_wq = wq; 2162 2163 mutex_exit(&connmgr_lock); 2164 2165 if (i = strioctl(tiptr->fp->f_vnode, I_PUSH, (intptr_t)"timod", 0, 2166 K_TO_K, kcred, &retval)) { 2167 RPCLOG(1, "connmgr_get: can't push timod, %d\n", i); 2168 (void) t_kclose(tiptr, 1); 2169 rpcerr->re_errno = i; 2170 connmgr_cancelconn(cm_entry); 2171 return (NULL); 2172 } 2173 2174 /* 2175 * If the caller has not specified reserved port usage then 2176 * take the system default. 2177 */ 2178 if (useresvport == -1) 2179 useresvport = clnt_cots_do_bindresvport; 2180 2181 if ((useresvport || retryaddr != NULL) && 2182 (addrfmly == AF_INET || addrfmly == AF_INET6)) { 2183 bool_t alloc_src = FALSE; 2184 2185 if (srcaddr->len != destaddr->len) { 2186 kmem_free(srcaddr->buf, srcaddr->maxlen); 2187 srcaddr->buf = kmem_zalloc(destaddr->len, KM_SLEEP); 2188 srcaddr->maxlen = destaddr->len; 2189 srcaddr->len = destaddr->len; 2190 alloc_src = TRUE; 2191 } 2192 2193 if ((i = bindresvport(tiptr, retryaddr, srcaddr, TRUE)) != 0) { 2194 (void) t_kclose(tiptr, 1); 2195 RPCLOG(1, "connmgr_get: couldn't bind, retryaddr: " 2196 "%p\n", (void *)retryaddr); 2197 2198 /* 2199 * 1225408: If we allocated a source address, then it 2200 * is either garbage or all zeroes. In that case 2201 * we need to clear srcaddr. 2202 */ 2203 if (alloc_src == TRUE) { 2204 kmem_free(srcaddr->buf, srcaddr->maxlen); 2205 srcaddr->maxlen = srcaddr->len = 0; 2206 srcaddr->buf = NULL; 2207 } 2208 rpcerr->re_errno = i; 2209 connmgr_cancelconn(cm_entry); 2210 return (NULL); 2211 } 2212 } else { 2213 if ((i = t_kbind(tiptr, NULL, NULL)) != 0) { 2214 RPCLOG(1, "clnt_cots_kcreate: t_kbind: %d\n", i); 2215 (void) t_kclose(tiptr, 1); 2216 rpcerr->re_errno = i; 2217 connmgr_cancelconn(cm_entry); 2218 return (NULL); 2219 } 2220 } 2221 2222 { 2223 /* 2224 * Keep the kernel stack lean. Don't move this call 2225 * declaration to the top of this function because a 2226 * call is declared in connmgr_wrapconnect() 2227 */ 2228 calllist_t call; 2229 2230 bzero(&call, sizeof (call)); 2231 cv_init(&call.call_cv, NULL, CV_DEFAULT, NULL); 2232 2233 /* 2234 * This is a bound end-point so don't close it's stream. 2235 */ 2236 connected = connmgr_connect(cm_entry, wq, destaddr, addrfmly, 2237 &call, &tidu_size, FALSE, waitp, nosignal, cr); 2238 *rpcerr = call.call_err; 2239 cv_destroy(&call.call_cv); 2240 2241 } 2242 2243 mutex_enter(&connmgr_lock); 2244 2245 /* 2246 * Set up a transport entry in the connection manager's list. 2247 */ 2248 cm_entry->x_src.buf = kmem_zalloc(srcaddr->len, KM_SLEEP); 2249 bcopy(srcaddr->buf, cm_entry->x_src.buf, srcaddr->len); 2250 cm_entry->x_src.len = cm_entry->x_src.maxlen = srcaddr->len; 2251 2252 cm_entry->x_tiptr = tiptr; 2253 cm_entry->x_time = ddi_get_lbolt(); 2254 2255 if (tiptr->tp_info.servtype == T_COTS_ORD) 2256 cm_entry->x_ordrel = TRUE; 2257 else 2258 cm_entry->x_ordrel = FALSE; 2259 2260 cm_entry->x_tidu_size = tidu_size; 2261 2262 if (cm_entry->x_early_disc) { 2263 /* 2264 * We need to check if a disconnect request has come 2265 * while we are connected, if so, then we need to 2266 * set rpcerr->re_status appropriately before returning 2267 * NULL to caller. 2268 */ 2269 if (rpcerr->re_status == RPC_SUCCESS) 2270 rpcerr->re_status = RPC_XPRTFAILED; 2271 cm_entry->x_connected = FALSE; 2272 } else 2273 cm_entry->x_connected = connected; 2274 2275 /* 2276 * There could be a discrepancy here such that 2277 * x_early_disc is TRUE yet connected is TRUE as well 2278 * and the connection is actually connected. In that case 2279 * lets be conservative and declare the connection as not 2280 * connected. 2281 */ 2282 cm_entry->x_early_disc = FALSE; 2283 cm_entry->x_needdis = (cm_entry->x_connected == FALSE); 2284 cm_entry->x_ctime = ddi_get_lbolt(); 2285 2286 /* 2287 * Notify any threads waiting that the connection attempt is done. 2288 */ 2289 cm_entry->x_thread = FALSE; 2290 cv_broadcast(&cm_entry->x_conn_cv); 2291 2292 if (cm_entry->x_connected == FALSE) { 2293 mutex_exit(&connmgr_lock); 2294 connmgr_release(cm_entry); 2295 return (NULL); 2296 } 2297 2298 mutex_exit(&connmgr_lock); 2299 2300 return (cm_entry); 2301 } 2302 2303 /* 2304 * Keep the cm_xprt entry on the connecton list when making a connection. This 2305 * is to prevent multiple connections to a slow server from appearing. 2306 * We use the bit field x_thread to tell if a thread is doing a connection 2307 * which keeps other interested threads from messing with connection. 2308 * Those other threads just wait if x_thread is set. 2309 * 2310 * If x_thread is not set, then we do the actual work of connecting via 2311 * connmgr_connect(). 2312 * 2313 * mutex convention: called with connmgr_lock held, returns with it released. 2314 */ 2315 static struct cm_xprt * 2316 connmgr_wrapconnect( 2317 struct cm_xprt *cm_entry, 2318 const struct timeval *waitp, 2319 struct netbuf *destaddr, 2320 int addrfmly, 2321 struct netbuf *srcaddr, 2322 struct rpc_err *rpcerr, 2323 bool_t reconnect, 2324 bool_t nosignal, 2325 cred_t *cr) 2326 { 2327 ASSERT(MUTEX_HELD(&connmgr_lock)); 2328 /* 2329 * Hold this entry as we are about to drop connmgr_lock. 2330 */ 2331 CONN_HOLD(cm_entry); 2332 2333 /* 2334 * If there is a thread already making a connection for us, then 2335 * wait for it to complete the connection. 2336 */ 2337 if (cm_entry->x_thread == TRUE) { 2338 rpcerr->re_status = connmgr_cwait(cm_entry, waitp, nosignal); 2339 2340 if (rpcerr->re_status != RPC_SUCCESS) { 2341 mutex_exit(&connmgr_lock); 2342 connmgr_release(cm_entry); 2343 return (NULL); 2344 } 2345 } else { 2346 bool_t connected; 2347 calllist_t call; 2348 2349 cm_entry->x_thread = TRUE; 2350 2351 while (cm_entry->x_needrel == TRUE) { 2352 cm_entry->x_needrel = FALSE; 2353 2354 connmgr_sndrel(cm_entry); 2355 delay(drv_sectohz(1)); 2356 2357 mutex_enter(&connmgr_lock); 2358 } 2359 2360 /* 2361 * If we need to send a T_DISCON_REQ, send one. 2362 */ 2363 connmgr_dis_and_wait(cm_entry); 2364 2365 mutex_exit(&connmgr_lock); 2366 2367 bzero(&call, sizeof (call)); 2368 cv_init(&call.call_cv, NULL, CV_DEFAULT, NULL); 2369 2370 connected = connmgr_connect(cm_entry, cm_entry->x_wq, 2371 destaddr, addrfmly, &call, &cm_entry->x_tidu_size, 2372 reconnect, waitp, nosignal, cr); 2373 2374 *rpcerr = call.call_err; 2375 cv_destroy(&call.call_cv); 2376 2377 mutex_enter(&connmgr_lock); 2378 2379 2380 if (cm_entry->x_early_disc) { 2381 /* 2382 * We need to check if a disconnect request has come 2383 * while we are connected, if so, then we need to 2384 * set rpcerr->re_status appropriately before returning 2385 * NULL to caller. 2386 */ 2387 if (rpcerr->re_status == RPC_SUCCESS) 2388 rpcerr->re_status = RPC_XPRTFAILED; 2389 cm_entry->x_connected = FALSE; 2390 } else 2391 cm_entry->x_connected = connected; 2392 2393 /* 2394 * There could be a discrepancy here such that 2395 * x_early_disc is TRUE yet connected is TRUE as well 2396 * and the connection is actually connected. In that case 2397 * lets be conservative and declare the connection as not 2398 * connected. 2399 */ 2400 2401 cm_entry->x_early_disc = FALSE; 2402 cm_entry->x_needdis = (cm_entry->x_connected == FALSE); 2403 2404 2405 /* 2406 * connmgr_connect() may have given up before the connection 2407 * actually timed out. So ensure that before the next 2408 * connection attempt we do a disconnect. 2409 */ 2410 cm_entry->x_ctime = ddi_get_lbolt(); 2411 cm_entry->x_thread = FALSE; 2412 2413 cv_broadcast(&cm_entry->x_conn_cv); 2414 2415 if (cm_entry->x_connected == FALSE) { 2416 mutex_exit(&connmgr_lock); 2417 connmgr_release(cm_entry); 2418 return (NULL); 2419 } 2420 } 2421 2422 if (srcaddr != NULL) { 2423 /* 2424 * Copy into the handle the 2425 * source address of the 2426 * connection, which we will use 2427 * in case of a later retry. 2428 */ 2429 if (srcaddr->len != cm_entry->x_src.len) { 2430 if (srcaddr->maxlen > 0) 2431 kmem_free(srcaddr->buf, srcaddr->maxlen); 2432 srcaddr->buf = kmem_zalloc(cm_entry->x_src.len, 2433 KM_SLEEP); 2434 srcaddr->maxlen = srcaddr->len = 2435 cm_entry->x_src.len; 2436 } 2437 bcopy(cm_entry->x_src.buf, srcaddr->buf, srcaddr->len); 2438 } 2439 cm_entry->x_time = ddi_get_lbolt(); 2440 mutex_exit(&connmgr_lock); 2441 return (cm_entry); 2442 } 2443 2444 /* 2445 * If we need to send a T_DISCON_REQ, send one. 2446 */ 2447 static void 2448 connmgr_dis_and_wait(struct cm_xprt *cm_entry) 2449 { 2450 ASSERT(MUTEX_HELD(&connmgr_lock)); 2451 for (;;) { 2452 while (cm_entry->x_needdis == TRUE) { 2453 RPCLOG(8, "connmgr_dis_and_wait: need " 2454 "T_DISCON_REQ for connection 0x%p\n", 2455 (void *)cm_entry); 2456 cm_entry->x_needdis = FALSE; 2457 cm_entry->x_waitdis = TRUE; 2458 2459 connmgr_snddis(cm_entry); 2460 2461 mutex_enter(&connmgr_lock); 2462 } 2463 2464 if (cm_entry->x_waitdis == TRUE) { 2465 clock_t timout; 2466 2467 RPCLOG(8, "connmgr_dis_and_wait waiting for " 2468 "T_DISCON_REQ's ACK for connection %p\n", 2469 (void *)cm_entry); 2470 2471 timout = drv_sectohz(clnt_cots_min_conntout); 2472 2473 /* 2474 * The TPI spec says that the T_DISCON_REQ 2475 * will get acknowledged, but in practice 2476 * the ACK may never get sent. So don't 2477 * block forever. 2478 */ 2479 (void) cv_reltimedwait(&cm_entry->x_dis_cv, 2480 &connmgr_lock, timout, TR_CLOCK_TICK); 2481 } 2482 /* 2483 * If we got the ACK, break. If we didn't, 2484 * then send another T_DISCON_REQ. 2485 */ 2486 if (cm_entry->x_waitdis == FALSE) { 2487 break; 2488 } else { 2489 RPCLOG(8, "connmgr_dis_and_wait: did" 2490 "not get T_DISCON_REQ's ACK for " 2491 "connection %p\n", (void *)cm_entry); 2492 cm_entry->x_needdis = TRUE; 2493 } 2494 } 2495 } 2496 2497 static void 2498 connmgr_cancelconn(struct cm_xprt *cm_entry) 2499 { 2500 /* 2501 * Mark the connection table entry as dead; the next thread that 2502 * goes through connmgr_release() will notice this and deal with it. 2503 */ 2504 mutex_enter(&connmgr_lock); 2505 cm_entry->x_dead = TRUE; 2506 2507 /* 2508 * Notify any threads waiting for the connection that it isn't 2509 * going to happen. 2510 */ 2511 cm_entry->x_thread = FALSE; 2512 cv_broadcast(&cm_entry->x_conn_cv); 2513 mutex_exit(&connmgr_lock); 2514 2515 connmgr_release(cm_entry); 2516 } 2517 2518 static void 2519 connmgr_close(struct cm_xprt *cm_entry) 2520 { 2521 mutex_enter(&cm_entry->x_lock); 2522 while (cm_entry->x_ref != 0) { 2523 /* 2524 * Must be a noninterruptible wait. 2525 */ 2526 cv_wait(&cm_entry->x_cv, &cm_entry->x_lock); 2527 } 2528 2529 if (cm_entry->x_tiptr != NULL) 2530 (void) t_kclose(cm_entry->x_tiptr, 1); 2531 2532 mutex_exit(&cm_entry->x_lock); 2533 if (cm_entry->x_ksp != NULL) { 2534 mutex_enter(&connmgr_lock); 2535 cm_entry->x_ksp->ks_private = NULL; 2536 mutex_exit(&connmgr_lock); 2537 2538 /* 2539 * Must free the buffer we allocated for the 2540 * server address in the update function 2541 */ 2542 if (((struct cm_kstat_xprt *)(cm_entry->x_ksp->ks_data))-> 2543 x_server.value.str.addr.ptr != NULL) 2544 kmem_free(((struct cm_kstat_xprt *)(cm_entry->x_ksp-> 2545 ks_data))->x_server.value.str.addr.ptr, 2546 INET6_ADDRSTRLEN); 2547 kmem_free(cm_entry->x_ksp->ks_data, 2548 cm_entry->x_ksp->ks_data_size); 2549 kstat_delete(cm_entry->x_ksp); 2550 } 2551 2552 mutex_destroy(&cm_entry->x_lock); 2553 cv_destroy(&cm_entry->x_cv); 2554 cv_destroy(&cm_entry->x_conn_cv); 2555 cv_destroy(&cm_entry->x_dis_cv); 2556 2557 if (cm_entry->x_server.buf != NULL) 2558 kmem_free(cm_entry->x_server.buf, cm_entry->x_server.maxlen); 2559 if (cm_entry->x_src.buf != NULL) 2560 kmem_free(cm_entry->x_src.buf, cm_entry->x_src.maxlen); 2561 kmem_free(cm_entry, sizeof (struct cm_xprt)); 2562 } 2563 2564 /* 2565 * Called by KRPC after sending the call message to release the connection 2566 * it was using. 2567 */ 2568 static void 2569 connmgr_release(struct cm_xprt *cm_entry) 2570 { 2571 mutex_enter(&cm_entry->x_lock); 2572 cm_entry->x_ref--; 2573 if (cm_entry->x_ref == 0) 2574 cv_signal(&cm_entry->x_cv); 2575 mutex_exit(&cm_entry->x_lock); 2576 } 2577 2578 /* 2579 * Set TCP receive and xmit buffer size for RPC connections. 2580 */ 2581 static bool_t 2582 connmgr_setbufsz(calllist_t *e, queue_t *wq, cred_t *cr) 2583 { 2584 int ok = FALSE; 2585 int val; 2586 2587 if (rpc_default_tcp_bufsz) 2588 return (FALSE); 2589 2590 /* 2591 * Only set new buffer size if it's larger than the system 2592 * default buffer size. If smaller buffer size is needed 2593 * then use /etc/system to set rpc_default_tcp_bufsz to 1. 2594 */ 2595 ok = connmgr_getopt_int(wq, SOL_SOCKET, SO_RCVBUF, &val, e, cr); 2596 if ((ok == TRUE) && (val < rpc_send_bufsz)) { 2597 ok = connmgr_setopt_int(wq, SOL_SOCKET, SO_RCVBUF, 2598 rpc_send_bufsz, e, cr); 2599 DTRACE_PROBE2(krpc__i__connmgr_rcvbufsz, 2600 int, ok, calllist_t *, e); 2601 } 2602 2603 ok = connmgr_getopt_int(wq, SOL_SOCKET, SO_SNDBUF, &val, e, cr); 2604 if ((ok == TRUE) && (val < rpc_recv_bufsz)) { 2605 ok = connmgr_setopt_int(wq, SOL_SOCKET, SO_SNDBUF, 2606 rpc_recv_bufsz, e, cr); 2607 DTRACE_PROBE2(krpc__i__connmgr_sndbufsz, 2608 int, ok, calllist_t *, e); 2609 } 2610 return (TRUE); 2611 } 2612 2613 /* 2614 * Given an open stream, connect to the remote. Returns true if connected, 2615 * false otherwise. 2616 */ 2617 static bool_t 2618 connmgr_connect( 2619 struct cm_xprt *cm_entry, 2620 queue_t *wq, 2621 struct netbuf *addr, 2622 int addrfmly, 2623 calllist_t *e, 2624 int *tidu_ptr, 2625 bool_t reconnect, 2626 const struct timeval *waitp, 2627 bool_t nosignal, 2628 cred_t *cr) 2629 { 2630 mblk_t *mp; 2631 struct T_conn_req *tcr; 2632 struct T_info_ack *tinfo; 2633 int interrupted, error; 2634 int tidu_size, kstat_instance; 2635 2636 /* if it's a reconnect, flush any lingering data messages */ 2637 if (reconnect) 2638 (void) putctl1(wq, M_FLUSH, FLUSHRW); 2639 2640 /* 2641 * Note: if the receiver uses SCM_UCRED/getpeerucred the pid will 2642 * appear as -1. 2643 */ 2644 mp = allocb_cred(sizeof (*tcr) + addr->len, cr, NOPID); 2645 if (mp == NULL) { 2646 /* 2647 * This is unfortunate, but we need to look up the stats for 2648 * this zone to increment the "memory allocation failed" 2649 * counter. curproc->p_zone is safe since we're initiating a 2650 * connection and not in some strange streams context. 2651 */ 2652 struct rpcstat *rpcstat; 2653 2654 rpcstat = zone_getspecific(rpcstat_zone_key, rpc_zone()); 2655 ASSERT(rpcstat != NULL); 2656 2657 RPCLOG0(1, "connmgr_connect: cannot alloc mp for " 2658 "sending conn request\n"); 2659 COTSRCSTAT_INCR(rpcstat->rpc_cots_client, rcnomem); 2660 e->call_status = RPC_SYSTEMERROR; 2661 e->call_reason = ENOSR; 2662 return (FALSE); 2663 } 2664 2665 /* Set TCP buffer size for RPC connections if needed */ 2666 if (addrfmly == AF_INET || addrfmly == AF_INET6) 2667 (void) connmgr_setbufsz(e, wq, cr); 2668 2669 mp->b_datap->db_type = M_PROTO; 2670 tcr = (struct T_conn_req *)mp->b_rptr; 2671 bzero(tcr, sizeof (*tcr)); 2672 tcr->PRIM_type = T_CONN_REQ; 2673 tcr->DEST_length = addr->len; 2674 tcr->DEST_offset = sizeof (struct T_conn_req); 2675 mp->b_wptr = mp->b_rptr + sizeof (*tcr); 2676 2677 bcopy(addr->buf, mp->b_wptr, tcr->DEST_length); 2678 mp->b_wptr += tcr->DEST_length; 2679 2680 RPCLOG(8, "connmgr_connect: sending conn request on queue " 2681 "%p", (void *)wq); 2682 RPCLOG(8, " call %p\n", (void *)wq); 2683 /* 2684 * We use the entry in the handle that is normally used for 2685 * waiting for RPC replies to wait for the connection accept. 2686 */ 2687 if (clnt_dispatch_send(wq, mp, e, 0, 0) != RPC_SUCCESS) { 2688 DTRACE_PROBE(krpc__e__connmgr__connect__cantsend); 2689 freemsg(mp); 2690 return (FALSE); 2691 } 2692 2693 mutex_enter(&clnt_pending_lock); 2694 2695 /* 2696 * We wait for the transport connection to be made, or an 2697 * indication that it could not be made. 2698 */ 2699 interrupted = 0; 2700 2701 /* 2702 * waitforack should have been called with T_OK_ACK, but the 2703 * present implementation needs to be passed T_INFO_ACK to 2704 * work correctly. 2705 */ 2706 error = waitforack(e, T_INFO_ACK, waitp, nosignal); 2707 if (error == EINTR) 2708 interrupted = 1; 2709 if (zone_status_get(curproc->p_zone) >= ZONE_IS_EMPTY) { 2710 /* 2711 * No time to lose; we essentially have been signaled to 2712 * quit. 2713 */ 2714 interrupted = 1; 2715 } 2716 #ifdef RPCDEBUG 2717 if (error == ETIME) 2718 RPCLOG0(8, "connmgr_connect: giving up " 2719 "on connection attempt; " 2720 "clnt_dispatch notifyconn " 2721 "diagnostic 'no one waiting for " 2722 "connection' should not be " 2723 "unexpected\n"); 2724 #endif 2725 if (e->call_prev) 2726 e->call_prev->call_next = e->call_next; 2727 else 2728 clnt_pending = e->call_next; 2729 if (e->call_next) 2730 e->call_next->call_prev = e->call_prev; 2731 mutex_exit(&clnt_pending_lock); 2732 2733 if (e->call_status != RPC_SUCCESS || error != 0) { 2734 if (interrupted) 2735 e->call_status = RPC_INTR; 2736 else if (error == ETIME) 2737 e->call_status = RPC_TIMEDOUT; 2738 else if (error == EPROTO) { 2739 e->call_status = RPC_SYSTEMERROR; 2740 e->call_reason = EPROTO; 2741 } 2742 2743 RPCLOG(8, "connmgr_connect: can't connect, status: " 2744 "%s\n", clnt_sperrno(e->call_status)); 2745 2746 if (e->call_reply) { 2747 freemsg(e->call_reply); 2748 e->call_reply = NULL; 2749 } 2750 2751 return (FALSE); 2752 } 2753 /* 2754 * The result of the "connection accept" is a T_info_ack 2755 * in the call_reply field. 2756 */ 2757 ASSERT(e->call_reply != NULL); 2758 mp = e->call_reply; 2759 e->call_reply = NULL; 2760 tinfo = (struct T_info_ack *)mp->b_rptr; 2761 2762 tidu_size = tinfo->TIDU_size; 2763 tidu_size -= (tidu_size % BYTES_PER_XDR_UNIT); 2764 if (tidu_size > COTS_DEFAULT_ALLOCSIZE || (tidu_size <= 0)) 2765 tidu_size = COTS_DEFAULT_ALLOCSIZE; 2766 *tidu_ptr = tidu_size; 2767 2768 freemsg(mp); 2769 2770 /* 2771 * Set up the pertinent options. NODELAY is so the transport doesn't 2772 * buffer up RPC messages on either end. This may not be valid for 2773 * all transports. Failure to set this option is not cause to 2774 * bail out so we return success anyway. Note that lack of NODELAY 2775 * or some other way to flush the message on both ends will cause 2776 * lots of retries and terrible performance. 2777 */ 2778 if (addrfmly == AF_INET || addrfmly == AF_INET6) { 2779 (void) connmgr_setopt(wq, IPPROTO_TCP, TCP_NODELAY, e, cr); 2780 if (e->call_status == RPC_XPRTFAILED) 2781 return (FALSE); 2782 } 2783 2784 /* 2785 * Since we have a connection, we now need to figure out if 2786 * we need to create a kstat. If x_ksp is not NULL then we 2787 * are reusing a connection and so we do not need to create 2788 * another kstat -- lets just return. 2789 */ 2790 if (cm_entry->x_ksp != NULL) 2791 return (TRUE); 2792 2793 /* 2794 * We need to increment rpc_kstat_instance atomically to prevent 2795 * two kstats being created with the same instance. 2796 */ 2797 kstat_instance = atomic_inc_32_nv((uint32_t *)&rpc_kstat_instance); 2798 2799 if ((cm_entry->x_ksp = kstat_create_zone("unix", kstat_instance, 2800 "rpc_cots_connections", "rpc", KSTAT_TYPE_NAMED, 2801 (uint_t)(sizeof (cm_kstat_xprt_t) / sizeof (kstat_named_t)), 2802 KSTAT_FLAG_VIRTUAL, cm_entry->x_zoneid)) == NULL) { 2803 return (TRUE); 2804 } 2805 2806 cm_entry->x_ksp->ks_lock = &connmgr_lock; 2807 cm_entry->x_ksp->ks_private = cm_entry; 2808 cm_entry->x_ksp->ks_data_size = ((INET6_ADDRSTRLEN * sizeof (char)) 2809 + sizeof (cm_kstat_template)); 2810 cm_entry->x_ksp->ks_data = kmem_alloc(cm_entry->x_ksp->ks_data_size, 2811 KM_SLEEP); 2812 bcopy(&cm_kstat_template, cm_entry->x_ksp->ks_data, 2813 cm_entry->x_ksp->ks_data_size); 2814 ((struct cm_kstat_xprt *)(cm_entry->x_ksp->ks_data))-> 2815 x_server.value.str.addr.ptr = 2816 kmem_alloc(INET6_ADDRSTRLEN, KM_SLEEP); 2817 2818 cm_entry->x_ksp->ks_update = conn_kstat_update; 2819 kstat_install(cm_entry->x_ksp); 2820 return (TRUE); 2821 } 2822 2823 /* 2824 * Verify that the specified offset falls within the mblk and 2825 * that the resulting pointer is aligned. 2826 * Returns NULL if not. 2827 * 2828 * code from fs/sockfs/socksubr.c 2829 */ 2830 static void * 2831 connmgr_opt_getoff(mblk_t *mp, t_uscalar_t offset, 2832 t_uscalar_t length, uint_t align_size) 2833 { 2834 uintptr_t ptr1, ptr2; 2835 2836 ASSERT(mp && mp->b_wptr >= mp->b_rptr); 2837 ptr1 = (uintptr_t)mp->b_rptr + offset; 2838 ptr2 = (uintptr_t)ptr1 + length; 2839 if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) { 2840 return (NULL); 2841 } 2842 if ((ptr1 & (align_size - 1)) != 0) { 2843 return (NULL); 2844 } 2845 return ((void *)ptr1); 2846 } 2847 2848 static bool_t 2849 connmgr_getopt_int(queue_t *wq, int level, int name, int *val, 2850 calllist_t *e, cred_t *cr) 2851 { 2852 mblk_t *mp; 2853 struct opthdr *opt, *opt_res; 2854 struct T_optmgmt_req *tor; 2855 struct T_optmgmt_ack *opt_ack; 2856 struct timeval waitp; 2857 int error; 2858 2859 mp = allocb_cred(sizeof (struct T_optmgmt_req) + 2860 sizeof (struct opthdr) + sizeof (int), cr, NOPID); 2861 if (mp == NULL) 2862 return (FALSE); 2863 2864 mp->b_datap->db_type = M_PROTO; 2865 tor = (struct T_optmgmt_req *)(mp->b_rptr); 2866 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 2867 tor->MGMT_flags = T_CURRENT; 2868 tor->OPT_length = sizeof (struct opthdr) + sizeof (int); 2869 tor->OPT_offset = sizeof (struct T_optmgmt_req); 2870 2871 opt = (struct opthdr *)(mp->b_rptr + sizeof (struct T_optmgmt_req)); 2872 opt->level = level; 2873 opt->name = name; 2874 opt->len = sizeof (int); 2875 mp->b_wptr += sizeof (struct T_optmgmt_req) + sizeof (struct opthdr) + 2876 sizeof (int); 2877 2878 /* 2879 * We will use this connection regardless 2880 * of whether or not the option is readable. 2881 */ 2882 if (clnt_dispatch_send(wq, mp, e, 0, 0) != RPC_SUCCESS) { 2883 DTRACE_PROBE(krpc__e__connmgr__getopt__cantsend); 2884 freemsg(mp); 2885 return (FALSE); 2886 } 2887 2888 mutex_enter(&clnt_pending_lock); 2889 2890 waitp.tv_sec = clnt_cots_min_conntout; 2891 waitp.tv_usec = 0; 2892 error = waitforack(e, T_OPTMGMT_ACK, &waitp, 1); 2893 2894 if (e->call_prev) 2895 e->call_prev->call_next = e->call_next; 2896 else 2897 clnt_pending = e->call_next; 2898 if (e->call_next) 2899 e->call_next->call_prev = e->call_prev; 2900 mutex_exit(&clnt_pending_lock); 2901 2902 /* get reply message */ 2903 mp = e->call_reply; 2904 e->call_reply = NULL; 2905 2906 if ((!mp) || (e->call_status != RPC_SUCCESS) || (error != 0)) { 2907 2908 DTRACE_PROBE4(krpc__e__connmgr_getopt, int, name, 2909 int, e->call_status, int, error, mblk_t *, mp); 2910 2911 if (mp) 2912 freemsg(mp); 2913 return (FALSE); 2914 } 2915 2916 opt_ack = (struct T_optmgmt_ack *)mp->b_rptr; 2917 opt_res = (struct opthdr *)connmgr_opt_getoff(mp, opt_ack->OPT_offset, 2918 opt_ack->OPT_length, __TPI_ALIGN_SIZE); 2919 2920 if (!opt_res) { 2921 DTRACE_PROBE4(krpc__e__connmgr_optres, mblk_t *, mp, int, name, 2922 int, opt_ack->OPT_offset, int, opt_ack->OPT_length); 2923 freemsg(mp); 2924 return (FALSE); 2925 } 2926 *val = *(int *)&opt_res[1]; 2927 2928 DTRACE_PROBE2(connmgr_getopt__ok, int, name, int, *val); 2929 2930 freemsg(mp); 2931 return (TRUE); 2932 } 2933 2934 /* 2935 * Called by connmgr_connect to set an option on the new stream. 2936 */ 2937 static bool_t 2938 connmgr_setopt_int(queue_t *wq, int level, int name, int val, 2939 calllist_t *e, cred_t *cr) 2940 { 2941 mblk_t *mp; 2942 struct opthdr *opt; 2943 struct T_optmgmt_req *tor; 2944 struct timeval waitp; 2945 int error; 2946 2947 mp = allocb_cred(sizeof (struct T_optmgmt_req) + 2948 sizeof (struct opthdr) + sizeof (int), cr, NOPID); 2949 if (mp == NULL) { 2950 RPCLOG0(1, "connmgr_setopt: cannot alloc mp for option " 2951 "request\n"); 2952 return (FALSE); 2953 } 2954 2955 mp->b_datap->db_type = M_PROTO; 2956 tor = (struct T_optmgmt_req *)(mp->b_rptr); 2957 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 2958 tor->MGMT_flags = T_NEGOTIATE; 2959 tor->OPT_length = sizeof (struct opthdr) + sizeof (int); 2960 tor->OPT_offset = sizeof (struct T_optmgmt_req); 2961 2962 opt = (struct opthdr *)(mp->b_rptr + sizeof (struct T_optmgmt_req)); 2963 opt->level = level; 2964 opt->name = name; 2965 opt->len = sizeof (int); 2966 *(int *)((char *)opt + sizeof (*opt)) = val; 2967 mp->b_wptr += sizeof (struct T_optmgmt_req) + sizeof (struct opthdr) + 2968 sizeof (int); 2969 2970 /* 2971 * We will use this connection regardless 2972 * of whether or not the option is settable. 2973 */ 2974 if (clnt_dispatch_send(wq, mp, e, 0, 0) != RPC_SUCCESS) { 2975 DTRACE_PROBE(krpc__e__connmgr__setopt__cantsend); 2976 freemsg(mp); 2977 return (FALSE); 2978 } 2979 2980 mutex_enter(&clnt_pending_lock); 2981 2982 waitp.tv_sec = clnt_cots_min_conntout; 2983 waitp.tv_usec = 0; 2984 error = waitforack(e, T_OPTMGMT_ACK, &waitp, 1); 2985 2986 if (e->call_prev) 2987 e->call_prev->call_next = e->call_next; 2988 else 2989 clnt_pending = e->call_next; 2990 if (e->call_next) 2991 e->call_next->call_prev = e->call_prev; 2992 mutex_exit(&clnt_pending_lock); 2993 2994 if (e->call_reply != NULL) { 2995 freemsg(e->call_reply); 2996 e->call_reply = NULL; 2997 } 2998 2999 if (e->call_status != RPC_SUCCESS || error != 0) { 3000 RPCLOG(1, "connmgr_setopt: can't set option: %d\n", name); 3001 return (FALSE); 3002 } 3003 RPCLOG(8, "connmgr_setopt: successfully set option: %d\n", name); 3004 return (TRUE); 3005 } 3006 3007 static bool_t 3008 connmgr_setopt(queue_t *wq, int level, int name, calllist_t *e, cred_t *cr) 3009 { 3010 return (connmgr_setopt_int(wq, level, name, 1, e, cr)); 3011 } 3012 3013 #ifdef DEBUG 3014 3015 /* 3016 * This is a knob to let us force code coverage in allocation failure 3017 * case. 3018 */ 3019 static int connmgr_failsnd; 3020 #define CONN_SND_ALLOC(Size, Pri) \ 3021 ((connmgr_failsnd-- > 0) ? NULL : allocb(Size, Pri)) 3022 3023 #else 3024 3025 #define CONN_SND_ALLOC(Size, Pri) allocb(Size, Pri) 3026 3027 #endif 3028 3029 /* 3030 * Sends an orderly release on the specified queue. 3031 * Entered with connmgr_lock. Exited without connmgr_lock 3032 */ 3033 static void 3034 connmgr_sndrel(struct cm_xprt *cm_entry) 3035 { 3036 struct T_ordrel_req *torr; 3037 mblk_t *mp; 3038 queue_t *q = cm_entry->x_wq; 3039 ASSERT(MUTEX_HELD(&connmgr_lock)); 3040 mp = CONN_SND_ALLOC(sizeof (struct T_ordrel_req), BPRI_LO); 3041 if (mp == NULL) { 3042 cm_entry->x_needrel = TRUE; 3043 mutex_exit(&connmgr_lock); 3044 RPCLOG(1, "connmgr_sndrel: cannot alloc mp for sending ordrel " 3045 "to queue %p\n", (void *)q); 3046 return; 3047 } 3048 mutex_exit(&connmgr_lock); 3049 3050 mp->b_datap->db_type = M_PROTO; 3051 torr = (struct T_ordrel_req *)(mp->b_rptr); 3052 torr->PRIM_type = T_ORDREL_REQ; 3053 mp->b_wptr = mp->b_rptr + sizeof (struct T_ordrel_req); 3054 3055 RPCLOG(8, "connmgr_sndrel: sending ordrel to queue %p\n", (void *)q); 3056 put(q, mp); 3057 } 3058 3059 /* 3060 * Sends an disconnect on the specified queue. 3061 * Entered with connmgr_lock. Exited without connmgr_lock 3062 */ 3063 static void 3064 connmgr_snddis(struct cm_xprt *cm_entry) 3065 { 3066 struct T_discon_req *tdis; 3067 mblk_t *mp; 3068 queue_t *q = cm_entry->x_wq; 3069 3070 ASSERT(MUTEX_HELD(&connmgr_lock)); 3071 mp = CONN_SND_ALLOC(sizeof (*tdis), BPRI_LO); 3072 if (mp == NULL) { 3073 cm_entry->x_needdis = TRUE; 3074 mutex_exit(&connmgr_lock); 3075 RPCLOG(1, "connmgr_snddis: cannot alloc mp for sending discon " 3076 "to queue %p\n", (void *)q); 3077 return; 3078 } 3079 mutex_exit(&connmgr_lock); 3080 3081 mp->b_datap->db_type = M_PROTO; 3082 tdis = (struct T_discon_req *)mp->b_rptr; 3083 tdis->PRIM_type = T_DISCON_REQ; 3084 mp->b_wptr = mp->b_rptr + sizeof (*tdis); 3085 3086 RPCLOG(8, "connmgr_snddis: sending discon to queue %p\n", (void *)q); 3087 put(q, mp); 3088 } 3089 3090 /* 3091 * Sets up the entry for receiving replies, and calls rpcmod's write put proc 3092 * (through put) to send the call. 3093 */ 3094 static int 3095 clnt_dispatch_send(queue_t *q, mblk_t *mp, calllist_t *e, uint_t xid, 3096 uint_t queue_flag) 3097 { 3098 ASSERT(e != NULL); 3099 3100 e->call_status = RPC_TIMEDOUT; /* optimistic, eh? */ 3101 e->call_reason = 0; 3102 e->call_wq = q; 3103 e->call_xid = xid; 3104 e->call_notified = FALSE; 3105 3106 if (!canput(q)) { 3107 e->call_status = RPC_CANTSEND; 3108 e->call_reason = ENOBUFS; 3109 return (RPC_CANTSEND); 3110 } 3111 3112 /* 3113 * If queue_flag is set then the calllist_t is already on the hash 3114 * queue. In this case just send the message and return. 3115 */ 3116 if (queue_flag) { 3117 put(q, mp); 3118 return (RPC_SUCCESS); 3119 3120 } 3121 3122 /* 3123 * Set up calls for RPC requests (with XID != 0) on the hash 3124 * queue for fast lookups and place other calls (i.e. 3125 * connection management) on the linked list. 3126 */ 3127 if (xid != 0) { 3128 RPCLOG(64, "clnt_dispatch_send: putting xid 0x%x on " 3129 "dispatch list\n", xid); 3130 e->call_hash = call_hash(xid, clnt_cots_hash_size); 3131 e->call_bucket = &cots_call_ht[e->call_hash]; 3132 call_table_enter(e); 3133 } else { 3134 mutex_enter(&clnt_pending_lock); 3135 if (clnt_pending) 3136 clnt_pending->call_prev = e; 3137 e->call_next = clnt_pending; 3138 e->call_prev = NULL; 3139 clnt_pending = e; 3140 mutex_exit(&clnt_pending_lock); 3141 } 3142 3143 put(q, mp); 3144 return (RPC_SUCCESS); 3145 } 3146 3147 /* 3148 * Called by rpcmod to notify a client with a clnt_pending call that its reply 3149 * has arrived. If we can't find a client waiting for this reply, we log 3150 * the error and return. 3151 */ 3152 bool_t 3153 clnt_dispatch_notify(mblk_t *mp, zoneid_t zoneid) 3154 { 3155 calllist_t *e = NULL; 3156 call_table_t *chtp; 3157 uint32_t xid; 3158 uint_t hash; 3159 3160 if ((IS_P2ALIGNED(mp->b_rptr, sizeof (uint32_t))) && 3161 (mp->b_wptr - mp->b_rptr) >= sizeof (xid)) 3162 xid = *((uint32_t *)mp->b_rptr); 3163 else { 3164 int i = 0; 3165 unsigned char *p = (unsigned char *)&xid; 3166 unsigned char *rptr; 3167 mblk_t *tmp = mp; 3168 3169 /* 3170 * Copy the xid, byte-by-byte into xid. 3171 */ 3172 while (tmp) { 3173 rptr = tmp->b_rptr; 3174 while (rptr < tmp->b_wptr) { 3175 *p++ = *rptr++; 3176 if (++i >= sizeof (xid)) 3177 goto done_xid_copy; 3178 } 3179 tmp = tmp->b_cont; 3180 } 3181 3182 /* 3183 * If we got here, we ran out of mblk space before the 3184 * xid could be copied. 3185 */ 3186 ASSERT(tmp == NULL && i < sizeof (xid)); 3187 3188 RPCLOG0(1, 3189 "clnt_dispatch_notify: message less than size of xid\n"); 3190 return (FALSE); 3191 3192 } 3193 done_xid_copy: 3194 3195 hash = call_hash(xid, clnt_cots_hash_size); 3196 chtp = &cots_call_ht[hash]; 3197 /* call_table_find returns with the hash bucket locked */ 3198 call_table_find(chtp, xid, e); 3199 3200 if (e != NULL) { 3201 /* 3202 * Found thread waiting for this reply 3203 */ 3204 mutex_enter(&e->call_lock); 3205 3206 /* 3207 * verify that the reply is coming in on 3208 * the same zone that it was sent from. 3209 */ 3210 if (e->call_zoneid != zoneid) { 3211 mutex_exit(&e->call_lock); 3212 mutex_exit(&chtp->ct_lock); 3213 RPCLOG0(1, "clnt_dispatch_notify: incorrect zoneid\n"); 3214 return (FALSE); 3215 } 3216 3217 if (e->call_reply) 3218 /* 3219 * This can happen under the following scenario: 3220 * clnt_cots_kcallit() times out on the response, 3221 * rfscall() repeats the CLNT_CALL() with 3222 * the same xid, clnt_cots_kcallit() sends the retry, 3223 * thereby putting the clnt handle on the pending list, 3224 * the first response arrives, signalling the thread 3225 * in clnt_cots_kcallit(). Before that thread is 3226 * dispatched, the second response arrives as well, 3227 * and clnt_dispatch_notify still finds the handle on 3228 * the pending list, with call_reply set. So free the 3229 * old reply now. 3230 * 3231 * It is also possible for a response intended for 3232 * an RPC call with a different xid to reside here. 3233 * This can happen if the thread that owned this 3234 * client handle prior to the current owner bailed 3235 * out and left its call record on the dispatch 3236 * queue. A window exists where the response can 3237 * arrive before the current owner dispatches its 3238 * RPC call. 3239 * 3240 * In any case, this is the very last point where we 3241 * can safely check the call_reply field before 3242 * placing the new response there. 3243 */ 3244 freemsg(e->call_reply); 3245 e->call_reply = mp; 3246 e->call_status = RPC_SUCCESS; 3247 e->call_notified = TRUE; 3248 cv_signal(&e->call_cv); 3249 mutex_exit(&e->call_lock); 3250 mutex_exit(&chtp->ct_lock); 3251 return (TRUE); 3252 } else { 3253 zone_t *zone; 3254 struct rpcstat *rpcstat; 3255 3256 mutex_exit(&chtp->ct_lock); 3257 RPCLOG(65, "clnt_dispatch_notify: no caller for reply 0x%x\n", 3258 xid); 3259 /* 3260 * This is unfortunate, but we need to lookup the zone so we 3261 * can increment its "rcbadxids" counter. 3262 */ 3263 zone = zone_find_by_id(zoneid); 3264 if (zone == NULL) { 3265 /* 3266 * The zone went away... 3267 */ 3268 return (FALSE); 3269 } 3270 rpcstat = zone_getspecific(rpcstat_zone_key, zone); 3271 if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN) { 3272 /* 3273 * Not interested 3274 */ 3275 zone_rele(zone); 3276 return (FALSE); 3277 } 3278 COTSRCSTAT_INCR(rpcstat->rpc_cots_client, rcbadxids); 3279 zone_rele(zone); 3280 } 3281 return (FALSE); 3282 } 3283 3284 /* 3285 * Called by rpcmod when a non-data indication arrives. The ones in which we 3286 * are interested are connection indications and options acks. We dispatch 3287 * based on the queue the indication came in on. If we are not interested in 3288 * what came in, we return false to rpcmod, who will then pass it upstream. 3289 */ 3290 bool_t 3291 clnt_dispatch_notifyconn(queue_t *q, mblk_t *mp) 3292 { 3293 calllist_t *e; 3294 int type; 3295 3296 ASSERT((q->q_flag & QREADR) == 0); 3297 3298 type = ((union T_primitives *)mp->b_rptr)->type; 3299 RPCLOG(8, "clnt_dispatch_notifyconn: prim type: [%s]\n", 3300 rpc_tpiprim2name(type)); 3301 mutex_enter(&clnt_pending_lock); 3302 for (e = clnt_pending; /* NO CONDITION */; e = e->call_next) { 3303 if (e == NULL) { 3304 mutex_exit(&clnt_pending_lock); 3305 RPCLOG(1, "clnt_dispatch_notifyconn: no one waiting " 3306 "for connection on queue 0x%p\n", (void *)q); 3307 return (FALSE); 3308 } 3309 if (e->call_wq == q) 3310 break; 3311 } 3312 3313 switch (type) { 3314 case T_CONN_CON: 3315 /* 3316 * The transport is now connected, send a T_INFO_REQ to get 3317 * the tidu size. 3318 */ 3319 mutex_exit(&clnt_pending_lock); 3320 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 3321 sizeof (struct T_info_req)); 3322 mp->b_rptr = mp->b_datap->db_base; 3323 ((union T_primitives *)mp->b_rptr)->type = T_INFO_REQ; 3324 mp->b_wptr = mp->b_rptr + sizeof (struct T_info_req); 3325 mp->b_datap->db_type = M_PCPROTO; 3326 put(q, mp); 3327 return (TRUE); 3328 case T_INFO_ACK: 3329 case T_OPTMGMT_ACK: 3330 e->call_status = RPC_SUCCESS; 3331 e->call_reply = mp; 3332 e->call_notified = TRUE; 3333 cv_signal(&e->call_cv); 3334 break; 3335 case T_ERROR_ACK: 3336 e->call_status = RPC_CANTCONNECT; 3337 e->call_reply = mp; 3338 e->call_notified = TRUE; 3339 cv_signal(&e->call_cv); 3340 break; 3341 case T_OK_ACK: 3342 /* 3343 * Great, but we are really waiting for a T_CONN_CON 3344 */ 3345 freemsg(mp); 3346 break; 3347 default: 3348 mutex_exit(&clnt_pending_lock); 3349 RPCLOG(1, "clnt_dispatch_notifyconn: bad type %d\n", type); 3350 return (FALSE); 3351 } 3352 3353 mutex_exit(&clnt_pending_lock); 3354 return (TRUE); 3355 } 3356 3357 /* 3358 * Called by rpcmod when the transport is (or should be) going away. Informs 3359 * all callers waiting for replies and marks the entry in the connection 3360 * manager's list as unconnected, and either closing (close handshake in 3361 * progress) or dead. 3362 */ 3363 void 3364 clnt_dispatch_notifyall(queue_t *q, int32_t msg_type, int32_t reason) 3365 { 3366 calllist_t *e; 3367 call_table_t *ctp; 3368 struct cm_xprt *cm_entry; 3369 int have_connmgr_lock; 3370 int i; 3371 3372 ASSERT((q->q_flag & QREADR) == 0); 3373 3374 RPCLOG(1, "clnt_dispatch_notifyall on queue %p", (void *)q); 3375 RPCLOG(1, " received a notifcation prim type [%s]", 3376 rpc_tpiprim2name(msg_type)); 3377 RPCLOG(1, " and reason %d\n", reason); 3378 3379 /* 3380 * Find the transport entry in the connection manager's list, close 3381 * the transport and delete the entry. In the case where rpcmod's 3382 * idle timer goes off, it sends us a T_ORDREL_REQ, indicating we 3383 * should gracefully close the connection. 3384 */ 3385 have_connmgr_lock = 1; 3386 mutex_enter(&connmgr_lock); 3387 for (cm_entry = cm_hd; cm_entry; cm_entry = cm_entry->x_next) { 3388 ASSERT(cm_entry != cm_entry->x_next); 3389 if (cm_entry->x_wq == q) { 3390 ASSERT(MUTEX_HELD(&connmgr_lock)); 3391 ASSERT(have_connmgr_lock == 1); 3392 switch (msg_type) { 3393 case T_ORDREL_REQ: 3394 3395 if (cm_entry->x_dead) { 3396 RPCLOG(1, "idle timeout on dead " 3397 "connection: %p\n", 3398 (void *)cm_entry); 3399 if (clnt_stop_idle != NULL) 3400 (*clnt_stop_idle)(q); 3401 break; 3402 } 3403 3404 /* 3405 * Only mark the connection as dead if it is 3406 * connected and idle. 3407 * An unconnected connection has probably 3408 * gone idle because the server is down, 3409 * and when it comes back up there will be 3410 * retries that need to use that connection. 3411 */ 3412 if (cm_entry->x_connected || 3413 cm_entry->x_doomed) { 3414 if (cm_entry->x_ordrel) { 3415 if (cm_entry->x_closing == 3416 TRUE) { 3417 /* 3418 * The connection is 3419 * obviously wedged due 3420 * to a bug or problem 3421 * with the transport. 3422 * Mark it as dead. 3423 * Otherwise we can 3424 * leak connections. 3425 */ 3426 cm_entry->x_dead = TRUE; 3427 mutex_exit( 3428 &connmgr_lock); 3429 have_connmgr_lock = 0; 3430 if (clnt_stop_idle != 3431 NULL) 3432 (*clnt_stop_idle)(q); 3433 break; 3434 } 3435 cm_entry->x_closing = TRUE; 3436 connmgr_sndrel(cm_entry); 3437 have_connmgr_lock = 0; 3438 } else { 3439 cm_entry->x_dead = TRUE; 3440 mutex_exit(&connmgr_lock); 3441 have_connmgr_lock = 0; 3442 if (clnt_stop_idle != NULL) 3443 (*clnt_stop_idle)(q); 3444 } 3445 } else { 3446 /* 3447 * We don't mark the connection 3448 * as dead, but we turn off the 3449 * idle timer. 3450 */ 3451 mutex_exit(&connmgr_lock); 3452 have_connmgr_lock = 0; 3453 if (clnt_stop_idle != NULL) 3454 (*clnt_stop_idle)(q); 3455 RPCLOG(1, "clnt_dispatch_notifyall:" 3456 " ignoring timeout from rpcmod" 3457 " (q %p) because we are not " 3458 " connected\n", (void *)q); 3459 } 3460 break; 3461 case T_ORDREL_IND: 3462 /* 3463 * If this entry is marked closing, then we are 3464 * completing a close handshake, and the 3465 * connection is dead. Otherwise, the server is 3466 * trying to close. Since the server will not 3467 * be sending any more RPC replies, we abort 3468 * the connection, including flushing 3469 * any RPC requests that are in-transit. 3470 * In either case, mark the entry as dead so 3471 * that it can be closed by the connection 3472 * manager's garbage collector. 3473 */ 3474 cm_entry->x_dead = TRUE; 3475 if (cm_entry->x_closing) { 3476 mutex_exit(&connmgr_lock); 3477 have_connmgr_lock = 0; 3478 if (clnt_stop_idle != NULL) 3479 (*clnt_stop_idle)(q); 3480 } else { 3481 /* 3482 * if we're getting a disconnect 3483 * before we've finished our 3484 * connect attempt, mark it for 3485 * later processing 3486 */ 3487 if (cm_entry->x_thread) 3488 cm_entry->x_early_disc = TRUE; 3489 else 3490 cm_entry->x_connected = FALSE; 3491 cm_entry->x_waitdis = TRUE; 3492 connmgr_snddis(cm_entry); 3493 have_connmgr_lock = 0; 3494 } 3495 break; 3496 3497 case T_ERROR_ACK: 3498 case T_OK_ACK: 3499 cm_entry->x_waitdis = FALSE; 3500 cv_signal(&cm_entry->x_dis_cv); 3501 mutex_exit(&connmgr_lock); 3502 return; 3503 3504 case T_DISCON_REQ: 3505 if (cm_entry->x_thread) 3506 cm_entry->x_early_disc = TRUE; 3507 else 3508 cm_entry->x_connected = FALSE; 3509 cm_entry->x_waitdis = TRUE; 3510 3511 connmgr_snddis(cm_entry); 3512 have_connmgr_lock = 0; 3513 break; 3514 3515 case T_DISCON_IND: 3516 default: 3517 /* 3518 * if we're getting a disconnect before 3519 * we've finished our connect attempt, 3520 * mark it for later processing 3521 */ 3522 if (cm_entry->x_closing) { 3523 cm_entry->x_dead = TRUE; 3524 mutex_exit(&connmgr_lock); 3525 have_connmgr_lock = 0; 3526 if (clnt_stop_idle != NULL) 3527 (*clnt_stop_idle)(q); 3528 } else { 3529 if (cm_entry->x_thread) { 3530 cm_entry->x_early_disc = TRUE; 3531 } else { 3532 cm_entry->x_dead = TRUE; 3533 cm_entry->x_connected = FALSE; 3534 } 3535 } 3536 break; 3537 } 3538 break; 3539 } 3540 } 3541 3542 if (have_connmgr_lock) 3543 mutex_exit(&connmgr_lock); 3544 3545 if (msg_type == T_ERROR_ACK || msg_type == T_OK_ACK) { 3546 RPCLOG(1, "clnt_dispatch_notifyall: (wq %p) could not find " 3547 "connmgr entry for discon ack\n", (void *)q); 3548 return; 3549 } 3550 3551 /* 3552 * Then kick all the clnt_pending calls out of their wait. There 3553 * should be no clnt_pending calls in the case of rpcmod's idle 3554 * timer firing. 3555 */ 3556 for (i = 0; i < clnt_cots_hash_size; i++) { 3557 ctp = &cots_call_ht[i]; 3558 mutex_enter(&ctp->ct_lock); 3559 for (e = ctp->ct_call_next; 3560 e != (calllist_t *)ctp; 3561 e = e->call_next) { 3562 if (e->call_wq == q && e->call_notified == FALSE) { 3563 RPCLOG(1, 3564 "clnt_dispatch_notifyall for queue %p ", 3565 (void *)q); 3566 RPCLOG(1, "aborting clnt_pending call %p\n", 3567 (void *)e); 3568 3569 if (msg_type == T_DISCON_IND) 3570 e->call_reason = reason; 3571 e->call_notified = TRUE; 3572 e->call_status = RPC_XPRTFAILED; 3573 cv_signal(&e->call_cv); 3574 } 3575 } 3576 mutex_exit(&ctp->ct_lock); 3577 } 3578 3579 mutex_enter(&clnt_pending_lock); 3580 for (e = clnt_pending; e; e = e->call_next) { 3581 /* 3582 * Only signal those RPC handles that haven't been 3583 * signalled yet. Otherwise we can get a bogus call_reason. 3584 * This can happen if thread A is making a call over a 3585 * connection. If the server is killed, it will cause 3586 * reset, and reason will default to EIO as a result of 3587 * a T_ORDREL_IND. Thread B then attempts to recreate 3588 * the connection but gets a T_DISCON_IND. If we set the 3589 * call_reason code for all threads, then if thread A 3590 * hasn't been dispatched yet, it will get the wrong 3591 * reason. The bogus call_reason can make it harder to 3592 * discriminate between calls that fail because the 3593 * connection attempt failed versus those where the call 3594 * may have been executed on the server. 3595 */ 3596 if (e->call_wq == q && e->call_notified == FALSE) { 3597 RPCLOG(1, "clnt_dispatch_notifyall for queue %p ", 3598 (void *)q); 3599 RPCLOG(1, " aborting clnt_pending call %p\n", 3600 (void *)e); 3601 3602 if (msg_type == T_DISCON_IND) 3603 e->call_reason = reason; 3604 e->call_notified = TRUE; 3605 /* 3606 * Let the caller timeout, else he will retry 3607 * immediately. 3608 */ 3609 e->call_status = RPC_XPRTFAILED; 3610 3611 /* 3612 * We used to just signal those threads 3613 * waiting for a connection, (call_xid = 0). 3614 * That meant that threads waiting for a response 3615 * waited till their timeout expired. This 3616 * could be a long time if they've specified a 3617 * maximum timeout. (2^31 - 1). So we 3618 * Signal all threads now. 3619 */ 3620 cv_signal(&e->call_cv); 3621 } 3622 } 3623 mutex_exit(&clnt_pending_lock); 3624 } 3625 3626 3627 /*ARGSUSED*/ 3628 /* 3629 * after resuming a system that's been suspended for longer than the 3630 * NFS server's idle timeout (svc_idle_timeout for Solaris 2), rfscall() 3631 * generates "NFS server X not responding" and "NFS server X ok" messages; 3632 * here we reset inet connections to cause a re-connect and avoid those 3633 * NFS messages. see 4045054 3634 */ 3635 boolean_t 3636 connmgr_cpr_reset(void *arg, int code) 3637 { 3638 struct cm_xprt *cxp; 3639 3640 if (code == CB_CODE_CPR_CHKPT) 3641 return (B_TRUE); 3642 3643 if (mutex_tryenter(&connmgr_lock) == 0) 3644 return (B_FALSE); 3645 for (cxp = cm_hd; cxp; cxp = cxp->x_next) { 3646 if ((cxp->x_family == AF_INET || cxp->x_family == AF_INET6) && 3647 cxp->x_connected == TRUE) { 3648 if (cxp->x_thread) 3649 cxp->x_early_disc = TRUE; 3650 else 3651 cxp->x_connected = FALSE; 3652 cxp->x_needdis = TRUE; 3653 } 3654 } 3655 mutex_exit(&connmgr_lock); 3656 return (B_TRUE); 3657 } 3658 3659 void 3660 clnt_cots_stats_init(zoneid_t zoneid, struct rpc_cots_client **statsp) 3661 { 3662 3663 *statsp = (struct rpc_cots_client *)rpcstat_zone_init_common(zoneid, 3664 "unix", "rpc_cots_client", (const kstat_named_t *)&cots_rcstat_tmpl, 3665 sizeof (cots_rcstat_tmpl)); 3666 } 3667 3668 void 3669 clnt_cots_stats_fini(zoneid_t zoneid, struct rpc_cots_client **statsp) 3670 { 3671 rpcstat_zone_fini_common(zoneid, "unix", "rpc_cots_client"); 3672 kmem_free(*statsp, sizeof (cots_rcstat_tmpl)); 3673 } 3674 3675 void 3676 clnt_cots_init(void) 3677 { 3678 mutex_init(&connmgr_lock, NULL, MUTEX_DEFAULT, NULL); 3679 mutex_init(&clnt_pending_lock, NULL, MUTEX_DEFAULT, NULL); 3680 3681 if (clnt_cots_hash_size < DEFAULT_MIN_HASH_SIZE) 3682 clnt_cots_hash_size = DEFAULT_MIN_HASH_SIZE; 3683 3684 cots_call_ht = call_table_init(clnt_cots_hash_size); 3685 zone_key_create(&zone_cots_key, NULL, NULL, clnt_zone_destroy); 3686 } 3687 3688 void 3689 clnt_cots_fini(void) 3690 { 3691 (void) zone_key_delete(zone_cots_key); 3692 } 3693 3694 /* 3695 * Wait for TPI ack, returns success only if expected ack is received 3696 * within timeout period. 3697 */ 3698 3699 static int 3700 waitforack(calllist_t *e, t_scalar_t ack_prim, const struct timeval *waitp, 3701 bool_t nosignal) 3702 { 3703 union T_primitives *tpr; 3704 clock_t timout; 3705 int cv_stat = 1; 3706 3707 ASSERT(MUTEX_HELD(&clnt_pending_lock)); 3708 while (e->call_reply == NULL) { 3709 if (waitp != NULL) { 3710 timout = drv_sectohz(waitp->tv_sec) + 3711 drv_usectohz(waitp->tv_usec); 3712 if (nosignal) 3713 cv_stat = cv_reltimedwait(&e->call_cv, 3714 &clnt_pending_lock, timout, TR_CLOCK_TICK); 3715 else 3716 cv_stat = cv_reltimedwait_sig(&e->call_cv, 3717 &clnt_pending_lock, timout, TR_CLOCK_TICK); 3718 } else { 3719 if (nosignal) 3720 cv_wait(&e->call_cv, &clnt_pending_lock); 3721 else 3722 cv_stat = cv_wait_sig(&e->call_cv, 3723 &clnt_pending_lock); 3724 } 3725 if (cv_stat == -1) 3726 return (ETIME); 3727 if (cv_stat == 0) 3728 return (EINTR); 3729 /* 3730 * if we received an error from the server and we know a reply 3731 * is not going to be sent, do not wait for the full timeout, 3732 * return now. 3733 */ 3734 if (e->call_status == RPC_XPRTFAILED) 3735 return (e->call_reason); 3736 } 3737 tpr = (union T_primitives *)e->call_reply->b_rptr; 3738 if (tpr->type == ack_prim) 3739 return (0); /* Success */ 3740 3741 if (tpr->type == T_ERROR_ACK) { 3742 if (tpr->error_ack.TLI_error == TSYSERR) 3743 return (tpr->error_ack.UNIX_error); 3744 else 3745 return (t_tlitosyserr(tpr->error_ack.TLI_error)); 3746 } 3747 3748 return (EPROTO); /* unknown or unexpected primitive */ 3749 }