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: releng/12.0/sys/rpc/clnt_rc.c 336542 2018-07-20 12:03:16Z rmacklem $");
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 /*
179 * I do not believe a timeout of less than 1sec would make
180 * sense here since short delays can occur when a server is
181 * temporarily overloaded.
182 */
183 if (rc->rc_timeout.tv_sec > 0 && rc->rc_timeout.tv_usec >= 0) {
184 error = so_setsockopt(so, SOL_SOCKET, SO_SNDTIMEO,
185 &rc->rc_timeout, sizeof(struct timeval));
186 if (error != 0) {
187 stat = rpc_createerr.cf_stat = RPC_CANTSEND;
188 rpc_createerr.cf_error.re_errno = error;
189 td->td_ucred = oldcred;
190 goto out;
191 }
192 }
193 newclient = clnt_vc_create(so,
194 (struct sockaddr *) &rc->rc_addr, rc->rc_prog, rc->rc_vers,
195 rc->rc_sendsz, rc->rc_recvsz, rc->rc_intr);
196 }
197 td->td_ucred = oldcred;
198
199 if (!newclient) {
200 soclose(so);
201 rc->rc_err = rpc_createerr.cf_error;
202 stat = rpc_createerr.cf_stat;
203 goto out;
204 }
205
206 CLNT_CONTROL(newclient, CLSET_FD_CLOSE, 0);
207 CLNT_CONTROL(newclient, CLSET_CONNECT, &one);
208 CLNT_CONTROL(newclient, CLSET_TIMEOUT, &rc->rc_timeout);
209 CLNT_CONTROL(newclient, CLSET_RETRY_TIMEOUT, &rc->rc_retry);
210 CLNT_CONTROL(newclient, CLSET_WAITCHAN, rc->rc_waitchan);
211 CLNT_CONTROL(newclient, CLSET_INTERRUPTIBLE, &rc->rc_intr);
212 if (rc->rc_backchannel != NULL)
213 CLNT_CONTROL(newclient, CLSET_BACKCHANNEL, rc->rc_backchannel);
214 stat = RPC_SUCCESS;
215
216 out:
217 mtx_lock(&rc->rc_lock);
218 KASSERT(rc->rc_client == NULL, ("rc_client not null"));
219 if (!rc->rc_closed) {
220 rc->rc_client = newclient;
221 newclient = NULL;
222 }
223 rc->rc_connecting = FALSE;
224 wakeup(rc);
225 mtx_unlock(&rc->rc_lock);
226
227 if (newclient) {
228 /*
229 * It has been closed, so discard the new client.
230 * nb: clnt_[dg|vc]_close()/clnt_[dg|vc]_destroy() cannot
231 * be called with the rc_lock mutex held, since they may
232 * msleep() while holding a different mutex.
233 */
234 CLNT_CLOSE(newclient);
235 CLNT_RELEASE(newclient);
236 }
237
238 return (stat);
239 }
240
241 static enum clnt_stat
242 clnt_reconnect_call(
243 CLIENT *cl, /* client handle */
244 struct rpc_callextra *ext, /* call metadata */
245 rpcproc_t proc, /* procedure number */
246 struct mbuf *args, /* pointer to args */
247 struct mbuf **resultsp, /* pointer to results */
248 struct timeval utimeout)
249 {
250 struct rc_data *rc = (struct rc_data *)cl->cl_private;
251 CLIENT *client;
252 enum clnt_stat stat;
253 int tries, error;
254
255 tries = 0;
256 do {
257 mtx_lock(&rc->rc_lock);
258 if (rc->rc_closed) {
259 mtx_unlock(&rc->rc_lock);
260 return (RPC_CANTSEND);
261 }
262
263 if (!rc->rc_client) {
264 mtx_unlock(&rc->rc_lock);
265 stat = clnt_reconnect_connect(cl);
266 if (stat == RPC_SYSTEMERROR) {
267 error = tsleep(&fake_wchan,
268 rc->rc_intr ? PCATCH : 0, "rpccon", hz);
269 if (error == EINTR || error == ERESTART)
270 return (RPC_INTR);
271 tries++;
272 if (tries >= rc->rc_retries)
273 return (stat);
274 continue;
275 }
276 if (stat != RPC_SUCCESS)
277 return (stat);
278 mtx_lock(&rc->rc_lock);
279 }
280
281 if (!rc->rc_client) {
282 mtx_unlock(&rc->rc_lock);
283 stat = RPC_FAILED;
284 continue;
285 }
286 CLNT_ACQUIRE(rc->rc_client);
287 client = rc->rc_client;
288 mtx_unlock(&rc->rc_lock);
289 stat = CLNT_CALL_MBUF(client, ext, proc, args,
290 resultsp, utimeout);
291
292 if (stat != RPC_SUCCESS) {
293 if (!ext)
294 CLNT_GETERR(client, &rc->rc_err);
295 }
296
297 if (stat == RPC_TIMEDOUT) {
298 /*
299 * Check for async send misfeature for NLM
300 * protocol.
301 */
302 if ((rc->rc_timeout.tv_sec == 0
303 && rc->rc_timeout.tv_usec == 0)
304 || (rc->rc_timeout.tv_sec == -1
305 && utimeout.tv_sec == 0
306 && utimeout.tv_usec == 0)) {
307 CLNT_RELEASE(client);
308 break;
309 }
310 }
311
312 if (stat == RPC_TIMEDOUT || stat == RPC_CANTSEND
313 || stat == RPC_CANTRECV) {
314 tries++;
315 if (tries >= rc->rc_retries) {
316 CLNT_RELEASE(client);
317 break;
318 }
319
320 if (ext && ext->rc_feedback)
321 ext->rc_feedback(FEEDBACK_RECONNECT, proc,
322 ext->rc_feedback_arg);
323
324 mtx_lock(&rc->rc_lock);
325 /*
326 * Make sure that someone else hasn't already
327 * reconnected by checking if rc_client has changed.
328 * If not, we are done with the client and must
329 * do CLNT_RELEASE(client) twice to dispose of it,
330 * because there is both an initial refcnt and one
331 * acquired by CLNT_ACQUIRE() above.
332 */
333 if (rc->rc_client == client) {
334 rc->rc_client = NULL;
335 mtx_unlock(&rc->rc_lock);
336 CLNT_RELEASE(client);
337 } else {
338 mtx_unlock(&rc->rc_lock);
339 }
340 CLNT_RELEASE(client);
341 } else {
342 CLNT_RELEASE(client);
343 break;
344 }
345 } while (stat != RPC_SUCCESS);
346
347 KASSERT(stat != RPC_SUCCESS || *resultsp,
348 ("RPC_SUCCESS without reply"));
349
350 return (stat);
351 }
352
353 static void
354 clnt_reconnect_geterr(CLIENT *cl, struct rpc_err *errp)
355 {
356 struct rc_data *rc = (struct rc_data *)cl->cl_private;
357
358 *errp = rc->rc_err;
359 }
360
361 /*
362 * Since this function requires that rc_client be valid, it can
363 * only be called when that is guaranteed to be the case.
364 */
365 static bool_t
366 clnt_reconnect_freeres(CLIENT *cl, xdrproc_t xdr_res, void *res_ptr)
367 {
368 struct rc_data *rc = (struct rc_data *)cl->cl_private;
369
370 return (CLNT_FREERES(rc->rc_client, xdr_res, res_ptr));
371 }
372
373 /*ARGSUSED*/
374 static void
375 clnt_reconnect_abort(CLIENT *h)
376 {
377 }
378
379 /*
380 * CLNT_CONTROL() on the client returned by clnt_reconnect_create() must
381 * always be called before CLNT_CALL_MBUF() by a single thread only.
382 */
383 static bool_t
384 clnt_reconnect_control(CLIENT *cl, u_int request, void *info)
385 {
386 struct rc_data *rc = (struct rc_data *)cl->cl_private;
387 SVCXPRT *xprt;
388
389 if (info == NULL) {
390 return (FALSE);
391 }
392 switch (request) {
393 case CLSET_TIMEOUT:
394 rc->rc_timeout = *(struct timeval *)info;
395 if (rc->rc_client)
396 CLNT_CONTROL(rc->rc_client, request, info);
397 break;
398
399 case CLGET_TIMEOUT:
400 *(struct timeval *)info = rc->rc_timeout;
401 break;
402
403 case CLSET_RETRY_TIMEOUT:
404 rc->rc_retry = *(struct timeval *)info;
405 if (rc->rc_client)
406 CLNT_CONTROL(rc->rc_client, request, info);
407 break;
408
409 case CLGET_RETRY_TIMEOUT:
410 *(struct timeval *)info = rc->rc_retry;
411 break;
412
413 case CLGET_VERS:
414 *(uint32_t *)info = rc->rc_vers;
415 break;
416
417 case CLSET_VERS:
418 rc->rc_vers = *(uint32_t *) info;
419 if (rc->rc_client)
420 CLNT_CONTROL(rc->rc_client, CLSET_VERS, info);
421 break;
422
423 case CLGET_PROG:
424 *(uint32_t *)info = rc->rc_prog;
425 break;
426
427 case CLSET_PROG:
428 rc->rc_prog = *(uint32_t *) info;
429 if (rc->rc_client)
430 CLNT_CONTROL(rc->rc_client, request, info);
431 break;
432
433 case CLSET_WAITCHAN:
434 rc->rc_waitchan = (char *)info;
435 if (rc->rc_client)
436 CLNT_CONTROL(rc->rc_client, request, info);
437 break;
438
439 case CLGET_WAITCHAN:
440 *(const char **) info = rc->rc_waitchan;
441 break;
442
443 case CLSET_INTERRUPTIBLE:
444 rc->rc_intr = *(int *) info;
445 if (rc->rc_client)
446 CLNT_CONTROL(rc->rc_client, request, info);
447 break;
448
449 case CLGET_INTERRUPTIBLE:
450 *(int *) info = rc->rc_intr;
451 break;
452
453 case CLSET_RETRIES:
454 rc->rc_retries = *(int *) info;
455 break;
456
457 case CLGET_RETRIES:
458 *(int *) info = rc->rc_retries;
459 break;
460
461 case CLSET_PRIVPORT:
462 rc->rc_privport = *(int *) info;
463 break;
464
465 case CLGET_PRIVPORT:
466 *(int *) info = rc->rc_privport;
467 break;
468
469 case CLSET_BACKCHANNEL:
470 xprt = (SVCXPRT *)info;
471 xprt_register(xprt);
472 rc->rc_backchannel = info;
473 break;
474
475 default:
476 return (FALSE);
477 }
478
479 return (TRUE);
480 }
481
482 static void
483 clnt_reconnect_close(CLIENT *cl)
484 {
485 struct rc_data *rc = (struct rc_data *)cl->cl_private;
486 CLIENT *client;
487
488 mtx_lock(&rc->rc_lock);
489
490 if (rc->rc_closed) {
491 mtx_unlock(&rc->rc_lock);
492 return;
493 }
494
495 rc->rc_closed = TRUE;
496 client = rc->rc_client;
497 rc->rc_client = NULL;
498
499 mtx_unlock(&rc->rc_lock);
500
501 if (client) {
502 CLNT_CLOSE(client);
503 CLNT_RELEASE(client);
504 }
505 }
506
507 static void
508 clnt_reconnect_destroy(CLIENT *cl)
509 {
510 struct rc_data *rc = (struct rc_data *)cl->cl_private;
511 SVCXPRT *xprt;
512
513 if (rc->rc_client)
514 CLNT_DESTROY(rc->rc_client);
515 if (rc->rc_backchannel) {
516 xprt = (SVCXPRT *)rc->rc_backchannel;
517 xprt_unregister(xprt);
518 SVC_RELEASE(xprt);
519 }
520 crfree(rc->rc_ucred);
521 mtx_destroy(&rc->rc_lock);
522 mem_free(rc, sizeof(*rc));
523 mem_free(cl, sizeof (CLIENT));
524 }
Cache object: aac8a6eb4bd23c5386fe38081d684861
|