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(struct socket **sockp, lnet_nid_t peer_nid,
149 __u32 local_ip, __u32 peer_ip, int peer_port, struct net *ns)
151 struct lnet_acceptor_connreq cr;
157 BUILD_BUG_ON(sizeof(cr) > 16); /* not too big to be on the stack */
159 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
160 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
162 /* Iterate through reserved ports. */
164 rc = lnet_sock_connect(&sock, &fatal,
166 peer_ip, peer_port, ns);
173 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
175 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
176 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
177 cr.acr_nid = peer_nid;
179 if (the_lnet.ln_testprotocompat) {
180 /* single-shot proto check */
181 if (test_and_clear_bit(2, &the_lnet.ln_testprotocompat))
183 if (test_and_clear_bit(3, &the_lnet.ln_testprotocompat))
184 cr.acr_magic = LNET_PROTO_MAGIC;
187 rc = lnet_sock_write(sock, &cr, sizeof(cr),
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, true, &peer_ip, &peer_port);
221 LASSERT(rc == 0); /* we succeeded before */
223 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
225 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 (lnet_accept_magic(magic, LNET_PROTO_TCP_MAGIC))
244 str = "'old' socknal/tcpnal";
246 str = "unrecognised";
248 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
249 " magic %08x: %s acceptor protocol\n",
250 &peer_ip, magic, str);
254 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
256 rc = lnet_sock_read(sock, &cr.acr_version,
257 sizeof(cr.acr_version),
260 CERROR("Error %d reading connection request version from "
261 "%pI4h\n", rc, &peer_ip);
266 __swab32s(&cr.acr_version);
268 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
269 /* future version compatibility!
270 * An acceptor-specific protocol rev will first send a version
271 * 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),
283 CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
284 peer_version, &peer_ip, rc);
288 rc = lnet_sock_read(sock, &cr.acr_nid,
290 offsetof(struct lnet_acceptor_connreq, acr_nid),
293 CERROR("Error %d reading connection request from "
294 "%pI4h\n", rc, &peer_ip);
299 __swab64s(&cr.acr_nid);
301 ni = lnet_nid2ni_addref(cr.acr_nid);
302 if (ni == NULL || /* no matching net */
303 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
306 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
307 "for %s: No matching NI\n",
308 &peer_ip, libcfs_nid2str(cr.acr_nid));
312 if (ni->ni_net->net_lnd->lnd_accept == NULL) {
313 /* This catches a request for the loopback LND */
315 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
316 "for %s: NI doesn not accept IP connections\n",
317 &peer_ip, libcfs_nid2str(cr.acr_nid));
321 CDEBUG(D_NET, "Accept %s from %pI4h\n",
322 libcfs_nid2str(cr.acr_nid), &peer_ip);
324 rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
330 #ifdef HAVE_SK_DATA_READY_ONE_ARG
331 static void lnet_acceptor_ready(struct sock *sk)
333 static void lnet_acceptor_ready(struct sock *sk, int len)
336 /* Ensure pta_odata has actually been set before calling it */
338 #ifdef HAVE_SK_DATA_READY_ONE_ARG
339 lnet_acceptor_state.pta_odata(sk);
341 lnet_acceptor_state.pta_odata(sk, 0);
344 atomic_set(&lnet_acceptor_state.pta_ready, 1);
345 wake_up(&lnet_acceptor_state.pta_waitq);
349 lnet_acceptor(void *arg)
351 struct socket *newsock;
356 int secure = (int)((uintptr_t)arg);
358 LASSERT(lnet_acceptor_state.pta_sock == NULL);
360 rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
361 0, accept_port, accept_backlog,
362 lnet_acceptor_state.pta_ns);
364 if (rc == -EADDRINUSE)
365 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
366 " %d: port already in use\n",
369 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
370 "%d: unexpected error %d\n",
373 lnet_acceptor_state.pta_sock = NULL;
375 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
376 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
377 lnet_acceptor_state.pta_odata =
378 lnet_acceptor_state.pta_sock->sk->sk_data_ready;
379 /* ensure pta_odata gets set before there is any chance of
380 * lnet_accept_ready() trying to read it.
383 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
385 atomic_set(&lnet_acceptor_state.pta_ready, 1);
388 /* set init status and unblock parent */
389 lnet_acceptor_state.pta_shutdown = rc;
390 complete(&lnet_acceptor_state.pta_signal);
395 while (!lnet_acceptor_state.pta_shutdown) {
397 wait_event_idle(lnet_acceptor_state.pta_waitq,
398 lnet_acceptor_state.pta_shutdown ||
399 atomic_read(&lnet_acceptor_state.pta_ready));
400 if (!atomic_read(&lnet_acceptor_state.pta_ready))
402 atomic_set(&lnet_acceptor_state.pta_ready, 0);
403 rc = kernel_accept(lnet_acceptor_state.pta_sock, &newsock,
407 CWARN("Accept error %d: pausing...\n", rc);
408 schedule_timeout_uninterruptible(
409 cfs_time_seconds(1));
414 /* make sure we call lnet_sock_accept() again, until it fails */
415 atomic_set(&lnet_acceptor_state.pta_ready, 1);
417 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
419 CERROR("Can't determine new connection's address\n");
423 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
424 CERROR("Refusing connection from %pI4h: "
425 "insecure port %d\n", &peer_ip, peer_port);
429 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
432 CERROR("Error %d reading connection request from "
433 "%pI4h\n", rc, &peer_ip);
437 rc = lnet_accept(newsock, magic);
444 sock_release(newsock);
447 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
448 lnet_acceptor_state.pta_odata;
449 sock_release(lnet_acceptor_state.pta_sock);
450 lnet_acceptor_state.pta_sock = NULL;
452 CDEBUG(D_NET, "Acceptor stopping\n");
454 /* unblock lnet_acceptor_stop() */
455 complete(&lnet_acceptor_state.pta_signal);
460 accept2secure(const char *acc, long *sec)
462 if (!strcmp(acc, "secure")) {
465 } else if (!strcmp(acc, "all")) {
468 } else if (!strcmp(acc, "none")) {
471 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
478 lnet_acceptor_start(void)
480 struct task_struct *task;
485 /* if acceptor is already running return immediately */
486 if (!lnet_acceptor_state.pta_shutdown)
489 LASSERT(lnet_acceptor_state.pta_sock == NULL);
491 init_completion(&lnet_acceptor_state.pta_signal);
492 rc = accept2secure(accept_type, &secure);
496 if (lnet_count_acceptor_nets() == 0) /* not required */
498 if (current->nsproxy && current->nsproxy->net_ns)
499 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
501 lnet_acceptor_state.pta_ns = &init_net;
502 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
503 "acceptor_%03ld", secure);
506 CERROR("Can't start acceptor thread: %ld\n", rc2);
510 /* wait for acceptor to startup */
511 wait_for_completion(&lnet_acceptor_state.pta_signal);
513 if (!lnet_acceptor_state.pta_shutdown) {
515 LASSERT(lnet_acceptor_state.pta_sock != NULL);
519 LASSERT(lnet_acceptor_state.pta_sock == NULL);
525 lnet_acceptor_stop(void)
527 if (lnet_acceptor_state.pta_shutdown) /* not running */
530 lnet_acceptor_state.pta_shutdown = 1;
531 wake_up(&lnet_acceptor_state.pta_waitq);
533 /* block until acceptor signals exit */
534 wait_for_completion(&lnet_acceptor_state.pta_signal);