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