4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
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 completion pta_signal;
52 mt_completion_t 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 mt_init_completion(c) init_completion(c)
72 #define mt_wait_for_completion(c) wait_for_completion(c)
73 #define mt_complete(c) complete(c)
74 #define mt_fini_completion(c) fini_completion(c)
76 EXPORT_SYMBOL(lnet_acceptor_port);
78 static char *accept = "secure";
80 CFS_MODULE_PARM(accept, "s", charp, 0444,
81 "Accept connections (secure|all|none)");
82 CFS_MODULE_PARM(accept_port, "i", int, 0444,
83 "Acceptor's port (same on all nodes)");
84 CFS_MODULE_PARM(accept_backlog, "i", int, 0444,
85 "Acceptor's listen backlog");
86 CFS_MODULE_PARM(accept_timeout, "i", int, 0644,
87 "Acceptor's timeout (seconds)");
89 static char *accept_type = NULL;
92 lnet_acceptor_get_tunables(void)
94 /* Userland acceptor uses 'accept_type' instead of 'accept', due to
95 * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
96 * for compatibility. Hence the trick. */
102 lnet_acceptor_timeout(void)
104 return accept_timeout;
106 EXPORT_SYMBOL(lnet_acceptor_timeout);
109 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
110 __u32 peer_ip, int peer_port)
113 /* "normal" errors */
115 CNETERR("Connection to %s at host %u.%u.%u.%u on port %d was "
116 "refused: check that Lustre is running on that node.\n",
117 libcfs_nid2str(peer_nid),
118 HIPQUAD(peer_ip), peer_port);
122 CNETERR("Connection to %s at host %u.%u.%u.%u "
123 "was unreachable: the network or that node may "
124 "be down, or Lustre may be misconfigured.\n",
125 libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
128 CNETERR("Connection to %s at host %u.%u.%u.%u on "
129 "port %d took too long: that node may be hung "
130 "or experiencing high load.\n",
131 libcfs_nid2str(peer_nid),
132 HIPQUAD(peer_ip), peer_port);
135 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
136 " on port %d was reset: "
137 "is it running a compatible version of "
138 "Lustre and is %s one of its NIDs?\n",
139 libcfs_nid2str(peer_nid),
140 HIPQUAD(peer_ip), peer_port,
141 libcfs_nid2str(peer_nid));
144 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
145 "host %u.%u.%u.%u on port %d: is it running "
146 "a compatible version of Lustre?\n",
147 libcfs_nid2str(peer_nid),
148 HIPQUAD(peer_ip), peer_port);
151 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
152 "connect to %s at host %u.%u.%u.%u on port "
153 "%d\n", libcfs_nid2str(peer_nid),
154 HIPQUAD(peer_ip), peer_port);
157 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
158 " at host %u.%u.%u.%u on port %d\n", rc,
159 libcfs_nid2str(peer_nid),
160 HIPQUAD(peer_ip), peer_port);
164 EXPORT_SYMBOL(lnet_connect_console_error);
167 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
168 __u32 local_ip, __u32 peer_ip, int peer_port)
170 lnet_acceptor_connreq_t cr;
176 CLASSERT (sizeof(cr) <= 16); /* not too big to be on the stack */
178 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
179 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
181 /* Iterate through reserved ports. */
183 rc = libcfs_sock_connect(&sock, &fatal,
192 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
194 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
195 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
196 cr.acr_nid = peer_nid;
198 if (the_lnet.ln_testprotocompat != 0) {
199 /* single-shot proto check */
200 lnet_net_lock(LNET_LOCK_EX);
201 if ((the_lnet.ln_testprotocompat & 4) != 0) {
203 the_lnet.ln_testprotocompat &= ~4;
205 if ((the_lnet.ln_testprotocompat & 8) != 0) {
206 cr.acr_magic = LNET_PROTO_MAGIC;
207 the_lnet.ln_testprotocompat &= ~8;
209 lnet_net_unlock(LNET_LOCK_EX);
212 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
225 libcfs_sock_release(sock);
227 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
230 EXPORT_SYMBOL(lnet_connect);
232 #else /* below is multi-threaded user-space code */
234 static char *accept_type = "secure";
237 lnet_acceptor_get_tunables()
240 char *env = getenv("LNET_ACCEPT");
245 rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT");
250 rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG");
255 rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT");
260 CDEBUG(D_NET, "accept_type = %s\n", accept_type);
261 CDEBUG(D_NET, "accept_port = %d\n", accept_port);
262 CDEBUG(D_NET, "accept_backlog = %d\n", accept_backlog);
263 CDEBUG(D_NET, "accept_timeout = %d\n", accept_timeout);
267 #endif /* __KERNEL__ */
269 /* Below is the code common for both kernel and MT user-space */
272 lnet_accept(cfs_socket_t *sock, __u32 magic)
274 lnet_acceptor_connreq_t cr;
282 LASSERT (sizeof(cr) <= 16); /* not too big for the stack */
284 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
285 LASSERT (rc == 0); /* we succeeded before */
287 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
289 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
290 /* future version compatibility!
291 * When LNET unifies protocols over all LNDs, the first
292 * thing sent will be a version query. I send back
293 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
295 memset (&cr, 0, sizeof(cr));
296 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
297 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
298 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
302 CERROR("Error sending magic+version in response"
303 "to LNET magic from %u.%u.%u.%u: %d\n",
304 HIPQUAD(peer_ip), rc);
308 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
309 str = "'old' socknal/tcpnal";
310 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
313 str = "unrecognised";
315 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
316 " magic %08x: %s acceptor protocol\n",
317 HIPQUAD(peer_ip), magic, str);
321 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
323 rc = libcfs_sock_read(sock, &cr.acr_version,
324 sizeof(cr.acr_version),
327 CERROR("Error %d reading connection request version from "
328 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
333 __swab32s(&cr.acr_version);
335 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
336 /* future version compatibility!
337 * An acceptor-specific protocol rev will first send a version
338 * query. I send back my current version to tell her I'm
340 int peer_version = cr.acr_version;
342 memset (&cr, 0, sizeof(cr));
343 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
344 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
346 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
350 CERROR("Error sending magic+version in response"
351 "to version %d from %u.%u.%u.%u: %d\n",
352 peer_version, HIPQUAD(peer_ip), rc);
356 rc = libcfs_sock_read(sock, &cr.acr_nid,
358 offsetof(lnet_acceptor_connreq_t, acr_nid),
361 CERROR("Error %d reading connection request from "
362 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
367 __swab64s(&cr.acr_nid);
369 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
370 if (ni == NULL || /* no matching net */
371 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
374 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
375 " for %s: No matching NI\n",
376 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
380 if (ni->ni_lnd->lnd_accept == NULL) {
381 /* This catches a request for the loopback LND */
383 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
384 " for %s: NI doesn not accept IP connections\n",
385 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
389 CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
390 libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
392 rc = ni->ni_lnd->lnd_accept(ni, sock);
399 lnet_acceptor(void *arg)
402 cfs_socket_t *newsock;
407 int secure = (int)((long_ptr_t)arg);
409 LASSERT (lnet_acceptor_state.pta_sock == NULL);
411 snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
415 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
416 0, accept_port, accept_backlog);
418 if (rc == -EADDRINUSE)
419 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
420 " %d: port already in use\n",
423 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
424 "%d: unexpected error %d\n",
427 lnet_acceptor_state.pta_sock = NULL;
429 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
432 /* set init status and unblock parent */
433 lnet_acceptor_state.pta_shutdown = rc;
434 mt_complete(&lnet_acceptor_state.pta_signal);
439 while (!lnet_acceptor_state.pta_shutdown) {
441 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
444 CWARN("Accept error %d: pausing...\n", rc);
445 cfs_pause(cfs_time_seconds(1));
450 /* maybe we're waken up with libcfs_sock_abort_accept() */
451 if (lnet_acceptor_state.pta_shutdown) {
452 libcfs_sock_release(newsock);
456 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
458 CERROR("Can't determine new connection's address\n");
462 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
463 CERROR("Refusing connection from %u.%u.%u.%u: "
464 "insecure port %d\n",
465 HIPQUAD(peer_ip), peer_port);
469 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
472 CERROR("Error %d reading connection request from "
473 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
477 rc = lnet_accept(newsock, magic);
484 libcfs_sock_release(newsock);
487 libcfs_sock_release(lnet_acceptor_state.pta_sock);
488 lnet_acceptor_state.pta_sock = NULL;
490 CDEBUG(D_NET, "Acceptor stopping\n");
492 /* unblock lnet_acceptor_stop() */
493 mt_complete(&lnet_acceptor_state.pta_signal);
498 accept2secure(const char *acc, long *sec)
500 if (!strcmp(acc, "secure")) {
503 } else if (!strcmp(acc, "all")) {
506 } else if (!strcmp(acc, "none")) {
509 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
516 lnet_acceptor_start(void)
522 LASSERT (lnet_acceptor_state.pta_sock == NULL);
524 rc = lnet_acceptor_get_tunables();
529 /* Do nothing if we're liblustre clients */
530 if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
534 mt_init_completion(&lnet_acceptor_state.pta_signal);
535 rc = accept2secure(accept_type, &secure);
537 mt_fini_completion(&lnet_acceptor_state.pta_signal);
541 if (lnet_count_acceptor_nis() == 0) /* not required */
544 rc2 = cfs_create_thread(lnet_acceptor, (void *)(ulong_ptr_t)secure, 0);
546 CERROR("Can't start acceptor thread: %d\n", rc);
547 mt_fini_completion(&lnet_acceptor_state.pta_signal);
552 /* wait for acceptor to startup */
553 mt_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 mt_fini_completion(&lnet_acceptor_state.pta_signal);
568 lnet_acceptor_stop(void)
570 if (lnet_acceptor_state.pta_sock == NULL) /* not running */
573 lnet_acceptor_state.pta_shutdown = 1;
574 libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
576 /* block until acceptor signals exit */
577 mt_wait_for_completion(&lnet_acceptor_state.pta_signal);
579 mt_fini_completion(&lnet_acceptor_state.pta_signal);
582 #else /* single-threaded user-space */
584 lnet_acceptor_start(void)
590 lnet_acceptor_stop(void)
593 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */