1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_LNET
24 #include <lnet/lib-lnet.h>
27 static char *accept = "secure";
28 CFS_MODULE_PARM(accept, "s", charp, 0444,
29 "Accept connections (secure|all|none)");
31 static int accept_port = 988;
32 CFS_MODULE_PARM(accept_port, "i", int, 0444,
33 "Acceptor's port (same on all nodes)");
35 static int accept_backlog = 127;
36 CFS_MODULE_PARM(accept_backlog, "i", int, 0444,
37 "Acceptor's listen backlog");
39 static int accept_timeout = 5;
40 CFS_MODULE_PARM(accept_timeout, "i", int, 0644,
41 "Acceptor's timeout (seconds)");
45 cfs_socket_t *pta_sock;
46 struct semaphore pta_signal;
47 } lnet_acceptor_state;
50 lnet_acceptor_timeout(void)
52 return accept_timeout;
54 EXPORT_SYMBOL(lnet_acceptor_timeout);
57 lnet_acceptor_port(void)
61 EXPORT_SYMBOL(lnet_acceptor_port);
64 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
65 __u32 peer_ip, int peer_port)
70 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u "
71 "on port %d was refused: "
72 "check that Lustre is running on that node.\n",
73 libcfs_nid2str(peer_nid),
74 HIPQUAD(peer_ip), peer_port);
78 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u "
79 "was unreachable: the network or that node may "
80 "be down, or Lustre may be misconfigured.\n",
81 libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
84 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u on "
85 "port %d took too long: that node may be hung "
86 "or experiencing high load.\n",
87 libcfs_nid2str(peer_nid),
88 HIPQUAD(peer_ip), peer_port);
91 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
92 " on port %d was reset: "
93 "is it running a compatible version of "
94 "Lustre and is %s one of its NIDs?\n",
95 libcfs_nid2str(peer_nid),
96 HIPQUAD(peer_ip), peer_port,
97 libcfs_nid2str(peer_nid));
100 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
101 "host %u.%u.%u.%u on port %d: is it running "
102 "a compatible version of Lustre?\n",
103 libcfs_nid2str(peer_nid),
104 HIPQUAD(peer_ip), peer_port);
107 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
108 "connect to %s at host %u.%u.%u.%u on port "
109 "%d\n", libcfs_nid2str(peer_nid),
110 HIPQUAD(peer_ip), peer_port);
113 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
114 " at host %u.%u.%u.%u on port %d\n", rc,
115 libcfs_nid2str(peer_nid),
116 HIPQUAD(peer_ip), peer_port);
120 EXPORT_SYMBOL(lnet_connect_console_error);
123 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
124 __u32 local_ip, __u32 peer_ip, int peer_port)
126 lnet_acceptor_connreq_t cr;
132 CLASSERT (sizeof(cr) <= 16); /* not too big to be on the stack */
134 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
135 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
137 /* Iterate through reserved ports. */
139 rc = libcfs_sock_connect(&sock, &fatal,
148 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
150 if (the_lnet.ln_ptlcompat != 2) {
151 /* When portals compatibility is "strong", simply
152 * connect (i.e. send no acceptor connection request).
153 * Othewise send an acceptor connection request. I can
154 * have no portals peers so everyone else should
155 * understand my protocol. */
156 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
157 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
158 cr.acr_nid = peer_nid;
160 if (the_lnet.ln_testprotocompat != 0) {
161 /* single-shot proto check */
163 if ((the_lnet.ln_testprotocompat & 4) != 0) {
165 the_lnet.ln_testprotocompat &= ~4;
167 if ((the_lnet.ln_testprotocompat & 8) != 0) {
168 cr.acr_magic = LNET_PROTO_MAGIC;
169 the_lnet.ln_testprotocompat &= ~8;
174 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
188 libcfs_sock_release(sock);
190 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
193 EXPORT_SYMBOL(lnet_connect);
196 lnet_accept_magic(__u32 magic, __u32 constant)
198 return (magic == constant ||
199 magic == __swab32(constant));
203 lnet_accept(lnet_ni_t *blind_ni, cfs_socket_t *sock, __u32 magic)
205 lnet_acceptor_connreq_t cr;
213 /* CAVEAT EMPTOR: I may be called by an LND in any thread's context if
214 * I passed the new socket "blindly" to the single NI that needed an
215 * acceptor. If so, blind_ni != NULL... */
217 LASSERT (sizeof(cr) <= 16); /* not too big for the stack */
219 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
220 LASSERT (rc == 0); /* we succeeded before */
222 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
224 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
225 /* future version compatibility!
226 * When LNET unifies protocols over all LNDs, the first
227 * thing sent will be a version query. I send back
228 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
230 memset (&cr, 0, sizeof(cr));
231 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
232 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
233 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
237 CERROR("Error sending magic+version in response"
238 "to LNET magic from %u.%u.%u.%u: %d\n",
239 HIPQUAD(peer_ip), rc);
243 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
244 str = "'old' socknal/tcpnal";
245 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
247 else if (lnet_accept_magic(magic, LNET_PROTO_OPENIB_MAGIC))
248 str = "'old' openibnal";
250 str = "unrecognised";
252 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
253 " magic %08x: %s acceptor protocol\n",
254 HIPQUAD(peer_ip), magic, str);
258 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
260 rc = libcfs_sock_read(sock, &cr.acr_version,
261 sizeof(cr.acr_version),
264 CERROR("Error %d reading connection request version from "
265 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
270 __swab32s(&cr.acr_version);
272 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
273 /* future version compatibility!
274 * An acceptor-specific protocol rev will first send a version
275 * query. I send back my current version to tell her I'm
277 int peer_version = cr.acr_version;
279 memset (&cr, 0, sizeof(cr));
280 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
281 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
283 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
287 CERROR("Error sending magic+version in response"
288 "to version %d from %u.%u.%u.%u: %d\n",
289 peer_version, HIPQUAD(peer_ip), rc);
293 rc = libcfs_sock_read(sock, &cr.acr_nid,
295 offsetof(lnet_acceptor_connreq_t, acr_nid),
298 CERROR("Error %d reading connection request from "
299 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
304 __swab64s(&cr.acr_nid);
306 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
307 if (ni == NULL || /* no matching net */
308 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
311 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
312 " for %s: No matching NI\n",
313 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
317 if (ni->ni_lnd->lnd_accept == NULL) {
318 /* This catches a request for the loopback LND */
320 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
321 " for %s: NI doesn not accept IP connections\n",
322 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
326 CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u%s\n",
327 libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip),
328 blind_ni == NULL ? "" : " (blind)");
330 if (blind_ni == NULL) {
331 /* called by the acceptor: call into the requested NI... */
332 rc = ni->ni_lnd->lnd_accept(ni, sock);
334 /* portals_compatible set and the (only) NI called me to verify
335 * and skip the connection request... */
336 LASSERT (the_lnet.ln_ptlcompat != 0);
337 LASSERT (ni == blind_ni);
344 EXPORT_SYMBOL(lnet_accept);
347 lnet_acceptor(void *arg)
350 cfs_socket_t *newsock;
356 lnet_ni_t *blind_ni = NULL;
357 int secure = (int)((unsigned long)arg);
359 LASSERT (lnet_acceptor_state.pta_sock == NULL);
361 if (the_lnet.ln_ptlcompat != 0) {
362 /* When portals_compatibility is enabled, peers may connect
363 * without sending an acceptor connection request. There is no
364 * ambiguity about which network the peer wants to connect to
365 * since there can only be 1 network, so I pass connections
366 * "blindly" to it. */
367 n_acceptor_nis = lnet_count_acceptor_nis(&blind_ni);
368 LASSERT (n_acceptor_nis == 1);
369 LASSERT (blind_ni != NULL);
372 snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
376 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
377 0, accept_port, accept_backlog);
379 if (rc == -EADDRINUSE)
380 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
381 " %d: port already in use\n",
384 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
385 "%d: unexpected error %d\n",
388 lnet_acceptor_state.pta_sock = NULL;
390 LCONSOLE(0, "Accept %s, port %d%s\n",
392 blind_ni == NULL ? "" : " (proto compatible)");
395 /* set init status and unblock parent */
396 lnet_acceptor_state.pta_shutdown = rc;
397 mutex_up(&lnet_acceptor_state.pta_signal);
402 while (!lnet_acceptor_state.pta_shutdown) {
404 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
407 CWARN("Accept error %d: pausing...\n", rc);
408 cfs_pause(cfs_time_seconds(1));
413 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
415 CERROR("Can't determine new connection's address\n");
419 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
420 CERROR("Refusing connection from %u.%u.%u.%u: "
421 "insecure port %d\n",
422 HIPQUAD(peer_ip), peer_port);
426 if (blind_ni != NULL) {
427 rc = blind_ni->ni_lnd->lnd_accept(blind_ni, newsock);
429 CERROR("NI %s refused 'blind' connection from "
431 libcfs_nid2str(blind_ni->ni_nid),
438 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
441 CERROR("Error %d reading connection request from "
442 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
446 rc = lnet_accept(NULL, newsock, magic);
453 libcfs_sock_release(newsock);
456 libcfs_sock_release(lnet_acceptor_state.pta_sock);
457 lnet_acceptor_state.pta_sock = NULL;
459 if (blind_ni != NULL)
460 lnet_ni_decref(blind_ni);
462 LCONSOLE(0,"Acceptor stopping\n");
464 /* unblock lnet_acceptor_stop() */
465 mutex_up(&lnet_acceptor_state.pta_signal);
470 lnet_acceptor_start(void)
475 LASSERT (lnet_acceptor_state.pta_sock == NULL);
476 init_mutex_locked(&lnet_acceptor_state.pta_signal);
478 if (!strcmp(accept, "secure")) {
480 } else if (!strcmp(accept, "all")) {
482 } else if (!strcmp(accept, "none")) {
485 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
490 if (lnet_count_acceptor_nis(NULL) == 0) /* not required */
493 pid = cfs_kernel_thread(lnet_acceptor, (void *)secure, 0);
495 CERROR("Can't start acceptor thread: %ld\n", pid);
499 mutex_down(&lnet_acceptor_state.pta_signal); /* wait for acceptor to startup */
501 if (!lnet_acceptor_state.pta_shutdown) {
503 LASSERT (lnet_acceptor_state.pta_sock != NULL);
507 LASSERT (lnet_acceptor_state.pta_sock == NULL);
512 lnet_acceptor_stop(void)
514 if (lnet_acceptor_state.pta_sock == NULL) /* not running */
517 lnet_acceptor_state.pta_shutdown = 1;
518 libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
520 /* block until acceptor signals exit */
521 mutex_down(&lnet_acceptor_state.pta_signal);
524 #else /* __KERNEL__ */
525 #ifdef HAVE_LIBPTHREAD
527 static char *accept_type;
528 static int accept_port = 988;
529 static int accept_backlog;
530 static int accept_timeout;
535 struct cfs_completion pta_completion;
536 } lnet_acceptor_state;
539 lnet_acceptor_port(void)
545 lnet_parse_int_tunable(int *value, char *name, int dflt)
547 char *env = getenv(name);
555 *value = strtoull(env, &end, 0);
559 CERROR("Can't parse tunable %s=%s\n", name, env);
564 lnet_parse_string_tunable(char **value, char *name, char *dflt)
566 char *env = getenv(name);
577 lnet_acceptor_get_tunables()
580 rc = lnet_parse_string_tunable(&accept_type, "LNET_ACCEPT", "secure");
585 rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT", 988);
590 rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG", 127);
595 rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT", 5);
600 CDEBUG(D_NET, "accept_type = %s\n", accept_type);
601 CDEBUG(D_NET, "accept_port = %d\n", accept_port);
602 CDEBUG(D_NET, "accept_backlog = %d\n", accept_backlog);
603 CDEBUG(D_NET, "accept_timeout = %d\n", accept_timeout);
608 lnet_accept_magic(__u32 magic, __u32 constant)
610 return (magic == constant ||
611 magic == __swab32(constant));
614 /* user-land lnet_accept() isn't used by any LND's directly. So, we don't
615 * do it visible outside acceptor.c and we can change its prototype
618 lnet_accept(int sock, __u32 magic, __u32 peer_ip, int peer_port)
621 lnet_acceptor_connreq_t cr;
624 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
625 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u magic %08x: "
626 "unsupported acceptor protocol\n",
627 HIPQUAD(peer_ip), magic);
631 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
633 rc = libcfs_sock_read(sock, &cr.acr_version,
634 sizeof(cr.acr_version),
637 CERROR("Error %d reading connection request version from "
638 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
643 __swab32s(&cr.acr_version);
645 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION)
648 rc = libcfs_sock_read(sock, &cr.acr_nid,
650 offsetof(lnet_acceptor_connreq_t, acr_nid),
653 CERROR("Error %d reading connection request from "
654 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
659 __swab64s(&cr.acr_nid);
661 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
663 if (ni == NULL || /* no matching net */
664 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
667 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u for %s: "
669 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
673 if (ni->ni_lnd->lnd_accept == NULL) {
675 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u for %s: "
676 " NI doesn not accept IP connections\n",
677 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
681 CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
682 libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
684 rc = ni->ni_lnd->lnd_accept(ni, sock);
691 lnet_acceptor(void *arg)
694 int secure = (int)((unsigned long)arg);
701 snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
705 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
706 0, accept_port, accept_backlog);
708 if (rc == -EADDRINUSE)
709 LCONSOLE_ERROR("Can't start acceptor on port %d: "
710 "port already in use\n",
713 LCONSOLE_ERROR("Can't start acceptor on port %d: "
714 "unexpected error %d\n",
718 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
721 /* set init status and unblock parent */
722 lnet_acceptor_state.pta_shutdown = rc;
723 cfs_complete(&lnet_acceptor_state.pta_completion);
728 while (!lnet_acceptor_state.pta_shutdown) {
730 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock,
731 &peer_ip, &peer_port);
735 /* maybe we're waken up with libcfs_sock_abort_accept() */
736 if (lnet_acceptor_state.pta_shutdown) {
741 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
742 CERROR("Refusing connection from %u.%u.%u.%u: "
743 "insecure port %d\n",
744 HIPQUAD(peer_ip), peer_port);
748 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
751 CERROR("Error %d reading connection request from "
752 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
756 rc = lnet_accept(newsock, magic, peer_ip, peer_port);
766 close(lnet_acceptor_state.pta_sock);
767 LCONSOLE(0,"Acceptor stopping\n");
769 /* unblock lnet_acceptor_stop() */
770 cfs_complete(&lnet_acceptor_state.pta_completion);
775 static int skip_waiting_for_completion;
778 lnet_acceptor_start(void)
783 rc = lnet_acceptor_get_tunables();
787 /* Do nothing if we're liblustre clients */
788 if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
791 cfs_init_completion(&lnet_acceptor_state.pta_completion);
793 if (!strcmp(accept_type, "secure")) {
795 } else if (!strcmp(accept_type, "all")) {
797 } else if (!strcmp(accept_type, "none")) {
798 skip_waiting_for_completion = 1;
801 LCONSOLE_ERROR ("Can't parse 'accept_type=\"%s\"'\n", accept_type);
802 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
806 if (lnet_count_acceptor_nis(NULL) == 0) { /* not required */
807 skip_waiting_for_completion = 1;
811 rc = cfs_create_thread(lnet_acceptor, (void *)secure);
813 CERROR("Can't start acceptor thread: %d\n", rc);
814 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
818 /* wait for acceptor to startup */
819 cfs_wait_for_completion(&lnet_acceptor_state.pta_completion);
821 if (!lnet_acceptor_state.pta_shutdown)
824 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
829 lnet_acceptor_stop(void)
831 /* Do nothing if we're liblustre clients */
832 if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
835 if (!skip_waiting_for_completion) {
836 lnet_acceptor_state.pta_shutdown = 1;
837 libcfs_sock_abort_accept(accept_port);
839 /* block until acceptor signals exit */
840 cfs_wait_for_completion(&lnet_acceptor_state.pta_completion);
843 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
847 lnet_acceptor_start(void)
853 lnet_acceptor_stop(void)
856 #endif /* !HAVE_LIBPTHREAD */
857 #endif /* !__KERNEL__ */