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