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