Whamcloud - gitweb
LU-10391 lnet: use data_ready callback to trigger accept()
[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         cfs_block_allsigs();
373
374         rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
375                               0, accept_port, accept_backlog,
376                               lnet_acceptor_state.pta_ns);
377         if (rc != 0) {
378                 if (rc == -EADDRINUSE)
379                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
380                                            " %d: port already in use\n",
381                                            accept_port);
382                 else
383                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
384                                            "%d: unexpected error %d\n",
385                                            accept_port, rc);
386
387                 lnet_acceptor_state.pta_sock = NULL;
388         } else {
389                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
390                 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
391                 lnet_acceptor_state.pta_odata =
392                         lnet_acceptor_state.pta_sock->sk->sk_data_ready;
393                 /* ensure pta_odata gets set before there is any chance of
394                  * lnet_accept_ready() trying to read it.
395                  */
396                 wmb();
397                 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
398                         lnet_acceptor_ready;
399                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
400         }
401
402         /* set init status and unblock parent */
403         lnet_acceptor_state.pta_shutdown = rc;
404         complete(&lnet_acceptor_state.pta_signal);
405
406         if (rc != 0)
407                 return rc;
408
409         while (!lnet_acceptor_state.pta_shutdown) {
410
411                 wait_event_idle(lnet_acceptor_state.pta_waitq,
412                                 lnet_acceptor_state.pta_shutdown ||
413                                 atomic_read(&lnet_acceptor_state.pta_ready));
414                 if (!atomic_read(&lnet_acceptor_state.pta_ready))
415                         continue;
416                 atomic_set(&lnet_acceptor_state.pta_ready, 0);
417                 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
418                 if (rc != 0) {
419                         if (rc != -EAGAIN) {
420                                 CWARN("Accept error %d: pausing...\n", rc);
421                                 schedule_timeout_uninterruptible(
422                                         cfs_time_seconds(1));
423                         }
424                         continue;
425                 }
426
427                 /* make sure we call lnet_sock_accept() again, until it fails */
428                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
429
430                 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
431                 if (rc != 0) {
432                         CERROR("Can't determine new connection's address\n");
433                         goto failed;
434                 }
435
436                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
437                         CERROR("Refusing connection from %pI4h: "
438                                "insecure port %d\n", &peer_ip, peer_port);
439                         goto failed;
440                 }
441
442                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
443                                       accept_timeout);
444                 if (rc != 0) {
445                         CERROR("Error %d reading connection request from "
446                                "%pI4h\n", rc, &peer_ip);
447                         goto failed;
448                 }
449
450                 rc = lnet_accept(newsock, magic);
451                 if (rc != 0)
452                         goto failed;
453
454                 continue;
455
456 failed:
457                 sock_release(newsock);
458         }
459
460         lnet_acceptor_state.pta_sock->sk->sk_data_ready =
461                 lnet_acceptor_state.pta_odata;
462         sock_release(lnet_acceptor_state.pta_sock);
463         lnet_acceptor_state.pta_sock = NULL;
464
465         CDEBUG(D_NET, "Acceptor stopping\n");
466
467         /* unblock lnet_acceptor_stop() */
468         complete(&lnet_acceptor_state.pta_signal);
469         return 0;
470 }
471
472 static inline int
473 accept2secure(const char *acc, long *sec)
474 {
475         if (!strcmp(acc, "secure")) {
476                 *sec = 1;
477                 return 1;
478         } else if (!strcmp(acc, "all")) {
479                 *sec = 0;
480                 return 1;
481         } else if (!strcmp(acc, "none")) {
482                 return 0;
483         } else {
484                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
485                                    acc);
486                 return -EINVAL;
487         }
488 }
489
490 int
491 lnet_acceptor_start(void)
492 {
493         struct task_struct *task;
494         int  rc;
495         long rc2;
496         long secure;
497
498         /* if acceptor is already running return immediately */
499         if (!lnet_acceptor_state.pta_shutdown)
500                 return 0;
501
502         LASSERT(lnet_acceptor_state.pta_sock == NULL);
503
504         rc = lnet_acceptor_get_tunables();
505         if (rc != 0)
506                 return rc;
507
508         init_completion(&lnet_acceptor_state.pta_signal);
509         rc = accept2secure(accept_type, &secure);
510         if (rc <= 0)
511                 return rc;
512
513         if (lnet_count_acceptor_nets() == 0)  /* not required */
514                 return 0;
515         if (current->nsproxy && current->nsproxy->net_ns)
516                 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
517         else
518                 lnet_acceptor_state.pta_ns = &init_net;
519         task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
520                            "acceptor_%03ld", secure);
521         if (IS_ERR(task)) {
522                 rc2 = PTR_ERR(task);
523                 CERROR("Can't start acceptor thread: %ld\n", rc2);
524                 return -ESRCH;
525         }
526
527         /* wait for acceptor to startup */
528         wait_for_completion(&lnet_acceptor_state.pta_signal);
529
530         if (!lnet_acceptor_state.pta_shutdown) {
531                 /* started OK */
532                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
533                 return 0;
534         }
535
536         LASSERT(lnet_acceptor_state.pta_sock == NULL);
537
538         return -ENETDOWN;
539 }
540
541 void
542 lnet_acceptor_stop(void)
543 {
544         if (lnet_acceptor_state.pta_shutdown) /* not running */
545                 return;
546
547         lnet_acceptor_state.pta_shutdown = 1;
548         wake_up(&lnet_acceptor_state.pta_waitq);
549
550         /* block until acceptor signals exit */
551         wait_for_completion(&lnet_acceptor_state.pta_signal);
552 }