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 "../../include/linux/lnet/lib-lnet.h"
41 static int accept_port = 988;
42 static int accept_backlog = 127;
43 static int accept_timeout = 5;
47 struct socket *pta_sock;
48 struct completion pta_signal;
49 } lnet_acceptor_state;
52 lnet_acceptor_port(void)
56 EXPORT_SYMBOL(lnet_acceptor_port);
59 lnet_accept_magic(__u32 magic, __u32 constant)
61 return (magic == constant ||
62 magic == __swab32(constant));
65 static char *accept = "secure";
67 module_param(accept, charp, 0444);
68 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
69 module_param(accept_port, int, 0444);
70 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
71 module_param(accept_backlog, int, 0444);
72 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
73 module_param(accept_timeout, int, 0644);
74 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
76 static char *accept_type;
79 lnet_acceptor_get_tunables(void)
81 /* Userland acceptor uses 'accept_type' instead of 'accept', due to
82 * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
83 * for compatibility. Hence the trick. */
89 lnet_acceptor_timeout(void)
91 return accept_timeout;
93 EXPORT_SYMBOL(lnet_acceptor_timeout);
96 lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
97 __u32 peer_ip, int peer_port)
100 /* "normal" errors */
102 CNETERR("Connection to %s at host %pI4h on port %d was refused: check that Lustre is running on that node.\n",
103 libcfs_nid2str(peer_nid),
104 &peer_ip, peer_port);
108 CNETERR("Connection to %s at host %pI4h was unreachable: the network or that node may be down, or Lustre may be misconfigured.\n",
109 libcfs_nid2str(peer_nid), &peer_ip);
112 CNETERR("Connection to %s at host %pI4h on port %d took too long: that node may be hung or experiencing high load.\n",
113 libcfs_nid2str(peer_nid),
114 &peer_ip, peer_port);
117 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h on port %d was reset: is it running a compatible version of Lustre and is %s one of its NIDs?\n",
118 libcfs_nid2str(peer_nid),
120 libcfs_nid2str(peer_nid));
123 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at host %pI4h on port %d: is it running a compatible version of Lustre?\n",
124 libcfs_nid2str(peer_nid),
125 &peer_ip, peer_port);
128 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to connect to %s at host %pI4h on port %d\n",
129 libcfs_nid2str(peer_nid),
130 &peer_ip, peer_port);
133 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s at host %pI4h on port %d\n",
134 rc, libcfs_nid2str(peer_nid),
135 &peer_ip, peer_port);
139 EXPORT_SYMBOL(lnet_connect_console_error);
142 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
143 __u32 local_ip, __u32 peer_ip, int peer_port)
145 lnet_acceptor_connreq_t cr;
151 CLASSERT(sizeof(cr) <= 16); /* not too big to be on the stack */
153 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
154 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
156 /* Iterate through reserved ports. */
158 rc = libcfs_sock_connect(&sock, &fatal,
167 CLASSERT(LNET_PROTO_ACCEPTOR_VERSION == 1);
169 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
170 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
171 cr.acr_nid = peer_nid;
173 if (the_lnet.ln_testprotocompat != 0) {
174 /* single-shot proto check */
175 lnet_net_lock(LNET_LOCK_EX);
176 if ((the_lnet.ln_testprotocompat & 4) != 0) {
178 the_lnet.ln_testprotocompat &= ~4;
180 if ((the_lnet.ln_testprotocompat & 8) != 0) {
181 cr.acr_magic = LNET_PROTO_MAGIC;
182 the_lnet.ln_testprotocompat &= ~8;
184 lnet_net_unlock(LNET_LOCK_EX);
187 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
200 libcfs_sock_release(sock);
202 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
205 EXPORT_SYMBOL(lnet_connect);
208 /* Below is the code common for both kernel and MT user-space */
211 lnet_accept(struct socket *sock, __u32 magic)
213 lnet_acceptor_connreq_t cr;
221 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
223 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
224 LASSERT(rc == 0); /* we succeeded before */
226 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
228 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
229 /* future version compatibility!
230 * When LNET unifies protocols over all LNDs, the first
231 * thing sent will be a version query. I send back
232 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
234 memset(&cr, 0, sizeof(cr));
235 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
236 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
237 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
241 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
246 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
247 str = "'old' socknal/tcpnal";
248 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
251 str = "unrecognised";
253 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h magic %08x: %s acceptor protocol\n",
254 &peer_ip, magic, str);
258 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
260 rc = libcfs_sock_read(sock, &cr.acr_version,
261 sizeof(cr.acr_version),
264 CERROR("Error %d reading connection request version from %pI4h\n",
270 __swab32s(&cr.acr_version);
272 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
273 /* future version compatibility!
274 * An acceptor-specific protocol rev will first send a version
275 * query. I send back my current version to tell her I'm
277 int peer_version = cr.acr_version;
279 memset(&cr, 0, sizeof(cr));
280 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
281 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
283 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
287 CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
288 peer_version, &peer_ip, rc);
292 rc = libcfs_sock_read(sock, &cr.acr_nid,
294 offsetof(lnet_acceptor_connreq_t, acr_nid),
297 CERROR("Error %d reading connection request from %pI4h\n",
303 __swab64s(&cr.acr_nid);
305 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
306 if (ni == NULL || /* no matching net */
307 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
310 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
311 &peer_ip, libcfs_nid2str(cr.acr_nid));
315 if (ni->ni_lnd->lnd_accept == NULL) {
316 /* This catches a request for the loopback LND */
318 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h for %s: NI doesn not accept IP connections\n",
319 &peer_ip, libcfs_nid2str(cr.acr_nid));
323 CDEBUG(D_NET, "Accept %s from %pI4h\n",
324 libcfs_nid2str(cr.acr_nid), &peer_ip);
326 rc = ni->ni_lnd->lnd_accept(ni, sock);
333 lnet_acceptor(void *arg)
335 struct socket *newsock;
340 int secure = (int)((long_ptr_t)arg);
342 LASSERT(lnet_acceptor_state.pta_sock == NULL);
346 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
347 0, accept_port, accept_backlog);
349 if (rc == -EADDRINUSE)
350 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
353 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
356 lnet_acceptor_state.pta_sock = NULL;
358 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
361 /* set init status and unblock parent */
362 lnet_acceptor_state.pta_shutdown = rc;
363 complete(&lnet_acceptor_state.pta_signal);
368 while (!lnet_acceptor_state.pta_shutdown) {
370 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
373 CWARN("Accept error %d: pausing...\n", rc);
374 set_current_state(TASK_UNINTERRUPTIBLE);
375 schedule_timeout(cfs_time_seconds(1));
380 /* maybe we're waken up with libcfs_sock_abort_accept() */
381 if (lnet_acceptor_state.pta_shutdown) {
382 libcfs_sock_release(newsock);
386 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
388 CERROR("Can't determine new connection's address\n");
392 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
393 CERROR("Refusing connection from %pI4h: insecure port %d\n",
394 &peer_ip, peer_port);
398 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
401 CERROR("Error %d reading connection request from %pI4h\n",
406 rc = lnet_accept(newsock, magic);
413 libcfs_sock_release(newsock);
416 libcfs_sock_release(lnet_acceptor_state.pta_sock);
417 lnet_acceptor_state.pta_sock = NULL;
419 CDEBUG(D_NET, "Acceptor stopping\n");
421 /* unblock lnet_acceptor_stop() */
422 complete(&lnet_acceptor_state.pta_signal);
427 accept2secure(const char *acc, long *sec)
429 if (!strcmp(acc, "secure")) {
432 } else if (!strcmp(acc, "all")) {
435 } else if (!strcmp(acc, "none")) {
439 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
445 lnet_acceptor_start(void)
451 LASSERT(lnet_acceptor_state.pta_sock == NULL);
453 rc = lnet_acceptor_get_tunables();
458 init_completion(&lnet_acceptor_state.pta_signal);
459 rc = accept2secure(accept_type, &secure);
463 if (lnet_count_acceptor_nis() == 0) /* not required */
466 rc2 = PTR_ERR(kthread_run(lnet_acceptor,
467 (void *)(ulong_ptr_t)secure,
468 "acceptor_%03ld", secure));
469 if (IS_ERR_VALUE(rc2)) {
470 CERROR("Can't start acceptor thread: %ld\n", rc2);
475 /* wait for acceptor to startup */
476 wait_for_completion(&lnet_acceptor_state.pta_signal);
478 if (!lnet_acceptor_state.pta_shutdown) {
480 LASSERT(lnet_acceptor_state.pta_sock != NULL);
484 LASSERT(lnet_acceptor_state.pta_sock == NULL);
490 lnet_acceptor_stop(void)
492 if (lnet_acceptor_state.pta_sock == NULL) /* not running */
495 lnet_acceptor_state.pta_shutdown = 1;
496 libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
498 /* block until acceptor signals exit */
499 wait_for_completion(&lnet_acceptor_state.pta_signal);