Whamcloud - gitweb
LU-9859 libcfs: remove unnecessary cfs_block_allsigs() calls
[fs/lustre-release.git] / lnet / lnet / acceptor.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LNET
34
35 #include <linux/completion.h>
36 #include <net/sock.h>
37 #include <lnet/lib-lnet.h>
38
39 static int   accept_port    = 988;
40 static int   accept_backlog = 127;
41 static int   accept_timeout = 5;
42
43 static struct {
44         int                     pta_shutdown;
45         struct socket           *pta_sock;
46         struct completion       pta_signal;
47         struct net              *pta_ns;
48         wait_queue_head_t       pta_waitq;
49         atomic_t                pta_ready;
50 #ifdef HAVE_SK_DATA_READY_ONE_ARG
51         void                    (*pta_odata)(struct sock *);
52 #else
53         void                    (*pta_odata)(struct sock *, int);
54 #endif
55 } lnet_acceptor_state = {
56         .pta_shutdown = 1
57 };
58
59 int
60 lnet_acceptor_port(void)
61 {
62         return accept_port;
63 }
64
65 static inline int
66 lnet_accept_magic(__u32 magic, __u32 constant)
67 {
68         return (magic == constant ||
69                 magic == __swab32(constant));
70 }
71
72 EXPORT_SYMBOL(lnet_acceptor_port);
73
74 static char *accept = "secure";
75
76 module_param(accept, charp, 0444);
77 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
78 module_param(accept_port, int, 0444);
79 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
80 module_param(accept_backlog, int, 0444);
81 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
82 module_param(accept_timeout, int, 0644);
83 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
84
85 static char *accept_type = NULL;
86
87 static int
88 lnet_acceptor_get_tunables(void)
89 {
90         /* Userland acceptor uses 'accept_type' instead of 'accept', due to
91          * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
92          * for compatibility. Hence the trick. */
93         accept_type = accept;
94         return 0;
95 }
96
97 int
98 lnet_acceptor_timeout(void)
99 {
100         return accept_timeout;
101 }
102 EXPORT_SYMBOL(lnet_acceptor_timeout);
103
104 void
105 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
106                            __u32 peer_ip, int peer_port)
107 {
108         switch (rc) {
109         /* "normal" errors */
110         case -ECONNREFUSED:
111                 CNETERR("Connection to %s at host %pI4h on port %d was "
112                         "refused: check that Lustre is running on that node.\n",
113                         libcfs_nid2str(peer_nid), &peer_ip, peer_port);
114                 break;
115         case -EHOSTUNREACH:
116         case -ENETUNREACH:
117                 CNETERR("Connection to %s at host %pI4h "
118                         "was unreachable: the network or that node may "
119                         "be down, or Lustre may be misconfigured.\n",
120                         libcfs_nid2str(peer_nid), &peer_ip);
121                 break;
122         case -ETIMEDOUT:
123                 CNETERR("Connection to %s at host %pI4h on "
124                         "port %d took too long: that node may be hung "
125                         "or experiencing high load.\n",
126                         libcfs_nid2str(peer_nid), &peer_ip, peer_port);
127                 break;
128         case -ECONNRESET:
129                 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
130                                    " on port %d was reset: "
131                                    "is it running a compatible version of "
132                                    "Lustre and is %s one of its NIDs?\n",
133                                    libcfs_nid2str(peer_nid), &peer_ip,
134                                    peer_port, libcfs_nid2str(peer_nid));
135                 break;
136         case -EPROTO:
137                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
138                                    "host %pI4h on port %d: is it running "
139                                    "a compatible version of Lustre?\n",
140                                    libcfs_nid2str(peer_nid), &peer_ip,
141                                    peer_port);
142                 break;
143         case -EADDRINUSE:
144                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
145                                    "connect to %s at host %pI4h on port "
146                                    "%d\n", libcfs_nid2str(peer_nid),
147                                    &peer_ip, peer_port);
148                 break;
149         default:
150                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
151                                    " at host %pI4h on port %d\n", rc,
152                                    libcfs_nid2str(peer_nid),
153                                    &peer_ip, peer_port);
154                 break;
155         }
156 }
157 EXPORT_SYMBOL(lnet_connect_console_error);
158
159 int
160 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
161             __u32 local_ip, __u32 peer_ip, int peer_port, struct net *ns)
162 {
163         struct lnet_acceptor_connreq cr;
164         struct socket           *sock;
165         int                     rc;
166         int                     port;
167         int                     fatal;
168
169         BUILD_BUG_ON(sizeof(cr) > 16); /* not too big to be on the stack */
170
171         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
172              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
173              --port) {
174                 /* Iterate through reserved ports. */
175
176                 rc = lnet_sock_connect(&sock, &fatal,
177                                          local_ip, port,
178                                          peer_ip, peer_port, ns);
179                 if (rc != 0) {
180                         if (fatal)
181                                 goto failed;
182                         continue;
183                 }
184
185                 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
186
187                 cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
188                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
189                 cr.acr_nid     = peer_nid;
190
191                 if (the_lnet.ln_testprotocompat) {
192                         /* single-shot proto check */
193                         if (test_and_clear_bit(2, &the_lnet.ln_testprotocompat))
194                                 cr.acr_version++;
195                         if (test_and_clear_bit(3, &the_lnet.ln_testprotocompat))
196                                 cr.acr_magic = LNET_PROTO_MAGIC;
197                 }
198
199                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
200                                        accept_timeout);
201                 if (rc != 0)
202                         goto failed_sock;
203
204                 *sockp = sock;
205                 return 0;
206         }
207
208         rc = -EADDRINUSE;
209         goto failed;
210
211 failed_sock:
212         sock_release(sock);
213 failed:
214         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
215         return rc;
216 }
217 EXPORT_SYMBOL(lnet_connect);
218
219 static int
220 lnet_accept(struct socket *sock, __u32 magic)
221 {
222         struct lnet_acceptor_connreq cr;
223         __u32                   peer_ip;
224         int                     peer_port;
225         int                     rc;
226         int                     flip;
227         struct lnet_ni *ni;
228         char                   *str;
229
230         LASSERT(sizeof(cr) <= 16);              /* not too big for the stack */
231
232         rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
233         LASSERT(rc == 0);                       /* we succeeded before */
234
235         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
236
237                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
238                         /* future version compatibility!
239                          * When LNET unifies protocols over all LNDs, the first
240                          * thing sent will be a version query.  I send back
241                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
242
243                         memset(&cr, 0, sizeof(cr));
244                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
245                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
246                         rc = lnet_sock_write(sock, &cr, sizeof(cr),
247                                                accept_timeout);
248
249                         if (rc != 0)
250                                 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
251                                        &peer_ip, rc);
252                         return -EPROTO;
253                 }
254
255                 if (lnet_accept_magic(magic, LNET_PROTO_TCP_MAGIC))
256                         str = "'old' socknal/tcpnal";
257                 else
258                         str = "unrecognised";
259
260                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
261                                    " magic %08x: %s acceptor protocol\n",
262                                    &peer_ip, magic, str);
263                 return -EPROTO;
264         }
265
266         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
267
268         rc = lnet_sock_read(sock, &cr.acr_version,
269                               sizeof(cr.acr_version),
270                               accept_timeout);
271         if (rc != 0) {
272                 CERROR("Error %d reading connection request version from "
273                        "%pI4h\n", rc, &peer_ip);
274                 return -EIO;
275         }
276
277         if (flip)
278                 __swab32s(&cr.acr_version);
279
280         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
281                 /* future version compatibility!
282                  * An acceptor-specific protocol rev will first send a version
283                  * query.  I send back my current version to tell her I'm
284                  * "old". */
285                 int peer_version = cr.acr_version;
286
287                 memset(&cr, 0, sizeof(cr));
288                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
289                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
290
291                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
292                                        accept_timeout);
293
294                 if (rc != 0)
295                         CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
296                                peer_version, &peer_ip, rc);
297                 return -EPROTO;
298         }
299
300         rc = lnet_sock_read(sock, &cr.acr_nid,
301                               sizeof(cr) -
302                               offsetof(struct lnet_acceptor_connreq, acr_nid),
303                               accept_timeout);
304         if (rc != 0) {
305                 CERROR("Error %d reading connection request from "
306                        "%pI4h\n", rc, &peer_ip);
307                 return -EIO;
308         }
309
310         if (flip)
311                 __swab64s(&cr.acr_nid);
312
313         ni = lnet_nid2ni_addref(cr.acr_nid);
314         if (ni == NULL ||               /* no matching net */
315             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
316                 if (ni != NULL)
317                         lnet_ni_decref(ni);
318                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
319                                    "for %s: No matching NI\n",
320                                    &peer_ip, libcfs_nid2str(cr.acr_nid));
321                 return -EPERM;
322         }
323
324         if (ni->ni_net->net_lnd->lnd_accept == NULL) {
325                 /* This catches a request for the loopback LND */
326                 lnet_ni_decref(ni);
327                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
328                                   "for %s: NI doesn not accept IP connections\n",
329                                   &peer_ip, libcfs_nid2str(cr.acr_nid));
330                 return -EPERM;
331         }
332
333         CDEBUG(D_NET, "Accept %s from %pI4h\n",
334                libcfs_nid2str(cr.acr_nid), &peer_ip);
335
336         rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
337
338         lnet_ni_decref(ni);
339         return rc;
340 }
341
342 #ifdef HAVE_SK_DATA_READY_ONE_ARG
343 static void lnet_acceptor_ready(struct sock *sk)
344 #else
345 static void lnet_acceptor_ready(struct sock *sk, int len)
346 #endif
347 {
348         /* Ensure pta_odata has actually been set before calling it */
349         rmb();
350 #ifdef HAVE_SK_DATA_READY_ONE_ARG
351         lnet_acceptor_state.pta_odata(sk);
352 #else
353         lnet_acceptor_state.pta_odata(sk, 0);
354 #endif
355
356         atomic_set(&lnet_acceptor_state.pta_ready, 1);
357         wake_up(&lnet_acceptor_state.pta_waitq);
358 }
359
360 static int
361 lnet_acceptor(void *arg)
362 {
363         struct socket  *newsock;
364         int            rc;
365         __u32          magic;
366         __u32          peer_ip;
367         int            peer_port;
368         int            secure = (int)((uintptr_t)arg);
369
370         LASSERT(lnet_acceptor_state.pta_sock == NULL);
371
372         rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
373                               0, accept_port, accept_backlog,
374                               lnet_acceptor_state.pta_ns);
375         if (rc != 0) {
376                 if (rc == -EADDRINUSE)
377                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
378                                            " %d: port already in use\n",
379                                            accept_port);
380                 else
381                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
382                                            "%d: unexpected error %d\n",
383                                            accept_port, rc);
384
385                 lnet_acceptor_state.pta_sock = NULL;
386         } else {
387                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
388                 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
389                 lnet_acceptor_state.pta_odata =
390                         lnet_acceptor_state.pta_sock->sk->sk_data_ready;
391                 /* ensure pta_odata gets set before there is any chance of
392                  * lnet_accept_ready() trying to read it.
393                  */
394                 wmb();
395                 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
396                         lnet_acceptor_ready;
397                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
398         }
399
400         /* set init status and unblock parent */
401         lnet_acceptor_state.pta_shutdown = rc;
402         complete(&lnet_acceptor_state.pta_signal);
403
404         if (rc != 0)
405                 return rc;
406
407         while (!lnet_acceptor_state.pta_shutdown) {
408
409                 wait_event_idle(lnet_acceptor_state.pta_waitq,
410                                 lnet_acceptor_state.pta_shutdown ||
411                                 atomic_read(&lnet_acceptor_state.pta_ready));
412                 if (!atomic_read(&lnet_acceptor_state.pta_ready))
413                         continue;
414                 atomic_set(&lnet_acceptor_state.pta_ready, 0);
415                 rc = kernel_accept(lnet_acceptor_state.pta_sock, &newsock,
416                                    SOCK_NONBLOCK);
417                 if (rc != 0) {
418                         if (rc != -EAGAIN) {
419                                 CWARN("Accept error %d: pausing...\n", rc);
420                                 schedule_timeout_uninterruptible(
421                                         cfs_time_seconds(1));
422                         }
423                         continue;
424                 }
425
426                 /* make sure we call lnet_sock_accept() again, until it fails */
427                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
428
429                 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
430                 if (rc != 0) {
431                         CERROR("Can't determine new connection's address\n");
432                         goto failed;
433                 }
434
435                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
436                         CERROR("Refusing connection from %pI4h: "
437                                "insecure port %d\n", &peer_ip, peer_port);
438                         goto failed;
439                 }
440
441                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
442                                       accept_timeout);
443                 if (rc != 0) {
444                         CERROR("Error %d reading connection request from "
445                                "%pI4h\n", rc, &peer_ip);
446                         goto failed;
447                 }
448
449                 rc = lnet_accept(newsock, magic);
450                 if (rc != 0)
451                         goto failed;
452
453                 continue;
454
455 failed:
456                 sock_release(newsock);
457         }
458
459         lnet_acceptor_state.pta_sock->sk->sk_data_ready =
460                 lnet_acceptor_state.pta_odata;
461         sock_release(lnet_acceptor_state.pta_sock);
462         lnet_acceptor_state.pta_sock = NULL;
463
464         CDEBUG(D_NET, "Acceptor stopping\n");
465
466         /* unblock lnet_acceptor_stop() */
467         complete(&lnet_acceptor_state.pta_signal);
468         return 0;
469 }
470
471 static inline int
472 accept2secure(const char *acc, long *sec)
473 {
474         if (!strcmp(acc, "secure")) {
475                 *sec = 1;
476                 return 1;
477         } else if (!strcmp(acc, "all")) {
478                 *sec = 0;
479                 return 1;
480         } else if (!strcmp(acc, "none")) {
481                 return 0;
482         } else {
483                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
484                                    acc);
485                 return -EINVAL;
486         }
487 }
488
489 int
490 lnet_acceptor_start(void)
491 {
492         struct task_struct *task;
493         int  rc;
494         long rc2;
495         long secure;
496
497         /* if acceptor is already running return immediately */
498         if (!lnet_acceptor_state.pta_shutdown)
499                 return 0;
500
501         LASSERT(lnet_acceptor_state.pta_sock == NULL);
502
503         rc = lnet_acceptor_get_tunables();
504         if (rc != 0)
505                 return rc;
506
507         init_completion(&lnet_acceptor_state.pta_signal);
508         rc = accept2secure(accept_type, &secure);
509         if (rc <= 0)
510                 return rc;
511
512         if (lnet_count_acceptor_nets() == 0)  /* not required */
513                 return 0;
514         if (current->nsproxy && current->nsproxy->net_ns)
515                 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
516         else
517                 lnet_acceptor_state.pta_ns = &init_net;
518         task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
519                            "acceptor_%03ld", secure);
520         if (IS_ERR(task)) {
521                 rc2 = PTR_ERR(task);
522                 CERROR("Can't start acceptor thread: %ld\n", rc2);
523                 return -ESRCH;
524         }
525
526         /* wait for acceptor to startup */
527         wait_for_completion(&lnet_acceptor_state.pta_signal);
528
529         if (!lnet_acceptor_state.pta_shutdown) {
530                 /* started OK */
531                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
532                 return 0;
533         }
534
535         LASSERT(lnet_acceptor_state.pta_sock == NULL);
536
537         return -ENETDOWN;
538 }
539
540 void
541 lnet_acceptor_stop(void)
542 {
543         if (lnet_acceptor_state.pta_shutdown) /* not running */
544                 return;
545
546         lnet_acceptor_state.pta_shutdown = 1;
547         wake_up(&lnet_acceptor_state.pta_waitq);
548
549         /* block until acceptor signals exit */
550         wait_for_completion(&lnet_acceptor_state.pta_signal);
551 }