Merge branch 'master' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/torvalds...
[linux-2.6-microblaze.git] / drivers / staging / lustre / lnet / lnet / acceptor.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * GPL HEADER START
4  *
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 only,
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License version 2 for more details (a copy is included
15  * in the LICENSE file that accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License
18  * version 2 along with this program; If not, see
19  * http://www.gnu.org/licenses/gpl-2.0.html
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Use is subject to license terms.
26  *
27  * Copyright (c) 2011, 2015, Intel Corporation.
28  */
29 /*
30  * This file is part of Lustre, http://www.lustre.org/
31  * Lustre is a trademark of Sun Microsystems, Inc.
32  */
33
34 #define DEBUG_SUBSYSTEM S_LNET
35 #include <linux/completion.h>
36 #include <net/sock.h>
37 #include <linux/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 } lnet_acceptor_state = {
48         .pta_shutdown = 1
49 };
50
51 int
52 lnet_acceptor_port(void)
53 {
54         return accept_port;
55 }
56 EXPORT_SYMBOL(lnet_acceptor_port);
57
58 static inline int
59 lnet_accept_magic(__u32 magic, __u32 constant)
60 {
61         return (magic == constant ||
62                 magic == __swab32(constant));
63 }
64
65 static char *accept = "secure";
66
67 module_param(accept, charp, 0444);
68 MODULE_PARM_DESC(accept, "Accept connections (secure|all|none)");
69 module_param(accept_port, int, 0444);
70 MODULE_PARM_DESC(accept_port, "Acceptor's port (same on all nodes)");
71 module_param(accept_backlog, int, 0444);
72 MODULE_PARM_DESC(accept_backlog, "Acceptor's listen backlog");
73 module_param(accept_timeout, int, 0644);
74 MODULE_PARM_DESC(accept_timeout, "Acceptor's timeout (seconds)");
75
76 static char *accept_type;
77
78 static int
79 lnet_acceptor_get_tunables(void)
80 {
81         /*
82          * Userland acceptor uses 'accept_type' instead of 'accept', due to
83          * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
84          * for compatibility. Hence the trick.
85          */
86         accept_type = accept;
87         return 0;
88 }
89
90 int
91 lnet_acceptor_timeout(void)
92 {
93         return accept_timeout;
94 }
95 EXPORT_SYMBOL(lnet_acceptor_timeout);
96
97 void
98 lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
99                            __u32 peer_ip, int peer_port)
100 {
101         switch (rc) {
102         /* "normal" errors */
103         case -ECONNREFUSED:
104                 CNETERR("Connection to %s at host %pI4h on port %d was refused: check that Lustre is running on that node.\n",
105                         libcfs_nid2str(peer_nid),
106                         &peer_ip, peer_port);
107                 break;
108         case -EHOSTUNREACH:
109         case -ENETUNREACH:
110                 CNETERR("Connection to %s at host %pI4h was unreachable: the network or that node may be down, or Lustre may be misconfigured.\n",
111                         libcfs_nid2str(peer_nid), &peer_ip);
112                 break;
113         case -ETIMEDOUT:
114                 CNETERR("Connection to %s at host %pI4h on port %d took too long: that node may be hung or experiencing high load.\n",
115                         libcfs_nid2str(peer_nid),
116                         &peer_ip, peer_port);
117                 break;
118         case -ECONNRESET:
119                 LCONSOLE_ERROR_MSG(0x11b, "Connection to %s at host %pI4h on port %d was reset: is it running a compatible version of Lustre and is %s one of its NIDs?\n",
120                                    libcfs_nid2str(peer_nid),
121                                    &peer_ip, peer_port,
122                                    libcfs_nid2str(peer_nid));
123                 break;
124         case -EPROTO:
125                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at host %pI4h on port %d: is it running a compatible version of Lustre?\n",
126                                    libcfs_nid2str(peer_nid),
127                                    &peer_ip, peer_port);
128                 break;
129         case -EADDRINUSE:
130                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to connect to %s at host %pI4h on port %d\n",
131                                    libcfs_nid2str(peer_nid),
132                                    &peer_ip, peer_port);
133                 break;
134         default:
135                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s at host %pI4h on port %d\n",
136                                    rc, libcfs_nid2str(peer_nid),
137                                    &peer_ip, peer_port);
138                 break;
139         }
140 }
141 EXPORT_SYMBOL(lnet_connect_console_error);
142
143 int
144 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
145              __u32 local_ip, __u32 peer_ip, int peer_port)
146 {
147         struct lnet_acceptor_connreq cr;
148         struct socket *sock;
149         int rc;
150         int port;
151         int fatal;
152
153         BUILD_BUG_ON(sizeof(cr) > 16);      /* too big to be on the stack */
154
155         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
156              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
157              --port) {
158                 /* Iterate through reserved ports. */
159
160                 rc = lnet_sock_connect(&sock, &fatal, local_ip, port, peer_ip,
161                                        peer_port);
162                 if (rc) {
163                         if (fatal)
164                                 goto failed;
165                         continue;
166                 }
167
168                 BUILD_BUG_ON(LNET_PROTO_ACCEPTOR_VERSION != 1);
169
170                 cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
171                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
172                 cr.acr_nid     = peer_nid;
173
174                 if (the_lnet.ln_testprotocompat) {
175                         /* single-shot proto check */
176                         lnet_net_lock(LNET_LOCK_EX);
177                         if (the_lnet.ln_testprotocompat & 4) {
178                                 cr.acr_version++;
179                                 the_lnet.ln_testprotocompat &= ~4;
180                         }
181                         if (the_lnet.ln_testprotocompat & 8) {
182                                 cr.acr_magic = LNET_PROTO_MAGIC;
183                                 the_lnet.ln_testprotocompat &= ~8;
184                         }
185                         lnet_net_unlock(LNET_LOCK_EX);
186                 }
187
188                 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
189                 if (rc)
190                         goto failed_sock;
191
192                 *sockp = sock;
193                 return 0;
194         }
195
196         rc = -EADDRINUSE;
197         goto failed;
198
199  failed_sock:
200         sock_release(sock);
201  failed:
202         lnet_connect_console_error(rc, peer_nid, peer_ip, peer_port);
203         return rc;
204 }
205 EXPORT_SYMBOL(lnet_connect);
206
207 static int
208 lnet_accept(struct socket *sock, __u32 magic)
209 {
210         struct lnet_acceptor_connreq cr;
211         __u32 peer_ip;
212         int peer_port;
213         int rc;
214         int flip;
215         struct lnet_ni *ni;
216         char *str;
217
218         LASSERT(sizeof(cr) <= 16);           /* not too big for the stack */
219
220         rc = lnet_sock_getaddr(sock, 1, &peer_ip, &peer_port);
221         LASSERT(!rc);                 /* we succeeded before */
222
223         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
224                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
225                         /*
226                          * future version compatibility!
227                          * When LNET unifies protocols over all LNDs, the first
228                          * thing sent will be a version query. I send back
229                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old"
230                          */
231                         memset(&cr, 0, sizeof(cr));
232                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
233                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
234                         rc = lnet_sock_write(sock, &cr, sizeof(cr),
235                                              accept_timeout);
236
237                         if (rc)
238                                 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
239                                        &peer_ip, rc);
240                         return -EPROTO;
241                 }
242
243                 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
244                         str = "'old' socknal/tcpnal";
245                 else
246                         str = "unrecognised";
247
248                 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h magic %08x: %s acceptor protocol\n",
249                                    &peer_ip, magic, str);
250                 return -EPROTO;
251         }
252
253         flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
254
255         rc = lnet_sock_read(sock, &cr.acr_version, sizeof(cr.acr_version),
256                             accept_timeout);
257         if (rc) {
258                 CERROR("Error %d reading connection request version from %pI4h\n",
259                        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                 /*
268                  * future version compatibility!
269                  * An acceptor-specific protocol rev will first send a version
270                  * query.  I send back my current version to tell her I'm
271                  * "old".
272                  */
273                 int peer_version = cr.acr_version;
274
275                 memset(&cr, 0, sizeof(cr));
276                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
277                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
278
279                 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
280                 if (rc)
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) {
291                 CERROR("Error %d reading connection request from %pI4h\n",
292                        rc, &peer_ip);
293                 return -EIO;
294         }
295
296         if (flip)
297                 __swab64s(&cr.acr_nid);
298
299         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
300         if (!ni ||             /* no matching net */
301             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
302                 if (ni)
303                         lnet_ni_decref(ni);
304                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
305                                    &peer_ip, libcfs_nid2str(cr.acr_nid));
306                 return -EPERM;
307         }
308
309         if (!ni->ni_lnd->lnd_accept) {
310                 /* This catches a request for the loopback LND */
311                 lnet_ni_decref(ni);
312                 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h for %s: NI doesn not accept IP connections\n",
313                                    &peer_ip, libcfs_nid2str(cr.acr_nid));
314                 return -EPERM;
315         }
316
317         CDEBUG(D_NET, "Accept %s from %pI4h\n",
318                libcfs_nid2str(cr.acr_nid), &peer_ip);
319
320         rc = ni->ni_lnd->lnd_accept(ni, sock);
321
322         lnet_ni_decref(ni);
323         return rc;
324 }
325
326 static int
327 lnet_acceptor(void *arg)
328 {
329         struct socket *newsock;
330         int rc;
331         __u32 magic;
332         __u32 peer_ip;
333         int peer_port;
334         int secure = (int)((long)arg);
335
336         LASSERT(!lnet_acceptor_state.pta_sock);
337
338         cfs_block_allsigs();
339
340         rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock, 0, accept_port,
341                               accept_backlog);
342         if (rc) {
343                 if (rc == -EADDRINUSE)
344                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
345                                            accept_port);
346                 else
347                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
348                                            accept_port, rc);
349
350                 lnet_acceptor_state.pta_sock = NULL;
351         } else {
352                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
353         }
354
355         /* set init status and unblock parent */
356         lnet_acceptor_state.pta_shutdown = rc;
357         complete(&lnet_acceptor_state.pta_signal);
358
359         if (rc)
360                 return rc;
361
362         while (!lnet_acceptor_state.pta_shutdown) {
363                 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
364                 if (rc) {
365                         if (rc != -EAGAIN) {
366                                 CWARN("Accept error %d: pausing...\n", rc);
367                                 set_current_state(TASK_UNINTERRUPTIBLE);
368                                 schedule_timeout(cfs_time_seconds(1));
369                         }
370                         continue;
371                 }
372
373                 /* maybe the LNet acceptor thread has been waken */
374                 if (lnet_acceptor_state.pta_shutdown) {
375                         sock_release(newsock);
376                         break;
377                 }
378
379                 rc = lnet_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
380                 if (rc) {
381                         CERROR("Can't determine new connection's address\n");
382                         goto failed;
383                 }
384
385                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
386                         CERROR("Refusing connection from %pI4h: insecure port %d\n",
387                                &peer_ip, peer_port);
388                         goto failed;
389                 }
390
391                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
392                                     accept_timeout);
393                 if (rc) {
394                         CERROR("Error %d reading connection request from %pI4h\n",
395                                rc, &peer_ip);
396                         goto failed;
397                 }
398
399                 rc = lnet_accept(newsock, magic);
400                 if (rc)
401                         goto failed;
402
403                 continue;
404
405 failed:
406                 sock_release(newsock);
407         }
408
409         sock_release(lnet_acceptor_state.pta_sock);
410         lnet_acceptor_state.pta_sock = NULL;
411
412         CDEBUG(D_NET, "Acceptor stopping\n");
413
414         /* unblock lnet_acceptor_stop() */
415         complete(&lnet_acceptor_state.pta_signal);
416         return 0;
417 }
418
419 static inline int
420 accept2secure(const char *acc, long *sec)
421 {
422         if (!strcmp(acc, "secure")) {
423                 *sec = 1;
424                 return 1;
425         } else if (!strcmp(acc, "all")) {
426                 *sec = 0;
427                 return 1;
428         } else if (!strcmp(acc, "none")) {
429                 return 0;
430         }
431
432         LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
433                            acc);
434         return -EINVAL;
435 }
436
437 int
438 lnet_acceptor_start(void)
439 {
440         struct task_struct *task;
441         int rc;
442         long rc2;
443         long secure;
444
445         /* if acceptor is already running return immediately */
446         if (!lnet_acceptor_state.pta_shutdown)
447                 return 0;
448
449         LASSERT(!lnet_acceptor_state.pta_sock);
450
451         rc = lnet_acceptor_get_tunables();
452         if (rc)
453                 return rc;
454
455         init_completion(&lnet_acceptor_state.pta_signal);
456         rc = accept2secure(accept_type, &secure);
457         if (rc <= 0)
458                 return rc;
459
460         if (!lnet_count_acceptor_nis())  /* not required */
461                 return 0;
462
463         task = kthread_run(lnet_acceptor, (void *)(uintptr_t)secure,
464                            "acceptor_%03ld", secure);
465         if (IS_ERR(task)) {
466                 rc2 = PTR_ERR(task);
467                 CERROR("Can't start acceptor thread: %ld\n", rc2);
468
469                 return -ESRCH;
470         }
471
472         /* wait for acceptor to startup */
473         wait_for_completion(&lnet_acceptor_state.pta_signal);
474
475         if (!lnet_acceptor_state.pta_shutdown) {
476                 /* started OK */
477                 LASSERT(lnet_acceptor_state.pta_sock);
478                 return 0;
479         }
480
481         LASSERT(!lnet_acceptor_state.pta_sock);
482
483         return -ENETDOWN;
484 }
485
486 void
487 lnet_acceptor_stop(void)
488 {
489         struct sock *sk;
490
491         if (lnet_acceptor_state.pta_shutdown) /* not running */
492                 return;
493
494         lnet_acceptor_state.pta_shutdown = 1;
495
496         sk = lnet_acceptor_state.pta_sock->sk;
497
498         /* awake any sleepers using safe method */
499         sk->sk_state_change(sk);
500
501         /* block until acceptor signals exit */
502         wait_for_completion(&lnet_acceptor_state.pta_signal);
503 }