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
lnet_acceptor_port(void)52 lnet_acceptor_port(void)
53 {
54 	return accept_port;
55 }
56 EXPORT_SYMBOL(lnet_acceptor_port);
57 
58 static inline int
lnet_accept_magic(__u32 magic,__u32 constant)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
lnet_acceptor_get_tunables(void)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
lnet_acceptor_timeout(void)89 lnet_acceptor_timeout(void)
90 {
91 	return accept_timeout;
92 }
93 EXPORT_SYMBOL(lnet_acceptor_timeout);
94 
95 void
lnet_connect_console_error(int rc,lnet_nid_t peer_nid,__u32 peer_ip,int peer_port)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
lnet_connect(struct socket ** sockp,lnet_nid_t peer_nid,__u32 local_ip,__u32 peer_ip,int peer_port)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 /* Below is the code common for both kernel and MT user-space */
206 
207 static int
lnet_accept(struct socket * sock,__u32 magic)208 lnet_accept(struct socket *sock, __u32 magic)
209 {
210 	lnet_acceptor_connreq_t cr;
211 	__u32 peer_ip;
212 	int peer_port;
213 	int rc;
214 	int flip;
215 	lnet_ni_t *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 == 0);		      /* we succeeded before */
222 
223 	if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
224 
225 		if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
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 != 0)
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 != 0) {
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 		/* 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), accept_timeout);
278 		if (rc != 0)
279 			CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
280 			       peer_version, &peer_ip, rc);
281 		return -EPROTO;
282 	}
283 
284 	rc = lnet_sock_read(sock, &cr.acr_nid,
285 			    sizeof(cr) -
286 			    offsetof(lnet_acceptor_connreq_t, acr_nid),
287 			    accept_timeout);
288 	if (rc != 0) {
289 		CERROR("Error %d reading connection request from %pI4h\n",
290 			rc, &peer_ip);
291 		return -EIO;
292 	}
293 
294 	if (flip)
295 		__swab64s(&cr.acr_nid);
296 
297 	ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
298 	if (ni == NULL ||	       /* no matching net */
299 	    ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
300 		if (ni != NULL)
301 			lnet_ni_decref(ni);
302 		LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
303 				   &peer_ip, libcfs_nid2str(cr.acr_nid));
304 		return -EPERM;
305 	}
306 
307 	if (ni->ni_lnd->lnd_accept == NULL) {
308 		/* This catches a request for the loopback LND */
309 		lnet_ni_decref(ni);
310 		LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h for %s: NI doesn not accept IP connections\n",
311 				  &peer_ip, libcfs_nid2str(cr.acr_nid));
312 		return -EPERM;
313 	}
314 
315 	CDEBUG(D_NET, "Accept %s from %pI4h\n",
316 	       libcfs_nid2str(cr.acr_nid), &peer_ip);
317 
318 	rc = ni->ni_lnd->lnd_accept(ni, sock);
319 
320 	lnet_ni_decref(ni);
321 	return rc;
322 }
323 
324 static int
lnet_acceptor(void * arg)325 lnet_acceptor(void *arg)
326 {
327 	struct socket *newsock;
328 	int rc;
329 	__u32 magic;
330 	__u32 peer_ip;
331 	int peer_port;
332 	int secure = (int)((long_ptr_t)arg);
333 
334 	LASSERT(lnet_acceptor_state.pta_sock == NULL);
335 
336 	cfs_block_allsigs();
337 
338 	rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock, 0, accept_port,
339 			      accept_backlog);
340 	if (rc != 0) {
341 		if (rc == -EADDRINUSE)
342 			LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
343 					   accept_port);
344 		else
345 			LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
346 					   accept_port, rc);
347 
348 		lnet_acceptor_state.pta_sock = NULL;
349 	} else {
350 		LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
351 	}
352 
353 	/* set init status and unblock parent */
354 	lnet_acceptor_state.pta_shutdown = rc;
355 	complete(&lnet_acceptor_state.pta_signal);
356 
357 	if (rc != 0)
358 		return rc;
359 
360 	while (!lnet_acceptor_state.pta_shutdown) {
361 
362 		rc = lnet_sock_accept(&newsock, lnet_acceptor_state.pta_sock);
363 		if (rc != 0) {
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 != 0) {
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 != 0) {
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 != 0)
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 	int rc;
440 	long rc2;
441 	long secure;
442 
443 	LASSERT(lnet_acceptor_state.pta_sock == NULL);
444 
445 	rc = lnet_acceptor_get_tunables();
446 	if (rc != 0)
447 		return rc;
448 
449 	init_completion(&lnet_acceptor_state.pta_signal);
450 	rc = accept2secure(accept_type, &secure);
451 	if (rc <= 0)
452 		return rc;
453 
454 	if (lnet_count_acceptor_nis() == 0)  /* not required */
455 		return 0;
456 
457 	rc2 = PTR_ERR(kthread_run(lnet_acceptor,
458 				  (void *)(ulong_ptr_t)secure,
459 				  "acceptor_%03ld", secure));
460 	if (IS_ERR_VALUE(rc2)) {
461 		CERROR("Can't start acceptor thread: %ld\n", rc2);
462 
463 		return -ESRCH;
464 	}
465 
466 	/* wait for acceptor to startup */
467 	wait_for_completion(&lnet_acceptor_state.pta_signal);
468 
469 	if (!lnet_acceptor_state.pta_shutdown) {
470 		/* started OK */
471 		LASSERT(lnet_acceptor_state.pta_sock != NULL);
472 		return 0;
473 	}
474 
475 	LASSERT(lnet_acceptor_state.pta_sock == NULL);
476 
477 	return -ENETDOWN;
478 }
479 
480 void
lnet_acceptor_stop(void)481 lnet_acceptor_stop(void)
482 {
483 	if (lnet_acceptor_state.pta_sock == NULL) /* not running */
484 		return;
485 
486 	lnet_acceptor_state.pta_shutdown = 1;
487 	wake_up_all(sk_sleep(lnet_acceptor_state.pta_sock->sk));
488 
489 	/* block until acceptor signals exit */
490 	wait_for_completion(&lnet_acceptor_state.pta_signal);
491 }
492