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 [sun.com URL with a
20  * copy of GPLv2].
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                 if (the_lnet.ln_ptlcompat != 2) {
165                         /* When portals compatibility is "strong", simply
166                          * connect (i.e. send no acceptor connection request).
167                          * Othewise send an acceptor connection request. I can
168                          * have no portals peers so everyone else should
169                          * understand my protocol. */
170                         cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
171                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
172                         cr.acr_nid     = peer_nid;
173
174                         if (the_lnet.ln_testprotocompat != 0) {
175                                 /* single-shot proto check */
176                                 LNET_LOCK();
177                                 if ((the_lnet.ln_testprotocompat & 4) != 0) {
178                                         cr.acr_version++;
179                                         the_lnet.ln_testprotocompat &= ~4;
180                                 }
181                                 if ((the_lnet.ln_testprotocompat & 8) != 0) {
182                                         cr.acr_magic = LNET_PROTO_MAGIC;
183                                         the_lnet.ln_testprotocompat &= ~8;
184                                 }
185                                 LNET_UNLOCK();
186                         }
187
188                         rc = libcfs_sock_write(sock, &cr, sizeof(cr),
189                                                accept_timeout);
190                         if (rc != 0)
191                                 goto failed_sock;
192                 }
193                 
194                 *sockp = sock;
195                 return 0;
196         }
197
198         rc = -EADDRINUSE;
199         goto failed;
200         
201  failed_sock:
202         libcfs_sock_release(sock);
203  failed:
204         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
205         return rc;
206 }
207 EXPORT_SYMBOL(lnet_connect);
208
209 static inline int
210 lnet_accept_magic(__u32 magic, __u32 constant)
211 {
212         return (magic == constant ||
213                 magic == __swab32(constant));
214 }
215
216 int
217 lnet_accept(lnet_ni_t *blind_ni, cfs_socket_t *sock, __u32 magic)
218 {
219         lnet_acceptor_connreq_t cr;
220         __u32                   peer_ip;
221         int                     peer_port;
222         int                     rc;
223         int                     flip;
224         lnet_ni_t              *ni;
225         char                   *str;
226
227         /* CAVEAT EMPTOR: I may be called by an LND in any thread's context if
228          * I passed the new socket "blindly" to the single NI that needed an
229          * acceptor.  If so, blind_ni != NULL... */
230
231         LASSERT (sizeof(cr) <= 16);             /* not too big for the stack */
232         
233         rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
234         LASSERT (rc == 0);                      /* we succeeded before */
235
236         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
237
238                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
239                         /* future version compatibility!
240                          * When LNET unifies protocols over all LNDs, the first
241                          * thing sent will be a version query.  I send back
242                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
243
244                         memset (&cr, 0, sizeof(cr));
245                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
246                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
247                         rc = libcfs_sock_write(sock, &cr, sizeof(cr),
248                                                accept_timeout);
249
250                         if (rc != 0)
251                                 CERROR("Error sending magic+version in response"
252                                        "to LNET magic from %u.%u.%u.%u: %d\n",
253                                        HIPQUAD(peer_ip), rc);
254                         return -EPROTO;
255                 }
256
257                 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
258                         str = "'old' socknal/tcpnal";
259                 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
260                         str = "'old' ranal";
261                 else if (lnet_accept_magic(magic, LNET_PROTO_OPENIB_MAGIC))
262                         str = "'old' openibnal";
263                 else
264                         str = "unrecognised";
265             
266                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
267                                    " magic %08x: %s acceptor protocol\n",
268                                    HIPQUAD(peer_ip), magic, str);
269                 return -EPROTO;
270         }
271
272         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
273
274         rc = libcfs_sock_read(sock, &cr.acr_version, 
275                               sizeof(cr.acr_version),
276                               accept_timeout);
277         if (rc != 0) {
278                 CERROR("Error %d reading connection request version from "
279                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
280                 return -EIO;
281         }
282
283         if (flip)
284                 __swab32s(&cr.acr_version);
285         
286         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
287                 /* future version compatibility!
288                  * An acceptor-specific protocol rev will first send a version
289                  * query.  I send back my current version to tell her I'm
290                  * "old". */
291                 int peer_version = cr.acr_version;
292
293                 memset (&cr, 0, sizeof(cr));
294                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
295                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
296
297                 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
298                                        accept_timeout);
299
300                 if (rc != 0)
301                         CERROR("Error sending magic+version in response"
302                                "to version %d from %u.%u.%u.%u: %d\n",
303                                peer_version, HIPQUAD(peer_ip), rc);
304                 return -EPROTO;
305         }
306
307         rc = libcfs_sock_read(sock, &cr.acr_nid,
308                               sizeof(cr) -
309                               offsetof(lnet_acceptor_connreq_t, acr_nid),
310                               accept_timeout);
311         if (rc != 0) {
312                 CERROR("Error %d reading connection request from "
313                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
314                 return -EIO;
315         }
316
317         if (flip)
318                 __swab64s(&cr.acr_nid);
319
320         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
321         if (ni == NULL ||               /* no matching net */
322             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
323                 if (ni != NULL)
324                         lnet_ni_decref(ni);
325                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
326                                    " for %s: No matching NI\n",
327                                    HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
328                 return -EPERM;
329         }
330
331         if (ni->ni_lnd->lnd_accept == NULL) {
332                 /* This catches a request for the loopback LND */
333                 lnet_ni_decref(ni);
334                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
335                                   " for %s: NI doesn not accept IP connections\n",
336                                   HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
337                 return -EPERM;
338         }
339
340         CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u%s\n",
341                libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip),
342                blind_ni == NULL ? "" : " (blind)");
343
344         if (blind_ni == NULL) {
345                 /* called by the acceptor: call into the requested NI... */
346                 rc = ni->ni_lnd->lnd_accept(ni, sock);
347         } else {
348                 /* portals_compatible set and the (only) NI called me to verify
349                  * and skip the connection request... */
350                 LASSERT (the_lnet.ln_ptlcompat != 0);
351                 LASSERT (ni == blind_ni);
352                 rc = 0;
353         }
354
355         lnet_ni_decref(ni);
356         return rc;
357 }
358 EXPORT_SYMBOL(lnet_accept);
359         
360 int
361 lnet_acceptor(void *arg)
362 {
363         char           name[16];
364         cfs_socket_t  *newsock;
365         int            rc;
366         int            n_acceptor_nis;
367         __u32          magic;
368         __u32          peer_ip;
369         int            peer_port;
370         lnet_ni_t     *blind_ni = NULL;
371         int            secure = (int)((unsigned long)arg);
372
373         LASSERT (lnet_acceptor_state.pta_sock == NULL);
374
375         if (the_lnet.ln_ptlcompat != 0) {
376                 /* When portals_compatibility is enabled, peers may connect
377                  * without sending an acceptor connection request.  There is no
378                  * ambiguity about which network the peer wants to connect to
379                  * since there can only be 1 network, so I pass connections
380                  * "blindly" to it. */
381                 n_acceptor_nis = lnet_count_acceptor_nis(&blind_ni);
382                 LASSERT (n_acceptor_nis == 1);
383                 LASSERT (blind_ni != NULL);
384         }
385
386         snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
387         cfs_daemonize(name);
388         cfs_block_allsigs();
389
390         rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
391                                 0, accept_port, accept_backlog);
392         if (rc != 0) {
393                 if (rc == -EADDRINUSE)
394                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
395                                            " %d: port already in use\n",
396                                            accept_port);
397                 else
398                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
399                                            "%d: unexpected error %d\n",
400                                            accept_port, rc);
401
402                 lnet_acceptor_state.pta_sock = NULL;
403         } else {
404                 LCONSOLE(0, "Accept %s, port %d%s\n", 
405                          accept, accept_port,
406                          blind_ni == NULL ? "" : " (proto compatible)");
407         }
408         
409         /* set init status and unblock parent */
410         lnet_acceptor_state.pta_shutdown = rc;
411         mutex_up(&lnet_acceptor_state.pta_signal);
412         
413         if (rc != 0)
414                 return rc;
415
416         while (!lnet_acceptor_state.pta_shutdown) {
417                 
418                 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
419                 if (rc != 0) {
420                         if (rc != -EAGAIN) {
421                                 CWARN("Accept error %d: pausing...\n", rc);
422                                 cfs_pause(cfs_time_seconds(1));
423                         }
424                         continue;
425                 }
426
427                 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
428                 if (rc != 0) {
429                         CERROR("Can't determine new connection's address\n");
430                         goto failed;
431                 }
432
433                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
434                         CERROR("Refusing connection from %u.%u.%u.%u: "
435                                "insecure port %d\n",
436                                HIPQUAD(peer_ip), peer_port);
437                         goto failed;
438                 }
439
440                 if (blind_ni != NULL) {
441                         rc = blind_ni->ni_lnd->lnd_accept(blind_ni, newsock);
442                         if (rc != 0) {
443                                 CERROR("NI %s refused 'blind' connection from "
444                                        "%u.%u.%u.%u\n", 
445                                        libcfs_nid2str(blind_ni->ni_nid), 
446                                        HIPQUAD(peer_ip));
447                                 goto failed;
448                         }
449                         continue;
450                 }
451                 
452                 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
453                                       accept_timeout);
454                 if (rc != 0) {
455                         CERROR("Error %d reading connection request from "
456                                "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
457                         goto failed;
458                 }
459
460                 rc = lnet_accept(NULL, newsock, magic);
461                 if (rc != 0)
462                         goto failed;
463                 
464                 continue;
465                 
466         failed:
467                 libcfs_sock_release(newsock);
468         }
469         
470         libcfs_sock_release(lnet_acceptor_state.pta_sock);
471         lnet_acceptor_state.pta_sock = NULL;
472
473         if (blind_ni != NULL)
474                 lnet_ni_decref(blind_ni);
475
476         LCONSOLE(0,"Acceptor stopping\n");
477         
478         /* unblock lnet_acceptor_stop() */
479         mutex_up(&lnet_acceptor_state.pta_signal);
480         return 0;
481 }
482
483 int
484 lnet_acceptor_start(void)
485 {
486         long   pid;
487         long   secure;
488
489         LASSERT (lnet_acceptor_state.pta_sock == NULL);
490         init_mutex_locked(&lnet_acceptor_state.pta_signal);
491
492         if (!strcmp(accept, "secure")) {
493                 secure = 1;
494         } else if (!strcmp(accept, "all")) {
495                 secure = 0;
496         } else if (!strcmp(accept, "none")) {
497                 return 0;
498         } else {
499                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
500                                    accept);
501                 return -EINVAL;
502         }
503         
504         if (lnet_count_acceptor_nis(NULL) == 0)  /* not required */
505                 return 0;
506         
507         pid = cfs_kernel_thread(lnet_acceptor, (void *)secure, 0);
508         if (pid < 0) {
509                 CERROR("Can't start acceptor thread: %ld\n", pid);
510                 return -ESRCH;
511         }
512
513         mutex_down(&lnet_acceptor_state.pta_signal); /* wait for acceptor to startup */
514
515         if (!lnet_acceptor_state.pta_shutdown) {
516                 /* started OK */
517                 LASSERT (lnet_acceptor_state.pta_sock != NULL);
518                 return 0;
519         }
520
521         LASSERT (lnet_acceptor_state.pta_sock == NULL);
522         return -ENETDOWN;
523 }
524
525 void
526 lnet_acceptor_stop(void)
527 {
528         if (lnet_acceptor_state.pta_sock == NULL) /* not running */
529                 return;
530         
531         lnet_acceptor_state.pta_shutdown = 1;
532         libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
533
534         /* block until acceptor signals exit */
535         mutex_down(&lnet_acceptor_state.pta_signal);
536 }
537
538 #else /* __KERNEL__ */
539 #ifdef HAVE_LIBPTHREAD
540
541 static char *accept_type;
542 static int accept_port = 988;
543 static int accept_backlog;
544 static int accept_timeout;
545
546 struct {
547         int                   pta_shutdown;
548         int                   pta_sock;
549         struct cfs_completion pta_completion;
550 } lnet_acceptor_state;
551
552 int
553 lnet_acceptor_port(void)
554 {
555         return accept_port;
556 }
557
558 int
559 lnet_parse_int_tunable(int *value, char *name, int dflt)
560 {
561         char    *env = getenv(name);
562         char    *end;
563
564         if (env == NULL) {
565                 *value = dflt;
566                 return 0;
567         }
568
569         *value = strtoull(env, &end, 0);
570         if (*end == 0)
571                 return 0;
572
573         CERROR("Can't parse tunable %s=%s\n", name, env);
574         return -EINVAL;
575 }
576
577 int
578 lnet_parse_string_tunable(char **value, char *name, char *dflt)
579 {
580         char    *env = getenv(name);
581
582         if (env == NULL)
583                 *value = dflt;             
584         else
585                 *value = env;
586
587         return 0;
588 }
589
590 int
591 lnet_acceptor_get_tunables()
592 {
593         int rc;
594         rc = lnet_parse_string_tunable(&accept_type, "LNET_ACCEPT", "secure");
595
596         if (rc != 0)
597                 return rc;
598
599         rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT", 988);
600
601         if (rc != 0)
602                 return rc;
603         
604         rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG", 127);
605
606         if (rc != 0)
607                 return rc;
608
609         rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT", 5);
610
611         if (rc != 0)
612                 return rc;
613
614         CDEBUG(D_NET, "accept_type     = %s\n", accept_type);
615         CDEBUG(D_NET, "accept_port     = %d\n", accept_port);
616         CDEBUG(D_NET, "accept_backlog  = %d\n", accept_backlog);
617         CDEBUG(D_NET, "accept_timeout  = %d\n", accept_timeout);
618         return 0;
619 }
620
621 static inline int
622 lnet_accept_magic(__u32 magic, __u32 constant)
623 {
624         return (magic == constant ||
625                 magic == __swab32(constant));
626 }
627
628 /* user-land lnet_accept() isn't used by any LND's directly. So, we don't
629  * do it visible outside acceptor.c and we can change its prototype
630  * freely */
631 static int
632 lnet_accept(int sock, __u32 magic, __u32 peer_ip, int peer_port)
633 {
634         int rc, flip;
635         lnet_acceptor_connreq_t cr;
636         lnet_ni_t *ni;
637         
638         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
639                 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u magic %08x: "
640                                "unsupported acceptor protocol\n",
641                                HIPQUAD(peer_ip), magic);
642                 return -EPROTO;
643         }
644
645         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
646         
647         rc = libcfs_sock_read(sock, &cr.acr_version, 
648                               sizeof(cr.acr_version),
649                               accept_timeout);
650         if (rc != 0) {
651                 CERROR("Error %d reading connection request version from "
652                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
653                 return -EIO;
654         }
655
656         if (flip)
657                 __swab32s(&cr.acr_version);
658
659         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION)
660                 return -EPROTO;
661                 
662         rc = libcfs_sock_read(sock, &cr.acr_nid,
663                               sizeof(cr) -
664                               offsetof(lnet_acceptor_connreq_t, acr_nid),
665                               accept_timeout);
666         if (rc != 0) {
667                 CERROR("Error %d reading connection request from "
668                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
669                 return -EIO;
670         }
671
672         if (flip)
673                 __swab64s(&cr.acr_nid);
674
675         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
676                        
677         if (ni == NULL ||                    /* no matching net */
678              ni->ni_nid != cr.acr_nid) {     /* right NET, wrong NID! */
679                 if (ni != NULL)
680                         lnet_ni_decref(ni);
681                 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u for %s: "
682                                " No matching NI\n",
683                                HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
684                 return -EPERM;
685         }
686
687         if (ni->ni_lnd->lnd_accept == NULL) {
688                 lnet_ni_decref(ni);
689                 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u for %s: "
690                                " NI doesn not accept IP connections\n",
691                                HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
692                 return -EPERM;
693         }
694         
695         CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
696                libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
697
698         rc = ni->ni_lnd->lnd_accept(ni, sock);
699         
700         lnet_ni_decref(ni);
701         return rc;
702 }
703
704 int
705 lnet_acceptor(void *arg)
706 {
707         char           name[16];
708         int            secure = (int)((unsigned long)arg);
709         int            rc;
710         int            newsock;
711         __u32          peer_ip;
712         int            peer_port;
713         __u32          magic;
714
715         snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
716         cfs_daemonize(name);
717         cfs_block_allsigs();
718
719         rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
720                                 0, accept_port, accept_backlog);
721         if (rc != 0) {
722                 if (rc == -EADDRINUSE)
723                         LCONSOLE_ERROR("Can't start acceptor on port %d: "
724                                        "port already in use\n",
725                                        accept_port);
726                 else
727                         LCONSOLE_ERROR("Can't start acceptor on port %d: "
728                                        "unexpected error %d\n",
729                                        accept_port, rc);
730
731         } else {
732                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
733         }
734         
735         /* set init status and unblock parent */
736         lnet_acceptor_state.pta_shutdown = rc;
737         cfs_complete(&lnet_acceptor_state.pta_completion);
738
739         if (rc != 0)
740                 return rc;
741
742         while (!lnet_acceptor_state.pta_shutdown) {
743
744                 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock,
745                                         &peer_ip, &peer_port);
746                 if (rc != 0)
747                         continue;
748
749                 /* maybe we're waken up with libcfs_sock_abort_accept() */
750                 if (lnet_acceptor_state.pta_shutdown) {
751                         close(newsock);
752                         break;
753                 }
754
755                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
756                         CERROR("Refusing connection from %u.%u.%u.%u: "
757                                "insecure port %d\n",
758                                HIPQUAD(peer_ip), peer_port);
759                         goto failed;
760                 }
761
762                 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
763                                       accept_timeout);
764                 if (rc != 0) {
765                         CERROR("Error %d reading connection request from "
766                                "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
767                         goto failed;
768                 }
769
770                 rc = lnet_accept(newsock, magic, peer_ip, peer_port);
771                 if (rc != 0)
772                         goto failed;
773                 
774                 continue;
775                 
776           failed:
777                 close(newsock);
778         }
779         
780         close(lnet_acceptor_state.pta_sock);
781         LCONSOLE(0,"Acceptor stopping\n");
782
783         /* unblock lnet_acceptor_stop() */
784         cfs_complete(&lnet_acceptor_state.pta_completion);        
785
786         return 0;
787 }
788
789 static int skip_waiting_for_completion;
790
791 int
792 lnet_acceptor_start(void)
793 {
794         long   secure;
795         int rc;
796
797         rc = lnet_acceptor_get_tunables();
798         if (rc != 0)
799                 return rc;
800
801         /* Do nothing if we're liblustre clients */
802         if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
803                 return 0;
804                         
805         cfs_init_completion(&lnet_acceptor_state.pta_completion);
806
807         if (!strcmp(accept_type, "secure")) {
808                 secure = 1;
809         } else if (!strcmp(accept_type, "all")) {
810                 secure = 0;
811         } else if (!strcmp(accept_type, "none")) {
812                 skip_waiting_for_completion = 1;
813                 return 0;
814         } else {
815                 LCONSOLE_ERROR ("Can't parse 'accept_type=\"%s\"'\n", accept_type);
816                 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
817                 return -EINVAL;
818         }
819
820         if (lnet_count_acceptor_nis(NULL) == 0) { /* not required */
821                 skip_waiting_for_completion = 1;
822                 return 0;
823         }
824
825         rc = cfs_create_thread(lnet_acceptor, (void *)secure);
826         if (rc != 0) {
827                 CERROR("Can't start acceptor thread: %d\n", rc);
828                 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
829                 return rc;
830         }
831
832         /* wait for acceptor to startup */
833         cfs_wait_for_completion(&lnet_acceptor_state.pta_completion);
834
835         if (!lnet_acceptor_state.pta_shutdown)
836                 return 0;
837         
838         cfs_fini_completion(&lnet_acceptor_state.pta_completion);
839         return -ENETDOWN;
840 }
841
842 void
843 lnet_acceptor_stop(void)
844 {
845         /* Do nothing if we're liblustre clients */
846         if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
847                 return;
848
849         if (!skip_waiting_for_completion) {
850                 lnet_acceptor_state.pta_shutdown = 1;
851                 libcfs_sock_abort_accept(accept_port);
852                 
853                 /* block until acceptor signals exit */
854                 cfs_wait_for_completion(&lnet_acceptor_state.pta_completion);
855         }
856         
857         cfs_fini_completion(&lnet_acceptor_state.pta_completion);
858 }
859 #else
860 int
861 lnet_acceptor_start(void)
862 {
863         return 0;
864 }
865
866 void
867 lnet_acceptor_stop(void)
868 {
869 }
870 #endif /* !HAVE_LIBPTHREAD */
871 #endif /* !__KERNEL__ */