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_clts.c
+++ new/usr/src/uts/common/rpc/svc_clts.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) 1989, 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_clts.c
35 35 * Server side for 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/strsun.h>
45 45 #include <sys/strsubr.h>
46 46 #include <sys/tihdr.h>
47 47 #include <sys/tiuser.h>
48 48 #include <sys/t_kuser.h>
49 49 #include <sys/fcntl.h>
50 50 #include <sys/errno.h>
51 51 #include <sys/kmem.h>
52 52 #include <sys/systm.h>
53 53 #include <sys/cmn_err.h>
54 54 #include <sys/kstat.h>
55 55 #include <sys/vtrace.h>
56 56 #include <sys/debug.h>
57 57
58 58 #include <rpc/types.h>
59 59 #include <rpc/xdr.h>
60 60 #include <rpc/auth.h>
61 61 #include <rpc/clnt.h>
62 62 #include <rpc/rpc_msg.h>
63 63 #include <rpc/svc.h>
64 64 #include <inet/ip.h>
65 65
66 66 /*
67 67 * Routines exported through ops vector.
68 68 */
69 69 static bool_t svc_clts_krecv(SVCXPRT *, mblk_t *, struct rpc_msg *);
70 70 static bool_t svc_clts_ksend(SVCXPRT *, struct rpc_msg *);
71 71 static bool_t svc_clts_kgetargs(SVCXPRT *, xdrproc_t, caddr_t);
72 72 static bool_t svc_clts_kfreeargs(SVCXPRT *, xdrproc_t, caddr_t);
73 73 static void svc_clts_kdestroy(SVCMASTERXPRT *);
74 74 static int svc_clts_kdup(struct svc_req *, caddr_t, int,
75 75 struct dupreq **, bool_t *);
76 76 static void svc_clts_kdupdone(struct dupreq *, caddr_t,
77 77 void (*)(), int, int);
78 78 static int32_t *svc_clts_kgetres(SVCXPRT *, int);
79 79 static void svc_clts_kclone_destroy(SVCXPRT *);
80 80 static void svc_clts_kfreeres(SVCXPRT *);
81 81 static void svc_clts_kstart(SVCMASTERXPRT *);
82 82 static void svc_clts_kclone_xprt(SVCXPRT *, SVCXPRT *);
83 83 static void svc_clts_ktattrs(SVCXPRT *, int, void **);
84 84
85 85 /*
86 86 * Server transport operations vector.
87 87 */
88 88 struct svc_ops svc_clts_op = {
89 89 svc_clts_krecv, /* Get requests */
90 90 svc_clts_kgetargs, /* Deserialize arguments */
91 91 svc_clts_ksend, /* Send reply */
92 92 svc_clts_kfreeargs, /* Free argument data space */
93 93 svc_clts_kdestroy, /* Destroy transport handle */
94 94 svc_clts_kdup, /* Check entry in dup req cache */
95 95 svc_clts_kdupdone, /* Mark entry in dup req cache as done */
96 96 svc_clts_kgetres, /* Get pointer to response buffer */
97 97 svc_clts_kfreeres, /* Destroy pre-serialized response header */
98 98 svc_clts_kclone_destroy, /* Destroy a clone xprt */
99 99 svc_clts_kstart, /* Tell `ready-to-receive' to rpcmod */
100 100 svc_clts_kclone_xprt, /* transport specific clone xprt function */
101 101 svc_clts_ktattrs /* Transport specific attributes. */
102 102 };
103 103
104 104 /*
105 105 * Transport private data.
106 106 * Kept in xprt->xp_p2buf.
107 107 */
108 108 struct udp_data {
109 109 mblk_t *ud_resp; /* buffer for response */
110 110 mblk_t *ud_inmp; /* mblk chain of request */
111 111 };
112 112
113 113 #define UD_MAXSIZE 8800
114 114 #define UD_INITSIZE 2048
115 115
116 116 /*
117 117 * Connectionless server statistics
118 118 */
119 119 static const struct rpc_clts_server {
120 120 kstat_named_t rscalls;
121 121 kstat_named_t rsbadcalls;
122 122 kstat_named_t rsnullrecv;
123 123 kstat_named_t rsbadlen;
124 124 kstat_named_t rsxdrcall;
125 125 kstat_named_t rsdupchecks;
126 126 kstat_named_t rsdupreqs;
127 127 } clts_rsstat_tmpl = {
128 128 { "calls", KSTAT_DATA_UINT64 },
129 129 { "badcalls", KSTAT_DATA_UINT64 },
130 130 { "nullrecv", KSTAT_DATA_UINT64 },
131 131 { "badlen", KSTAT_DATA_UINT64 },
132 132 { "xdrcall", KSTAT_DATA_UINT64 },
133 133 { "dupchecks", KSTAT_DATA_UINT64 },
↓ open down ↓ |
133 lines elided |
↑ open up ↑ |
134 134 { "dupreqs", KSTAT_DATA_UINT64 }
135 135 };
136 136
137 137 static uint_t clts_rsstat_ndata =
138 138 sizeof (clts_rsstat_tmpl) / sizeof (kstat_named_t);
139 139
140 140 #define CLONE2STATS(clone_xprt) \
141 141 (struct rpc_clts_server *)(clone_xprt)->xp_master->xp_p2
142 142
143 143 #define RSSTAT_INCR(stats, x) \
144 - atomic_add_64(&(stats)->x.value.ui64, 1)
144 + atomic_inc_64(&(stats)->x.value.ui64)
145 145
146 146 /*
147 147 * Create a transport record.
148 148 * The transport record, output buffer, and private data structure
149 149 * are allocated. The output buffer is serialized into using xdrmem.
150 150 * There is one transport record per user process which implements a
151 151 * set of services.
152 152 */
153 153 /* ARGSUSED */
154 154 int
155 155 svc_clts_kcreate(file_t *fp, uint_t sendsz, struct T_info_ack *tinfo,
156 156 SVCMASTERXPRT **nxprt)
157 157 {
158 158 SVCMASTERXPRT *xprt;
159 159 struct rpcstat *rpcstat;
160 160
161 161 if (nxprt == NULL)
162 162 return (EINVAL);
163 163
164 164 rpcstat = zone_getspecific(rpcstat_zone_key, curproc->p_zone);
165 165 ASSERT(rpcstat != NULL);
166 166
167 167 xprt = kmem_zalloc(sizeof (*xprt), KM_SLEEP);
168 168 xprt->xp_lcladdr.buf = kmem_zalloc(sizeof (sin6_t), KM_SLEEP);
169 169 xprt->xp_p2 = (caddr_t)rpcstat->rpc_clts_server;
170 170 xprt->xp_ops = &svc_clts_op;
171 171 xprt->xp_msg_size = tinfo->TSDU_size;
172 172
173 173 xprt->xp_rtaddr.buf = NULL;
174 174 xprt->xp_rtaddr.maxlen = tinfo->ADDR_size;
175 175 xprt->xp_rtaddr.len = 0;
176 176
177 177 *nxprt = xprt;
178 178
179 179 return (0);
180 180 }
181 181
182 182 /*
183 183 * Destroy a transport record.
184 184 * Frees the space allocated for a transport record.
185 185 */
186 186 static void
187 187 svc_clts_kdestroy(SVCMASTERXPRT *xprt)
188 188 {
189 189 if (xprt->xp_netid)
190 190 kmem_free(xprt->xp_netid, strlen(xprt->xp_netid) + 1);
191 191 if (xprt->xp_addrmask.maxlen)
192 192 kmem_free(xprt->xp_addrmask.buf, xprt->xp_addrmask.maxlen);
193 193
194 194 mutex_destroy(&xprt->xp_req_lock);
195 195 mutex_destroy(&xprt->xp_thread_lock);
196 196
197 197 kmem_free(xprt->xp_lcladdr.buf, sizeof (sin6_t));
198 198 kmem_free(xprt, sizeof (SVCMASTERXPRT));
199 199 }
200 200
201 201 /*
202 202 * Transport-type specific part of svc_xprt_cleanup().
203 203 * Frees the message buffer space allocated for a clone of a transport record
204 204 */
205 205 static void
206 206 svc_clts_kclone_destroy(SVCXPRT *clone_xprt)
207 207 {
208 208 /* LINTED pointer alignment */
209 209 struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
210 210
211 211 if (ud->ud_resp) {
212 212 /*
213 213 * There should not be any left over results buffer.
214 214 */
215 215 ASSERT(ud->ud_resp->b_cont == NULL);
216 216
217 217 /*
218 218 * Free the T_UNITDATA_{REQ/IND} that svc_clts_krecv
219 219 * saved.
220 220 */
221 221 freeb(ud->ud_resp);
222 222 }
223 223 if (ud->ud_inmp)
224 224 freemsg(ud->ud_inmp);
225 225 }
226 226
227 227 /*
228 228 * svc_tli_kcreate() calls this function at the end to tell
229 229 * rpcmod that the transport is ready to receive requests.
230 230 */
231 231 /* ARGSUSED */
232 232 static void
233 233 svc_clts_kstart(SVCMASTERXPRT *xprt)
234 234 {
235 235 }
236 236
237 237 static void
238 238 svc_clts_kclone_xprt(SVCXPRT *src_xprt, SVCXPRT *dst_xprt)
239 239 {
240 240 struct udp_data *ud_src =
241 241 (struct udp_data *)src_xprt->xp_p2buf;
242 242 struct udp_data *ud_dst =
243 243 (struct udp_data *)dst_xprt->xp_p2buf;
244 244
245 245 if (ud_src->ud_resp)
246 246 ud_dst->ud_resp = dupb(ud_src->ud_resp);
247 247
248 248 }
249 249
250 250 static void
251 251 svc_clts_ktattrs(SVCXPRT *clone_xprt, int attrflag, void **tattr)
252 252 {
253 253 *tattr = NULL;
254 254
255 255 switch (attrflag) {
256 256 case SVC_TATTR_ADDRMASK:
257 257 *tattr = (void *)&clone_xprt->xp_master->xp_addrmask;
258 258 }
259 259 }
260 260
261 261 /*
262 262 * Receive rpc requests.
263 263 * Pulls a request in off the socket, checks if the packet is intact,
264 264 * and deserializes the call packet.
265 265 */
266 266 static bool_t
267 267 svc_clts_krecv(SVCXPRT *clone_xprt, mblk_t *mp, struct rpc_msg *msg)
268 268 {
269 269 /* LINTED pointer alignment */
270 270 struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
271 271 XDR *xdrs = &clone_xprt->xp_xdrin;
272 272 struct rpc_clts_server *stats = CLONE2STATS(clone_xprt);
273 273 union T_primitives *pptr;
274 274 int hdrsz;
275 275 cred_t *cr;
276 276
277 277 TRACE_0(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_START,
278 278 "svc_clts_krecv_start:");
279 279
280 280 RSSTAT_INCR(stats, rscalls);
281 281
282 282 /*
283 283 * The incoming request should start with an M_PROTO message.
284 284 */
285 285 if (mp->b_datap->db_type != M_PROTO) {
286 286 goto bad;
287 287 }
288 288
289 289 /*
290 290 * The incoming request should be an T_UNITDTA_IND. There
291 291 * might be other messages coming up the stream, but we can
292 292 * ignore them.
293 293 */
294 294 pptr = (union T_primitives *)mp->b_rptr;
295 295 if (pptr->type != T_UNITDATA_IND) {
296 296 goto bad;
297 297 }
298 298 /*
299 299 * Do some checking to make sure that the header at least looks okay.
300 300 */
301 301 hdrsz = (int)(mp->b_wptr - mp->b_rptr);
302 302 if (hdrsz < TUNITDATAINDSZ ||
303 303 hdrsz < (pptr->unitdata_ind.OPT_offset +
304 304 pptr->unitdata_ind.OPT_length) ||
305 305 hdrsz < (pptr->unitdata_ind.SRC_offset +
306 306 pptr->unitdata_ind.SRC_length)) {
307 307 goto bad;
308 308 }
309 309
310 310 /*
311 311 * Make sure that the transport provided a usable address.
312 312 */
313 313 if (pptr->unitdata_ind.SRC_length <= 0) {
314 314 goto bad;
315 315 }
316 316 /*
317 317 * Point the remote transport address in the service_transport
318 318 * handle at the address in the request.
319 319 */
320 320 clone_xprt->xp_rtaddr.buf = (char *)mp->b_rptr +
321 321 pptr->unitdata_ind.SRC_offset;
322 322 clone_xprt->xp_rtaddr.len = pptr->unitdata_ind.SRC_length;
323 323
324 324 /*
325 325 * Copy the local transport address in the service_transport
326 326 * handle at the address in the request. We will have only
327 327 * the local IP address in options.
328 328 */
329 329 ((sin_t *)(clone_xprt->xp_lcladdr.buf))->sin_family = AF_UNSPEC;
330 330 if (pptr->unitdata_ind.OPT_length && pptr->unitdata_ind.OPT_offset) {
331 331 char *dstopt = (char *)mp->b_rptr +
332 332 pptr->unitdata_ind.OPT_offset;
333 333 struct T_opthdr *toh = (struct T_opthdr *)dstopt;
334 334
335 335 if (toh->level == IPPROTO_IPV6 && toh->status == 0 &&
336 336 toh->name == IPV6_PKTINFO) {
337 337 struct in6_pktinfo *pkti;
338 338
339 339 dstopt += sizeof (struct T_opthdr);
340 340 pkti = (struct in6_pktinfo *)dstopt;
341 341 ((sin6_t *)(clone_xprt->xp_lcladdr.buf))->sin6_addr
342 342 = pkti->ipi6_addr;
343 343 ((sin6_t *)(clone_xprt->xp_lcladdr.buf))->sin6_family
344 344 = AF_INET6;
345 345 } else if (toh->level == IPPROTO_IP && toh->status == 0 &&
346 346 toh->name == IP_RECVDSTADDR) {
347 347 dstopt += sizeof (struct T_opthdr);
348 348 ((sin_t *)(clone_xprt->xp_lcladdr.buf))->sin_addr
349 349 = *(struct in_addr *)dstopt;
350 350 ((sin_t *)(clone_xprt->xp_lcladdr.buf))->sin_family
351 351 = AF_INET;
352 352 }
353 353 }
354 354
355 355 /*
356 356 * Save the first mblk which contains the T_unidata_ind in
357 357 * ud_resp. It will be used to generate the T_unitdata_req
358 358 * during the reply.
359 359 * We reuse any options in the T_unitdata_ind for the T_unitdata_req
360 360 * since we must pass any SCM_UCRED across in order for TX to
361 361 * work. We also make sure any cred_t is carried across.
362 362 */
363 363 if (ud->ud_resp) {
364 364 if (ud->ud_resp->b_cont != NULL) {
365 365 cmn_err(CE_WARN, "svc_clts_krecv: ud_resp %p, "
366 366 "b_cont %p", (void *)ud->ud_resp,
367 367 (void *)ud->ud_resp->b_cont);
368 368 }
369 369 freeb(ud->ud_resp);
370 370 }
371 371 /* Move any cred_t to the first mblk in the message */
372 372 cr = msg_getcred(mp, NULL);
373 373 if (cr != NULL)
374 374 mblk_setcred(mp, cr, NOPID);
375 375
376 376 ud->ud_resp = mp;
377 377 mp = mp->b_cont;
378 378 ud->ud_resp->b_cont = NULL;
379 379
380 380 xdrmblk_init(xdrs, mp, XDR_DECODE, 0);
381 381
382 382 TRACE_0(TR_FAC_KRPC, TR_XDR_CALLMSG_START,
383 383 "xdr_callmsg_start:");
384 384 if (! xdr_callmsg(xdrs, msg)) {
385 385 TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
386 386 "xdr_callmsg_end:(%S)", "bad");
387 387 RSSTAT_INCR(stats, rsxdrcall);
388 388 goto bad;
389 389 }
390 390 TRACE_1(TR_FAC_KRPC, TR_XDR_CALLMSG_END,
391 391 "xdr_callmsg_end:(%S)", "good");
392 392
393 393 clone_xprt->xp_xid = msg->rm_xid;
394 394 ud->ud_inmp = mp;
395 395
396 396 TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_END,
397 397 "svc_clts_krecv_end:(%S)", "good");
398 398 return (TRUE);
399 399
400 400 bad:
401 401 freemsg(mp);
402 402 if (ud->ud_resp) {
403 403 /*
404 404 * There should not be any left over results buffer.
405 405 */
406 406 ASSERT(ud->ud_resp->b_cont == NULL);
407 407 freeb(ud->ud_resp);
408 408 ud->ud_resp = NULL;
409 409 }
410 410
411 411 RSSTAT_INCR(stats, rsbadcalls);
412 412 TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KRECV_END,
413 413 "svc_clts_krecv_end:(%S)", "bad");
414 414 return (FALSE);
415 415 }
416 416
417 417 /*
418 418 * Send rpc reply.
419 419 * Serialize the reply packet into the output buffer then
420 420 * call t_ksndudata to send it.
421 421 */
422 422 static bool_t
423 423 svc_clts_ksend(SVCXPRT *clone_xprt, struct rpc_msg *msg)
424 424 {
425 425 /* LINTED pointer alignment */
426 426 struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
427 427 XDR *xdrs = &clone_xprt->xp_xdrout;
428 428 int stat = FALSE;
429 429 mblk_t *mp;
430 430 int msgsz;
431 431 struct T_unitdata_req *udreq;
432 432 xdrproc_t xdr_results;
433 433 caddr_t xdr_location;
434 434 bool_t has_args;
435 435
436 436 TRACE_0(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_START,
437 437 "svc_clts_ksend_start:");
438 438
439 439 ASSERT(ud->ud_resp != NULL);
440 440
441 441 /*
442 442 * If there is a result procedure specified in the reply message,
443 443 * it will be processed in the xdr_replymsg and SVCAUTH_WRAP.
444 444 * We need to make sure it won't be processed twice, so we null
445 445 * it for xdr_replymsg here.
446 446 */
447 447 has_args = FALSE;
448 448 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
449 449 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
450 450 if ((xdr_results = msg->acpted_rply.ar_results.proc) != NULL) {
451 451 has_args = TRUE;
452 452 xdr_location = msg->acpted_rply.ar_results.where;
453 453 msg->acpted_rply.ar_results.proc = xdr_void;
454 454 msg->acpted_rply.ar_results.where = NULL;
455 455 }
456 456 }
457 457
458 458 if (ud->ud_resp->b_cont == NULL) {
459 459 /*
460 460 * Allocate an initial mblk for the response data.
461 461 */
462 462 while ((mp = allocb(UD_INITSIZE, BPRI_LO)) == NULL) {
463 463 if (strwaitbuf(UD_INITSIZE, BPRI_LO)) {
464 464 TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_END,
465 465 "svc_clts_ksend_end:(%S)", "strwaitbuf");
466 466 return (FALSE);
467 467 }
468 468 }
469 469
470 470 /*
471 471 * Initialize the XDR decode stream. Additional mblks
472 472 * will be allocated if necessary. They will be UD_MAXSIZE
473 473 * sized.
474 474 */
475 475 xdrmblk_init(xdrs, mp, XDR_ENCODE, UD_MAXSIZE);
476 476
477 477 /*
478 478 * Leave some space for protocol headers.
479 479 */
480 480 (void) XDR_SETPOS(xdrs, 512);
481 481 mp->b_rptr += 512;
482 482
483 483 msg->rm_xid = clone_xprt->xp_xid;
484 484
485 485 ud->ud_resp->b_cont = mp;
486 486
487 487 TRACE_0(TR_FAC_KRPC, TR_XDR_REPLYMSG_START,
488 488 "xdr_replymsg_start:");
489 489 if (!(xdr_replymsg(xdrs, msg) &&
490 490 (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
491 491 xdr_results, xdr_location)))) {
492 492 TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
493 493 "xdr_replymsg_end:(%S)", "bad");
494 494 RPCLOG0(1, "xdr_replymsg/SVCAUTH_WRAP failed\n");
495 495 goto out;
496 496 }
497 497 TRACE_1(TR_FAC_KRPC, TR_XDR_REPLYMSG_END,
498 498 "xdr_replymsg_end:(%S)", "good");
499 499
500 500 } else if (!(xdr_replymsg_body(xdrs, msg) &&
501 501 (!has_args || SVCAUTH_WRAP(&clone_xprt->xp_auth, xdrs,
502 502 xdr_results, xdr_location)))) {
503 503 RPCLOG0(1, "xdr_replymsg_body/SVCAUTH_WRAP failed\n");
504 504 goto out;
505 505 }
506 506
507 507 msgsz = (int)xmsgsize(ud->ud_resp->b_cont);
508 508
509 509 if (msgsz <= 0 || (clone_xprt->xp_msg_size != -1 &&
510 510 msgsz > clone_xprt->xp_msg_size)) {
511 511 #ifdef DEBUG
512 512 cmn_err(CE_NOTE,
513 513 "KRPC: server response message of %d bytes; transport limits are [0, %d]",
514 514 msgsz, clone_xprt->xp_msg_size);
515 515 #endif
516 516 goto out;
517 517 }
518 518
519 519 /*
520 520 * Construct the T_unitdata_req. We take advantage of the fact that
521 521 * T_unitdata_ind looks just like T_unitdata_req, except for the
522 522 * primitive type. Reusing it means we preserve the SCM_UCRED, and
523 523 * we must preserve it for TX to work.
524 524 *
525 525 * This has the side effect that we can also pass certain receive-side
526 526 * options like IPV6_PKTINFO back down the send side. This implies
527 527 * that we can not ASSERT on a non-NULL db_credp when we have send-side
528 528 * options in UDP.
529 529 */
530 530 ASSERT(MBLKL(ud->ud_resp) >= TUNITDATAREQSZ);
531 531 udreq = (struct T_unitdata_req *)ud->ud_resp->b_rptr;
532 532 ASSERT(udreq->PRIM_type == T_UNITDATA_IND);
533 533 udreq->PRIM_type = T_UNITDATA_REQ;
534 534
535 535 /*
536 536 * If the local IPv4 transport address is known use it as a source
537 537 * address for the outgoing UDP packet.
538 538 */
539 539 if (((sin_t *)(clone_xprt->xp_lcladdr.buf))->sin_family == AF_INET) {
540 540 struct T_opthdr *opthdr;
541 541 in_pktinfo_t *pktinfo;
542 542 size_t size;
543 543
544 544 if (udreq->DEST_length == 0)
545 545 udreq->OPT_offset = _TPI_ALIGN_TOPT(TUNITDATAREQSZ);
546 546 else
547 547 udreq->OPT_offset = _TPI_ALIGN_TOPT(udreq->DEST_offset +
548 548 udreq->DEST_length);
549 549
550 550 udreq->OPT_length = sizeof (struct T_opthdr) +
551 551 sizeof (in_pktinfo_t);
552 552
553 553 size = udreq->OPT_length + udreq->OPT_offset;
554 554
555 555 /* make sure we have enough space for the option data */
556 556 mp = reallocb(ud->ud_resp, size, 1);
557 557 if (mp == NULL)
558 558 goto out;
559 559 ud->ud_resp = mp;
560 560 udreq = (struct T_unitdata_req *)mp->b_rptr;
561 561
562 562 /* set desired option header */
563 563 opthdr = (struct T_opthdr *)(mp->b_rptr + udreq->OPT_offset);
564 564 opthdr->len = udreq->OPT_length;
565 565 opthdr->level = IPPROTO_IP;
566 566 opthdr->name = IP_PKTINFO;
567 567
568 568 /*
569 569 * 1. set source IP of outbound packet
570 570 * 2. value '0' for index means IP layer uses this as source
571 571 * address
572 572 */
573 573 pktinfo = (in_pktinfo_t *)(opthdr + 1);
574 574 (void) memset(pktinfo, 0, sizeof (in_pktinfo_t));
575 575 pktinfo->ipi_spec_dst.s_addr =
576 576 ((sin_t *)(clone_xprt->xp_lcladdr.buf))->sin_addr.s_addr;
577 577 pktinfo->ipi_ifindex = 0;
578 578
579 579 /* adjust the end of active data */
580 580 mp->b_wptr = mp->b_rptr + size;
581 581 }
582 582
583 583 put(clone_xprt->xp_wq, ud->ud_resp);
584 584 stat = TRUE;
585 585 ud->ud_resp = NULL;
586 586
587 587 out:
588 588 if (stat == FALSE) {
589 589 freemsg(ud->ud_resp);
590 590 ud->ud_resp = NULL;
591 591 }
592 592
593 593 /*
594 594 * This is completely disgusting. If public is set it is
595 595 * a pointer to a structure whose first field is the address
596 596 * of the function to free that structure and any related
597 597 * stuff. (see rrokfree in nfs_xdr.c).
598 598 */
599 599 if (xdrs->x_public) {
600 600 /* LINTED pointer alignment */
601 601 (**((int (**)())xdrs->x_public))(xdrs->x_public);
602 602 }
603 603
604 604 TRACE_1(TR_FAC_KRPC, TR_SVC_CLTS_KSEND_END,
605 605 "svc_clts_ksend_end:(%S)", "done");
606 606 return (stat);
607 607 }
608 608
609 609 /*
610 610 * Deserialize arguments.
611 611 */
612 612 static bool_t
613 613 svc_clts_kgetargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
614 614 caddr_t args_ptr)
615 615 {
616 616
617 617 /* LINTED pointer alignment */
618 618 return (SVCAUTH_UNWRAP(&clone_xprt->xp_auth, &clone_xprt->xp_xdrin,
619 619 xdr_args, args_ptr));
620 620
621 621 }
622 622
623 623 static bool_t
624 624 svc_clts_kfreeargs(SVCXPRT *clone_xprt, xdrproc_t xdr_args,
625 625 caddr_t args_ptr)
626 626 {
627 627 /* LINTED pointer alignment */
628 628 struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
629 629 XDR *xdrs = &clone_xprt->xp_xdrin;
630 630 bool_t retval;
631 631
632 632 if (args_ptr) {
633 633 xdrs->x_op = XDR_FREE;
634 634 retval = (*xdr_args)(xdrs, args_ptr);
635 635 } else
636 636 retval = TRUE;
637 637
638 638 if (ud->ud_inmp) {
639 639 freemsg(ud->ud_inmp);
640 640 ud->ud_inmp = NULL;
641 641 }
642 642
643 643 return (retval);
644 644 }
645 645
646 646 static int32_t *
647 647 svc_clts_kgetres(SVCXPRT *clone_xprt, int size)
648 648 {
649 649 /* LINTED pointer alignment */
650 650 struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
651 651 XDR *xdrs = &clone_xprt->xp_xdrout;
652 652 mblk_t *mp;
653 653 int32_t *buf;
654 654 struct rpc_msg rply;
655 655
656 656 /*
657 657 * Allocate an initial mblk for the response data.
658 658 */
659 659 while ((mp = allocb(UD_INITSIZE, BPRI_LO)) == NULL) {
660 660 if (strwaitbuf(UD_INITSIZE, BPRI_LO)) {
661 661 return (FALSE);
662 662 }
663 663 }
664 664
665 665 mp->b_cont = NULL;
666 666
667 667 /*
668 668 * Initialize the XDR decode stream. Additional mblks
669 669 * will be allocated if necessary. They will be UD_MAXSIZE
670 670 * sized.
671 671 */
672 672 xdrmblk_init(xdrs, mp, XDR_ENCODE, UD_MAXSIZE);
673 673
674 674 /*
675 675 * Leave some space for protocol headers.
676 676 */
677 677 (void) XDR_SETPOS(xdrs, 512);
678 678 mp->b_rptr += 512;
679 679
680 680 /*
681 681 * Assume a successful RPC since most of them are.
682 682 */
683 683 rply.rm_xid = clone_xprt->xp_xid;
684 684 rply.rm_direction = REPLY;
685 685 rply.rm_reply.rp_stat = MSG_ACCEPTED;
686 686 rply.acpted_rply.ar_verf = clone_xprt->xp_verf;
687 687 rply.acpted_rply.ar_stat = SUCCESS;
688 688
689 689 if (!xdr_replymsg_hdr(xdrs, &rply)) {
690 690 freeb(mp);
691 691 return (NULL);
692 692 }
693 693
694 694 buf = XDR_INLINE(xdrs, size);
695 695
696 696 if (buf == NULL)
697 697 freeb(mp);
698 698 else
699 699 ud->ud_resp->b_cont = mp;
700 700
701 701 return (buf);
702 702 }
703 703
704 704 static void
705 705 svc_clts_kfreeres(SVCXPRT *clone_xprt)
706 706 {
707 707 /* LINTED pointer alignment */
708 708 struct udp_data *ud = (struct udp_data *)clone_xprt->xp_p2buf;
709 709
710 710 if (ud->ud_resp == NULL || ud->ud_resp->b_cont == NULL)
711 711 return;
712 712
713 713 /*
714 714 * SVC_FREERES() is called whenever the server decides not to
715 715 * send normal reply. Thus, we expect only one mblk to be allocated,
716 716 * because we have not attempted any XDR encoding.
717 717 * If we do any XDR encoding and we get an error, then SVC_REPLY()
718 718 * will freemsg(ud->ud_resp);
719 719 */
720 720 ASSERT(ud->ud_resp->b_cont->b_cont == NULL);
721 721 freeb(ud->ud_resp->b_cont);
722 722 ud->ud_resp->b_cont = NULL;
723 723 }
724 724
725 725 /*
726 726 * the dup cacheing routines below provide a cache of non-failure
727 727 * transaction id's. rpc service routines can use this to detect
728 728 * retransmissions and re-send a non-failure response.
729 729 */
730 730
731 731 /*
732 732 * MAXDUPREQS is the number of cached items. It should be adjusted
733 733 * to the service load so that there is likely to be a response entry
734 734 * when the first retransmission comes in.
735 735 */
736 736 #define MAXDUPREQS 1024
737 737
738 738 /*
739 739 * This should be appropriately scaled to MAXDUPREQS.
740 740 */
741 741 #define DRHASHSZ 257
742 742
743 743 #if ((DRHASHSZ & (DRHASHSZ - 1)) == 0)
744 744 #define XIDHASH(xid) ((xid) & (DRHASHSZ - 1))
745 745 #else
746 746 #define XIDHASH(xid) ((xid) % DRHASHSZ)
747 747 #endif
748 748 #define DRHASH(dr) XIDHASH((dr)->dr_xid)
749 749 #define REQTOXID(req) ((req)->rq_xprt->xp_xid)
750 750
751 751 static int ndupreqs = 0;
752 752 int maxdupreqs = MAXDUPREQS;
753 753 static kmutex_t dupreq_lock;
754 754 static struct dupreq *drhashtbl[DRHASHSZ];
755 755 static int drhashstat[DRHASHSZ];
756 756
757 757 static void unhash(struct dupreq *);
758 758
759 759 /*
760 760 * drmru points to the head of a circular linked list in lru order.
761 761 * drmru->dr_next == drlru
762 762 */
763 763 struct dupreq *drmru;
764 764
765 765 /*
766 766 * PSARC 2003/523 Contract Private Interface
767 767 * svc_clts_kdup
768 768 * Changes must be reviewed by Solaris File Sharing
769 769 * Changes must be communicated to contract-2003-523@sun.com
770 770 *
771 771 * svc_clts_kdup searches the request cache and returns 0 if the
772 772 * request is not found in the cache. If it is found, then it
773 773 * returns the state of the request (in progress or done) and
774 774 * the status or attributes that were part of the original reply.
775 775 *
776 776 * If DUP_DONE (there is a duplicate) svc_clts_kdup copies over the
777 777 * value of the response. In that case, also return in *dupcachedp
778 778 * whether the response free routine is cached in the dupreq - in which case
779 779 * the caller should not be freeing it, because it will be done later
780 780 * in the svc_clts_kdup code when the dupreq is reused.
781 781 */
782 782 static int
783 783 svc_clts_kdup(struct svc_req *req, caddr_t res, int size, struct dupreq **drpp,
784 784 bool_t *dupcachedp)
785 785 {
786 786 struct rpc_clts_server *stats = CLONE2STATS(req->rq_xprt);
787 787 struct dupreq *dr;
788 788 uint32_t xid;
789 789 uint32_t drhash;
790 790 int status;
791 791
792 792 xid = REQTOXID(req);
793 793 mutex_enter(&dupreq_lock);
794 794 RSSTAT_INCR(stats, rsdupchecks);
795 795 /*
796 796 * Check to see whether an entry already exists in the cache.
797 797 */
798 798 dr = drhashtbl[XIDHASH(xid)];
799 799 while (dr != NULL) {
800 800 if (dr->dr_xid == xid &&
801 801 dr->dr_proc == req->rq_proc &&
802 802 dr->dr_prog == req->rq_prog &&
803 803 dr->dr_vers == req->rq_vers &&
804 804 dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
805 805 bcmp(dr->dr_addr.buf, req->rq_xprt->xp_rtaddr.buf,
806 806 dr->dr_addr.len) == 0) {
807 807 status = dr->dr_status;
808 808 if (status == DUP_DONE) {
809 809 bcopy(dr->dr_resp.buf, res, size);
810 810 if (dupcachedp != NULL)
811 811 *dupcachedp = (dr->dr_resfree != NULL);
812 812 } else {
813 813 dr->dr_status = DUP_INPROGRESS;
814 814 *drpp = dr;
815 815 }
816 816 RSSTAT_INCR(stats, rsdupreqs);
817 817 mutex_exit(&dupreq_lock);
818 818 return (status);
819 819 }
820 820 dr = dr->dr_chain;
821 821 }
822 822
823 823 /*
824 824 * There wasn't an entry, either allocate a new one or recycle
825 825 * an old one.
826 826 */
827 827 if (ndupreqs < maxdupreqs) {
828 828 dr = kmem_alloc(sizeof (*dr), KM_NOSLEEP);
829 829 if (dr == NULL) {
830 830 mutex_exit(&dupreq_lock);
831 831 return (DUP_ERROR);
832 832 }
833 833 dr->dr_resp.buf = NULL;
834 834 dr->dr_resp.maxlen = 0;
835 835 dr->dr_addr.buf = NULL;
836 836 dr->dr_addr.maxlen = 0;
837 837 if (drmru) {
838 838 dr->dr_next = drmru->dr_next;
839 839 drmru->dr_next = dr;
840 840 } else {
841 841 dr->dr_next = dr;
842 842 }
843 843 ndupreqs++;
844 844 } else {
845 845 dr = drmru->dr_next;
846 846 while (dr->dr_status == DUP_INPROGRESS) {
847 847 dr = dr->dr_next;
848 848 if (dr == drmru->dr_next) {
849 849 cmn_err(CE_WARN, "svc_clts_kdup no slots free");
850 850 mutex_exit(&dupreq_lock);
851 851 return (DUP_ERROR);
852 852 }
853 853 }
854 854 unhash(dr);
855 855 if (dr->dr_resfree) {
856 856 (*dr->dr_resfree)(dr->dr_resp.buf);
857 857 }
858 858 }
859 859 dr->dr_resfree = NULL;
860 860 drmru = dr;
861 861
862 862 dr->dr_xid = REQTOXID(req);
863 863 dr->dr_prog = req->rq_prog;
864 864 dr->dr_vers = req->rq_vers;
865 865 dr->dr_proc = req->rq_proc;
866 866 if (dr->dr_addr.maxlen < req->rq_xprt->xp_rtaddr.len) {
867 867 if (dr->dr_addr.buf != NULL)
868 868 kmem_free(dr->dr_addr.buf, dr->dr_addr.maxlen);
869 869 dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
870 870 dr->dr_addr.buf = kmem_alloc(dr->dr_addr.maxlen,
871 871 KM_NOSLEEP);
872 872 if (dr->dr_addr.buf == NULL) {
873 873 dr->dr_addr.maxlen = 0;
874 874 dr->dr_status = DUP_DROP;
875 875 mutex_exit(&dupreq_lock);
876 876 return (DUP_ERROR);
877 877 }
878 878 }
879 879 dr->dr_addr.len = req->rq_xprt->xp_rtaddr.len;
880 880 bcopy(req->rq_xprt->xp_rtaddr.buf, dr->dr_addr.buf, dr->dr_addr.len);
881 881 if (dr->dr_resp.maxlen < size) {
882 882 if (dr->dr_resp.buf != NULL)
883 883 kmem_free(dr->dr_resp.buf, dr->dr_resp.maxlen);
884 884 dr->dr_resp.maxlen = (unsigned int)size;
885 885 dr->dr_resp.buf = kmem_alloc(size, KM_NOSLEEP);
886 886 if (dr->dr_resp.buf == NULL) {
887 887 dr->dr_resp.maxlen = 0;
888 888 dr->dr_status = DUP_DROP;
889 889 mutex_exit(&dupreq_lock);
890 890 return (DUP_ERROR);
891 891 }
892 892 }
893 893 dr->dr_status = DUP_INPROGRESS;
894 894
895 895 drhash = (uint32_t)DRHASH(dr);
896 896 dr->dr_chain = drhashtbl[drhash];
897 897 drhashtbl[drhash] = dr;
898 898 drhashstat[drhash]++;
899 899 mutex_exit(&dupreq_lock);
900 900 *drpp = dr;
901 901 return (DUP_NEW);
902 902 }
903 903
904 904 /*
905 905 * PSARC 2003/523 Contract Private Interface
906 906 * svc_clts_kdupdone
907 907 * Changes must be reviewed by Solaris File Sharing
908 908 * Changes must be communicated to contract-2003-523@sun.com
909 909 *
910 910 * svc_clts_kdupdone marks the request done (DUP_DONE or DUP_DROP)
911 911 * and stores the response.
912 912 */
913 913 static void
914 914 svc_clts_kdupdone(struct dupreq *dr, caddr_t res, void (*dis_resfree)(),
915 915 int size, int status)
916 916 {
917 917
918 918 ASSERT(dr->dr_resfree == NULL);
919 919 if (status == DUP_DONE) {
920 920 bcopy(res, dr->dr_resp.buf, size);
921 921 dr->dr_resfree = dis_resfree;
922 922 }
923 923 dr->dr_status = status;
924 924 }
925 925
926 926 /*
927 927 * This routine expects that the mutex, dupreq_lock, is already held.
928 928 */
929 929 static void
930 930 unhash(struct dupreq *dr)
931 931 {
932 932 struct dupreq *drt;
933 933 struct dupreq *drtprev = NULL;
934 934 uint32_t drhash;
935 935
936 936 ASSERT(MUTEX_HELD(&dupreq_lock));
937 937
938 938 drhash = (uint32_t)DRHASH(dr);
939 939 drt = drhashtbl[drhash];
940 940 while (drt != NULL) {
941 941 if (drt == dr) {
942 942 drhashstat[drhash]--;
943 943 if (drtprev == NULL) {
944 944 drhashtbl[drhash] = drt->dr_chain;
945 945 } else {
946 946 drtprev->dr_chain = drt->dr_chain;
947 947 }
948 948 return;
949 949 }
950 950 drtprev = drt;
951 951 drt = drt->dr_chain;
952 952 }
953 953 }
954 954
955 955 void
956 956 svc_clts_stats_init(zoneid_t zoneid, struct rpc_clts_server **statsp)
957 957 {
958 958 kstat_t *ksp;
959 959 kstat_named_t *knp;
960 960
961 961 knp = rpcstat_zone_init_common(zoneid, "unix", "rpc_clts_server",
962 962 (const kstat_named_t *)&clts_rsstat_tmpl,
963 963 sizeof (clts_rsstat_tmpl));
964 964 /*
965 965 * Backwards compatibility for old kstat clients
966 966 */
967 967 ksp = kstat_create_zone("unix", 0, "rpc_server", "rpc",
968 968 KSTAT_TYPE_NAMED, clts_rsstat_ndata,
969 969 KSTAT_FLAG_VIRTUAL | KSTAT_FLAG_WRITABLE, zoneid);
970 970 if (ksp) {
971 971 ksp->ks_data = knp;
972 972 kstat_install(ksp);
973 973 }
974 974 *statsp = (struct rpc_clts_server *)knp;
975 975 }
976 976
977 977 void
978 978 svc_clts_stats_fini(zoneid_t zoneid, struct rpc_clts_server **statsp)
979 979 {
980 980 rpcstat_zone_fini_common(zoneid, "unix", "rpc_clts_server");
981 981 kstat_delete_byname_zone("unix", 0, "rpc_server", zoneid);
982 982 kmem_free(*statsp, sizeof (clts_rsstat_tmpl));
983 983 }
984 984
985 985 void
986 986 svc_clts_init()
987 987 {
988 988 /*
989 989 * Check to make sure that the clts private data will fit into
990 990 * the stack buffer allocated by svc_run. The compiler should
991 991 * remove this check, but it's a safety net if the udp_data
992 992 * structure ever changes.
993 993 */
994 994 /*CONSTANTCONDITION*/
995 995 ASSERT(sizeof (struct udp_data) <= SVC_P2LEN);
996 996
997 997 mutex_init(&dupreq_lock, NULL, MUTEX_DEFAULT, NULL);
998 998 }
↓ open down ↓ |
844 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX