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>
38 #include <linux/sunrpc/addr.h>
40 static int accept_port = 988;
41 static int accept_backlog = 127;
42 static int accept_timeout = 5;
46 struct socket *pta_sock;
47 struct completion pta_signal;
49 wait_queue_head_t pta_waitq;
51 #ifdef HAVE_SK_DATA_READY_ONE_ARG
52 void (*pta_odata)(struct sock *);
54 void (*pta_odata)(struct sock *, int);
56 } lnet_acceptor_state = {
61 lnet_acceptor_port(void)
67 lnet_accept_magic(__u32 magic, __u32 constant)
69 return (magic == constant ||
70 magic == __swab32(constant));
73 EXPORT_SYMBOL(lnet_acceptor_port);
75 static char *accept_type = "secure";
77 module_param_named(accept, accept_type, charp, 0444);
78 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
79 module_param(accept_port, int, 0444);
80 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
81 module_param(accept_backlog, int, 0444);
82 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
83 module_param(accept_timeout, int, 0644);
84 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
87 lnet_acceptor_timeout(void)
89 return accept_timeout;
91 EXPORT_SYMBOL(lnet_acceptor_timeout);
94 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
100 CNETERR("Connection to %s at host %pISp was refused: check that Lustre is running on that node.\n",
101 libcfs_nid2str(peer_nid), sa);
105 CNETERR("Connection to %s at host %pIS was unreachable: the network or that node may be down, or Lustre may be misconfigured.\n",
106 libcfs_nid2str(peer_nid), sa);
109 CNETERR("Connection to %s at host %pISp took too long: that node may be hung or experiencing high load.\n",
110 libcfs_nid2str(peer_nid), sa);
113 LCONSOLE_ERROR_MSG(0x11b,
114 "Connection to %s at host %pISp was reset: is it running a compatible version of Lustre and is %s one of its NIDs?\n",
115 libcfs_nid2str(peer_nid), sa,
116 libcfs_nid2str(peer_nid));
119 LCONSOLE_ERROR_MSG(0x11c,
120 "Protocol error connecting to %s at host %pISp: is it running a compatible version of Lustre?\n",
121 libcfs_nid2str(peer_nid), sa);
124 LCONSOLE_ERROR_MSG(0x11d,
125 "No privileged ports available to connect to %s at host %pISp\n",
126 libcfs_nid2str(peer_nid), sa);
129 LCONSOLE_ERROR_MSG(0x11e,
130 "Unexpected error %d connecting to %s at host %pISp\n",
131 rc, libcfs_nid2str(peer_nid), sa);
135 EXPORT_SYMBOL(lnet_connect_console_error);
138 lnet_connect(lnet_nid_t peer_nid, int interface, struct sockaddr *peeraddr,
141 struct lnet_acceptor_connreq cr;
146 BUILD_BUG_ON(sizeof(cr) > 16); /* not too big to be on the stack */
148 LASSERT(peeraddr->sa_family == AF_INET);
150 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
151 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
153 /* Iterate through reserved ports. */
154 sock = lnet_sock_connect(interface, port, peeraddr, ns);
157 if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL)
162 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
164 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
165 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
166 cr.acr_nid = peer_nid;
168 if (the_lnet.ln_testprotocompat) {
169 /* single-shot proto check */
170 if (test_and_clear_bit(2, &the_lnet.ln_testprotocompat))
172 if (test_and_clear_bit(3, &the_lnet.ln_testprotocompat))
173 cr.acr_magic = LNET_PROTO_MAGIC;
176 rc = lnet_sock_write(sock, &cr, sizeof(cr),
190 lnet_connect_console_error(rc, peer_nid, peeraddr);
193 EXPORT_SYMBOL(lnet_connect);
196 lnet_accept(struct socket *sock, __u32 magic)
198 struct lnet_acceptor_connreq cr;
199 struct sockaddr_storage peer;
205 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
207 rc = lnet_sock_getaddr(sock, true, &peer);
208 LASSERT(rc == 0); /* we succeeded before */
210 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
212 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
213 /* future version compatibility!
214 * When LNET unifies protocols over all LNDs, the first
215 * thing sent will be a version query. I send back
216 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
218 memset(&cr, 0, sizeof(cr));
219 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
220 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
221 rc = lnet_sock_write(sock, &cr, sizeof(cr),
225 CERROR("Error sending magic+version in response to LNET magic from %pIS: %d\n",
230 if (lnet_accept_magic(magic, LNET_PROTO_TCP_MAGIC))
231 str = "'old' socknal/tcpnal";
233 str = "unrecognised";
235 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pIS"
236 " magic %08x: %s acceptor protocol\n",
241 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
243 rc = lnet_sock_read(sock, &cr.acr_version,
244 sizeof(cr.acr_version),
247 CERROR("Error %d reading connection request version from %pIS\n",
253 __swab32s(&cr.acr_version);
255 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
256 /* future version compatibility!
257 * An acceptor-specific protocol rev will first send a version
258 * query. I send back my current version to tell her I'm
260 int peer_version = cr.acr_version;
262 memset(&cr, 0, sizeof(cr));
263 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
264 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
266 rc = lnet_sock_write(sock, &cr, sizeof(cr),
270 CERROR("Error sending magic+version in response to version %d from %pIS: %d\n",
271 peer_version, &peer, rc);
275 rc = lnet_sock_read(sock, &cr.acr_nid,
277 offsetof(struct lnet_acceptor_connreq, acr_nid),
280 CERROR("Error %d reading connection request from %pIS\n",
286 __swab64s(&cr.acr_nid);
288 ni = lnet_nid2ni_addref(cr.acr_nid);
289 if (ni == NULL || /* no matching net */
290 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
293 LCONSOLE_ERROR_MSG(0x120,
294 "Refusing connection from %pIS for %s: No matching NI\n",
295 &peer, libcfs_nid2str(cr.acr_nid));
299 if (ni->ni_net->net_lnd->lnd_accept == NULL) {
300 /* This catches a request for the loopback LND */
302 LCONSOLE_ERROR_MSG(0x121,
303 "Refusing connection from %pIS for %s: NI doesn not accept IP connections\n",
304 &peer, libcfs_nid2str(cr.acr_nid));
308 CDEBUG(D_NET, "Accept %s from %pI4h\n",
309 libcfs_nid2str(cr.acr_nid), &peer);
311 rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
317 #ifdef HAVE_SK_DATA_READY_ONE_ARG
318 static void lnet_acceptor_ready(struct sock *sk)
320 static void lnet_acceptor_ready(struct sock *sk, int len)
323 /* Ensure pta_odata has actually been set before calling it */
325 #ifdef HAVE_SK_DATA_READY_ONE_ARG
326 lnet_acceptor_state.pta_odata(sk);
328 lnet_acceptor_state.pta_odata(sk, 0);
331 atomic_set(&lnet_acceptor_state.pta_ready, 1);
332 wake_up(&lnet_acceptor_state.pta_waitq);
336 lnet_acceptor(void *arg)
338 struct socket *newsock;
341 struct sockaddr_storage peer;
342 int secure = (int)((uintptr_t)arg);
344 LASSERT(lnet_acceptor_state.pta_sock == NULL);
346 lnet_acceptor_state.pta_sock =
347 lnet_sock_listen(accept_port, accept_backlog,
348 lnet_acceptor_state.pta_ns);
349 if (IS_ERR(lnet_acceptor_state.pta_sock)) {
350 rc = PTR_ERR(lnet_acceptor_state.pta_sock);
351 if (rc == -EADDRINUSE)
352 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
353 " %d: port already in use\n",
356 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
357 "%d: unexpected error %d\n",
360 lnet_acceptor_state.pta_sock = NULL;
363 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
364 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
365 lnet_acceptor_state.pta_odata =
366 lnet_acceptor_state.pta_sock->sk->sk_data_ready;
367 /* ensure pta_odata gets set before there is any chance of
368 * lnet_accept_ready() trying to read it.
371 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
373 atomic_set(&lnet_acceptor_state.pta_ready, 1);
376 /* set init status and unblock parent */
377 lnet_acceptor_state.pta_shutdown = rc;
378 complete(&lnet_acceptor_state.pta_signal);
383 while (!lnet_acceptor_state.pta_shutdown) {
385 wait_event_idle(lnet_acceptor_state.pta_waitq,
386 lnet_acceptor_state.pta_shutdown ||
387 atomic_read(&lnet_acceptor_state.pta_ready));
388 if (!atomic_read(&lnet_acceptor_state.pta_ready))
390 atomic_set(&lnet_acceptor_state.pta_ready, 0);
391 rc = kernel_accept(lnet_acceptor_state.pta_sock, &newsock,
395 CWARN("Accept error %d: pausing...\n", rc);
396 schedule_timeout_uninterruptible(
397 cfs_time_seconds(1));
402 /* make sure we call lnet_sock_accept() again, until it fails */
403 atomic_set(&lnet_acceptor_state.pta_ready, 1);
405 rc = lnet_sock_getaddr(newsock, true, &peer);
407 CERROR("Can't determine new connection's address\n");
412 rpc_get_port((struct sockaddr *)&peer) >
413 LNET_ACCEPTOR_MAX_RESERVED_PORT) {
414 CERROR("Refusing connection from %pISp: insecure port.\n",
419 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
422 CERROR("Error %d reading connection request from %pIS\n",
427 rc = lnet_accept(newsock, magic);
434 sock_release(newsock);
437 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
438 lnet_acceptor_state.pta_odata;
439 sock_release(lnet_acceptor_state.pta_sock);
440 lnet_acceptor_state.pta_sock = NULL;
442 CDEBUG(D_NET, "Acceptor stopping\n");
444 /* unblock lnet_acceptor_stop() */
445 complete(&lnet_acceptor_state.pta_signal);
450 accept2secure(const char *acc, long *sec)
452 if (!strcmp(acc, "secure")) {
455 } else if (!strcmp(acc, "all")) {
458 } else if (!strcmp(acc, "none")) {
461 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
468 lnet_acceptor_start(void)
470 struct task_struct *task;
475 /* if acceptor is already running return immediately */
476 if (!lnet_acceptor_state.pta_shutdown)
479 LASSERT(lnet_acceptor_state.pta_sock == NULL);
481 init_completion(&lnet_acceptor_state.pta_signal);
482 rc = accept2secure(accept_type, &secure);
486 if (lnet_count_acceptor_nets() == 0) /* not required */
488 if (current->nsproxy && current->nsproxy->net_ns)
489 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
491 lnet_acceptor_state.pta_ns = &init_net;
492 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
493 "acceptor_%03ld", secure);
496 CERROR("Can't start acceptor thread: %ld\n", rc2);
500 /* wait for acceptor to startup */
501 wait_for_completion(&lnet_acceptor_state.pta_signal);
503 if (!lnet_acceptor_state.pta_shutdown) {
505 LASSERT(lnet_acceptor_state.pta_sock != NULL);
509 LASSERT(lnet_acceptor_state.pta_sock == NULL);
515 lnet_acceptor_stop(void)
517 if (lnet_acceptor_state.pta_shutdown) /* not running */
520 /* If still required, return immediately */
521 if (the_lnet.ln_refcount && lnet_count_acceptor_nets() > 0)
524 lnet_acceptor_state.pta_shutdown = 1;
525 wake_up(&lnet_acceptor_state.pta_waitq);
527 /* block until acceptor signals exit */
528 wait_for_completion(&lnet_acceptor_state.pta_signal);