Print this page
3354 kernel crash in rpcsec_gss after using gsscred
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Carlos Neira <cneirabustos@gmail.com>
Approved by: Robert Mustacchi <rm@joyent.com>
NEX-4123 xdrmblk_getpos() is unreliable
Reviewed by: Josef 'Jeff' Sipek <josef.sipek@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
re #13613 rb4516 Tunables needs volatile keyword
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/rpc/svc_cots.c
+++ new/usr/src/uts/common/rpc/svc_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
|
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 - * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
24 + * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
25 25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 + * Copyright 2012 Marcel Telka <marcel@telka.sk>
27 + * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
26 28 */
27 29
28 30 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 -/* All Rights Reserved */
31 +/* All Rights Reserved */
30 32
31 33 /*
32 34 * Portions of this source code were derived from Berkeley 4.3 BSD
33 35 * under license from the Regents of the University of California.
34 36 */
35 37
36 38 /*
37 39 * svc_cots.c
38 40 * Server side for connection-oriented RPC in the kernel.
39 41 *
40 42 */
41 43
42 44 #include <sys/param.h>
43 45 #include <sys/types.h>
44 46 #include <sys/sysmacros.h>
45 47 #include <sys/file.h>
46 48 #include <sys/stream.h>
47 49 #include <sys/strsubr.h>
48 50 #include <sys/strsun.h>
49 51 #include <sys/stropts.h>
50 52 #include <sys/tiuser.h>
51 53 #include <sys/timod.h>
52 54 #include <sys/tihdr.h>
53 55 #include <sys/fcntl.h>
54 56 #include <sys/errno.h>
55 57 #include <sys/kmem.h>
56 58 #include <sys/systm.h>
57 59 #include <sys/debug.h>
58 60 #include <sys/cmn_err.h>
59 61 #include <sys/kstat.h>
60 62 #include <sys/vtrace.h>
61 63
62 64 #include <rpc/types.h>
63 65 #include <rpc/xdr.h>
64 66 #include <rpc/auth.h>
65 67 #include <rpc/rpc_msg.h>
66 68 #include <rpc/svc.h>
67 69 #include <inet/ip.h>
68 70
69 71 #define COTS_MAX_ALLOCSIZE 2048
70 72 #define MSG_OFFSET 128 /* offset of call into the mblk */
71 73 #define RM_HDR_SIZE 4 /* record mark header size */
72 74
73 75 /*
74 76 * Routines exported through ops vector.
75 77 */
76 78 static bool_t svc_cots_krecv(SVCXPRT *, mblk_t *, struct rpc_msg *);
77 79 static bool_t svc_cots_ksend(SVCXPRT *, struct rpc_msg *);
78 80 static bool_t svc_cots_kgetargs(SVCXPRT *, xdrproc_t, caddr_t);
79 81 static bool_t svc_cots_kfreeargs(SVCXPRT *, xdrproc_t, caddr_t);
80 82 static void svc_cots_kdestroy(SVCMASTERXPRT *);
81 83 static int svc_cots_kdup(struct svc_req *, caddr_t, int,
82 84 struct dupreq **, bool_t *);
83 85 static void svc_cots_kdupdone(struct dupreq *, caddr_t,
84 86 void (*)(), int, int);
85 87 static int32_t *svc_cots_kgetres(SVCXPRT *, int);
86 88 static void svc_cots_kfreeres(SVCXPRT *);
87 89 static void svc_cots_kclone_destroy(SVCXPRT *);
88 90 static void svc_cots_kstart(SVCMASTERXPRT *);
89 91 static void svc_cots_ktattrs(SVCXPRT *, int, void **);
90 92
91 93 /*
92 94 * Server transport operations vector.
93 95 */
94 96 struct svc_ops svc_cots_op = {
95 97 svc_cots_krecv, /* Get requests */
96 98 svc_cots_kgetargs, /* Deserialize arguments */
|
↓ open down ↓ |
57 lines elided |
↑ open up ↑ |
97 99 svc_cots_ksend, /* Send reply */
98 100 svc_cots_kfreeargs, /* Free argument data space */
99 101 svc_cots_kdestroy, /* Destroy transport handle */
100 102 svc_cots_kdup, /* Check entry in dup req cache */
101 103 svc_cots_kdupdone, /* Mark entry in dup req cache as done */
102 104 svc_cots_kgetres, /* Get pointer to response buffer */
103 105 svc_cots_kfreeres, /* Destroy pre-serialized response header */
104 106 svc_cots_kclone_destroy, /* Destroy a clone xprt */
105 107 svc_cots_kstart, /* Tell `ready-to-receive' to rpcmod */
106 108 NULL, /* Transport specific clone xprt */
107 - svc_cots_ktattrs /* Transport Attributes */
109 + svc_cots_ktattrs, /* Transport Attributes */
110 + mir_svc_hold, /* Increment transport reference count */
111 + mir_svc_release /* Decrement transport reference count */
108 112 };
109 113
110 114 /*
111 115 * Master transport private data.
112 116 * Kept in xprt->xp_p2.
113 117 */
114 118 struct cots_master_data {
115 119 char *cmd_src_addr; /* client's address */
116 120 int cmd_xprt_started; /* flag for clone routine to call */
117 121 /* rpcmod's start routine. */
118 122 struct rpc_cots_server *cmd_stats; /* stats for zone */
119 123 };
120 124
121 125 /*
122 126 * Transport private data.
123 127 * Kept in clone_xprt->xp_p2buf.
124 128 */
125 129 typedef struct cots_data {
126 130 mblk_t *cd_mp; /* pre-allocated reply message */
127 131 mblk_t *cd_req_mp; /* request message */
128 132 } cots_data_t;
129 133
130 134 /*
131 135 * Server statistics
132 136 * NOTE: This structure type is duplicated in the NFS fast path.
133 137 */
134 138 static const struct rpc_cots_server {
135 139 kstat_named_t rscalls;
136 140 kstat_named_t rsbadcalls;
137 141 kstat_named_t rsnullrecv;
138 142 kstat_named_t rsbadlen;
139 143 kstat_named_t rsxdrcall;
140 144 kstat_named_t rsdupchecks;
141 145 kstat_named_t rsdupreqs;
142 146 } cots_rsstat_tmpl = {
143 147 { "calls", KSTAT_DATA_UINT64 },
144 148 { "badcalls", KSTAT_DATA_UINT64 },
145 149 { "nullrecv", KSTAT_DATA_UINT64 },
146 150 { "badlen", KSTAT_DATA_UINT64 },
147 151 { "xdrcall", KSTAT_DATA_UINT64 },
148 152 { "dupchecks", KSTAT_DATA_UINT64 },
149 153 { "dupreqs", KSTAT_DATA_UINT64 }
150 154 };
151 155
152 156 #define CLONE2STATS(clone_xprt) \
153 157 ((struct cots_master_data *)(clone_xprt)->xp_master->xp_p2)->cmd_stats
154 158 #define RSSTAT_INCR(s, x) \
155 159 atomic_inc_64(&(s)->x.value.ui64)
156 160
157 161 /*
158 162 * Pointer to a transport specific `ready to receive' function in rpcmod
159 163 * (set from rpcmod).
160 164 */
161 165 void (*mir_start)(queue_t *);
162 166 uint_t *svc_max_msg_sizep;
163 167
164 168 /*
165 169 * the address size of the underlying transport can sometimes be
166 170 * unknown (tinfo->ADDR_size == -1). For this case, it is
167 171 * necessary to figure out what the size is so the correct amount
168 172 * of data is allocated. This is an itterative process:
169 173 * 1. take a good guess (use T_MINADDRSIZE)
170 174 * 2. try it.
171 175 * 3. if it works then everything is ok
172 176 * 4. if the error is ENAMETOLONG, double the guess
173 177 * 5. go back to step 2.
174 178 */
175 179 #define T_UNKNOWNADDRSIZE (-1)
176 180 #define T_MINADDRSIZE 32
177 181
178 182 /*
179 183 * Create a transport record.
180 184 * The transport record, output buffer, and private data structure
181 185 * are allocated. The output buffer is serialized into using xdrmem.
182 186 * There is one transport record per user process which implements a
183 187 * set of services.
184 188 */
185 189 static kmutex_t cots_kcreate_lock;
186 190
187 191 int
188 192 svc_cots_kcreate(file_t *fp, uint_t max_msgsize, struct T_info_ack *tinfo,
189 193 SVCMASTERXPRT **nxprt)
190 194 {
191 195 struct cots_master_data *cmd;
192 196 int err, retval;
193 197 SVCMASTERXPRT *xprt;
194 198 struct rpcstat *rpcstat;
195 199 struct T_addr_ack *ack_p;
196 200 struct strioctl getaddr;
197 201
198 202 if (nxprt == NULL)
199 203 return (EINVAL);
200 204
201 205 rpcstat = zone_getspecific(rpcstat_zone_key, curproc->p_zone);
202 206 ASSERT(rpcstat != NULL);
203 207
204 208 xprt = kmem_zalloc(sizeof (SVCMASTERXPRT), KM_SLEEP);
205 209
206 210 cmd = kmem_zalloc(sizeof (*cmd) + sizeof (*ack_p)
207 211 + (2 * sizeof (sin6_t)), KM_SLEEP);
208 212
209 213 ack_p = (struct T_addr_ack *)&cmd[1];
210 214
211 215 if ((tinfo->TIDU_size > COTS_MAX_ALLOCSIZE) ||
212 216 (tinfo->TIDU_size <= 0))
213 217 xprt->xp_msg_size = COTS_MAX_ALLOCSIZE;
214 218 else {
215 219 xprt->xp_msg_size = tinfo->TIDU_size -
216 220 (tinfo->TIDU_size % BYTES_PER_XDR_UNIT);
217 221 }
218 222
219 223 xprt->xp_ops = &svc_cots_op;
220 224 xprt->xp_p2 = (caddr_t)cmd;
221 225 cmd->cmd_xprt_started = 0;
222 226 cmd->cmd_stats = rpcstat->rpc_cots_server;
223 227
224 228 getaddr.ic_cmd = TI_GETINFO;
225 229 getaddr.ic_timout = -1;
226 230 getaddr.ic_len = sizeof (*ack_p) + (2 * sizeof (sin6_t));
227 231 getaddr.ic_dp = (char *)ack_p;
228 232 ack_p->PRIM_type = T_ADDR_REQ;
229 233
230 234 err = strioctl(fp->f_vnode, I_STR, (intptr_t)&getaddr,
231 235 0, K_TO_K, CRED(), &retval);
232 236 if (err) {
233 237 kmem_free(cmd, sizeof (*cmd) + sizeof (*ack_p) +
234 238 (2 * sizeof (sin6_t)));
235 239 kmem_free(xprt, sizeof (SVCMASTERXPRT));
236 240 return (err);
237 241 }
238 242
239 243 xprt->xp_rtaddr.maxlen = ack_p->REMADDR_length;
240 244 xprt->xp_rtaddr.len = ack_p->REMADDR_length;
241 245 cmd->cmd_src_addr = xprt->xp_rtaddr.buf =
242 246 (char *)ack_p + ack_p->REMADDR_offset;
243 247
244 248 xprt->xp_lcladdr.maxlen = ack_p->LOCADDR_length;
245 249 xprt->xp_lcladdr.len = ack_p->LOCADDR_length;
246 250 xprt->xp_lcladdr.buf = (char *)ack_p + ack_p->LOCADDR_offset;
247 251
248 252 /*
249 253 * If the current sanity check size in rpcmod is smaller
250 254 * than the size needed for this xprt, then increase
251 255 * the sanity check.
252 256 */
253 257 if (max_msgsize != 0 && svc_max_msg_sizep &&
254 258 max_msgsize > *svc_max_msg_sizep) {
255 259
256 260 /* This check needs a lock */
257 261 mutex_enter(&cots_kcreate_lock);
258 262 if (svc_max_msg_sizep && max_msgsize > *svc_max_msg_sizep)
259 263 *svc_max_msg_sizep = max_msgsize;
260 264 mutex_exit(&cots_kcreate_lock);
261 265 }
262 266
263 267 *nxprt = xprt;
264 268
265 269 return (0);
266 270 }
267 271
268 272 /*
269 273 * Destroy a master transport record.
270 274 * Frees the space allocated for a transport record.
271 275 */
272 276 static void
273 277 svc_cots_kdestroy(SVCMASTERXPRT *xprt)
274 278 {
275 279 struct cots_master_data *cmd = (struct cots_master_data *)xprt->xp_p2;
276 280
277 281 ASSERT(cmd);
278 282
279 283 if (xprt->xp_netid)
280 284 kmem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
281 285 if (xprt->xp_addrmask.maxlen)
282 286 kmem_free(xprt->xp_addrmask.buf, xprt->xp_addrmask.maxlen);
283 287
284 288 mutex_destroy(&xprt->xp_req_lock);
285 289 mutex_destroy(&xprt->xp_thread_lock);
286 290
287 291 kmem_free(cmd, sizeof (*cmd) + sizeof (struct T_addr_ack) +
288 292 (2 * sizeof (sin6_t)));
289 293
290 294 kmem_free(xprt, sizeof (SVCMASTERXPRT));
291 295 }
292 296
293 297 /*
294 298 * svc_tli_kcreate() calls this function at the end to tell
295 299 * rpcmod that the transport is ready to receive requests.
296 300 */
297 301 static void
298 302 svc_cots_kstart(SVCMASTERXPRT *xprt)
299 303 {
300 304 struct cots_master_data *cmd = (struct cots_master_data *)xprt->xp_p2;
301 305
302 306 if (cmd->cmd_xprt_started == 0) {
303 307 /*
304 308 * Acquire the xp_req_lock in order to use xp_wq
305 309 * safely (we don't want to qenable a queue that has
306 310 * already been closed).
307 311 */
308 312 mutex_enter(&xprt->xp_req_lock);
309 313 if (cmd->cmd_xprt_started == 0 &&
310 314 xprt->xp_wq != NULL) {
311 315 (*mir_start)(xprt->xp_wq);
312 316 cmd->cmd_xprt_started = 1;
313 317 }
314 318 mutex_exit(&xprt->xp_req_lock);
315 319 }
316 320 }
317 321
318 322 /*
319 323 * Transport-type specific part of svc_xprt_cleanup().
320 324 */
321 325 static void
322 326 svc_cots_kclone_destroy(SVCXPRT *clone_xprt)
323 327 {
324 328 cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
325 329
326 330 if (cd->cd_req_mp) {
327 331 freemsg(cd->cd_req_mp);
328 332 cd->cd_req_mp = (mblk_t *)0;
329 333 }
330 334 ASSERT(cd->cd_mp == NULL);
331 335 }
332 336
333 337 /*
334 338 * Transport Attributes.
335 339 */
336 340 static void
337 341 svc_cots_ktattrs(SVCXPRT *clone_xprt, int attrflag, void **tattr)
338 342 {
339 343 *tattr = NULL;
340 344
341 345 switch (attrflag) {
342 346 case SVC_TATTR_ADDRMASK:
343 347 *tattr = (void *)&clone_xprt->xp_master->xp_addrmask;
344 348 }
345 349 }
346 350
347 351 /*
348 352 * Receive rpc requests.
349 353 * Checks if the message is intact, and deserializes the call packet.
350 354 */
351 355 static bool_t
352 356 svc_cots_krecv(SVCXPRT *clone_xprt, mblk_t *mp, struct rpc_msg *msg)
353 357 {
354 358 cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
355 359 XDR *xdrs = &clone_xprt->xp_xdrin;
356 360 struct rpc_cots_server *stats = CLONE2STATS(clone_xprt);
357 361
358 362 TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KRECV_START,
359 363 "svc_cots_krecv_start:");
360 364 RPCLOG(4, "svc_cots_krecv_start clone_xprt = %p:\n",
361 365 (void *)clone_xprt);
362 366
363 367 RSSTAT_INCR(stats, rscalls);
364 368
365 369 if (mp->b_datap->db_type != M_DATA) {
366 370 RPCLOG(16, "svc_cots_krecv bad db_type %d\n",
367 371 mp->b_datap->db_type);
368 372 goto bad;
369 373 }
370 374
371 375 xdrmblk_init(xdrs, mp, XDR_DECODE, 0);
372 376
373 377 TRACE_0(TR_FAC_KRPC, TR_XDR_CALLMSG_START,
374 378 "xdr_callmsg_start:");
375 379 RPCLOG0(4, "xdr_callmsg_start:\n");
376 380 if (!xdr_callmsg(xdrs, msg)) {
377 381 XDR_DESTROY(xdrs);
378 382 TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
379 383 "xdr_callmsg_end:(%S)", "bad");
380 384 RPCLOG0(1, "svc_cots_krecv xdr_callmsg failure\n");
381 385 RSSTAT_INCR(stats, rsxdrcall);
382 386 goto bad;
383 387 }
384 388 TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
385 389 "xdr_callmsg_end:(%S)", "good");
386 390
387 391 clone_xprt->xp_xid = msg->rm_xid;
388 392 cd->cd_req_mp = mp;
389 393
390 394 TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KRECV_END,
391 395 "svc_cots_krecv_end:(%S)", "good");
392 396 RPCLOG0(4, "svc_cots_krecv_end:good\n");
393 397 return (TRUE);
394 398
395 399 bad:
396 400 if (mp)
397 401 freemsg(mp);
398 402
399 403 RSSTAT_INCR(stats, rsbadcalls);
400 404 TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KRECV_END,
401 405 "svc_cots_krecv_end:(%S)", "bad");
402 406 return (FALSE);
403 407 }
404 408
405 409 /*
406 410 * Send rpc reply.
407 411 */
408 412 static bool_t
409 413 svc_cots_ksend(SVCXPRT *clone_xprt, struct rpc_msg *msg)
410 414 {
411 415 /* LINTED pointer alignment */
412 416 cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
413 417 XDR *xdrs = &(clone_xprt->xp_xdrout);
414 418 int retval = FALSE;
415 419 mblk_t *mp;
416 420 xdrproc_t xdr_results;
417 421 caddr_t xdr_location;
418 422 bool_t has_args;
419 423
420 424 TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KSEND_START,
421 425 "svc_cots_ksend_start:");
422 426
423 427 /*
424 428 * If there is a result procedure specified in the reply message,
425 429 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
426 430 * We need to make sure it won't be processed twice, so we null
427 431 * it for xdr_replymsg here.
428 432 */
429 433 has_args = FALSE;
430 434 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
431 435 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
432 436 if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
433 437 has_args = TRUE;
434 438 xdr_location = msg->acpted_rply.ar_results.where;
435 439 msg->acpted_rply.ar_results.proc = xdr_void;
436 440 msg->acpted_rply.ar_results.where = NULL;
437 441 }
438 442 }
439 443
440 444 mp = cd->cd_mp;
441 445 if (mp) {
442 446 /*
443 447 * The program above pre-allocated an mblk and put
444 448 * the data in place.
445 449 */
446 450 cd->cd_mp = (mblk_t *)NULL;
447 451 if (!(xdr_replymsg_body(xdrs, msg) &&
448 452 (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
449 453 xdr_results, xdr_location)))) {
450 454 XDR_DESTROY(xdrs);
451 455 RPCLOG0(1, "svc_cots_ksend: "
452 456 "xdr_replymsg_body/SVCAUTH_WRAP failed\n");
453 457 freemsg(mp);
454 458 goto out;
455 459 }
456 460 } else {
457 461 int len;
458 462 int mpsize;
459 463
460 464 /*
461 465 * Leave space for protocol headers.
462 466 */
463 467 len = MSG_OFFSET + clone_xprt->xp_msg_size;
464 468
465 469 /*
466 470 * Allocate an initial mblk for the response data.
467 471 */
468 472 while (!(mp = allocb(len, BPRI_LO))) {
469 473 RPCLOG0(16, "svc_cots_ksend: allocb failed failed\n");
470 474 if (strwaitbuf(len, BPRI_LO)) {
471 475 TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KSEND_END,
472 476 "svc_cots_ksend_end:(%S)", "strwaitbuf");
473 477 RPCLOG0(1,
474 478 "svc_cots_ksend: strwaitbuf failed\n");
475 479 goto out;
476 480 }
477 481 }
478 482
479 483 /*
480 484 * Initialize the XDR encode stream. Additional mblks
481 485 * will be allocated if necessary. They will be TIDU
482 486 * sized.
483 487 */
484 488 xdrmblk_init(xdrs, mp, XDR_ENCODE, clone_xprt->xp_msg_size);
485 489 mpsize = MBLKSIZE(mp);
486 490 ASSERT(mpsize >= len);
487 491 ASSERT(mp->b_rptr == mp->b_datap->db_base);
488 492
489 493 /*
490 494 * If the size of mblk is not appreciably larger than what we
491 495 * asked, then resize the mblk to exactly len bytes. Reason for
492 496 * this: suppose len is 1600 bytes, the tidu is 1460 bytes
493 497 * (from TCP over ethernet), and the arguments to RPC require
494 498 * 2800 bytes. Ideally we want the protocol to render two
495 499 * ~1400 byte segments over the wire. If allocb() gives us a 2k
496 500 * mblk, and we allocate a second mblk for the rest, the
497 501 * protocol module may generate 3 segments over the wire:
498 502 * 1460 bytes for the first, 448 (2048 - 1600) for the 2nd, and
499 503 * 892 for the 3rd. If we "waste" 448 bytes in the first mblk,
500 504 * the XDR encoding will generate two ~1400 byte mblks, and the
501 505 * protocol module is more likely to produce properly sized
502 506 * segments.
503 507 */
504 508 if ((mpsize >> 1) <= len) {
505 509 mp->b_rptr += (mpsize - len);
506 510 }
507 511
508 512 /*
509 513 * Adjust b_rptr to reserve space for the non-data protocol
510 514 * headers that any downstream modules might like to add, and
511 515 * for the record marking header.
512 516 */
513 517 mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE);
514 518
515 519 XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base));
516 520 ASSERT(mp->b_wptr == mp->b_rptr);
517 521
518 522 msg->rm_xid = clone_xprt->xp_xid;
519 523
520 524 TRACE_0(TR_FAC_KRPC, TR_XDR_REPLYMSG_START,
521 525 "xdr_replymsg_start:");
522 526 if (!(xdr_replymsg(xdrs, msg) &&
523 527 (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
524 528 xdr_results, xdr_location)))) {
525 529 XDR_DESTROY(xdrs);
526 530 TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
527 531 "xdr_replymsg_end:(%S)", "bad");
528 532 freemsg(mp);
529 533 RPCLOG0(1, "svc_cots_ksend: xdr_replymsg/SVCAUTH_WRAP "
530 534 "failed\n");
531 535 goto out;
532 536 }
533 537 TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
534 538 "xdr_replymsg_end:(%S)", "good");
535 539 }
536 540
537 541 XDR_DESTROY(xdrs);
538 542
539 543 put(clone_xprt->xp_wq, mp);
540 544 retval = TRUE;
541 545
542 546 out:
543 547 /*
544 548 * This is completely disgusting. If public is set it is
545 549 * a pointer to a structure whose first field is the address
546 550 * of the function to free that structure and any related
547 551 * stuff. (see rrokfree in nfs_xdr.c).
548 552 */
549 553 if (xdrs->x_public) {
550 554 /* LINTED pointer alignment */
551 555 (**((int (**)())xdrs->x_public))(xdrs->x_public);
552 556 }
553 557
554 558 TRACE_1(TR_FAC_KRPC, TR_SVC_COTS_KSEND_END,
555 559 "svc_cots_ksend_end:(%S)", "done");
556 560 return (retval);
557 561 }
558 562
559 563 /*
560 564 * Deserialize arguments.
561 565 */
562 566 static bool_t
563 567 svc_cots_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
564 568 caddr_t args_ptr)
565 569 {
566 570 return (SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
567 571 xdr_args, args_ptr));
568 572 }
569 573
570 574 static bool_t
571 575 svc_cots_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
572 576 caddr_t args_ptr)
573 577 {
574 578 cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
575 579 /* LINTED pointer alignment */
576 580 XDR *xdrs = &clone_xprt->xp_xdrin;
577 581 mblk_t *mp;
578 582 bool_t retval;
579 583
580 584 /*
581 585 * It is important to call the XDR routine before
582 586 * freeing the request mblk. Structures in the
583 587 * XDR data may point into the mblk and require that
584 588 * the memory be intact during the free routine.
585 589 */
586 590 if (args_ptr) {
587 591 xdrs->x_op = XDR_FREE;
588 592 retval = (*xdr_args)(xdrs, args_ptr);
589 593 } else
590 594 retval = TRUE;
591 595
592 596 XDR_DESTROY(xdrs);
593 597
594 598 if ((mp = cd->cd_req_mp) != NULL) {
595 599 cd->cd_req_mp = (mblk_t *)0;
596 600 freemsg(mp);
597 601 }
598 602
599 603 return (retval);
600 604 }
601 605
602 606 static int32_t *
603 607 svc_cots_kgetres(SVCXPRT *clone_xprt, int size)
604 608 {
605 609 /* LINTED pointer alignment */
606 610 cots_data_t *cd = (cots_data_t *)clone_xprt->xp_p2buf;
607 611 XDR *xdrs = &clone_xprt->xp_xdrout;
608 612 mblk_t *mp;
609 613 int32_t *buf;
610 614 struct rpc_msg rply;
611 615 int len;
612 616 int mpsize;
613 617
614 618 /*
615 619 * Leave space for protocol headers.
616 620 */
617 621 len = MSG_OFFSET + clone_xprt->xp_msg_size;
618 622
619 623 /*
620 624 * Allocate an initial mblk for the response data.
621 625 */
622 626 while ((mp = allocb(len, BPRI_LO)) == NULL) {
623 627 if (strwaitbuf(len, BPRI_LO))
624 628 return (NULL);
625 629 }
626 630
627 631 /*
628 632 * Initialize the XDR encode stream. Additional mblks
629 633 * will be allocated if necessary. They will be TIDU
630 634 * sized.
631 635 */
632 636 xdrmblk_init(xdrs, mp, XDR_ENCODE, clone_xprt->xp_msg_size);
633 637 mpsize = MBLKSIZE(mp);
634 638 ASSERT(mpsize >= len);
635 639 ASSERT(mp->b_rptr == mp->b_datap->db_base);
636 640
637 641 /*
638 642 * If the size of mblk is not appreciably larger than what we
639 643 * asked, then resize the mblk to exactly len bytes. Reason for
640 644 * this: suppose len is 1600 bytes, the tidu is 1460 bytes
641 645 * (from TCP over ethernet), and the arguments to RPC require
642 646 * 2800 bytes. Ideally we want the protocol to render two
643 647 * ~1400 byte segments over the wire. If allocb() gives us a 2k
644 648 * mblk, and we allocate a second mblk for the rest, the
645 649 * protocol module may generate 3 segments over the wire:
646 650 * 1460 bytes for the first, 448 (2048 - 1600) for the 2nd, and
647 651 * 892 for the 3rd. If we "waste" 448 bytes in the first mblk,
648 652 * the XDR encoding will generate two ~1400 byte mblks, and the
649 653 * protocol module is more likely to produce properly sized
650 654 * segments.
651 655 */
652 656 if ((mpsize >> 1) <= len) {
653 657 mp->b_rptr += (mpsize - len);
654 658 }
655 659
656 660 /*
657 661 * Adjust b_rptr to reserve space for the non-data protocol
658 662 * headers that any downstream modules might like to add, and
659 663 * for the record marking header.
660 664 */
661 665 mp->b_rptr += (MSG_OFFSET + RM_HDR_SIZE);
662 666
663 667 XDR_SETPOS(xdrs, (uint_t)(mp->b_rptr - mp->b_datap->db_base));
664 668 ASSERT(mp->b_wptr == mp->b_rptr);
665 669
666 670 /*
667 671 * Assume a successful RPC since most of them are.
668 672 */
669 673 rply.rm_xid = clone_xprt->xp_xid;
670 674 rply.rm_direction = REPLY;
671 675 rply.rm_reply.rp_stat = MSG_ACCEPTED;
672 676 rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
673 677 rply.acpted_rply.ar_stat = SUCCESS;
674 678
675 679 if (!xdr_replymsg_hdr(xdrs, &rply)) {
676 680 XDR_DESTROY(xdrs);
677 681 freeb(mp);
678 682 return (NULL);
679 683 }
680 684
681 685 buf = XDR_INLINE(xdrs, size);
682 686 if (buf == NULL) {
683 687 XDR_DESTROY(xdrs);
684 688 ASSERT(cd->cd_mp == NULL);
685 689 freemsg(mp);
686 690 } else {
687 691 cd->cd_mp = mp;
688 692 }
689 693 return (buf);
690 694 }
691 695
692 696 static void
693 697 svc_cots_kfreeres(SVCXPRT *clone_xprt)
694 698 {
695 699 cots_data_t *cd;
696 700 mblk_t *mp;
697 701
698 702 cd = (cots_data_t *)clone_xprt->xp_p2buf;
699 703 if ((mp = cd->cd_mp) != NULL) {
700 704 XDR_DESTROY(&clone_xprt->xp_xdrout);
701 705 cd->cd_mp = (mblk_t *)NULL;
702 706 freemsg(mp);
703 707 }
704 708 }
705 709
706 710 /*
707 711 * the dup cacheing routines below provide a cache of non-failure
708 712 * transaction id's. rpc service routines can use this to detect
709 713 * retransmissions and re-send a non-failure response.
710 714 */
711 715
712 716 /*
713 717 * MAXDUPREQS is the number of cached items. It should be adjusted
714 718 * to the service load so that there is likely to be a response entry
715 719 * when the first retransmission comes in.
716 720 */
717 721 #define MAXDUPREQS 8192
718 722
719 723 /*
|
↓ open down ↓ |
602 lines elided |
↑ open up ↑ |
720 724 * This should be appropriately scaled to MAXDUPREQS. To produce as less as
721 725 * possible collisions it is suggested to set this to a prime.
722 726 */
723 727 #define DRHASHSZ 2053
724 728
725 729 #define XIDHASH(xid) ((xid) % DRHASHSZ)
726 730 #define DRHASH(dr) XIDHASH((dr)->dr_xid)
727 731 #define REQTOXID(req) ((req)->rq_xprt->xp_xid)
728 732
729 733 static int cotsndupreqs = 0;
730 -int cotsmaxdupreqs = MAXDUPREQS;
734 +volatile int cotsmaxdupreqs = MAXDUPREQS;
731 735 static kmutex_t cotsdupreq_lock;
732 736 static struct dupreq *cotsdrhashtbl[DRHASHSZ];
733 737 static int cotsdrhashstat[DRHASHSZ];
734 738
735 739 static void unhash(struct dupreq *);
736 740
737 741 /*
738 742 * cotsdrmru points to the head of a circular linked list in lru order.
739 743 * cotsdrmru->dr_next == drlru
740 744 */
741 745 struct dupreq *cotsdrmru;
742 746
743 747 /*
744 748 * PSARC 2003/523 Contract Private Interface
745 749 * svc_cots_kdup
746 750 * Changes must be reviewed by Solaris File Sharing
747 751 * Changes must be communicated to contract-2003-523@sun.com
748 752 *
749 753 * svc_cots_kdup searches the request cache and returns 0 if the
750 754 * request is not found in the cache. If it is found, then it
751 755 * returns the state of the request (in progress or done) and
752 756 * the status or attributes that were part of the original reply.
753 757 *
754 758 * If DUP_DONE (there is a duplicate) svc_cots_kdup copies over the
755 759 * value of the response. In that case, also return in *dupcachedp
756 760 * whether the response free routine is cached in the dupreq - in which case
757 761 * the caller should not be freeing it, because it will be done later
758 762 * in the svc_cots_kdup code when the dupreq is reused.
759 763 */
760 764 static int
761 765 svc_cots_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
762 766 bool_t *dupcachedp)
763 767 {
764 768 struct rpc_cots_server *stats = CLONE2STATS(req->rq_xprt);
765 769 struct dupreq *dr;
766 770 uint32_t xid;
767 771 uint32_t drhash;
768 772 int status;
769 773
770 774 xid = REQTOXID(req);
771 775 mutex_enter(&cotsdupreq_lock);
772 776 RSSTAT_INCR(stats, rsdupchecks);
773 777 /*
774 778 * Check to see whether an entry already exists in the cache.
775 779 */
776 780 dr = cotsdrhashtbl[XIDHASH(xid)];
777 781 while (dr != NULL) {
778 782 if (dr->dr_xid == xid &&
779 783 dr->dr_proc == req->rq_proc &&
780 784 dr->dr_prog == req->rq_prog &&
781 785 dr->dr_vers == req->rq_vers &&
782 786 dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
783 787 bcmp((caddr_t)dr->dr_addr.buf,
784 788 (caddr_t)req->rq_xprt->xp_rtaddr.buf,
785 789 dr->dr_addr.len) == 0) {
786 790 status = dr->dr_status;
787 791 if (status == DUP_DONE) {
788 792 bcopy(dr->dr_resp.buf, res, size);
789 793 if (dupcachedp != NULL)
790 794 *dupcachedp = (dr->dr_resfree != NULL);
791 795 TRACE_0(TR_FAC_KRPC, TR_SVC_COTS_KDUP_DONE,
792 796 "svc_cots_kdup: DUP_DONE");
793 797 } else {
794 798 dr->dr_status = DUP_INPROGRESS;
795 799 *drpp = dr;
796 800 TRACE_0(TR_FAC_KRPC,
797 801 TR_SVC_COTS_KDUP_INPROGRESS,
798 802 "svc_cots_kdup: DUP_INPROGRESS");
799 803 }
800 804 RSSTAT_INCR(stats, rsdupreqs);
801 805 mutex_exit(&cotsdupreq_lock);
802 806 return (status);
803 807 }
804 808 dr = dr->dr_chain;
805 809 }
806 810
807 811 /*
808 812 * There wasn't an entry, either allocate a new one or recycle
809 813 * an old one.
810 814 */
811 815 if (cotsndupreqs < cotsmaxdupreqs) {
812 816 dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
813 817 if (dr == NULL) {
814 818 mutex_exit(&cotsdupreq_lock);
815 819 return (DUP_ERROR);
816 820 }
817 821 dr->dr_resp.buf = NULL;
818 822 dr->dr_resp.maxlen = 0;
819 823 dr->dr_addr.buf = NULL;
820 824 dr->dr_addr.maxlen = 0;
821 825 if (cotsdrmru) {
822 826 dr->dr_next = cotsdrmru->dr_next;
823 827 cotsdrmru->dr_next = dr;
824 828 } else {
825 829 dr->dr_next = dr;
826 830 }
827 831 cotsndupreqs++;
828 832 } else {
829 833 dr = cotsdrmru->dr_next;
830 834 while (dr->dr_status == DUP_INPROGRESS) {
831 835 dr = dr->dr_next;
832 836 if (dr == cotsdrmru->dr_next) {
833 837 cmn_err(CE_WARN, "svc_cots_kdup no slots free");
834 838 mutex_exit(&cotsdupreq_lock);
835 839 return (DUP_ERROR);
836 840 }
837 841 }
838 842 unhash(dr);
839 843 if (dr->dr_resfree) {
840 844 (*dr->dr_resfree)(dr->dr_resp.buf);
841 845 }
842 846 }
843 847 dr->dr_resfree = NULL;
844 848 cotsdrmru = dr;
845 849
846 850 dr->dr_xid = REQTOXID(req);
847 851 dr->dr_prog = req->rq_prog;
848 852 dr->dr_vers = req->rq_vers;
849 853 dr->dr_proc = req->rq_proc;
850 854 if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
851 855 if (dr->dr_addr.buf != NULL)
852 856 kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
853 857 dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
854 858 dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen, KM_NOSLEEP);
855 859 if (dr->dr_addr.buf == NULL) {
856 860 dr->dr_addr.maxlen = 0;
857 861 dr->dr_status = DUP_DROP;
858 862 mutex_exit(&cotsdupreq_lock);
859 863 return (DUP_ERROR);
860 864 }
861 865 }
862 866 dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
863 867 bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
864 868 if (dr->dr_resp.maxlen < size) {
865 869 if (dr->dr_resp.buf != NULL)
866 870 kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
867 871 dr->dr_resp.maxlen = (unsigned int)size;
868 872 dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
869 873 if (dr->dr_resp.buf == NULL) {
870 874 dr->dr_resp.maxlen = 0;
871 875 dr->dr_status = DUP_DROP;
872 876 mutex_exit(&cotsdupreq_lock);
873 877 return (DUP_ERROR);
874 878 }
875 879 }
876 880 dr->dr_status = DUP_INPROGRESS;
877 881
878 882 drhash = (uint32_t)DRHASH(dr);
879 883 dr->dr_chain = cotsdrhashtbl[drhash];
880 884 cotsdrhashtbl[drhash] = dr;
881 885 cotsdrhashstat[drhash]++;
882 886 mutex_exit(&cotsdupreq_lock);
883 887 *drpp = dr;
884 888 return (DUP_NEW);
885 889 }
886 890
887 891 /*
888 892 * PSARC 2003/523 Contract Private Interface
889 893 * svc_cots_kdupdone
890 894 * Changes must be reviewed by Solaris File Sharing
891 895 * Changes must be communicated to contract-2003-523@sun.com
892 896 *
893 897 * svc_cots_kdupdone marks the request done (DUP_DONE or DUP_DROP)
894 898 * and stores the response.
895 899 */
896 900 static void
897 901 svc_cots_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
898 902 int size, int status)
899 903 {
900 904 ASSERT(dr->dr_resfree == NULL);
901 905 if (status == DUP_DONE) {
902 906 bcopy(res, dr->dr_resp.buf, size);
903 907 dr->dr_resfree = dis_resfree;
904 908 }
905 909 dr->dr_status = status;
906 910 }
907 911
908 912 /*
909 913 * This routine expects that the mutex, cotsdupreq_lock, is already held.
910 914 */
911 915 static void
912 916 unhash(struct dupreq *dr)
913 917 {
914 918 struct dupreq *drt;
915 919 struct dupreq *drtprev = NULL;
916 920 uint32_t drhash;
917 921
918 922 ASSERT(MUTEX_HELD(&cotsdupreq_lock));
919 923
920 924 drhash = (uint32_t)DRHASH(dr);
921 925 drt = cotsdrhashtbl[drhash];
922 926 while (drt != NULL) {
923 927 if (drt == dr) {
924 928 cotsdrhashstat[drhash]--;
925 929 if (drtprev == NULL) {
926 930 cotsdrhashtbl[drhash] = drt->dr_chain;
927 931 } else {
928 932 drtprev->dr_chain = drt->dr_chain;
929 933 }
930 934 return;
931 935 }
932 936 drtprev = drt;
933 937 drt = drt->dr_chain;
934 938 }
935 939 }
936 940
937 941 void
938 942 svc_cots_stats_init(zoneid_t zoneid, struct rpc_cots_server **statsp)
939 943 {
940 944 *statsp = (struct rpc_cots_server *)rpcstat_zone_init_common(zoneid,
941 945 "unix", "rpc_cots_server", (const kstat_named_t *)&cots_rsstat_tmpl,
942 946 sizeof (cots_rsstat_tmpl));
943 947 }
944 948
945 949 void
946 950 svc_cots_stats_fini(zoneid_t zoneid, struct rpc_cots_server **statsp)
947 951 {
948 952 rpcstat_zone_fini_common(zoneid, "unix", "rpc_cots_server");
949 953 kmem_free(*statsp, sizeof (cots_rsstat_tmpl));
950 954 }
951 955
952 956 void
953 957 svc_cots_init(void)
954 958 {
955 959 /*
956 960 * Check to make sure that the cots private data will fit into
957 961 * the stack buffer allocated by svc_run. The ASSERT is a safety
958 962 * net if the cots_data_t structure ever changes.
959 963 */
960 964 /*CONSTANTCONDITION*/
961 965 ASSERT(sizeof (cots_data_t) <= SVC_P2LEN);
962 966
963 967 mutex_init(&cots_kcreate_lock, NULL, MUTEX_DEFAULT, NULL);
964 968 mutex_init(&cotsdupreq_lock, NULL, MUTEX_DEFAULT, NULL);
965 969 }
|
↓ open down ↓ |
225 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX