Whamcloud - gitweb
Branch HEAD
[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 #ifdef __KERNEL__
41 static char *accept = "secure";
42 CFS_MODULE_PARM(accept, "s", charp, 0444,
43                 "Accept connections (secure|all|none)");
44
45 static int accept_port = 988;
46 CFS_MODULE_PARM(accept_port, "i", int, 0444,
47                 "Acceptor's port (same on all nodes)");
48
49 static int accept_backlog = 127;
50 CFS_MODULE_PARM(accept_backlog, "i", int, 0444,
51                 "Acceptor's listen backlog");
52
53 static int accept_timeout = 5;
54 CFS_MODULE_PARM(accept_timeout, "i", int, 0644,
55                 "Acceptor's timeout (seconds)");
56
57 struct {
58         int               pta_shutdown;
59         cfs_socket_t     *pta_sock;
60         struct semaphore  pta_signal;
61 } lnet_acceptor_state;
62
63 int
64 lnet_acceptor_timeout(void)
65 {
66         return accept_timeout;
67 }
68 EXPORT_SYMBOL(lnet_acceptor_timeout);
69
70 int
71 lnet_acceptor_port(void)
72 {
73         return accept_port;
74 }
75 EXPORT_SYMBOL(lnet_acceptor_port);
76
77 void
78 lnet_connect_console_error (int rc, lnet_nid_t peer_nid, 
79                            __u32 peer_ip, int peer_port)
80 {
81         switch (rc) {
82         /* "normal" errors */
83         case -ECONNREFUSED:
84                 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u "
85                        "on port %d was refused: "
86                        "check that Lustre is running on that node.\n",
87                        libcfs_nid2str(peer_nid),
88                        HIPQUAD(peer_ip), peer_port);
89                 break;
90         case -EHOSTUNREACH:
91         case -ENETUNREACH:
92                 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u "
93                        "was unreachable: the network or that node may "
94                        "be down, or Lustre may be misconfigured.\n",
95                        libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
96                 break;
97         case -ETIMEDOUT:
98                 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u on "
99                        "port %d took too long: that node may be hung "
100                        "or experiencing high load.\n",
101                        libcfs_nid2str(peer_nid),
102                        HIPQUAD(peer_ip), peer_port);
103                 break;
104         case -ECONNRESET:
105                 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
106                                    " on port %d was reset: "
107                                    "is it running a compatible version of "
108                                    "Lustre and is %s one of its NIDs?\n",
109                                    libcfs_nid2str(peer_nid),
110                                    HIPQUAD(peer_ip), peer_port,
111                                    libcfs_nid2str(peer_nid));
112                 break;
113         case -EPROTO:
114                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
115                                    "host %u.%u.%u.%u on port %d: is it running "
116                                    "a compatible version of Lustre?\n",
117                                    libcfs_nid2str(peer_nid),
118                                    HIPQUAD(peer_ip), peer_port);
119                 break;
120         case -EADDRINUSE:
121                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
122                                    "connect to %s at host %u.%u.%u.%u on port "
123                                    "%d\n", libcfs_nid2str(peer_nid),
124                                    HIPQUAD(peer_ip), peer_port);
125                 break;
126         default:
127                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
128                                    " at host %u.%u.%u.%u on port %d\n", rc,
129                                    libcfs_nid2str(peer_nid),
130                                    HIPQUAD(peer_ip), peer_port);
131                 break;
132         }
133 }
134 EXPORT_SYMBOL(lnet_connect_console_error);
135
136 int
137 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
138             __u32 local_ip, __u32 peer_ip, int peer_port)
139 {
140         lnet_acceptor_connreq_t cr;
141         cfs_socket_t           *sock;
142         int                     rc;
143         int                     port;
144         int                     fatal;
145
146         CLASSERT (sizeof(cr) <= 16);            /* not too big to be on the stack */
147
148         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT; 
149              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT; 
150              --port) {
151                 /* Iterate through reserved ports. */
152
153                 rc = libcfs_sock_connect(&sock, &fatal, 
154                                          local_ip, port, 
155                                          peer_ip, peer_port);
156                 if (rc != 0) {
157                         if (fatal)
158                                 goto failed;
159                         continue;
160                 }
161
162                 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
163
164                 cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
165                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
166                 cr.acr_nid     = peer_nid;
167
168                 if (the_lnet.ln_testprotocompat != 0) {
169                         /* single-shot proto check */
170                         LNET_LOCK();
171                         if ((the_lnet.ln_testprotocompat & 4) != 0) {
172                                 cr.acr_version++;
173                                 the_lnet.ln_testprotocompat &= ~4;
174                         }
175                         if ((the_lnet.ln_testprotocompat & 8) != 0) {
176                                 cr.acr_magic = LNET_PROTO_MAGIC;
177                                 the_lnet.ln_testprotocompat &= ~8;
178                         }
179                         LNET_UNLOCK();
180                 }
181
182                 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
183                                        accept_timeout);
184                 if (rc != 0)
185                         goto failed_sock;
186                 
187                 *sockp = sock;
188                 return 0;
189         }
190
191         rc = -EADDRINUSE;
192         goto failed;
193         
194  failed_sock:
195         libcfs_sock_release(sock);
196  failed:
197         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
198         return rc;
199 }
200 EXPORT_SYMBOL(lnet_connect);
201
202 static inline int
203 lnet_accept_magic(__u32 magic, __u32 constant)
204 {
205         return (magic == constant ||
206                 magic == __swab32(constant));
207 }
208
209 int
210 lnet_accept(cfs_socket_t *sock, __u32 magic)
211 {
212         lnet_acceptor_connreq_t cr;
213         __u32                   peer_ip;
214         int                     peer_port;
215         int                     rc;
216         int                     flip;
217         lnet_ni_t              *ni;
218         char                   *str;
219
220         LASSERT (sizeof(cr) <= 16);             /* not too big for the stack */
221         
222         rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
223         LASSERT (rc == 0);                      /* we succeeded before */
224
225         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
226
227                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
228                         /* future version compatibility!
229                          * When LNET unifies protocols over all LNDs, the first
230                          * thing sent will be a version query.  I send back
231                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
232
233                         memset (&cr, 0, sizeof(cr));
234                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
235                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
236                         rc = libcfs_sock_write(sock, &cr, sizeof(cr),
237                                                accept_timeout);
238
239                         if (rc != 0)
240                                 CERROR("Error sending magic+version in response"
241                                        "to LNET magic from %u.%u.%u.%u: %d\n",
242                                        HIPQUAD(peer_ip), rc);
243                         return -EPROTO;
244                 }
245
246                 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
247                         str = "'old' socknal/tcpnal";
248                 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
249                         str = "'old' ranal";
250                 else if (lnet_accept_magic(magic, LNET_PROTO_OPENIB_MAGIC))
251                         str = "'old' openibnal";
252                 else
253                         str = "unrecognised";
254             
255                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
256                                    " magic %08x: %s acceptor protocol\n",
257                                    HIPQUAD(peer_ip), magic, str);
258                 return -EPROTO;
259         }
260
261         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
262
263         rc = libcfs_sock_read(sock, &cr.acr_version, 
264                               sizeof(cr.acr_version),
265                               accept_timeout);
266         if (rc != 0) {
267                 CERROR("Error %d reading connection request version from "
268                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
269                 return -EIO;
270         }
271
272         if (flip)
273                 __swab32s(&cr.acr_version);
274         
275         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
276                 /* future version compatibility!
277                  * An acceptor-specific protocol rev will first send a version
278                  * query.  I send back my current version to tell her I'm
279                  * "old". */
280                 int peer_version = cr.acr_version;
281
282                 memset (&cr, 0, sizeof(cr));
283                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
284                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
285
286                 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
287                                        accept_timeout);
288
289                 if (rc != 0)
290                         CERROR("Error sending magic+version in response"
291                                "to version %d from %u.%u.%u.%u: %d\n",
292                                peer_version, HIPQUAD(peer_ip), rc);
293                 return -EPROTO;
294         }
295
296         rc = libcfs_sock_read(sock, &cr.acr_nid,
297                               sizeof(cr) -
298                               offsetof(lnet_acceptor_connreq_t, acr_nid),
299                               accept_timeout);
300         if (rc != 0) {
301                 CERROR("Error %d reading connection request from "
302                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
303                 return -EIO;
304         }
305
306         if (flip)
307                 __swab64s(&cr.acr_nid);
308
309         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
310         if (ni == NULL ||               /* no matching net */
311             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
312                 if (ni != NULL)
313                         lnet_ni_decref(ni);
314                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
315                                    " for %s: No matching NI\n",
316                                    HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
317                 return -EPERM;
318         }
319
320         if (ni->ni_lnd->lnd_accept == NULL) {
321                 /* This catches a request for the loopback LND */
322                 lnet_ni_decref(ni);
323                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
324                                   " for %s: NI doesn not accept IP connections\n",
325                                   HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
326                 return -EPERM;
327         }
328
329         CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
330                libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
331
332         rc = ni->ni_lnd->lnd_accept(ni, sock);
333         lnet_ni_decref(ni);
334         return rc;
335 }
336         
337 int
338 lnet_acceptor(void *arg)
339 {
340         char           name[16];
341         cfs_socket_t  *newsock;
342         int            rc;
343         __u32          magic;
344         __u32          peer_ip;
345         int            peer_port;
346         int            secure = (int)((long_ptr_t)arg);
347
348         LASSERT (lnet_acceptor_state.pta_sock == NULL);
349
350         snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
351         cfs_daemonize(name);
352         cfs_block_allsigs();
353
354         rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
355                                 0, accept_port, accept_backlog);
356         if (rc != 0) {
357                 if (rc == -EADDRINUSE)
358                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
359                                            " %d: port already in use\n",
360                                            accept_port);
361                 else
362                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
363                                            "%d: unexpected error %d\n",
364                                            accept_port, rc);
365
366                 lnet_acceptor_state.pta_sock = NULL;
367         } else {
368                 LCONSOLE(0, "Accept %s, port %d\n", accept, accept_port);
369         }
370         
371         /* set init status and unblock parent */
372         lnet_acceptor_state.pta_shutdown = rc;
373         mutex_up(&lnet_acceptor_state.pta_signal);
374         
375         if (rc != 0)
376                 return rc;
377
378         while (!lnet_acceptor_state.pta_shutdown) {
379                 
380                 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
381                 if (rc != 0) {
382                         if (rc != -EAGAIN) {
383                                 CWARN("Accept error %d: pausing...\n", rc);
384                                 cfs_pause(cfs_time_seconds(1));
385                         }
386                         continue;
387                 }
388
389                 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
390                 if (rc != 0) {
391                         CERROR("Can't determine new connection's address\n");
392                         goto failed;
393                 }
394
395                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
396                         CERROR("Refusing connection from %u.%u.%u.%u: "
397                                "insecure port %d\n",
398                                HIPQUAD(peer_ip), peer_port);
399                         goto failed;
400                 }
401
402                 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
403                                       accept_timeout);
404                 if (rc != 0) {
405                         CERROR("Error %d reading connection request from "
406                                "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
407                         goto failed;
408                 }
409
410                 rc = lnet_accept(newsock, magic);
411                 if (rc != 0)
412                         goto failed;
413                 
414                 continue;
415                 
416         failed:
417                 libcfs_sock_release(newsock);
418         }
419         
420         libcfs_sock_release(lnet_acceptor_state.pta_sock);
421         lnet_acceptor_state.pta_sock = NULL;
422
423         LCONSOLE(0, "Acceptor stopping\n");
424         
425         /* unblock lnet_acceptor_stop() */
426         mutex_up(&lnet_acceptor_state.pta_signal);
427         return 0;
428 }
429
430 int
431 lnet_acceptor_start(void)
432 {
433         long   pid;
434         long   secure;
435
436         LASSERT (lnet_acceptor_state.pta_sock == NULL);
437         init_mutex_locked(&lnet_acceptor_state.pta_signal);
438
439         if (!strcmp(accept, "secure")) {
440                 secure = 1;
441         } else if (!strcmp(accept, "all")) {
442                 secure = 0;
443         } else if (!strcmp(accept, "none")) {
444                 return 0;
445         } else {
446                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
447                                    accept);
448                 return -EINVAL;
449         }
450         
451         if (lnet_count_acceptor_nis() == 0)  /* not required */
452                 return 0;
453         
454         pid = cfs_kernel_thread(lnet_acceptor, (void *)(ulong_ptr_t)secure, 0);
455         if (pid < 0) {
456                 CERROR("Can't start acceptor thread: %ld\n", pid);
457                 return -ESRCH;
458         }
459
460         mutex_down(&lnet_acceptor_state.pta_signal); /* wait for acceptor to startup */
461
462         if (!lnet_acceptor_state.pta_shutdown) {
463                 /* started OK */
464                 LASSERT (lnet_acceptor_state.pta_sock != NULL);
465                 return 0;
466         }
467
468         LASSERT (lnet_acceptor_state.pta_sock == NULL);
469         return -ENETDOWN;
470 }
471
472 void
473 lnet_acceptor_stop(void)
474 {
475         if (lnet_acceptor_state.pta_sock == NULL) /* not running */
476                 return;
477         
478         lnet_acceptor_state.pta_shutdown = 1;
479         libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
480
481         /* block until acceptor signals exit */
482         mutex_down(&lnet_acceptor_state.pta_signal);
483 }
484
485 #else /* __KERNEL__ */
486 #ifdef HAVE_LIBPTHREAD
487
488 static char *accept_type;
489 static int accept_port = 988;
490 static int accept_backlog;
491 static int accept_timeout;
492
493 struct {
494         int                   pta_shutdown;
495         int                   pta_sock;
496         struct cfs_completion pta_completion;
497 } lnet_acceptor_state;
498
499 int
500 lnet_acceptor_port(void)
501 {
502         return accept_port;
503 }
504
505 int
506 lnet_parse_int_tunable(int *value, char *name, int dflt)
507 {
508         char    *env = getenv(name);
509         char    *end;
510
511         if (env == NULL) {
512                 *value = dflt;
513                 return 0;
514         }
515
516         *value = strtoull(env, &end, 0);
517         if (*end == 0)
518                 return 0;
519
520         CERROR("Can't parse tunable %s=%s\n", name, env);
521         return -EINVAL;
522 }
523
524 int
525 lnet_parse_string_tunable(char **value, char *name, char *dflt)
526 {
527         char    *env = getenv(name);
528
529         if (env == NULL)
530                 *value = dflt;             
531         else
532                 *value = env;
533
534         return 0;
535 }
536
537 int
538 lnet_acceptor_get_tunables()
539 {
540         int rc;
541         rc = lnet_parse_string_tunable(&accept_type, "LNET_ACCEPT", "secure");
542
543         if (rc != 0)
544                 return rc;
545
546         rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT", 988);
547
548         if (rc != 0)
549                 return rc;
550         
551         rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG", 127);
552
553         if (rc != 0)
554                 return rc;
555
556         rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT", 5);
557
558         if (rc != 0)
559                 return rc;
560
561         CDEBUG(D_NET, "accept_type     = %s\n", accept_type);
562         CDEBUG(D_NET, "accept_port     = %d\n", accept_port);
563         CDEBUG(D_NET, "accept_backlog  = %d\n", accept_backlog);
564         CDEBUG(D_NET, "accept_timeout  = %d\n", accept_timeout);
565         return 0;
566 }
567
568 static inline int
569 lnet_accept_magic(__u32 magic, __u32 constant)
570 {
571         return (magic == constant ||
572                 magic == __swab32(constant));
573 }
574
575 /* user-land lnet_accept() isn't used by any LND's directly. So, we don't
576  * do it visible outside acceptor.c and we can change its prototype
577  * freely */
578 static int
579 lnet_accept(int sock, __u32 magic, __u32 peer_ip, int peer_port)
580 {
581         int rc, flip;
582         lnet_acceptor_connreq_t cr;
583         lnet_ni_t *ni;
584         
585         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
586                 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u magic %08x: "
587                                "unsupported acceptor protocol\n",
588                                HIPQUAD(peer_ip), magic);
589                 return -EPROTO;
590         }
591
592         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
593         
594         rc = libcfs_sock_read(sock, &cr.acr_version, 
595                               sizeof(cr.acr_version),
596                               accept_timeout);
597         if (rc != 0) {
598                 CERROR("Error %d reading connection request version from "
599                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
600                 return -EIO;
601         }
602
603         if (flip)
604                 __swab32s(&cr.acr_version);
605
606         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION)
607                 return -EPROTO;
608                 
609         rc = libcfs_sock_read(sock, &cr.acr_nid,
610                               sizeof(cr) -
611                               offsetof(lnet_acceptor_connreq_t, acr_nid),
612                               accept_timeout);
613         if (rc != 0) {
614                 CERROR("Error %d reading connection request from "
615                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
616                 return -EIO;
617         }
618
619         if (flip)
620                 __swab64s(&cr.acr_nid);
621
622         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
623                        
624         if (ni == NULL ||                    /* no matching net */
625              ni->ni_nid != cr.acr_nid) {     /* right NET, wrong NID! */
626                 if (ni != NULL)
627                         lnet_ni_decref(ni);
628                 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u for %s: "
629                                " No matching NI\n",
630                                HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
631                 return -EPERM;
632         }
633
634         if (ni->ni_lnd->lnd_accept == NULL) {
635                 lnet_ni_decref(ni);
636                 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u for %s: "
637                                " NI doesn not accept IP connections\n",
638                                HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
639                 return -EPERM;
640         }
641         
642         CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
643                libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
644
645         rc = ni->ni_lnd->lnd_accept(ni, sock);
646         
647         lnet_ni_decref(ni);
648         return rc;
649 }
650
651 int
652 lnet_acceptor(void *arg)
653 {
654         char           name[16];
655         int            secure = (int)((unsigned long)arg);
656         int            rc;
657         int            newsock;
658         __u32          peer_ip;
659         int            peer_port;
660         __u32          magic;
661
662         snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
663         cfs_daemonize(name);
664         cfs_block_allsigs();
665
666         rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
667                                 0, accept_port, accept_backlog);
668         if (rc != 0) {
669                 if (rc == -EADDRINUSE)
670                         LCONSOLE_ERROR("Can't start acceptor on port %d: "
671                                        "port already in use\n",
672                                        accept_port);
673                 else
674                         LCONSOLE_ERROR("Can't start acceptor on port %d: "
675                                        "unexpected error %d\n",
676                                        accept_port, rc);
677
678         } else {
679                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
680         }
681         
682         /* set init status and unblock parent */
683         lnet_acceptor_state.pta_shutdown = rc;
684         cfs_complete(&lnet_acceptor_state.pta_completion);
685
686         if (rc != 0)
687                 return rc;
688
689         while (!lnet_acceptor_state.pta_shutdown) {
690
691                 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock,
692                                         &peer_ip, &peer_port);
693                 if (rc != 0)
694                         continue;
695
696                 /* maybe we're waken up with libcfs_sock_abort_accept() */
697                 if (lnet_acceptor_state.pta_shutdown) {
698                         libcfs_sock_release(newsock);
699                         break;
700                 }
701
702                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
703                         CERROR("Refusing connection from %u.%u.%u.%u: "
704                                "insecure port %d\n",
705                                HIPQUAD(peer_ip), peer_port);
706                         goto failed;
707                 }
708
709                 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
710                                       accept_timeout);
711                 if (rc != 0) {
712                         CERROR("Error %d reading connection request from "
713                                "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
714                         goto failed;
715                 }
716
717                 rc = lnet_accept(newsock, magic, peer_ip, peer_port);
718                 if (rc != 0)
719                         goto failed;
720                 
721                 continue;
722                 
723           failed:
724                 libcfs_sock_release(newsock);
725         }
726         
727         libcfs_sock_release(lnet_acceptor_state.pta_sock);
728         LCONSOLE(0,"Acceptor stopping\n");
729
730         /* unblock lnet_acceptor_stop() */
731         cfs_complete(&lnet_acceptor_state.pta_completion);        
732
733         return 0;
734 }
735
736 static int skip_waiting_for_completion;
737
738 int
739 lnet_acceptor_start(void)
740 {
741         long   secure;
742         int rc;
743
744         rc = lnet_acceptor_get_tunables();
745         if (rc != 0)
746                 return rc;
747
748         /* Do nothing if we're liblustre clients */
749         if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
750                 return 0;
751                         
752         cfs_init_completion(&lnet_acceptor_state.pta_completion);
753
754         if (!strcmp(accept_type, "secure")) {
755                 secure = 1;
756         } else if (!strcmp(accept_type, "all")) {
757                 secure = 0;
758         } else if (!strcmp(accept_type, "none")) {
759                 skip_waiting_for_completion = 1;
760                 return 0;
761         } else {
762                 LCONSOLE_ERROR ("Can't parse 'accept_type=\"%s\"'\n", accept_type);
763                 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
764                 return -EINVAL;
765         }
766
767         if (lnet_count_acceptor_nis() == 0) { /* not required */
768                 skip_waiting_for_completion = 1;
769                 return 0;
770         }
771
772         rc = cfs_create_thread(lnet_acceptor, (void *)secure);
773         if (rc != 0) {
774                 CERROR("Can't start acceptor thread: %d\n", rc);
775                 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
776                 return rc;
777         }
778
779         /* wait for acceptor to startup */
780         cfs_wait_for_completion(&lnet_acceptor_state.pta_completion);
781
782         if (!lnet_acceptor_state.pta_shutdown)
783                 return 0;
784         
785         cfs_fini_completion(&lnet_acceptor_state.pta_completion);
786         return -ENETDOWN;
787 }
788
789 void
790 lnet_acceptor_stop(void)
791 {
792         /* Do nothing if we're liblustre clients */
793         if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
794                 return;
795
796         if (!skip_waiting_for_completion) {
797                 lnet_acceptor_state.pta_shutdown = 1;
798                 libcfs_sock_abort_accept(accept_port);
799                 
800                 /* block until acceptor signals exit */
801                 cfs_wait_for_completion(&lnet_acceptor_state.pta_completion);
802         }
803         
804         cfs_fini_completion(&lnet_acceptor_state.pta_completion);
805 }
806 #else
807 int
808 lnet_acceptor_start(void)
809 {
810         return 0;
811 }
812
813 void
814 lnet_acceptor_stop(void)
815 {
816 }
817 #endif /* !HAVE_LIBPTHREAD */
818 #endif /* !__KERNEL__ */