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