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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/lnet/lib-lnet.h>
41 static int accept_port
= 988;
42 static int accept_backlog
= 127;
43 static int accept_timeout
= 5;
48 struct completion pta_signal
;
49 } lnet_acceptor_state
;
52 lnet_acceptor_port(void)
58 lnet_accept_magic(__u32 magic
, __u32 constant
)
60 return (magic
== constant
||
61 magic
== __swab32(constant
));
65 EXPORT_SYMBOL(lnet_acceptor_port
);
67 static char *accept
= "secure";
69 CFS_MODULE_PARM(accept
, "s", charp
, 0444,
70 "Accept connections (secure|all|none)");
71 CFS_MODULE_PARM(accept_port
, "i", int, 0444,
72 "Acceptor's port (same on all nodes)");
73 CFS_MODULE_PARM(accept_backlog
, "i", int, 0444,
74 "Acceptor's listen backlog");
75 CFS_MODULE_PARM(accept_timeout
, "i", int, 0644,
76 "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
),
107 &peer_ip
, peer_port
);
111 CNETERR("Connection to %s at host %pI4h "
112 "was unreachable: the network or that node may "
113 "be down, or Lustre may be misconfigured.\n",
114 libcfs_nid2str(peer_nid
), &peer_ip
);
117 CNETERR("Connection to %s at host %pI4h on "
118 "port %d took too long: that node may be hung "
119 "or experiencing high load.\n",
120 libcfs_nid2str(peer_nid
),
121 &peer_ip
, peer_port
);
124 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
125 " on port %d was reset: "
126 "is it running a compatible version of "
127 "Lustre and is %s one of its NIDs?\n",
128 libcfs_nid2str(peer_nid
),
130 libcfs_nid2str(peer_nid
));
133 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
134 "host %pI4h on port %d: is it running "
135 "a compatible version of Lustre?\n",
136 libcfs_nid2str(peer_nid
),
137 &peer_ip
, peer_port
);
140 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
141 "connect to %s at host %pI4h on port "
142 "%d\n", libcfs_nid2str(peer_nid
),
143 &peer_ip
, peer_port
);
146 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
147 " at host %pI4h on port %d\n", rc
,
148 libcfs_nid2str(peer_nid
),
149 &peer_ip
, peer_port
);
153 EXPORT_SYMBOL(lnet_connect_console_error
);
156 lnet_connect(socket_t
**sockp
, lnet_nid_t peer_nid
,
157 __u32 local_ip
, __u32 peer_ip
, int peer_port
)
159 lnet_acceptor_connreq_t cr
;
165 CLASSERT (sizeof(cr
) <= 16); /* not too big to be on the stack */
167 for (port
= LNET_ACCEPTOR_MAX_RESERVED_PORT
;
168 port
>= LNET_ACCEPTOR_MIN_RESERVED_PORT
;
170 /* Iterate through reserved ports. */
172 rc
= libcfs_sock_connect(&sock
, &fatal
,
181 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION
== 1);
183 cr
.acr_magic
= LNET_PROTO_ACCEPTOR_MAGIC
;
184 cr
.acr_version
= LNET_PROTO_ACCEPTOR_VERSION
;
185 cr
.acr_nid
= peer_nid
;
187 if (the_lnet
.ln_testprotocompat
!= 0) {
188 /* single-shot proto check */
189 lnet_net_lock(LNET_LOCK_EX
);
190 if ((the_lnet
.ln_testprotocompat
& 4) != 0) {
192 the_lnet
.ln_testprotocompat
&= ~4;
194 if ((the_lnet
.ln_testprotocompat
& 8) != 0) {
195 cr
.acr_magic
= LNET_PROTO_MAGIC
;
196 the_lnet
.ln_testprotocompat
&= ~8;
198 lnet_net_unlock(LNET_LOCK_EX
);
201 rc
= libcfs_sock_write(sock
, &cr
, sizeof(cr
),
214 libcfs_sock_release(sock
);
216 lnet_connect_console_error(rc
, peer_nid
, peer_ip
, peer_port
);
219 EXPORT_SYMBOL(lnet_connect
);
222 /* Below is the code common for both kernel and MT user-space */
225 lnet_accept(socket_t
*sock
, __u32 magic
)
227 lnet_acceptor_connreq_t cr
;
235 LASSERT (sizeof(cr
) <= 16); /* not too big for the stack */
237 rc
= libcfs_sock_getaddr(sock
, 1, &peer_ip
, &peer_port
);
238 LASSERT (rc
== 0); /* we succeeded before */
240 if (!lnet_accept_magic(magic
, LNET_PROTO_ACCEPTOR_MAGIC
)) {
242 if (lnet_accept_magic(magic
, LNET_PROTO_MAGIC
)) {
243 /* future version compatibility!
244 * When LNET unifies protocols over all LNDs, the first
245 * thing sent will be a version query. I send back
246 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
248 memset (&cr
, 0, sizeof(cr
));
249 cr
.acr_magic
= LNET_PROTO_ACCEPTOR_MAGIC
;
250 cr
.acr_version
= LNET_PROTO_ACCEPTOR_VERSION
;
251 rc
= libcfs_sock_write(sock
, &cr
, sizeof(cr
),
255 CERROR("Error sending magic+version in response"
256 "to LNET magic from %pI4h: %d\n",
261 if (magic
== le32_to_cpu(LNET_PROTO_TCP_MAGIC
))
262 str
= "'old' socknal/tcpnal";
263 else if (lnet_accept_magic(magic
, LNET_PROTO_RA_MAGIC
))
266 str
= "unrecognised";
268 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
269 " magic %08x: %s acceptor protocol\n",
270 &peer_ip
, magic
, str
);
274 flip
= (magic
!= LNET_PROTO_ACCEPTOR_MAGIC
);
276 rc
= libcfs_sock_read(sock
, &cr
.acr_version
,
277 sizeof(cr
.acr_version
),
280 CERROR("Error %d reading connection request version from "
281 "%pI4h\n", rc
, &peer_ip
);
286 __swab32s(&cr
.acr_version
);
288 if (cr
.acr_version
!= LNET_PROTO_ACCEPTOR_VERSION
) {
289 /* future version compatibility!
290 * An acceptor-specific protocol rev will first send a version
291 * query. I send back my current version to tell her I'm
293 int peer_version
= cr
.acr_version
;
295 memset (&cr
, 0, sizeof(cr
));
296 cr
.acr_magic
= LNET_PROTO_ACCEPTOR_MAGIC
;
297 cr
.acr_version
= LNET_PROTO_ACCEPTOR_VERSION
;
299 rc
= libcfs_sock_write(sock
, &cr
, sizeof(cr
),
303 CERROR("Error sending magic+version in response"
304 "to version %d from %pI4h: %d\n",
305 peer_version
, &peer_ip
, rc
);
309 rc
= libcfs_sock_read(sock
, &cr
.acr_nid
,
311 offsetof(lnet_acceptor_connreq_t
, acr_nid
),
314 CERROR("Error %d reading connection request from "
315 "%pI4h\n", rc
, &peer_ip
);
320 __swab64s(&cr
.acr_nid
);
322 ni
= lnet_net2ni(LNET_NIDNET(cr
.acr_nid
));
323 if (ni
== NULL
|| /* no matching net */
324 ni
->ni_nid
!= cr
.acr_nid
) { /* right NET, wrong NID! */
327 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h"
328 " for %s: No matching NI\n",
329 &peer_ip
, libcfs_nid2str(cr
.acr_nid
));
333 if (ni
->ni_lnd
->lnd_accept
== NULL
) {
334 /* This catches a request for the loopback LND */
336 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h"
337 " for %s: NI doesn not accept IP connections\n",
338 &peer_ip
, libcfs_nid2str(cr
.acr_nid
));
342 CDEBUG(D_NET
, "Accept %s from %pI4h\n",
343 libcfs_nid2str(cr
.acr_nid
), &peer_ip
);
345 rc
= ni
->ni_lnd
->lnd_accept(ni
, sock
);
352 lnet_acceptor(void *arg
)
359 int secure
= (int)((long_ptr_t
)arg
);
361 LASSERT (lnet_acceptor_state
.pta_sock
== NULL
);
365 rc
= libcfs_sock_listen(&lnet_acceptor_state
.pta_sock
,
366 0, accept_port
, accept_backlog
);
368 if (rc
== -EADDRINUSE
)
369 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
370 " %d: port already in use\n",
373 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
374 "%d: unexpected error %d\n",
377 lnet_acceptor_state
.pta_sock
= NULL
;
379 LCONSOLE(0, "Accept %s, port %d\n", accept_type
, accept_port
);
382 /* set init status and unblock parent */
383 lnet_acceptor_state
.pta_shutdown
= rc
;
384 complete(&lnet_acceptor_state
.pta_signal
);
389 while (!lnet_acceptor_state
.pta_shutdown
) {
391 rc
= libcfs_sock_accept(&newsock
, lnet_acceptor_state
.pta_sock
);
394 CWARN("Accept error %d: pausing...\n", rc
);
395 cfs_pause(cfs_time_seconds(1));
400 /* maybe we're waken up with libcfs_sock_abort_accept() */
401 if (lnet_acceptor_state
.pta_shutdown
) {
402 libcfs_sock_release(newsock
);
406 rc
= libcfs_sock_getaddr(newsock
, 1, &peer_ip
, &peer_port
);
408 CERROR("Can't determine new connection's address\n");
412 if (secure
&& peer_port
> LNET_ACCEPTOR_MAX_RESERVED_PORT
) {
413 CERROR("Refusing connection from %pI4h: "
414 "insecure port %d\n",
415 &peer_ip
, peer_port
);
419 rc
= libcfs_sock_read(newsock
, &magic
, sizeof(magic
),
422 CERROR("Error %d reading connection request from "
423 "%pI4h\n", rc
, &peer_ip
);
427 rc
= lnet_accept(newsock
, magic
);
434 libcfs_sock_release(newsock
);
437 libcfs_sock_release(lnet_acceptor_state
.pta_sock
);
438 lnet_acceptor_state
.pta_sock
= NULL
;
440 CDEBUG(D_NET
, "Acceptor stopping\n");
442 /* unblock lnet_acceptor_stop() */
443 complete(&lnet_acceptor_state
.pta_signal
);
448 accept2secure(const char *acc
, long *sec
)
450 if (!strcmp(acc
, "secure")) {
453 } else if (!strcmp(acc
, "all")) {
456 } else if (!strcmp(acc
, "none")) {
459 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
466 lnet_acceptor_start(void)
472 LASSERT (lnet_acceptor_state
.pta_sock
== NULL
);
474 rc
= lnet_acceptor_get_tunables();
479 init_completion(&lnet_acceptor_state
.pta_signal
);
480 rc
= accept2secure(accept_type
, &secure
);
482 fini_completion(&lnet_acceptor_state
.pta_signal
);
486 if (lnet_count_acceptor_nis() == 0) /* not required */
489 rc2
= PTR_ERR(kthread_run(lnet_acceptor
,
490 (void *)(ulong_ptr_t
)secure
,
491 "acceptor_%03ld", secure
));
492 if (IS_ERR_VALUE(rc2
)) {
493 CERROR("Can't start acceptor thread: %ld\n", rc2
);
494 fini_completion(&lnet_acceptor_state
.pta_signal
);
499 /* wait for acceptor to startup */
500 wait_for_completion(&lnet_acceptor_state
.pta_signal
);
502 if (!lnet_acceptor_state
.pta_shutdown
) {
504 LASSERT(lnet_acceptor_state
.pta_sock
!= NULL
);
508 LASSERT(lnet_acceptor_state
.pta_sock
== NULL
);
509 fini_completion(&lnet_acceptor_state
.pta_signal
);
515 lnet_acceptor_stop(void)
517 if (lnet_acceptor_state
.pta_sock
== NULL
) /* not running */
520 lnet_acceptor_state
.pta_shutdown
= 1;
521 libcfs_sock_abort_accept(lnet_acceptor_state
.pta_sock
);
523 /* block until acceptor signals exit */
524 wait_for_completion(&lnet_acceptor_state
.pta_signal
);
526 fini_completion(&lnet_acceptor_state
.pta_signal
);