FreeBSD/Linux Kernel Cross Reference
sys/rpc/clnt_rc.c
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/mutex.h>
41 #include <sys/pcpu.h>
42 #include <sys/proc.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/time.h>
46 #include <sys/uio.h>
47
48 #include <rpc/rpc.h>
49 #include <rpc/rpc_com.h>
50 #include <rpc/krpc.h>
51 #include <rpc/rpcsec_tls.h>
52
53 static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
54 rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
55 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
56 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
57 static void clnt_reconnect_abort(CLIENT *);
58 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
59 static void clnt_reconnect_close(CLIENT *);
60 static void clnt_reconnect_destroy(CLIENT *);
61
62 static struct clnt_ops clnt_reconnect_ops = {
63 .cl_call = clnt_reconnect_call,
64 .cl_abort = clnt_reconnect_abort,
65 .cl_geterr = clnt_reconnect_geterr,
66 .cl_freeres = clnt_reconnect_freeres,
67 .cl_close = clnt_reconnect_close,
68 .cl_destroy = clnt_reconnect_destroy,
69 .cl_control = clnt_reconnect_control
70 };
71
72 static int fake_wchan;
73
74 CLIENT *
75 clnt_reconnect_create(
76 struct netconfig *nconf, /* network type */
77 struct sockaddr *svcaddr, /* servers address */
78 rpcprog_t program, /* program number */
79 rpcvers_t version, /* version number */
80 size_t sendsz, /* buffer recv size */
81 size_t recvsz) /* buffer send size */
82 {
83 CLIENT *cl = NULL; /* client handle */
84 struct rc_data *rc = NULL; /* private data */
85
86 if (svcaddr == NULL) {
87 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
88 return (NULL);
89 }
90
91 cl = mem_alloc(sizeof (CLIENT));
92 rc = mem_alloc(sizeof (*rc));
93 mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
94 (void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
95 rc->rc_nconf = nconf;
96 rc->rc_prog = program;
97 rc->rc_vers = version;
98 rc->rc_sendsz = sendsz;
99 rc->rc_recvsz = recvsz;
100 rc->rc_timeout.tv_sec = -1;
101 rc->rc_timeout.tv_usec = -1;
102 rc->rc_retry.tv_sec = 3;
103 rc->rc_retry.tv_usec = 0;
104 rc->rc_retries = INT_MAX;
105 rc->rc_privport = FALSE;
106 rc->rc_waitchan = "rpcrecv";
107 rc->rc_intr = 0;
108 rc->rc_connecting = FALSE;
109 rc->rc_closed = FALSE;
110 rc->rc_ucred = crdup(curthread->td_ucred);
111 rc->rc_client = NULL;
112 rc->rc_tls = false;
113 rc->rc_tlscertname = NULL;
114
115 cl->cl_refs = 1;
116 cl->cl_ops = &clnt_reconnect_ops;
117 cl->cl_private = (caddr_t)(void *)rc;
118 cl->cl_auth = authnone_create();
119 cl->cl_tp = NULL;
120 cl->cl_netid = NULL;
121 return (cl);
122 }
123
124 static enum clnt_stat
125 clnt_reconnect_connect(CLIENT *cl)
126 {
127 struct thread *td = curthread;
128 struct rc_data *rc = (struct rc_data *)cl->cl_private;
129 struct socket *so;
130 enum clnt_stat stat;
131 int error;
132 int one = 1;
133 struct ucred *oldcred;
134 CLIENT *newclient = NULL;
135 uint64_t ssl[3];
136 uint32_t reterr;
137
138 mtx_lock(&rc->rc_lock);
139 while (rc->rc_connecting) {
140 error = msleep(rc, &rc->rc_lock,
141 rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
142 if (error) {
143 mtx_unlock(&rc->rc_lock);
144 return (RPC_INTR);
145 }
146 }
147 if (rc->rc_closed) {
148 mtx_unlock(&rc->rc_lock);
149 return (RPC_CANTSEND);
150 }
151 if (rc->rc_client) {
152 mtx_unlock(&rc->rc_lock);
153 return (RPC_SUCCESS);
154 }
155
156 /*
157 * My turn to attempt a connect. The rc_connecting variable
158 * serializes the following code sequence, so it is guaranteed
159 * that rc_client will still be NULL after it is re-locked below,
160 * since that is the only place it is set non-NULL.
161 */
162 rc->rc_connecting = TRUE;
163 mtx_unlock(&rc->rc_lock);
164
165 oldcred = td->td_ucred;
166 td->td_ucred = rc->rc_ucred;
167 so = __rpc_nconf2socket(rc->rc_nconf);
168 if (!so) {
169 stat = rpc_createerr.cf_stat = RPC_TLIERROR;
170 rpc_createerr.cf_error.re_errno = 0;
171 td->td_ucred = oldcred;
172 goto out;
173 }
174
175 if (rc->rc_privport)
176 bindresvport(so, NULL);
177
178 if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
179 newclient = clnt_dg_create(so,
180 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
181 rc->rc_sendsz, rc->rc_recvsz);
182 else {
183 /*
184 * I do not believe a timeout of less than 1sec would make
185 * sense here since short delays can occur when a server is
186 * temporarily overloaded.
187 */
188 if (rc->rc_timeout.tv_sec > 0 && rc->rc_timeout.tv_usec >= 0) {
189 error = so_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO,
190 &rc->rc_timeout, sizeof(struct timeval));
191 if (error != 0) {
192 stat = rpc_createerr.cf_stat = RPC_CANTSEND;
193 rpc_createerr.cf_error.re_errno = error;
194 td->td_ucred = oldcred;
195 goto out;
196 }
197 }
198 newclient = clnt_vc_create(so,
199 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
200 rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr);
201 if (rc->rc_tls && newclient != NULL) {
202 stat = rpctls_connect(newclient, rc->rc_tlscertname, so,
203 ssl, &reterr);
204 if (stat != RPC_SUCCESS || reterr != RPCTLSERR_OK) {
205 if (stat == RPC_SUCCESS)
206 stat = RPC_FAILED;
207 stat = rpc_createerr.cf_stat = stat;
208 rpc_createerr.cf_error.re_errno = 0;
209 CLNT_CLOSE(newclient);
210 CLNT_RELEASE(newclient);
211 newclient = NULL;
212 td->td_ucred = oldcred;
213 goto out;
214 }
215 }
216 }
217 td->td_ucred = oldcred;
218
219 if (!newclient) {
220 soclose(so);
221 rc->rc_err = rpc_createerr.cf_error;
222 stat = rpc_createerr.cf_stat;
223 goto out;
224 }
225
226 CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
227 CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
228 CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
229 CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
230 CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
231 CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
232 if (rc->rc_tls)
233 CLNT_CONTROL(newclient, CLSET_TLS, ssl);
234 if (rc->rc_backchannel != NULL)
235 CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel);
236 stat = RPC_SUCCESS;
237
238 out:
239 mtx_lock(&rc->rc_lock);
240 KASSERT(rc->rc_client == NULL, ("rc_client not null"));
241 if (!rc->rc_closed) {
242 rc->rc_client = newclient;
243 newclient = NULL;
244 }
245 rc->rc_connecting = FALSE;
246 wakeup(rc);
247 mtx_unlock(&rc->rc_lock);
248
249 if (newclient) {
250 /*
251 * It has been closed, so discard the new client.
252 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
253 * be called with the rc_lock mutex held, since they may
254 * msleep() while holding a different mutex.
255 */
256 CLNT_CLOSE(newclient);
257 CLNT_RELEASE(newclient);
258 }
259
260 return (stat);
261 }
262
263 static enum clnt_stat
264 clnt_reconnect_call(
265 CLIENT *cl, /* client handle */
266 struct rpc_callextra *ext, /* call metadata */
267 rpcproc_t proc, /* procedure number */
268 struct mbuf *args, /* pointer to args */
269 struct mbuf **resultsp, /* pointer to results */
270 struct timeval utimeout)
271 {
272 struct rc_data *rc = (struct rc_data *)cl->cl_private;
273 CLIENT *client;
274 enum clnt_stat stat;
275 int tries, error;
276
277 tries = 0;
278 do {
279 mtx_lock(&rc->rc_lock);
280 if (rc->rc_closed) {
281 mtx_unlock(&rc->rc_lock);
282 return (RPC_CANTSEND);
283 }
284
285 if (!rc->rc_client) {
286 mtx_unlock(&rc->rc_lock);
287 stat = clnt_reconnect_connect(cl);
288 if (stat == RPC_SYSTEMERROR) {
289 error = tsleep(&fake_wchan,
290 rc->rc_intr ? PCATCH : 0, "rpccon", hz);
291 if (error == EINTR || error == ERESTART)
292 return (RPC_INTR);
293 tries++;
294 if (tries >= rc->rc_retries)
295 return (stat);
296 continue;
297 }
298 if (stat != RPC_SUCCESS)
299 return (stat);
300 mtx_lock(&rc->rc_lock);
301 }
302
303 if (!rc->rc_client) {
304 mtx_unlock(&rc->rc_lock);
305 stat = RPC_FAILED;
306 continue;
307 }
308 CLNT_ACQUIRE(rc->rc_client);
309 client = rc->rc_client;
310 mtx_unlock(&rc->rc_lock);
311 stat = CLNT_CALL_MBUF(client, ext, proc, args,
312 resultsp, utimeout);
313
314 if (stat != RPC_SUCCESS) {
315 if (!ext)
316 CLNT_GETERR(client, &rc->rc_err);
317 }
318
319 if (stat == RPC_TIMEDOUT) {
320 /*
321 * Check for async send misfeature for NLM
322 * protocol.
323 */
324 if ((rc->rc_timeout.tv_sec == 0
325 && rc->rc_timeout.tv_usec == 0)
326 || (rc->rc_timeout.tv_sec == -1
327 && utimeout.tv_sec == 0
328 && utimeout.tv_usec == 0)) {
329 CLNT_RELEASE(client);
330 break;
331 }
332 }
333
334 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
335 || stat == RPC_CANTRECV) {
336 tries++;
337 if (tries >= rc->rc_retries) {
338 CLNT_RELEASE(client);
339 break;
340 }
341
342 if (ext && ext->rc_feedback)
343 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
344 ext->rc_feedback_arg);
345
346 mtx_lock(&rc->rc_lock);
347 /*
348 * Make sure that someone else hasn't already
349 * reconnected by checking if rc_client has changed.
350 * If not, we are done with the client and must
351 * do CLNT_RELEASE(client) twice to dispose of it,
352 * because there is both an initial refcnt and one
353 * acquired by CLNT_ACQUIRE() above.
354 */
355 if (rc->rc_client == client) {
356 rc->rc_client = NULL;
357 mtx_unlock(&rc->rc_lock);
358 CLNT_RELEASE(client);
359 } else {
360 mtx_unlock(&rc->rc_lock);
361 }
362 CLNT_RELEASE(client);
363 } else {
364 CLNT_RELEASE(client);
365 break;
366 }
367 } while (stat != RPC_SUCCESS);
368
369 KASSERT(stat != RPC_SUCCESS || *resultsp,
370 ("RPC_SUCCESS without reply"));
371
372 return (stat);
373 }
374
375 static void
376 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
377 {
378 struct rc_data *rc = (struct rc_data *)cl->cl_private;
379
380 *errp = rc->rc_err;
381 }
382
383 /*
384 * Since this function requires that rc_client be valid, it can
385 * only be called when that is guaranteed to be the case.
386 */
387 static bool_t
388 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
389 {
390 struct rc_data *rc = (struct rc_data *)cl->cl_private;
391
392 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
393 }
394
395 /*ARGSUSED*/
396 static void
397 clnt_reconnect_abort(CLIENT *h)
398 {
399 }
400
401 /*
402 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
403 * always be called before CLNT_CALL_MBUF() by a single thread only.
404 */
405 static bool_t
406 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
407 {
408 struct rc_data *rc = (struct rc_data *)cl->cl_private;
409 SVCXPRT *xprt;
410 size_t slen;
411
412 if (info == NULL) {
413 return (FALSE);
414 }
415 switch (request) {
416 case CLSET_TIMEOUT:
417 rc->rc_timeout = *(struct timeval *)info;
418 if (rc->rc_client)
419 CLNT_CONTROL(rc->rc_client, request, info);
420 break;
421
422 case CLGET_TIMEOUT:
423 *(struct timeval *)info = rc->rc_timeout;
424 break;
425
426 case CLSET_RETRY_TIMEOUT:
427 rc->rc_retry = *(struct timeval *)info;
428 if (rc->rc_client)
429 CLNT_CONTROL(rc->rc_client, request, info);
430 break;
431
432 case CLGET_RETRY_TIMEOUT:
433 *(struct timeval *)info = rc->rc_retry;
434 break;
435
436 case CLGET_VERS:
437 *(uint32_t *)info = rc->rc_vers;
438 break;
439
440 case CLSET_VERS:
441 rc->rc_vers = *(uint32_t *) info;
442 if (rc->rc_client)
443 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
444 break;
445
446 case CLGET_PROG:
447 *(uint32_t *)info = rc->rc_prog;
448 break;
449
450 case CLSET_PROG:
451 rc->rc_prog = *(uint32_t *) info;
452 if (rc->rc_client)
453 CLNT_CONTROL(rc->rc_client, request, info);
454 break;
455
456 case CLSET_WAITCHAN:
457 rc->rc_waitchan = (char *)info;
458 if (rc->rc_client)
459 CLNT_CONTROL(rc->rc_client, request, info);
460 break;
461
462 case CLGET_WAITCHAN:
463 *(const char **) info = rc->rc_waitchan;
464 break;
465
466 case CLSET_INTERRUPTIBLE:
467 rc->rc_intr = *(int *) info;
468 if (rc->rc_client)
469 CLNT_CONTROL(rc->rc_client, request, info);
470 break;
471
472 case CLGET_INTERRUPTIBLE:
473 *(int *) info = rc->rc_intr;
474 break;
475
476 case CLSET_RETRIES:
477 rc->rc_retries = *(int *) info;
478 break;
479
480 case CLGET_RETRIES:
481 *(int *) info = rc->rc_retries;
482 break;
483
484 case CLSET_PRIVPORT:
485 rc->rc_privport = *(int *) info;
486 break;
487
488 case CLGET_PRIVPORT:
489 *(int *) info = rc->rc_privport;
490 break;
491
492 case CLSET_BACKCHANNEL:
493 xprt = (SVCXPRT *)info;
494 xprt_register(xprt);
495 rc->rc_backchannel = info;
496 break;
497
498 case CLSET_TLS:
499 rc->rc_tls = true;
500 break;
501
502 case CLSET_TLSCERTNAME:
503 slen = strlen(info) + 1;
504 /*
505 * tlscertname with "key.pem" appended to it forms a file
506 * name. As such, the maximum allowable strlen(info) is
507 * NAME_MAX - 7. However, "slen" includes the nul termination
508 * byte so it can be up to NAME_MAX - 6.
509 */
510 if (slen <= 1 || slen > NAME_MAX - 6)
511 return (FALSE);
512 rc->rc_tlscertname = mem_alloc(slen);
513 strlcpy(rc->rc_tlscertname, info, slen);
514 break;
515
516 default:
517 return (FALSE);
518 }
519
520 return (TRUE);
521 }
522
523 static void
524 clnt_reconnect_close(CLIENT *cl)
525 {
526 struct rc_data *rc = (struct rc_data *)cl->cl_private;
527 CLIENT *client;
528
529 mtx_lock(&rc->rc_lock);
530
531 if (rc->rc_closed) {
532 mtx_unlock(&rc->rc_lock);
533 return;
534 }
535
536 rc->rc_closed = TRUE;
537 client = rc->rc_client;
538 rc->rc_client = NULL;
539
540 mtx_unlock(&rc->rc_lock);
541
542 if (client) {
543 CLNT_CLOSE(client);
544 CLNT_RELEASE(client);
545 }
546 }
547
548 static void
549 clnt_reconnect_destroy(CLIENT *cl)
550 {
551 struct rc_data *rc = (struct rc_data *)cl->cl_private;
552 SVCXPRT *xprt;
553
554 if (rc->rc_client)
555 CLNT_DESTROY(rc->rc_client);
556 if (rc->rc_backchannel) {
557 xprt = (SVCXPRT *)rc->rc_backchannel;
558 xprt_unregister(xprt);
559 SVC_RELEASE(xprt);
560 }
561 crfree(rc->rc_ucred);
562 mtx_destroy(&rc->rc_lock);
563 mem_free(rc->rc_tlscertname, 0); /* 0 ok, since arg. ignored. */
564 mem_free(rc, sizeof(*rc));
565 mem_free(cl, sizeof (CLIENT));
566 }
Cache object: c116a04e24fbeefef31fabb3e0e7cb50
|