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