staging: lustre: fix all NULL comparisons in LNet layer
[deliverable/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 "../../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 /*
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 lnet_acceptor_connreq_t cr;
148 struct socket *sock;
149 int rc;
150 int port;
151 int fatal;
152
153 CLASSERT(sizeof(cr) <= 16); /* not 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 != 0) {
163 if (fatal)
164 goto failed;
165 continue;
166 }
167
168 CLASSERT(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 != 0) {
175 /* single-shot proto check */
176 lnet_net_lock(LNET_LOCK_EX);
177 if ((the_lnet.ln_testprotocompat & 4) != 0) {
178 cr.acr_version++;
179 the_lnet.ln_testprotocompat &= ~4;
180 }
181 if ((the_lnet.ln_testprotocompat & 8) != 0) {
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 != 0)
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 /* Below is the code common for both kernel and MT user-space */
208
209 static int
210 lnet_accept(struct socket *sock, __u32 magic)
211 {
212 lnet_acceptor_connreq_t cr;
213 __u32 peer_ip;
214 int peer_port;
215 int rc;
216 int flip;
217 lnet_ni_t *ni;
218 char *str;
219
220 LASSERT(sizeof(cr) <= 16); /* not too big for the stack */
221
222 rc = lnet_sock_getaddr(sock, 1, &peer_ip, &peer_port);
223 LASSERT(rc == 0); /* we succeeded before */
224
225 if (!lnet_accept_magic(magic, LNET_PROTO_ACCEPTOR_MAGIC)) {
226 if (lnet_accept_magic(magic, LNET_PROTO_MAGIC)) {
227 /*
228 * future version compatibility!
229 * When LNET unifies protocols over all LNDs, the first
230 * thing sent will be a version query. I send back
231 * LNET_PROTO_ACCEPTOR_MAGIC to tell her I'm "old"
232 */
233 memset(&cr, 0, sizeof(cr));
234 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
235 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
236 rc = lnet_sock_write(sock, &cr, sizeof(cr),
237 accept_timeout);
238
239 if (rc != 0)
240 CERROR("Error sending magic+version in response to LNET magic from %pI4h: %d\n",
241 &peer_ip, rc);
242 return -EPROTO;
243 }
244
245 if (magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC))
246 str = "'old' socknal/tcpnal";
247 else
248 str = "unrecognised";
249
250 LCONSOLE_ERROR_MSG(0x11f, "Refusing connection from %pI4h magic %08x: %s acceptor protocol\n",
251 &peer_ip, magic, str);
252 return -EPROTO;
253 }
254
255 flip = (magic != LNET_PROTO_ACCEPTOR_MAGIC);
256
257 rc = lnet_sock_read(sock, &cr.acr_version, sizeof(cr.acr_version),
258 accept_timeout);
259 if (rc != 0) {
260 CERROR("Error %d reading connection request version from %pI4h\n",
261 rc, &peer_ip);
262 return -EIO;
263 }
264
265 if (flip)
266 __swab32s(&cr.acr_version);
267
268 if (cr.acr_version != LNET_PROTO_ACCEPTOR_VERSION) {
269 /*
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 */
275 int peer_version = cr.acr_version;
276
277 memset(&cr, 0, sizeof(cr));
278 cr.acr_magic = LNET_PROTO_ACCEPTOR_MAGIC;
279 cr.acr_version = LNET_PROTO_ACCEPTOR_VERSION;
280
281 rc = lnet_sock_write(sock, &cr, sizeof(cr), accept_timeout);
282 if (rc != 0)
283 CERROR("Error sending magic+version in response to version %d from %pI4h: %d\n",
284 peer_version, &peer_ip, rc);
285 return -EPROTO;
286 }
287
288 rc = lnet_sock_read(sock, &cr.acr_nid,
289 sizeof(cr) -
290 offsetof(lnet_acceptor_connreq_t, acr_nid),
291 accept_timeout);
292 if (rc != 0) {
293 CERROR("Error %d reading connection request from %pI4h\n",
294 rc, &peer_ip);
295 return -EIO;
296 }
297
298 if (flip)
299 __swab64s(&cr.acr_nid);
300
301 ni = lnet_net2ni(LNET_NIDNET(cr.acr_nid));
302 if (!ni || /* no matching net */
303 ni->ni_nid != cr.acr_nid) { /* right NET, wrong NID! */
304 if (ni)
305 lnet_ni_decref(ni);
306 LCONSOLE_ERROR_MSG(0x120, "Refusing connection from %pI4h for %s: No matching NI\n",
307 &peer_ip, libcfs_nid2str(cr.acr_nid));
308 return -EPERM;
309 }
310
311 if (!ni->ni_lnd->lnd_accept) {
312 /* This catches a request for the loopback LND */
313 lnet_ni_decref(ni);
314 LCONSOLE_ERROR_MSG(0x121, "Refusing connection from %pI4h for %s: NI doesn not accept IP connections\n",
315 &peer_ip, libcfs_nid2str(cr.acr_nid));
316 return -EPERM;
317 }
318
319 CDEBUG(D_NET, "Accept %s from %pI4h\n",
320 libcfs_nid2str(cr.acr_nid), &peer_ip);
321
322 rc = ni->ni_lnd->lnd_accept(ni, sock);
323
324 lnet_ni_decref(ni);
325 return rc;
326 }
327
328 static int
329 lnet_acceptor(void *arg)
330 {
331 struct socket *newsock;
332 int rc;
333 __u32 magic;
334 __u32 peer_ip;
335 int peer_port;
336 int secure = (int)((long_ptr_t)arg);
337
338 LASSERT(!lnet_acceptor_state.pta_sock);
339
340 cfs_block_allsigs();
341
342 rc = lnet_sock_listen(&lnet_acceptor_state.pta_sock, 0, accept_port,
343 accept_backlog);
344 if (rc != 0) {
345 if (rc == -EADDRINUSE)
346 LCONSOLE_ERROR_MSG(0x122, "Can't start acceptor on port %d: port already in use\n",
347 accept_port);
348 else
349 LCONSOLE_ERROR_MSG(0x123, "Can't start acceptor on port %d: unexpected error %d\n",
350 accept_port, rc);
351
352 lnet_acceptor_state.pta_sock = NULL;
353 } else {
354 LCONSOLE(0, "Accept %s, port %d\n", accept_type, accept_port);
355 }
356
357 /* set init status and unblock parent */
358 lnet_acceptor_state.pta_shutdown = rc;
359 complete(&lnet_acceptor_state.pta_signal);
360
361 if (rc != 0)
362 return rc;
363
364 while (!lnet_acceptor_state.pta_shutdown) {
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);
447
448 rc = lnet_acceptor_get_tunables();
449 if (rc != 0)
450 return rc;
451
452 init_completion(&lnet_acceptor_state.pta_signal);
453 rc = accept2secure(accept_type, &secure);
454 if (rc <= 0)
455 return rc;
456
457 if (lnet_count_acceptor_nis() == 0) /* not required */
458 return 0;
459
460 rc2 = PTR_ERR(kthread_run(lnet_acceptor,
461 (void *)(ulong_ptr_t)secure,
462 "acceptor_%03ld", secure));
463 if (IS_ERR_VALUE(rc2)) {
464 CERROR("Can't start acceptor thread: %ld\n", rc2);
465
466 return -ESRCH;
467 }
468
469 /* wait for acceptor to startup */
470 wait_for_completion(&lnet_acceptor_state.pta_signal);
471
472 if (!lnet_acceptor_state.pta_shutdown) {
473 /* started OK */
474 LASSERT(lnet_acceptor_state.pta_sock);
475 return 0;
476 }
477
478 LASSERT(!lnet_acceptor_state.pta_sock);
479
480 return -ENETDOWN;
481 }
482
483 void
484 lnet_acceptor_stop(void)
485 {
486 if (!lnet_acceptor_state.pta_sock) /* not running */
487 return;
488
489 lnet_acceptor_state.pta_shutdown = 1;
490 wake_up_all(sk_sleep(lnet_acceptor_state.pta_sock->sk));
491
492 /* block until acceptor signals exit */
493 wait_for_completion(&lnet_acceptor_state.pta_signal);
494 }
This page took 0.050132 seconds and 5 git commands to generate.