1 // SPDX-License-Identifier: GPL-2.0
5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 only,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License version 2 for more details (a copy is included
15 * in the LICENSE file that accompanied this code).
17 * You should have received a copy of the GNU General Public License
18 * version 2 along with this program; If not, see
19 * http://www.gnu.org/licenses/gpl-2.0.html
24 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Use is subject to license terms.
27 * Copyright (c) 2011, 2015, Intel Corporation.
30 * This file is part of Lustre, http://www.lustre.org/
31 * Lustre is a trademark of Sun Microsystems, Inc.
34 #define DEBUG_SUBSYSTEM S_LNET
35 #include <linux/completion.h>
37 #include <linux/lnet/lib-lnet.h>
39 static int accept_port = 988;
40 static int accept_backlog = 127;
41 static int accept_timeout = 5;
45 struct socket *pta_sock;
46 struct completion pta_signal;
47 } 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)
82 * Userland acceptor uses 'accept_type' instead of 'accept', due to
83 * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
84 * for compatibility. Hence the trick.
91 lnet_acceptor_timeout(void)
93 return accept_timeout;
95 EXPORT_SYMBOL(lnet_acceptor_timeout);
98 lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
99 __u32 peer_ip, int peer_port)
102 /* "normal" errors */
104 CNETERR("Connection to %s at host %pI4h on port %d was refused: check that Lustre is running on that node.\n",
105 libcfs_nid2str(peer_nid),
106 &peer_ip, peer_port);
110 CNETERR("Connection to %s at host %pI4h was unreachable: the network or that node may be down, or Lustre may be misconfigured.\n",
111 libcfs_nid2str(peer_nid), &peer_ip);
114 CNETERR("Connection to %s at host %pI4h on port %d took too long: that node may be hung or experiencing high load.\n",
115 libcfs_nid2str(peer_nid),
116 &peer_ip, peer_port);
119 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",
120 libcfs_nid2str(peer_nid),
122 libcfs_nid2str(peer_nid));
125 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at host %pI4h on port %d: is it running a compatible version of Lustre?\n",
126 libcfs_nid2str(peer_nid),
127 &peer_ip, peer_port);
130 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to connect to %s at host %pI4h on port %d\n",
131 libcfs_nid2str(peer_nid),
132 &peer_ip, peer_port);
135 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s at host %pI4h on port %d\n",
136 rc, libcfs_nid2str(peer_nid),
137 &peer_ip, peer_port);
141 EXPORT_SYMBOL(lnet_connect_console_error);
144 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
145 __u32 local_ip, __u32 peer_ip, int peer_port)
147 struct lnet_acceptor_connreq cr;
153 BUILD_BUG_ON(sizeof(cr) > 16); /* too big to be on the stack */
155 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
156 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
158 /* Iterate through reserved ports. */
160 rc = lnet_sock_connect(&sock, &fatal, local_ip, port, peer_ip,
168 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
170 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
171 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
172 cr.acr_nid = peer_nid;
174 if (the_lnet.ln_testprotocompat) {
175 /* single-shot proto check */
176 lnet_net_lock(LNET_LOCK_EX);
177 if (the_lnet.ln_testprotocompat & 4) {
179 the_lnet.ln_testprotocompat &= ~4;
181 if (the_lnet.ln_testprotocompat & 8) {
182 cr.acr_magic = LNET_PROTO_MAGIC;
183 the_lnet.ln_testprotocompat &= ~8;
185 lnet_net_unlock(LNET_LOCK_EX);
188 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
202 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
205 EXPORT_SYMBOL(lnet_connect);
208 lnet_accept(struct socket *sock, __u32 magic)
210 struct lnet_acceptor_connreq cr;
218 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
220 rc = lnet_sock_getaddr(sock, 1, &peer_ip, &peer_port);
221 LASSERT(!rc); /* we succeeded before */
223 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
224 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
226 * future version compatibility!
227 * When LNET unifies protocols over all LNDs, the first
228 * thing sent will be a version query. I send back
229 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old"
231 memset(&cr, 0, sizeof(cr));
232 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
233 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
234 rc = lnet_sock_write(sock, &cr, sizeof(cr),
238 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
243 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
244 str = "'old' socknal/tcpnal";
246 str = "unrecognised";
248 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h magic %08x: %s acceptor protocol\n",
249 &peer_ip, magic, str);
253 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
255 rc = lnet_sock_read(sock, &cr.acr_version, sizeof(cr.acr_version),
258 CERROR("Error %d reading connection request version from %pI4h\n",
264 __swab32s(&cr.acr_version);
266 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
268 * future version compatibility!
269 * An acceptor-specific protocol rev will first send a version
270 * query. I send back my current version to tell her I'm
273 int peer_version = cr.acr_version;
275 memset(&cr, 0, sizeof(cr));
276 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
277 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
279 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
281 CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
282 peer_version, &peer_ip, rc);
286 rc = lnet_sock_read(sock, &cr.acr_nid,
288 offsetof(struct lnet_acceptor_connreq, acr_nid),
291 CERROR("Error %d reading connection request from %pI4h\n",
297 __swab64s(&cr.acr_nid);
299 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
300 if (!ni || /* no matching net */
301 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
304 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
305 &peer_ip, libcfs_nid2str(cr.acr_nid));
309 if (!ni->ni_lnd->lnd_accept) {
310 /* This catches a request for the loopback LND */
312 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h for %s: NI doesn not accept IP connections\n",
313 &peer_ip, libcfs_nid2str(cr.acr_nid));
317 CDEBUG(D_NET, "Accept %s from %pI4h\n",
318 libcfs_nid2str(cr.acr_nid), &peer_ip);
320 rc = ni->ni_lnd->lnd_accept(ni, sock);
327 lnet_acceptor(void *arg)
329 struct socket *newsock;
334 int secure = (int)((long)arg);
336 LASSERT(!lnet_acceptor_state.pta_sock);
340 rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock, 0, accept_port,
343 if (rc == -EADDRINUSE)
344 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
347 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
350 lnet_acceptor_state.pta_sock = NULL;
352 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
355 /* set init status and unblock parent */
356 lnet_acceptor_state.pta_shutdown = rc;
357 complete(&lnet_acceptor_state.pta_signal);
362 while (!lnet_acceptor_state.pta_shutdown) {
363 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
366 CWARN("Accept error %d: pausing...\n", rc);
367 set_current_state(TASK_UNINTERRUPTIBLE);
368 schedule_timeout(cfs_time_seconds(1));
373 /* maybe the LNet acceptor thread has been waken */
374 if (lnet_acceptor_state.pta_shutdown) {
375 sock_release(newsock);
379 rc = lnet_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
381 CERROR("Can't determine new connection's address\n");
385 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
386 CERROR("Refusing connection from %pI4h: insecure port %d\n",
387 &peer_ip, peer_port);
391 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
394 CERROR("Error %d reading connection request from %pI4h\n",
399 rc = lnet_accept(newsock, magic);
406 sock_release(newsock);
409 sock_release(lnet_acceptor_state.pta_sock);
410 lnet_acceptor_state.pta_sock = NULL;
412 CDEBUG(D_NET, "Acceptor stopping\n");
414 /* unblock lnet_acceptor_stop() */
415 complete(&lnet_acceptor_state.pta_signal);
420 accept2secure(const char *acc, long *sec)
422 if (!strcmp(acc, "secure")) {
425 } else if (!strcmp(acc, "all")) {
428 } else if (!strcmp(acc, "none")) {
432 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
438 lnet_acceptor_start(void)
440 struct task_struct *task;
445 /* if acceptor is already running return immediately */
446 if (!lnet_acceptor_state.pta_shutdown)
449 LASSERT(!lnet_acceptor_state.pta_sock);
451 rc = lnet_acceptor_get_tunables();
455 init_completion(&lnet_acceptor_state.pta_signal);
456 rc = accept2secure(accept_type, &secure);
460 if (!lnet_count_acceptor_nis()) /* not required */
463 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
464 "acceptor_%03ld", secure);
467 CERROR("Can't start acceptor thread: %ld\n", rc2);
472 /* wait for acceptor to startup */
473 wait_for_completion(&lnet_acceptor_state.pta_signal);
475 if (!lnet_acceptor_state.pta_shutdown) {
477 LASSERT(lnet_acceptor_state.pta_sock);
481 LASSERT(!lnet_acceptor_state.pta_sock);
487 lnet_acceptor_stop(void)
491 if (lnet_acceptor_state.pta_shutdown) /* not running */
494 lnet_acceptor_state.pta_shutdown = 1;
496 sk = lnet_acceptor_state.pta_sock->sk;
498 /* awake any sleepers using safe method */
499 sk->sk_state_change(sk);
501 /* block until acceptor signals exit */
502 wait_for_completion(&lnet_acceptor_state.pta_signal);