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