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