Print this page
3910 t_look(3NSL) should never return T_ERROR
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libnsl/rpc/svc_dg.c
+++ new/usr/src/lib/libnsl/rpc/svc_dg.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 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
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 /*
23 + * Copyright 2014 Gary Mills
23 24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 25 * Use is subject to license terms.
25 26 */
26 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 28 /* All Rights Reserved */
28 29 /*
29 30 * Portions of this source code were derived from Berkeley
30 31 * 4.3 BSD under license from the Regents of the University of
31 32 * California.
32 33 */
33 34
34 35 /*
35 36 * svc_dg.c, Server side for connectionless RPC.
36 37 *
37 38 * Does some caching in the hopes of achieving execute-at-most-once semantics.
38 39 */
39 40
40 41 #include "mt.h"
41 42 #include "rpc_mt.h"
42 43 #include <stdio.h>
43 44 #include <sys/types.h>
44 45 #include <sys/sysmacros.h>
45 46 #include <rpc/rpc.h>
46 47 #include <rpcsvc/svc_dg_priv.h>
47 48 #include <errno.h>
48 49 #include <syslog.h>
49 50 #include <stdlib.h>
50 51 #include <string.h>
51 52 #include <ucred.h>
52 53 #include <unistd.h>
53 54 #include <sys/socket.h>
54 55 #include <netinet/in.h>
55 56 #include <arpa/inet.h>
56 57 #ifdef RPC_CACHE_DEBUG
57 58 #include <netconfig.h>
58 59 #include <netdir.h>
59 60 #endif
60 61
61 62 #ifndef MAX
62 63 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
63 64 #endif
64 65
65 66 static struct xp_ops *svc_dg_ops();
66 67 static void cache_set();
67 68 static int cache_get();
68 69
69 70 #define rpc_buffer(xprt) ((xprt)->xp_p1)
70 71
71 72 /*
72 73 * Usage:
73 74 * xprt = svc_dg_create(sock, sendsize, recvsize);
74 75 * Does other connectionless specific initializations.
75 76 * Once *xprt is initialized, it is registered.
76 77 * see (svc.h, xprt_register). If recvsize or sendsize are 0 suitable
77 78 * system defaults are chosen.
78 79 * The routines returns NULL if a problem occurred.
79 80 */
80 81 static const char svc_dg_str[] = "svc_dg_create: %s";
81 82 static const char svc_dg_err1[] = "could not get transport information";
82 83 static const char svc_dg_err2[] = " transport does not support data transfer";
83 84 static const char svc_dg_err3[] =
84 85 "fd > FD_SETSIZE; Use rpc_control(RPC_SVC_USE_POLLFD,...);";
85 86 static const char __no_mem_str[] = "out of memory";
86 87
87 88 /* Structure used to initialize SVC_XP_AUTH(xprt).svc_ah_ops. */
88 89 extern struct svc_auth_ops svc_auth_any_ops;
89 90 extern int __rpc_get_ltaddr(struct netbuf *, struct netbuf *);
90 91
91 92 void
92 93 svc_dg_xprtfree(SVCXPRT *xprt)
93 94 {
94 95 /* LINTED pointer alignment */
95 96 SVCXPRT_EXT *xt = xprt ? SVCEXT(xprt) : NULL;
96 97 /* LINTED pointer alignment */
97 98 struct svc_dg_data *su = xprt ? get_svc_dg_data(xprt) : NULL;
98 99
99 100 if (xprt == NULL)
100 101 return;
101 102 if (xprt->xp_netid)
102 103 free(xprt->xp_netid);
103 104 if (xprt->xp_tp)
104 105 free(xprt->xp_tp);
105 106 if (xt->parent == NULL)
106 107 if (xprt->xp_ltaddr.buf)
107 108 free(xprt->xp_ltaddr.buf);
108 109 if (xprt->xp_rtaddr.buf)
109 110 free(xprt->xp_rtaddr.buf);
110 111 if (su != NULL) {
111 112 XDR_DESTROY(&(su->su_xdrs));
112 113 free(su);
113 114 }
114 115 if (rpc_buffer(xprt))
115 116 free(rpc_buffer(xprt));
116 117 svc_xprt_free(xprt);
117 118 }
118 119
119 120 SVCXPRT *
120 121 svc_dg_create_private(int fd, uint_t sendsize, uint_t recvsize)
121 122 {
122 123 SVCXPRT *xprt;
123 124 struct svc_dg_data *su = NULL;
124 125 struct t_info tinfo;
125 126 size_t ucred_sz = ucred_size();
126 127
127 128 if (RPC_FD_NOTIN_FDSET(fd)) {
128 129 errno = EBADF;
129 130 t_errno = TBADF;
130 131 syslog(LOG_ERR, svc_dg_str, svc_dg_err3);
131 132 return (NULL);
132 133 }
133 134
134 135 if (t_getinfo(fd, &tinfo) == -1) {
135 136 syslog(LOG_ERR, svc_dg_str, svc_dg_err1);
136 137 return (NULL);
137 138 }
138 139 /*
139 140 * Find the receive and the send size
140 141 */
141 142 sendsize = __rpc_get_t_size((int)sendsize, tinfo.tsdu);
142 143 recvsize = __rpc_get_t_size((int)recvsize, tinfo.tsdu);
143 144 if ((sendsize == 0) || (recvsize == 0)) {
144 145 syslog(LOG_ERR, svc_dg_str, svc_dg_err2);
145 146 return (NULL);
146 147 }
147 148
148 149 if ((xprt = svc_xprt_alloc()) == NULL)
149 150 goto freedata;
150 151 /* LINTED pointer alignment */
151 152 svc_flags(xprt) |= SVC_DGRAM;
152 153
153 154 su = malloc(sizeof (*su) + ucred_sz);
154 155 if (su == NULL)
155 156 goto freedata;
156 157 su->su_iosz = ((MAX(sendsize, recvsize) + 3) / 4) * 4;
157 158 if ((rpc_buffer(xprt) = malloc(su->su_iosz)) == NULL)
158 159 goto freedata;
159 160 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
160 161 XDR_DECODE);
161 162 su->su_cache = NULL;
162 163 xprt->xp_fd = fd;
163 164 xprt->xp_p2 = (caddr_t)su;
164 165 xprt->xp_verf.oa_base = su->su_verfbody;
165 166 xprt->xp_ops = svc_dg_ops();
166 167
167 168 su->su_tudata.addr.maxlen = 0; /* Fill in later */
168 169
169 170 su->su_tudata.udata.buf = (char *)rpc_buffer(xprt);
170 171 su->su_tudata.opt.buf = (char *)su->opts;
171 172 su->su_tudata.udata.maxlen = su->su_iosz;
172 173 su->su_tudata.opt.maxlen = MAX_OPT_WORDS * sizeof (int) + ucred_sz;
173 174 /* LINTED pointer alignment */
174 175 SVC_XP_AUTH(xprt).svc_ah_ops = svc_auth_any_ops;
175 176 /* LINTED pointer alignment */
176 177 SVC_XP_AUTH(xprt).svc_ah_private = NULL;
177 178 return (xprt);
178 179 freedata:
179 180 (void) syslog(LOG_ERR, svc_dg_str, __no_mem_str);
180 181 if (xprt)
181 182 svc_dg_xprtfree(xprt);
182 183 return (NULL);
183 184 }
184 185
185 186 SVCXPRT *
186 187 svc_dg_create(const int fd, const uint_t sendsize, const uint_t recvsize)
187 188 {
188 189 SVCXPRT *xprt;
189 190
190 191 if ((xprt = svc_dg_create_private(fd, sendsize, recvsize)) != NULL)
191 192 xprt_register(xprt);
192 193 return (xprt);
193 194 }
194 195
195 196 SVCXPRT *
196 197 svc_dg_xprtcopy(SVCXPRT *parent)
197 198 {
198 199 SVCXPRT *xprt;
199 200 struct svc_dg_data *su;
200 201 size_t ucred_sz = ucred_size();
201 202
202 203 if ((xprt = svc_xprt_alloc()) == NULL)
203 204 return (NULL);
204 205
205 206 /* LINTED pointer alignment */
206 207 SVCEXT(xprt)->parent = parent;
207 208 /* LINTED pointer alignment */
208 209 SVCEXT(xprt)->flags = SVCEXT(parent)->flags;
209 210
210 211 xprt->xp_fd = parent->xp_fd;
211 212 xprt->xp_port = parent->xp_port;
212 213 xprt->xp_ops = svc_dg_ops();
213 214 if (parent->xp_tp) {
214 215 xprt->xp_tp = (char *)strdup(parent->xp_tp);
215 216 if (xprt->xp_tp == NULL) {
216 217 syslog(LOG_ERR, "svc_dg_xprtcopy: strdup failed");
217 218 svc_dg_xprtfree(xprt);
218 219 return (NULL);
219 220 }
220 221 }
221 222 if (parent->xp_netid) {
222 223 xprt->xp_netid = (char *)strdup(parent->xp_netid);
223 224 if (xprt->xp_netid == NULL) {
224 225 syslog(LOG_ERR, "svc_dg_xprtcopy: strdup failed");
225 226 if (parent->xp_tp)
226 227 free(parent->xp_tp);
227 228 svc_dg_xprtfree(xprt);
228 229 return (NULL);
229 230 }
230 231 }
231 232 xprt->xp_ltaddr = parent->xp_ltaddr; /* shared with parent */
232 233
233 234 xprt->xp_rtaddr = parent->xp_rtaddr;
234 235 xprt->xp_rtaddr.buf = malloc(xprt->xp_rtaddr.maxlen);
235 236 if (xprt->xp_rtaddr.buf == NULL) {
236 237 svc_dg_xprtfree(xprt);
237 238 return (NULL);
238 239 }
239 240 (void) memcpy(xprt->xp_rtaddr.buf, parent->xp_rtaddr.buf,
240 241 xprt->xp_rtaddr.maxlen);
241 242 xprt->xp_type = parent->xp_type;
242 243
243 244 if ((su = malloc(sizeof (struct svc_dg_data) + ucred_sz)) == NULL) {
244 245 svc_dg_xprtfree(xprt);
245 246 return (NULL);
246 247 }
247 248 /* LINTED pointer alignment */
248 249 su->su_iosz = get_svc_dg_data(parent)->su_iosz;
249 250 if ((rpc_buffer(xprt) = malloc(su->su_iosz)) == NULL) {
250 251 svc_dg_xprtfree(xprt);
251 252 free(su);
252 253 return (NULL);
253 254 }
254 255 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
255 256 XDR_DECODE);
256 257 su->su_cache = NULL;
257 258 su->su_tudata.addr.maxlen = 0; /* Fill in later */
258 259 su->su_tudata.udata.buf = (char *)rpc_buffer(xprt);
259 260 su->su_tudata.opt.buf = (char *)su->opts;
260 261 su->su_tudata.udata.maxlen = su->su_iosz;
261 262 su->su_tudata.opt.maxlen = MAX_OPT_WORDS * sizeof (int) + ucred_sz;
262 263 xprt->xp_p2 = (caddr_t)su; /* get_svc_dg_data(xprt) = su */
263 264 xprt->xp_verf.oa_base = su->su_verfbody;
264 265
265 266 return (xprt);
266 267 }
267 268
268 269 /*ARGSUSED*/
269 270 static enum xprt_stat
270 271 svc_dg_stat(SVCXPRT *xprt)
271 272 {
272 273 return (XPRT_IDLE);
273 274 }
274 275
275 276 /*
276 277 * Find the SCM_UCRED in src and place a pointer to that option alone in dest.
277 278 * Note that these two 'netbuf' structures might be the same one, so the code
278 279 * has to be careful about referring to src after changing dest.
279 280 */
280 281 static void
281 282 extract_cred(const struct netbuf *src, struct netbuf *dest)
282 283 {
283 284 char *cp = src->buf;
284 285 unsigned int len = src->len;
285 286 const struct T_opthdr *opt;
286 287 unsigned int olen;
287 288
288 289 while (len >= sizeof (*opt)) {
289 290 /* LINTED: pointer alignment */
290 291 opt = (const struct T_opthdr *)cp;
291 292 olen = opt->len;
292 293 if (olen > len || olen < sizeof (*opt) ||
293 294 !IS_P2ALIGNED(olen, sizeof (t_uscalar_t)))
294 295 break;
295 296 if (opt->level == SOL_SOCKET && opt->name == SCM_UCRED) {
296 297 dest->buf = cp;
297 298 dest->len = olen;
298 299 return;
299 300 }
300 301 cp += olen;
301 302 len -= olen;
302 303 }
303 304 dest->len = 0;
304 305 }
305 306
306 307 /*
307 308 * This routine extracts the destination IP address of the inbound RPC packet
308 309 * and sets that as source IP address for the outbound response.
309 310 */
310 311 static void
311 312 set_src_addr(SVCXPRT *xprt, struct netbuf *opt)
312 313 {
313 314 struct netbuf *nbufp, *ltaddr;
314 315 struct T_opthdr *opthdr;
315 316 in_pktinfo_t *pktinfo;
316 317 struct sockaddr_in *sock = (struct sockaddr_in *)NULL;
317 318
318 319 /* extract dest IP of inbound packet */
319 320 /* LINTED pointer alignment */
320 321 nbufp = (struct netbuf *)xprt->xp_p2;
321 322 ltaddr = &xprt->xp_ltaddr;
322 323 if (__rpc_get_ltaddr(nbufp, ltaddr) != 0)
323 324 return;
324 325
325 326 /* do nothing for non-IPv4 packet */
326 327 /* LINTED pointer alignment */
327 328 sock = (struct sockaddr_in *)ltaddr->buf;
328 329 if (sock->sin_family != AF_INET)
329 330 return;
330 331
331 332 /* set desired option header */
332 333 opthdr = (struct T_opthdr *)memalign(sizeof (int),
333 334 sizeof (struct T_opthdr) + sizeof (in_pktinfo_t));
334 335 if (opthdr == NULL)
335 336 return;
336 337 opthdr->len = sizeof (struct T_opthdr) + sizeof (in_pktinfo_t);
337 338 opthdr->level = IPPROTO_IP;
338 339 opthdr->name = IP_PKTINFO;
339 340
340 341 /*
341 342 * 1. set source IP of outbound packet
342 343 * 2. value '0' for index means IP layer uses this as source address
343 344 */
344 345 pktinfo = (in_pktinfo_t *)(opthdr + 1);
345 346 (void) memset(pktinfo, 0, sizeof (in_pktinfo_t));
346 347 pktinfo->ipi_spec_dst.s_addr = sock->sin_addr.s_addr;
347 348 pktinfo->ipi_ifindex = 0;
348 349
349 350 /* copy data into ancillary buffer */
350 351 if (opthdr->len + opt->len <= opt->maxlen) {
351 352 (void) memcpy((void *)(opt->buf+opt->len), (const void *)opthdr,
352 353 opthdr->len);
353 354 opt->len += opthdr->len;
354 355 }
355 356 free(opthdr);
356 357 }
357 358
358 359 static bool_t
359 360 svc_dg_recv(SVCXPRT *xprt, struct rpc_msg *msg)
360 361 {
361 362 /* LINTED pointer alignment */
362 363 struct svc_dg_data *su = get_svc_dg_data(xprt);
363 364 XDR *xdrs = &(su->su_xdrs);
364 365 struct t_unitdata *tu_data = &(su->su_tudata);
365 366 int moreflag;
366 367 struct netbuf *nbufp;
367 368 struct netconfig *nconf;
368 369
369 370 /* XXX: tudata should have been made a part of the server handle */
370 371 if (tu_data->addr.maxlen == 0)
371 372 tu_data->addr = xprt->xp_rtaddr;
372 373 again:
373 374 tu_data->addr.len = 0;
374 375 tu_data->opt.len = 0;
375 376 tu_data->udata.len = 0;
376 377
↓ open down ↓ |
344 lines elided |
↑ open up ↑ |
377 378 moreflag = 0;
378 379 if (t_rcvudata(xprt->xp_fd, tu_data, &moreflag) == -1) {
379 380 #ifdef RPC_DEBUG
380 381 syslog(LOG_ERR, "svc_dg_recv: t_rcvudata t_errno=%d errno=%d\n",
381 382 t_errno, errno);
382 383 #endif
383 384 if (t_errno == TLOOK) {
384 385 int lookres;
385 386
386 387 lookres = t_look(xprt->xp_fd);
387 - if ((lookres & T_UDERR) &&
388 + if ((lookres == T_UDERR) &&
388 389 (t_rcvuderr(xprt->xp_fd,
389 390 (struct t_uderr *)0) < 0)) {
390 391 /*EMPTY*/
391 392 #ifdef RPC_DEBUG
392 393 syslog(LOG_ERR,
393 394 "svc_dg_recv: t_rcvuderr t_errno = %d\n",
394 395 t_errno);
395 396 #endif
396 397 }
397 - if (lookres & T_DATA)
398 + if (lookres == T_DATA)
398 399 goto again;
399 400 } else if ((errno == EINTR) && (t_errno == TSYSERR))
400 401 goto again;
401 402 else {
402 403 return (FALSE);
403 404 }
404 405 }
405 406
406 407 if ((moreflag) ||
407 408 (tu_data->udata.len < 4 * (uint_t)sizeof (uint32_t))) {
408 409 /*
409 410 * If moreflag is set, drop that data packet. Something wrong
410 411 */
411 412 return (FALSE);
412 413 }
413 414 su->optbuf = tu_data->opt;
414 415 xprt->xp_rtaddr.len = tu_data->addr.len;
415 416 xdrs->x_op = XDR_DECODE;
416 417 XDR_SETPOS(xdrs, 0);
417 418 if (!xdr_callmsg(xdrs, msg))
418 419 return (FALSE);
419 420 su->su_xid = msg->rm_xid;
420 421 if (su->su_cache != NULL) {
421 422 char *reply;
422 423 uint32_t replylen;
423 424
424 425 if (cache_get(xprt, msg, &reply, &replylen)) {
425 426 /* tu_data.addr is already set */
426 427 tu_data->udata.buf = reply;
427 428 tu_data->udata.len = (uint_t)replylen;
428 429 extract_cred(&tu_data->opt, &tu_data->opt);
429 430 set_src_addr(xprt, &tu_data->opt);
430 431 (void) t_sndudata(xprt->xp_fd, tu_data);
431 432 tu_data->udata.buf = (char *)rpc_buffer(xprt);
432 433 tu_data->opt.buf = (char *)su->opts;
433 434 return (FALSE);
434 435 }
435 436 }
436 437
437 438 /*
438 439 * get local ip address
439 440 */
440 441
441 442 if ((nconf = getnetconfigent(xprt->xp_netid)) != NULL) {
442 443 if (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
443 444 strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
444 445 if (nconf->nc_semantics == NC_TPI_CLTS) {
445 446 /* LINTED pointer cast */
446 447 nbufp = (struct netbuf *)(xprt->xp_p2);
447 448 if (__rpc_get_ltaddr(nbufp,
448 449 &xprt->xp_ltaddr) < 0) {
449 450 if (strcmp(nconf->nc_protofmly,
450 451 NC_INET) == 0) {
451 452 syslog(LOG_ERR,
452 453 "svc_dg_recv: ip(udp), "
453 454 "t_errno=%d, errno=%d",
454 455 t_errno, errno);
455 456 }
456 457 if (strcmp(nconf->nc_protofmly,
457 458 NC_INET6) == 0) {
458 459 syslog(LOG_ERR,
459 460 "svc_dg_recv: ip (udp6), "
460 461 "t_errno=%d, errno=%d",
461 462 t_errno, errno);
462 463 }
463 464 freenetconfigent(nconf);
464 465 return (FALSE);
465 466 }
466 467 }
467 468 }
468 469 freenetconfigent(nconf);
469 470 }
470 471 return (TRUE);
471 472 }
472 473
473 474 static bool_t
474 475 svc_dg_reply(SVCXPRT *xprt, struct rpc_msg *msg)
475 476 {
476 477 /* LINTED pointer alignment */
477 478 struct svc_dg_data *su = get_svc_dg_data(xprt);
478 479 XDR *xdrs = &(su->su_xdrs);
479 480 bool_t stat = FALSE;
480 481 xdrproc_t xdr_results;
481 482 caddr_t xdr_location;
482 483 bool_t has_args;
483 484
484 485 if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
485 486 msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
486 487 has_args = TRUE;
487 488 xdr_results = msg->acpted_rply.ar_results.proc;
488 489 xdr_location = msg->acpted_rply.ar_results.where;
489 490 msg->acpted_rply.ar_results.proc = xdr_void;
490 491 msg->acpted_rply.ar_results.where = NULL;
491 492 } else
492 493 has_args = FALSE;
493 494
494 495 xdrs->x_op = XDR_ENCODE;
495 496 XDR_SETPOS(xdrs, 0);
496 497 msg->rm_xid = su->su_xid;
497 498 if (xdr_replymsg(xdrs, msg) && (!has_args ||
498 499 /* LINTED pointer alignment */
499 500 SVCAUTH_WRAP(&SVC_XP_AUTH(xprt), xdrs, xdr_results,
500 501 xdr_location))) {
501 502 int slen;
502 503 struct t_unitdata *tu_data = &(su->su_tudata);
503 504
504 505 slen = (int)XDR_GETPOS(xdrs);
505 506 tu_data->udata.len = slen;
506 507 extract_cred(&su->optbuf, &tu_data->opt);
507 508 set_src_addr(xprt, &tu_data->opt);
508 509 try_again:
509 510 if (t_sndudata(xprt->xp_fd, tu_data) == 0) {
510 511 stat = TRUE;
511 512 if (su->su_cache && slen >= 0) {
512 513 cache_set(xprt, (uint32_t)slen);
513 514 }
514 515 } else {
515 516 if (errno == EINTR)
516 517 goto try_again;
517 518
518 519 syslog(LOG_ERR,
519 520 "svc_dg_reply: t_sndudata error t_errno=%d ",
520 521 "errno=%d\n", t_errno, errno);
521 522 }
522 523 tu_data->opt.buf = (char *)su->opts;
523 524 }
524 525 return (stat);
525 526 }
526 527
527 528 static bool_t
528 529 svc_dg_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
529 530 {
530 531 if (svc_mt_mode != RPC_SVC_MT_NONE)
531 532 svc_args_done(xprt);
532 533 /* LINTED pointer alignment */
533 534 return (SVCAUTH_UNWRAP(&SVC_XP_AUTH(xprt),
534 535 &(get_svc_dg_data(xprt)->su_xdrs), xdr_args, args_ptr));
535 536 }
536 537
537 538 static bool_t
538 539 svc_dg_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
539 540 {
540 541 /* LINTED pointer alignment */
541 542 XDR *xdrs = &(get_svc_dg_data(xprt)->su_xdrs);
542 543
543 544 xdrs->x_op = XDR_FREE;
544 545 return ((*xdr_args)(xdrs, args_ptr));
545 546 }
546 547
547 548 static void
548 549 svc_dg_destroy(SVCXPRT *xprt)
549 550 {
550 551 (void) mutex_lock(&svc_mutex);
551 552 _svc_dg_destroy_private(xprt);
552 553 (void) mutex_unlock(&svc_mutex);
553 554 }
554 555
555 556 void
556 557 _svc_dg_destroy_private(SVCXPRT *xprt)
557 558 {
558 559 if (svc_mt_mode != RPC_SVC_MT_NONE) {
559 560 /* LINTED pointer alignment */
560 561 if (SVCEXT(xprt)->parent)
561 562 /* LINTED pointer alignment */
562 563 xprt = SVCEXT(xprt)->parent;
563 564 /* LINTED pointer alignment */
564 565 svc_flags(xprt) |= SVC_DEFUNCT;
565 566 /* LINTED pointer alignment */
566 567 if (SVCEXT(xprt)->refcnt > 0)
567 568 return;
568 569 }
569 570
570 571 xprt_unregister(xprt);
571 572 (void) t_close(xprt->xp_fd);
572 573
573 574 if (svc_mt_mode != RPC_SVC_MT_NONE)
574 575 svc_xprt_destroy(xprt);
575 576 else
576 577 svc_dg_xprtfree(xprt);
577 578 }
578 579
579 580 /*ARGSUSED*/
580 581 static bool_t
581 582 svc_dg_control(SVCXPRT *xprt, const uint_t rq, void *in)
582 583 {
583 584 switch (rq) {
584 585 case SVCGET_XID:
585 586 if (xprt->xp_p2 == NULL)
586 587 return (FALSE);
587 588 /* LINTED pointer alignment */
588 589 *(uint32_t *)in = ((struct svc_dg_data *)(xprt->xp_p2))->su_xid;
589 590 return (TRUE);
590 591 default:
591 592 return (FALSE);
592 593 }
593 594 }
594 595
595 596 static struct xp_ops *
596 597 svc_dg_ops(void)
597 598 {
598 599 static struct xp_ops ops;
599 600 extern mutex_t ops_lock;
600 601
601 602 /* VARIABLES PROTECTED BY ops_lock: ops */
602 603
603 604 (void) mutex_lock(&ops_lock);
604 605 if (ops.xp_recv == NULL) {
605 606 ops.xp_recv = svc_dg_recv;
606 607 ops.xp_stat = svc_dg_stat;
607 608 ops.xp_getargs = svc_dg_getargs;
608 609 ops.xp_reply = svc_dg_reply;
609 610 ops.xp_freeargs = svc_dg_freeargs;
610 611 ops.xp_destroy = svc_dg_destroy;
611 612 ops.xp_control = svc_dg_control;
612 613 }
613 614 (void) mutex_unlock(&ops_lock);
614 615 return (&ops);
615 616 }
616 617
617 618 /* The CACHING COMPONENT */
618 619
619 620 /*
620 621 * Could have been a separate file, but some part of it depends upon the
621 622 * private structure of the client handle.
622 623 *
623 624 * Fifo cache for cl server
624 625 * Copies pointers to reply buffers into fifo cache
625 626 * Buffers are sent again if retransmissions are detected.
626 627 */
627 628
628 629 #define SPARSENESS 4 /* 75% sparse */
629 630
630 631 /*
631 632 * An entry in the cache
632 633 */
633 634 typedef struct cache_node *cache_ptr;
634 635 struct cache_node {
635 636 /*
636 637 * Index into cache is xid, proc, vers, prog and address
637 638 */
638 639 uint32_t cache_xid;
639 640 rpcproc_t cache_proc;
640 641 rpcvers_t cache_vers;
641 642 rpcprog_t cache_prog;
642 643 struct netbuf cache_addr;
643 644 /*
644 645 * The cached reply and length
645 646 */
646 647 char *cache_reply;
647 648 uint32_t cache_replylen;
648 649 /*
649 650 * Next node on the list, if there is a collision
650 651 */
651 652 cache_ptr cache_next;
652 653 };
653 654
654 655 /*
655 656 * The entire cache
656 657 */
657 658 struct cl_cache {
658 659 uint32_t uc_size; /* size of cache */
659 660 cache_ptr *uc_entries; /* hash table of entries in cache */
660 661 cache_ptr *uc_fifo; /* fifo list of entries in cache */
661 662 uint32_t uc_nextvictim; /* points to next victim in fifo list */
662 663 rpcprog_t uc_prog; /* saved program number */
663 664 rpcvers_t uc_vers; /* saved version number */
664 665 rpcproc_t uc_proc; /* saved procedure number */
665 666 };
666 667
667 668
668 669 /*
669 670 * the hashing function
670 671 */
671 672 #define CACHE_LOC(transp, xid) \
672 673 (xid % (SPARSENESS * ((struct cl_cache *) \
673 674 get_svc_dg_data(transp)->su_cache)->uc_size))
674 675
675 676 extern mutex_t dupreq_lock;
676 677
677 678 /*
678 679 * Enable use of the cache. Returns 1 on success, 0 on failure.
679 680 * Note: there is no disable.
680 681 */
681 682 static const char cache_enable_str[] = "svc_enablecache: %s %s";
682 683 static const char alloc_err[] = "could not allocate cache ";
683 684 static const char enable_err[] = "cache already enabled";
684 685
685 686 int
686 687 svc_dg_enablecache(SVCXPRT *xprt, const uint_t size)
687 688 {
688 689 SVCXPRT *transp;
689 690 struct svc_dg_data *su;
690 691 struct cl_cache *uc;
691 692
692 693 /* LINTED pointer alignment */
693 694 if (svc_mt_mode != RPC_SVC_MT_NONE && SVCEXT(xprt)->parent != NULL)
694 695 /* LINTED pointer alignment */
695 696 transp = SVCEXT(xprt)->parent;
696 697 else
697 698 transp = xprt;
698 699 /* LINTED pointer alignment */
699 700 su = get_svc_dg_data(transp);
700 701
701 702 (void) mutex_lock(&dupreq_lock);
702 703 if (su->su_cache != NULL) {
703 704 (void) syslog(LOG_ERR, cache_enable_str,
704 705 enable_err, " ");
705 706 (void) mutex_unlock(&dupreq_lock);
706 707 return (0);
707 708 }
708 709 uc = malloc(sizeof (struct cl_cache));
709 710 if (uc == NULL) {
710 711 (void) syslog(LOG_ERR, cache_enable_str,
711 712 alloc_err, " ");
712 713 (void) mutex_unlock(&dupreq_lock);
713 714 return (0);
714 715 }
715 716 uc->uc_size = size;
716 717 uc->uc_nextvictim = 0;
717 718 uc->uc_entries = calloc(size * SPARSENESS, sizeof (cache_ptr));
718 719 if (uc->uc_entries == NULL) {
719 720 (void) syslog(LOG_ERR, cache_enable_str, alloc_err, "data");
720 721 free(uc);
721 722 (void) mutex_unlock(&dupreq_lock);
722 723 return (0);
723 724 }
724 725 uc->uc_fifo = calloc(size, sizeof (cache_ptr));
725 726 if (uc->uc_fifo == NULL) {
726 727 (void) syslog(LOG_ERR, cache_enable_str, alloc_err, "fifo");
727 728 free(uc->uc_entries);
728 729 free(uc);
729 730 (void) mutex_unlock(&dupreq_lock);
730 731 return (0);
731 732 }
732 733 su->su_cache = (char *)uc;
733 734 (void) mutex_unlock(&dupreq_lock);
734 735 return (1);
735 736 }
736 737
737 738 /*
738 739 * Set an entry in the cache. It assumes that the uc entry is set from
739 740 * the earlier call to cache_get() for the same procedure. This will always
740 741 * happen because cache_get() is calle by svc_dg_recv and cache_set() is called
741 742 * by svc_dg_reply(). All this hoopla because the right RPC parameters are
742 743 * not available at svc_dg_reply time.
743 744 */
744 745
745 746 static const char cache_set_str[] = "cache_set: %s";
746 747 static const char cache_set_err1[] = "victim not found";
747 748 static const char cache_set_err2[] = "victim alloc failed";
748 749 static const char cache_set_err3[] = "could not allocate new rpc buffer";
749 750
750 751 static void
751 752 cache_set(SVCXPRT *xprt, uint32_t replylen)
752 753 {
753 754 SVCXPRT *parent;
754 755 cache_ptr victim;
755 756 cache_ptr *vicp;
756 757 struct svc_dg_data *su;
757 758 struct cl_cache *uc;
758 759 uint_t loc;
759 760 char *newbuf, *newbuf2;
760 761 int my_mallocs = 0;
761 762 #ifdef RPC_CACHE_DEBUG
762 763 struct netconfig *nconf;
763 764 char *uaddr;
764 765 #endif
765 766
766 767 /* LINTED pointer alignment */
767 768 if (svc_mt_mode != RPC_SVC_MT_NONE && SVCEXT(xprt)->parent != NULL)
768 769 /* LINTED pointer alignment */
769 770 parent = SVCEXT(xprt)->parent;
770 771 else
771 772 parent = xprt;
772 773 /* LINTED pointer alignment */
773 774 su = get_svc_dg_data(xprt);
774 775 /* LINTED pointer alignment */
775 776 uc = (struct cl_cache *)get_svc_dg_data(parent)->su_cache;
776 777
777 778 (void) mutex_lock(&dupreq_lock);
778 779 /*
779 780 * Find space for the new entry, either by
780 781 * reusing an old entry, or by mallocing a new one
781 782 */
782 783 victim = uc->uc_fifo[uc->uc_nextvictim];
783 784 if (victim != NULL) {
784 785 /* LINTED pointer alignment */
785 786 loc = CACHE_LOC(parent, victim->cache_xid);
786 787 for (vicp = &uc->uc_entries[loc];
787 788 *vicp != NULL && *vicp != victim;
788 789 vicp = &(*vicp)->cache_next)
789 790 ;
790 791 if (*vicp == NULL) {
791 792 (void) syslog(LOG_ERR, cache_set_str, cache_set_err1);
792 793 (void) mutex_unlock(&dupreq_lock);
793 794 return;
794 795 }
795 796 *vicp = victim->cache_next; /* remove from cache */
796 797 newbuf = victim->cache_reply;
797 798 } else {
798 799 victim = malloc(sizeof (struct cache_node));
799 800 if (victim == NULL) {
800 801 (void) syslog(LOG_ERR, cache_set_str, cache_set_err2);
801 802 (void) mutex_unlock(&dupreq_lock);
802 803 return;
803 804 }
804 805 newbuf = malloc(su->su_iosz);
805 806 if (newbuf == NULL) {
806 807 (void) syslog(LOG_ERR, cache_set_str, cache_set_err3);
807 808 free(victim);
808 809 (void) mutex_unlock(&dupreq_lock);
809 810 return;
810 811 }
811 812 my_mallocs = 1;
812 813 }
813 814
814 815 /*
815 816 * Store it away
816 817 */
817 818 #ifdef RPC_CACHE_DEBUG
818 819 if (nconf = getnetconfigent(xprt->xp_netid)) {
819 820 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
820 821 freenetconfigent(nconf);
821 822 printf(
822 823 "cache set for xid= %x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
823 824 su->su_xid, uc->uc_prog, uc->uc_vers, uc->uc_proc, uaddr);
824 825 free(uaddr);
825 826 }
826 827 #endif
827 828 newbuf2 = malloc(sizeof (char) * xprt->xp_rtaddr.len);
828 829 if (newbuf2 == NULL) {
829 830 syslog(LOG_ERR, "cache_set : out of memory");
830 831 if (my_mallocs) {
831 832 free(victim);
832 833 free(newbuf);
833 834 }
834 835 (void) mutex_unlock(&dupreq_lock);
835 836 return;
836 837 }
837 838 victim->cache_replylen = replylen;
838 839 victim->cache_reply = rpc_buffer(xprt);
839 840 rpc_buffer(xprt) = newbuf;
840 841 xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz,
841 842 XDR_ENCODE);
842 843 su->su_tudata.udata.buf = (char *)rpc_buffer(xprt);
843 844 victim->cache_xid = su->su_xid;
844 845 victim->cache_proc = uc->uc_proc;
845 846 victim->cache_vers = uc->uc_vers;
846 847 victim->cache_prog = uc->uc_prog;
847 848 victim->cache_addr = xprt->xp_rtaddr;
848 849 victim->cache_addr.buf = newbuf2;
849 850 (void) memcpy(victim->cache_addr.buf, xprt->xp_rtaddr.buf,
850 851 (int)xprt->xp_rtaddr.len);
851 852 /* LINTED pointer alignment */
852 853 loc = CACHE_LOC(parent, victim->cache_xid);
853 854 victim->cache_next = uc->uc_entries[loc];
854 855 uc->uc_entries[loc] = victim;
855 856 uc->uc_fifo[uc->uc_nextvictim++] = victim;
856 857 uc->uc_nextvictim %= uc->uc_size;
857 858 (void) mutex_unlock(&dupreq_lock);
858 859 }
859 860
860 861 /*
861 862 * Try to get an entry from the cache
862 863 * return 1 if found, 0 if not found and set the stage for cache_set()
863 864 */
864 865 static int
865 866 cache_get(SVCXPRT *xprt, struct rpc_msg *msg, char **replyp,
866 867 uint32_t *replylenp)
867 868 {
868 869 SVCXPRT *parent;
869 870 uint_t loc;
870 871 cache_ptr ent;
871 872 struct svc_dg_data *su;
872 873 struct cl_cache *uc;
873 874 #ifdef RPC_CACHE_DEBUG
874 875 struct netconfig *nconf;
875 876 char *uaddr;
876 877 #endif
877 878
878 879 /* LINTED pointer alignment */
879 880 if (svc_mt_mode != RPC_SVC_MT_NONE && SVCEXT(xprt)->parent != NULL)
880 881 /* LINTED pointer alignment */
881 882 parent = SVCEXT(xprt)->parent;
882 883 else
883 884 parent = xprt;
884 885 /* LINTED pointer alignment */
885 886 su = get_svc_dg_data(xprt);
886 887 /* LINTED pointer alignment */
887 888 uc = (struct cl_cache *)get_svc_dg_data(parent)->su_cache;
888 889
889 890 (void) mutex_lock(&dupreq_lock);
890 891 /* LINTED pointer alignment */
891 892 loc = CACHE_LOC(parent, su->su_xid);
892 893 for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) {
893 894 if (ent->cache_xid == su->su_xid &&
894 895 ent->cache_proc == msg->rm_call.cb_proc &&
895 896 ent->cache_vers == msg->rm_call.cb_vers &&
896 897 ent->cache_prog == msg->rm_call.cb_prog &&
897 898 ent->cache_addr.len == xprt->xp_rtaddr.len &&
898 899 (memcmp(ent->cache_addr.buf, xprt->xp_rtaddr.buf,
899 900 xprt->xp_rtaddr.len) == 0)) {
900 901 #ifdef RPC_CACHE_DEBUG
901 902 if (nconf = getnetconfigent(xprt->xp_netid)) {
902 903 uaddr = taddr2uaddr(nconf, &xprt->xp_rtaddr);
903 904 freenetconfigent(nconf);
904 905 printf(
905 906 "cache entry found for xid=%x prog=%d vers=%d proc=%d for rmtaddr=%s\n",
906 907 su->su_xid, msg->rm_call.cb_prog,
907 908 msg->rm_call.cb_vers,
908 909 msg->rm_call.cb_proc, uaddr);
909 910 free(uaddr);
910 911 }
911 912 #endif
912 913 *replyp = ent->cache_reply;
913 914 *replylenp = ent->cache_replylen;
914 915 (void) mutex_unlock(&dupreq_lock);
915 916 return (1);
916 917 }
917 918 }
918 919 /*
919 920 * Failed to find entry
920 921 * Remember a few things so we can do a set later
921 922 */
922 923 uc->uc_proc = msg->rm_call.cb_proc;
923 924 uc->uc_vers = msg->rm_call.cb_vers;
924 925 uc->uc_prog = msg->rm_call.cb_prog;
925 926 (void) mutex_unlock(&dupreq_lock);
926 927 return (0);
927 928 }
↓ open down ↓ |
520 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX