Whamcloud - gitweb
LU-6002 lnet: startup acceptor thread dynamically
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
39
40 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
41
42 static int   accept_port    = 988;
43 static int   accept_backlog = 127;
44 static int   accept_timeout = 5;
45
46 static struct {
47         int                     pta_shutdown;
48         cfs_socket_t            *pta_sock;
49         struct completion       pta_signal;
50 } lnet_acceptor_state = {
51         .pta_shutdown = 1
52 };
53
54 int
55 lnet_acceptor_port(void)
56 {
57         return accept_port;
58 }
59
60 static inline int
61 lnet_accept_magic(__u32 magic, __u32 constant)
62 {
63         return (magic == constant ||
64                 magic == __swab32(constant));
65 }
66
67 #ifdef __KERNEL__
68
69 EXPORT_SYMBOL(lnet_acceptor_port);
70
71 static char *accept = "secure";
72
73 CFS_MODULE_PARM(accept, "s", charp, 0444,
74                 "Accept connections (secure|all|none)");
75 CFS_MODULE_PARM(accept_port, "i", int, 0444,
76                 "Acceptor's port (same on all nodes)");
77 CFS_MODULE_PARM(accept_backlog, "i", int, 0444,
78                 "Acceptor's listen backlog");
79 CFS_MODULE_PARM(accept_timeout, "i", int, 0644,
80                 "Acceptor's timeout (seconds)");
81
82 static char *accept_type = NULL;
83
84 static int
85 lnet_acceptor_get_tunables(void)
86 {
87         /* Userland acceptor uses 'accept_type' instead of 'accept', due to
88          * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
89          * for compatibility. Hence the trick. */
90         accept_type = accept;
91         return 0;
92 }
93
94 int
95 lnet_acceptor_timeout(void)
96 {
97         return accept_timeout;
98 }
99 EXPORT_SYMBOL(lnet_acceptor_timeout);
100
101 void
102 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
103                            __u32 peer_ip, int peer_port)
104 {
105         switch (rc) {
106         /* "normal" errors */
107         case -ECONNREFUSED:
108                 CNETERR("Connection to %s at host %u.%u.%u.%u on port %d was "
109                         "refused: check that Lustre is running on that node.\n",
110                         libcfs_nid2str(peer_nid),
111                         HIPQUAD(peer_ip), peer_port);
112                 break;
113         case -EHOSTUNREACH:
114         case -ENETUNREACH:
115                 CNETERR("Connection to %s at host %u.%u.%u.%u "
116                         "was unreachable: the network or that node may "
117                         "be down, or Lustre may be misconfigured.\n",
118                         libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
119                 break;
120         case -ETIMEDOUT:
121                 CNETERR("Connection to %s at host %u.%u.%u.%u on "
122                         "port %d took too long: that node may be hung "
123                         "or experiencing high load.\n",
124                         libcfs_nid2str(peer_nid),
125                         HIPQUAD(peer_ip), peer_port);
126                 break;
127         case -ECONNRESET:
128                 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
129                                    " on port %d was reset: "
130                                    "is it running a compatible version of "
131                                    "Lustre and is %s one of its NIDs?\n",
132                                    libcfs_nid2str(peer_nid),
133                                    HIPQUAD(peer_ip), peer_port,
134                                    libcfs_nid2str(peer_nid));
135                 break;
136         case -EPROTO:
137                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
138                                    "host %u.%u.%u.%u on port %d: is it running "
139                                    "a compatible version of Lustre?\n",
140                                    libcfs_nid2str(peer_nid),
141                                    HIPQUAD(peer_ip), peer_port);
142                 break;
143         case -EADDRINUSE:
144                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
145                                    "connect to %s at host %u.%u.%u.%u on port "
146                                    "%d\n", libcfs_nid2str(peer_nid),
147                                    HIPQUAD(peer_ip), peer_port);
148                 break;
149         default:
150                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
151                                    " at host %u.%u.%u.%u on port %d\n", rc,
152                                    libcfs_nid2str(peer_nid),
153                                    HIPQUAD(peer_ip), peer_port);
154                 break;
155         }
156 }
157 EXPORT_SYMBOL(lnet_connect_console_error);
158
159 int
160 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
161             __u32 local_ip, __u32 peer_ip, int peer_port)
162 {
163         lnet_acceptor_connreq_t cr;
164         cfs_socket_t           *sock;
165         int                     rc;
166         int                     port;
167         int                     fatal;
168
169         CLASSERT (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 = libcfs_sock_connect(&sock, &fatal,
177                                          local_ip, port,
178                                          peer_ip, peer_port);
179                 if (rc != 0) {
180                         if (fatal)
181                                 goto failed;
182                         continue;
183                 }
184
185                 CLASSERT (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 != 0) {
192                         /* single-shot proto check */
193                         lnet_net_lock(LNET_LOCK_EX);
194                         if ((the_lnet.ln_testprotocompat & 4) != 0) {
195                                 cr.acr_version++;
196                                 the_lnet.ln_testprotocompat &= ~4;
197                         }
198                         if ((the_lnet.ln_testprotocompat & 8) != 0) {
199                                 cr.acr_magic = LNET_PROTO_MAGIC;
200                                 the_lnet.ln_testprotocompat &= ~8;
201                         }
202                         lnet_net_unlock(LNET_LOCK_EX);
203                 }
204
205                 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
206                                        accept_timeout);
207                 if (rc != 0)
208                         goto failed_sock;
209
210                 *sockp = sock;
211                 return 0;
212         }
213
214         rc = -EADDRINUSE;
215         goto failed;
216
217  failed_sock:
218         libcfs_sock_release(sock);
219  failed:
220         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
221         return rc;
222 }
223 EXPORT_SYMBOL(lnet_connect);
224
225 #else /* below is multi-threaded user-space code */
226
227 static char *accept_type = "secure";
228
229 int
230 lnet_acceptor_get_tunables()
231 {
232         int   rc;
233         char *env = getenv("LNET_ACCEPT");
234
235         if (env != NULL)
236                 accept_type = env;
237
238         rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT");
239
240         if (rc != 0)
241                 return rc;
242
243         rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG");
244
245         if (rc != 0)
246                 return rc;
247
248         rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT");
249
250         if (rc != 0)
251                 return rc;
252
253         CDEBUG(D_NET, "accept_type     = %s\n", accept_type);
254         CDEBUG(D_NET, "accept_port     = %d\n", accept_port);
255         CDEBUG(D_NET, "accept_backlog  = %d\n", accept_backlog);
256         CDEBUG(D_NET, "accept_timeout  = %d\n", accept_timeout);
257         return 0;
258 }
259
260 #endif /* __KERNEL__ */
261
262 /* Below is the code common for both kernel and MT user-space */
263
264 static int
265 lnet_accept(cfs_socket_t *sock, __u32 magic)
266 {
267         lnet_acceptor_connreq_t cr;
268         __u32                   peer_ip;
269         int                     peer_port;
270         int                     rc;
271         int                     flip;
272         lnet_ni_t              *ni;
273         char                   *str;
274
275         LASSERT (sizeof(cr) <= 16);             /* not too big for the stack */
276
277         rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
278         LASSERT (rc == 0);                      /* we succeeded before */
279
280         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
281
282                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
283                         /* future version compatibility!
284                          * When LNET unifies protocols over all LNDs, the first
285                          * thing sent will be a version query.  I send back
286                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
287
288                         memset (&cr, 0, sizeof(cr));
289                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
290                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
291                         rc = libcfs_sock_write(sock, &cr, sizeof(cr),
292                                                accept_timeout);
293
294                         if (rc != 0)
295                                 CERROR("Error sending magic+version in response"
296                                        "to LNET magic from %u.%u.%u.%u: %d\n",
297                                        HIPQUAD(peer_ip), rc);
298                         return -EPROTO;
299                 }
300
301                 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
302                         str = "'old' socknal/tcpnal";
303                 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
304                         str = "'old' ranal";
305                 else
306                         str = "unrecognised";
307
308                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
309                                    " magic %08x: %s acceptor protocol\n",
310                                    HIPQUAD(peer_ip), magic, str);
311                 return -EPROTO;
312         }
313
314         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
315
316         rc = libcfs_sock_read(sock, &cr.acr_version,
317                               sizeof(cr.acr_version),
318                               accept_timeout);
319         if (rc != 0) {
320                 CERROR("Error %d reading connection request version from "
321                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
322                 return -EIO;
323         }
324
325         if (flip)
326                 __swab32s(&cr.acr_version);
327
328         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
329                 /* future version compatibility!
330                  * An acceptor-specific protocol rev will first send a version
331                  * query.  I send back my current version to tell her I'm
332                  * "old". */
333                 int peer_version = cr.acr_version;
334
335                 memset (&cr, 0, sizeof(cr));
336                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
337                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
338
339                 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
340                                        accept_timeout);
341
342                 if (rc != 0)
343                         CERROR("Error sending magic+version in response"
344                                "to version %d from %u.%u.%u.%u: %d\n",
345                                peer_version, HIPQUAD(peer_ip), rc);
346                 return -EPROTO;
347         }
348
349         rc = libcfs_sock_read(sock, &cr.acr_nid,
350                               sizeof(cr) -
351                               offsetof(lnet_acceptor_connreq_t, acr_nid),
352                               accept_timeout);
353         if (rc != 0) {
354                 CERROR("Error %d reading connection request from "
355                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
356                 return -EIO;
357         }
358
359         if (flip)
360                 __swab64s(&cr.acr_nid);
361
362         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
363         if (ni == NULL ||               /* no matching net */
364             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
365                 if (ni != NULL)
366                         lnet_ni_decref(ni);
367                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
368                                    " for %s: No matching NI\n",
369                                    HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
370                 return -EPERM;
371         }
372
373         if (ni->ni_lnd->lnd_accept == NULL) {
374                 /* This catches a request for the loopback LND */
375                 lnet_ni_decref(ni);
376                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
377                                   " for %s: NI doesn not accept IP connections\n",
378                                   HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
379                 return -EPERM;
380         }
381
382         CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
383                libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
384
385         rc = ni->ni_lnd->lnd_accept(ni, sock);
386
387         lnet_ni_decref(ni);
388         return rc;
389 }
390
391 static int
392 lnet_acceptor(void *arg)
393 {
394         cfs_socket_t  *newsock;
395         int            rc;
396         __u32          magic;
397         __u32          peer_ip;
398         int            peer_port;
399         int            secure = (int)((long_ptr_t)arg);
400
401         LASSERT (lnet_acceptor_state.pta_sock == NULL);
402
403         cfs_block_allsigs();
404
405         rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
406                                 0, accept_port, accept_backlog);
407         if (rc != 0) {
408                 if (rc == -EADDRINUSE)
409                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
410                                            " %d: port already in use\n",
411                                            accept_port);
412                 else
413                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
414                                            "%d: unexpected error %d\n",
415                                            accept_port, rc);
416
417                 lnet_acceptor_state.pta_sock = NULL;
418         } else {
419                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
420         }
421
422         /* set init status and unblock parent */
423         lnet_acceptor_state.pta_shutdown = rc;
424         complete(&lnet_acceptor_state.pta_signal);
425
426         if (rc != 0)
427                 return rc;
428
429         while (!lnet_acceptor_state.pta_shutdown) {
430
431                 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
432                 if (rc != 0) {
433                         if (rc != -EAGAIN) {
434                                 CWARN("Accept error %d: pausing...\n", rc);
435                                 cfs_pause(cfs_time_seconds(1));
436                         }
437                         continue;
438                 }
439
440                 /* maybe we're waken up with libcfs_sock_abort_accept() */
441                 if (lnet_acceptor_state.pta_shutdown) {
442                         libcfs_sock_release(newsock);
443                         break;
444                 }
445
446                 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
447                 if (rc != 0) {
448                         CERROR("Can't determine new connection's address\n");
449                         goto failed;
450                 }
451
452                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
453                         CERROR("Refusing connection from %u.%u.%u.%u: "
454                                "insecure port %d\n",
455                                HIPQUAD(peer_ip), peer_port);
456                         goto failed;
457                 }
458
459                 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
460                                       accept_timeout);
461                 if (rc != 0) {
462                         CERROR("Error %d reading connection request from "
463                                "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
464                         goto failed;
465                 }
466
467                 rc = lnet_accept(newsock, magic);
468                 if (rc != 0)
469                         goto failed;
470
471                 continue;
472
473         failed:
474                 libcfs_sock_release(newsock);
475         }
476
477         libcfs_sock_release(lnet_acceptor_state.pta_sock);
478         lnet_acceptor_state.pta_sock = NULL;
479
480         CDEBUG(D_NET, "Acceptor stopping\n");
481
482         /* unblock lnet_acceptor_stop() */
483         complete(&lnet_acceptor_state.pta_signal);
484         return 0;
485 }
486
487 static inline int
488 accept2secure(const char *acc, long *sec)
489 {
490         if (!strcmp(acc, "secure")) {
491                 *sec = 1;
492                 return 1;
493         } else if (!strcmp(acc, "all")) {
494                 *sec = 0;
495                 return 1;
496         } else if (!strcmp(acc, "none")) {
497                 return 0;
498         } else {
499                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
500                                    acc);
501                 return -EINVAL;
502         }
503 }
504
505 int
506 lnet_acceptor_start(void)
507 {
508         struct task_struct *task;
509         int  rc;
510         long rc2;
511         long secure;
512
513         /* if acceptor is already running return immediately */
514         if (!lnet_acceptor_state.pta_shutdown)
515                 return 0;
516
517         LASSERT (lnet_acceptor_state.pta_sock == NULL);
518
519         rc = lnet_acceptor_get_tunables();
520         if (rc != 0)
521                 return rc;
522
523 #ifndef __KERNEL__
524         /* Do nothing if we're liblustre clients */
525         if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
526                 return 0;
527 #endif
528
529         init_completion(&lnet_acceptor_state.pta_signal);
530         rc = accept2secure(accept_type, &secure);
531         if (rc <= 0) {
532                 fini_completion(&lnet_acceptor_state.pta_signal);
533                 return rc;
534         }
535
536         if (lnet_count_acceptor_nis() == 0)  /* not required */
537                 return 0;
538
539         task = kthread_run(lnet_acceptor, (void *)(ulong_ptr_t)secure,
540                            "acceptor_%03ld", secure);
541         if (IS_ERR(task)) {
542                 rc2 = PTR_ERR(task);
543                 CERROR("Can't start acceptor thread: %ld\n", rc2);
544                 fini_completion(&lnet_acceptor_state.pta_signal);
545
546                 return -ESRCH;
547         }
548
549         /* wait for acceptor to startup */
550         wait_for_completion(&lnet_acceptor_state.pta_signal);
551
552         if (!lnet_acceptor_state.pta_shutdown) {
553                 /* started OK */
554                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
555                 return 0;
556         }
557
558         LASSERT(lnet_acceptor_state.pta_sock == NULL);
559         fini_completion(&lnet_acceptor_state.pta_signal);
560
561         return -ENETDOWN;
562 }
563
564 void
565 lnet_acceptor_stop(void)
566 {
567         if (lnet_acceptor_state.pta_shutdown) /* not running */
568                 return;
569
570         lnet_acceptor_state.pta_shutdown = 1;
571         libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
572
573         /* block until acceptor signals exit */
574         wait_for_completion(&lnet_acceptor_state.pta_signal);
575
576         fini_completion(&lnet_acceptor_state.pta_signal);
577 }
578
579 #else /* single-threaded user-space */
580 int
581 lnet_acceptor_start(void)
582 {
583         return 0;
584 }
585
586 void
587 lnet_acceptor_stop(void)
588 {
589 }
590 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */