staging: lustre: fix all NULL comparisons in LNet layer
[deliverable/linux.git] / drivers / staging / lustre / lnet / lnet / api-ni.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) 2003, 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/log2.h>
39 #include <linux/ktime.h>
40
41 #include "../../include/linux/lnet/lib-lnet.h"
42
43 #define D_LNI D_CONSOLE
44
45 lnet_t the_lnet; /* THE state of the network */
46 EXPORT_SYMBOL(the_lnet);
47
48 static char *ip2nets = "";
49 module_param(ip2nets, charp, 0444);
50 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
51
52 static char *networks = "";
53 module_param(networks, charp, 0444);
54 MODULE_PARM_DESC(networks, "local networks");
55
56 static char *routes = "";
57 module_param(routes, charp, 0444);
58 MODULE_PARM_DESC(routes, "routes to non-local networks");
59
60 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
61 module_param(rnet_htable_size, int, 0444);
62 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
63
64 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
65 lnet_process_id_t __user *ids, int n_ids);
66
67 static char *
68 lnet_get_routes(void)
69 {
70 return routes;
71 }
72
73 static char *
74 lnet_get_networks(void)
75 {
76 char *nets;
77 int rc;
78
79 if (*networks != 0 && *ip2nets != 0) {
80 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or 'ip2nets' but not both at once\n");
81 return NULL;
82 }
83
84 if (*ip2nets != 0) {
85 rc = lnet_parse_ip2nets(&nets, ip2nets);
86 return (rc == 0) ? nets : NULL;
87 }
88
89 if (*networks != 0)
90 return networks;
91
92 return "tcp";
93 }
94
95 static void
96 lnet_init_locks(void)
97 {
98 spin_lock_init(&the_lnet.ln_eq_wait_lock);
99 init_waitqueue_head(&the_lnet.ln_eq_waitq);
100 mutex_init(&the_lnet.ln_lnd_mutex);
101 mutex_init(&the_lnet.ln_api_mutex);
102 }
103
104 static int
105 lnet_create_remote_nets_table(void)
106 {
107 int i;
108 struct list_head *hash;
109
110 LASSERT(!the_lnet.ln_remote_nets_hash);
111 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
112 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
113 if (!hash) {
114 CERROR("Failed to create remote nets hash table\n");
115 return -ENOMEM;
116 }
117
118 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
119 INIT_LIST_HEAD(&hash[i]);
120 the_lnet.ln_remote_nets_hash = hash;
121 return 0;
122 }
123
124 static void
125 lnet_destroy_remote_nets_table(void)
126 {
127 int i;
128
129 if (!the_lnet.ln_remote_nets_hash)
130 return;
131
132 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
133 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
134
135 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
136 LNET_REMOTE_NETS_HASH_SIZE *
137 sizeof(the_lnet.ln_remote_nets_hash[0]));
138 the_lnet.ln_remote_nets_hash = NULL;
139 }
140
141 static void
142 lnet_destroy_locks(void)
143 {
144 if (the_lnet.ln_res_lock) {
145 cfs_percpt_lock_free(the_lnet.ln_res_lock);
146 the_lnet.ln_res_lock = NULL;
147 }
148
149 if (the_lnet.ln_net_lock) {
150 cfs_percpt_lock_free(the_lnet.ln_net_lock);
151 the_lnet.ln_net_lock = NULL;
152 }
153 }
154
155 static int
156 lnet_create_locks(void)
157 {
158 lnet_init_locks();
159
160 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
161 if (!the_lnet.ln_res_lock)
162 goto failed;
163
164 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
165 if (!the_lnet.ln_net_lock)
166 goto failed;
167
168 return 0;
169
170 failed:
171 lnet_destroy_locks();
172 return -ENOMEM;
173 }
174
175 static void lnet_assert_wire_constants(void)
176 {
177 /*
178 * Wire protocol assertions generated by 'wirecheck'
179 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
180 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
181 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
182 */
183
184 /* Constants... */
185 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
186 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
187 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
188 CLASSERT(LNET_MSG_ACK == 0);
189 CLASSERT(LNET_MSG_PUT == 1);
190 CLASSERT(LNET_MSG_GET == 2);
191 CLASSERT(LNET_MSG_REPLY == 3);
192 CLASSERT(LNET_MSG_HELLO == 4);
193
194 /* Checks for struct ptl_handle_wire_t */
195 CLASSERT((int)sizeof(lnet_handle_wire_t) == 16);
196 CLASSERT((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
197 CLASSERT((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
198 CLASSERT((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
199 CLASSERT((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
200
201 /* Checks for struct lnet_magicversion_t */
202 CLASSERT((int)sizeof(lnet_magicversion_t) == 8);
203 CLASSERT((int)offsetof(lnet_magicversion_t, magic) == 0);
204 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
205 CLASSERT((int)offsetof(lnet_magicversion_t, version_major) == 4);
206 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
207 CLASSERT((int)offsetof(lnet_magicversion_t, version_minor) == 6);
208 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
209
210 /* Checks for struct lnet_hdr_t */
211 CLASSERT((int)sizeof(lnet_hdr_t) == 72);
212 CLASSERT((int)offsetof(lnet_hdr_t, dest_nid) == 0);
213 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
214 CLASSERT((int)offsetof(lnet_hdr_t, src_nid) == 8);
215 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
216 CLASSERT((int)offsetof(lnet_hdr_t, dest_pid) == 16);
217 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
218 CLASSERT((int)offsetof(lnet_hdr_t, src_pid) == 20);
219 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
220 CLASSERT((int)offsetof(lnet_hdr_t, type) == 24);
221 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
222 CLASSERT((int)offsetof(lnet_hdr_t, payload_length) == 28);
223 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
224 CLASSERT((int)offsetof(lnet_hdr_t, msg) == 32);
225 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
226
227 /* Ack */
228 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
229 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
230 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
231 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
232 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
233 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
234
235 /* Put */
236 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
237 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
238 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
239 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
240 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
241 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
242 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
243 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
244 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
245 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
246
247 /* Get */
248 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
249 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
250 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
251 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
252 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
253 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
254 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
255 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
256 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
257 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
258
259 /* Reply */
260 CLASSERT((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
261 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
262
263 /* Hello */
264 CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
265 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
266 CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
267 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
268 }
269
270 static lnd_t *
271 lnet_find_lnd_by_type(__u32 type)
272 {
273 lnd_t *lnd;
274 struct list_head *tmp;
275
276 /* holding lnd mutex */
277 list_for_each(tmp, &the_lnet.ln_lnds) {
278 lnd = list_entry(tmp, lnd_t, lnd_list);
279
280 if (lnd->lnd_type == type)
281 return lnd;
282 }
283
284 return NULL;
285 }
286
287 void
288 lnet_register_lnd(lnd_t *lnd)
289 {
290 mutex_lock(&the_lnet.ln_lnd_mutex);
291
292 LASSERT(the_lnet.ln_init);
293 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
294 LASSERT(!lnet_find_lnd_by_type(lnd->lnd_type));
295
296 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
297 lnd->lnd_refcount = 0;
298
299 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
300
301 mutex_unlock(&the_lnet.ln_lnd_mutex);
302 }
303 EXPORT_SYMBOL(lnet_register_lnd);
304
305 void
306 lnet_unregister_lnd(lnd_t *lnd)
307 {
308 mutex_lock(&the_lnet.ln_lnd_mutex);
309
310 LASSERT(the_lnet.ln_init);
311 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
312 LASSERT(lnd->lnd_refcount == 0);
313
314 list_del(&lnd->lnd_list);
315 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
316
317 mutex_unlock(&the_lnet.ln_lnd_mutex);
318 }
319 EXPORT_SYMBOL(lnet_unregister_lnd);
320
321 void
322 lnet_counters_get(lnet_counters_t *counters)
323 {
324 lnet_counters_t *ctr;
325 int i;
326
327 memset(counters, 0, sizeof(*counters));
328
329 lnet_net_lock(LNET_LOCK_EX);
330
331 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
332 counters->msgs_max += ctr->msgs_max;
333 counters->msgs_alloc += ctr->msgs_alloc;
334 counters->errors += ctr->errors;
335 counters->send_count += ctr->send_count;
336 counters->recv_count += ctr->recv_count;
337 counters->route_count += ctr->route_count;
338 counters->drop_count += ctr->drop_count;
339 counters->send_length += ctr->send_length;
340 counters->recv_length += ctr->recv_length;
341 counters->route_length += ctr->route_length;
342 counters->drop_length += ctr->drop_length;
343 }
344 lnet_net_unlock(LNET_LOCK_EX);
345 }
346 EXPORT_SYMBOL(lnet_counters_get);
347
348 void
349 lnet_counters_reset(void)
350 {
351 lnet_counters_t *counters;
352 int i;
353
354 lnet_net_lock(LNET_LOCK_EX);
355
356 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
357 memset(counters, 0, sizeof(lnet_counters_t));
358
359 lnet_net_unlock(LNET_LOCK_EX);
360 }
361
362 static char *
363 lnet_res_type2str(int type)
364 {
365 switch (type) {
366 default:
367 LBUG();
368 case LNET_COOKIE_TYPE_MD:
369 return "MD";
370 case LNET_COOKIE_TYPE_ME:
371 return "ME";
372 case LNET_COOKIE_TYPE_EQ:
373 return "EQ";
374 }
375 }
376
377 static void
378 lnet_res_container_cleanup(struct lnet_res_container *rec)
379 {
380 int count = 0;
381
382 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
383 return;
384
385 while (!list_empty(&rec->rec_active)) {
386 struct list_head *e = rec->rec_active.next;
387
388 list_del_init(e);
389 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
390 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
391
392 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
393 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
394
395 } else { /* NB: Active MEs should be attached on portals */
396 LBUG();
397 }
398 count++;
399 }
400
401 if (count > 0) {
402 /*
403 * Found alive MD/ME/EQ, user really should unlink/free
404 * all of them before finalize LNet, but if someone didn't,
405 * we have to recycle garbage for him
406 */
407 CERROR("%d active elements on exit of %s container\n",
408 count, lnet_res_type2str(rec->rec_type));
409 }
410
411 if (rec->rec_lh_hash) {
412 LIBCFS_FREE(rec->rec_lh_hash,
413 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
414 rec->rec_lh_hash = NULL;
415 }
416
417 rec->rec_type = 0; /* mark it as finalized */
418 }
419
420 static int
421 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
422 {
423 int rc = 0;
424 int i;
425
426 LASSERT(rec->rec_type == 0);
427
428 rec->rec_type = type;
429 INIT_LIST_HEAD(&rec->rec_active);
430 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
431
432 /* Arbitrary choice of hash table size */
433 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
434 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
435 if (!rec->rec_lh_hash) {
436 rc = -ENOMEM;
437 goto out;
438 }
439
440 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
441 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
442
443 return 0;
444
445 out:
446 CERROR("Failed to setup %s resource container\n",
447 lnet_res_type2str(type));
448 lnet_res_container_cleanup(rec);
449 return rc;
450 }
451
452 static void
453 lnet_res_containers_destroy(struct lnet_res_container **recs)
454 {
455 struct lnet_res_container *rec;
456 int i;
457
458 cfs_percpt_for_each(rec, i, recs)
459 lnet_res_container_cleanup(rec);
460
461 cfs_percpt_free(recs);
462 }
463
464 static struct lnet_res_container **
465 lnet_res_containers_create(int type)
466 {
467 struct lnet_res_container **recs;
468 struct lnet_res_container *rec;
469 int rc;
470 int i;
471
472 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
473 if (!recs) {
474 CERROR("Failed to allocate %s resource containers\n",
475 lnet_res_type2str(type));
476 return NULL;
477 }
478
479 cfs_percpt_for_each(rec, i, recs) {
480 rc = lnet_res_container_setup(rec, i, type);
481 if (rc != 0) {
482 lnet_res_containers_destroy(recs);
483 return NULL;
484 }
485 }
486
487 return recs;
488 }
489
490 lnet_libhandle_t *
491 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
492 {
493 /* ALWAYS called with lnet_res_lock held */
494 struct list_head *head;
495 lnet_libhandle_t *lh;
496 unsigned int hash;
497
498 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
499 return NULL;
500
501 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
502 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
503
504 list_for_each_entry(lh, head, lh_hash_chain) {
505 if (lh->lh_cookie == cookie)
506 return lh;
507 }
508
509 return NULL;
510 }
511
512 void
513 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
514 {
515 /* ALWAYS called with lnet_res_lock held */
516 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
517 unsigned int hash;
518
519 lh->lh_cookie = rec->rec_lh_cookie;
520 rec->rec_lh_cookie += 1 << ibits;
521
522 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
523
524 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
525 }
526
527 int lnet_unprepare(void);
528
529 static int
530 lnet_prepare(lnet_pid_t requested_pid)
531 {
532 /* Prepare to bring up the network */
533 struct lnet_res_container **recs;
534 int rc = 0;
535
536 LASSERT(the_lnet.ln_refcount == 0);
537
538 the_lnet.ln_routing = 0;
539
540 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
541 the_lnet.ln_pid = requested_pid;
542
543 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
544 INIT_LIST_HEAD(&the_lnet.ln_nis);
545 INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
546 INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
547 INIT_LIST_HEAD(&the_lnet.ln_routers);
548
549 rc = lnet_create_remote_nets_table();
550 if (rc != 0)
551 goto failed;
552 /*
553 * NB the interface cookie in wire handles guards against delayed
554 * replies and ACKs appearing valid after reboot.
555 */
556 the_lnet.ln_interface_cookie = ktime_get_ns();
557
558 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
559 sizeof(lnet_counters_t));
560 if (!the_lnet.ln_counters) {
561 CERROR("Failed to allocate counters for LNet\n");
562 rc = -ENOMEM;
563 goto failed;
564 }
565
566 rc = lnet_peer_tables_create();
567 if (rc != 0)
568 goto failed;
569
570 rc = lnet_msg_containers_create();
571 if (rc != 0)
572 goto failed;
573
574 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
575 LNET_COOKIE_TYPE_EQ);
576 if (rc != 0)
577 goto failed;
578
579 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
580 if (!recs) {
581 rc = -ENOMEM;
582 goto failed;
583 }
584
585 the_lnet.ln_me_containers = recs;
586
587 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
588 if (!recs) {
589 rc = -ENOMEM;
590 goto failed;
591 }
592
593 the_lnet.ln_md_containers = recs;
594
595 rc = lnet_portals_create();
596 if (rc != 0) {
597 CERROR("Failed to create portals for LNet: %d\n", rc);
598 goto failed;
599 }
600
601 return 0;
602
603 failed:
604 lnet_unprepare();
605 return rc;
606 }
607
608 int
609 lnet_unprepare(void)
610 {
611 /*
612 * NB no LNET_LOCK since this is the last reference. All LND instances
613 * have shut down already, so it is safe to unlink and free all
614 * descriptors, even those that appear committed to a network op (eg MD
615 * with non-zero pending count)
616 */
617 lnet_fail_nid(LNET_NID_ANY, 0);
618
619 LASSERT(the_lnet.ln_refcount == 0);
620 LASSERT(list_empty(&the_lnet.ln_test_peers));
621 LASSERT(list_empty(&the_lnet.ln_nis));
622 LASSERT(list_empty(&the_lnet.ln_nis_cpt));
623 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
624
625 lnet_portals_destroy();
626
627 if (the_lnet.ln_md_containers) {
628 lnet_res_containers_destroy(the_lnet.ln_md_containers);
629 the_lnet.ln_md_containers = NULL;
630 }
631
632 if (the_lnet.ln_me_containers) {
633 lnet_res_containers_destroy(the_lnet.ln_me_containers);
634 the_lnet.ln_me_containers = NULL;
635 }
636
637 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
638
639 lnet_msg_containers_destroy();
640 lnet_peer_tables_destroy();
641 lnet_rtrpools_free();
642
643 if (the_lnet.ln_counters) {
644 cfs_percpt_free(the_lnet.ln_counters);
645 the_lnet.ln_counters = NULL;
646 }
647 lnet_destroy_remote_nets_table();
648
649 return 0;
650 }
651
652 lnet_ni_t *
653 lnet_net2ni_locked(__u32 net, int cpt)
654 {
655 struct list_head *tmp;
656 lnet_ni_t *ni;
657
658 LASSERT(cpt != LNET_LOCK_EX);
659
660 list_for_each(tmp, &the_lnet.ln_nis) {
661 ni = list_entry(tmp, lnet_ni_t, ni_list);
662
663 if (LNET_NIDNET(ni->ni_nid) == net) {
664 lnet_ni_addref_locked(ni, cpt);
665 return ni;
666 }
667 }
668
669 return NULL;
670 }
671
672 lnet_ni_t *
673 lnet_net2ni(__u32 net)
674 {
675 lnet_ni_t *ni;
676
677 lnet_net_lock(0);
678 ni = lnet_net2ni_locked(net, 0);
679 lnet_net_unlock(0);
680
681 return ni;
682 }
683 EXPORT_SYMBOL(lnet_net2ni);
684
685 static unsigned int
686 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
687 {
688 __u64 key = nid;
689 unsigned int val;
690
691 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
692
693 if (number == 1)
694 return 0;
695
696 val = hash_long(key, LNET_CPT_BITS);
697 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
698 if (val < number)
699 return val;
700
701 return (unsigned int)(key + val + (val >> 1)) % number;
702 }
703
704 int
705 lnet_cpt_of_nid_locked(lnet_nid_t nid)
706 {
707 struct lnet_ni *ni;
708
709 /* must called with hold of lnet_net_lock */
710 if (LNET_CPT_NUMBER == 1)
711 return 0; /* the only one */
712
713 /* take lnet_net_lock(any) would be OK */
714 if (!list_empty(&the_lnet.ln_nis_cpt)) {
715 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
716 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
717 continue;
718
719 LASSERT(ni->ni_cpts);
720 return ni->ni_cpts[lnet_nid_cpt_hash
721 (nid, ni->ni_ncpts)];
722 }
723 }
724
725 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
726 }
727
728 int
729 lnet_cpt_of_nid(lnet_nid_t nid)
730 {
731 int cpt;
732 int cpt2;
733
734 if (LNET_CPT_NUMBER == 1)
735 return 0; /* the only one */
736
737 if (list_empty(&the_lnet.ln_nis_cpt))
738 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
739
740 cpt = lnet_net_lock_current();
741 cpt2 = lnet_cpt_of_nid_locked(nid);
742 lnet_net_unlock(cpt);
743
744 return cpt2;
745 }
746 EXPORT_SYMBOL(lnet_cpt_of_nid);
747
748 int
749 lnet_islocalnet(__u32 net)
750 {
751 struct lnet_ni *ni;
752 int cpt;
753
754 cpt = lnet_net_lock_current();
755
756 ni = lnet_net2ni_locked(net, cpt);
757 if (ni)
758 lnet_ni_decref_locked(ni, cpt);
759
760 lnet_net_unlock(cpt);
761
762 return !!ni;
763 }
764
765 lnet_ni_t *
766 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
767 {
768 struct lnet_ni *ni;
769 struct list_head *tmp;
770
771 LASSERT(cpt != LNET_LOCK_EX);
772
773 list_for_each(tmp, &the_lnet.ln_nis) {
774 ni = list_entry(tmp, lnet_ni_t, ni_list);
775
776 if (ni->ni_nid == nid) {
777 lnet_ni_addref_locked(ni, cpt);
778 return ni;
779 }
780 }
781
782 return NULL;
783 }
784
785 int
786 lnet_islocalnid(lnet_nid_t nid)
787 {
788 struct lnet_ni *ni;
789 int cpt;
790
791 cpt = lnet_net_lock_current();
792 ni = lnet_nid2ni_locked(nid, cpt);
793 if (ni)
794 lnet_ni_decref_locked(ni, cpt);
795 lnet_net_unlock(cpt);
796
797 return !!ni;
798 }
799
800 int
801 lnet_count_acceptor_nis(void)
802 {
803 /* Return the # of NIs that need the acceptor. */
804 int count = 0;
805 struct list_head *tmp;
806 struct lnet_ni *ni;
807 int cpt;
808
809 cpt = lnet_net_lock_current();
810 list_for_each(tmp, &the_lnet.ln_nis) {
811 ni = list_entry(tmp, lnet_ni_t, ni_list);
812
813 if (ni->ni_lnd->lnd_accept)
814 count++;
815 }
816
817 lnet_net_unlock(cpt);
818
819 return count;
820 }
821
822 static int
823 lnet_ni_tq_credits(lnet_ni_t *ni)
824 {
825 int credits;
826
827 LASSERT(ni->ni_ncpts >= 1);
828
829 if (ni->ni_ncpts == 1)
830 return ni->ni_maxtxcredits;
831
832 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
833 credits = max(credits, 8 * ni->ni_peertxcredits);
834 credits = min(credits, ni->ni_maxtxcredits);
835
836 return credits;
837 }
838
839 static void
840 lnet_shutdown_lndnis(void)
841 {
842 int i;
843 int islo;
844 lnet_ni_t *ni;
845
846 /* NB called holding the global mutex */
847
848 /* All quiet on the API front */
849 LASSERT(!the_lnet.ln_shutdown);
850 LASSERT(the_lnet.ln_refcount == 0);
851 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
852
853 lnet_net_lock(LNET_LOCK_EX);
854 the_lnet.ln_shutdown = 1; /* flag shutdown */
855
856 /* Unlink NIs from the global table */
857 while (!list_empty(&the_lnet.ln_nis)) {
858 ni = list_entry(the_lnet.ln_nis.next,
859 lnet_ni_t, ni_list);
860 /* move it to zombie list and nobody can find it anymore */
861 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
862 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
863
864 if (!list_empty(&ni->ni_cptlist)) {
865 list_del_init(&ni->ni_cptlist);
866 lnet_ni_decref_locked(ni, 0);
867 }
868 }
869
870 /* Drop the cached eqwait NI. */
871 if (the_lnet.ln_eq_waitni) {
872 lnet_ni_decref_locked(the_lnet.ln_eq_waitni, 0);
873 the_lnet.ln_eq_waitni = NULL;
874 }
875
876 /* Drop the cached loopback NI. */
877 if (the_lnet.ln_loni) {
878 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
879 the_lnet.ln_loni = NULL;
880 }
881
882 lnet_net_unlock(LNET_LOCK_EX);
883
884 /*
885 * Clear lazy portals and drop delayed messages which hold refs
886 * on their lnet_msg_t::msg_rxpeer
887 */
888 for (i = 0; i < the_lnet.ln_nportals; i++)
889 LNetClearLazyPortal(i);
890
891 /*
892 * Clear the peer table and wait for all peers to go (they hold refs on
893 * their NIs)
894 */
895 lnet_peer_tables_cleanup();
896
897 lnet_net_lock(LNET_LOCK_EX);
898 /*
899 * Now wait for the NI's I just nuked to show up on ln_zombie_nis
900 * and shut them down in guaranteed thread context
901 */
902 i = 2;
903 while (!list_empty(&the_lnet.ln_nis_zombie)) {
904 int *ref;
905 int j;
906
907 ni = list_entry(the_lnet.ln_nis_zombie.next,
908 lnet_ni_t, ni_list);
909 list_del_init(&ni->ni_list);
910 cfs_percpt_for_each(ref, j, ni->ni_refs) {
911 if (*ref == 0)
912 continue;
913 /* still busy, add it back to zombie list */
914 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
915 break;
916 }
917
918 if (!list_empty(&ni->ni_list)) {
919 lnet_net_unlock(LNET_LOCK_EX);
920 ++i;
921 if ((i & (-i)) == i) {
922 CDEBUG(D_WARNING, "Waiting for zombie LNI %s\n",
923 libcfs_nid2str(ni->ni_nid));
924 }
925 set_current_state(TASK_UNINTERRUPTIBLE);
926 schedule_timeout(cfs_time_seconds(1));
927 lnet_net_lock(LNET_LOCK_EX);
928 continue;
929 }
930
931 ni->ni_lnd->lnd_refcount--;
932 lnet_net_unlock(LNET_LOCK_EX);
933
934 islo = ni->ni_lnd->lnd_type == LOLND;
935
936 LASSERT(!in_interrupt());
937 ni->ni_lnd->lnd_shutdown(ni);
938
939 /*
940 * can't deref lnd anymore now; it might have unregistered
941 * itself...
942 */
943 if (!islo)
944 CDEBUG(D_LNI, "Removed LNI %s\n",
945 libcfs_nid2str(ni->ni_nid));
946
947 lnet_ni_free(ni);
948 i = 2;
949
950 lnet_net_lock(LNET_LOCK_EX);
951 }
952
953 the_lnet.ln_shutdown = 0;
954 lnet_net_unlock(LNET_LOCK_EX);
955
956 if (the_lnet.ln_network_tokens) {
957 LIBCFS_FREE(the_lnet.ln_network_tokens,
958 the_lnet.ln_network_tokens_nob);
959 the_lnet.ln_network_tokens = NULL;
960 }
961 }
962
963 static int
964 lnet_startup_lndnis(void)
965 {
966 lnd_t *lnd;
967 struct lnet_ni *ni;
968 struct lnet_tx_queue *tq;
969 struct list_head nilist;
970 int i;
971 int rc = 0;
972 __u32 lnd_type;
973 int nicount = 0;
974 char *nets = lnet_get_networks();
975
976 INIT_LIST_HEAD(&nilist);
977
978 if (!nets)
979 goto failed;
980
981 rc = lnet_parse_networks(&nilist, nets);
982 if (rc != 0)
983 goto failed;
984
985 while (!list_empty(&nilist)) {
986 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
987 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
988
989 LASSERT(libcfs_isknown_lnd(lnd_type));
990
991 if (lnd_type == CIBLND ||
992 lnd_type == OPENIBLND ||
993 lnd_type == IIBLND ||
994 lnd_type == VIBLND) {
995 CERROR("LND %s obsoleted\n",
996 libcfs_lnd2str(lnd_type));
997 goto failed;
998 }
999
1000 mutex_lock(&the_lnet.ln_lnd_mutex);
1001 lnd = lnet_find_lnd_by_type(lnd_type);
1002
1003 if (!lnd) {
1004 mutex_unlock(&the_lnet.ln_lnd_mutex);
1005 rc = request_module("%s",
1006 libcfs_lnd2modname(lnd_type));
1007 mutex_lock(&the_lnet.ln_lnd_mutex);
1008
1009 lnd = lnet_find_lnd_by_type(lnd_type);
1010 if (!lnd) {
1011 mutex_unlock(&the_lnet.ln_lnd_mutex);
1012 CERROR("Can't load LND %s, module %s, rc=%d\n",
1013 libcfs_lnd2str(lnd_type),
1014 libcfs_lnd2modname(lnd_type), rc);
1015 goto failed;
1016 }
1017 }
1018
1019 lnet_net_lock(LNET_LOCK_EX);
1020 lnd->lnd_refcount++;
1021 lnet_net_unlock(LNET_LOCK_EX);
1022
1023 ni->ni_lnd = lnd;
1024
1025 rc = lnd->lnd_startup(ni);
1026
1027 mutex_unlock(&the_lnet.ln_lnd_mutex);
1028
1029 if (rc != 0) {
1030 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1031 rc, libcfs_lnd2str(lnd->lnd_type));
1032 lnet_net_lock(LNET_LOCK_EX);
1033 lnd->lnd_refcount--;
1034 lnet_net_unlock(LNET_LOCK_EX);
1035 goto failed;
1036 }
1037
1038 LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query);
1039
1040 list_del(&ni->ni_list);
1041
1042 lnet_net_lock(LNET_LOCK_EX);
1043 /* refcount for ln_nis */
1044 lnet_ni_addref_locked(ni, 0);
1045 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1046 if (ni->ni_cpts) {
1047 list_add_tail(&ni->ni_cptlist,
1048 &the_lnet.ln_nis_cpt);
1049 lnet_ni_addref_locked(ni, 0);
1050 }
1051
1052 lnet_net_unlock(LNET_LOCK_EX);
1053
1054 if (lnd->lnd_type == LOLND) {
1055 lnet_ni_addref(ni);
1056 LASSERT(!the_lnet.ln_loni);
1057 the_lnet.ln_loni = ni;
1058 continue;
1059 }
1060
1061 if (ni->ni_peertxcredits == 0 ||
1062 ni->ni_maxtxcredits == 0) {
1063 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1064 libcfs_lnd2str(lnd->lnd_type),
1065 ni->ni_peertxcredits == 0 ?
1066 "" : "per-peer ");
1067 goto failed;
1068 }
1069
1070 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1071 tq->tq_credits_min =
1072 tq->tq_credits_max =
1073 tq->tq_credits = lnet_ni_tq_credits(ni);
1074 }
1075
1076 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1077 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1078 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1079 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1080
1081 nicount++;
1082 }
1083
1084 if (the_lnet.ln_eq_waitni && nicount > 1) {
1085 lnd_type = the_lnet.ln_eq_waitni->ni_lnd->lnd_type;
1086 LCONSOLE_ERROR_MSG(0x109, "LND %s can only run single-network\n",
1087 libcfs_lnd2str(lnd_type));
1088 goto failed;
1089 }
1090
1091 return 0;
1092
1093 failed:
1094 lnet_shutdown_lndnis();
1095
1096 while (!list_empty(&nilist)) {
1097 ni = list_entry(nilist.next, lnet_ni_t, ni_list);
1098 list_del(&ni->ni_list);
1099 lnet_ni_free(ni);
1100 }
1101
1102 return -ENETDOWN;
1103 }
1104
1105 /**
1106 * Initialize LNet library.
1107 *
1108 * Only userspace program needs to call this function - it's automatically
1109 * called in the kernel at module loading time. Caller has to call lnet_fini()
1110 * after a call to lnet_init(), if and only if the latter returned 0. It must
1111 * be called exactly once.
1112 *
1113 * \return 0 on success, and -ve on failures.
1114 */
1115 int
1116 lnet_init(void)
1117 {
1118 int rc;
1119
1120 lnet_assert_wire_constants();
1121 LASSERT(!the_lnet.ln_init);
1122
1123 memset(&the_lnet, 0, sizeof(the_lnet));
1124
1125 /* refer to global cfs_cpt_table for now */
1126 the_lnet.ln_cpt_table = cfs_cpt_table;
1127 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1128
1129 LASSERT(the_lnet.ln_cpt_number > 0);
1130 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1131 /* we are under risk of consuming all lh_cookie */
1132 CERROR("Can't have %d CPTs for LNet (max allowed is %d), please change setting of CPT-table and retry\n",
1133 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1134 return -1;
1135 }
1136
1137 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1138 the_lnet.ln_cpt_bits++;
1139
1140 rc = lnet_create_locks();
1141 if (rc != 0) {
1142 CERROR("Can't create LNet global locks: %d\n", rc);
1143 return -1;
1144 }
1145
1146 the_lnet.ln_refcount = 0;
1147 the_lnet.ln_init = 1;
1148 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1149 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1150 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1151 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1152
1153 /*
1154 * The hash table size is the number of bits it takes to express the set
1155 * ln_num_routes, minus 1 (better to under estimate than over so we
1156 * don't waste memory).
1157 */
1158 if (rnet_htable_size <= 0)
1159 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1160 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1161 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1162 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1163 order_base_2(rnet_htable_size) - 1);
1164
1165 /*
1166 * All LNDs apart from the LOLND are in separate modules. They
1167 * register themselves when their module loads, and unregister
1168 * themselves when their module is unloaded.
1169 */
1170 lnet_register_lnd(&the_lolnd);
1171 return 0;
1172 }
1173
1174 /**
1175 * Finalize LNet library.
1176 *
1177 * Only userspace program needs to call this function. It can be called
1178 * at most once.
1179 *
1180 * \pre lnet_init() called with success.
1181 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1182 */
1183 void
1184 lnet_fini(void)
1185 {
1186 LASSERT(the_lnet.ln_init);
1187 LASSERT(the_lnet.ln_refcount == 0);
1188
1189 while (!list_empty(&the_lnet.ln_lnds))
1190 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1191 lnd_t, lnd_list));
1192 lnet_destroy_locks();
1193
1194 the_lnet.ln_init = 0;
1195 }
1196
1197 /**
1198 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1199 *
1200 * Userspace program should call this after a successful call to lnet_init().
1201 * Users must call this function at least once before any other functions.
1202 * For each successful call there must be a corresponding call to
1203 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1204 * ignored.
1205 *
1206 * The PID used by LNet may be different from the one requested.
1207 * See LNetGetId().
1208 *
1209 * \param requested_pid PID requested by the caller.
1210 *
1211 * \return >= 0 on success, and < 0 error code on failures.
1212 */
1213 int
1214 LNetNIInit(lnet_pid_t requested_pid)
1215 {
1216 int im_a_router = 0;
1217 int rc;
1218
1219 mutex_lock(&the_lnet.ln_api_mutex);
1220
1221 LASSERT(the_lnet.ln_init);
1222 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1223
1224 if (the_lnet.ln_refcount > 0) {
1225 rc = the_lnet.ln_refcount++;
1226 goto out;
1227 }
1228
1229 if (requested_pid == LNET_PID_ANY) {
1230 /* Don't instantiate LNET just for me */
1231 rc = -ENETDOWN;
1232 goto failed0;
1233 }
1234
1235 rc = lnet_prepare(requested_pid);
1236 if (rc != 0)
1237 goto failed0;
1238
1239 rc = lnet_startup_lndnis();
1240 if (rc != 0)
1241 goto failed1;
1242
1243 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1244 if (rc != 0)
1245 goto failed2;
1246
1247 rc = lnet_check_routes();
1248 if (rc != 0)
1249 goto failed2;
1250
1251 rc = lnet_rtrpools_alloc(im_a_router);
1252 if (rc != 0)
1253 goto failed2;
1254
1255 rc = lnet_acceptor_start();
1256 if (rc != 0)
1257 goto failed2;
1258
1259 the_lnet.ln_refcount = 1;
1260 /* Now I may use my own API functions... */
1261
1262 /*
1263 * NB router checker needs the_lnet.ln_ping_info in
1264 * lnet_router_checker -> lnet_update_ni_status_locked
1265 */
1266 rc = lnet_ping_target_init();
1267 if (rc != 0)
1268 goto failed3;
1269
1270 rc = lnet_router_checker_start();
1271 if (rc != 0)
1272 goto failed4;
1273
1274 lnet_router_debugfs_init();
1275 goto out;
1276
1277 failed4:
1278 lnet_ping_target_fini();
1279 failed3:
1280 the_lnet.ln_refcount = 0;
1281 lnet_acceptor_stop();
1282 failed2:
1283 lnet_destroy_routes();
1284 lnet_shutdown_lndnis();
1285 failed1:
1286 lnet_unprepare();
1287 failed0:
1288 LASSERT(rc < 0);
1289 out:
1290 mutex_unlock(&the_lnet.ln_api_mutex);
1291 return rc;
1292 }
1293 EXPORT_SYMBOL(LNetNIInit);
1294
1295 /**
1296 * Stop LNet interfaces, routing, and forwarding.
1297 *
1298 * Users must call this function once for each successful call to LNetNIInit().
1299 * Once the LNetNIFini() operation has been started, the results of pending
1300 * API operations are undefined.
1301 *
1302 * \return always 0 for current implementation.
1303 */
1304 int
1305 LNetNIFini(void)
1306 {
1307 mutex_lock(&the_lnet.ln_api_mutex);
1308
1309 LASSERT(the_lnet.ln_init);
1310 LASSERT(the_lnet.ln_refcount > 0);
1311
1312 if (the_lnet.ln_refcount != 1) {
1313 the_lnet.ln_refcount--;
1314 } else {
1315 LASSERT(!the_lnet.ln_niinit_self);
1316
1317 lnet_router_debugfs_fini();
1318 lnet_router_checker_stop();
1319 lnet_ping_target_fini();
1320
1321 /* Teardown fns that use my own API functions BEFORE here */
1322 the_lnet.ln_refcount = 0;
1323
1324 lnet_acceptor_stop();
1325 lnet_destroy_routes();
1326 lnet_shutdown_lndnis();
1327 lnet_unprepare();
1328 }
1329
1330 mutex_unlock(&the_lnet.ln_api_mutex);
1331 return 0;
1332 }
1333 EXPORT_SYMBOL(LNetNIFini);
1334
1335 /**
1336 * LNet ioctl handler.
1337 *
1338 */
1339 int
1340 LNetCtl(unsigned int cmd, void *arg)
1341 {
1342 struct libcfs_ioctl_data *data = arg;
1343 lnet_process_id_t id = {0};
1344 lnet_ni_t *ni;
1345 int rc;
1346 unsigned long secs_passed;
1347
1348 LASSERT(the_lnet.ln_init);
1349 LASSERT(the_lnet.ln_refcount > 0);
1350
1351 switch (cmd) {
1352 case IOC_LIBCFS_GET_NI:
1353 rc = LNetGetId(data->ioc_count, &id);
1354 data->ioc_nid = id.nid;
1355 return rc;
1356
1357 case IOC_LIBCFS_FAIL_NID:
1358 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1359
1360 case IOC_LIBCFS_ADD_ROUTE:
1361 rc = lnet_add_route(data->ioc_net, data->ioc_count,
1362 data->ioc_nid, data->ioc_priority);
1363 return (rc != 0) ? rc : lnet_check_routes();
1364
1365 case IOC_LIBCFS_DEL_ROUTE:
1366 return lnet_del_route(data->ioc_net, data->ioc_nid);
1367
1368 case IOC_LIBCFS_GET_ROUTE:
1369 return lnet_get_route(data->ioc_count,
1370 &data->ioc_net, &data->ioc_count,
1371 &data->ioc_nid, &data->ioc_flags,
1372 &data->ioc_priority);
1373 case IOC_LIBCFS_NOTIFY_ROUTER:
1374 secs_passed = (ktime_get_real_seconds() - data->ioc_u64[0]);
1375 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
1376 jiffies - secs_passed * HZ);
1377
1378 case IOC_LIBCFS_LNET_DIST:
1379 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
1380 if (rc < 0 && rc != -EHOSTUNREACH)
1381 return rc;
1382
1383 data->ioc_u32[0] = rc;
1384 return 0;
1385
1386 case IOC_LIBCFS_TESTPROTOCOMPAT:
1387 lnet_net_lock(LNET_LOCK_EX);
1388 the_lnet.ln_testprotocompat = data->ioc_flags;
1389 lnet_net_unlock(LNET_LOCK_EX);
1390 return 0;
1391
1392 case IOC_LIBCFS_PING:
1393 id.nid = data->ioc_nid;
1394 id.pid = data->ioc_u32[0];
1395 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
1396 data->ioc_pbuf1,
1397 data->ioc_plen1 / sizeof(lnet_process_id_t));
1398 if (rc < 0)
1399 return rc;
1400 data->ioc_count = rc;
1401 return 0;
1402
1403 default:
1404 ni = lnet_net2ni(data->ioc_net);
1405 if (!ni)
1406 return -EINVAL;
1407
1408 if (!ni->ni_lnd->lnd_ctl)
1409 rc = -EINVAL;
1410 else
1411 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
1412
1413 lnet_ni_decref(ni);
1414 return rc;
1415 }
1416 /* not reached */
1417 }
1418 EXPORT_SYMBOL(LNetCtl);
1419
1420 void LNetDebugPeer(lnet_process_id_t id)
1421 {
1422 lnet_debug_peer(id.nid);
1423 }
1424 EXPORT_SYMBOL(LNetDebugPeer);
1425
1426 /**
1427 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
1428 * all interfaces share a same PID, as requested by LNetNIInit().
1429 *
1430 * \param index Index of the interface to look up.
1431 * \param id On successful return, this location will hold the
1432 * lnet_process_id_t ID of the interface.
1433 *
1434 * \retval 0 If an interface exists at \a index.
1435 * \retval -ENOENT If no interface has been found.
1436 */
1437 int
1438 LNetGetId(unsigned int index, lnet_process_id_t *id)
1439 {
1440 struct lnet_ni *ni;
1441 struct list_head *tmp;
1442 int cpt;
1443 int rc = -ENOENT;
1444
1445 LASSERT(the_lnet.ln_init);
1446
1447 /* LNetNI initilization failed? */
1448 if (the_lnet.ln_refcount == 0)
1449 return rc;
1450
1451 cpt = lnet_net_lock_current();
1452
1453 list_for_each(tmp, &the_lnet.ln_nis) {
1454 if (index-- != 0)
1455 continue;
1456
1457 ni = list_entry(tmp, lnet_ni_t, ni_list);
1458
1459 id->nid = ni->ni_nid;
1460 id->pid = the_lnet.ln_pid;
1461 rc = 0;
1462 break;
1463 }
1464
1465 lnet_net_unlock(cpt);
1466 return rc;
1467 }
1468 EXPORT_SYMBOL(LNetGetId);
1469
1470 /**
1471 * Print a string representation of handle \a h into buffer \a str of
1472 * \a len bytes.
1473 */
1474 void
1475 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
1476 {
1477 snprintf(str, len, "%#llx", h.cookie);
1478 }
1479 EXPORT_SYMBOL(LNetSnprintHandle);
1480
1481 static int
1482 lnet_create_ping_info(void)
1483 {
1484 int i;
1485 int n;
1486 int rc;
1487 unsigned int infosz;
1488 lnet_ni_t *ni;
1489 lnet_process_id_t id;
1490 lnet_ping_info_t *pinfo;
1491
1492 for (n = 0; ; n++) {
1493 rc = LNetGetId(n, &id);
1494 if (rc == -ENOENT)
1495 break;
1496
1497 LASSERT(rc == 0);
1498 }
1499
1500 infosz = offsetof(lnet_ping_info_t, pi_ni[n]);
1501 LIBCFS_ALLOC(pinfo, infosz);
1502 if (!pinfo) {
1503 CERROR("Can't allocate ping info[%d]\n", n);
1504 return -ENOMEM;
1505 }
1506
1507 pinfo->pi_nnis = n;
1508 pinfo->pi_pid = the_lnet.ln_pid;
1509 pinfo->pi_magic = LNET_PROTO_PING_MAGIC;
1510 pinfo->pi_features = LNET_PING_FEAT_NI_STATUS;
1511
1512 for (i = 0; i < n; i++) {
1513 lnet_ni_status_t *ns = &pinfo->pi_ni[i];
1514
1515 rc = LNetGetId(i, &id);
1516 LASSERT(rc == 0);
1517
1518 ns->ns_nid = id.nid;
1519 ns->ns_status = LNET_NI_STATUS_UP;
1520
1521 lnet_net_lock(0);
1522
1523 ni = lnet_nid2ni_locked(id.nid, 0);
1524 LASSERT(ni);
1525
1526 lnet_ni_lock(ni);
1527 LASSERT(!ni->ni_status);
1528 ni->ni_status = ns;
1529 lnet_ni_unlock(ni);
1530
1531 lnet_ni_decref_locked(ni, 0);
1532 lnet_net_unlock(0);
1533 }
1534
1535 the_lnet.ln_ping_info = pinfo;
1536 return 0;
1537 }
1538
1539 static void
1540 lnet_destroy_ping_info(void)
1541 {
1542 struct lnet_ni *ni;
1543
1544 lnet_net_lock(0);
1545
1546 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1547 lnet_ni_lock(ni);
1548 ni->ni_status = NULL;
1549 lnet_ni_unlock(ni);
1550 }
1551
1552 lnet_net_unlock(0);
1553
1554 LIBCFS_FREE(the_lnet.ln_ping_info,
1555 offsetof(lnet_ping_info_t,
1556 pi_ni[the_lnet.ln_ping_info->pi_nnis]));
1557 the_lnet.ln_ping_info = NULL;
1558 }
1559
1560 int
1561 lnet_ping_target_init(void)
1562 {
1563 lnet_md_t md = { NULL };
1564 lnet_handle_me_t meh;
1565 lnet_process_id_t id;
1566 int rc;
1567 int rc2;
1568 int infosz;
1569
1570 rc = lnet_create_ping_info();
1571 if (rc != 0)
1572 return rc;
1573
1574 /*
1575 * We can have a tiny EQ since we only need to see the unlink event on
1576 * teardown, which by definition is the last one!
1577 */
1578 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &the_lnet.ln_ping_target_eq);
1579 if (rc != 0) {
1580 CERROR("Can't allocate ping EQ: %d\n", rc);
1581 goto failed_0;
1582 }
1583
1584 memset(&id, 0, sizeof(lnet_process_id_t));
1585 id.nid = LNET_NID_ANY;
1586 id.pid = LNET_PID_ANY;
1587
1588 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1589 LNET_PROTO_PING_MATCHBITS, 0,
1590 LNET_UNLINK, LNET_INS_AFTER,
1591 &meh);
1592 if (rc != 0) {
1593 CERROR("Can't create ping ME: %d\n", rc);
1594 goto failed_1;
1595 }
1596
1597 /* initialize md content */
1598 infosz = offsetof(lnet_ping_info_t,
1599 pi_ni[the_lnet.ln_ping_info->pi_nnis]);
1600 md.start = the_lnet.ln_ping_info;
1601 md.length = infosz;
1602 md.threshold = LNET_MD_THRESH_INF;
1603 md.max_size = 0;
1604 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1605 LNET_MD_MANAGE_REMOTE;
1606 md.user_ptr = NULL;
1607 md.eq_handle = the_lnet.ln_ping_target_eq;
1608
1609 rc = LNetMDAttach(meh, md,
1610 LNET_RETAIN,
1611 &the_lnet.ln_ping_target_md);
1612 if (rc != 0) {
1613 CERROR("Can't attach ping MD: %d\n", rc);
1614 goto failed_2;
1615 }
1616
1617 return 0;
1618
1619 failed_2:
1620 rc2 = LNetMEUnlink(meh);
1621 LASSERT(rc2 == 0);
1622 failed_1:
1623 rc2 = LNetEQFree(the_lnet.ln_ping_target_eq);
1624 LASSERT(rc2 == 0);
1625 failed_0:
1626 lnet_destroy_ping_info();
1627 return rc;
1628 }
1629
1630 void
1631 lnet_ping_target_fini(void)
1632 {
1633 lnet_event_t event;
1634 int rc;
1635 int which;
1636 int timeout_ms = 1000;
1637 sigset_t blocked = cfs_block_allsigs();
1638
1639 LNetMDUnlink(the_lnet.ln_ping_target_md);
1640 /* NB md could be busy; this just starts the unlink */
1641
1642 for (;;) {
1643 rc = LNetEQPoll(&the_lnet.ln_ping_target_eq, 1,
1644 timeout_ms, &event, &which);
1645
1646 /* I expect overflow... */
1647 LASSERT(rc >= 0 || rc == -EOVERFLOW);
1648
1649 if (rc == 0) {
1650 /* timed out: provide a diagnostic */
1651 CWARN("Still waiting for ping MD to unlink\n");
1652 timeout_ms *= 2;
1653 continue;
1654 }
1655
1656 /* Got a valid event */
1657 if (event.unlinked)
1658 break;
1659 }
1660
1661 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1662 LASSERT(rc == 0);
1663 lnet_destroy_ping_info();
1664 cfs_restore_sigs(blocked);
1665 }
1666
1667 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
1668 lnet_process_id_t __user *ids, int n_ids)
1669 {
1670 lnet_handle_eq_t eqh;
1671 lnet_handle_md_t mdh;
1672 lnet_event_t event;
1673 lnet_md_t md = { NULL };
1674 int which;
1675 int unlinked = 0;
1676 int replied = 0;
1677 const int a_long_time = 60000; /* mS */
1678 int infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
1679 lnet_ping_info_t *info;
1680 lnet_process_id_t tmpid;
1681 int i;
1682 int nob;
1683 int rc;
1684 int rc2;
1685 sigset_t blocked;
1686
1687 if (n_ids <= 0 ||
1688 id.nid == LNET_NID_ANY ||
1689 timeout_ms > 500000 || /* arbitrary limit! */
1690 n_ids > 20) /* arbitrary limit! */
1691 return -EINVAL;
1692
1693 if (id.pid == LNET_PID_ANY)
1694 id.pid = LUSTRE_SRV_LNET_PID;
1695
1696 LIBCFS_ALLOC(info, infosz);
1697 if (!info)
1698 return -ENOMEM;
1699
1700 /* NB 2 events max (including any unlink event) */
1701 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
1702 if (rc != 0) {
1703 CERROR("Can't allocate EQ: %d\n", rc);
1704 goto out_0;
1705 }
1706
1707 /* initialize md content */
1708 md.start = info;
1709 md.length = infosz;
1710 md.threshold = 2; /*GET/REPLY*/
1711 md.max_size = 0;
1712 md.options = LNET_MD_TRUNCATE;
1713 md.user_ptr = NULL;
1714 md.eq_handle = eqh;
1715
1716 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
1717 if (rc != 0) {
1718 CERROR("Can't bind MD: %d\n", rc);
1719 goto out_1;
1720 }
1721
1722 rc = LNetGet(LNET_NID_ANY, mdh, id,
1723 LNET_RESERVED_PORTAL,
1724 LNET_PROTO_PING_MATCHBITS, 0);
1725
1726 if (rc != 0) {
1727 /* Don't CERROR; this could be deliberate! */
1728
1729 rc2 = LNetMDUnlink(mdh);
1730 LASSERT(rc2 == 0);
1731
1732 /* NB must wait for the UNLINK event below... */
1733 unlinked = 1;
1734 timeout_ms = a_long_time;
1735 }
1736
1737 do {
1738 /* MUST block for unlink to complete */
1739 if (unlinked)
1740 blocked = cfs_block_allsigs();
1741
1742 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
1743
1744 if (unlinked)
1745 cfs_restore_sigs(blocked);
1746
1747 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
1748 (rc2 <= 0) ? -1 : event.type,
1749 (rc2 <= 0) ? -1 : event.status,
1750 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
1751
1752 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
1753
1754 if (rc2 <= 0 || event.status != 0) {
1755 /* timeout or error */
1756 if (!replied && rc == 0)
1757 rc = (rc2 < 0) ? rc2 :
1758 (rc2 == 0) ? -ETIMEDOUT :
1759 event.status;
1760
1761 if (!unlinked) {
1762 /* Ensure completion in finite time... */
1763 LNetMDUnlink(mdh);
1764 /* No assertion (racing with network) */
1765 unlinked = 1;
1766 timeout_ms = a_long_time;
1767 } else if (rc2 == 0) {
1768 /* timed out waiting for unlink */
1769 CWARN("ping %s: late network completion\n",
1770 libcfs_id2str(id));
1771 }
1772 } else if (event.type == LNET_EVENT_REPLY) {
1773 replied = 1;
1774 rc = event.mlength;
1775 }
1776
1777 } while (rc2 <= 0 || !event.unlinked);
1778
1779 if (!replied) {
1780 if (rc >= 0)
1781 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
1782 libcfs_id2str(id));
1783 rc = -EIO;
1784 goto out_1;
1785 }
1786
1787 nob = rc;
1788 LASSERT(nob >= 0 && nob <= infosz);
1789
1790 rc = -EPROTO; /* if I can't parse... */
1791
1792 if (nob < 8) {
1793 /* can't check magic/version */
1794 CERROR("%s: ping info too short %d\n",
1795 libcfs_id2str(id), nob);
1796 goto out_1;
1797 }
1798
1799 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
1800 lnet_swap_pinginfo(info);
1801 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
1802 CERROR("%s: Unexpected magic %08x\n",
1803 libcfs_id2str(id), info->pi_magic);
1804 goto out_1;
1805 }
1806
1807 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
1808 CERROR("%s: ping w/o NI status: 0x%x\n",
1809 libcfs_id2str(id), info->pi_features);
1810 goto out_1;
1811 }
1812
1813 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
1814 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
1815 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
1816 goto out_1;
1817 }
1818
1819 if (info->pi_nnis < n_ids)
1820 n_ids = info->pi_nnis;
1821
1822 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
1823 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
1824 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
1825 goto out_1;
1826 }
1827
1828 rc = -EFAULT; /* If I SEGV... */
1829
1830 memset(&tmpid, 0, sizeof(tmpid));
1831 for (i = 0; i < n_ids; i++) {
1832 tmpid.pid = info->pi_pid;
1833 tmpid.nid = info->pi_ni[i].ns_nid;
1834 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
1835 goto out_1;
1836 }
1837 rc = info->pi_nnis;
1838
1839 out_1:
1840 rc2 = LNetEQFree(eqh);
1841 if (rc2 != 0)
1842 CERROR("rc2 %d\n", rc2);
1843 LASSERT(rc2 == 0);
1844
1845 out_0:
1846 LIBCFS_FREE(info, infosz);
1847 return rc;
1848 }
This page took 0.094888 seconds and 5 git commands to generate.