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