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
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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>
40 #if defined(__KERNEL__) || defined(HAVE_LIBPTHREAD)
42 static int accept_port = 988;
43 static int accept_backlog = 127;
44 static int accept_timeout = 5;
48 cfs_socket_t *pta_sock;
50 struct semaphore pta_signal;
52 struct cfs_completion pta_signal;
54 } lnet_acceptor_state;
57 lnet_acceptor_port(void)
63 lnet_accept_magic(__u32 magic, __u32 constant)
65 return (magic == constant ||
66 magic == __swab32(constant));
71 #define cfs_init_completion(c) init_mutex_locked(c)
72 #define cfs_wait_for_completion(c) mutex_down(c)
73 #define cfs_complete(c) mutex_up(c)
74 #define cfs_fini_completion(c) do { } while (0)
75 #define cfs_create_thread(func, a) cfs_kernel_thread(func, a, 0)
77 EXPORT_SYMBOL(lnet_acceptor_port);
79 static char *accept = "secure";
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)");
91 lnet_acceptor_timeout(void)
93 return accept_timeout;
95 EXPORT_SYMBOL(lnet_acceptor_timeout);
98 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
99 __u32 peer_ip, int peer_port)
102 /* "normal" errors */
104 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u "
105 "on port %d was refused: "
106 "check that Lustre is running on that node.\n",
107 libcfs_nid2str(peer_nid),
108 HIPQUAD(peer_ip), peer_port);
112 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u "
113 "was unreachable: the network or that node may "
114 "be down, or Lustre may be misconfigured.\n",
115 libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
118 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u on "
119 "port %d took too long: that node may be hung "
120 "or experiencing high load.\n",
121 libcfs_nid2str(peer_nid),
122 HIPQUAD(peer_ip), peer_port);
125 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
126 " on port %d was reset: "
127 "is it running a compatible version of "
128 "Lustre and is %s one of its NIDs?\n",
129 libcfs_nid2str(peer_nid),
130 HIPQUAD(peer_ip), peer_port,
131 libcfs_nid2str(peer_nid));
134 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
135 "host %u.%u.%u.%u on port %d: is it running "
136 "a compatible version of Lustre?\n",
137 libcfs_nid2str(peer_nid),
138 HIPQUAD(peer_ip), peer_port);
141 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
142 "connect to %s at host %u.%u.%u.%u on port "
143 "%d\n", libcfs_nid2str(peer_nid),
144 HIPQUAD(peer_ip), peer_port);
147 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
148 " at host %u.%u.%u.%u on port %d\n", rc,
149 libcfs_nid2str(peer_nid),
150 HIPQUAD(peer_ip), peer_port);
154 EXPORT_SYMBOL(lnet_connect_console_error);
157 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
158 __u32 local_ip, __u32 peer_ip, int peer_port)
160 lnet_acceptor_connreq_t cr;
166 CLASSERT (sizeof(cr) <= 16); /* not too big to be on the stack */
168 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
169 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
171 /* Iterate through reserved ports. */
173 rc = libcfs_sock_connect(&sock, &fatal,
182 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
184 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
185 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
186 cr.acr_nid = peer_nid;
188 if (the_lnet.ln_testprotocompat != 0) {
189 /* single-shot proto check */
191 if ((the_lnet.ln_testprotocompat & 4) != 0) {
193 the_lnet.ln_testprotocompat &= ~4;
195 if ((the_lnet.ln_testprotocompat & 8) != 0) {
196 cr.acr_magic = LNET_PROTO_MAGIC;
197 the_lnet.ln_testprotocompat &= ~8;
202 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
215 libcfs_sock_release(sock);
217 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
220 EXPORT_SYMBOL(lnet_connect);
222 #else /* below is multi-threaded user-space code */
224 static char *accept_type = "secure";
227 lnet_acceptor_get_tunables()
230 char *env = getenv("LNET_ACCEPT");
235 rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT");
240 rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG");
245 rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT");
250 CDEBUG(D_NET, "accept_type = %s\n", accept_type);
251 CDEBUG(D_NET, "accept_port = %d\n", accept_port);
252 CDEBUG(D_NET, "accept_backlog = %d\n", accept_backlog);
253 CDEBUG(D_NET, "accept_timeout = %d\n", accept_timeout);
257 #endif /* __KERNEL__ */
259 /* Below is the code common for both kernel and MT user-space */
262 lnet_accept(cfs_socket_t *sock, __u32 magic)
264 lnet_acceptor_connreq_t cr;
272 LASSERT (sizeof(cr) <= 16); /* not too big for the stack */
274 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
275 LASSERT (rc == 0); /* we succeeded before */
277 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
279 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
280 /* future version compatibility!
281 * When LNET unifies protocols over all LNDs, the first
282 * thing sent will be a version query. I send back
283 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
285 memset (&cr, 0, sizeof(cr));
286 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
287 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
288 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
292 CERROR("Error sending magic+version in response"
293 "to LNET magic from %u.%u.%u.%u: %d\n",
294 HIPQUAD(peer_ip), rc);
298 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
299 str = "'old' socknal/tcpnal";
300 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
302 else if (lnet_accept_magic(magic, LNET_PROTO_OPENIB_MAGIC))
303 str = "'old' openibnal";
305 str = "unrecognised";
307 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
308 " magic %08x: %s acceptor protocol\n",
309 HIPQUAD(peer_ip), magic, str);
313 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
315 rc = libcfs_sock_read(sock, &cr.acr_version,
316 sizeof(cr.acr_version),
319 CERROR("Error %d reading connection request version from "
320 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
325 __swab32s(&cr.acr_version);
327 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
328 /* future version compatibility!
329 * An acceptor-specific protocol rev will first send a version
330 * query. I send back my current version to tell her I'm
332 int peer_version = cr.acr_version;
334 memset (&cr, 0, sizeof(cr));
335 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
336 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
338 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
342 CERROR("Error sending magic+version in response"
343 "to version %d from %u.%u.%u.%u: %d\n",
344 peer_version, HIPQUAD(peer_ip), rc);
348 rc = libcfs_sock_read(sock, &cr.acr_nid,
350 offsetof(lnet_acceptor_connreq_t, acr_nid),
353 CERROR("Error %d reading connection request from "
354 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
359 __swab64s(&cr.acr_nid);
361 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
362 if (ni == NULL || /* no matching net */
363 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
366 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
367 " for %s: No matching NI\n",
368 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
372 if (ni->ni_lnd->lnd_accept == NULL) {
373 /* This catches a request for the loopback LND */
375 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
376 " for %s: NI doesn not accept IP connections\n",
377 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
381 CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
382 libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
384 rc = ni->ni_lnd->lnd_accept(ni, sock);
391 lnet_acceptor(void *arg)
394 cfs_socket_t *newsock;
399 int secure = (int)((long_ptr_t)arg);
401 LASSERT (lnet_acceptor_state.pta_sock == NULL);
403 snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
407 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
408 0, accept_port, accept_backlog);
410 if (rc == -EADDRINUSE)
411 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
412 " %d: port already in use\n",
415 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
416 "%d: unexpected error %d\n",
419 lnet_acceptor_state.pta_sock = NULL;
422 LCONSOLE(0, "Accept %s, port %d\n", accept, accept_port);
424 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
428 /* set init status and unblock parent */
429 lnet_acceptor_state.pta_shutdown = rc;
430 cfs_complete(&lnet_acceptor_state.pta_signal);
435 while (!lnet_acceptor_state.pta_shutdown) {
437 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
440 CWARN("Accept error %d: pausing...\n", rc);
441 cfs_pause(cfs_time_seconds(1));
446 /* maybe we're waken up with libcfs_sock_abort_accept() */
447 if (lnet_acceptor_state.pta_shutdown) {
448 libcfs_sock_release(newsock);
452 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
454 CERROR("Can't determine new connection's address\n");
458 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
459 CERROR("Refusing connection from %u.%u.%u.%u: "
460 "insecure port %d\n",
461 HIPQUAD(peer_ip), peer_port);
465 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
468 CERROR("Error %d reading connection request from "
469 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
473 rc = lnet_accept(newsock, magic);
480 libcfs_sock_release(newsock);
483 libcfs_sock_release(lnet_acceptor_state.pta_sock);
484 lnet_acceptor_state.pta_sock = NULL;
486 LCONSOLE(0, "Acceptor stopping\n");
488 /* unblock lnet_acceptor_stop() */
489 cfs_complete(&lnet_acceptor_state.pta_signal);
494 accept2secure(const char *acc, long *sec)
496 if (!strcmp(acc, "secure")) {
499 } else if (!strcmp(acc, "all")) {
502 } else if (!strcmp(acc, "none")) {
505 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
512 lnet_acceptor_start(void)
518 LASSERT (lnet_acceptor_state.pta_sock == NULL);
521 /* kernel version uses CFS_MODULE_PARM */
522 rc = lnet_acceptor_get_tunables();
526 /* Do nothing if we're liblustre clients */
527 if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
530 cfs_init_completion(&lnet_acceptor_state.pta_signal);
533 rc = accept2secure(accept, &secure);
535 rc = accept2secure(accept_type, &secure);
538 cfs_fini_completion(&lnet_acceptor_state.pta_signal);
542 if (lnet_count_acceptor_nis() == 0) /* not required */
545 rc2 = cfs_create_thread(lnet_acceptor, (void *)(ulong_ptr_t)secure);
547 CERROR("Can't start acceptor thread: %d\n", rc);
548 cfs_fini_completion(&lnet_acceptor_state.pta_signal);
552 /* wait for acceptor to startup */
553 cfs_wait_for_completion(&lnet_acceptor_state.pta_signal);
555 if (!lnet_acceptor_state.pta_shutdown) {
557 LASSERT (lnet_acceptor_state.pta_sock != NULL);
561 LASSERT (lnet_acceptor_state.pta_sock == NULL);
562 cfs_fini_completion(&lnet_acceptor_state.pta_signal);
567 lnet_acceptor_stop(void)
569 if (lnet_acceptor_state.pta_sock == NULL) /* not running */
572 lnet_acceptor_state.pta_shutdown = 1;
573 libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
575 /* block until acceptor signals exit */
576 cfs_wait_for_completion(&lnet_acceptor_state.pta_signal);
578 cfs_fini_completion(&lnet_acceptor_state.pta_signal);
581 #else /* single-threaded user-space */
583 lnet_acceptor_start(void)
589 lnet_acceptor_stop(void)
592 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */