]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/lustre/lnet/lnet/acceptor.c
staging:lustre: rename tcpip handling functions to lnet_* prefix
[karo-tx-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, 2012, 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 "../../include/linux/lnet/lib-lnet.h"
40
41 static int   accept_port    = 988;
42 static int   accept_backlog = 127;
43 static int   accept_timeout = 5;
44
45 static struct {
46         int                     pta_shutdown;
47         struct socket           *pta_sock;
48         struct completion       pta_signal;
49 } lnet_acceptor_state;
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         /* Userland acceptor uses 'accept_type' instead of 'accept', due to
82          * conflict with 'accept(2)', but kernel acceptor still uses 'accept'
83          * for compatibility. Hence the trick. */
84         accept_type = accept;
85         return 0;
86 }
87
88 int
89 lnet_acceptor_timeout(void)
90 {
91         return accept_timeout;
92 }
93 EXPORT_SYMBOL(lnet_acceptor_timeout);
94
95 void
96 lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
97                            __u32 peer_ip, int peer_port)
98 {
99         switch (rc) {
100         /* "normal" errors */
101         case -ECONNREFUSED:
102                 CNETERR("Connection to %s at host %pI4h on port %d was refused: check that Lustre is running on that node.\n",
103                         libcfs_nid2str(peer_nid),
104                         &peer_ip, peer_port);
105                 break;
106         case -EHOSTUNREACH:
107         case -ENETUNREACH:
108                 CNETERR("Connection to %s at host %pI4h was unreachable: the network or that node may be down, or Lustre may be misconfigured.\n",
109                         libcfs_nid2str(peer_nid), &peer_ip);
110                 break;
111         case -ETIMEDOUT:
112                 CNETERR("Connection to %s at host %pI4h on port %d took too long: that node may be hung or experiencing high load.\n",
113                         libcfs_nid2str(peer_nid),
114                         &peer_ip, peer_port);
115                 break;
116         case -ECONNRESET:
117                 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",
118                                    libcfs_nid2str(peer_nid),
119                                    &peer_ip, peer_port,
120                                    libcfs_nid2str(peer_nid));
121                 break;
122         case -EPROTO:
123                 LCONSOLE_ERROR_MSG(0x11c, "Protocol error connecting to %s at host %pI4h on port %d: is it running a compatible version of Lustre?\n",
124                                    libcfs_nid2str(peer_nid),
125                                    &peer_ip, peer_port);
126                 break;
127         case -EADDRINUSE:
128                 LCONSOLE_ERROR_MSG(0x11d, "No privileged ports available to connect to %s at host %pI4h on port %d\n",
129                                    libcfs_nid2str(peer_nid),
130                                    &peer_ip, peer_port);
131                 break;
132         default:
133                 LCONSOLE_ERROR_MSG(0x11e, "Unexpected error %d connecting to %s at host %pI4h on port %d\n",
134                                    rc, libcfs_nid2str(peer_nid),
135                                    &peer_ip, peer_port);
136                 break;
137         }
138 }
139 EXPORT_SYMBOL(lnet_connect_console_error);
140
141 int
142 lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
143             __u32 local_ip, __u32 peer_ip, int peer_port)
144 {
145         lnet_acceptor_connreq_t cr;
146         struct socket *sock;
147         int rc;
148         int port;
149         int fatal;
150
151         CLASSERT(sizeof(cr) <= 16);         /* not too big to be on the stack */
152
153         for (port = LNET_ACCEPTOR_MAX_RESERVED_PORT;
154              port >= LNET_ACCEPTOR_MIN_RESERVED_PORT;
155              --port) {
156                 /* Iterate through reserved ports. */
157
158                 rc = lnet_sock_connect(&sock, &fatal, local_ip, port, peer_ip,
159                                        peer_port);
160                 if (rc != 0) {
161                         if (fatal)
162                                 goto failed;
163                         continue;
164                 }
165
166                 CLASSERT(LNET_PROTO_ACCEPTOR_VERSION == 1);
167
168                 cr.acr_magic   = LNET_PROTO_ACCEPTOR_MAGIC;
169                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
170                 cr.acr_nid     = peer_nid;
171
172                 if (the_lnet.ln_testprotocompat != 0) {
173                         /* single-shot proto check */
174                         lnet_net_lock(LNET_LOCK_EX);
175                         if ((the_lnet.ln_testprotocompat & 4) != 0) {
176                                 cr.acr_version++;
177                                 the_lnet.ln_testprotocompat &= ~4;
178                         }
179                         if ((the_lnet.ln_testprotocompat & 8) != 0) {
180                                 cr.acr_magic = LNET_PROTO_MAGIC;
181                                 the_lnet.ln_testprotocompat &= ~8;
182                         }
183                         lnet_net_unlock(LNET_LOCK_EX);
184                 }
185
186                 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
187                 if (rc != 0)
188                         goto failed_sock;
189
190                 *sockp = sock;
191                 return 0;
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 rc;
202 }
203 EXPORT_SYMBOL(lnet_connect);
204
205
206 /* Below is the code common for both kernel and MT user-space */
207
208 static int
209 lnet_accept(struct socket *sock, __u32 magic)
210 {
211         lnet_acceptor_connreq_t cr;
212         __u32 peer_ip;
213         int peer_port;
214         int rc;
215         int flip;
216         lnet_ni_t *ni;
217         char *str;
218
219         LASSERT(sizeof(cr) <= 16);           /* not too big for the stack */
220
221         rc = lnet_sock_getaddr(sock, 1, &peer_ip, &peer_port);
222         LASSERT(rc == 0);                     /* we succeeded before */
223
224         if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
225
226                 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
227                         /* future version compatibility!
228                          * When LNET unifies protocols over all LNDs, the first
229                          * thing sent will be a version query.  I send back
230                          * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old" */
231
232                         memset(&cr, 0, sizeof(cr));
233                         cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
234                         cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
235                         rc = lnet_sock_write(sock, &cr, sizeof(cr),
236                                              accept_timeout);
237
238                         if (rc != 0)
239                                 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
240                                        &peer_ip, rc);
241                         return -EPROTO;
242                 }
243
244                 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
245                         str = "'old' socknal/tcpnal";
246                 else if (lnet_accept_magic(magic, LNET_PROTO_RA_MAGIC))
247                         str = "'old' ranal";
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 != 0) {
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                 /* future version compatibility!
271                  * An acceptor-specific protocol rev will first send a version
272                  * query.  I send back my current version to tell her I'm
273                  * "old". */
274                 int peer_version = cr.acr_version;
275
276                 memset(&cr, 0, sizeof(cr));
277                 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
278                 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
279
280                 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
281                 if (rc != 0)
282                         CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
283                                peer_version, &peer_ip, rc);
284                 return -EPROTO;
285         }
286
287         rc = lnet_sock_read(sock, &cr.acr_nid,
288                             sizeof(cr) -
289                             offsetof(lnet_acceptor_connreq_t, acr_nid),
290                             accept_timeout);
291         if (rc != 0) {
292                 CERROR("Error %d reading connection request from %pI4h\n",
293                         rc, &peer_ip);
294                 return -EIO;
295         }
296
297         if (flip)
298                 __swab64s(&cr.acr_nid);
299
300         ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
301         if (ni == NULL ||              /* no matching net */
302             ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
303                 if (ni != NULL)
304                         lnet_ni_decref(ni);
305                 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
306                                    &peer_ip, libcfs_nid2str(cr.acr_nid));
307                 return -EPERM;
308         }
309
310         if (ni->ni_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 for %s: NI doesn not accept IP connections\n",
314                                   &peer_ip, libcfs_nid2str(cr.acr_nid));
315                 return -EPERM;
316         }
317
318         CDEBUG(D_NET, "Accept %s from %pI4h\n",
319                libcfs_nid2str(cr.acr_nid), &peer_ip);
320
321         rc = ni->ni_lnd->lnd_accept(ni, sock);
322
323         lnet_ni_decref(ni);
324         return rc;
325 }
326
327 static int
328 lnet_acceptor(void *arg)
329 {
330         struct socket *newsock;
331         int rc;
332         __u32 magic;
333         __u32 peer_ip;
334         int peer_port;
335         int secure = (int)((long_ptr_t)arg);
336
337         LASSERT(lnet_acceptor_state.pta_sock == NULL);
338
339         cfs_block_allsigs();
340
341         rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock, 0, accept_port,
342                               accept_backlog);
343         if (rc != 0) {
344                 if (rc == -EADDRINUSE)
345                         LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
346                                            accept_port);
347                 else
348                         LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
349                                            accept_port, rc);
350
351                 lnet_acceptor_state.pta_sock = NULL;
352         } else {
353                 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
354         }
355
356         /* set init status and unblock parent */
357         lnet_acceptor_state.pta_shutdown = rc;
358         complete(&lnet_acceptor_state.pta_signal);
359
360         if (rc != 0)
361                 return rc;
362
363         while (!lnet_acceptor_state.pta_shutdown) {
364
365                 rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
366                 if (rc != 0) {
367                         if (rc != -EAGAIN) {
368                                 CWARN("Accept error %d: pausing...\n", rc);
369                                 set_current_state(TASK_UNINTERRUPTIBLE);
370                                 schedule_timeout(cfs_time_seconds(1));
371                         }
372                         continue;
373                 }
374
375                 /* maybe the LNet acceptor thread has been waken */
376                 if (lnet_acceptor_state.pta_shutdown) {
377                         sock_release(newsock);
378                         break;
379                 }
380
381                 rc = lnet_sock_getaddr(newsock, 1, &peer_ip, &peer_port);
382                 if (rc != 0) {
383                         CERROR("Can't determine new connection's address\n");
384                         goto failed;
385                 }
386
387                 if (secure && peer_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
388                         CERROR("Refusing connection from %pI4h: insecure port %d\n",
389                                &peer_ip, peer_port);
390                         goto failed;
391                 }
392
393                 rc = lnet_sock_read(newsock, &magic, sizeof(magic),
394                                     accept_timeout);
395                 if (rc != 0) {
396                         CERROR("Error %d reading connection request from %pI4h\n",
397                                 rc, &peer_ip);
398                         goto failed;
399                 }
400
401                 rc = lnet_accept(newsock, magic);
402                 if (rc != 0)
403                         goto failed;
404
405                 continue;
406
407 failed:
408                 sock_release(newsock);
409         }
410
411         sock_release(lnet_acceptor_state.pta_sock);
412         lnet_acceptor_state.pta_sock = NULL;
413
414         CDEBUG(D_NET, "Acceptor stopping\n");
415
416         /* unblock lnet_acceptor_stop() */
417         complete(&lnet_acceptor_state.pta_signal);
418         return 0;
419 }
420
421 static inline int
422 accept2secure(const char *acc, long *sec)
423 {
424         if (!strcmp(acc, "secure")) {
425                 *sec = 1;
426                 return 1;
427         } else if (!strcmp(acc, "all")) {
428                 *sec = 0;
429                 return 1;
430         } else if (!strcmp(acc, "none")) {
431                 return 0;
432         }
433
434         LCONSOLE_ERROR_MSG(0x124, "Can't parse 'accept=\"%s\"'\n",
435                            acc);
436         return -EINVAL;
437 }
438
439 int
440 lnet_acceptor_start(void)
441 {
442         int rc;
443         long rc2;
444         long secure;
445
446         LASSERT(lnet_acceptor_state.pta_sock == NULL);
447
448         rc = lnet_acceptor_get_tunables();
449         if (rc != 0)
450                 return rc;
451
452
453         init_completion(&lnet_acceptor_state.pta_signal);
454         rc = accept2secure(accept_type, &secure);
455         if (rc <= 0)
456                 return rc;
457
458         if (lnet_count_acceptor_nis() == 0)  /* not required */
459                 return 0;
460
461         rc2 = PTR_ERR(kthread_run(lnet_acceptor,
462                                   (void *)(ulong_ptr_t)secure,
463                                   "acceptor_%03ld", secure));
464         if (IS_ERR_VALUE(rc2)) {
465                 CERROR("Can't start acceptor thread: %ld\n", rc2);
466
467                 return -ESRCH;
468         }
469
470         /* wait for acceptor to startup */
471         wait_for_completion(&lnet_acceptor_state.pta_signal);
472
473         if (!lnet_acceptor_state.pta_shutdown) {
474                 /* started OK */
475                 LASSERT(lnet_acceptor_state.pta_sock != NULL);
476                 return 0;
477         }
478
479         LASSERT(lnet_acceptor_state.pta_sock == NULL);
480
481         return -ENETDOWN;
482 }
483
484 void
485 lnet_acceptor_stop(void)
486 {
487         if (lnet_acceptor_state.pta_sock == NULL) /* not running */
488                 return;
489
490         lnet_acceptor_state.pta_shutdown = 1;
491         wake_up_all(sk_sleep(lnet_acceptor_state.pta_sock->sk));
492
493         /* block until acceptor signals exit */
494         wait_for_completion(&lnet_acceptor_state.pta_signal);
495 }