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, 2017, 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
35 #include <linux/completion.h>
37 #include <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)
58 lnet_accept_magic(__u32 magic, __u32 constant)
60 return (magic == constant ||
61 magic == __swab32(constant));
64 EXPORT_SYMBOL(lnet_acceptor_port);
66 static char *accept = "secure";
68 module_param(accept, charp, 0444);
69 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
70 module_param(accept_port, int, 0444);
71 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
72 module_param(accept_backlog, int, 0444);
73 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
74 module_param(accept_timeout, int, 0644);
75 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
77 static char *accept_type = NULL;
80 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. */
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 "
104 "refused: check that Lustre is running on that node.\n",
105 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
109 CNETERR("Connection to %s at host %pI4h "
110 "was unreachable: the network or that node may "
111 "be down, or Lustre may be misconfigured.\n",
112 libcfs_nid2str(peer_nid), &peer_ip);
115 CNETERR("Connection to %s at host %pI4h on "
116 "port %d took too long: that node may be hung "
117 "or experiencing high load.\n",
118 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
121 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
122 " on port %d was reset: "
123 "is it running a compatible version of "
124 "Lustre and is %s one of its NIDs?\n",
125 libcfs_nid2str(peer_nid), &peer_ip,
126 peer_port, libcfs_nid2str(peer_nid));
129 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
130 "host %pI4h on port %d: is it running "
131 "a compatible version of Lustre?\n",
132 libcfs_nid2str(peer_nid), &peer_ip,
136 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
137 "connect to %s at host %pI4h on port "
138 "%d\n", libcfs_nid2str(peer_nid),
139 &peer_ip, peer_port);
142 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
143 " at host %pI4h on port %d\n", rc,
144 libcfs_nid2str(peer_nid),
145 &peer_ip, peer_port);
149 EXPORT_SYMBOL(lnet_connect_console_error);
152 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
153 __u32 local_ip, __u32 peer_ip, int peer_port)
155 struct lnet_acceptor_connreq cr;
161 CLASSERT(sizeof(cr) <= 16); /* not too big to be on the stack */
163 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
164 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
166 /* Iterate through reserved ports. */
168 rc = lnet_sock_connect(&sock, &fatal,
177 CLASSERT(LNET_PROTO_ACCEPTOR_VERSION == 1);
179 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
180 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
181 cr.acr_nid = peer_nid;
183 if (the_lnet.ln_testprotocompat != 0) {
184 /* single-shot proto check */
185 lnet_net_lock(LNET_LOCK_EX);
186 if ((the_lnet.ln_testprotocompat & 4) != 0) {
188 the_lnet.ln_testprotocompat &= ~4;
190 if ((the_lnet.ln_testprotocompat & 8) != 0) {
191 cr.acr_magic = LNET_PROTO_MAGIC;
192 the_lnet.ln_testprotocompat &= ~8;
194 lnet_net_unlock(LNET_LOCK_EX);
197 rc = lnet_sock_write(sock, &cr, sizeof(cr),
212 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
215 EXPORT_SYMBOL(lnet_connect);
218 lnet_accept(struct socket *sock, __u32 magic)
220 struct lnet_acceptor_connreq cr;
228 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
230 rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
231 LASSERT(rc == 0); /* we succeeded before */
233 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
235 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
236 /* future version compatibility!
237 * When LNET unifies protocols over all LNDs, the first
238 * thing sent will be a version query. I send back
239 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
241 memset(&cr, 0, sizeof(cr));
242 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
243 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
244 rc = lnet_sock_write(sock, &cr, sizeof(cr),
248 CERROR("Error sending magic+version in response"
249 "to LNET magic from %pI4h: %d\n",
254 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
255 str = "'old' socknal/tcpnal";
257 str = "unrecognised";
259 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
260 " magic %08x: %s acceptor protocol\n",
261 &peer_ip, magic, str);
265 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
267 rc = lnet_sock_read(sock, &cr.acr_version,
268 sizeof(cr.acr_version),
271 CERROR("Error %d reading connection request version from "
272 "%pI4h\n", rc, &peer_ip);
277 __swab32s(&cr.acr_version);
279 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
280 /* future version compatibility!
281 * An acceptor-specific protocol rev will first send a version
282 * query. I send back my current version to tell her I'm
284 int peer_version = cr.acr_version;
286 memset(&cr, 0, sizeof(cr));
287 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
288 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
290 rc = lnet_sock_write(sock, &cr, sizeof(cr),
294 CERROR("Error sending magic+version in response"
295 "to version %d from %pI4h: %d\n",
296 peer_version, &peer_ip, rc);
300 rc = lnet_sock_read(sock, &cr.acr_nid,
302 offsetof(struct lnet_acceptor_connreq, acr_nid),
305 CERROR("Error %d reading connection request from "
306 "%pI4h\n", rc, &peer_ip);
311 __swab64s(&cr.acr_nid);
313 ni = lnet_nid2ni_addref(cr.acr_nid);
314 if (ni == NULL || /* no matching net */
315 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
318 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
319 "for %s: No matching NI\n",
320 &peer_ip, libcfs_nid2str(cr.acr_nid));
324 if (ni->ni_net->net_lnd->lnd_accept == NULL) {
325 /* This catches a request for the loopback LND */
327 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
328 "for %s: NI doesn not accept IP connections\n",
329 &peer_ip, libcfs_nid2str(cr.acr_nid));
333 CDEBUG(D_NET, "Accept %s from %pI4h\n",
334 libcfs_nid2str(cr.acr_nid), &peer_ip);
336 rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
343 lnet_acceptor(void *arg)
345 struct socket *newsock;
350 int secure = (int)((uintptr_t)arg);
352 LASSERT(lnet_acceptor_state.pta_sock == NULL);
356 rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
357 0, accept_port, accept_backlog);
359 if (rc == -EADDRINUSE)
360 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
361 " %d: port already in use\n",
364 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
365 "%d: unexpected error %d\n",
368 lnet_acceptor_state.pta_sock = NULL;
370 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
373 /* set init status and unblock parent */
374 lnet_acceptor_state.pta_shutdown = rc;
375 complete(&lnet_acceptor_state.pta_signal);
380 while (!lnet_acceptor_state.pta_shutdown) {
382 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
385 CWARN("Accept error %d: pausing...\n", rc);
386 set_current_state(TASK_UNINTERRUPTIBLE);
387 schedule_timeout(cfs_time_seconds(1));
392 /* maybe we're waken up with lnet_sock_abort_accept() */
393 if (lnet_acceptor_state.pta_shutdown) {
394 sock_release(newsock);
398 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
400 CERROR("Can't determine new connection's address\n");
404 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
405 CERROR("Refusing connection from %pI4h: "
406 "insecure port %d\n", &peer_ip, peer_port);
410 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
413 CERROR("Error %d reading connection request from "
414 "%pI4h\n", rc, &peer_ip);
418 rc = lnet_accept(newsock, magic);
425 sock_release(newsock);
428 sock_release(lnet_acceptor_state.pta_sock);
429 lnet_acceptor_state.pta_sock = NULL;
431 CDEBUG(D_NET, "Acceptor stopping\n");
433 /* unblock lnet_acceptor_stop() */
434 complete(&lnet_acceptor_state.pta_signal);
439 accept2secure(const char *acc, long *sec)
441 if (!strcmp(acc, "secure")) {
444 } else if (!strcmp(acc, "all")) {
447 } else if (!strcmp(acc, "none")) {
450 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
457 lnet_acceptor_start(void)
459 struct task_struct *task;
464 /* if acceptor is already running return immediately */
465 if (!lnet_acceptor_state.pta_shutdown)
468 LASSERT(lnet_acceptor_state.pta_sock == NULL);
470 rc = lnet_acceptor_get_tunables();
474 init_completion(&lnet_acceptor_state.pta_signal);
475 rc = accept2secure(accept_type, &secure);
479 if (lnet_count_acceptor_nets() == 0) /* not required */
482 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
483 "acceptor_%03ld", secure);
486 CERROR("Can't start acceptor thread: %ld\n", rc2);
491 /* wait for acceptor to startup */
492 wait_for_completion(&lnet_acceptor_state.pta_signal);
494 if (!lnet_acceptor_state.pta_shutdown) {
496 LASSERT(lnet_acceptor_state.pta_sock != NULL);
500 LASSERT(lnet_acceptor_state.pta_sock == NULL);
506 lnet_acceptor_stop(void)
510 if (lnet_acceptor_state.pta_shutdown) /* not running */
513 lnet_acceptor_state.pta_shutdown = 1;
515 sk = lnet_acceptor_state.pta_sock->sk;
517 /* awake any sleepers using safe method */
518 sk->sk_state_change(sk);
520 /* block until acceptor signals exit */
521 wait_for_completion(&lnet_acceptor_state.pta_signal);