Whamcloud - gitweb
a0c0783533778f6baa9d93a888cde611c7ab9de6
[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 <linux/completion.h>
39 #include <lnet/lib-lnet.h>
40
41 static int   accept_port    = 988;
42 static int   accept_backlog = 127;
43 static int   accept_timeout = 5;
44
45 static struct {
46         int                     pta_shutdown;
47         struct socket           *pta_sock;
48         struct completion       pta_signal;
49 } lnet_acceptor_state = {
50         .pta_shutdown = 1
51 };
52
53 int
54 lnet_acceptor_port(void)
55 {
56         return accept_port;
57 }
58
59 static inline int
60 lnet_accept_magic(__u32 magic, __u32 constant)
61 {
62         return (magic == constant ||
63                 magic == __swab32(constant));
64 }
65
66 EXPORT_SYMBOL(lnet_acceptor_port);
67
68 static char *accept = "secure";
69
70 CFS_MODULE_PARM(accept, "s", charp, 0444,
71                 "Accept connections (secure|all|none)");
72 CFS_MODULE_PARM(accept_port, "i", int, 0444,
73                 "Acceptor's port (same on all nodes)");
74 CFS_MODULE_PARM(accept_backlog, "i", int, 0444,
75                 "Acceptor's listen backlog");
76 CFS_MODULE_PARM(accept_timeout, "i", int, 0644,
77                 "Acceptor's timeout (seconds)");
78
79 static char *accept_type = NULL;
80
81 static int
82 lnet_acceptor_get_tunables(void)
83 {
84         /* Userland acceptor uses 'accept_type' instead of 'accept', due to
85          * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
86          * for compatibility. Hence the trick. */
87         accept_type = accept;
88         return 0;
89 }
90
91 int
92 lnet_acceptor_timeout(void)
93 {
94         return accept_timeout;
95 }
96 EXPORT_SYMBOL(lnet_acceptor_timeout);
97
98 void
99 lnet_connect_console_error (int rc, lnet_nid_t peer_nid,
100                            __u32 peer_ip, int peer_port)
101 {
102         switch (rc) {
103         /* "normal" errors */
104         case -ECONNREFUSED:
105                 CNETERR("Connection to %s at host %pI4h on port %d was "
106                         "refused: check that Lustre is running on that node.\n",
107                         libcfs_nid2str(peer_nid), &peer_ip, peer_port);
108                 break;
109         case -EHOSTUNREACH:
110         case -ENETUNREACH:
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);
115                 break;
116         case -ETIMEDOUT:
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), &peer_ip, peer_port);
121                 break;
122         case -ECONNRESET:
123                 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
124                                    " on port %d was reset: "
125                                    "is it running a compatible version of "
126                                    "Lustre and is %s one of its NIDs?\n",
127                                    libcfs_nid2str(peer_nid), &peer_ip,
128                                    peer_port, libcfs_nid2str(peer_nid));
129                 break;
130         case -EPROTO:
131                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
132                                    "host %pI4h on port %d: is it running "
133                                    "a compatible version of Lustre?\n",
134                                    libcfs_nid2str(peer_nid), &peer_ip,
135                                    peer_port);
136                 break;
137         case -EADDRINUSE:
138                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
139                                    "connect to %s at host %pI4h on port "
140                                    "%d\n", libcfs_nid2str(peer_nid),
141                                    &peer_ip, peer_port);
142                 break;
143         default:
144                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
145                                    " at host %pI4h on port %d\n", rc,
146                                    libcfs_nid2str(peer_nid),
147                                    &peer_ip, peer_port);
148                 break;
149         }
150 }
151 EXPORT_SYMBOL(lnet_connect_console_error);
152
153 int
154 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
155             __u32 local_ip, __u32 peer_ip, int peer_port)
156 {
157         lnet_acceptor_connreq_t cr;
158         struct socket           *sock;
159         int                     rc;
160         int                     port;
161         int                     fatal;
162
163         CLASSERT (sizeof(cr) <= 16);            /* not too big to be on the stack */
164
165         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
166              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
167              --port) {
168                 /* Iterate through reserved ports. */
169
170                 rc = lnet_sock_connect(&sock, &fatal,
171                                          local_ip, port,
172                                          peer_ip, peer_port);
173                 if (rc != 0) {
174                         if (fatal)
175                                 goto failed;
176                         continue;
177                 }
178
179                 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
180
181                 cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
182                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
183                 cr.acr_nid     = peer_nid;
184
185                 if (the_lnet.ln_testprotocompat != 0) {
186                         /* single-shot proto check */
187                         lnet_net_lock(LNET_LOCK_EX);
188                         if ((the_lnet.ln_testprotocompat & 4) != 0) {
189                                 cr.acr_version++;
190                                 the_lnet.ln_testprotocompat &= ~4;
191                         }
192                         if ((the_lnet.ln_testprotocompat & 8) != 0) {
193                                 cr.acr_magic = LNET_PROTO_MAGIC;
194                                 the_lnet.ln_testprotocompat &= ~8;
195                         }
196                         lnet_net_unlock(LNET_LOCK_EX);
197                 }
198
199                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
200                                        accept_timeout);
201                 if (rc != 0)
202                         goto failed_sock;
203
204                 *sockp = sock;
205                 return 0;
206         }
207
208         rc = -EADDRINUSE;
209         goto failed;
210
211 failed_sock:
212         sock_release(sock);
213 failed:
214         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
215         return rc;
216 }
217 EXPORT_SYMBOL(lnet_connect);
218
219 static int
220 lnet_accept(struct socket *sock, __u32 magic)
221 {
222         lnet_acceptor_connreq_t cr;
223         __u32                   peer_ip;
224         int                     peer_port;
225         int                     rc;
226         int                     flip;
227         lnet_ni_t              *ni;
228         char                   *str;
229
230         LASSERT (sizeof(cr) <= 16);             /* not too big for the stack */
231
232         rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
233         LASSERT (rc == 0);                      /* we succeeded before */
234
235         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
236
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" */
242
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),
247                                                accept_timeout);
248
249                         if (rc != 0)
250                                 CERROR("Error sending magic+version in response"
251                                        "to LNET magic from %pI4h: %d\n",
252                                        &peer_ip, rc);
253                         return -EPROTO;
254                 }
255
256                 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
257                         str = "'old' socknal/tcpnal";
258                 else
259                         str = "unrecognised";
260
261                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
262                                    " magic %08x: %s acceptor protocol\n",
263                                    &peer_ip, magic, str);
264                 return -EPROTO;
265         }
266
267         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
268
269         rc = lnet_sock_read(sock, &cr.acr_version,
270                               sizeof(cr.acr_version),
271                               accept_timeout);
272         if (rc != 0) {
273                 CERROR("Error %d reading connection request version from "
274                        "%pI4h\n", rc, &peer_ip);
275                 return -EIO;
276         }
277
278         if (flip)
279                 __swab32s(&cr.acr_version);
280
281         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
282                 /* future version compatibility!
283                  * An acceptor-specific protocol rev will first send a version
284                  * query.  I send back my current version to tell her I'm
285                  * "old". */
286                 int peer_version = cr.acr_version;
287
288                 memset (&cr, 0, sizeof(cr));
289                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
290                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
291
292                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
293                                        accept_timeout);
294
295                 if (rc != 0)
296                         CERROR("Error sending magic+version in response"
297                                "to version %d from %pI4h: %d\n",
298                                peer_version, &peer_ip, rc);
299                 return -EPROTO;
300         }
301
302         rc = lnet_sock_read(sock, &cr.acr_nid,
303                               sizeof(cr) -
304                               offsetof(lnet_acceptor_connreq_t, acr_nid),
305                               accept_timeout);
306         if (rc != 0) {
307                 CERROR("Error %d reading connection request from "
308                        "%pI4h\n", rc, &peer_ip);
309                 return -EIO;
310         }
311
312         if (flip)
313                 __swab64s(&cr.acr_nid);
314
315         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
316         if (ni == NULL ||               /* no matching net */
317             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
318                 if (ni != NULL)
319                         lnet_ni_decref(ni);
320                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
321                                    "for %s: No matching NI\n",
322                                    &peer_ip, libcfs_nid2str(cr.acr_nid));
323                 return -EPERM;
324         }
325
326         if (ni->ni_lnd->lnd_accept == NULL) {
327                 /* This catches a request for the loopback LND */
328                 lnet_ni_decref(ni);
329                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
330                                   "for %s: NI doesn not accept IP connections\n",
331                                   &peer_ip, libcfs_nid2str(cr.acr_nid));
332                 return -EPERM;
333         }
334
335         CDEBUG(D_NET, "Accept %s from %pI4h\n",
336                libcfs_nid2str(cr.acr_nid), &peer_ip);
337
338         rc = ni->ni_lnd->lnd_accept(ni, sock);
339
340         lnet_ni_decref(ni);
341         return rc;
342 }
343
344 static int
345 lnet_acceptor(void *arg)
346 {
347         struct socket  *newsock;
348         int            rc;
349         __u32          magic;
350         __u32          peer_ip;
351         int            peer_port;
352         int            secure = (int)((long_ptr_t)arg);
353
354         LASSERT (lnet_acceptor_state.pta_sock == NULL);
355
356         cfs_block_allsigs();
357
358         rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
359                                 0, accept_port, accept_backlog);
360         if (rc != 0) {
361                 if (rc == -EADDRINUSE)
362                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
363                                            " %d: port already in use\n",
364                                            accept_port);
365                 else
366                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
367                                            "%d: unexpected error %d\n",
368                                            accept_port, rc);
369
370                 lnet_acceptor_state.pta_sock = NULL;
371         } else {
372                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
373         }
374
375         /* set init status and unblock parent */
376         lnet_acceptor_state.pta_shutdown = rc;
377         complete(&lnet_acceptor_state.pta_signal);
378
379         if (rc != 0)
380                 return rc;
381
382         while (!lnet_acceptor_state.pta_shutdown) {
383
384                 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
385                 if (rc != 0) {
386                         if (rc != -EAGAIN) {
387                                 CWARN("Accept error %d: pausing...\n", rc);
388                                 set_current_state(TASK_UNINTERRUPTIBLE);
389                                 schedule_timeout(cfs_time_seconds(1));
390                         }
391                         continue;
392                 }
393
394                 /* maybe we're waken up with lnet_sock_abort_accept() */
395                 if (lnet_acceptor_state.pta_shutdown) {
396                         sock_release(newsock);
397                         break;
398                 }
399
400                 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
401                 if (rc != 0) {
402                         CERROR("Can't determine new connection's address\n");
403                         goto failed;
404                 }
405
406                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
407                         CERROR("Refusing connection from %pI4h: "
408                                "insecure port %d\n", &peer_ip, peer_port);
409                         goto failed;
410                 }
411
412                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
413                                       accept_timeout);
414                 if (rc != 0) {
415                         CERROR("Error %d reading connection request from "
416                                "%pI4h\n", rc, &peer_ip);
417                         goto failed;
418                 }
419
420                 rc = lnet_accept(newsock, magic);
421                 if (rc != 0)
422                         goto failed;
423
424                 continue;
425
426 failed:
427                 sock_release(newsock);
428         }
429
430         sock_release(lnet_acceptor_state.pta_sock);
431         lnet_acceptor_state.pta_sock = NULL;
432
433         CDEBUG(D_NET, "Acceptor stopping\n");
434
435         /* unblock lnet_acceptor_stop() */
436         complete(&lnet_acceptor_state.pta_signal);
437         return 0;
438 }
439
440 static inline int
441 accept2secure(const char *acc, long *sec)
442 {
443         if (!strcmp(acc, "secure")) {
444                 *sec = 1;
445                 return 1;
446         } else if (!strcmp(acc, "all")) {
447                 *sec = 0;
448                 return 1;
449         } else if (!strcmp(acc, "none")) {
450                 return 0;
451         } else {
452                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
453                                    acc);
454                 return -EINVAL;
455         }
456 }
457
458 int
459 lnet_acceptor_start(void)
460 {
461         struct task_struct *task;
462         int  rc;
463         long rc2;
464         long secure;
465
466         /* if acceptor is already running return immediately */
467         if (!lnet_acceptor_state.pta_shutdown)
468                 return 0;
469
470         LASSERT (lnet_acceptor_state.pta_sock == NULL);
471
472         rc = lnet_acceptor_get_tunables();
473         if (rc != 0)
474                 return rc;
475
476         init_completion(&lnet_acceptor_state.pta_signal);
477         rc = accept2secure(accept_type, &secure);
478         if (rc <= 0)
479                 return rc;
480
481         if (lnet_count_acceptor_nis() == 0)  /* not required */
482                 return 0;
483
484         task = kthread_run(lnet_acceptor, (void *)(ulong_ptr_t)secure,
485                            "acceptor_%03ld", secure);
486         if (IS_ERR(task)) {
487                 rc2 = PTR_ERR(task);
488                 CERROR("Can't start acceptor thread: %ld\n", rc2);
489
490                 return -ESRCH;
491         }
492
493         /* wait for acceptor to startup */
494         wait_for_completion(&lnet_acceptor_state.pta_signal);
495
496         if (!lnet_acceptor_state.pta_shutdown) {
497                 /* started OK */
498                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
499                 return 0;
500         }
501
502         LASSERT(lnet_acceptor_state.pta_sock == NULL);
503
504         return -ENETDOWN;
505 }
506
507 void
508 lnet_acceptor_stop(void)
509 {
510         if (lnet_acceptor_state.pta_shutdown) /* not running */
511                 return;
512
513         lnet_acceptor_state.pta_shutdown = 1;
514         wake_up_all(sk_sleep(lnet_acceptor_state.pta_sock->sk));
515
516         /* block until acceptor signals exit */
517         wait_for_completion(&lnet_acceptor_state.pta_signal);
518 }