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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/completion.h>
36 #include <linux/lnet/lib-lnet.h>
38 static int accept_port = 988;
39 static int accept_backlog = 127;
40 static int accept_timeout = 5;
44 struct socket *pta_sock;
45 struct completion pta_signal;
46 } lnet_acceptor_state = {
51 lnet_acceptor_port(void)
55 EXPORT_SYMBOL(lnet_acceptor_port);
58 lnet_accept_magic(__u32 magic, __u32 constant)
60 return (magic == constant ||
61 magic == __swab32(constant));
64 static char *accept = "secure";
66 module_param(accept, charp, 0444);
67 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
68 module_param(accept_port, int, 0444);
69 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
70 module_param(accept_backlog, int, 0444);
71 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
72 module_param(accept_timeout, int, 0644);
73 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
75 static char *accept_type;
78 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.
90 lnet_acceptor_timeout(void)
92 return accept_timeout;
94 EXPORT_SYMBOL(lnet_acceptor_timeout);
97 lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
98 __u32 peer_ip, int peer_port)
101 /* "normal" errors */
103 CNETERR("Connection to %s at host %pI4h on port %d was refused: check that Lustre is running on that node.\n",
104 libcfs_nid2str(peer_nid),
105 &peer_ip, peer_port);
109 CNETERR("Connection to %s at host %pI4h was unreachable: the network or that node may be down, or Lustre may be misconfigured.\n",
110 libcfs_nid2str(peer_nid), &peer_ip);
113 CNETERR("Connection to %s at host %pI4h on port %d took too long: that node may be hung or experiencing high load.\n",
114 libcfs_nid2str(peer_nid),
115 &peer_ip, peer_port);
118 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",
119 libcfs_nid2str(peer_nid),
121 libcfs_nid2str(peer_nid));
124 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at host %pI4h on port %d: is it running a compatible version of Lustre?\n",
125 libcfs_nid2str(peer_nid),
126 &peer_ip, peer_port);
129 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to connect to %s at host %pI4h on port %d\n",
130 libcfs_nid2str(peer_nid),
131 &peer_ip, peer_port);
134 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s at host %pI4h on port %d\n",
135 rc, libcfs_nid2str(peer_nid),
136 &peer_ip, peer_port);
140 EXPORT_SYMBOL(lnet_connect_console_error);
143 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
144 __u32 local_ip, __u32 peer_ip, int peer_port)
146 struct lnet_acceptor_connreq cr;
152 BUILD_BUG_ON(sizeof(cr) > 16); /* too big to be on the stack */
154 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
155 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
157 /* Iterate through reserved ports. */
159 rc = lnet_sock_connect(&sock, &fatal, local_ip, port, peer_ip,
167 BUILD_BUG_ON(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) {
174 /* single-shot proto check */
175 lnet_net_lock(LNET_LOCK_EX);
176 if (the_lnet.ln_testprotocompat & 4) {
178 the_lnet.ln_testprotocompat &= ~4;
180 if (the_lnet.ln_testprotocompat & 8) {
181 cr.acr_magic = LNET_PROTO_MAGIC;
182 the_lnet.ln_testprotocompat &= ~8;
184 lnet_net_unlock(LNET_LOCK_EX);
187 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
201 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
204 EXPORT_SYMBOL(lnet_connect);
207 lnet_accept(struct socket *sock, __u32 magic)
209 struct lnet_acceptor_connreq cr;
217 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
219 rc = lnet_sock_getaddr(sock, 1, &peer_ip, &peer_port);
220 LASSERT(!rc); /* we succeeded before */
222 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
223 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
225 * future version compatibility!
226 * When LNET unifies protocols over all LNDs, the first
227 * thing sent will be a version query. I send back
228 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old"
230 memset(&cr, 0, sizeof(cr));
231 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
232 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
233 rc = lnet_sock_write(sock, &cr, sizeof(cr),
237 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
242 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
243 str = "'old' socknal/tcpnal";
245 str = "unrecognised";
247 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h magic %08x: %s acceptor protocol\n",
248 &peer_ip, magic, str);
252 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
254 rc = lnet_sock_read(sock, &cr.acr_version, sizeof(cr.acr_version),
257 CERROR("Error %d reading connection request version from %pI4h\n",
263 __swab32s(&cr.acr_version);
265 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
267 * future version compatibility!
268 * An acceptor-specific protocol rev will first send a version
269 * query. I send back my current version to tell her I'm
272 int peer_version = cr.acr_version;
274 memset(&cr, 0, sizeof(cr));
275 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
276 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
278 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
280 CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
281 peer_version, &peer_ip, rc);
285 rc = lnet_sock_read(sock, &cr.acr_nid,
287 offsetof(struct lnet_acceptor_connreq, acr_nid),
290 CERROR("Error %d reading connection request from %pI4h\n",
296 __swab64s(&cr.acr_nid);
298 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
299 if (!ni || /* no matching net */
300 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
303 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
304 &peer_ip, libcfs_nid2str(cr.acr_nid));
308 if (!ni->ni_lnd->lnd_accept) {
309 /* This catches a request for the loopback LND */
311 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h for %s: NI doesn not accept IP connections\n",
312 &peer_ip, libcfs_nid2str(cr.acr_nid));
316 CDEBUG(D_NET, "Accept %s from %pI4h\n",
317 libcfs_nid2str(cr.acr_nid), &peer_ip);
319 rc = ni->ni_lnd->lnd_accept(ni, sock);
326 lnet_acceptor(void *arg)
328 struct socket *newsock;
333 int secure = (int)((long)arg);
335 LASSERT(!lnet_acceptor_state.pta_sock);
339 rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock, 0, accept_port,
342 if (rc == -EADDRINUSE)
343 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
346 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
349 lnet_acceptor_state.pta_sock = NULL;
351 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
354 /* set init status and unblock parent */
355 lnet_acceptor_state.pta_shutdown = rc;
356 complete(&lnet_acceptor_state.pta_signal);
361 while (!lnet_acceptor_state.pta_shutdown) {
362 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
365 CWARN("Accept error %d: pausing...\n", rc);
366 set_current_state(TASK_UNINTERRUPTIBLE);
367 schedule_timeout(cfs_time_seconds(1));
372 /* maybe the LNet acceptor thread has been waken */
373 if (lnet_acceptor_state.pta_shutdown) {
374 sock_release(newsock);
378 rc = lnet_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
380 CERROR("Can't determine new connection's address\n");
384 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
385 CERROR("Refusing connection from %pI4h: insecure port %d\n",
386 &peer_ip, peer_port);
390 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
393 CERROR("Error %d reading connection request from %pI4h\n",
398 rc = lnet_accept(newsock, magic);
405 sock_release(newsock);
408 sock_release(lnet_acceptor_state.pta_sock);
409 lnet_acceptor_state.pta_sock = NULL;
411 CDEBUG(D_NET, "Acceptor stopping\n");
413 /* unblock lnet_acceptor_stop() */
414 complete(&lnet_acceptor_state.pta_signal);
419 accept2secure(const char *acc, long *sec)
421 if (!strcmp(acc, "secure")) {
424 } else if (!strcmp(acc, "all")) {
427 } else if (!strcmp(acc, "none")) {
431 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
437 lnet_acceptor_start(void)
439 struct task_struct *task;
444 /* if acceptor is already running return immediately */
445 if (!lnet_acceptor_state.pta_shutdown)
448 LASSERT(!lnet_acceptor_state.pta_sock);
450 rc = lnet_acceptor_get_tunables();
454 init_completion(&lnet_acceptor_state.pta_signal);
455 rc = accept2secure(accept_type, &secure);
459 if (!lnet_count_acceptor_nis()) /* not required */
462 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
463 "acceptor_%03ld", secure);
466 CERROR("Can't start acceptor thread: %ld\n", rc2);
471 /* wait for acceptor to startup */
472 wait_for_completion(&lnet_acceptor_state.pta_signal);
474 if (!lnet_acceptor_state.pta_shutdown) {
476 LASSERT(lnet_acceptor_state.pta_sock);
480 LASSERT(!lnet_acceptor_state.pta_sock);
486 lnet_acceptor_stop(void)
490 if (lnet_acceptor_state.pta_shutdown) /* not running */
493 lnet_acceptor_state.pta_shutdown = 1;
495 sk = lnet_acceptor_state.pta_sock->sk;
497 /* awake any sleepers using safe method */
498 sk->sk_state_change(sk);
500 /* block until acceptor signals exit */
501 wait_for_completion(&lnet_acceptor_state.pta_signal);