Whamcloud - gitweb
LU-6209 lnet: Delete all obsolete LND drivers
[fs/lustre-release.git] / lnet / lnet / acceptor.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
39
40 static int   accept_port    = 988;
41 static int   accept_backlog = 127;
42 static int   accept_timeout = 5;
43
44 static struct {
45         int                     pta_shutdown;
46         cfs_socket_t            *pta_sock;
47         struct completion       pta_signal;
48 } lnet_acceptor_state = {
49         .pta_shutdown = 1
50 };
51
52 int
53 lnet_acceptor_port(void)
54 {
55         return accept_port;
56 }
57
58 static inline int
59 lnet_accept_magic(__u32 magic, __u32 constant)
60 {
61         return (magic == constant ||
62                 magic == __swab32(constant));
63 }
64
65 EXPORT_SYMBOL(lnet_acceptor_port);
66
67 static char *accept = "secure";
68
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)");
77
78 static char *accept_type = NULL;
79
80 static int
81 lnet_acceptor_get_tunables(void)
82 {
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. */
86         accept_type = accept;
87         return 0;
88 }
89
90 int
91 lnet_acceptor_timeout(void)
92 {
93         return accept_timeout;
94 }
95 EXPORT_SYMBOL(lnet_acceptor_timeout);
96
97 void
98 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
99                            __u32 peer_ip, int peer_port)
100 {
101         switch (rc) {
102         /* "normal" errors */
103         case -ECONNREFUSED:
104                 CNETERR("Connection to %s at host %u.%u.%u.%u on port %d was "
105                         "refused: check that Lustre is running on that node.\n",
106                         libcfs_nid2str(peer_nid),
107                         HIPQUAD(peer_ip), peer_port);
108                 break;
109         case -EHOSTUNREACH:
110         case -ENETUNREACH:
111                 CNETERR("Connection to %s at host %u.%u.%u.%u "
112                         "was unreachable: the network or that node may "
113                         "be down, or Lustre may be misconfigured.\n",
114                         libcfs_nid2str(peer_nid), HIPQUAD(peer_ip));
115                 break;
116         case -ETIMEDOUT:
117                 CNETERR("Connection to %s at host %u.%u.%u.%u on "
118                         "port %d took too long: that node may be hung "
119                         "or experiencing high load.\n",
120                         libcfs_nid2str(peer_nid),
121                         HIPQUAD(peer_ip), peer_port);
122                 break;
123         case -ECONNRESET:
124                 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %u.%u.%u.%u"
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),
129                                    HIPQUAD(peer_ip), peer_port,
130                                    libcfs_nid2str(peer_nid));
131                 break;
132         case -EPROTO:
133                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
134                                    "host %u.%u.%u.%u on port %d: is it running "
135                                    "a compatible version of Lustre?\n",
136                                    libcfs_nid2str(peer_nid),
137                                    HIPQUAD(peer_ip), peer_port);
138                 break;
139         case -EADDRINUSE:
140                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
141                                    "connect to %s at host %u.%u.%u.%u on port "
142                                    "%d\n", libcfs_nid2str(peer_nid),
143                                    HIPQUAD(peer_ip), peer_port);
144                 break;
145         default:
146                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
147                                    " at host %u.%u.%u.%u on port %d\n", rc,
148                                    libcfs_nid2str(peer_nid),
149                                    HIPQUAD(peer_ip), peer_port);
150                 break;
151         }
152 }
153 EXPORT_SYMBOL(lnet_connect_console_error);
154
155 int
156 lnet_connect(cfs_socket_t **sockp, lnet_nid_t peer_nid,
157             __u32 local_ip, __u32 peer_ip, int peer_port)
158 {
159         lnet_acceptor_connreq_t cr;
160         cfs_socket_t           *sock;
161         int                     rc;
162         int                     port;
163         int                     fatal;
164
165         CLASSERT (sizeof(cr) <= 16);            /* not too big to be on the stack */
166
167         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
168              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
169              --port) {
170                 /* Iterate through reserved ports. */
171
172                 rc = libcfs_sock_connect(&sock, &fatal,
173                                          local_ip, port,
174                                          peer_ip, peer_port);
175                 if (rc != 0) {
176                         if (fatal)
177                                 goto failed;
178                         continue;
179                 }
180
181                 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
182
183                 cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
184                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
185                 cr.acr_nid     = peer_nid;
186
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) {
191                                 cr.acr_version++;
192                                 the_lnet.ln_testprotocompat &= ~4;
193                         }
194                         if ((the_lnet.ln_testprotocompat & 8) != 0) {
195                                 cr.acr_magic = LNET_PROTO_MAGIC;
196                                 the_lnet.ln_testprotocompat &= ~8;
197                         }
198                         lnet_net_unlock(LNET_LOCK_EX);
199                 }
200
201                 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
202                                        accept_timeout);
203                 if (rc != 0)
204                         goto failed_sock;
205
206                 *sockp = sock;
207                 return 0;
208         }
209
210         rc = -EADDRINUSE;
211         goto failed;
212
213  failed_sock:
214         libcfs_sock_release(sock);
215  failed:
216         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
217         return rc;
218 }
219 EXPORT_SYMBOL(lnet_connect);
220
221 static int
222 lnet_accept(cfs_socket_t *sock, __u32 magic)
223 {
224         lnet_acceptor_connreq_t cr;
225         __u32                   peer_ip;
226         int                     peer_port;
227         int                     rc;
228         int                     flip;
229         lnet_ni_t              *ni;
230         char                   *str;
231
232         LASSERT (sizeof(cr) <= 16);             /* not too big for the stack */
233
234         rc = libcfs_sock_getaddr(sock, 1, &peer_ip, &peer_port);
235         LASSERT (rc == 0);                      /* we succeeded before */
236
237         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
238
239                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
240                         /* future version compatibility!
241                          * When LNET unifies protocols over all LNDs, the first
242                          * thing sent will be a version query.  I send back
243                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
244
245                         memset (&cr, 0, sizeof(cr));
246                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
247                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
248                         rc = libcfs_sock_write(sock, &cr, sizeof(cr),
249                                                accept_timeout);
250
251                         if (rc != 0)
252                                 CERROR("Error sending magic+version in response"
253                                        "to LNET magic from %u.%u.%u.%u: %d\n",
254                                        HIPQUAD(peer_ip), rc);
255                         return -EPROTO;
256                 }
257
258                 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
259                         str = "'old' socknal/tcpnal";
260                 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
261                         str = "'old' ranal";
262                 else
263                         str = "unrecognised";
264
265                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %u.%u.%u.%u"
266                                    " magic %08x: %s acceptor protocol\n",
267                                    HIPQUAD(peer_ip), magic, str);
268                 return -EPROTO;
269         }
270
271         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
272
273         rc = libcfs_sock_read(sock, &cr.acr_version,
274                               sizeof(cr.acr_version),
275                               accept_timeout);
276         if (rc != 0) {
277                 CERROR("Error %d reading connection request version from "
278                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
279                 return -EIO;
280         }
281
282         if (flip)
283                 __swab32s(&cr.acr_version);
284
285         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
286                 /* future version compatibility!
287                  * An acceptor-specific protocol rev will first send a version
288                  * query.  I send back my current version to tell her I'm
289                  * "old". */
290                 int peer_version = cr.acr_version;
291
292                 memset (&cr, 0, sizeof(cr));
293                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
294                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
295
296                 rc = libcfs_sock_write(sock, &cr, sizeof(cr),
297                                        accept_timeout);
298
299                 if (rc != 0)
300                         CERROR("Error sending magic+version in response"
301                                "to version %d from %u.%u.%u.%u: %d\n",
302                                peer_version, HIPQUAD(peer_ip), rc);
303                 return -EPROTO;
304         }
305
306         rc = libcfs_sock_read(sock, &cr.acr_nid,
307                               sizeof(cr) -
308                               offsetof(lnet_acceptor_connreq_t, acr_nid),
309                               accept_timeout);
310         if (rc != 0) {
311                 CERROR("Error %d reading connection request from "
312                        "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
313                 return -EIO;
314         }
315
316         if (flip)
317                 __swab64s(&cr.acr_nid);
318
319         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
320         if (ni == NULL ||               /* no matching net */
321             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
322                 if (ni != NULL)
323                         lnet_ni_decref(ni);
324                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %u.%u.%u.%u"
325                                    " for %s: No matching NI\n",
326                                    HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
327                 return -EPERM;
328         }
329
330         if (ni->ni_lnd->lnd_accept == NULL) {
331                 /* This catches a request for the loopback LND */
332                 lnet_ni_decref(ni);
333                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %u.%u.%u.%u"
334                                   " for %s: NI doesn not accept IP connections\n",
335                                   HIPQUAD(peer_ip), libcfs_nid2str(cr.acr_nid));
336                 return -EPERM;
337         }
338
339         CDEBUG(D_NET, "Accept %s from %u.%u.%u.%u\n",
340                libcfs_nid2str(cr.acr_nid), HIPQUAD(peer_ip));
341
342         rc = ni->ni_lnd->lnd_accept(ni, sock);
343
344         lnet_ni_decref(ni);
345         return rc;
346 }
347
348 static int
349 lnet_acceptor(void *arg)
350 {
351         cfs_socket_t  *newsock;
352         int            rc;
353         __u32          magic;
354         __u32          peer_ip;
355         int            peer_port;
356         int            secure = (int)((long_ptr_t)arg);
357
358         LASSERT (lnet_acceptor_state.pta_sock == NULL);
359
360         cfs_block_allsigs();
361
362         rc = libcfs_sock_listen(&lnet_acceptor_state.pta_sock,
363                                 0, accept_port, accept_backlog);
364         if (rc != 0) {
365                 if (rc == -EADDRINUSE)
366                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
367                                            " %d: port already in use\n",
368                                            accept_port);
369                 else
370                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
371                                            "%d: unexpected error %d\n",
372                                            accept_port, rc);
373
374                 lnet_acceptor_state.pta_sock = NULL;
375         } else {
376                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
377         }
378
379         /* set init status and unblock parent */
380         lnet_acceptor_state.pta_shutdown = rc;
381         complete(&lnet_acceptor_state.pta_signal);
382
383         if (rc != 0)
384                 return rc;
385
386         while (!lnet_acceptor_state.pta_shutdown) {
387
388                 rc = libcfs_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
389                 if (rc != 0) {
390                         if (rc != -EAGAIN) {
391                                 CWARN("Accept error %d: pausing...\n", rc);
392                                 cfs_pause(cfs_time_seconds(1));
393                         }
394                         continue;
395                 }
396
397                 /* maybe we're waken up with libcfs_sock_abort_accept() */
398                 if (lnet_acceptor_state.pta_shutdown) {
399                         libcfs_sock_release(newsock);
400                         break;
401                 }
402
403                 rc = libcfs_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
404                 if (rc != 0) {
405                         CERROR("Can't determine new connection's address\n");
406                         goto failed;
407                 }
408
409                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
410                         CERROR("Refusing connection from %u.%u.%u.%u: "
411                                "insecure port %d\n",
412                                HIPQUAD(peer_ip), peer_port);
413                         goto failed;
414                 }
415
416                 rc = libcfs_sock_read(newsock, &magic, sizeof(magic),
417                                       accept_timeout);
418                 if (rc != 0) {
419                         CERROR("Error %d reading connection request from "
420                                "%u.%u.%u.%u\n", rc, HIPQUAD(peer_ip));
421                         goto failed;
422                 }
423
424                 rc = lnet_accept(newsock, magic);
425                 if (rc != 0)
426                         goto failed;
427
428                 continue;
429
430         failed:
431                 libcfs_sock_release(newsock);
432         }
433
434         libcfs_sock_release(lnet_acceptor_state.pta_sock);
435         lnet_acceptor_state.pta_sock = NULL;
436
437         CDEBUG(D_NET, "Acceptor stopping\n");
438
439         /* unblock lnet_acceptor_stop() */
440         complete(&lnet_acceptor_state.pta_signal);
441         return 0;
442 }
443
444 static inline int
445 accept2secure(const char *acc, long *sec)
446 {
447         if (!strcmp(acc, "secure")) {
448                 *sec = 1;
449                 return 1;
450         } else if (!strcmp(acc, "all")) {
451                 *sec = 0;
452                 return 1;
453         } else if (!strcmp(acc, "none")) {
454                 return 0;
455         } else {
456                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
457                                    acc);
458                 return -EINVAL;
459         }
460 }
461
462 int
463 lnet_acceptor_start(void)
464 {
465         struct task_struct *task;
466         int  rc;
467         long rc2;
468         long secure;
469
470         /* if acceptor is already running return immediately */
471         if (!lnet_acceptor_state.pta_shutdown)
472                 return 0;
473
474         LASSERT (lnet_acceptor_state.pta_sock == NULL);
475
476         rc = lnet_acceptor_get_tunables();
477         if (rc != 0)
478                 return rc;
479
480         init_completion(&lnet_acceptor_state.pta_signal);
481         rc = accept2secure(accept_type, &secure);
482         if (rc <= 0) {
483                 fini_completion(&lnet_acceptor_state.pta_signal);
484                 return rc;
485         }
486
487         if (lnet_count_acceptor_nis() == 0)  /* not required */
488                 return 0;
489
490         task = kthread_run(lnet_acceptor, (void *)(ulong_ptr_t)secure,
491                            "acceptor_%03ld", secure);
492         if (IS_ERR(task)) {
493                 rc2 = PTR_ERR(task);
494                 CERROR("Can't start acceptor thread: %ld\n", rc2);
495                 fini_completion(&lnet_acceptor_state.pta_signal);
496
497                 return -ESRCH;
498         }
499
500         /* wait for acceptor to startup */
501         wait_for_completion(&lnet_acceptor_state.pta_signal);
502
503         if (!lnet_acceptor_state.pta_shutdown) {
504                 /* started OK */
505                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
506                 return 0;
507         }
508
509         LASSERT(lnet_acceptor_state.pta_sock == NULL);
510         fini_completion(&lnet_acceptor_state.pta_signal);
511
512         return -ENETDOWN;
513 }
514
515 void
516 lnet_acceptor_stop(void)
517 {
518         if (lnet_acceptor_state.pta_shutdown) /* not running */
519                 return;
520
521         lnet_acceptor_state.pta_shutdown = 1;
522         libcfs_sock_abort_accept(lnet_acceptor_state.pta_sock);
523
524         /* block until acceptor signals exit */
525         wait_for_completion(&lnet_acceptor_state.pta_signal);
526
527         fini_completion(&lnet_acceptor_state.pta_signal);
528 }