Whamcloud - gitweb
LU-14945 lnet: don't use hops to determine the route state
[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  */
31
32 #define DEBUG_SUBSYSTEM S_LNET
33
34 #include <linux/completion.h>
35 #include <net/sock.h>
36 #include <lnet/lib-lnet.h>
37 #include <linux/sunrpc/addr.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         wait_queue_head_t       pta_waitq;
49         atomic_t                pta_ready;
50 #ifdef HAVE_SK_DATA_READY_ONE_ARG
51         void                    (*pta_odata)(struct sock *);
52 #else
53         void                    (*pta_odata)(struct sock *, int);
54 #endif
55 } lnet_acceptor_state = {
56         .pta_shutdown = 1
57 };
58
59 int
60 lnet_acceptor_port(void)
61 {
62         return accept_port;
63 }
64
65 static inline int
66 lnet_accept_magic(__u32 magic, __u32 constant)
67 {
68         return (magic == constant ||
69                 magic == __swab32(constant));
70 }
71
72 EXPORT_SYMBOL(lnet_acceptor_port);
73
74 static char *accept_type = "secure";
75
76 module_param_named(accept, accept_type, charp, 0444);
77 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
78 module_param(accept_port, int, 0444);
79 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
80 module_param(accept_backlog, int, 0444);
81 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
82 module_param(accept_timeout, int, 0644);
83 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
84
85 int
86 lnet_acceptor_timeout(void)
87 {
88         return accept_timeout;
89 }
90 EXPORT_SYMBOL(lnet_acceptor_timeout);
91
92 void
93 lnet_connect_console_error(int rc, struct lnet_nid *peer_nid,
94                            struct sockaddr *sa)
95 {
96         switch (rc) {
97         /* "normal" errors */
98         case -ECONNREFUSED:
99                 CNETERR("Connection to %s at host %pISp was refused: check that Lustre is running on that node.\n",
100                         libcfs_nidstr(peer_nid), sa);
101                 break;
102         case -EHOSTUNREACH:
103         case -ENETUNREACH:
104                 CNETERR("Connection to %s at host %pIS was unreachable: the network or that node may be down, or Lustre may be misconfigured.\n",
105                         libcfs_nidstr(peer_nid), sa);
106                 break;
107         case -ETIMEDOUT:
108                 CNETERR("Connection to %s at host %pISp took too long: that node may be hung or experiencing high load.\n",
109                         libcfs_nidstr(peer_nid), sa);
110                 break;
111         case -ECONNRESET:
112                 LCONSOLE_ERROR_MSG(0x11b,
113                                    "Connection to %s at host %pISp was reset: is it running a compatible version of Lustre and is %s one of its NIDs?\n",
114                                    libcfs_nidstr(peer_nid), sa,
115                                    libcfs_nidstr(peer_nid));
116                 break;
117         case -EPROTO:
118                 LCONSOLE_ERROR_MSG(0x11c,
119                                    "Protocol error connecting to %s at host %pISp: is it running a compatible version of Lustre?\n",
120                                    libcfs_nidstr(peer_nid), sa);
121                 break;
122         case -EADDRINUSE:
123                 LCONSOLE_ERROR_MSG(0x11d,
124                                    "No privileged ports available to connect to %s at host %pISp\n",
125                                    libcfs_nidstr(peer_nid), sa);
126                 break;
127         default:
128                 LCONSOLE_ERROR_MSG(0x11e,
129                                    "Unexpected error %d connecting to %s at host %pISp\n",
130                                    rc, libcfs_nidstr(peer_nid), sa);
131                 break;
132         }
133 }
134 EXPORT_SYMBOL(lnet_connect_console_error);
135
136 struct socket *
137 lnet_connect(struct lnet_nid *peer_nid, int interface,
138              struct sockaddr *peeraddr,
139              struct net *ns)
140 {
141         struct lnet_acceptor_connreq cr1;
142         struct lnet_acceptor_connreq_v2 cr2;
143         void *cr;
144         int crsize;
145         struct socket *sock;
146         int rc;
147         int port;
148
149         BUILD_BUG_ON(sizeof(cr) > 16); /* not too big to be on the stack */
150
151         LASSERT(peeraddr->sa_family == AF_INET ||
152                 peeraddr->sa_family == AF_INET6);
153
154         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
155              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
156              --port) {
157                 /* Iterate through reserved ports. */
158                 sock = lnet_sock_connect(interface, port, peeraddr, ns);
159                 if (IS_ERR(sock)) {
160                         rc = PTR_ERR(sock);
161                         if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL)
162                                 continue;
163                         goto failed;
164                 }
165
166                 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
167
168                 if (nid_is_nid4(peer_nid)) {
169                         cr1.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
170                         cr1.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
171                         cr1.acr_nid     = lnet_nid_to_nid4(peer_nid);
172                         cr = &cr1;
173                         crsize = sizeof(cr1);
174
175                         if (the_lnet.ln_testprotocompat) {
176                                 /* single-shot proto check */
177                                 if (test_and_clear_bit(
178                                             2, &the_lnet.ln_testprotocompat))
179                                         cr1.acr_version++;
180                                 if (test_and_clear_bit(
181                                             3, &the_lnet.ln_testprotocompat))
182                                         cr1.acr_magic = LNET_PROTO_MAGIC;
183                         }
184
185                 } else {
186                         cr2.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
187                         cr2.acr_version = LNET_PROTO_ACCEPTOR_VERSION_16;
188                         cr2.acr_nid     = *peer_nid;
189                         cr = &cr2;
190                         crsize = sizeof(cr2);
191                 }
192
193                 rc = lnet_sock_write(sock, cr, crsize, accept_timeout);
194                 if (rc != 0)
195                         goto failed_sock;
196
197                 return sock;
198         }
199
200         rc = -EADDRINUSE;
201         goto failed;
202
203 failed_sock:
204         sock_release(sock);
205 failed:
206         lnet_connect_console_error(rc, peer_nid, peeraddr);
207         return ERR_PTR(rc);
208 }
209 EXPORT_SYMBOL(lnet_connect);
210
211 static int
212 lnet_accept(struct socket *sock, __u32 magic)
213 {
214         struct lnet_acceptor_connreq cr;
215         struct lnet_acceptor_connreq_v2 cr2;
216         struct lnet_nid nid;
217         struct sockaddr_storage peer;
218         int peer_version;
219         int rc;
220         int flip;
221         struct lnet_ni *ni;
222         char *str;
223
224         LASSERT(sizeof(cr) <= 16);              /* not too big for the stack */
225
226         rc = lnet_sock_getaddr(sock, true, &peer);
227         if (rc != 0) {
228                 CERROR("Can't determine new connection's address\n");
229                 return rc;
230         }
231
232         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
233
234                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
235                         /* future version compatibility!
236                          * When LNET unifies protocols over all LNDs, the first
237                          * thing sent will be a version query.  I send back
238                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
239
240                         memset(&cr, 0, sizeof(cr));
241                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
242                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
243                         rc = lnet_sock_write(sock, &cr, sizeof(cr),
244                                                accept_timeout);
245
246                         if (rc != 0)
247                                 CERROR("Error sending magic+version in response to LNET magic from %pIS: %d\n",
248                                        &peer, rc);
249                         return -EPROTO;
250                 }
251
252                 if (lnet_accept_magic(magic, LNET_PROTO_TCP_MAGIC))
253                         str = "'old' socknal/tcpnal";
254                 else
255                         str = "unrecognised";
256
257                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pIS"
258                                    " magic %08x: %s acceptor protocol\n",
259                                    &peer, magic, str);
260                 return -EPROTO;
261         }
262
263         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
264
265         rc = lnet_sock_read(sock, &cr.acr_version,
266                               sizeof(cr.acr_version),
267                               accept_timeout);
268         if (rc != 0) {
269                 CERROR("Error %d reading connection request version from %pIS\n",
270                        rc, &peer);
271                 return -EIO;
272         }
273
274         if (flip)
275                 __swab32s(&cr.acr_version);
276
277         switch (cr.acr_version) {
278         default:
279                 /* future version compatibility!
280                  * An acceptor-specific protocol rev will first send a version
281                  * query.  I send back my current version to tell her I'm
282                  * "old". */
283                 peer_version = cr.acr_version;
284
285                 memset(&cr, 0, sizeof(cr));
286                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
287                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
288
289                 rc = lnet_sock_write(sock, &cr, sizeof(cr),
290                                        accept_timeout);
291
292                 if (rc != 0)
293                         CERROR("Error sending magic+version in response to version %d from %pIS: %d\n",
294                                peer_version, &peer, rc);
295                 return -EPROTO;
296
297         case LNET_PROTO_ACCEPTOR_VERSION:
298
299                 rc = lnet_sock_read(sock, &cr.acr_nid,
300                                     sizeof(cr) -
301                                     offsetof(struct lnet_acceptor_connreq,
302                                              acr_nid),
303                                     accept_timeout);
304                 if (rc)
305                         break;
306                 if (flip)
307                         __swab64s(&cr.acr_nid);
308
309                 lnet_nid4_to_nid(cr.acr_nid, &nid);
310                 break;
311
312         case LNET_PROTO_ACCEPTOR_VERSION_16:
313                 rc = lnet_sock_read(sock, &cr2.acr_nid,
314                                     sizeof(cr2) -
315                                     offsetof(struct lnet_acceptor_connreq_v2,
316                                              acr_nid),
317                                     accept_timeout);
318                 if (rc)
319                         break;
320                 nid = cr2.acr_nid;
321                 break;
322         }
323         if (rc != 0) {
324                 CERROR("Error %d reading connection request from %pIS\n",
325                        rc, &peer);
326                 return -EIO;
327         }
328
329         ni = lnet_nid_to_ni_addref(&nid);
330         if (ni == NULL ||               /* no matching net */
331             !nid_same(&ni->ni_nid, &nid)) {
332                 /* right NET, wrong NID! */
333                 if (ni != NULL)
334                         lnet_ni_decref(ni);
335                 LCONSOLE_ERROR_MSG(0x120,
336                                    "Refusing connection from %pIS for %s: No matching NI\n",
337                                    &peer, libcfs_nidstr(&nid));
338                 return -EPERM;
339         }
340
341         if (ni->ni_net->net_lnd->lnd_accept == NULL) {
342                 /* This catches a request for the loopback LND */
343                 lnet_ni_decref(ni);
344                 LCONSOLE_ERROR_MSG(0x121,
345                                    "Refusing connection from %pIS for %s: NI doesn not accept IP connections\n",
346                                   &peer, libcfs_nidstr(&nid));
347                 return -EPERM;
348         }
349
350         CDEBUG(D_NET, "Accept %s from %pI4h\n", libcfs_nidstr(&nid), &peer);
351
352         rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
353
354         lnet_ni_decref(ni);
355         return rc;
356 }
357
358 #ifdef HAVE_SK_DATA_READY_ONE_ARG
359 static void lnet_acceptor_ready(struct sock *sk)
360 #else
361 static void lnet_acceptor_ready(struct sock *sk, int len)
362 #endif
363 {
364         /* Ensure pta_odata has actually been set before calling it */
365         rmb();
366 #ifdef HAVE_SK_DATA_READY_ONE_ARG
367         lnet_acceptor_state.pta_odata(sk);
368 #else
369         lnet_acceptor_state.pta_odata(sk, 0);
370 #endif
371
372         atomic_set(&lnet_acceptor_state.pta_ready, 1);
373         wake_up(&lnet_acceptor_state.pta_waitq);
374 }
375
376 static int
377 lnet_acceptor(void *arg)
378 {
379         struct socket  *newsock;
380         int            rc;
381         __u32          magic;
382         struct sockaddr_storage peer;
383         int            secure = (int)((uintptr_t)arg);
384
385         LASSERT(lnet_acceptor_state.pta_sock == NULL);
386
387         lnet_acceptor_state.pta_sock =
388                 lnet_sock_listen(accept_port, accept_backlog,
389                                  lnet_acceptor_state.pta_ns);
390         if (IS_ERR(lnet_acceptor_state.pta_sock)) {
391                 rc = PTR_ERR(lnet_acceptor_state.pta_sock);
392                 if (rc == -EADDRINUSE)
393                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
394                                            " %d: port already in use\n",
395                                            accept_port);
396                 else
397                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
398                                            "%d: unexpected error %d\n",
399                                            accept_port, rc);
400
401                 lnet_acceptor_state.pta_sock = NULL;
402         } else {
403                 rc = 0;
404                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
405                 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
406                 lnet_acceptor_state.pta_odata =
407                         lnet_acceptor_state.pta_sock->sk->sk_data_ready;
408                 /* ensure pta_odata gets set before there is any chance of
409                  * lnet_accept_ready() trying to read it.
410                  */
411                 wmb();
412                 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
413                         lnet_acceptor_ready;
414                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
415         }
416
417         /* set init status and unblock parent */
418         lnet_acceptor_state.pta_shutdown = rc;
419         complete(&lnet_acceptor_state.pta_signal);
420
421         if (rc != 0)
422                 return rc;
423
424         while (!lnet_acceptor_state.pta_shutdown) {
425
426                 wait_event_idle(lnet_acceptor_state.pta_waitq,
427                                 lnet_acceptor_state.pta_shutdown ||
428                                 atomic_read(&lnet_acceptor_state.pta_ready));
429                 if (!atomic_read(&lnet_acceptor_state.pta_ready))
430                         continue;
431                 atomic_set(&lnet_acceptor_state.pta_ready, 0);
432                 rc = kernel_accept(lnet_acceptor_state.pta_sock, &newsock,
433                                    SOCK_NONBLOCK);
434                 if (rc != 0) {
435                         if (rc != -EAGAIN) {
436                                 CWARN("Accept error %d: pausing...\n", rc);
437                                 schedule_timeout_uninterruptible(
438                                         cfs_time_seconds(1));
439                         }
440                         continue;
441                 }
442
443                 /* make sure we call lnet_sock_accept() again, until it fails */
444                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
445
446                 rc = lnet_sock_getaddr(newsock, true, &peer);
447                 if (rc != 0) {
448                         CERROR("Can't determine new connection's address\n");
449                         goto failed;
450                 }
451
452                 if (secure &&
453                     rpc_get_port((struct sockaddr *)&peer) >
454                     LNET_ACCEPTOR_MAX_RESERVED_PORT) {
455                         CERROR("Refusing connection from %pISp: insecure port.\n",
456                                &peer);
457                         goto failed;
458                 }
459
460                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
461                                       accept_timeout);
462                 if (rc != 0) {
463                         CERROR("Error %d reading connection request from %pIS\n",
464                                rc, &peer);
465                         goto failed;
466                 }
467
468                 rc = lnet_accept(newsock, magic);
469                 if (rc != 0)
470                         goto failed;
471
472                 continue;
473
474 failed:
475                 sock_release(newsock);
476         }
477
478         lnet_acceptor_state.pta_sock->sk->sk_data_ready =
479                 lnet_acceptor_state.pta_odata;
480         sock_release(lnet_acceptor_state.pta_sock);
481         lnet_acceptor_state.pta_sock = NULL;
482
483         CDEBUG(D_NET, "Acceptor stopping\n");
484
485         /* unblock lnet_acceptor_stop() */
486         complete(&lnet_acceptor_state.pta_signal);
487         return 0;
488 }
489
490 static inline int
491 accept2secure(const char *acc, long *sec)
492 {
493         if (!strcmp(acc, "secure")) {
494                 *sec = 1;
495                 return 1;
496         } else if (!strcmp(acc, "all")) {
497                 *sec = 0;
498                 return 1;
499         } else if (!strcmp(acc, "none")) {
500                 return 0;
501         } else {
502                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
503                                    acc);
504                 return -EINVAL;
505         }
506 }
507
508 int
509 lnet_acceptor_start(void)
510 {
511         struct task_struct *task;
512         int  rc;
513         long rc2;
514         long secure;
515
516         /* if acceptor is already running return immediately */
517         if (!lnet_acceptor_state.pta_shutdown)
518                 return 0;
519
520         LASSERT(lnet_acceptor_state.pta_sock == NULL);
521
522         init_completion(&lnet_acceptor_state.pta_signal);
523         rc = accept2secure(accept_type, &secure);
524         if (rc <= 0)
525                 return rc;
526
527         if (lnet_count_acceptor_nets() == 0)  /* not required */
528                 return 0;
529         if (current->nsproxy && current->nsproxy->net_ns)
530                 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
531         else
532                 lnet_acceptor_state.pta_ns = &init_net;
533         task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
534                            "acceptor_%03ld", secure);
535         if (IS_ERR(task)) {
536                 rc2 = PTR_ERR(task);
537                 CERROR("Can't start acceptor thread: %ld\n", rc2);
538                 return -ESRCH;
539         }
540
541         /* wait for acceptor to startup */
542         wait_for_completion(&lnet_acceptor_state.pta_signal);
543
544         if (!lnet_acceptor_state.pta_shutdown) {
545                 /* started OK */
546                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
547                 return 0;
548         }
549
550         LASSERT(lnet_acceptor_state.pta_sock == NULL);
551
552         return -ENETDOWN;
553 }
554
555 void
556 lnet_acceptor_stop(void)
557 {
558         if (lnet_acceptor_state.pta_shutdown) /* not running */
559                 return;
560
561         /* If still required, return immediately */
562         if (the_lnet.ln_refcount && lnet_count_acceptor_nets() > 0)
563                 return;
564
565         lnet_acceptor_state.pta_shutdown = 1;
566         wake_up(&lnet_acceptor_state.pta_waitq);
567
568         /* block until acceptor signals exit */
569         wait_for_completion(&lnet_acceptor_state.pta_signal);
570 }