Whamcloud - gitweb
LU-10391 lnet: remove 'fatal' arg from lnet_{sock_}connect
[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_type = "secure";
75
76 module_param_named(accept, accept_type, 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 int
86 lnet_acceptor_timeout(void)
87 {
88         return accept_timeout;
89 }
90 EXPORT_SYMBOL(lnet_acceptor_timeout);
91
92 void
93 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
94                            __u32 peer_ip, int peer_port)
95 {
96         switch (rc) {
97         /* "normal" errors */
98         case -ECONNREFUSED:
99                 CNETERR("Connection to %s at host %pI4h on port %d was "
100                         "refused: check that Lustre is running on that node.\n",
101                         libcfs_nid2str(peer_nid), &peer_ip, peer_port);
102                 break;
103         case -EHOSTUNREACH:
104         case -ENETUNREACH:
105                 CNETERR("Connection to %s at host %pI4h "
106                         "was unreachable: the network or that node may "
107                         "be down, or Lustre may be misconfigured.\n",
108                         libcfs_nid2str(peer_nid), &peer_ip);
109                 break;
110         case -ETIMEDOUT:
111                 CNETERR("Connection to %s at host %pI4h on "
112                         "port %d took too long: that node may be hung "
113                         "or experiencing high load.\n",
114                         libcfs_nid2str(peer_nid), &peer_ip, peer_port);
115                 break;
116         case -ECONNRESET:
117                 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
118                                    " on port %d was reset: "
119                                    "is it running a compatible version of "
120                                    "Lustre and is %s one of its NIDs?\n",
121                                    libcfs_nid2str(peer_nid), &peer_ip,
122                                    peer_port, libcfs_nid2str(peer_nid));
123                 break;
124         case -EPROTO:
125                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
126                                    "host %pI4h on port %d: is it running "
127                                    "a compatible version of Lustre?\n",
128                                    libcfs_nid2str(peer_nid), &peer_ip,
129                                    peer_port);
130                 break;
131         case -EADDRINUSE:
132                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
133                                    "connect to %s at host %pI4h on port "
134                                    "%d\n", libcfs_nid2str(peer_nid),
135                                    &peer_ip, peer_port);
136                 break;
137         default:
138                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
139                                    " at host %pI4h on port %d\n", rc,
140                                    libcfs_nid2str(peer_nid),
141                                    &peer_ip, peer_port);
142                 break;
143         }
144 }
145 EXPORT_SYMBOL(lnet_connect_console_error);
146
147 int
148 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
149             __u32 local_ip, __u32 peer_ip, int peer_port, struct net *ns)
150 {
151         struct lnet_acceptor_connreq cr;
152         struct socket           *sock;
153         int                     rc;
154         int                     port;
155
156         BUILD_BUG_ON(sizeof(cr) > 16); /* not too big to be on the stack */
157
158         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
159              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
160              --port) {
161                 /* Iterate through reserved ports. */
162
163                 rc = lnet_sock_connect(&sock,
164                                        local_ip, port,
165                                        peer_ip, peer_port, ns);
166                 if (rc) {
167                         if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL)
168                                 continue;
169                         goto failed;
170                 }
171
172                 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
173
174                 cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
175                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
176                 cr.acr_nid     = peer_nid;
177
178                 if (the_lnet.ln_testprotocompat) {
179                         /* single-shot proto check */
180                         if (test_and_clear_bit(2, &the_lnet.ln_testprotocompat))
181                                 cr.acr_version++;
182                         if (test_and_clear_bit(3, &the_lnet.ln_testprotocompat))
183                                 cr.acr_magic = LNET_PROTO_MAGIC;
184                 }
185
186                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
187                                        accept_timeout);
188                 if (rc != 0)
189                         goto failed_sock;
190
191                 *sockp = sock;
192                 return 0;
193         }
194
195         rc = -EADDRINUSE;
196         goto failed;
197
198 failed_sock:
199         sock_release(sock);
200 failed:
201         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
202         return rc;
203 }
204 EXPORT_SYMBOL(lnet_connect);
205
206 static int
207 lnet_accept(struct socket *sock, __u32 magic)
208 {
209         struct lnet_acceptor_connreq cr;
210         __u32                   peer_ip;
211         int                     peer_port;
212         int                     rc;
213         int                     flip;
214         struct lnet_ni *ni;
215         char                   *str;
216
217         LASSERT(sizeof(cr) <= 16);              /* not too big for the stack */
218
219         rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
220         LASSERT(rc == 0);                       /* we succeeded before */
221
222         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
223
224                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
225                         /* future version compatibility!
226                          * When LNET unifies protocols over all LNDs, the first
227                          * thing sent will be a version query.  I send back
228                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
229
230                         memset(&cr, 0, sizeof(cr));
231                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
232                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
233                         rc = lnet_sock_write(sock, &cr, sizeof(cr),
234                                                accept_timeout);
235
236                         if (rc != 0)
237                                 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
238                                        &peer_ip, rc);
239                         return -EPROTO;
240                 }
241
242                 if (lnet_accept_magic(magic, LNET_PROTO_TCP_MAGIC))
243                         str = "'old' socknal/tcpnal";
244                 else
245                         str = "unrecognised";
246
247                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
248                                    " magic %08x: %s acceptor protocol\n",
249                                    &peer_ip, magic, str);
250                 return -EPROTO;
251         }
252
253         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
254
255         rc = lnet_sock_read(sock, &cr.acr_version,
256                               sizeof(cr.acr_version),
257                               accept_timeout);
258         if (rc != 0) {
259                 CERROR("Error %d reading connection request version from "
260                        "%pI4h\n", rc, &peer_ip);
261                 return -EIO;
262         }
263
264         if (flip)
265                 __swab32s(&cr.acr_version);
266
267         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
268                 /* future version compatibility!
269                  * An acceptor-specific protocol rev will first send a version
270                  * query.  I send back my current version to tell her I'm
271                  * "old". */
272                 int peer_version = cr.acr_version;
273
274                 memset(&cr, 0, sizeof(cr));
275                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
276                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
277
278                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
279                                        accept_timeout);
280
281                 if (rc != 0)
282                         CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
283                                peer_version, &peer_ip, rc);
284                 return -EPROTO;
285         }
286
287         rc = lnet_sock_read(sock, &cr.acr_nid,
288                               sizeof(cr) -
289                               offsetof(struct lnet_acceptor_connreq, acr_nid),
290                               accept_timeout);
291         if (rc != 0) {
292                 CERROR("Error %d reading connection request from "
293                        "%pI4h\n", rc, &peer_ip);
294                 return -EIO;
295         }
296
297         if (flip)
298                 __swab64s(&cr.acr_nid);
299
300         ni = lnet_nid2ni_addref(cr.acr_nid);
301         if (ni == NULL ||               /* no matching net */
302             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
303                 if (ni != NULL)
304                         lnet_ni_decref(ni);
305                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
306                                    "for %s: No matching NI\n",
307                                    &peer_ip, libcfs_nid2str(cr.acr_nid));
308                 return -EPERM;
309         }
310
311         if (ni->ni_net->net_lnd->lnd_accept == NULL) {
312                 /* This catches a request for the loopback LND */
313                 lnet_ni_decref(ni);
314                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
315                                   "for %s: NI doesn not accept IP connections\n",
316                                   &peer_ip, libcfs_nid2str(cr.acr_nid));
317                 return -EPERM;
318         }
319
320         CDEBUG(D_NET, "Accept %s from %pI4h\n",
321                libcfs_nid2str(cr.acr_nid), &peer_ip);
322
323         rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
324
325         lnet_ni_decref(ni);
326         return rc;
327 }
328
329 #ifdef HAVE_SK_DATA_READY_ONE_ARG
330 static void lnet_acceptor_ready(struct sock *sk)
331 #else
332 static void lnet_acceptor_ready(struct sock *sk, int len)
333 #endif
334 {
335         /* Ensure pta_odata has actually been set before calling it */
336         rmb();
337 #ifdef HAVE_SK_DATA_READY_ONE_ARG
338         lnet_acceptor_state.pta_odata(sk);
339 #else
340         lnet_acceptor_state.pta_odata(sk, 0);
341 #endif
342
343         atomic_set(&lnet_acceptor_state.pta_ready, 1);
344         wake_up(&lnet_acceptor_state.pta_waitq);
345 }
346
347 static int
348 lnet_acceptor(void *arg)
349 {
350         struct socket  *newsock;
351         int            rc;
352         __u32          magic;
353         __u32          peer_ip;
354         int            peer_port;
355         int            secure = (int)((uintptr_t)arg);
356
357         LASSERT(lnet_acceptor_state.pta_sock == NULL);
358
359         rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
360                               0, accept_port, accept_backlog,
361                               lnet_acceptor_state.pta_ns);
362         if (rc != 0) {
363                 if (rc == -EADDRINUSE)
364                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
365                                            " %d: port already in use\n",
366                                            accept_port);
367                 else
368                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
369                                            "%d: unexpected error %d\n",
370                                            accept_port, rc);
371
372                 lnet_acceptor_state.pta_sock = NULL;
373         } else {
374                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
375                 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
376                 lnet_acceptor_state.pta_odata =
377                         lnet_acceptor_state.pta_sock->sk->sk_data_ready;
378                 /* ensure pta_odata gets set before there is any chance of
379                  * lnet_accept_ready() trying to read it.
380                  */
381                 wmb();
382                 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
383                         lnet_acceptor_ready;
384                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
385         }
386
387         /* set init status and unblock parent */
388         lnet_acceptor_state.pta_shutdown = rc;
389         complete(&lnet_acceptor_state.pta_signal);
390
391         if (rc != 0)
392                 return rc;
393
394         while (!lnet_acceptor_state.pta_shutdown) {
395
396                 wait_event_idle(lnet_acceptor_state.pta_waitq,
397                                 lnet_acceptor_state.pta_shutdown ||
398                                 atomic_read(&lnet_acceptor_state.pta_ready));
399                 if (!atomic_read(&lnet_acceptor_state.pta_ready))
400                         continue;
401                 atomic_set(&lnet_acceptor_state.pta_ready, 0);
402                 rc = kernel_accept(lnet_acceptor_state.pta_sock, &newsock,
403                                    SOCK_NONBLOCK);
404                 if (rc != 0) {
405                         if (rc != -EAGAIN) {
406                                 CWARN("Accept error %d: pausing...\n", rc);
407                                 schedule_timeout_uninterruptible(
408                                         cfs_time_seconds(1));
409                         }
410                         continue;
411                 }
412
413                 /* make sure we call lnet_sock_accept() again, until it fails */
414                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
415
416                 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
417                 if (rc != 0) {
418                         CERROR("Can't determine new connection's address\n");
419                         goto failed;
420                 }
421
422                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
423                         CERROR("Refusing connection from %pI4h: "
424                                "insecure port %d\n", &peer_ip, peer_port);
425                         goto failed;
426                 }
427
428                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
429                                       accept_timeout);
430                 if (rc != 0) {
431                         CERROR("Error %d reading connection request from "
432                                "%pI4h\n", rc, &peer_ip);
433                         goto failed;
434                 }
435
436                 rc = lnet_accept(newsock, magic);
437                 if (rc != 0)
438                         goto failed;
439
440                 continue;
441
442 failed:
443                 sock_release(newsock);
444         }
445
446         lnet_acceptor_state.pta_sock->sk->sk_data_ready =
447                 lnet_acceptor_state.pta_odata;
448         sock_release(lnet_acceptor_state.pta_sock);
449         lnet_acceptor_state.pta_sock = NULL;
450
451         CDEBUG(D_NET, "Acceptor stopping\n");
452
453         /* unblock lnet_acceptor_stop() */
454         complete(&lnet_acceptor_state.pta_signal);
455         return 0;
456 }
457
458 static inline int
459 accept2secure(const char *acc, long *sec)
460 {
461         if (!strcmp(acc, "secure")) {
462                 *sec = 1;
463                 return 1;
464         } else if (!strcmp(acc, "all")) {
465                 *sec = 0;
466                 return 1;
467         } else if (!strcmp(acc, "none")) {
468                 return 0;
469         } else {
470                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
471                                    acc);
472                 return -EINVAL;
473         }
474 }
475
476 int
477 lnet_acceptor_start(void)
478 {
479         struct task_struct *task;
480         int  rc;
481         long rc2;
482         long secure;
483
484         /* if acceptor is already running return immediately */
485         if (!lnet_acceptor_state.pta_shutdown)
486                 return 0;
487
488         LASSERT(lnet_acceptor_state.pta_sock == NULL);
489
490         init_completion(&lnet_acceptor_state.pta_signal);
491         rc = accept2secure(accept_type, &secure);
492         if (rc <= 0)
493                 return rc;
494
495         if (lnet_count_acceptor_nets() == 0)  /* not required */
496                 return 0;
497         if (current->nsproxy && current->nsproxy->net_ns)
498                 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
499         else
500                 lnet_acceptor_state.pta_ns = &init_net;
501         task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
502                            "acceptor_%03ld", secure);
503         if (IS_ERR(task)) {
504                 rc2 = PTR_ERR(task);
505                 CERROR("Can't start acceptor thread: %ld\n", rc2);
506                 return -ESRCH;
507         }
508
509         /* wait for acceptor to startup */
510         wait_for_completion(&lnet_acceptor_state.pta_signal);
511
512         if (!lnet_acceptor_state.pta_shutdown) {
513                 /* started OK */
514                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
515                 return 0;
516         }
517
518         LASSERT(lnet_acceptor_state.pta_sock == NULL);
519
520         return -ENETDOWN;
521 }
522
523 void
524 lnet_acceptor_stop(void)
525 {
526         if (lnet_acceptor_state.pta_shutdown) /* not running */
527                 return;
528
529         lnet_acceptor_state.pta_shutdown = 1;
530         wake_up(&lnet_acceptor_state.pta_waitq);
531
532         /* block until acceptor signals exit */
533         wait_for_completion(&lnet_acceptor_state.pta_signal);
534 }