Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
294                 if (ni != NULL)
295                         lnet_ni_decref(ni);
296                 LCONSOLE_ERROR_MSG(0x120,
297                                    "Refusing connection from %pIS for %s: No matching NI\n",
298                                    &peer, libcfs_nid2str(cr.acr_nid));
299                 return -EPERM;
300         }
301
302         if (ni->ni_net->net_lnd->lnd_accept == NULL) {
303                 /* This catches a request for the loopback LND */
304                 lnet_ni_decref(ni);
305                 LCONSOLE_ERROR_MSG(0x121,
306                                    "Refusing connection from %pIS for %s: NI doesn not accept IP connections\n",
307                                   &peer, libcfs_nid2str(cr.acr_nid));
308                 return -EPERM;
309         }
310
311         CDEBUG(D_NET, "Accept %s from %pI4h\n",
312                libcfs_nid2str(cr.acr_nid), &peer);
313
314         rc = ni->ni_net->net_lnd->lnd_accept(ni, sock);
315
316         lnet_ni_decref(ni);
317         return rc;
318 }
319
320 #ifdef HAVE_SK_DATA_READY_ONE_ARG
321 static void lnet_acceptor_ready(struct sock *sk)
322 #else
323 static void lnet_acceptor_ready(struct sock *sk, int len)
324 #endif
325 {
326         /* Ensure pta_odata has actually been set before calling it */
327         rmb();
328 #ifdef HAVE_SK_DATA_READY_ONE_ARG
329         lnet_acceptor_state.pta_odata(sk);
330 #else
331         lnet_acceptor_state.pta_odata(sk, 0);
332 #endif
333
334         atomic_set(&lnet_acceptor_state.pta_ready, 1);
335         wake_up(&lnet_acceptor_state.pta_waitq);
336 }
337
338 static int
339 lnet_acceptor(void *arg)
340 {
341         struct socket  *newsock;
342         int            rc;
343         __u32          magic;
344         struct sockaddr_storage peer;
345         int            secure = (int)((uintptr_t)arg);
346
347         LASSERT(lnet_acceptor_state.pta_sock == NULL);
348
349         lnet_acceptor_state.pta_sock =
350                 lnet_sock_listen(accept_port, accept_backlog,
351                                  lnet_acceptor_state.pta_ns);
352         if (IS_ERR(lnet_acceptor_state.pta_sock)) {
353                 rc = PTR_ERR(lnet_acceptor_state.pta_sock);
354                 if (rc == -EADDRINUSE)
355                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port"
356                                            " %d: port already in use\n",
357                                            accept_port);
358                 else
359                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port "
360                                            "%d: unexpected error %d\n",
361                                            accept_port, rc);
362
363                 lnet_acceptor_state.pta_sock = NULL;
364         } else {
365                 rc = 0;
366                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
367                 init_waitqueue_head(&lnet_acceptor_state.pta_waitq);
368                 lnet_acceptor_state.pta_odata =
369                         lnet_acceptor_state.pta_sock->sk->sk_data_ready;
370                 /* ensure pta_odata gets set before there is any chance of
371                  * lnet_accept_ready() trying to read it.
372                  */
373                 wmb();
374                 lnet_acceptor_state.pta_sock->sk->sk_data_ready =
375                         lnet_acceptor_ready;
376                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
377         }
378
379         /* set init status and unblock parent */
380         lnet_acceptor_state.pta_shutdown = rc;
381         complete(&lnet_acceptor_state.pta_signal);
382
383         if (rc != 0)
384                 return rc;
385
386         while (!lnet_acceptor_state.pta_shutdown) {
387
388                 wait_event_idle(lnet_acceptor_state.pta_waitq,
389                                 lnet_acceptor_state.pta_shutdown ||
390                                 atomic_read(&lnet_acceptor_state.pta_ready));
391                 if (!atomic_read(&lnet_acceptor_state.pta_ready))
392                         continue;
393                 atomic_set(&lnet_acceptor_state.pta_ready, 0);
394                 rc = kernel_accept(lnet_acceptor_state.pta_sock, &newsock,
395                                    SOCK_NONBLOCK);
396                 if (rc != 0) {
397                         if (rc != -EAGAIN) {
398                                 CWARN("Accept error %d: pausing...\n", rc);
399                                 schedule_timeout_uninterruptible(
400                                         cfs_time_seconds(1));
401                         }
402                         continue;
403                 }
404
405                 /* make sure we call lnet_sock_accept() again, until it fails */
406                 atomic_set(&lnet_acceptor_state.pta_ready, 1);
407
408                 rc = lnet_sock_getaddr(newsock, true, &peer);
409                 if (rc != 0) {
410                         CERROR("Can't determine new connection's address\n");
411                         goto failed;
412                 }
413
414                 if (secure &&
415                     rpc_get_port((struct sockaddr *)&peer) >
416                     LNET_ACCEPTOR_MAX_RESERVED_PORT) {
417                         CERROR("Refusing connection from %pISp: insecure port.\n",
418                                &peer);
419                         goto failed;
420                 }
421
422                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
423                                       accept_timeout);
424                 if (rc != 0) {
425                         CERROR("Error %d reading connection request from %pIS\n",
426                                rc, &peer);
427                         goto failed;
428                 }
429
430                 rc = lnet_accept(newsock, magic);
431                 if (rc != 0)
432                         goto failed;
433
434                 continue;
435
436 failed:
437                 sock_release(newsock);
438         }
439
440         lnet_acceptor_state.pta_sock->sk->sk_data_ready =
441                 lnet_acceptor_state.pta_odata;
442         sock_release(lnet_acceptor_state.pta_sock);
443         lnet_acceptor_state.pta_sock = NULL;
444
445         CDEBUG(D_NET, "Acceptor stopping\n");
446
447         /* unblock lnet_acceptor_stop() */
448         complete(&lnet_acceptor_state.pta_signal);
449         return 0;
450 }
451
452 static inline int
453 accept2secure(const char *acc, long *sec)
454 {
455         if (!strcmp(acc, "secure")) {
456                 *sec = 1;
457                 return 1;
458         } else if (!strcmp(acc, "all")) {
459                 *sec = 0;
460                 return 1;
461         } else if (!strcmp(acc, "none")) {
462                 return 0;
463         } else {
464                 LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
465                                    acc);
466                 return -EINVAL;
467         }
468 }
469
470 int
471 lnet_acceptor_start(void)
472 {
473         struct task_struct *task;
474         int  rc;
475         long rc2;
476         long secure;
477
478         /* if acceptor is already running return immediately */
479         if (!lnet_acceptor_state.pta_shutdown)
480                 return 0;
481
482         LASSERT(lnet_acceptor_state.pta_sock == NULL);
483
484         init_completion(&lnet_acceptor_state.pta_signal);
485         rc = accept2secure(accept_type, &secure);
486         if (rc <= 0)
487                 return rc;
488
489         if (lnet_count_acceptor_nets() == 0)  /* not required */
490                 return 0;
491         if (current->nsproxy && current->nsproxy->net_ns)
492                 lnet_acceptor_state.pta_ns = current->nsproxy->net_ns;
493         else
494                 lnet_acceptor_state.pta_ns = &init_net;
495         task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
496                            "acceptor_%03ld", secure);
497         if (IS_ERR(task)) {
498                 rc2 = PTR_ERR(task);
499                 CERROR("Can't start acceptor thread: %ld\n", rc2);
500                 return -ESRCH;
501         }
502
503         /* wait for acceptor to startup */
504         wait_for_completion(&lnet_acceptor_state.pta_signal);
505
506         if (!lnet_acceptor_state.pta_shutdown) {
507                 /* started OK */
508                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
509                 return 0;
510         }
511
512         LASSERT(lnet_acceptor_state.pta_sock == NULL);
513
514         return -ENETDOWN;
515 }
516
517 void
518 lnet_acceptor_stop(void)
519 {
520         if (lnet_acceptor_state.pta_shutdown) /* not running */
521                 return;
522
523         /* If still required, return immediately */
524         if (the_lnet.ln_refcount && lnet_count_acceptor_nets() > 0)
525                 return;
526
527         lnet_acceptor_state.pta_shutdown = 1;
528         wake_up(&lnet_acceptor_state.pta_waitq);
529
530         /* block until acceptor signals exit */
531         wait_for_completion(&lnet_acceptor_state.pta_signal);
532 }