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, 2012, Intel Corporation.
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;
49 struct completion pta_signal;
50 } lnet_acceptor_state;
53 lnet_acceptor_port(void)
59 lnet_accept_magic(__u32 magic, __u32 constant)
61 return (magic == constant ||
62 magic == __swab32(constant));
67 EXPORT_SYMBOL(lnet_acceptor_port);
69 static char *accept = "secure";
71 CFS_MODULE_PARM(accept, "s", charp, 0444,
72 "Accept connections (secure|all|none)");
73 CFS_MODULE_PARM(accept_port, "i", int, 0444,
74 "Acceptor's port (same on all nodes)");
75 CFS_MODULE_PARM(accept_backlog, "i", int, 0444,
76 "Acceptor's listen backlog");
77 CFS_MODULE_PARM(accept_timeout, "i", int, 0644,
78 "Acceptor's timeout (seconds)");
80 static char *accept_type = NULL;
83 lnet_acceptor_get_tunables(void)
85 /* Userland acceptor uses 'accept_type' instead of 'accept', due to
86 * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
87 * for compatibility. Hence the trick. */
93 lnet_acceptor_timeout(void)
95 return accept_timeout;
97 EXPORT_SYMBOL(lnet_acceptor_timeout);
100 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
101 __u32 peer_ip, int peer_port)
104 /* "normal" errors */
106 CNETERR("Connection to %s at host %u.%u.%u.%u on port %d was "
107 "refused: check that Lustre is running on that node.\n",
108 libcfs_nid2str(peer_nid),
109 HIPQUAD(peer_ip), peer_port);
113 CNETERR("Connection to %s at host %u.%u.%u.%u "
114 "was unreachable: the network or that node may "
115 "be down, or Lustre may be misconfigured.\n",
116 libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
119 CNETERR("Connection to %s at host %u.%u.%u.%u on "
120 "port %d took too long: that node may be hung "
121 "or experiencing high load.\n",
122 libcfs_nid2str(peer_nid),
123 HIPQUAD(peer_ip), peer_port);
126 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
127 " on port %d was reset: "
128 "is it running a compatible version of "
129 "Lustre and is %s one of its NIDs?\n",
130 libcfs_nid2str(peer_nid),
131 HIPQUAD(peer_ip), peer_port,
132 libcfs_nid2str(peer_nid));
135 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
136 "host %u.%u.%u.%u on port %d: is it running "
137 "a compatible version of Lustre?\n",
138 libcfs_nid2str(peer_nid),
139 HIPQUAD(peer_ip), peer_port);
142 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
143 "connect to %s at host %u.%u.%u.%u on port "
144 "%d\n", libcfs_nid2str(peer_nid),
145 HIPQUAD(peer_ip), peer_port);
148 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
149 " at host %u.%u.%u.%u on port %d\n", rc,
150 libcfs_nid2str(peer_nid),
151 HIPQUAD(peer_ip), peer_port);
155 EXPORT_SYMBOL(lnet_connect_console_error);
158 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
159 __u32 local_ip, __u32 peer_ip, int peer_port)
161 lnet_acceptor_connreq_t cr;
167 CLASSERT (sizeof(cr) <= 16); /* not too big to be on the stack */
169 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
170 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
172 /* Iterate through reserved ports. */
174 rc = libcfs_sock_connect(&sock, &fatal,
183 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
185 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
186 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
187 cr.acr_nid = peer_nid;
189 if (the_lnet.ln_testprotocompat != 0) {
190 /* single-shot proto check */
191 lnet_net_lock(LNET_LOCK_EX);
192 if ((the_lnet.ln_testprotocompat & 4) != 0) {
194 the_lnet.ln_testprotocompat &= ~4;
196 if ((the_lnet.ln_testprotocompat & 8) != 0) {
197 cr.acr_magic = LNET_PROTO_MAGIC;
198 the_lnet.ln_testprotocompat &= ~8;
200 lnet_net_unlock(LNET_LOCK_EX);
203 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
216 libcfs_sock_release(sock);
218 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
221 EXPORT_SYMBOL(lnet_connect);
223 #else /* below is multi-threaded user-space code */
225 static char *accept_type = "secure";
228 lnet_acceptor_get_tunables()
231 char *env = getenv("LNET_ACCEPT");
236 rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT");
241 rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG");
246 rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT");
251 CDEBUG(D_NET, "accept_type = %s\n", accept_type);
252 CDEBUG(D_NET, "accept_port = %d\n", accept_port);
253 CDEBUG(D_NET, "accept_backlog = %d\n", accept_backlog);
254 CDEBUG(D_NET, "accept_timeout = %d\n", accept_timeout);
258 #endif /* __KERNEL__ */
260 /* Below is the code common for both kernel and MT user-space */
263 lnet_accept(cfs_socket_t *sock, __u32 magic)
265 lnet_acceptor_connreq_t cr;
273 LASSERT (sizeof(cr) <= 16); /* not too big for the stack */
275 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
276 LASSERT (rc == 0); /* we succeeded before */
278 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
280 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
281 /* future version compatibility!
282 * When LNET unifies protocols over all LNDs, the first
283 * thing sent will be a version query. I send back
284 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
286 memset (&cr, 0, sizeof(cr));
287 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
288 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
289 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
293 CERROR("Error sending magic+version in response"
294 "to LNET magic from %u.%u.%u.%u: %d\n",
295 HIPQUAD(peer_ip), rc);
299 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
300 str = "'old' socknal/tcpnal";
301 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
304 str = "unrecognised";
306 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
307 " magic %08x: %s acceptor protocol\n",
308 HIPQUAD(peer_ip), magic, str);
312 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
314 rc = libcfs_sock_read(sock, &cr.acr_version,
315 sizeof(cr.acr_version),
318 CERROR("Error %d reading connection request version from "
319 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
324 __swab32s(&cr.acr_version);
326 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
327 /* future version compatibility!
328 * An acceptor-specific protocol rev will first send a version
329 * query. I send back my current version to tell her I'm
331 int peer_version = cr.acr_version;
333 memset (&cr, 0, sizeof(cr));
334 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
335 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
337 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
341 CERROR("Error sending magic+version in response"
342 "to version %d from %u.%u.%u.%u: %d\n",
343 peer_version, HIPQUAD(peer_ip), rc);
347 rc = libcfs_sock_read(sock, &cr.acr_nid,
349 offsetof(lnet_acceptor_connreq_t, acr_nid),
352 CERROR("Error %d reading connection request from "
353 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
358 __swab64s(&cr.acr_nid);
360 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
361 if (ni == NULL || /* no matching net */
362 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
365 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
366 " for %s: No matching NI\n",
367 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
371 if (ni->ni_lnd->lnd_accept == NULL) {
372 /* This catches a request for the loopback LND */
374 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
375 " for %s: NI doesn not accept IP connections\n",
376 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
380 CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
381 libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
383 rc = ni->ni_lnd->lnd_accept(ni, sock);
390 lnet_acceptor(void *arg)
392 cfs_socket_t *newsock;
397 int secure = (int)((long_ptr_t)arg);
399 LASSERT (lnet_acceptor_state.pta_sock == NULL);
403 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
404 0, accept_port, accept_backlog);
406 if (rc == -EADDRINUSE)
407 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
408 " %d: port already in use\n",
411 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
412 "%d: unexpected error %d\n",
415 lnet_acceptor_state.pta_sock = NULL;
417 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
420 /* set init status and unblock parent */
421 lnet_acceptor_state.pta_shutdown = rc;
422 complete(&lnet_acceptor_state.pta_signal);
427 while (!lnet_acceptor_state.pta_shutdown) {
429 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
432 CWARN("Accept error %d: pausing...\n", rc);
433 cfs_pause(cfs_time_seconds(1));
438 /* maybe we're waken up with libcfs_sock_abort_accept() */
439 if (lnet_acceptor_state.pta_shutdown) {
440 libcfs_sock_release(newsock);
444 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
446 CERROR("Can't determine new connection's address\n");
450 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
451 CERROR("Refusing connection from %u.%u.%u.%u: "
452 "insecure port %d\n",
453 HIPQUAD(peer_ip), peer_port);
457 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
460 CERROR("Error %d reading connection request from "
461 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
465 rc = lnet_accept(newsock, magic);
472 libcfs_sock_release(newsock);
475 libcfs_sock_release(lnet_acceptor_state.pta_sock);
476 lnet_acceptor_state.pta_sock = NULL;
478 CDEBUG(D_NET, "Acceptor stopping\n");
480 /* unblock lnet_acceptor_stop() */
481 complete(&lnet_acceptor_state.pta_signal);
486 accept2secure(const char *acc, long *sec)
488 if (!strcmp(acc, "secure")) {
491 } else if (!strcmp(acc, "all")) {
494 } else if (!strcmp(acc, "none")) {
497 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
504 lnet_acceptor_start(void)
506 struct task_struct *task;
511 LASSERT (lnet_acceptor_state.pta_sock == NULL);
513 rc = lnet_acceptor_get_tunables();
518 /* Do nothing if we're liblustre clients */
519 if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
523 init_completion(&lnet_acceptor_state.pta_signal);
524 rc = accept2secure(accept_type, &secure);
526 fini_completion(&lnet_acceptor_state.pta_signal);
530 if (lnet_count_acceptor_nis() == 0) /* not required */
533 task = kthread_run(lnet_acceptor, (void *)(ulong_ptr_t)secure,
534 "acceptor_%03ld", secure);
537 CERROR("Can't start acceptor thread: %ld\n", rc2);
538 fini_completion(&lnet_acceptor_state.pta_signal);
543 /* wait for acceptor to startup */
544 wait_for_completion(&lnet_acceptor_state.pta_signal);
546 if (!lnet_acceptor_state.pta_shutdown) {
548 LASSERT(lnet_acceptor_state.pta_sock != NULL);
552 LASSERT(lnet_acceptor_state.pta_sock == NULL);
553 fini_completion(&lnet_acceptor_state.pta_signal);
559 lnet_acceptor_stop(void)
561 if (lnet_acceptor_state.pta_sock == NULL) /* not running */
564 lnet_acceptor_state.pta_shutdown = 1;
565 libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
567 /* block until acceptor signals exit */
568 wait_for_completion(&lnet_acceptor_state.pta_signal);
570 fini_completion(&lnet_acceptor_state.pta_signal);
573 #else /* single-threaded user-space */
575 lnet_acceptor_start(void)
581 lnet_acceptor_stop(void)
584 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */