]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/rpc/clnt_rc.c
ident(1): Normalizing date format
[FreeBSD/FreeBSD.git] / 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
114         cl->cl_refs = 1;
115         cl->cl_ops = &clnt_reconnect_ops;
116         cl->cl_private = (caddr_t)(void *)rc;
117         cl->cl_auth = authnone_create();
118         cl->cl_tp = NULL;
119         cl->cl_netid = NULL;
120         return (cl);
121 }
122
123 static enum clnt_stat
124 clnt_reconnect_connect(CLIENT *cl)
125 {
126         struct thread *td = curthread;
127         struct rc_data *rc = (struct rc_data *)cl->cl_private;
128         struct socket *so;
129         enum clnt_stat stat;
130         int error;
131         int one = 1;
132         struct ucred *oldcred;
133         CLIENT *newclient = NULL;
134         uint64_t ssl[3];
135         uint32_t reterr;
136
137         mtx_lock(&rc->rc_lock);
138         while (rc->rc_connecting) {
139                 error = msleep(rc, &rc->rc_lock,
140                     rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
141                 if (error) {
142                         mtx_unlock(&rc->rc_lock);
143                         return (RPC_INTR);
144                 }
145         }
146         if (rc->rc_closed) {
147                 mtx_unlock(&rc->rc_lock);
148                 return (RPC_CANTSEND);
149         }
150         if (rc->rc_client) {
151                 mtx_unlock(&rc->rc_lock);
152                 return (RPC_SUCCESS);
153         }
154
155         /*
156          * My turn to attempt a connect. The rc_connecting variable
157          * serializes the following code sequence, so it is guaranteed
158          * that rc_client will still be NULL after it is re-locked below,
159          * since that is the only place it is set non-NULL.
160          */
161         rc->rc_connecting = TRUE;
162         mtx_unlock(&rc->rc_lock);
163
164         oldcred = td->td_ucred;
165         td->td_ucred = rc->rc_ucred;
166         so = __rpc_nconf2socket(rc->rc_nconf);
167         if (!so) {
168                 stat = rpc_createerr.cf_stat = RPC_TLIERROR;
169                 rpc_createerr.cf_error.re_errno = 0;
170                 td->td_ucred = oldcred;
171                 goto out;
172         }
173
174         if (rc->rc_privport)
175                 bindresvport(so, NULL);
176
177         if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
178                 newclient = clnt_dg_create(so,
179                     (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
180                     rc->rc_sendsz, rc->rc_recvsz);
181         else {
182                 /*
183                  * I do not believe a timeout of less than 1sec would make
184                  * sense here since short delays can occur when a server is
185                  * temporarily overloaded.
186                  */
187                 if (rc->rc_timeout.tv_sec > 0 && rc->rc_timeout.tv_usec >= 0) {
188                         error = so_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO,
189                             &rc->rc_timeout, sizeof(struct timeval));
190                         if (error != 0) {
191                                 stat = rpc_createerr.cf_stat = RPC_CANTSEND;
192                                 rpc_createerr.cf_error.re_errno = error;
193                                 td->td_ucred = oldcred;
194                                 goto out;
195                         }
196                 }
197                 newclient = clnt_vc_create(so,
198                     (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
199                     rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr);
200                 if (rc->rc_tls && newclient != NULL) {
201                         stat = rpctls_connect(newclient, so, ssl, &reterr);
202                         if (stat != RPC_SUCCESS || reterr != RPCTLSERR_OK) {
203                                 if (stat == RPC_SUCCESS)
204                                         stat = RPC_FAILED;
205                                 stat = rpc_createerr.cf_stat = stat;
206                                 rpc_createerr.cf_error.re_errno = 0;
207                                 CLNT_CLOSE(newclient);
208                                 CLNT_RELEASE(newclient);
209                                 newclient = NULL;
210                                 td->td_ucred = oldcred;
211                                 goto out;
212                         }
213                 }
214         }
215         td->td_ucred = oldcred;
216
217         if (!newclient) {
218                 soclose(so);
219                 rc->rc_err = rpc_createerr.cf_error;
220                 stat = rpc_createerr.cf_stat;
221                 goto out;
222         }
223
224         CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
225         CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
226         CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
227         CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
228         CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
229         CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
230         if (rc->rc_tls)
231                 CLNT_CONTROL(newclient, CLSET_TLS, ssl);
232         if (rc->rc_backchannel != NULL)
233                 CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel);
234         stat = RPC_SUCCESS;
235
236 out:
237         mtx_lock(&rc->rc_lock);
238         KASSERT(rc->rc_client == NULL, ("rc_client not null"));
239         if (!rc->rc_closed) {
240                 rc->rc_client = newclient;
241                 newclient = NULL;
242         }
243         rc->rc_connecting = FALSE;
244         wakeup(rc);
245         mtx_unlock(&rc->rc_lock);
246
247         if (newclient) {
248                 /*
249                  * It has been closed, so discard the new client.
250                  * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
251                  * be called with the rc_lock mutex held, since they may
252                  * msleep() while holding a different mutex.
253                  */
254                 CLNT_CLOSE(newclient);
255                 CLNT_RELEASE(newclient);
256         }
257
258         return (stat);
259 }
260
261 static enum clnt_stat
262 clnt_reconnect_call(
263         CLIENT          *cl,            /* client handle */
264         struct rpc_callextra *ext,      /* call metadata */
265         rpcproc_t       proc,           /* procedure number */
266         struct mbuf     *args,          /* pointer to args */
267         struct mbuf     **resultsp,     /* pointer to results */
268         struct timeval  utimeout)
269 {
270         struct rc_data *rc = (struct rc_data *)cl->cl_private;
271         CLIENT *client;
272         enum clnt_stat stat;
273         int tries, error;
274
275         tries = 0;
276         do {
277                 mtx_lock(&rc->rc_lock);
278                 if (rc->rc_closed) {
279                         mtx_unlock(&rc->rc_lock);
280                         return (RPC_CANTSEND);
281                 }
282
283                 if (!rc->rc_client) {
284                         mtx_unlock(&rc->rc_lock);
285                         stat = clnt_reconnect_connect(cl);
286                         if (stat == RPC_SYSTEMERROR) {
287                                 error = tsleep(&fake_wchan,
288                                     rc->rc_intr ? PCATCH : 0, "rpccon", hz);
289                                 if (error == EINTR || error == ERESTART)
290                                         return (RPC_INTR);
291                                 tries++;
292                                 if (tries >= rc->rc_retries)
293                                         return (stat);
294                                 continue;
295                         }
296                         if (stat != RPC_SUCCESS)
297                                 return (stat);
298                         mtx_lock(&rc->rc_lock);
299                 }
300
301                 if (!rc->rc_client) {
302                         mtx_unlock(&rc->rc_lock);
303                         stat = RPC_FAILED;
304                         continue;
305                 }
306                 CLNT_ACQUIRE(rc->rc_client);
307                 client = rc->rc_client;
308                 mtx_unlock(&rc->rc_lock);
309                 stat = CLNT_CALL_MBUF(client, ext, proc, args,
310                     resultsp, utimeout);
311
312                 if (stat != RPC_SUCCESS) {
313                         if (!ext)
314                                 CLNT_GETERR(client, &rc->rc_err);
315                 }
316
317                 if (stat == RPC_TIMEDOUT) {
318                         /*
319                          * Check for async send misfeature for NLM
320                          * protocol.
321                          */
322                         if ((rc->rc_timeout.tv_sec == 0
323                                 && rc->rc_timeout.tv_usec == 0)
324                             || (rc->rc_timeout.tv_sec == -1
325                                 && utimeout.tv_sec == 0
326                                 && utimeout.tv_usec == 0)) {
327                                 CLNT_RELEASE(client);
328                                 break;
329                         }
330                 }
331
332                 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
333                     || stat == RPC_CANTRECV) {
334                         tries++;
335                         if (tries >= rc->rc_retries) {
336                                 CLNT_RELEASE(client);
337                                 break;
338                         }
339
340                         if (ext && ext->rc_feedback)
341                                 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
342                                     ext->rc_feedback_arg);
343
344                         mtx_lock(&rc->rc_lock);
345                         /*
346                          * Make sure that someone else hasn't already
347                          * reconnected by checking if rc_client has changed.
348                          * If not, we are done with the client and must
349                          * do CLNT_RELEASE(client) twice to dispose of it,
350                          * because there is both an initial refcnt and one
351                          * acquired by CLNT_ACQUIRE() above.
352                          */
353                         if (rc->rc_client == client) {
354                                 rc->rc_client = NULL;
355                                 mtx_unlock(&rc->rc_lock);
356                                 CLNT_RELEASE(client);
357                         } else {
358                                 mtx_unlock(&rc->rc_lock);
359                         }
360                         CLNT_RELEASE(client);
361                 } else {
362                         CLNT_RELEASE(client);
363                         break;
364                 }
365         } while (stat != RPC_SUCCESS);
366
367         KASSERT(stat != RPC_SUCCESS || *resultsp,
368             ("RPC_SUCCESS without reply"));
369
370         return (stat);
371 }
372
373 static void
374 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
375 {
376         struct rc_data *rc = (struct rc_data *)cl->cl_private;
377
378         *errp = rc->rc_err;
379 }
380
381 /*
382  * Since this function requires that rc_client be valid, it can
383  * only be called when that is guaranteed to be the case.
384  */
385 static bool_t
386 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
387 {
388         struct rc_data *rc = (struct rc_data *)cl->cl_private;
389
390         return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
391 }
392
393 /*ARGSUSED*/
394 static void
395 clnt_reconnect_abort(CLIENT *h)
396 {
397 }
398
399 /*
400  * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
401  * always be called before CLNT_CALL_MBUF() by a single thread only.
402  */
403 static bool_t
404 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
405 {
406         struct rc_data *rc = (struct rc_data *)cl->cl_private;
407         SVCXPRT *xprt;
408
409         if (info == NULL) {
410                 return (FALSE);
411         }
412         switch (request) {
413         case CLSET_TIMEOUT:
414                 rc->rc_timeout = *(struct timeval *)info;
415                 if (rc->rc_client)
416                         CLNT_CONTROL(rc->rc_client, request, info);
417                 break;
418
419         case CLGET_TIMEOUT:
420                 *(struct timeval *)info = rc->rc_timeout;
421                 break;
422
423         case CLSET_RETRY_TIMEOUT:
424                 rc->rc_retry = *(struct timeval *)info;
425                 if (rc->rc_client)
426                         CLNT_CONTROL(rc->rc_client, request, info);
427                 break;
428
429         case CLGET_RETRY_TIMEOUT:
430                 *(struct timeval *)info = rc->rc_retry;
431                 break;
432
433         case CLGET_VERS:
434                 *(uint32_t *)info = rc->rc_vers;
435                 break;
436
437         case CLSET_VERS:
438                 rc->rc_vers = *(uint32_t *) info;
439                 if (rc->rc_client)
440                         CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
441                 break;
442
443         case CLGET_PROG:
444                 *(uint32_t *)info = rc->rc_prog;
445                 break;
446
447         case CLSET_PROG:
448                 rc->rc_prog = *(uint32_t *) info;
449                 if (rc->rc_client)
450                         CLNT_CONTROL(rc->rc_client, request, info);
451                 break;
452
453         case CLSET_WAITCHAN:
454                 rc->rc_waitchan = (char *)info;
455                 if (rc->rc_client)
456                         CLNT_CONTROL(rc->rc_client, request, info);
457                 break;
458
459         case CLGET_WAITCHAN:
460                 *(const char **) info = rc->rc_waitchan;
461                 break;
462
463         case CLSET_INTERRUPTIBLE:
464                 rc->rc_intr = *(int *) info;
465                 if (rc->rc_client)
466                         CLNT_CONTROL(rc->rc_client, request, info);
467                 break;
468
469         case CLGET_INTERRUPTIBLE:
470                 *(int *) info = rc->rc_intr;
471                 break;
472
473         case CLSET_RETRIES:
474                 rc->rc_retries = *(int *) info;
475                 break;
476
477         case CLGET_RETRIES:
478                 *(int *) info = rc->rc_retries;
479                 break;
480
481         case CLSET_PRIVPORT:
482                 rc->rc_privport = *(int *) info;
483                 break;
484
485         case CLGET_PRIVPORT:
486                 *(int *) info = rc->rc_privport;
487                 break;
488
489         case CLSET_BACKCHANNEL:
490                 xprt = (SVCXPRT *)info;
491                 xprt_register(xprt);
492                 rc->rc_backchannel = info;
493                 break;
494
495         case CLSET_TLS:
496                 rc->rc_tls = true;
497                 break;
498
499         default:
500                 return (FALSE);
501         }
502
503         return (TRUE);
504 }
505
506 static void
507 clnt_reconnect_close(CLIENT *cl)
508 {
509         struct rc_data *rc = (struct rc_data *)cl->cl_private;
510         CLIENT *client;
511
512         mtx_lock(&rc->rc_lock);
513
514         if (rc->rc_closed) {
515                 mtx_unlock(&rc->rc_lock);
516                 return;
517         }
518
519         rc->rc_closed = TRUE;
520         client = rc->rc_client;
521         rc->rc_client = NULL;
522
523         mtx_unlock(&rc->rc_lock);
524
525         if (client) {
526                 CLNT_CLOSE(client);
527                 CLNT_RELEASE(client);
528         }
529 }
530
531 static void
532 clnt_reconnect_destroy(CLIENT *cl)
533 {
534         struct rc_data *rc = (struct rc_data *)cl->cl_private;
535         SVCXPRT *xprt;
536
537         if (rc->rc_client)
538                 CLNT_DESTROY(rc->rc_client);
539         if (rc->rc_backchannel) {
540                 xprt = (SVCXPRT *)rc->rc_backchannel;
541                 xprt_unregister(xprt);
542                 SVC_RELEASE(xprt);
543         }
544         crfree(rc->rc_ucred);
545         mtx_destroy(&rc->rc_lock);
546         mem_free(rc, sizeof(*rc));
547         mem_free(cl, sizeof (CLIENT));
548 }