Whamcloud - gitweb
3f48c7bbc5daa2fd9a05446895d055a16eda9c7c
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LNET
34
35 #include <linux/completion.h>
36 #include <net/sock.h>
37 #include <lnet/lib-lnet.h>
38
39 static int   accept_port    = 988;
40 static int   accept_backlog = 127;
41 static int   accept_timeout = 5;
42
43 static struct {
44         int                     pta_shutdown;
45         struct socket           *pta_sock;
46         struct completion       pta_signal;
47         struct net              *pta_ns;
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 module_param(accept, charp, 0444);
70 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
71 module_param(accept_port, int, 0444);
72 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
73 module_param(accept_backlog, int, 0444);
74 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
75 module_param(accept_timeout, int, 0644);
76 MODULE_PARM_DESC(accept_timeout, "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, struct net *ns)
155 {
156         struct lnet_acceptor_connreq cr;
157         struct socket           *sock;
158         int                     rc;
159         int                     port;
160         int                     fatal;
161
162         BUILD_BUG_ON(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, ns);
172                 if (rc != 0) {
173                         if (fatal)
174                                 goto failed;
175                         continue;
176                 }
177
178                 BUILD_BUG_ON(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) {
185                         /* single-shot proto check */
186                         if (test_and_clear_bit(2, &the_lnet.ln_testprotocompat))
187                                 cr.acr_version++;
188                         if (test_and_clear_bit(3, &the_lnet.ln_testprotocompat))
189                                 cr.acr_magic = LNET_PROTO_MAGIC;
190                 }
191
192                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
193                                        accept_timeout);
194                 if (rc != 0)
195                         goto failed_sock;
196
197                 *sockp = sock;
198                 return 0;
199         }
200
201         rc = -EADDRINUSE;
202         goto failed;
203
204 failed_sock:
205         sock_release(sock);
206 failed:
207         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
208         return rc;
209 }
210 EXPORT_SYMBOL(lnet_connect);
211
212 static int
213 lnet_accept(struct socket *sock, __u32 magic)
214 {
215         struct lnet_acceptor_connreq cr;
216         __u32                   peer_ip;
217         int                     peer_port;
218         int                     rc;
219         int                     flip;
220         struct lnet_ni *ni;
221         char                   *str;
222
223         LASSERT(sizeof(cr) <= 16);              /* not too big for the stack */
224
225         rc = lnet_sock_getaddr(sock, true, &peer_ip, &peer_port);
226         LASSERT(rc == 0);                       /* we succeeded before */
227
228         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
229
230                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
231                         /* future version compatibility!
232                          * When LNET unifies protocols over all LNDs, the first
233                          * thing sent will be a version query.  I send back
234                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
235
236                         memset(&cr, 0, sizeof(cr));
237                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
238                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
239                         rc = lnet_sock_write(sock, &cr, sizeof(cr),
240                                                accept_timeout);
241
242                         if (rc != 0)
243                                 CERROR("Error sending magic+version in response"
244                                        "to LNET magic from %pI4h: %d\n",
245                                        &peer_ip, rc);
246                         return -EPROTO;
247                 }
248
249                 if (lnet_accept_magic(magic, LNET_PROTO_TCP_MAGIC))
250                         str = "'old' socknal/tcpnal";
251                 else
252                         str = "unrecognised";
253
254                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h"
255                                    " magic %08x: %s acceptor protocol\n",
256                                    &peer_ip, magic, str);
257                 return -EPROTO;
258         }
259
260         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
261
262         rc = lnet_sock_read(sock, &cr.acr_version,
263                               sizeof(cr.acr_version),
264                               accept_timeout);
265         if (rc != 0) {
266                 CERROR("Error %d reading connection request version from "
267                        "%pI4h\n", rc, &peer_ip);
268                 return -EIO;
269         }
270
271         if (flip)
272                 __swab32s(&cr.acr_version);
273
274         if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
275                 /* future version compatibility!
276                  * An acceptor-specific protocol rev will first send a version
277                  * query.  I send back my current version to tell her I'm
278                  * "old". */
279                 int peer_version = cr.acr_version;
280
281                 memset(&cr, 0, sizeof(cr));
282                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
283                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
284
285                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
286                                        accept_timeout);
287
288                 if (rc != 0)
289                         CERROR("Error sending magic+version in response"
290                                "to version %d from %pI4h: %d\n",
291                                peer_version, &peer_ip, rc);
292                 return -EPROTO;
293         }
294
295         rc = lnet_sock_read(sock, &cr.acr_nid,
296                               sizeof(cr) -
297                               offsetof(struct lnet_acceptor_connreq, acr_nid),
298                               accept_timeout);
299         if (rc != 0) {
300                 CERROR("Error %d reading connection request from "
301                        "%pI4h\n", rc, &peer_ip);
302                 return -EIO;
303         }
304
305         if (flip)
306                 __swab64s(&cr.acr_nid);
307
308         ni = lnet_nid2ni_addref(cr.acr_nid);
309         if (ni == NULL ||               /* no matching net */
310             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
311                 if (ni != NULL)
312                         lnet_ni_decref(ni);
313                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h "
314                                    "for %s: No matching NI\n",
315                                    &peer_ip, libcfs_nid2str(cr.acr_nid));
316                 return -EPERM;
317         }
318
319         if (ni->ni_net->net_lnd->lnd_accept == NULL) {
320                 /* This catches a request for the loopback LND */
321                 lnet_ni_decref(ni);
322                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h "
323                                   "for %s: NI doesn not accept IP connections\n",
324                                   &peer_ip, libcfs_nid2str(cr.acr_nid));
325                 return -EPERM;
326         }
327
328         CDEBUG(D_NET, "Accept %s from %pI4h\n",
329                libcfs_nid2str(cr.acr_nid), &peer_ip);
330
331         rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
332
333         lnet_ni_decref(ni);
334         return rc;
335 }
336
337 static int
338 lnet_acceptor(void *arg)
339 {
340         struct socket  *newsock;
341         int            rc;
342         __u32          magic;
343         __u32          peer_ip;
344         int            peer_port;
345         int            secure = (int)((uintptr_t)arg);
346
347         LASSERT(lnet_acceptor_state.pta_sock == NULL);
348
349         cfs_block_allsigs();
350
351         rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock,
352                               0, accept_port, accept_backlog,
353                               lnet_acceptor_state.pta_ns);
354         if (rc != 0) {
355                 if (rc == -EADDRINUSE)
356                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
357                                            " %d: port already in use\n",
358                                            accept_port);
359                 else
360                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
361                                            "%d: unexpected error %d\n",
362                                            accept_port, rc);
363
364                 lnet_acceptor_state.pta_sock = NULL;
365         } else {
366                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
367         }
368
369         /* set init status and unblock parent */
370         lnet_acceptor_state.pta_shutdown = rc;
371         complete(&lnet_acceptor_state.pta_signal);
372
373         if (rc != 0)
374                 return rc;
375
376         while (!lnet_acceptor_state.pta_shutdown) {
377
378                 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
379                 if (rc != 0) {
380                         if (rc != -EAGAIN) {
381                                 CWARN("Accept error %d: pausing...\n", rc);
382                                 set_current_state(TASK_UNINTERRUPTIBLE);
383                                 schedule_timeout(cfs_time_seconds(1));
384                         }
385                         continue;
386                 }
387
388                 /* maybe we're waken up with lnet_sock_abort_accept() */
389                 if (lnet_acceptor_state.pta_shutdown) {
390                         sock_release(newsock);
391                         break;
392                 }
393
394                 rc = lnet_sock_getaddr(newsock, true, &peer_ip, &peer_port);
395                 if (rc != 0) {
396                         CERROR("Can't determine new connection's address\n");
397                         goto failed;
398                 }
399
400                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
401                         CERROR("Refusing connection from %pI4h: "
402                                "insecure port %d\n", &peer_ip, peer_port);
403                         goto failed;
404                 }
405
406                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
407                                       accept_timeout);
408                 if (rc != 0) {
409                         CERROR("Error %d reading connection request from "
410                                "%pI4h\n", rc, &peer_ip);
411                         goto failed;
412                 }
413
414                 rc = lnet_accept(newsock, magic);
415                 if (rc != 0)
416                         goto failed;
417
418                 continue;
419
420 failed:
421                 sock_release(newsock);
422         }
423
424         sock_release(lnet_acceptor_state.pta_sock);
425         lnet_acceptor_state.pta_sock = NULL;
426
427         CDEBUG(D_NET, "Acceptor stopping\n");
428
429         /* unblock lnet_acceptor_stop() */
430         complete(&lnet_acceptor_state.pta_signal);
431         return 0;
432 }
433
434 static inline int
435 accept2secure(const char *acc, long *sec)
436 {
437         if (!strcmp(acc, "secure")) {
438                 *sec = 1;
439                 return 1;
440         } else if (!strcmp(acc, "all")) {
441                 *sec = 0;
442                 return 1;
443         } else if (!strcmp(acc, "none")) {
444                 return 0;
445         } else {
446                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
447                                    acc);
448                 return -EINVAL;
449         }
450 }
451
452 int
453 lnet_acceptor_start(void)
454 {
455         struct task_struct *task;
456         int  rc;
457         long rc2;
458         long secure;
459
460         /* if acceptor is already running return immediately */
461         if (!lnet_acceptor_state.pta_shutdown)
462                 return 0;
463
464         LASSERT(lnet_acceptor_state.pta_sock == NULL);
465
466         rc = lnet_acceptor_get_tunables();
467         if (rc != 0)
468                 return rc;
469
470         init_completion(&lnet_acceptor_state.pta_signal);
471         rc = accept2secure(accept_type, &secure);
472         if (rc <= 0)
473                 return rc;
474
475         if (lnet_count_acceptor_nets() == 0)  /* not required */
476                 return 0;
477         if (current->nsproxy && current->nsproxy->net_ns)
478                 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
479         else
480                 lnet_acceptor_state.pta_ns = &init_net;
481         task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
482                            "acceptor_%03ld", secure);
483         if (IS_ERR(task)) {
484                 rc2 = PTR_ERR(task);
485                 CERROR("Can't start acceptor thread: %ld\n", rc2);
486                 return -ESRCH;
487         }
488
489         /* wait for acceptor to startup */
490         wait_for_completion(&lnet_acceptor_state.pta_signal);
491
492         if (!lnet_acceptor_state.pta_shutdown) {
493                 /* started OK */
494                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
495                 return 0;
496         }
497
498         LASSERT(lnet_acceptor_state.pta_sock == NULL);
499
500         return -ENETDOWN;
501 }
502
503 void
504 lnet_acceptor_stop(void)
505 {
506         struct sock *sk;
507
508         if (lnet_acceptor_state.pta_shutdown) /* not running */
509                 return;
510
511         lnet_acceptor_state.pta_shutdown = 1;
512
513         sk = lnet_acceptor_state.pta_sock->sk;
514
515         /* awake any sleepers using safe method */
516         sk->sk_state_change(sk);
517
518         /* block until acceptor signals exit */
519         wait_for_completion(&lnet_acceptor_state.pta_signal);
520 }