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