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;
48 } lnet_acceptor_state = {
53 lnet_acceptor_port(void)
59 lnet_accept_magic(__u32 magic, __u32 constant)
61 return (magic == constant ||
62 magic == __swab32(constant));
65 EXPORT_SYMBOL(lnet_acceptor_port);
67 static char *accept = "secure";
69 module_param(accept, charp, 0444);
70 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
71 module_param(accept_port, int, 0444);
72 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
73 module_param(accept_backlog, int, 0444);
74 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
75 module_param(accept_timeout, int, 0644);
76 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
78 static char *accept_type = NULL;
81 lnet_acceptor_get_tunables(void)
83 /* Userland acceptor uses 'accept_type' instead of 'accept', due to
84 * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
85 * 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 "
105 "refused: check that Lustre is running on that node.\n",
106 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
110 CNETERR("Connection to %s at host %pI4h "
111 "was unreachable: the network or that node may "
112 "be down, or Lustre may be misconfigured.\n",
113 libcfs_nid2str(peer_nid), &peer_ip);
116 CNETERR("Connection to %s at host %pI4h on "
117 "port %d took too long: that node may be hung "
118 "or experiencing high load.\n",
119 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
122 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
123 " on port %d was reset: "
124 "is it running a compatible version of "
125 "Lustre and is %s one of its NIDs?\n",
126 libcfs_nid2str(peer_nid), &peer_ip,
127 peer_port, libcfs_nid2str(peer_nid));
130 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
131 "host %pI4h on port %d: is it running "
132 "a compatible version of Lustre?\n",
133 libcfs_nid2str(peer_nid), &peer_ip,
137 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
138 "connect to %s at host %pI4h on port "
139 "%d\n", libcfs_nid2str(peer_nid),
140 &peer_ip, peer_port);
143 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
144 " at host %pI4h on port %d\n", rc,
145 libcfs_nid2str(peer_nid),
146 &peer_ip, peer_port);
150 EXPORT_SYMBOL(lnet_connect_console_error);
153 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
154 __u32 local_ip, __u32 peer_ip, int peer_port, struct net *ns)
156 struct lnet_acceptor_connreq cr;
162 CLASSERT(sizeof(cr) <= 16); /* not too big to be on the stack */
164 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
165 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
167 /* Iterate through reserved ports. */
169 rc = lnet_sock_connect(&sock, &fatal,
171 peer_ip, peer_port, ns);
178 CLASSERT(LNET_PROTO_ACCEPTOR_VERSION == 1);
180 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
181 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
182 cr.acr_nid = peer_nid;
184 if (the_lnet.ln_testprotocompat != 0) {
185 /* single-shot proto check */
186 lnet_net_lock(LNET_LOCK_EX);
187 if ((the_lnet.ln_testprotocompat & 4) != 0) {
189 the_lnet.ln_testprotocompat &= ~4;
191 if ((the_lnet.ln_testprotocompat & 8) != 0) {
192 cr.acr_magic = LNET_PROTO_MAGIC;
193 the_lnet.ln_testprotocompat &= ~8;
195 lnet_net_unlock(LNET_LOCK_EX);
198 rc = lnet_sock_write(sock, &cr, sizeof(cr),
213 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
216 EXPORT_SYMBOL(lnet_connect);
219 lnet_accept(struct socket *sock, __u32 magic)
221 struct lnet_acceptor_connreq cr;
229 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
231 rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
232 LASSERT(rc == 0); /* we succeeded before */
234 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
236 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
237 /* future version compatibility!
238 * When LNET unifies protocols over all LNDs, the first
239 * thing sent will be a version query. I send back
240 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
242 memset(&cr, 0, sizeof(cr));
243 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
244 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
245 rc = lnet_sock_write(sock, &cr, sizeof(cr),
249 CERROR("Error sending magic+version in response"
250 "to LNET magic from %pI4h: %d\n",
255 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
256 str = "'old' socknal/tcpnal";
258 str = "unrecognised";
260 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
261 " magic %08x: %s acceptor protocol\n",
262 &peer_ip, magic, str);
266 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
268 rc = lnet_sock_read(sock, &cr.acr_version,
269 sizeof(cr.acr_version),
272 CERROR("Error %d reading connection request version from "
273 "%pI4h\n", rc, &peer_ip);
278 __swab32s(&cr.acr_version);
280 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
281 /* future version compatibility!
282 * An acceptor-specific protocol rev will first send a version
283 * query. I send back my current version to tell her I'm
285 int peer_version = cr.acr_version;
287 memset(&cr, 0, sizeof(cr));
288 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
289 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
291 rc = lnet_sock_write(sock, &cr, sizeof(cr),
295 CERROR("Error sending magic+version in response"
296 "to version %d from %pI4h: %d\n",
297 peer_version, &peer_ip, rc);
301 rc = lnet_sock_read(sock, &cr.acr_nid,
303 offsetof(struct lnet_acceptor_connreq, acr_nid),
306 CERROR("Error %d reading connection request from "
307 "%pI4h\n", rc, &peer_ip);
312 __swab64s(&cr.acr_nid);
314 ni = lnet_nid2ni_addref(cr.acr_nid);
315 if (ni == NULL || /* no matching net */
316 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
319 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
320 "for %s: No matching NI\n",
321 &peer_ip, libcfs_nid2str(cr.acr_nid));
325 if (ni->ni_net->net_lnd->lnd_accept == NULL) {
326 /* This catches a request for the loopback LND */
328 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
329 "for %s: NI doesn not accept IP connections\n",
330 &peer_ip, libcfs_nid2str(cr.acr_nid));
334 CDEBUG(D_NET, "Accept %s from %pI4h\n",
335 libcfs_nid2str(cr.acr_nid), &peer_ip);
337 rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
344 lnet_acceptor(void *arg)
346 struct socket *newsock;
351 int secure = (int)((uintptr_t)arg);
353 LASSERT(lnet_acceptor_state.pta_sock == NULL);
357 rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
358 0, accept_port, accept_backlog,
359 lnet_acceptor_state.pta_ns);
361 if (rc == -EADDRINUSE)
362 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
363 " %d: port already in use\n",
366 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
367 "%d: unexpected error %d\n",
370 lnet_acceptor_state.pta_sock = NULL;
372 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
375 /* set init status and unblock parent */
376 lnet_acceptor_state.pta_shutdown = rc;
377 complete(&lnet_acceptor_state.pta_signal);
382 while (!lnet_acceptor_state.pta_shutdown) {
384 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
387 CWARN("Accept error %d: pausing...\n", rc);
388 set_current_state(TASK_UNINTERRUPTIBLE);
389 schedule_timeout(cfs_time_seconds(1));
394 /* maybe we're waken up with lnet_sock_abort_accept() */
395 if (lnet_acceptor_state.pta_shutdown) {
396 sock_release(newsock);
400 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
402 CERROR("Can't determine new connection's address\n");
406 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
407 CERROR("Refusing connection from %pI4h: "
408 "insecure port %d\n", &peer_ip, peer_port);
412 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
415 CERROR("Error %d reading connection request from "
416 "%pI4h\n", rc, &peer_ip);
420 rc = lnet_accept(newsock, magic);
427 sock_release(newsock);
430 sock_release(lnet_acceptor_state.pta_sock);
431 lnet_acceptor_state.pta_sock = NULL;
433 CDEBUG(D_NET, "Acceptor stopping\n");
435 /* unblock lnet_acceptor_stop() */
436 complete(&lnet_acceptor_state.pta_signal);
441 accept2secure(const char *acc, long *sec)
443 if (!strcmp(acc, "secure")) {
446 } else if (!strcmp(acc, "all")) {
449 } else if (!strcmp(acc, "none")) {
452 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
459 lnet_acceptor_start(void)
461 struct task_struct *task;
466 /* if acceptor is already running return immediately */
467 if (!lnet_acceptor_state.pta_shutdown)
470 LASSERT(lnet_acceptor_state.pta_sock == NULL);
472 rc = lnet_acceptor_get_tunables();
476 init_completion(&lnet_acceptor_state.pta_signal);
477 rc = accept2secure(accept_type, &secure);
481 if (lnet_count_acceptor_nets() == 0) /* not required */
484 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
485 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
486 "acceptor_%03ld", secure);
489 CERROR("Can't start acceptor thread: %ld\n", rc2);
494 /* wait for acceptor to startup */
495 wait_for_completion(&lnet_acceptor_state.pta_signal);
497 if (!lnet_acceptor_state.pta_shutdown) {
499 LASSERT(lnet_acceptor_state.pta_sock != NULL);
503 LASSERT(lnet_acceptor_state.pta_sock == NULL);
509 lnet_acceptor_stop(void)
513 if (lnet_acceptor_state.pta_shutdown) /* not running */
516 lnet_acceptor_state.pta_shutdown = 1;
518 sk = lnet_acceptor_state.pta_sock->sk;
520 /* awake any sleepers using safe method */
521 sk->sk_state_change(sk);
523 /* block until acceptor signals exit */
524 wait_for_completion(&lnet_acceptor_state.pta_signal);