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, 2015, 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
39 #ifdef HAVE_COMPAT_RDMA
40 #include <linux/compat-2.6.h>
42 #include <linux/completion.h>
44 #include <lnet/lib-lnet.h>
46 static int accept_port = 988;
47 static int accept_backlog = 127;
48 static int accept_timeout = 5;
52 struct socket *pta_sock;
53 struct completion pta_signal;
54 } lnet_acceptor_state = {
59 lnet_acceptor_port(void)
65 lnet_accept_magic(__u32 magic, __u32 constant)
67 return (magic == constant ||
68 magic == __swab32(constant));
71 EXPORT_SYMBOL(lnet_acceptor_port);
73 static char *accept = "secure";
75 module_param(accept, charp, 0444);
76 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
77 module_param(accept_port, int, 0444);
78 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
79 module_param(accept_backlog, int, 0444);
80 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
81 module_param(accept_timeout, int, 0644);
82 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
84 static char *accept_type = NULL;
87 lnet_acceptor_get_tunables(void)
89 /* Userland acceptor uses 'accept_type' instead of 'accept', due to
90 * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
91 * for compatibility. Hence the trick. */
97 lnet_acceptor_timeout(void)
99 return accept_timeout;
101 EXPORT_SYMBOL(lnet_acceptor_timeout);
104 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
105 __u32 peer_ip, int peer_port)
108 /* "normal" errors */
110 CNETERR("Connection to %s at host %pI4h on port %d was "
111 "refused: check that Lustre is running on that node.\n",
112 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
116 CNETERR("Connection to %s at host %pI4h "
117 "was unreachable: the network or that node may "
118 "be down, or Lustre may be misconfigured.\n",
119 libcfs_nid2str(peer_nid), &peer_ip);
122 CNETERR("Connection to %s at host %pI4h on "
123 "port %d took too long: that node may be hung "
124 "or experiencing high load.\n",
125 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
128 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
129 " on port %d was reset: "
130 "is it running a compatible version of "
131 "Lustre and is %s one of its NIDs?\n",
132 libcfs_nid2str(peer_nid), &peer_ip,
133 peer_port, libcfs_nid2str(peer_nid));
136 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
137 "host %pI4h on port %d: is it running "
138 "a compatible version of Lustre?\n",
139 libcfs_nid2str(peer_nid), &peer_ip,
143 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
144 "connect to %s at host %pI4h on port "
145 "%d\n", libcfs_nid2str(peer_nid),
146 &peer_ip, peer_port);
149 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
150 " at host %pI4h on port %d\n", rc,
151 libcfs_nid2str(peer_nid),
152 &peer_ip, peer_port);
156 EXPORT_SYMBOL(lnet_connect_console_error);
159 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
160 __u32 local_ip, __u32 peer_ip, int peer_port)
162 lnet_acceptor_connreq_t cr;
168 CLASSERT (sizeof(cr) <= 16); /* not too big to be on the stack */
170 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
171 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
173 /* Iterate through reserved ports. */
175 rc = lnet_sock_connect(&sock, &fatal,
184 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
186 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
187 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
188 cr.acr_nid = peer_nid;
190 if (the_lnet.ln_testprotocompat != 0) {
191 /* single-shot proto check */
192 lnet_net_lock(LNET_LOCK_EX);
193 if ((the_lnet.ln_testprotocompat & 4) != 0) {
195 the_lnet.ln_testprotocompat &= ~4;
197 if ((the_lnet.ln_testprotocompat & 8) != 0) {
198 cr.acr_magic = LNET_PROTO_MAGIC;
199 the_lnet.ln_testprotocompat &= ~8;
201 lnet_net_unlock(LNET_LOCK_EX);
204 rc = lnet_sock_write(sock, &cr, sizeof(cr),
219 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
222 EXPORT_SYMBOL(lnet_connect);
225 lnet_accept(struct socket *sock, __u32 magic)
227 lnet_acceptor_connreq_t cr;
235 LASSERT (sizeof(cr) <= 16); /* not too big for the stack */
237 rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
238 LASSERT (rc == 0); /* we succeeded before */
240 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
242 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
243 /* future version compatibility!
244 * When LNET unifies protocols over all LNDs, the first
245 * thing sent will be a version query. I send back
246 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
248 memset (&cr, 0, sizeof(cr));
249 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
250 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
251 rc = lnet_sock_write(sock, &cr, sizeof(cr),
255 CERROR("Error sending magic+version in response"
256 "to LNET magic from %pI4h: %d\n",
261 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
262 str = "'old' socknal/tcpnal";
264 str = "unrecognised";
266 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
267 " magic %08x: %s acceptor protocol\n",
268 &peer_ip, magic, str);
272 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
274 rc = lnet_sock_read(sock, &cr.acr_version,
275 sizeof(cr.acr_version),
278 CERROR("Error %d reading connection request version from "
279 "%pI4h\n", rc, &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 = lnet_sock_write(sock, &cr, sizeof(cr),
301 CERROR("Error sending magic+version in response"
302 "to version %d from %pI4h: %d\n",
303 peer_version, &peer_ip, rc);
307 rc = lnet_sock_read(sock, &cr.acr_nid,
309 offsetof(lnet_acceptor_connreq_t, acr_nid),
312 CERROR("Error %d reading connection request from "
313 "%pI4h\n", rc, &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 %pI4h "
326 "for %s: No matching NI\n",
327 &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 %pI4h "
335 "for %s: NI doesn not accept IP connections\n",
336 &peer_ip, libcfs_nid2str(cr.acr_nid));
340 CDEBUG(D_NET, "Accept %s from %pI4h\n",
341 libcfs_nid2str(cr.acr_nid), &peer_ip);
343 rc = ni->ni_lnd->lnd_accept(ni, sock);
350 lnet_acceptor(void *arg)
352 struct socket *newsock;
357 int secure = (int)((uintptr_t)arg);
359 LASSERT (lnet_acceptor_state.pta_sock == NULL);
363 rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
364 0, accept_port, accept_backlog);
366 if (rc == -EADDRINUSE)
367 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
368 " %d: port already in use\n",
371 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
372 "%d: unexpected error %d\n",
375 lnet_acceptor_state.pta_sock = NULL;
377 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
380 /* set init status and unblock parent */
381 lnet_acceptor_state.pta_shutdown = rc;
382 complete(&lnet_acceptor_state.pta_signal);
387 while (!lnet_acceptor_state.pta_shutdown) {
389 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
392 CWARN("Accept error %d: pausing...\n", rc);
393 set_current_state(TASK_UNINTERRUPTIBLE);
394 schedule_timeout(cfs_time_seconds(1));
399 /* maybe we're waken up with lnet_sock_abort_accept() */
400 if (lnet_acceptor_state.pta_shutdown) {
401 sock_release(newsock);
405 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
407 CERROR("Can't determine new connection's address\n");
411 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
412 CERROR("Refusing connection from %pI4h: "
413 "insecure port %d\n", &peer_ip, peer_port);
417 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
420 CERROR("Error %d reading connection request from "
421 "%pI4h\n", rc, &peer_ip);
425 rc = lnet_accept(newsock, magic);
432 sock_release(newsock);
435 sock_release(lnet_acceptor_state.pta_sock);
436 lnet_acceptor_state.pta_sock = NULL;
438 CDEBUG(D_NET, "Acceptor stopping\n");
440 /* unblock lnet_acceptor_stop() */
441 complete(&lnet_acceptor_state.pta_signal);
446 accept2secure(const char *acc, long *sec)
448 if (!strcmp(acc, "secure")) {
451 } else if (!strcmp(acc, "all")) {
454 } else if (!strcmp(acc, "none")) {
457 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
464 lnet_acceptor_start(void)
466 struct task_struct *task;
471 /* if acceptor is already running return immediately */
472 if (!lnet_acceptor_state.pta_shutdown)
475 LASSERT (lnet_acceptor_state.pta_sock == NULL);
477 rc = lnet_acceptor_get_tunables();
481 init_completion(&lnet_acceptor_state.pta_signal);
482 rc = accept2secure(accept_type, &secure);
486 if (lnet_count_acceptor_nis() == 0) /* not required */
489 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
490 "acceptor_%03ld", secure);
493 CERROR("Can't start acceptor thread: %ld\n", rc2);
498 /* wait for acceptor to startup */
499 wait_for_completion(&lnet_acceptor_state.pta_signal);
501 if (!lnet_acceptor_state.pta_shutdown) {
503 LASSERT(lnet_acceptor_state.pta_sock != NULL);
507 LASSERT(lnet_acceptor_state.pta_sock == NULL);
513 lnet_acceptor_stop(void)
517 if (lnet_acceptor_state.pta_shutdown) /* not running */
520 lnet_acceptor_state.pta_shutdown = 1;
522 sk = lnet_acceptor_state.pta_sock->sk;
524 /* awake any sleepers using safe method */
525 sk->sk_state_change(sk);
527 /* block until acceptor signals exit */
528 wait_for_completion(&lnet_acceptor_state.pta_signal);