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 (c) 2007, 2010, Oracle and/or its affiliates. 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 cfs_completion_t pta_signal;
52 cfs_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 cfs_mt_init_completion(c) cfs_init_completion(c)
72 #define cfs_mt_wait_for_completion(c) cfs_wait_for_completion(c)
73 #define cfs_mt_complete(c) cfs_complete(c)
74 #define cfs_mt_fini_completion(c) cfs_fini_completion(c)
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)");
90 static char *accept_type = NULL;
93 lnet_acceptor_get_tunables(void)
95 /* Userland acceptor uses 'accept_type' instead of 'accept', due to
96 * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
97 * for compatibility. Hence the trick. */
103 lnet_acceptor_timeout(void)
105 return accept_timeout;
107 EXPORT_SYMBOL(lnet_acceptor_timeout);
110 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
111 __u32 peer_ip, int peer_port)
114 /* "normal" errors */
116 CNETERR("Connection to %s at host %u.%u.%u.%u on port %d was "
117 "refused: check that Lustre is running on that node.\n",
118 libcfs_nid2str(peer_nid),
119 HIPQUAD(peer_ip), peer_port);
123 CNETERR("Connection to %s at host %u.%u.%u.%u "
124 "was unreachable: the network or that node may "
125 "be down, or Lustre may be misconfigured.\n",
126 libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
129 CNETERR("Connection to %s at host %u.%u.%u.%u on "
130 "port %d took too long: that node may be hung "
131 "or experiencing high load.\n",
132 libcfs_nid2str(peer_nid),
133 HIPQUAD(peer_ip), peer_port);
136 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
137 " on port %d was reset: "
138 "is it running a compatible version of "
139 "Lustre and is %s one of its NIDs?\n",
140 libcfs_nid2str(peer_nid),
141 HIPQUAD(peer_ip), peer_port,
142 libcfs_nid2str(peer_nid));
145 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
146 "host %u.%u.%u.%u on port %d: is it running "
147 "a compatible version of Lustre?\n",
148 libcfs_nid2str(peer_nid),
149 HIPQUAD(peer_ip), peer_port);
152 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
153 "connect to %s at host %u.%u.%u.%u on port "
154 "%d\n", libcfs_nid2str(peer_nid),
155 HIPQUAD(peer_ip), peer_port);
158 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
159 " at host %u.%u.%u.%u on port %d\n", rc,
160 libcfs_nid2str(peer_nid),
161 HIPQUAD(peer_ip), peer_port);
165 EXPORT_SYMBOL(lnet_connect_console_error);
168 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
169 __u32 local_ip, __u32 peer_ip, int peer_port)
171 lnet_acceptor_connreq_t cr;
177 CLASSERT (sizeof(cr) <= 16); /* not too big to be on the stack */
179 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
180 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
182 /* Iterate through reserved ports. */
184 rc = libcfs_sock_connect(&sock, &fatal,
193 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
195 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
196 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
197 cr.acr_nid = peer_nid;
199 if (the_lnet.ln_testprotocompat != 0) {
200 /* single-shot proto check */
202 if ((the_lnet.ln_testprotocompat & 4) != 0) {
204 the_lnet.ln_testprotocompat &= ~4;
206 if ((the_lnet.ln_testprotocompat & 8) != 0) {
207 cr.acr_magic = LNET_PROTO_MAGIC;
208 the_lnet.ln_testprotocompat &= ~8;
213 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
226 libcfs_sock_release(sock);
228 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
231 EXPORT_SYMBOL(lnet_connect);
233 #else /* below is multi-threaded user-space code */
235 static char *accept_type = "secure";
238 lnet_acceptor_get_tunables()
241 char *env = getenv("LNET_ACCEPT");
246 rc = lnet_parse_int_tunable(&accept_port, "LNET_ACCEPT_PORT");
251 rc = lnet_parse_int_tunable(&accept_backlog, "LNET_ACCEPT_BACKLOG");
256 rc = lnet_parse_int_tunable(&accept_timeout, "LNET_ACCEPT_TIMEOUT");
261 CDEBUG(D_NET, "accept_type = %s\n", accept_type);
262 CDEBUG(D_NET, "accept_port = %d\n", accept_port);
263 CDEBUG(D_NET, "accept_backlog = %d\n", accept_backlog);
264 CDEBUG(D_NET, "accept_timeout = %d\n", accept_timeout);
268 #endif /* __KERNEL__ */
270 /* Below is the code common for both kernel and MT user-space */
273 lnet_accept(cfs_socket_t *sock, __u32 magic)
275 lnet_acceptor_connreq_t cr;
283 LASSERT (sizeof(cr) <= 16); /* not too big for the stack */
285 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
286 LASSERT (rc == 0); /* we succeeded before */
288 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
290 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
291 /* future version compatibility!
292 * When LNET unifies protocols over all LNDs, the first
293 * thing sent will be a version query. I send back
294 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
296 memset (&cr, 0, sizeof(cr));
297 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
298 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
299 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
303 CERROR("Error sending magic+version in response"
304 "to LNET magic from %u.%u.%u.%u: %d\n",
305 HIPQUAD(peer_ip), rc);
309 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
310 str = "'old' socknal/tcpnal";
311 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
314 str = "unrecognised";
316 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
317 " magic %08x: %s acceptor protocol\n",
318 HIPQUAD(peer_ip), magic, str);
322 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
324 rc = libcfs_sock_read(sock, &cr.acr_version,
325 sizeof(cr.acr_version),
328 CERROR("Error %d reading connection request version from "
329 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
334 __swab32s(&cr.acr_version);
336 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
337 /* future version compatibility!
338 * An acceptor-specific protocol rev will first send a version
339 * query. I send back my current version to tell her I'm
341 int peer_version = cr.acr_version;
343 memset (&cr, 0, sizeof(cr));
344 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
345 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
347 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
351 CERROR("Error sending magic+version in response"
352 "to version %d from %u.%u.%u.%u: %d\n",
353 peer_version, HIPQUAD(peer_ip), rc);
357 rc = libcfs_sock_read(sock, &cr.acr_nid,
359 offsetof(lnet_acceptor_connreq_t, acr_nid),
362 CERROR("Error %d reading connection request from "
363 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
368 __swab64s(&cr.acr_nid);
370 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
371 if (ni == NULL || /* no matching net */
372 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
375 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
376 " for %s: No matching NI\n",
377 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
381 if (ni->ni_lnd->lnd_accept == NULL) {
382 /* This catches a request for the loopback LND */
384 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
385 " for %s: NI doesn not accept IP connections\n",
386 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
390 CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
391 libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
393 rc = ni->ni_lnd->lnd_accept(ni, sock);
400 lnet_acceptor(void *arg)
403 cfs_socket_t *newsock;
408 int secure = (int)((long_ptr_t)arg);
410 LASSERT (lnet_acceptor_state.pta_sock == NULL);
412 snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
416 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
417 0, accept_port, accept_backlog);
419 if (rc == -EADDRINUSE)
420 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
421 " %d: port already in use\n",
424 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
425 "%d: unexpected error %d\n",
428 lnet_acceptor_state.pta_sock = NULL;
430 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
433 /* set init status and unblock parent */
434 lnet_acceptor_state.pta_shutdown = rc;
435 cfs_mt_complete(&lnet_acceptor_state.pta_signal);
440 while (!lnet_acceptor_state.pta_shutdown) {
442 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
445 CWARN("Accept error %d: pausing...\n", rc);
446 cfs_pause(cfs_time_seconds(1));
451 /* maybe we're waken up with libcfs_sock_abort_accept() */
452 if (lnet_acceptor_state.pta_shutdown) {
453 libcfs_sock_release(newsock);
457 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
459 CERROR("Can't determine new connection's address\n");
463 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
464 CERROR("Refusing connection from %u.%u.%u.%u: "
465 "insecure port %d\n",
466 HIPQUAD(peer_ip), peer_port);
470 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
473 CERROR("Error %d reading connection request from "
474 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
478 rc = lnet_accept(newsock, magic);
485 libcfs_sock_release(newsock);
488 libcfs_sock_release(lnet_acceptor_state.pta_sock);
489 lnet_acceptor_state.pta_sock = NULL;
491 LCONSOLE(0, "Acceptor stopping\n");
493 /* unblock lnet_acceptor_stop() */
494 cfs_mt_complete(&lnet_acceptor_state.pta_signal);
499 accept2secure(const char *acc, long *sec)
501 if (!strcmp(acc, "secure")) {
504 } else if (!strcmp(acc, "all")) {
507 } else if (!strcmp(acc, "none")) {
510 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
517 lnet_acceptor_start(void)
523 LASSERT (lnet_acceptor_state.pta_sock == NULL);
525 rc = lnet_acceptor_get_tunables();
530 /* Do nothing if we're liblustre clients */
531 if ((the_lnet.ln_pid & LNET_PID_USERFLAG) != 0)
535 cfs_mt_init_completion(&lnet_acceptor_state.pta_signal);
536 rc = accept2secure(accept_type, &secure);
538 cfs_mt_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_mt_fini_completion(&lnet_acceptor_state.pta_signal);
553 /* wait for acceptor to startup */
554 cfs_mt_wait_for_completion(&lnet_acceptor_state.pta_signal);
556 if (!lnet_acceptor_state.pta_shutdown) {
558 LASSERT (lnet_acceptor_state.pta_sock != NULL);
562 LASSERT (lnet_acceptor_state.pta_sock == NULL);
563 cfs_mt_fini_completion(&lnet_acceptor_state.pta_signal);
569 lnet_acceptor_stop(void)
571 if (lnet_acceptor_state.pta_sock == NULL) /* not running */
574 lnet_acceptor_state.pta_shutdown = 1;
575 libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
577 /* block until acceptor signals exit */
578 cfs_mt_wait_for_completion(&lnet_acceptor_state.pta_signal);
580 cfs_mt_fini_completion(&lnet_acceptor_state.pta_signal);
583 #else /* single-threaded user-space */
585 lnet_acceptor_start(void)
591 lnet_acceptor_stop(void)
594 #endif /* defined(__KERNEL__) || defined(HAVE_LIBPTHREAD) */