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 wait_queue_head_t pta_waitq;
50 #ifdef HAVE_SK_DATA_READY_ONE_ARG
51 void (*pta_odata)(struct sock *);
53 void (*pta_odata)(struct sock *, int);
55 } lnet_acceptor_state = {
60 lnet_acceptor_port(void)
66 lnet_accept_magic(__u32 magic, __u32 constant)
68 return (magic == constant ||
69 magic == __swab32(constant));
72 EXPORT_SYMBOL(lnet_acceptor_port);
74 static char *accept_type = "secure";
76 module_param_named(accept, accept_type, charp, 0444);
77 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
78 module_param(accept_port, int, 0444);
79 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
80 module_param(accept_backlog, int, 0444);
81 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
82 module_param(accept_timeout, int, 0644);
83 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
86 lnet_acceptor_timeout(void)
88 return accept_timeout;
90 EXPORT_SYMBOL(lnet_acceptor_timeout);
93 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
94 __u32 peer_ip, int peer_port)
99 CNETERR("Connection to %s at host %pI4h on port %d was "
100 "refused: check that Lustre is running on that node.\n",
101 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
105 CNETERR("Connection to %s at host %pI4h "
106 "was unreachable: the network or that node may "
107 "be down, or Lustre may be misconfigured.\n",
108 libcfs_nid2str(peer_nid), &peer_ip);
111 CNETERR("Connection to %s at host %pI4h on "
112 "port %d took too long: that node may be hung "
113 "or experiencing high load.\n",
114 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
117 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
118 " on port %d was reset: "
119 "is it running a compatible version of "
120 "Lustre and is %s one of its NIDs?\n",
121 libcfs_nid2str(peer_nid), &peer_ip,
122 peer_port, libcfs_nid2str(peer_nid));
125 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
126 "host %pI4h on port %d: is it running "
127 "a compatible version of Lustre?\n",
128 libcfs_nid2str(peer_nid), &peer_ip,
132 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
133 "connect to %s at host %pI4h on port "
134 "%d\n", libcfs_nid2str(peer_nid),
135 &peer_ip, peer_port);
138 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
139 " at host %pI4h on port %d\n", rc,
140 libcfs_nid2str(peer_nid),
141 &peer_ip, peer_port);
145 EXPORT_SYMBOL(lnet_connect_console_error);
148 lnet_connect(lnet_nid_t peer_nid, int interface, __u32 peer_ip,
149 int peer_port, struct net *ns)
151 struct lnet_acceptor_connreq cr;
156 BUILD_BUG_ON(sizeof(cr) > 16); /* not too big to be on the stack */
158 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
159 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
161 /* Iterate through reserved ports. */
162 struct sockaddr_in paddr = {.sin_family = AF_INET};
164 paddr.sin_addr.s_addr = htonl(peer_ip);
165 paddr.sin_port = htons(peer_port);
166 sock = lnet_sock_connect(interface, port,
167 (struct sockaddr *)&paddr, ns);
170 if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL)
175 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
177 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
178 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
179 cr.acr_nid = peer_nid;
181 if (the_lnet.ln_testprotocompat) {
182 /* single-shot proto check */
183 if (test_and_clear_bit(2, &the_lnet.ln_testprotocompat))
185 if (test_and_clear_bit(3, &the_lnet.ln_testprotocompat))
186 cr.acr_magic = LNET_PROTO_MAGIC;
189 rc = lnet_sock_write(sock, &cr, sizeof(cr),
203 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
206 EXPORT_SYMBOL(lnet_connect);
209 lnet_accept(struct socket *sock, __u32 magic)
211 struct lnet_acceptor_connreq cr;
219 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
221 rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
222 LASSERT(rc == 0); /* we succeeded before */
224 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
226 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
227 /* future version compatibility!
228 * When LNET unifies protocols over all LNDs, the first
229 * thing sent will be a version query. I send back
230 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
232 memset(&cr, 0, sizeof(cr));
233 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
234 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
235 rc = lnet_sock_write(sock, &cr, sizeof(cr),
239 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
244 if (lnet_accept_magic(magic, LNET_PROTO_TCP_MAGIC))
245 str = "'old' socknal/tcpnal";
247 str = "unrecognised";
249 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
250 " magic %08x: %s acceptor protocol\n",
251 &peer_ip, magic, str);
255 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
257 rc = lnet_sock_read(sock, &cr.acr_version,
258 sizeof(cr.acr_version),
261 CERROR("Error %d reading connection request version from "
262 "%pI4h\n", rc, &peer_ip);
267 __swab32s(&cr.acr_version);
269 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
270 /* future version compatibility!
271 * An acceptor-specific protocol rev will first send a version
272 * query. I send back my current version to tell her I'm
274 int peer_version = cr.acr_version;
276 memset(&cr, 0, sizeof(cr));
277 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
278 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
280 rc = lnet_sock_write(sock, &cr, sizeof(cr),
284 CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
285 peer_version, &peer_ip, rc);
289 rc = lnet_sock_read(sock, &cr.acr_nid,
291 offsetof(struct lnet_acceptor_connreq, acr_nid),
294 CERROR("Error %d reading connection request from "
295 "%pI4h\n", rc, &peer_ip);
300 __swab64s(&cr.acr_nid);
302 ni = lnet_nid2ni_addref(cr.acr_nid);
303 if (ni == NULL || /* no matching net */
304 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
307 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
308 "for %s: No matching NI\n",
309 &peer_ip, libcfs_nid2str(cr.acr_nid));
313 if (ni->ni_net->net_lnd->lnd_accept == NULL) {
314 /* This catches a request for the loopback LND */
316 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
317 "for %s: NI doesn not accept IP connections\n",
318 &peer_ip, libcfs_nid2str(cr.acr_nid));
322 CDEBUG(D_NET, "Accept %s from %pI4h\n",
323 libcfs_nid2str(cr.acr_nid), &peer_ip);
325 rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
331 #ifdef HAVE_SK_DATA_READY_ONE_ARG
332 static void lnet_acceptor_ready(struct sock *sk)
334 static void lnet_acceptor_ready(struct sock *sk, int len)
337 /* Ensure pta_odata has actually been set before calling it */
339 #ifdef HAVE_SK_DATA_READY_ONE_ARG
340 lnet_acceptor_state.pta_odata(sk);
342 lnet_acceptor_state.pta_odata(sk, 0);
345 atomic_set(&lnet_acceptor_state.pta_ready, 1);
346 wake_up(&lnet_acceptor_state.pta_waitq);
350 lnet_acceptor(void *arg)
352 struct socket *newsock;
357 int secure = (int)((uintptr_t)arg);
359 LASSERT(lnet_acceptor_state.pta_sock == NULL);
361 lnet_acceptor_state.pta_sock =
362 lnet_sock_listen(accept_port, accept_backlog,
363 lnet_acceptor_state.pta_ns);
364 if (IS_ERR(lnet_acceptor_state.pta_sock)) {
365 rc = PTR_ERR(lnet_acceptor_state.pta_sock);
366 if (rc == -EADDRINUSE)
367 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
368 " %d: port already in use\n",
371 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
372 "%d: unexpected error %d\n",
375 lnet_acceptor_state.pta_sock = NULL;
378 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
379 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
380 lnet_acceptor_state.pta_odata =
381 lnet_acceptor_state.pta_sock->sk->sk_data_ready;
382 /* ensure pta_odata gets set before there is any chance of
383 * lnet_accept_ready() trying to read it.
386 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
388 atomic_set(&lnet_acceptor_state.pta_ready, 1);
391 /* set init status and unblock parent */
392 lnet_acceptor_state.pta_shutdown = rc;
393 complete(&lnet_acceptor_state.pta_signal);
398 while (!lnet_acceptor_state.pta_shutdown) {
400 wait_event_idle(lnet_acceptor_state.pta_waitq,
401 lnet_acceptor_state.pta_shutdown ||
402 atomic_read(&lnet_acceptor_state.pta_ready));
403 if (!atomic_read(&lnet_acceptor_state.pta_ready))
405 atomic_set(&lnet_acceptor_state.pta_ready, 0);
406 rc = kernel_accept(lnet_acceptor_state.pta_sock, &newsock,
410 CWARN("Accept error %d: pausing...\n", rc);
411 schedule_timeout_uninterruptible(
412 cfs_time_seconds(1));
417 /* make sure we call lnet_sock_accept() again, until it fails */
418 atomic_set(&lnet_acceptor_state.pta_ready, 1);
420 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
422 CERROR("Can't determine new connection's address\n");
426 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
427 CERROR("Refusing connection from %pI4h: "
428 "insecure port %d\n", &peer_ip, peer_port);
432 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
435 CERROR("Error %d reading connection request from "
436 "%pI4h\n", rc, &peer_ip);
440 rc = lnet_accept(newsock, magic);
447 sock_release(newsock);
450 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
451 lnet_acceptor_state.pta_odata;
452 sock_release(lnet_acceptor_state.pta_sock);
453 lnet_acceptor_state.pta_sock = NULL;
455 CDEBUG(D_NET, "Acceptor stopping\n");
457 /* unblock lnet_acceptor_stop() */
458 complete(&lnet_acceptor_state.pta_signal);
463 accept2secure(const char *acc, long *sec)
465 if (!strcmp(acc, "secure")) {
468 } else if (!strcmp(acc, "all")) {
471 } else if (!strcmp(acc, "none")) {
474 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
481 lnet_acceptor_start(void)
483 struct task_struct *task;
488 /* if acceptor is already running return immediately */
489 if (!lnet_acceptor_state.pta_shutdown)
492 LASSERT(lnet_acceptor_state.pta_sock == NULL);
494 init_completion(&lnet_acceptor_state.pta_signal);
495 rc = accept2secure(accept_type, &secure);
499 if (lnet_count_acceptor_nets() == 0) /* not required */
501 if (current->nsproxy && current->nsproxy->net_ns)
502 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
504 lnet_acceptor_state.pta_ns = &init_net;
505 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
506 "acceptor_%03ld", secure);
509 CERROR("Can't start acceptor thread: %ld\n", rc2);
513 /* wait for acceptor to startup */
514 wait_for_completion(&lnet_acceptor_state.pta_signal);
516 if (!lnet_acceptor_state.pta_shutdown) {
518 LASSERT(lnet_acceptor_state.pta_sock != NULL);
522 LASSERT(lnet_acceptor_state.pta_sock == NULL);
528 lnet_acceptor_stop(void)
530 if (lnet_acceptor_state.pta_shutdown) /* not running */
533 /* If still required, return immediately */
534 if (the_lnet.ln_refcount && lnet_count_acceptor_nets() > 0)
537 lnet_acceptor_state.pta_shutdown = 1;
538 wake_up(&lnet_acceptor_state.pta_waitq);
540 /* block until acceptor signals exit */
541 wait_for_completion(&lnet_acceptor_state.pta_signal);