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