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 = "secure";
76 module_param(accept, 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)");
85 static char *accept_type = NULL;
88 lnet_acceptor_get_tunables(void)
90 /* Userland acceptor uses 'accept_type' instead of 'accept', due to
91 * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
92 * for compatibility. Hence the trick. */
98 lnet_acceptor_timeout(void)
100 return accept_timeout;
102 EXPORT_SYMBOL(lnet_acceptor_timeout);
105 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
106 __u32 peer_ip, int peer_port)
109 /* "normal" errors */
111 CNETERR("Connection to %s at host %pI4h on port %d was "
112 "refused: check that Lustre is running on that node.\n",
113 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
117 CNETERR("Connection to %s at host %pI4h "
118 "was unreachable: the network or that node may "
119 "be down, or Lustre may be misconfigured.\n",
120 libcfs_nid2str(peer_nid), &peer_ip);
123 CNETERR("Connection to %s at host %pI4h on "
124 "port %d took too long: that node may be hung "
125 "or experiencing high load.\n",
126 libcfs_nid2str(peer_nid), &peer_ip, peer_port);
129 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
130 " on port %d was reset: "
131 "is it running a compatible version of "
132 "Lustre and is %s one of its NIDs?\n",
133 libcfs_nid2str(peer_nid), &peer_ip,
134 peer_port, libcfs_nid2str(peer_nid));
137 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
138 "host %pI4h on port %d: is it running "
139 "a compatible version of Lustre?\n",
140 libcfs_nid2str(peer_nid), &peer_ip,
144 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
145 "connect to %s at host %pI4h on port "
146 "%d\n", libcfs_nid2str(peer_nid),
147 &peer_ip, peer_port);
150 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
151 " at host %pI4h on port %d\n", rc,
152 libcfs_nid2str(peer_nid),
153 &peer_ip, peer_port);
157 EXPORT_SYMBOL(lnet_connect_console_error);
160 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
161 __u32 local_ip, __u32 peer_ip, int peer_port, struct net *ns)
163 struct lnet_acceptor_connreq cr;
169 BUILD_BUG_ON(sizeof(cr) > 16); /* not too big to be on the stack */
171 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
172 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
174 /* Iterate through reserved ports. */
176 rc = lnet_sock_connect(&sock, &fatal,
178 peer_ip, peer_port, ns);
185 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
187 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
188 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
189 cr.acr_nid = peer_nid;
191 if (the_lnet.ln_testprotocompat) {
192 /* single-shot proto check */
193 if (test_and_clear_bit(2, &the_lnet.ln_testprotocompat))
195 if (test_and_clear_bit(3, &the_lnet.ln_testprotocompat))
196 cr.acr_magic = LNET_PROTO_MAGIC;
199 rc = lnet_sock_write(sock, &cr, sizeof(cr),
214 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
217 EXPORT_SYMBOL(lnet_connect);
220 lnet_accept(struct socket *sock, __u32 magic)
222 struct lnet_acceptor_connreq cr;
230 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
232 rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
233 LASSERT(rc == 0); /* we succeeded before */
235 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
237 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
238 /* future version compatibility!
239 * When LNET unifies protocols over all LNDs, the first
240 * thing sent will be a version query. I send back
241 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
243 memset(&cr, 0, sizeof(cr));
244 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
245 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
246 rc = lnet_sock_write(sock, &cr, sizeof(cr),
250 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
255 if (lnet_accept_magic(magic, 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 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);
342 #ifdef HAVE_SK_DATA_READY_ONE_ARG
343 static void lnet_acceptor_ready(struct sock *sk)
345 static void lnet_acceptor_ready(struct sock *sk, int len)
348 /* Ensure pta_odata has actually been set before calling it */
350 #ifdef HAVE_SK_DATA_READY_ONE_ARG
351 lnet_acceptor_state.pta_odata(sk);
353 lnet_acceptor_state.pta_odata(sk, 0);
356 atomic_set(&lnet_acceptor_state.pta_ready, 1);
357 wake_up(&lnet_acceptor_state.pta_waitq);
361 lnet_acceptor(void *arg)
363 struct socket *newsock;
368 int secure = (int)((uintptr_t)arg);
370 LASSERT(lnet_acceptor_state.pta_sock == NULL);
372 rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
373 0, accept_port, accept_backlog,
374 lnet_acceptor_state.pta_ns);
376 if (rc == -EADDRINUSE)
377 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
378 " %d: port already in use\n",
381 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
382 "%d: unexpected error %d\n",
385 lnet_acceptor_state.pta_sock = NULL;
387 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
388 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
389 lnet_acceptor_state.pta_odata =
390 lnet_acceptor_state.pta_sock->sk->sk_data_ready;
391 /* ensure pta_odata gets set before there is any chance of
392 * lnet_accept_ready() trying to read it.
395 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
397 atomic_set(&lnet_acceptor_state.pta_ready, 1);
400 /* set init status and unblock parent */
401 lnet_acceptor_state.pta_shutdown = rc;
402 complete(&lnet_acceptor_state.pta_signal);
407 while (!lnet_acceptor_state.pta_shutdown) {
409 wait_event_idle(lnet_acceptor_state.pta_waitq,
410 lnet_acceptor_state.pta_shutdown ||
411 atomic_read(&lnet_acceptor_state.pta_ready));
412 if (!atomic_read(&lnet_acceptor_state.pta_ready))
414 atomic_set(&lnet_acceptor_state.pta_ready, 0);
415 rc = kernel_accept(lnet_acceptor_state.pta_sock, &newsock,
419 CWARN("Accept error %d: pausing...\n", rc);
420 schedule_timeout_uninterruptible(
421 cfs_time_seconds(1));
426 /* make sure we call lnet_sock_accept() again, until it fails */
427 atomic_set(&lnet_acceptor_state.pta_ready, 1);
429 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
431 CERROR("Can't determine new connection's address\n");
435 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
436 CERROR("Refusing connection from %pI4h: "
437 "insecure port %d\n", &peer_ip, peer_port);
441 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
444 CERROR("Error %d reading connection request from "
445 "%pI4h\n", rc, &peer_ip);
449 rc = lnet_accept(newsock, magic);
456 sock_release(newsock);
459 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
460 lnet_acceptor_state.pta_odata;
461 sock_release(lnet_acceptor_state.pta_sock);
462 lnet_acceptor_state.pta_sock = NULL;
464 CDEBUG(D_NET, "Acceptor stopping\n");
466 /* unblock lnet_acceptor_stop() */
467 complete(&lnet_acceptor_state.pta_signal);
472 accept2secure(const char *acc, long *sec)
474 if (!strcmp(acc, "secure")) {
477 } else if (!strcmp(acc, "all")) {
480 } else if (!strcmp(acc, "none")) {
483 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
490 lnet_acceptor_start(void)
492 struct task_struct *task;
497 /* if acceptor is already running return immediately */
498 if (!lnet_acceptor_state.pta_shutdown)
501 LASSERT(lnet_acceptor_state.pta_sock == NULL);
503 rc = lnet_acceptor_get_tunables();
507 init_completion(&lnet_acceptor_state.pta_signal);
508 rc = accept2secure(accept_type, &secure);
512 if (lnet_count_acceptor_nets() == 0) /* not required */
514 if (current->nsproxy && current->nsproxy->net_ns)
515 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
517 lnet_acceptor_state.pta_ns = &init_net;
518 task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
519 "acceptor_%03ld", secure);
522 CERROR("Can't start acceptor thread: %ld\n", rc2);
526 /* wait for acceptor to startup */
527 wait_for_completion(&lnet_acceptor_state.pta_signal);
529 if (!lnet_acceptor_state.pta_shutdown) {
531 LASSERT(lnet_acceptor_state.pta_sock != NULL);
535 LASSERT(lnet_acceptor_state.pta_sock == NULL);
541 lnet_acceptor_stop(void)
543 if (lnet_acceptor_state.pta_shutdown) /* not running */
546 lnet_acceptor_state.pta_shutdown = 1;
547 wake_up(&lnet_acceptor_state.pta_waitq);
549 /* block until acceptor signals exit */
550 wait_for_completion(&lnet_acceptor_state.pta_signal);