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