1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #define DEBUG_SUBSYSTEM S_LNET
24 #include <lnet/lib-lnet.h>
27 static char *accept = "secure";
28 CFS_MODULE_PARM(accept, "s", charp, 0444,
29 "Accept connections (secure|all|none)");
31 static int accept_port = 988;
32 CFS_MODULE_PARM(accept_port, "i", int, 0444,
33 "Acceptor's port (same on all nodes)");
35 static int accept_backlog = 127;
36 CFS_MODULE_PARM(accept_backlog, "i", int, 0444,
37 "Acceptor's listen backlog");
39 static int accept_timeout = 5;
40 CFS_MODULE_PARM(accept_timeout, "i", int, 0644,
41 "Acceptor's timeout (seconds)");
45 cfs_socket_t *pta_sock;
46 struct semaphore pta_signal;
47 } lnet_acceptor_state;
50 lnet_acceptor_timeout(void)
52 return accept_timeout;
54 EXPORT_SYMBOL(lnet_acceptor_timeout);
57 lnet_acceptor_port(void)
61 EXPORT_SYMBOL(lnet_acceptor_port);
64 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
65 __u32 peer_ip, int peer_port)
70 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u "
71 "on port %d was refused: "
72 "check that Lustre is running on that node.\n",
73 libcfs_nid2str(peer_nid),
74 HIPQUAD(peer_ip), peer_port);
78 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u "
79 "was unreachable: the network or that node may "
80 "be down, or Lustre may be misconfigured.\n",
81 libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
84 CDEBUG(D_NETERROR, "Connection to %s at host %u.%u.%u.%u on "
85 "port %d took too long: that node may be hung "
86 "or experiencing high load.\n",
87 libcfs_nid2str(peer_nid),
88 HIPQUAD(peer_ip), peer_port);
91 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
92 " on port %d was reset: "
93 "is it running a compatible version of "
94 "Lustre and is %s one of its NIDs?\n",
95 libcfs_nid2str(peer_nid),
96 HIPQUAD(peer_ip), peer_port,
97 libcfs_nid2str(peer_nid));
100 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
101 "host %u.%u.%u.%u on port %d: is it running "
102 "a compatible version of Lustre?\n",
103 libcfs_nid2str(peer_nid),
104 HIPQUAD(peer_ip), peer_port);
107 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
108 "connect to %s at host %u.%u.%u.%u on port "
109 "%d\n", libcfs_nid2str(peer_nid),
110 HIPQUAD(peer_ip), peer_port);
113 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
114 " at host %u.%u.%u.%u on port %d\n", rc,
115 libcfs_nid2str(peer_nid),
116 HIPQUAD(peer_ip), peer_port);
120 EXPORT_SYMBOL(lnet_connect_console_error);
123 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
124 __u32 local_ip, __u32 peer_ip, int peer_port)
126 lnet_acceptor_connreq_t cr;
132 CLASSERT (sizeof(cr) <= 16); /* not too big to be on the stack */
134 for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
135 port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
137 /* Iterate through reserved ports. */
139 rc = libcfs_sock_connect(&sock, &fatal,
148 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
150 if (the_lnet.ln_ptlcompat != 2) {
151 /* When portals compatibility is "strong", simply
152 * connect (i.e. send no acceptor connection request).
153 * Othewise send an acceptor connection request. I can
154 * have no portals peers so everyone else should
155 * understand my protocol. */
156 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
157 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
158 cr.acr_nid = peer_nid;
160 if (the_lnet.ln_testprotocompat != 0) {
161 /* single-shot proto check */
163 if ((the_lnet.ln_testprotocompat & 4) != 0) {
165 the_lnet.ln_testprotocompat &= ~4;
167 if ((the_lnet.ln_testprotocompat & 8) != 0) {
168 cr.acr_magic = LNET_PROTO_MAGIC;
169 the_lnet.ln_testprotocompat &= ~8;
174 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
188 libcfs_sock_release(sock);
190 lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
193 EXPORT_SYMBOL(lnet_connect);
196 lnet_accept_magic(__u32 magic, __u32 constant)
198 return (magic == constant ||
199 magic == __swab32(constant));
203 lnet_accept(lnet_ni_t *blind_ni, cfs_socket_t *sock, __u32 magic)
205 lnet_acceptor_connreq_t cr;
213 /* CAVEAT EMPTOR: I may be called by an LND in any thread's context if
214 * I passed the new socket "blindly" to the single NI that needed an
215 * acceptor. If so, blind_ni != NULL... */
217 LASSERT (sizeof(cr) <= 16); /* not too big for the stack */
219 rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
220 LASSERT (rc == 0); /* we succeeded before */
222 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
224 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
225 /* future version compatibility!
226 * When LNET unifies protocols over all LNDs, the first
227 * thing sent will be a version query. I send back
228 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
230 memset (&cr, 0, sizeof(cr));
231 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
232 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
233 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
237 CERROR("Error sending magic+version in response"
238 "to LNET magic from %u.%u.%u.%u: %d\n",
239 HIPQUAD(peer_ip), rc);
243 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
244 str = "'old' socknal/tcpnal";
245 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
247 else if (lnet_accept_magic(magic, LNET_PROTO_OPENIB_MAGIC))
248 str = "'old' openibnal";
250 str = "unrecognised";
252 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
253 " magic %08x: %s acceptor protocol\n",
254 HIPQUAD(peer_ip), magic, str);
258 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
260 rc = libcfs_sock_read(sock, &cr.acr_version,
261 sizeof(cr.acr_version),
264 CERROR("Error %d reading connection request version from "
265 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
270 __swab32s(&cr.acr_version);
272 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
273 /* future version compatibility!
274 * An acceptor-specific protocol rev will first send a version
275 * query. I send back my current version to tell her I'm
277 int peer_version = cr.acr_version;
279 memset (&cr, 0, sizeof(cr));
280 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
281 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
283 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
287 CERROR("Error sending magic+version in response"
288 "to version %d from %u.%u.%u.%u: %d\n",
289 peer_version, HIPQUAD(peer_ip), rc);
293 rc = libcfs_sock_read(sock, &cr.acr_nid,
295 offsetof(lnet_acceptor_connreq_t, acr_nid),
298 CERROR("Error %d reading connection request from "
299 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
304 __swab64s(&cr.acr_nid);
306 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
307 if (ni == NULL || /* no matching net */
308 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
311 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
312 " for %s: No matching NI\n",
313 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
317 if (ni->ni_lnd->lnd_accept == NULL) {
318 /* This catches a request for the loopback LND */
320 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
321 " for %s: NI doesn not accept IP connections\n",
322 HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
326 CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u%s\n",
327 libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip),
328 blind_ni == NULL ? "" : " (blind)");
330 if (blind_ni == NULL) {
331 /* called by the acceptor: call into the requested NI... */
332 rc = ni->ni_lnd->lnd_accept(ni, sock);
334 /* portals_compatible set and the (only) NI called me to verify
335 * and skip the connection request... */
336 LASSERT (the_lnet.ln_ptlcompat != 0);
337 LASSERT (ni == blind_ni);
344 EXPORT_SYMBOL(lnet_accept);
347 lnet_acceptor(void *arg)
350 cfs_socket_t *newsock;
356 lnet_ni_t *blind_ni = NULL;
357 int secure = (int)((unsigned long)arg);
359 LASSERT (lnet_acceptor_state.pta_sock == NULL);
361 if (the_lnet.ln_ptlcompat != 0) {
362 /* When portals_compatibility is enabled, peers may connect
363 * without sending an acceptor connection request. There is no
364 * ambiguity about which network the peer wants to connect to
365 * since there can only be 1 network, so I pass connections
366 * "blindly" to it. */
367 n_acceptor_nis = lnet_count_acceptor_nis(&blind_ni);
368 LASSERT (n_acceptor_nis == 1);
369 LASSERT (blind_ni != NULL);
372 snprintf(name, sizeof(name), "acceptor_%03d", accept_port);
376 rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
377 0, accept_port, accept_backlog);
379 if (rc == -EADDRINUSE)
380 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
381 " %d: port already in use\n",
384 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
385 "%d: unexpected error %d\n",
388 lnet_acceptor_state.pta_sock = NULL;
390 LCONSOLE(0, "Accept %s, port %d%s\n",
392 blind_ni == NULL ? "" : " (proto compatible)");
395 /* set init status and unblock parent */
396 lnet_acceptor_state.pta_shutdown = rc;
397 mutex_up(&lnet_acceptor_state.pta_signal);
402 while (lnet_acceptor_state.pta_shutdown == 0) {
404 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
407 CWARN("Accept error %d: pausing...\n", rc);
408 cfs_pause(cfs_time_seconds(1));
413 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
415 CERROR("Can't determine new connection's address\n");
419 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
420 CERROR("Refusing connection from %u.%u.%u.%u: "
421 "insecure port %d\n",
422 HIPQUAD(peer_ip), peer_port);
426 if (blind_ni != NULL) {
427 rc = blind_ni->ni_lnd->lnd_accept(blind_ni, newsock);
429 CERROR("NI %s refused 'blind' connection from "
431 libcfs_nid2str(blind_ni->ni_nid),
438 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
441 CERROR("Error %d reading connection request from "
442 "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
446 rc = lnet_accept(NULL, newsock, magic);
453 libcfs_sock_release(newsock);
456 libcfs_sock_release(lnet_acceptor_state.pta_sock);
457 lnet_acceptor_state.pta_sock = NULL;
459 if (blind_ni != NULL)
460 lnet_ni_decref(blind_ni);
462 LCONSOLE(0,"Acceptor stopping\n");
464 /* unblock lnet_acceptor_stop() */
465 mutex_up(&lnet_acceptor_state.pta_signal);
470 lnet_acceptor_start(void)
475 LASSERT (lnet_acceptor_state.pta_sock == NULL);
476 init_mutex_locked(&lnet_acceptor_state.pta_signal);
478 if (!strcmp(accept, "secure")) {
480 } else if (!strcmp(accept, "all")) {
482 } else if (!strcmp(accept, "none")) {
485 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
490 if (lnet_count_acceptor_nis(NULL) == 0) /* not required */
493 pid = cfs_kernel_thread(lnet_acceptor, (void *)secure, 0);
495 CERROR("Can't start acceptor thread: %ld\n", pid);
499 mutex_down(&lnet_acceptor_state.pta_signal); /* wait for acceptor to startup */
501 if (lnet_acceptor_state.pta_shutdown == 0) {
503 LASSERT (lnet_acceptor_state.pta_sock != NULL);
507 LASSERT (lnet_acceptor_state.pta_sock == NULL);
512 lnet_acceptor_stop(void)
514 if (lnet_acceptor_state.pta_sock == NULL) /* not running */
517 lnet_acceptor_state.pta_shutdown = 1;
518 libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
520 /* block until acceptor signals exit */
521 mutex_down(&lnet_acceptor_state.pta_signal);
524 #else /* __KERNEL__ */
527 lnet_acceptor_start(void)
533 lnet_acceptor_stop(void)
537 #endif /* !__KERNEL__ */