1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
41 static char *accept = "secure";
42 CFS_MODULE_PARM(accept, "s", charp, 0444,
43 "Accept connections (secure|all|none)");
45 static int accept_port = 988;
46 CFS_MODULE_PARM(accept_port, "i", int, 0444,
47 "Acceptor's port (same on all nodes)");
49 static int accept_backlog = 127;
50 CFS_MODULE_PARM(accept_backlog, "i", int, 0444,
51 "Acceptor's listen backlog");
53 static int accept_timeout = 5;
54 CFS_MODULE_PARM(accept_timeout, "i", int, 0644,
55 "Acceptor's timeout (seconds)");
59 cfs_socket_t *pta_sock;
60 struct semaphore pta_signal;
61 } lnet_acceptor_state;
64 lnet_acceptor_timeout(void)
66 return accept_timeout;
68 EXPORT_SYMBOL(lnet_acceptor_timeout);
71 lnet_acceptor_port(void)
75 EXPORT_SYMBOL(lnet_acceptor_port);
78 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
79 __u32 peer_ip, int peer_port)
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);
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));
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);
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));
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);
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);
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);
134 EXPORT_SYMBOL(lnet_connect_console_error);
137 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
138 __u32 local_ip, __u32 peer_ip, int peer_port)
140 lnet_acceptor_connreq_t cr;
146 CLASSERT (sizeof(cr) <= 16); /* not too big to be on the stack */
148 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
149 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
151 /* Iterate through reserved ports. */
153 rc = libcfs_sock_connect(&sock, &fatal,
162 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
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;
174 if (the_lnet.ln_testprotocompat != 0) {
175 /* single-shot proto check */
177 if ((the_lnet.ln_testprotocompat & 4) != 0) {
179 the_lnet.ln_testprotocompat &= ~4;
181 if ((the_lnet.ln_testprotocompat & 8) != 0) {
182 cr.acr_magic = LNET_PROTO_MAGIC;
183 the_lnet.ln_testprotocompat &= ~8;
188 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
202 libcfs_sock_release(sock);
204 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
207 EXPORT_SYMBOL(lnet_connect);
210 lnet_accept_magic(__u32 magic, __u32 constant)
212 return (magic == constant ||
213 magic == __swab32(constant));
217 lnet_accept(lnet_ni_t *blind_ni, cfs_socket_t *sock, __u32 magic)
219 lnet_acceptor_connreq_t cr;
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... */
231 LASSERT (sizeof(cr) <= 16); /* not too big for the stack */
233 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
234 LASSERT (rc == 0); /* we succeeded before */
236 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
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" */
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),
251 CERROR("Error sending magic+version in response"
252 "to LNET magic from %u.%u.%u.%u: %d\n",
253 HIPQUAD(peer_ip), rc);
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))
261 else if (lnet_accept_magic(magic, LNET_PROTO_OPENIB_MAGIC))
262 str = "'old' openibnal";
264 str = "unrecognised";
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);
272 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
274 rc = libcfs_sock_read(sock, &cr.acr_version,
275 sizeof(cr.acr_version),
278 CERROR("Error %d reading connection request version from "
279 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
284 __swab32s(&cr.acr_version);
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
291 int peer_version = cr.acr_version;
293 memset (&cr, 0, sizeof(cr));
294 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
295 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
297 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
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);
307 rc = libcfs_sock_read(sock, &cr.acr_nid,
309 offsetof(lnet_acceptor_connreq_t, acr_nid),
312 CERROR("Error %d reading connection request from "
313 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
318 __swab64s(&cr.acr_nid);
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! */
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));
331 if (ni->ni_lnd->lnd_accept == NULL) {
332 /* This catches a request for the loopback LND */
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));
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)");
344 if (blind_ni == NULL) {
345 /* called by the acceptor: call into the requested NI... */
346 rc = ni->ni_lnd->lnd_accept(ni, sock);
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);
358 EXPORT_SYMBOL(lnet_accept);
361 lnet_acceptor(void *arg)
364 cfs_socket_t *newsock;
370 lnet_ni_t *blind_ni = NULL;
371 int secure = (int)((unsigned long)arg);
373 LASSERT (lnet_acceptor_state.pta_sock == NULL);
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);
386 snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
390 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
391 0, accept_port, accept_backlog);
393 if (rc == -EADDRINUSE)
394 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
395 " %d: port already in use\n",
398 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
399 "%d: unexpected error %d\n",
402 lnet_acceptor_state.pta_sock = NULL;
404 LCONSOLE(0, "Accept %s, port %d%s\n",
406 blind_ni == NULL ? "" : " (proto compatible)");
409 /* set init status and unblock parent */
410 lnet_acceptor_state.pta_shutdown = rc;
411 mutex_up(&lnet_acceptor_state.pta_signal);
416 while (!lnet_acceptor_state.pta_shutdown) {
418 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
421 CWARN("Accept error %d: pausing...\n", rc);
422 cfs_pause(cfs_time_seconds(1));
427 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
429 CERROR("Can't determine new connection's address\n");
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);
440 if (blind_ni != NULL) {
441 rc = blind_ni->ni_lnd->lnd_accept(blind_ni, newsock);
443 CERROR("NI %s refused 'blind' connection from "
445 libcfs_nid2str(blind_ni->ni_nid),
452 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
455 CERROR("Error %d reading connection request from "
456 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
460 rc = lnet_accept(NULL, newsock, magic);
467 libcfs_sock_release(newsock);
470 libcfs_sock_release(lnet_acceptor_state.pta_sock);
471 lnet_acceptor_state.pta_sock = NULL;
473 if (blind_ni != NULL)
474 lnet_ni_decref(blind_ni);
476 LCONSOLE(0,"Acceptor stopping\n");
478 /* unblock lnet_acceptor_stop() */
479 mutex_up(&lnet_acceptor_state.pta_signal);
484 lnet_acceptor_start(void)
489 LASSERT (lnet_acceptor_state.pta_sock == NULL);
490 init_mutex_locked(&lnet_acceptor_state.pta_signal);
492 if (!strcmp(accept, "secure")) {
494 } else if (!strcmp(accept, "all")) {
496 } else if (!strcmp(accept, "none")) {
499 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
504 if (lnet_count_acceptor_nis(NULL) == 0) /* not required */
507 pid = cfs_kernel_thread(lnet_acceptor, (void *)secure, 0);
509 CERROR("Can't start acceptor thread: %ld\n", pid);
513 mutex_down(&lnet_acceptor_state.pta_signal); /* wait for acceptor to startup */
515 if (!lnet_acceptor_state.pta_shutdown) {
517 LASSERT (lnet_acceptor_state.pta_sock != NULL);
521 LASSERT (lnet_acceptor_state.pta_sock == NULL);
526 lnet_acceptor_stop(void)
528 if (lnet_acceptor_state.pta_sock == NULL) /* not running */
531 lnet_acceptor_state.pta_shutdown = 1;
532 libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
534 /* block until acceptor signals exit */
535 mutex_down(&lnet_acceptor_state.pta_signal);
538 #else /* __KERNEL__ */
539 #ifdef HAVE_LIBPTHREAD
541 static char *accept_type;
542 static int accept_port = 988;
543 static int accept_backlog;
544 static int accept_timeout;
549 struct cfs_completion pta_completion;
550 } lnet_acceptor_state;
553 lnet_acceptor_port(void)
559 lnet_parse_int_tunable(int *value, char *name, int dflt)
561 char *env = getenv(name);
569 *value = strtoull(env, &end, 0);
573 CERROR("Can't parse tunable %s=%s\n", name, env);
578 lnet_parse_string_tunable(char **value, char *name, char *dflt)
580 char *env = getenv(name);
591 lnet_acceptor_get_tunables()
594 rc = lnet_parse_string_tunable(&accept_type, "LNET_ACCEPT", "secure");
599 rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT", 988);
604 rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG", 127);
609 rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT", 5);
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);
622 lnet_accept_magic(__u32 magic, __u32 constant)
624 return (magic == constant ||
625 magic == __swab32(constant));
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
632 lnet_accept(int sock, __u32 magic, __u32 peer_ip, int peer_port)
635 lnet_acceptor_connreq_t cr;
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);
645 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
647 rc = libcfs_sock_read(sock, &cr.acr_version,
648 sizeof(cr.acr_version),
651 CERROR("Error %d reading connection request version from "
652 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
657 __swab32s(&cr.acr_version);
659 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION)
662 rc = libcfs_sock_read(sock, &cr.acr_nid,
664 offsetof(lnet_acceptor_connreq_t, acr_nid),
667 CERROR("Error %d reading connection request from "
668 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
673 __swab64s(&cr.acr_nid);
675 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
677 if (ni == NULL || /* no matching net */
678 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
681 LCONSOLE_ERROR("Refusing connection from %u.%u.%u.%u for %s: "
683 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
687 if (ni->ni_lnd->lnd_accept == NULL) {
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));
695 CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
696 libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
698 rc = ni->ni_lnd->lnd_accept(ni, sock);
705 lnet_acceptor(void *arg)
708 int secure = (int)((unsigned long)arg);
715 snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
719 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
720 0, accept_port, accept_backlog);
722 if (rc == -EADDRINUSE)
723 LCONSOLE_ERROR("Can't start acceptor on port %d: "
724 "port already in use\n",
727 LCONSOLE_ERROR("Can't start acceptor on port %d: "
728 "unexpected error %d\n",
732 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
735 /* set init status and unblock parent */
736 lnet_acceptor_state.pta_shutdown = rc;
737 cfs_complete(&lnet_acceptor_state.pta_completion);
742 while (!lnet_acceptor_state.pta_shutdown) {
744 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock,
745 &peer_ip, &peer_port);
749 /* maybe we're waken up with libcfs_sock_abort_accept() */
750 if (lnet_acceptor_state.pta_shutdown) {
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);
762 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
765 CERROR("Error %d reading connection request from "
766 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
770 rc = lnet_accept(newsock, magic, peer_ip, peer_port);
780 close(lnet_acceptor_state.pta_sock);
781 LCONSOLE(0,"Acceptor stopping\n");
783 /* unblock lnet_acceptor_stop() */
784 cfs_complete(&lnet_acceptor_state.pta_completion);
789 static int skip_waiting_for_completion;
792 lnet_acceptor_start(void)
797 rc = lnet_acceptor_get_tunables();
801 /* Do nothing if we're liblustre clients */
802 if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
805 cfs_init_completion(&lnet_acceptor_state.pta_completion);
807 if (!strcmp(accept_type, "secure")) {
809 } else if (!strcmp(accept_type, "all")) {
811 } else if (!strcmp(accept_type, "none")) {
812 skip_waiting_for_completion = 1;
815 LCONSOLE_ERROR ("Can't parse 'accept_type=\"%s\"'\n", accept_type);
816 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
820 if (lnet_count_acceptor_nis(NULL) == 0) { /* not required */
821 skip_waiting_for_completion = 1;
825 rc = cfs_create_thread(lnet_acceptor, (void *)secure);
827 CERROR("Can't start acceptor thread: %d\n", rc);
828 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
832 /* wait for acceptor to startup */
833 cfs_wait_for_completion(&lnet_acceptor_state.pta_completion);
835 if (!lnet_acceptor_state.pta_shutdown)
838 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
843 lnet_acceptor_stop(void)
845 /* Do nothing if we're liblustre clients */
846 if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
849 if (!skip_waiting_for_completion) {
850 lnet_acceptor_state.pta_shutdown = 1;
851 libcfs_sock_abort_accept(accept_port);
853 /* block until acceptor signals exit */
854 cfs_wait_for_completion(&lnet_acceptor_state.pta_completion);
857 cfs_fini_completion(&lnet_acceptor_state.pta_completion);
861 lnet_acceptor_start(void)
867 lnet_acceptor_stop(void)
870 #endif /* !HAVE_LIBPTHREAD */
871 #endif /* !__KERNEL__ */