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