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