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