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