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