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