Whamcloud - gitweb
b6d85ef6b1a13668a51918bdd8e0f7b7c8426138
[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         struct socket           *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 %pI4h on port %d was "
105                         "refused: check that Lustre is running on that node.\n",
106                         libcfs_nid2str(peer_nid), &peer_ip, peer_port);
107                 break;
108         case -EHOSTUNREACH:
109         case -ENETUNREACH:
110                 CNETERR("Connection to %s at host %pI4h "
111                         "was unreachable: the network or that node may "
112                         "be down, or Lustre may be misconfigured.\n",
113                         libcfs_nid2str(peer_nid), &peer_ip);
114                 break;
115         case -ETIMEDOUT:
116                 CNETERR("Connection to %s at host %pI4h on "
117                         "port %d took too long: that node may be hung "
118                         "or experiencing high load.\n",
119                         libcfs_nid2str(peer_nid), &peer_ip, peer_port);
120                 break;
121         case -ECONNRESET:
122                 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h"
123                                    " on port %d was reset: "
124                                    "is it running a compatible version of "
125                                    "Lustre and is %s one of its NIDs?\n",
126                                    libcfs_nid2str(peer_nid), &peer_ip,
127                                    peer_port, libcfs_nid2str(peer_nid));
128                 break;
129         case -EPROTO:
130                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at "
131                                    "host %pI4h on port %d: is it running "
132                                    "a compatible version of Lustre?\n",
133                                    libcfs_nid2str(peer_nid), &peer_ip,
134                                    peer_port);
135                 break;
136         case -EADDRINUSE:
137                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to "
138                                    "connect to %s at host %pI4h on port "
139                                    "%d\n", libcfs_nid2str(peer_nid),
140                                    &peer_ip, peer_port);
141                 break;
142         default:
143                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s"
144                                    " at host %pI4h on port %d\n", rc,
145                                    libcfs_nid2str(peer_nid),
146                                    &peer_ip, peer_port);
147                 break;
148         }
149 }
150 EXPORT_SYMBOL(lnet_connect_console_error);
151
152 int
153 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
154             __u32 local_ip, __u32 peer_ip, int peer_port)
155 {
156         lnet_acceptor_connreq_t cr;
157         struct socket           *sock;
158         int                     rc;
159         int                     port;
160         int                     fatal;
161
162         CLASSERT (sizeof(cr) <= 16);            /* not too big to be on the stack */
163
164         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
165              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
166              --port) {
167                 /* Iterate through reserved ports. */
168
169                 rc = lnet_sock_connect(&sock, &fatal,
170                                          local_ip, port,
171                                          peer_ip, peer_port);
172                 if (rc != 0) {
173                         if (fatal)
174                                 goto failed;
175                         continue;
176                 }
177
178                 CLASSERT (LNET_PROTO_ACCEPTOR_VERSION == 1);
179
180                 cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
181                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
182                 cr.acr_nid     = peer_nid;
183
184                 if (the_lnet.ln_testprotocompat != 0) {
185                         /* single-shot proto check */
186                         lnet_net_lock(LNET_LOCK_EX);
187                         if ((the_lnet.ln_testprotocompat & 4) != 0) {
188                                 cr.acr_version++;
189                                 the_lnet.ln_testprotocompat &= ~4;
190                         }
191                         if ((the_lnet.ln_testprotocompat & 8) != 0) {
192                                 cr.acr_magic = LNET_PROTO_MAGIC;
193                                 the_lnet.ln_testprotocompat &= ~8;
194                         }
195                         lnet_net_unlock(LNET_LOCK_EX);
196                 }
197
198                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
199                                        accept_timeout);
200                 if (rc != 0)
201                         goto failed_sock;
202
203                 *sockp = sock;
204                 return 0;
205         }
206
207         rc = -EADDRINUSE;
208         goto failed;
209
210 failed_sock:
211         sock_release(sock);
212 failed:
213         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
214         return rc;
215 }
216 EXPORT_SYMBOL(lnet_connect);
217
218 static int
219 lnet_accept(struct socket *sock, __u32 magic)
220 {
221         lnet_acceptor_connreq_t cr;
222         __u32                   peer_ip;
223         int                     peer_port;
224         int                     rc;
225         int                     flip;
226         lnet_ni_t              *ni;
227         char                   *str;
228
229         LASSERT (sizeof(cr) <= 16);             /* not too big for the stack */
230
231         rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
232         LASSERT (rc == 0);                      /* we succeeded before */
233
234         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
235
236                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
237                         /* future version compatibility!
238                          * When LNET unifies protocols over all LNDs, the first
239                          * thing sent will be a version query.  I send back
240                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
241
242                         memset (&cr, 0, sizeof(cr));
243                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
244                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
245                         rc = lnet_sock_write(sock, &cr, sizeof(cr),
246                                                accept_timeout);
247
248                         if (rc != 0)
249                                 CERROR("Error sending magic+version in response"
250                                        "to LNET magic from %pI4h: %d\n",
251                                        &peer_ip, rc);
252                         return -EPROTO;
253                 }
254
255                 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
256                         str = "'old' socknal/tcpnal";
257                 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
258                         str = "'old' ranal";
259                 else
260                         str = "unrecognised";
261
262                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
263                                    " magic %08x: %s acceptor protocol\n",
264                                    &peer_ip, magic, str);
265                 return -EPROTO;
266         }
267
268         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
269
270         rc = lnet_sock_read(sock, &cr.acr_version,
271                               sizeof(cr.acr_version),
272                               accept_timeout);
273         if (rc != 0) {
274                 CERROR("Error %d reading connection request version from "
275                        "%pI4h\n", rc, &peer_ip);
276                 return -EIO;
277         }
278
279         if (flip)
280                 __swab32s(&cr.acr_version);
281
282         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
283                 /* future version compatibility!
284                  * An acceptor-specific protocol rev will first send a version
285                  * query.  I send back my current version to tell her I'm
286                  * "old". */
287                 int peer_version = cr.acr_version;
288
289                 memset (&cr, 0, sizeof(cr));
290                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
291                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
292
293                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
294                                        accept_timeout);
295
296                 if (rc != 0)
297                         CERROR("Error sending magic+version in response"
298                                "to version %d from %pI4h: %d\n",
299                                peer_version, &peer_ip, rc);
300                 return -EPROTO;
301         }
302
303         rc = lnet_sock_read(sock, &cr.acr_nid,
304                               sizeof(cr) -
305                               offsetof(lnet_acceptor_connreq_t, acr_nid),
306                               accept_timeout);
307         if (rc != 0) {
308                 CERROR("Error %d reading connection request from "
309                        "%pI4h\n", rc, &peer_ip);
310                 return -EIO;
311         }
312
313         if (flip)
314                 __swab64s(&cr.acr_nid);
315
316         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
317         if (ni == NULL ||               /* no matching net */
318             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
319                 if (ni != NULL)
320                         lnet_ni_decref(ni);
321                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
322                                    "for %s: No matching NI\n",
323                                    &peer_ip, libcfs_nid2str(cr.acr_nid));
324                 return -EPERM;
325         }
326
327         if (ni->ni_lnd->lnd_accept == NULL) {
328                 /* This catches a request for the loopback LND */
329                 lnet_ni_decref(ni);
330                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
331                                   "for %s: NI doesn not accept IP connections\n",
332                                   &peer_ip, libcfs_nid2str(cr.acr_nid));
333                 return -EPERM;
334         }
335
336         CDEBUG(D_NET, "Accept %s from %pI4h\n",
337                libcfs_nid2str(cr.acr_nid), &peer_ip);
338
339         rc = ni->ni_lnd->lnd_accept(ni, sock);
340
341         lnet_ni_decref(ni);
342         return rc;
343 }
344
345 static int
346 lnet_acceptor(void *arg)
347 {
348         struct socket  *newsock;
349         int            rc;
350         __u32          magic;
351         __u32          peer_ip;
352         int            peer_port;
353         int            secure = (int)((long_ptr_t)arg);
354
355         LASSERT (lnet_acceptor_state.pta_sock == NULL);
356
357         cfs_block_allsigs();
358
359         rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
360                                 0, accept_port, accept_backlog);
361         if (rc != 0) {
362                 if (rc == -EADDRINUSE)
363                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
364                                            " %d: port already in use\n",
365                                            accept_port);
366                 else
367                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
368                                            "%d: unexpected error %d\n",
369                                            accept_port, rc);
370
371                 lnet_acceptor_state.pta_sock = NULL;
372         } else {
373                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
374         }
375
376         /* set init status and unblock parent */
377         lnet_acceptor_state.pta_shutdown = rc;
378         complete(&lnet_acceptor_state.pta_signal);
379
380         if (rc != 0)
381                 return rc;
382
383         while (!lnet_acceptor_state.pta_shutdown) {
384
385                 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
386                 if (rc != 0) {
387                         if (rc != -EAGAIN) {
388                                 CWARN("Accept error %d: pausing...\n", rc);
389                                 cfs_pause(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                 fini_completion(&lnet_acceptor_state.pta_signal);
480                 return rc;
481         }
482
483         if (lnet_count_acceptor_nis() == 0)  /* not required */
484                 return 0;
485
486         task = kthread_run(lnet_acceptor, (void *)(ulong_ptr_t)secure,
487                            "acceptor_%03ld", secure);
488         if (IS_ERR(task)) {
489                 rc2 = PTR_ERR(task);
490                 CERROR("Can't start acceptor thread: %ld\n", rc2);
491                 fini_completion(&lnet_acceptor_state.pta_signal);
492
493                 return -ESRCH;
494         }
495
496         /* wait for acceptor to startup */
497         wait_for_completion(&lnet_acceptor_state.pta_signal);
498
499         if (!lnet_acceptor_state.pta_shutdown) {
500                 /* started OK */
501                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
502                 return 0;
503         }
504
505         LASSERT(lnet_acceptor_state.pta_sock == NULL);
506         fini_completion(&lnet_acceptor_state.pta_signal);
507
508         return -ENETDOWN;
509 }
510
511 void
512 lnet_acceptor_stop(void)
513 {
514         if (lnet_acceptor_state.pta_shutdown) /* not running */
515                 return;
516
517         lnet_acceptor_state.pta_shutdown = 1;
518         wake_up_all(sk_sleep(lnet_acceptor_state.pta_sock->sk));
519
520         /* block until acceptor signals exit */
521         wait_for_completion(&lnet_acceptor_state.pta_signal);
522
523         fini_completion(&lnet_acceptor_state.pta_signal);
524 }