libfdt: Update to 1.4.6, switch to using libfdt for overlay support
[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
52 static enum clnt_stat clnt_reconnect_call(CLIENT *, struct rpc_callextra *,
53     rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
54 static void clnt_reconnect_geterr(CLIENT *, struct rpc_err *);
55 static bool_t clnt_reconnect_freeres(CLIENT *, xdrproc_t, void *);
56 static void clnt_reconnect_abort(CLIENT *);
57 static bool_t clnt_reconnect_control(CLIENT *, u_int, void *);
58 static void clnt_reconnect_close(CLIENT *);
59 static void clnt_reconnect_destroy(CLIENT *);
60
61 static struct clnt_ops clnt_reconnect_ops = {
62         .cl_call =      clnt_reconnect_call,
63         .cl_abort =     clnt_reconnect_abort,
64         .cl_geterr =    clnt_reconnect_geterr,
65         .cl_freeres =   clnt_reconnect_freeres,
66         .cl_close =     clnt_reconnect_close,
67         .cl_destroy =   clnt_reconnect_destroy,
68         .cl_control =   clnt_reconnect_control
69 };
70
71 static int      fake_wchan;
72
73 CLIENT *
74 clnt_reconnect_create(
75         struct netconfig *nconf,        /* network type */
76         struct sockaddr *svcaddr,       /* servers address */
77         rpcprog_t program,              /* program number */
78         rpcvers_t version,              /* version number */
79         size_t sendsz,                  /* buffer recv size */
80         size_t recvsz)                  /* buffer send size */
81 {
82         CLIENT *cl = NULL;              /* client handle */
83         struct rc_data *rc = NULL;      /* private data */
84
85         if (svcaddr == NULL) {
86                 rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
87                 return (NULL);
88         }
89
90         cl = mem_alloc(sizeof (CLIENT));
91         rc = mem_alloc(sizeof (*rc));
92         mtx_init(&rc->rc_lock, "rc->rc_lock", NULL, MTX_DEF);
93         (void) memcpy(&rc->rc_addr, svcaddr, (size_t)svcaddr->sa_len);
94         rc->rc_nconf = nconf;
95         rc->rc_prog = program;
96         rc->rc_vers = version;
97         rc->rc_sendsz = sendsz;
98         rc->rc_recvsz = recvsz;
99         rc->rc_timeout.tv_sec = -1;
100         rc->rc_timeout.tv_usec = -1;
101         rc->rc_retry.tv_sec = 3;
102         rc->rc_retry.tv_usec = 0;
103         rc->rc_retries = INT_MAX;
104         rc->rc_privport = FALSE;
105         rc->rc_waitchan = "rpcrecv";
106         rc->rc_intr = 0;
107         rc->rc_connecting = FALSE;
108         rc->rc_closed = FALSE;
109         rc->rc_ucred = crdup(curthread->td_ucred);
110         rc->rc_client = NULL;
111
112         cl->cl_refs = 1;
113         cl->cl_ops = &clnt_reconnect_ops;
114         cl->cl_private = (caddr_t)(void *)rc;
115         cl->cl_auth = authnone_create();
116         cl->cl_tp = NULL;
117         cl->cl_netid = NULL;
118         return (cl);
119 }
120
121 static enum clnt_stat
122 clnt_reconnect_connect(CLIENT *cl)
123 {
124         struct thread *td = curthread;
125         struct rc_data *rc = (struct rc_data *)cl->cl_private;
126         struct socket *so;
127         enum clnt_stat stat;
128         int error;
129         int one = 1;
130         struct ucred *oldcred;
131         CLIENT *newclient = NULL;
132
133         mtx_lock(&rc->rc_lock);
134         while (rc->rc_connecting) {
135                 error = msleep(rc, &rc->rc_lock,
136                     rc->rc_intr ? PCATCH : 0, "rpcrecon", 0);
137                 if (error) {
138                         mtx_unlock(&rc->rc_lock);
139                         return (RPC_INTR);
140                 }
141         }
142         if (rc->rc_closed) {
143                 mtx_unlock(&rc->rc_lock);
144                 return (RPC_CANTSEND);
145         }
146         if (rc->rc_client) {
147                 mtx_unlock(&rc->rc_lock);
148                 return (RPC_SUCCESS);
149         }
150
151         /*
152          * My turn to attempt a connect. The rc_connecting variable
153          * serializes the following code sequence, so it is guaranteed
154          * that rc_client will still be NULL after it is re-locked below,
155          * since that is the only place it is set non-NULL.
156          */
157         rc->rc_connecting = TRUE;
158         mtx_unlock(&rc->rc_lock);
159
160         oldcred = td->td_ucred;
161         td->td_ucred = rc->rc_ucred;
162         so = __rpc_nconf2socket(rc->rc_nconf);
163         if (!so) {
164                 stat = rpc_createerr.cf_stat = RPC_TLIERROR;
165                 rpc_createerr.cf_error.re_errno = 0;
166                 td->td_ucred = oldcred;
167                 goto out;
168         }
169
170         if (rc->rc_privport)
171                 bindresvport(so, NULL);
172
173         if (rc->rc_nconf->nc_semantics == NC_TPI_CLTS)
174                 newclient = clnt_dg_create(so,
175                     (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
176                     rc->rc_sendsz, rc->rc_recvsz);
177         else
178                 newclient = clnt_vc_create(so,
179                     (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
180                     rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr);
181         td->td_ucred = oldcred;
182
183         if (!newclient) {
184                 soclose(so);
185                 rc->rc_err = rpc_createerr.cf_error;
186                 stat = rpc_createerr.cf_stat;
187                 goto out;
188         }
189
190         CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
191         CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
192         CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
193         CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
194         CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
195         CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
196         if (rc->rc_backchannel != NULL)
197                 CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel);
198         stat = RPC_SUCCESS;
199
200 out:
201         mtx_lock(&rc->rc_lock);
202         KASSERT(rc->rc_client == NULL, ("rc_client not null"));
203         if (!rc->rc_closed) {
204                 rc->rc_client = newclient;
205                 newclient = NULL;
206         }
207         rc->rc_connecting = FALSE;
208         wakeup(rc);
209         mtx_unlock(&rc->rc_lock);
210
211         if (newclient) {
212                 /*
213                  * It has been closed, so discard the new client.
214                  * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
215                  * be called with the rc_lock mutex held, since they may
216                  * msleep() while holding a different mutex.
217                  */
218                 CLNT_CLOSE(newclient);
219                 CLNT_RELEASE(newclient);
220         }
221
222         return (stat);
223 }
224
225 static enum clnt_stat
226 clnt_reconnect_call(
227         CLIENT          *cl,            /* client handle */
228         struct rpc_callextra *ext,      /* call metadata */
229         rpcproc_t       proc,           /* procedure number */
230         struct mbuf     *args,          /* pointer to args */
231         struct mbuf     **resultsp,     /* pointer to results */
232         struct timeval  utimeout)
233 {
234         struct rc_data *rc = (struct rc_data *)cl->cl_private;
235         CLIENT *client;
236         enum clnt_stat stat;
237         int tries, error;
238
239         tries = 0;
240         do {
241                 mtx_lock(&rc->rc_lock);
242                 if (rc->rc_closed) {
243                         mtx_unlock(&rc->rc_lock);
244                         return (RPC_CANTSEND);
245                 }
246
247                 if (!rc->rc_client) {
248                         mtx_unlock(&rc->rc_lock);
249                         stat = clnt_reconnect_connect(cl);
250                         if (stat == RPC_SYSTEMERROR) {
251                                 error = tsleep(&fake_wchan,
252                                     rc->rc_intr ? PCATCH : 0, "rpccon", hz);
253                                 if (error == EINTR || error == ERESTART)
254                                         return (RPC_INTR);
255                                 tries++;
256                                 if (tries >= rc->rc_retries)
257                                         return (stat);
258                                 continue;
259                         }
260                         if (stat != RPC_SUCCESS)
261                                 return (stat);
262                         mtx_lock(&rc->rc_lock);
263                 }
264
265                 if (!rc->rc_client) {
266                         mtx_unlock(&rc->rc_lock);
267                         stat = RPC_FAILED;
268                         continue;
269                 }
270                 CLNT_ACQUIRE(rc->rc_client);
271                 client = rc->rc_client;
272                 mtx_unlock(&rc->rc_lock);
273                 stat = CLNT_CALL_MBUF(client, ext, proc, args,
274                     resultsp, utimeout);
275
276                 if (stat != RPC_SUCCESS) {
277                         if (!ext)
278                                 CLNT_GETERR(client, &rc->rc_err);
279                 }
280
281                 if (stat == RPC_TIMEDOUT) {
282                         /*
283                          * Check for async send misfeature for NLM
284                          * protocol.
285                          */
286                         if ((rc->rc_timeout.tv_sec == 0
287                                 && rc->rc_timeout.tv_usec == 0)
288                             || (rc->rc_timeout.tv_sec == -1
289                                 && utimeout.tv_sec == 0
290                                 && utimeout.tv_usec == 0)) {
291                                 CLNT_RELEASE(client);
292                                 break;
293                         }
294                 }
295
296                 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
297                     || stat == RPC_CANTRECV) {
298                         tries++;
299                         if (tries >= rc->rc_retries) {
300                                 CLNT_RELEASE(client);
301                                 break;
302                         }
303
304                         if (ext && ext->rc_feedback)
305                                 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
306                                     ext->rc_feedback_arg);
307
308                         mtx_lock(&rc->rc_lock);
309                         /*
310                          * Make sure that someone else hasn't already
311                          * reconnected by checking if rc_client has changed.
312                          * If not, we are done with the client and must
313                          * do CLNT_RELEASE(client) twice to dispose of it,
314                          * because there is both an initial refcnt and one
315                          * acquired by CLNT_ACQUIRE() above.
316                          */
317                         if (rc->rc_client == client) {
318                                 rc->rc_client = NULL;
319                                 mtx_unlock(&rc->rc_lock);
320                                 CLNT_RELEASE(client);
321                         } else {
322                                 mtx_unlock(&rc->rc_lock);
323                         }
324                         CLNT_RELEASE(client);
325                 } else {
326                         CLNT_RELEASE(client);
327                         break;
328                 }
329         } while (stat != RPC_SUCCESS);
330
331         KASSERT(stat != RPC_SUCCESS || *resultsp,
332             ("RPC_SUCCESS without reply"));
333
334         return (stat);
335 }
336
337 static void
338 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
339 {
340         struct rc_data *rc = (struct rc_data *)cl->cl_private;
341
342         *errp = rc->rc_err;
343 }
344
345 /*
346  * Since this function requires that rc_client be valid, it can
347  * only be called when that is guaranteed to be the case.
348  */
349 static bool_t
350 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
351 {
352         struct rc_data *rc = (struct rc_data *)cl->cl_private;
353
354         return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
355 }
356
357 /*ARGSUSED*/
358 static void
359 clnt_reconnect_abort(CLIENT *h)
360 {
361 }
362
363 /*
364  * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
365  * always be called before CLNT_CALL_MBUF() by a single thread only.
366  */
367 static bool_t
368 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
369 {
370         struct rc_data *rc = (struct rc_data *)cl->cl_private;
371         SVCXPRT *xprt;
372
373         if (info == NULL) {
374                 return (FALSE);
375         }
376         switch (request) {
377         case CLSET_TIMEOUT:
378                 rc->rc_timeout = *(struct timeval *)info;
379                 if (rc->rc_client)
380                         CLNT_CONTROL(rc->rc_client, request, info);
381                 break;
382
383         case CLGET_TIMEOUT:
384                 *(struct timeval *)info = rc->rc_timeout;
385                 break;
386
387         case CLSET_RETRY_TIMEOUT:
388                 rc->rc_retry = *(struct timeval *)info;
389                 if (rc->rc_client)
390                         CLNT_CONTROL(rc->rc_client, request, info);
391                 break;
392
393         case CLGET_RETRY_TIMEOUT:
394                 *(struct timeval *)info = rc->rc_retry;
395                 break;
396
397         case CLGET_VERS:
398                 *(uint32_t *)info = rc->rc_vers;
399                 break;
400
401         case CLSET_VERS:
402                 rc->rc_vers = *(uint32_t *) info;
403                 if (rc->rc_client)
404                         CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
405                 break;
406
407         case CLGET_PROG:
408                 *(uint32_t *)info = rc->rc_prog;
409                 break;
410
411         case CLSET_PROG:
412                 rc->rc_prog = *(uint32_t *) info;
413                 if (rc->rc_client)
414                         CLNT_CONTROL(rc->rc_client, request, info);
415                 break;
416
417         case CLSET_WAITCHAN:
418                 rc->rc_waitchan = (char *)info;
419                 if (rc->rc_client)
420                         CLNT_CONTROL(rc->rc_client, request, info);
421                 break;
422
423         case CLGET_WAITCHAN:
424                 *(const char **) info = rc->rc_waitchan;
425                 break;
426
427         case CLSET_INTERRUPTIBLE:
428                 rc->rc_intr = *(int *) info;
429                 if (rc->rc_client)
430                         CLNT_CONTROL(rc->rc_client, request, info);
431                 break;
432
433         case CLGET_INTERRUPTIBLE:
434                 *(int *) info = rc->rc_intr;
435                 break;
436
437         case CLSET_RETRIES:
438                 rc->rc_retries = *(int *) info;
439                 break;
440
441         case CLGET_RETRIES:
442                 *(int *) info = rc->rc_retries;
443                 break;
444
445         case CLSET_PRIVPORT:
446                 rc->rc_privport = *(int *) info;
447                 break;
448
449         case CLGET_PRIVPORT:
450                 *(int *) info = rc->rc_privport;
451                 break;
452
453         case CLSET_BACKCHANNEL:
454                 xprt = (SVCXPRT *)info;
455                 xprt_register(xprt);
456                 rc->rc_backchannel = info;
457                 break;
458
459         default:
460                 return (FALSE);
461         }
462
463         return (TRUE);
464 }
465
466 static void
467 clnt_reconnect_close(CLIENT *cl)
468 {
469         struct rc_data *rc = (struct rc_data *)cl->cl_private;
470         CLIENT *client;
471
472         mtx_lock(&rc->rc_lock);
473
474         if (rc->rc_closed) {
475                 mtx_unlock(&rc->rc_lock);
476                 return;
477         }
478
479         rc->rc_closed = TRUE;
480         client = rc->rc_client;
481         rc->rc_client = NULL;
482
483         mtx_unlock(&rc->rc_lock);
484
485         if (client) {
486                 CLNT_CLOSE(client);
487                 CLNT_RELEASE(client);
488         }
489 }
490
491 static void
492 clnt_reconnect_destroy(CLIENT *cl)
493 {
494         struct rc_data *rc = (struct rc_data *)cl->cl_private;
495         SVCXPRT *xprt;
496
497         if (rc->rc_client)
498                 CLNT_DESTROY(rc->rc_client);
499         if (rc->rc_backchannel) {
500                 xprt = (SVCXPRT *)rc->rc_backchannel;
501                 xprt_unregister(xprt);
502                 SVC_RELEASE(xprt);
503         }
504         crfree(rc->rc_ucred);
505         mtx_destroy(&rc->rc_lock);
506         mem_free(rc, sizeof(*rc));
507         mem_free(cl, sizeof (CLIENT));
508 }