4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
39 #include <lnet/lib-dlc.h>
40 #include <linux/log2.h>
42 #define D_LNI D_CONSOLE
44 lnet_t the_lnet; /* THE state of the network */
45 EXPORT_SYMBOL(the_lnet);
47 static char *ip2nets = "";
48 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
49 "LNET network <- IP table");
51 static char *networks = "";
52 CFS_MODULE_PARM(networks, "s", charp, 0444,
55 static char *routes = "";
56 CFS_MODULE_PARM(routes, "s", charp, 0444,
57 "routes to non-local networks");
59 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
60 CFS_MODULE_PARM(rnet_htable_size, "i", int, 0444,
61 "size of remote network hash table");
63 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
64 lnet_process_id_t *ids, int n_ids);
73 lnet_get_networks(void)
78 if (*networks != 0 && *ip2nets != 0) {
79 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
80 "'ip2nets' but not both at once\n");
85 rc = lnet_parse_ip2nets(&nets, ip2nets);
86 return (rc == 0) ? nets : NULL;
98 spin_lock_init(&the_lnet.ln_eq_wait_lock);
99 init_waitqueue_head(&the_lnet.ln_eq_waitq);
100 init_waitqueue_head(&the_lnet.ln_rc_waitq);
101 mutex_init(&the_lnet.ln_lnd_mutex);
102 mutex_init(&the_lnet.ln_api_mutex);
106 lnet_fini_locks(void)
111 lnet_create_remote_nets_table(void)
114 struct list_head *hash;
116 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
117 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
118 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
120 CERROR("Failed to create remote nets hash table\n");
124 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
125 INIT_LIST_HEAD(&hash[i]);
126 the_lnet.ln_remote_nets_hash = hash;
131 lnet_destroy_remote_nets_table(void)
135 if (the_lnet.ln_remote_nets_hash == NULL)
138 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
139 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
141 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
142 LNET_REMOTE_NETS_HASH_SIZE *
143 sizeof(the_lnet.ln_remote_nets_hash[0]));
144 the_lnet.ln_remote_nets_hash = NULL;
148 lnet_destroy_locks(void)
150 if (the_lnet.ln_res_lock != NULL) {
151 cfs_percpt_lock_free(the_lnet.ln_res_lock);
152 the_lnet.ln_res_lock = NULL;
155 if (the_lnet.ln_net_lock != NULL) {
156 cfs_percpt_lock_free(the_lnet.ln_net_lock);
157 the_lnet.ln_net_lock = NULL;
164 lnet_create_locks(void)
168 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
169 if (the_lnet.ln_res_lock == NULL)
172 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
173 if (the_lnet.ln_net_lock == NULL)
179 lnet_destroy_locks();
183 static void lnet_assert_wire_constants(void)
185 /* Wire protocol assertions generated by 'wirecheck'
186 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
187 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
188 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
191 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
192 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
193 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
194 CLASSERT (LNET_MSG_ACK == 0);
195 CLASSERT (LNET_MSG_PUT == 1);
196 CLASSERT (LNET_MSG_GET == 2);
197 CLASSERT (LNET_MSG_REPLY == 3);
198 CLASSERT (LNET_MSG_HELLO == 4);
200 /* Checks for struct ptl_handle_wire_t */
201 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
202 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
203 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
204 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
205 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
207 /* Checks for struct lnet_magicversion_t */
208 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
209 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
210 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
211 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
212 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
213 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
214 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
216 /* Checks for struct lnet_hdr_t */
217 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
218 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
219 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
220 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
221 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
222 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
223 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
224 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
225 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
226 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
227 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
228 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
229 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
230 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
231 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
234 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
235 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
236 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
237 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
238 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
239 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
242 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
243 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
244 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
245 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
246 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
247 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
248 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
249 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
250 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
251 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
254 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
255 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
256 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
257 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
258 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
259 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
260 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
261 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
262 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
263 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
266 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
267 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
270 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
271 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
272 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
273 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
277 lnet_find_lnd_by_type (int type)
280 struct list_head *tmp;
282 /* holding lnd mutex */
283 list_for_each(tmp, &the_lnet.ln_lnds) {
284 lnd = list_entry(tmp, lnd_t, lnd_list);
286 if ((int)lnd->lnd_type == type)
293 lnet_register_lnd (lnd_t *lnd)
295 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
297 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
298 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
300 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
301 lnd->lnd_refcount = 0;
303 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
305 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
307 EXPORT_SYMBOL(lnet_register_lnd);
310 lnet_unregister_lnd (lnd_t *lnd)
312 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
314 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
315 LASSERT(lnd->lnd_refcount == 0);
317 list_del(&lnd->lnd_list);
318 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
320 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
322 EXPORT_SYMBOL(lnet_unregister_lnd);
325 lnet_counters_get(lnet_counters_t *counters)
327 lnet_counters_t *ctr;
330 memset(counters, 0, sizeof(*counters));
332 lnet_net_lock(LNET_LOCK_EX);
334 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
335 counters->msgs_max += ctr->msgs_max;
336 counters->msgs_alloc += ctr->msgs_alloc;
337 counters->errors += ctr->errors;
338 counters->send_count += ctr->send_count;
339 counters->recv_count += ctr->recv_count;
340 counters->route_count += ctr->route_count;
341 counters->drop_count += ctr->drop_count;
342 counters->send_length += ctr->send_length;
343 counters->recv_length += ctr->recv_length;
344 counters->route_length += ctr->route_length;
345 counters->drop_length += ctr->drop_length;
348 lnet_net_unlock(LNET_LOCK_EX);
350 EXPORT_SYMBOL(lnet_counters_get);
353 lnet_counters_reset(void)
355 lnet_counters_t *counters;
358 lnet_net_lock(LNET_LOCK_EX);
360 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
361 memset(counters, 0, sizeof(lnet_counters_t));
363 lnet_net_unlock(LNET_LOCK_EX);
365 EXPORT_SYMBOL(lnet_counters_reset);
367 static __u64 lnet_create_interface_cookie(void)
369 /* NB the interface cookie in wire handles guards against delayed
370 * replies and ACKs appearing valid after reboot. Initialisation time,
371 * even if it's only implemented to millisecond resolution is probably
372 * easily good enough. */
375 do_gettimeofday(&tv);
378 cookie += tv.tv_usec;
383 lnet_res_type2str(int type)
388 case LNET_COOKIE_TYPE_MD:
390 case LNET_COOKIE_TYPE_ME:
392 case LNET_COOKIE_TYPE_EQ:
398 lnet_res_container_cleanup(struct lnet_res_container *rec)
402 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
405 while (!list_empty(&rec->rec_active)) {
406 struct list_head *e = rec->rec_active.next;
409 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
410 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
412 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
413 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
415 } else { /* NB: Active MEs should be attached on portals */
422 /* Found alive MD/ME/EQ, user really should unlink/free
423 * all of them before finalize LNet, but if someone didn't,
424 * we have to recycle garbage for him */
425 CERROR("%d active elements on exit of %s container\n",
426 count, lnet_res_type2str(rec->rec_type));
429 if (rec->rec_lh_hash != NULL) {
430 LIBCFS_FREE(rec->rec_lh_hash,
431 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
432 rec->rec_lh_hash = NULL;
435 rec->rec_type = 0; /* mark it as finalized */
439 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
444 LASSERT(rec->rec_type == 0);
446 rec->rec_type = type;
447 INIT_LIST_HEAD(&rec->rec_active);
449 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
451 /* Arbitrary choice of hash table size */
452 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
453 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
454 if (rec->rec_lh_hash == NULL) {
459 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
460 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
465 CERROR("Failed to setup %s resource container\n",
466 lnet_res_type2str(type));
467 lnet_res_container_cleanup(rec);
472 lnet_res_containers_destroy(struct lnet_res_container **recs)
474 struct lnet_res_container *rec;
477 cfs_percpt_for_each(rec, i, recs)
478 lnet_res_container_cleanup(rec);
480 cfs_percpt_free(recs);
483 static struct lnet_res_container **
484 lnet_res_containers_create(int type)
486 struct lnet_res_container **recs;
487 struct lnet_res_container *rec;
491 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
493 CERROR("Failed to allocate %s resource containers\n",
494 lnet_res_type2str(type));
498 cfs_percpt_for_each(rec, i, recs) {
499 rc = lnet_res_container_setup(rec, i, type);
501 lnet_res_containers_destroy(recs);
510 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
512 /* ALWAYS called with lnet_res_lock held */
513 struct list_head *head;
514 lnet_libhandle_t *lh;
517 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
520 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
521 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
523 list_for_each_entry(lh, head, lh_hash_chain) {
524 if (lh->lh_cookie == cookie)
532 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
534 /* ALWAYS called with lnet_res_lock held */
535 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
538 lh->lh_cookie = rec->rec_lh_cookie;
539 rec->rec_lh_cookie += 1 << ibits;
541 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
543 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
546 static int lnet_unprepare(void);
549 lnet_prepare(lnet_pid_t requested_pid)
551 /* Prepare to bring up the network */
552 struct lnet_res_container **recs;
555 if (requested_pid == LNET_PID_ANY) {
556 /* Don't instantiate LNET just for me */
560 LASSERT(the_lnet.ln_refcount == 0);
562 the_lnet.ln_routing = 0;
564 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
565 the_lnet.ln_pid = requested_pid;
567 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
568 INIT_LIST_HEAD(&the_lnet.ln_nis);
569 INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
570 INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
571 INIT_LIST_HEAD(&the_lnet.ln_routers);
572 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
573 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
575 rc = lnet_create_remote_nets_table();
579 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
581 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
582 sizeof(lnet_counters_t));
583 if (the_lnet.ln_counters == NULL) {
584 CERROR("Failed to allocate counters for LNet\n");
589 rc = lnet_peer_tables_create();
593 rc = lnet_msg_containers_create();
597 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
598 LNET_COOKIE_TYPE_EQ);
602 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
608 the_lnet.ln_me_containers = recs;
610 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
616 the_lnet.ln_md_containers = recs;
618 rc = lnet_portals_create();
620 CERROR("Failed to create portals for LNet: %d\n", rc);
632 lnet_unprepare (void)
634 /* NB no LNET_LOCK since this is the last reference. All LND instances
635 * have shut down already, so it is safe to unlink and free all
636 * descriptors, even those that appear committed to a network op (eg MD
637 * with non-zero pending count) */
639 lnet_fail_nid(LNET_NID_ANY, 0);
641 LASSERT(the_lnet.ln_refcount == 0);
642 LASSERT(list_empty(&the_lnet.ln_test_peers));
643 LASSERT(list_empty(&the_lnet.ln_nis));
644 LASSERT(list_empty(&the_lnet.ln_nis_cpt));
645 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
647 lnet_portals_destroy();
649 if (the_lnet.ln_md_containers != NULL) {
650 lnet_res_containers_destroy(the_lnet.ln_md_containers);
651 the_lnet.ln_md_containers = NULL;
654 if (the_lnet.ln_me_containers != NULL) {
655 lnet_res_containers_destroy(the_lnet.ln_me_containers);
656 the_lnet.ln_me_containers = NULL;
659 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
661 lnet_msg_containers_destroy();
662 lnet_peer_tables_destroy();
663 lnet_rtrpools_free(0);
665 if (the_lnet.ln_counters != NULL) {
666 cfs_percpt_free(the_lnet.ln_counters);
667 the_lnet.ln_counters = NULL;
669 lnet_destroy_remote_nets_table();
675 lnet_net2ni_locked(__u32 net, int cpt)
677 struct list_head *tmp;
680 LASSERT(cpt != LNET_LOCK_EX);
682 list_for_each(tmp, &the_lnet.ln_nis) {
683 ni = list_entry(tmp, lnet_ni_t, ni_list);
685 if (LNET_NIDNET(ni->ni_nid) == net) {
686 lnet_ni_addref_locked(ni, cpt);
695 lnet_net2ni(__u32 net)
700 ni = lnet_net2ni_locked(net, 0);
705 EXPORT_SYMBOL(lnet_net2ni);
708 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
713 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
718 val = hash_long(key, LNET_CPT_BITS);
719 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
723 return (unsigned int)(key + val + (val >> 1)) % number;
727 lnet_cpt_of_nid_locked(lnet_nid_t nid)
731 /* must called with hold of lnet_net_lock */
732 if (LNET_CPT_NUMBER == 1)
733 return 0; /* the only one */
735 /* take lnet_net_lock(any) would be OK */
736 if (!list_empty(&the_lnet.ln_nis_cpt)) {
737 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
738 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
741 LASSERT(ni->ni_cpts != NULL);
742 return ni->ni_cpts[lnet_nid_cpt_hash
743 (nid, ni->ni_ncpts)];
747 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
751 lnet_cpt_of_nid(lnet_nid_t nid)
756 if (LNET_CPT_NUMBER == 1)
757 return 0; /* the only one */
759 if (list_empty(&the_lnet.ln_nis_cpt))
760 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
762 cpt = lnet_net_lock_current();
763 cpt2 = lnet_cpt_of_nid_locked(nid);
764 lnet_net_unlock(cpt);
768 EXPORT_SYMBOL(lnet_cpt_of_nid);
771 lnet_islocalnet(__u32 net)
776 cpt = lnet_net_lock_current();
778 ni = lnet_net2ni_locked(net, cpt);
780 lnet_ni_decref_locked(ni, cpt);
782 lnet_net_unlock(cpt);
788 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
791 struct list_head *tmp;
793 LASSERT(cpt != LNET_LOCK_EX);
795 list_for_each(tmp, &the_lnet.ln_nis) {
796 ni = list_entry(tmp, lnet_ni_t, ni_list);
798 if (ni->ni_nid == nid) {
799 lnet_ni_addref_locked(ni, cpt);
808 lnet_islocalnid(lnet_nid_t nid)
813 cpt = lnet_net_lock_current();
814 ni = lnet_nid2ni_locked(nid, cpt);
816 lnet_ni_decref_locked(ni, cpt);
817 lnet_net_unlock(cpt);
823 lnet_count_acceptor_nis (void)
825 /* Return the # of NIs that need the acceptor. */
827 struct list_head *tmp;
831 cpt = lnet_net_lock_current();
832 list_for_each(tmp, &the_lnet.ln_nis) {
833 ni = list_entry(tmp, lnet_ni_t, ni_list);
835 if (ni->ni_lnd->lnd_accept != NULL)
839 lnet_net_unlock(cpt);
844 static lnet_ping_info_t *
845 lnet_ping_info_create(int num_ni)
847 lnet_ping_info_t *ping_info;
850 infosz = offsetof(lnet_ping_info_t, pi_ni[num_ni]);
851 LIBCFS_ALLOC(ping_info, infosz);
852 if (ping_info == NULL) {
853 CERROR("Can't allocate ping info[%d]\n", num_ni);
857 ping_info->pi_nnis = num_ni;
858 ping_info->pi_pid = the_lnet.ln_pid;
859 ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
860 ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
866 lnet_get_ni_count(void)
873 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list)
882 lnet_ping_info_free(lnet_ping_info_t *pinfo)
885 offsetof(lnet_ping_info_t,
886 pi_ni[pinfo->pi_nnis]));
890 lnet_ping_info_destroy(void)
894 lnet_net_lock(LNET_LOCK_EX);
896 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
898 ni->ni_status = NULL;
902 lnet_ping_info_free(the_lnet.ln_ping_info);
903 the_lnet.ln_ping_info = NULL;
905 lnet_net_unlock(LNET_LOCK_EX);
909 lnet_ping_event_handler(lnet_event_t *event)
911 lnet_ping_info_t *pinfo = event->md.user_ptr;
914 pinfo->pi_features = LNET_PING_FEAT_INVAL;
918 lnet_ping_info_setup(lnet_ping_info_t **ppinfo, lnet_handle_md_t *md_handle,
919 int ni_count, bool set_eq)
921 lnet_handle_me_t me_handle;
922 lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
923 lnet_md_t md = {NULL};
927 rc = LNetEQAlloc(0, lnet_ping_event_handler,
928 &the_lnet.ln_ping_target_eq);
930 CERROR("Can't allocate ping EQ: %d\n", rc);
935 *ppinfo = lnet_ping_info_create(ni_count);
936 if (*ppinfo == NULL) {
941 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
942 LNET_PROTO_PING_MATCHBITS, 0,
943 LNET_UNLINK, LNET_INS_AFTER,
946 CERROR("Can't create ping ME: %d\n", rc);
950 /* initialize md content */
952 md.length = offsetof(lnet_ping_info_t,
953 pi_ni[(*ppinfo)->pi_nnis]);
954 md.threshold = LNET_MD_THRESH_INF;
956 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
957 LNET_MD_MANAGE_REMOTE;
959 md.eq_handle = the_lnet.ln_ping_target_eq;
960 md.user_ptr = *ppinfo;
962 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
964 CERROR("Can't attach ping MD: %d\n", rc);
971 rc2 = LNetMEUnlink(me_handle);
974 lnet_ping_info_free(*ppinfo);
978 LNetEQFree(the_lnet.ln_ping_target_eq);
983 lnet_ping_md_unlink(lnet_ping_info_t *pinfo, lnet_handle_md_t *md_handle)
985 sigset_t blocked = cfs_block_allsigs();
987 LNetMDUnlink(*md_handle);
988 LNetInvalidateHandle(md_handle);
990 /* NB md could be busy; this just starts the unlink */
991 while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
992 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
993 cfs_pause(cfs_time_seconds(1));
996 cfs_restore_sigs(blocked);
1000 lnet_ping_info_install_locked(lnet_ping_info_t *ping_info)
1004 lnet_ni_status_t *ns;
1007 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1008 LASSERT(i < ping_info->pi_nnis);
1010 ns = &ping_info->pi_ni[i];
1012 ns->ns_nid = ni->ni_nid;
1015 ns->ns_status = (ni->ni_status != NULL) ?
1016 ni->ni_status->ns_status : LNET_NI_STATUS_UP;
1025 lnet_ping_target_update(lnet_ping_info_t *pinfo, lnet_handle_md_t md_handle)
1027 lnet_ping_info_t *old_pinfo = NULL;
1028 lnet_handle_md_t old_md;
1030 /* switch the NIs to point to the new ping info created */
1031 lnet_net_lock(LNET_LOCK_EX);
1033 if (!the_lnet.ln_routing)
1034 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1035 lnet_ping_info_install_locked(pinfo);
1037 if (the_lnet.ln_ping_info != NULL) {
1038 old_pinfo = the_lnet.ln_ping_info;
1039 old_md = the_lnet.ln_ping_target_md;
1041 the_lnet.ln_ping_target_md = md_handle;
1042 the_lnet.ln_ping_info = pinfo;
1044 lnet_net_unlock(LNET_LOCK_EX);
1046 if (old_pinfo != NULL) {
1047 /* unlink the old ping info */
1048 lnet_ping_md_unlink(old_pinfo, &old_md);
1049 lnet_ping_info_free(old_pinfo);
1054 lnet_ping_target_fini(void)
1058 lnet_ping_md_unlink(the_lnet.ln_ping_info,
1059 &the_lnet.ln_ping_target_md);
1061 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1064 lnet_ping_info_destroy();
1068 lnet_ni_tq_credits(lnet_ni_t *ni)
1072 LASSERT(ni->ni_ncpts >= 1);
1074 if (ni->ni_ncpts == 1)
1075 return ni->ni_maxtxcredits;
1077 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1078 credits = max(credits, 8 * ni->ni_peertxcredits);
1079 credits = min(credits, ni->ni_maxtxcredits);
1085 lnet_ni_unlink_locked(lnet_ni_t *ni)
1087 if (!list_empty(&ni->ni_cptlist)) {
1088 list_del_init(&ni->ni_cptlist);
1089 lnet_ni_decref_locked(ni, 0);
1092 /* move it to zombie list and nobody can find it anymore */
1093 LASSERT(!list_empty(&ni->ni_list));
1094 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1095 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
1099 lnet_clear_zombies_nis_locked(void)
1105 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1106 * and shut them down in guaranteed thread context */
1108 while (!list_empty(&the_lnet.ln_nis_zombie)) {
1112 ni = list_entry(the_lnet.ln_nis_zombie.next,
1113 lnet_ni_t, ni_list);
1114 list_del_init(&ni->ni_list);
1115 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1118 /* still busy, add it back to zombie list */
1119 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1123 if (!list_empty(&ni->ni_list)) {
1124 lnet_net_unlock(LNET_LOCK_EX);
1126 if ((i & (-i)) == i) {
1128 "Waiting for zombie LNI %s\n",
1129 libcfs_nid2str(ni->ni_nid));
1131 cfs_pause(cfs_time_seconds(1));
1132 lnet_net_lock(LNET_LOCK_EX);
1136 ni->ni_lnd->lnd_refcount--;
1137 lnet_net_unlock(LNET_LOCK_EX);
1139 islo = ni->ni_lnd->lnd_type == LOLND;
1141 LASSERT(!in_interrupt());
1142 (ni->ni_lnd->lnd_shutdown)(ni);
1144 /* can't deref lnd anymore now; it might have unregistered
1148 CDEBUG(D_LNI, "Removed LNI %s\n",
1149 libcfs_nid2str(ni->ni_nid));
1153 lnet_net_lock(LNET_LOCK_EX);
1158 lnet_shutdown_lndnis(void)
1163 /* NB called holding the global mutex */
1165 /* All quiet on the API front */
1166 LASSERT(!the_lnet.ln_shutdown);
1167 LASSERT(the_lnet.ln_refcount == 0);
1168 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
1170 lnet_net_lock(LNET_LOCK_EX);
1171 the_lnet.ln_shutdown = 1; /* flag shutdown */
1173 /* Unlink NIs from the global table */
1174 while (!list_empty(&the_lnet.ln_nis)) {
1175 ni = list_entry(the_lnet.ln_nis.next,
1176 lnet_ni_t, ni_list);
1177 lnet_ni_unlink_locked(ni);
1180 /* Drop the cached loopback NI. */
1181 if (the_lnet.ln_loni != NULL) {
1182 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1183 the_lnet.ln_loni = NULL;
1186 lnet_net_unlock(LNET_LOCK_EX);
1188 /* Clear lazy portals and drop delayed messages which hold refs
1189 * on their lnet_msg_t::msg_rxpeer */
1190 for (i = 0; i < the_lnet.ln_nportals; i++)
1191 LNetClearLazyPortal(i);
1193 /* Clear the peer table and wait for all peers to go (they hold refs on
1195 lnet_peer_tables_cleanup(NULL);
1197 lnet_net_lock(LNET_LOCK_EX);
1199 lnet_clear_zombies_nis_locked();
1200 the_lnet.ln_shutdown = 0;
1201 lnet_net_unlock(LNET_LOCK_EX);
1204 /* shutdown down the NI and release refcount */
1206 lnet_shutdown_lndni(struct lnet_ni *ni)
1208 lnet_net_lock(LNET_LOCK_EX);
1209 lnet_ni_unlink_locked(ni);
1210 lnet_net_unlock(LNET_LOCK_EX);
1212 /* Do peer table cleanup for this ni */
1213 lnet_peer_tables_cleanup(ni);
1215 lnet_net_lock(LNET_LOCK_EX);
1216 lnet_clear_zombies_nis_locked();
1217 lnet_net_unlock(LNET_LOCK_EX);
1221 lnet_startup_lndni(struct lnet_ni *ni, __s32 peer_timeout,
1222 __s32 peer_cr, __s32 peer_buf_cr, __s32 credits)
1227 struct lnet_tx_queue *tq;
1230 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1232 LASSERT(libcfs_isknown_lnd(lnd_type));
1234 if (lnd_type == CIBLND || lnd_type == OPENIBLND ||
1235 lnd_type == IIBLND || lnd_type == VIBLND) {
1236 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type));
1240 /* Make sure this new NI is unique. */
1241 lnet_net_lock(LNET_LOCK_EX);
1242 rc = lnet_net_unique(LNET_NIDNET(ni->ni_nid), &the_lnet.ln_nis);
1243 lnet_net_unlock(LNET_LOCK_EX);
1246 if (lnd_type == LOLND) {
1251 CERROR("Net %s is not unique\n",
1252 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
1258 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1259 lnd = lnet_find_lnd_by_type(lnd_type);
1262 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1263 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1264 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1266 lnd = lnet_find_lnd_by_type(lnd_type);
1268 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1269 CERROR("Can't load LND %s, module %s, rc=%d\n",
1270 libcfs_lnd2str(lnd_type),
1271 libcfs_lnd2modname(lnd_type), rc);
1272 #ifndef HAVE_MODULE_LOADING_SUPPORT
1273 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1274 "compiled with kernel module "
1275 "loading support.");
1282 lnet_net_lock(LNET_LOCK_EX);
1283 lnd->lnd_refcount++;
1284 lnet_net_unlock(LNET_LOCK_EX);
1288 rc = (lnd->lnd_startup)(ni);
1290 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1293 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1294 rc, libcfs_lnd2str(lnd->lnd_type));
1295 lnet_net_lock(LNET_LOCK_EX);
1296 lnd->lnd_refcount--;
1297 lnet_net_unlock(LNET_LOCK_EX);
1301 /* If given some LND tunable parameters, parse those now to
1302 * override the values in the NI structure. */
1303 if (peer_buf_cr >= 0)
1304 ni->ni_peerrtrcredits = peer_buf_cr;
1305 if (peer_timeout >= 0)
1306 ni->ni_peertimeout = peer_timeout;
1309 * Note: For now, don't allow the user to change
1310 * peertxcredits as this number is used in the
1311 * IB LND to control queue depth.
1312 * if (peer_cr != -1)
1313 * ni->ni_peertxcredits = peer_cr;
1316 ni->ni_maxtxcredits = credits;
1318 LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1320 lnet_net_lock(LNET_LOCK_EX);
1321 /* refcount for ln_nis */
1322 lnet_ni_addref_locked(ni, 0);
1323 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1324 if (ni->ni_cpts != NULL) {
1325 lnet_ni_addref_locked(ni, 0);
1326 list_add_tail(&ni->ni_cptlist, &the_lnet.ln_nis_cpt);
1329 lnet_net_unlock(LNET_LOCK_EX);
1331 if (lnd->lnd_type == LOLND) {
1333 LASSERT(the_lnet.ln_loni == NULL);
1334 the_lnet.ln_loni = ni;
1338 if (ni->ni_peertxcredits == 0 || ni->ni_maxtxcredits == 0) {
1339 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1340 libcfs_lnd2str(lnd->lnd_type),
1341 ni->ni_peertxcredits == 0 ?
1343 /* shutdown the NI since if we get here then it must've already
1346 lnet_shutdown_lndni(ni);
1350 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1351 tq->tq_credits_min =
1352 tq->tq_credits_max =
1353 tq->tq_credits = lnet_ni_tq_credits(ni);
1356 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1357 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1358 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1359 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1368 lnet_startup_lndnis(struct list_head *nilist)
1374 while (!list_empty(nilist)) {
1375 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
1376 list_del(&ni->ni_list);
1377 rc = lnet_startup_lndni(ni, -1, -1, -1, -1);
1387 lnet_shutdown_lndnis();
1393 * Initialize LNet library.
1395 * Automatically called at module loading time. Caller has to call
1396 * lnet_fini() after a call to lnet_init(), if and only if the latter
1397 * returned 0. It must be called exactly once.
1399 * \return 0 on success, and -ve on failures.
1406 lnet_assert_wire_constants();
1408 memset(&the_lnet, 0, sizeof(the_lnet));
1410 /* refer to global cfs_cpt_table for now */
1411 the_lnet.ln_cpt_table = cfs_cpt_table;
1412 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1414 LASSERT(the_lnet.ln_cpt_number > 0);
1415 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1416 /* we are under risk of consuming all lh_cookie */
1417 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1418 "please change setting of CPT-table and retry\n",
1419 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1423 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1424 the_lnet.ln_cpt_bits++;
1426 rc = lnet_create_locks();
1428 CERROR("Can't create LNet global locks: %d\n", rc);
1432 the_lnet.ln_refcount = 0;
1433 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1434 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1435 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1436 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1438 /* The hash table size is the number of bits it takes to express the set
1439 * ln_num_routes, minus 1 (better to under estimate than over so we
1440 * don't waste memory). */
1441 if (rnet_htable_size <= 0)
1442 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1443 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1444 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1445 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1446 order_base_2(rnet_htable_size) - 1);
1448 /* All LNDs apart from the LOLND are in separate modules. They
1449 * register themselves when their module loads, and unregister
1450 * themselves when their module is unloaded. */
1451 lnet_register_lnd(&the_lolnd);
1456 * Finalize LNet library.
1458 * \pre lnet_init() called with success.
1459 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1464 LASSERT(the_lnet.ln_refcount == 0);
1466 while (!list_empty(&the_lnet.ln_lnds))
1467 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1469 lnet_destroy_locks();
1473 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1475 * Users must call this function at least once before any other functions.
1476 * For each successful call there must be a corresponding call to
1477 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1480 * The PID used by LNet may be different from the one requested.
1483 * \param requested_pid PID requested by the caller.
1485 * \return >= 0 on success, and < 0 error code on failures.
1488 LNetNIInit(lnet_pid_t requested_pid)
1490 int im_a_router = 0;
1493 lnet_ping_info_t *pinfo;
1494 lnet_handle_md_t md_handle;
1495 struct list_head net_head;
1497 INIT_LIST_HEAD(&net_head);
1499 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1501 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1503 if (the_lnet.ln_refcount > 0) {
1504 rc = the_lnet.ln_refcount++;
1505 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1509 rc = lnet_prepare(requested_pid);
1511 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1515 /* Add in the loopback network */
1516 if (lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, &net_head) == NULL) {
1521 /* If LNet is being initialized via DLC it is possible
1522 * that the user requests not to load module parameters (ones which
1523 * are supported by DLC) on initialization. Therefore, make sure not
1524 * to load networks, routes and forwarding from module parameters
1525 * in this case. On cleanup in case of failure only clean up
1526 * routes if it has been loaded */
1527 if (!the_lnet.ln_nis_from_mod_params) {
1528 rc = lnet_parse_networks(&net_head,
1529 lnet_get_networks());
1534 ni_count = lnet_startup_lndnis(&net_head);
1540 if (!the_lnet.ln_nis_from_mod_params) {
1541 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1545 rc = lnet_check_routes();
1549 rc = lnet_rtrpools_alloc(im_a_router);
1554 rc = lnet_acceptor_start();
1557 the_lnet.ln_refcount = 1;
1558 /* Now I may use my own API functions... */
1560 rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1564 lnet_ping_target_update(pinfo, md_handle);
1566 rc = lnet_router_checker_start();
1573 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1578 lnet_ping_target_fini();
1580 the_lnet.ln_refcount = 0;
1581 lnet_acceptor_stop();
1583 if (!the_lnet.ln_nis_from_mod_params)
1584 lnet_destroy_routes();
1586 lnet_shutdown_lndnis();
1590 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1591 while (!list_empty(&net_head)) {
1593 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1594 list_del_init(&ni->ni_list);
1599 EXPORT_SYMBOL(LNetNIInit);
1602 * Stop LNet interfaces, routing, and forwarding.
1604 * Users must call this function once for each successful call to LNetNIInit().
1605 * Once the LNetNIFini() operation has been started, the results of pending
1606 * API operations are undefined.
1608 * \return always 0 for current implementation.
1613 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1615 LASSERT (the_lnet.ln_refcount > 0);
1617 if (the_lnet.ln_refcount != 1) {
1618 the_lnet.ln_refcount--;
1620 LASSERT(!the_lnet.ln_niinit_self);
1625 lnet_router_checker_stop();
1626 lnet_ping_target_fini();
1628 /* Teardown fns that use my own API functions BEFORE here */
1629 the_lnet.ln_refcount = 0;
1631 lnet_acceptor_stop();
1632 lnet_destroy_routes();
1633 lnet_shutdown_lndnis();
1637 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1640 EXPORT_SYMBOL(LNetNIFini);
1643 * Grabs the ni data from the ni structure and fills the out
1646 * \param[in] ni network interface structure
1647 * \param[out] cpt_count the number of cpts the ni is on
1648 * \param[out] nid Network Interface ID
1649 * \param[out] peer_timeout NI peer timeout
1650 * \param[out] peer_tx_crdits NI peer transmit credits
1651 * \param[out] peer_rtr_credits NI peer router credits
1652 * \param[out] max_tx_credits NI max transmit credit
1653 * \param[out] net_config Network configuration
1656 lnet_fill_ni_info(struct lnet_ni *ni, __u32 *cpt_count, __u64 *nid,
1657 int *peer_timeout, int *peer_tx_credits,
1658 int *peer_rtr_credits, int *max_tx_credits,
1659 struct lnet_ioctl_net_config *net_config)
1666 if (net_config == NULL)
1669 CLASSERT(ARRAY_SIZE(ni->ni_interfaces) ==
1670 ARRAY_SIZE(net_config->ni_interfaces));
1672 if (ni->ni_interfaces[0] != NULL) {
1673 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1674 if (ni->ni_interfaces[i] != NULL) {
1675 strncpy(net_config->ni_interfaces[i],
1676 ni->ni_interfaces[i],
1677 sizeof(net_config->ni_interfaces[i]));
1683 *peer_timeout = ni->ni_peertimeout;
1684 *peer_tx_credits = ni->ni_peertxcredits;
1685 *peer_rtr_credits = ni->ni_peerrtrcredits;
1686 *max_tx_credits = ni->ni_maxtxcredits;
1688 net_config->ni_status = ni->ni_status->ns_status;
1691 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
1692 i < LNET_MAX_SHOW_NUM_CPT;
1694 net_config->ni_cpts[i] = ni->ni_cpts[i];
1696 *cpt_count = ni->ni_ncpts;
1700 lnet_get_net_config(int idx, __u32 *cpt_count, __u64 *nid, int *peer_timeout,
1701 int *peer_tx_credits, int *peer_rtr_credits,
1702 int *max_tx_credits,
1703 struct lnet_ioctl_net_config *net_config)
1706 struct list_head *tmp;
1710 cpt = lnet_net_lock_current();
1712 list_for_each(tmp, &the_lnet.ln_nis) {
1713 ni = list_entry(tmp, lnet_ni_t, ni_list);
1717 lnet_fill_ni_info(ni, cpt_count, nid, peer_timeout,
1718 peer_tx_credits, peer_rtr_credits,
1719 max_tx_credits, net_config);
1725 lnet_net_unlock(cpt);
1730 lnet_dyn_add_ni(lnet_pid_t requested_pid, char *nets,
1731 __s32 peer_timeout, __s32 peer_cr, __s32 peer_buf_cr,
1734 lnet_ping_info_t *pinfo;
1735 lnet_handle_md_t md_handle;
1737 struct list_head net_head;
1739 lnet_remotenet_t *rnet;
1741 INIT_LIST_HEAD(&net_head);
1743 /* Create a ni structure for the network string */
1744 rc = lnet_parse_networks(&net_head, nets);
1746 return rc == 0 ? -EINVAL : rc;
1748 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1751 rc = -EINVAL; /* only add one interface per call */
1755 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1757 lnet_net_lock(LNET_LOCK_EX);
1758 rnet = lnet_find_net_locked(LNET_NIDNET(ni->ni_nid));
1759 lnet_net_unlock(LNET_LOCK_EX);
1760 /* make sure that the net added doesn't invalidate the current
1761 * configuration LNet is keeping */
1763 CERROR("Adding net %s will invalidate routing configuration\n",
1769 rc = lnet_ping_info_setup(&pinfo, &md_handle, 1 + lnet_get_ni_count(),
1774 list_del_init(&ni->ni_list);
1776 rc = lnet_startup_lndni(ni, peer_timeout, peer_cr,
1777 peer_buf_cr, credits);
1781 if (ni->ni_lnd->lnd_accept != NULL) {
1782 rc = lnet_acceptor_start();
1784 /* shutdown the ni that we just started */
1785 CERROR("Failed to start up acceptor thread\n");
1786 lnet_shutdown_lndni(ni);
1791 lnet_ping_target_update(pinfo, md_handle);
1792 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1797 lnet_ping_md_unlink(pinfo, &md_handle);
1798 lnet_ping_info_free(pinfo);
1800 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1801 while (!list_empty(&net_head)) {
1802 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1803 list_del_init(&ni->ni_list);
1810 lnet_dyn_del_ni(__u32 net)
1813 lnet_ping_info_t *pinfo;
1814 lnet_handle_md_t md_handle;
1817 /* don't allow userspace to shutdown the LOLND */
1818 if (LNET_NETTYP(net) == LOLND)
1821 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1822 /* create and link a new ping info, before removing the old one */
1823 rc = lnet_ping_info_setup(&pinfo, &md_handle,
1824 lnet_get_ni_count() - 1, false);
1828 ni = lnet_net2ni(net);
1834 /* decrement the reference counter taken by lnet_net2ni() */
1835 lnet_ni_decref_locked(ni, 0);
1837 lnet_shutdown_lndni(ni);
1839 if (lnet_count_acceptor_nis() == 0)
1840 lnet_acceptor_stop();
1842 lnet_ping_target_update(pinfo, md_handle);
1845 lnet_ping_md_unlink(pinfo, &md_handle);
1846 lnet_ping_info_free(pinfo);
1848 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1854 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1855 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1856 * internal ioctl handler.
1858 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1860 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1861 * The data will be printed to system console. Don't use it excessively.
1862 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1864 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1867 LNetCtl(unsigned int cmd, void *arg)
1869 struct libcfs_ioctl_data *data = arg;
1870 struct lnet_ioctl_config_data *config;
1871 lnet_process_id_t id = {0};
1875 CLASSERT(LIBCFS_IOC_DATA_MAX >= sizeof(struct lnet_ioctl_net_config) +
1876 sizeof(struct lnet_ioctl_config_data));
1879 case IOC_LIBCFS_GET_NI:
1880 rc = LNetGetId(data->ioc_count, &id);
1881 data->ioc_nid = id.nid;
1884 case IOC_LIBCFS_FAIL_NID:
1885 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1887 case IOC_LIBCFS_ADD_ROUTE:
1890 if (config->cfg_hdr.ioc_len < sizeof(*config))
1893 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1894 rc = lnet_add_route(config->cfg_net,
1895 config->cfg_config_u.cfg_route.rtr_hop,
1897 config->cfg_config_u.cfg_route.
1900 rc = lnet_check_routes();
1902 lnet_del_route(config->cfg_net,
1905 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1908 case IOC_LIBCFS_DEL_ROUTE:
1911 if (config->cfg_hdr.ioc_len < sizeof(*config))
1914 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1915 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
1916 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1919 case IOC_LIBCFS_GET_ROUTE:
1922 if (config->cfg_hdr.ioc_len < sizeof(*config))
1925 return lnet_get_route(config->cfg_count,
1927 &config->cfg_config_u.cfg_route.rtr_hop,
1929 &config->cfg_config_u.cfg_route.rtr_flags,
1930 &config->cfg_config_u.cfg_route.
1933 case IOC_LIBCFS_GET_NET: {
1934 struct lnet_ioctl_net_config *net_config;
1935 size_t total = sizeof(*config) + sizeof(*net_config);
1939 if (config->cfg_hdr.ioc_len < total)
1942 net_config = (struct lnet_ioctl_net_config *)
1944 if (config == NULL || net_config == NULL)
1947 return lnet_get_net_config(config->cfg_count,
1950 &config->cfg_config_u.
1951 cfg_net.net_peer_timeout,
1952 &config->cfg_config_u.cfg_net.
1953 net_peer_tx_credits,
1954 &config->cfg_config_u.cfg_net.
1955 net_peer_rtr_credits,
1956 &config->cfg_config_u.cfg_net.
1961 case IOC_LIBCFS_GET_LNET_STATS:
1963 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
1965 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
1968 lnet_counters_get(&lnet_stats->st_cntrs);
1972 case IOC_LIBCFS_CONFIG_RTR:
1975 if (config->cfg_hdr.ioc_len < sizeof(*config))
1978 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1979 if (config->cfg_config_u.cfg_buffers.buf_enable) {
1980 rc = lnet_rtrpools_enable();
1981 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1984 lnet_rtrpools_disable();
1985 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1988 case IOC_LIBCFS_ADD_BUF:
1991 if (config->cfg_hdr.ioc_len < sizeof(*config))
1994 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1995 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
1997 config->cfg_config_u.cfg_buffers.
1999 config->cfg_config_u.cfg_buffers.
2001 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2004 case IOC_LIBCFS_GET_BUF: {
2005 struct lnet_ioctl_pool_cfg *pool_cfg;
2006 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2010 if (config->cfg_hdr.ioc_len < total)
2013 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2014 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2017 case IOC_LIBCFS_GET_PEER_INFO: {
2018 struct lnet_ioctl_peer *peer_info = arg;
2020 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2023 return lnet_get_peer_info(
2024 peer_info->pr_count,
2026 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2027 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2028 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2029 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2030 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2031 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2032 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2033 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2036 case IOC_LIBCFS_NOTIFY_ROUTER:
2037 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2038 cfs_time_current() -
2039 cfs_time_seconds(cfs_time_current_sec() -
2040 (time_t)data->ioc_u64[0]));
2042 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
2043 /* This can be removed once lustre stops calling it */
2046 case IOC_LIBCFS_LNET_DIST:
2047 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2048 if (rc < 0 && rc != -EHOSTUNREACH)
2051 data->ioc_u32[0] = rc;
2054 case IOC_LIBCFS_TESTPROTOCOMPAT:
2055 lnet_net_lock(LNET_LOCK_EX);
2056 the_lnet.ln_testprotocompat = data->ioc_flags;
2057 lnet_net_unlock(LNET_LOCK_EX);
2060 case IOC_LIBCFS_LNET_FAULT:
2061 return lnet_fault_ctl(data->ioc_flags, data);
2063 case IOC_LIBCFS_PING:
2064 id.nid = data->ioc_nid;
2065 id.pid = data->ioc_u32[0];
2066 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
2067 (lnet_process_id_t __user *)data->ioc_pbuf1,
2068 data->ioc_plen1/sizeof(lnet_process_id_t));
2071 data->ioc_count = rc;
2074 case IOC_LIBCFS_DEBUG_PEER: {
2075 /* CAVEAT EMPTOR: this one designed for calling directly; not
2077 id = *((lnet_process_id_t *) arg);
2079 lnet_debug_peer(id.nid);
2081 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2083 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
2085 if (ni->ni_lnd->lnd_ctl == NULL) {
2086 CDEBUG(D_WARNING, "No ctl for %s\n",
2089 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2098 ni = lnet_net2ni(data->ioc_net);
2102 if (ni->ni_lnd->lnd_ctl == NULL)
2105 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2112 EXPORT_SYMBOL(LNetCtl);
2115 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2116 * all interfaces share a same PID, as requested by LNetNIInit().
2118 * \param index Index of the interface to look up.
2119 * \param id On successful return, this location will hold the
2120 * lnet_process_id_t ID of the interface.
2122 * \retval 0 If an interface exists at \a index.
2123 * \retval -ENOENT If no interface has been found.
2126 LNetGetId(unsigned int index, lnet_process_id_t *id)
2129 struct list_head *tmp;
2133 LASSERT(the_lnet.ln_refcount > 0);
2135 cpt = lnet_net_lock_current();
2137 list_for_each(tmp, &the_lnet.ln_nis) {
2141 ni = list_entry(tmp, lnet_ni_t, ni_list);
2143 id->nid = ni->ni_nid;
2144 id->pid = the_lnet.ln_pid;
2149 lnet_net_unlock(cpt);
2152 EXPORT_SYMBOL(LNetGetId);
2155 * Print a string representation of handle \a h into buffer \a str of
2159 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2161 snprintf(str, len, LPX64, h.cookie);
2163 EXPORT_SYMBOL(LNetSnprintHandle);
2166 lnet_ping(lnet_process_id_t id, int timeout_ms, lnet_process_id_t __user *ids,
2169 lnet_handle_eq_t eqh;
2170 lnet_handle_md_t mdh;
2172 lnet_md_t md = { NULL };
2176 const int a_long_time = 60000; /* mS */
2178 lnet_ping_info_t *info;
2179 lnet_process_id_t tmpid;
2186 infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
2189 id.nid == LNET_NID_ANY ||
2190 timeout_ms > 500000 || /* arbitrary limit! */
2191 n_ids > 20) /* arbitrary limit! */
2194 if (id.pid == LNET_PID_ANY)
2195 id.pid = LNET_PID_LUSTRE;
2197 LIBCFS_ALLOC(info, infosz);
2201 /* NB 2 events max (including any unlink event) */
2202 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2204 CERROR("Can't allocate EQ: %d\n", rc);
2208 /* initialize md content */
2211 md.threshold = 2; /*GET/REPLY*/
2213 md.options = LNET_MD_TRUNCATE;
2217 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2219 CERROR("Can't bind MD: %d\n", rc);
2223 rc = LNetGet(LNET_NID_ANY, mdh, id,
2224 LNET_RESERVED_PORTAL,
2225 LNET_PROTO_PING_MATCHBITS, 0);
2228 /* Don't CERROR; this could be deliberate! */
2230 rc2 = LNetMDUnlink(mdh);
2233 /* NB must wait for the UNLINK event below... */
2235 timeout_ms = a_long_time;
2239 /* MUST block for unlink to complete */
2241 blocked = cfs_block_allsigs();
2243 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
2246 cfs_restore_sigs(blocked);
2248 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2249 (rc2 <= 0) ? -1 : event.type,
2250 (rc2 <= 0) ? -1 : event.status,
2251 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2253 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
2255 if (rc2 <= 0 || event.status != 0) {
2256 /* timeout or error */
2257 if (!replied && rc == 0)
2258 rc = (rc2 < 0) ? rc2 :
2259 (rc2 == 0) ? -ETIMEDOUT :
2263 /* Ensure completion in finite time... */
2265 /* No assertion (racing with network) */
2267 timeout_ms = a_long_time;
2268 } else if (rc2 == 0) {
2269 /* timed out waiting for unlink */
2270 CWARN("ping %s: late network completion\n",
2273 } else if (event.type == LNET_EVENT_REPLY) {
2278 } while (rc2 <= 0 || !event.unlinked);
2282 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2289 LASSERT(nob >= 0 && nob <= infosz);
2291 rc = -EPROTO; /* if I can't parse... */
2294 /* can't check magic/version */
2295 CERROR("%s: ping info too short %d\n",
2296 libcfs_id2str(id), nob);
2300 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2301 lnet_swap_pinginfo(info);
2302 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2303 CERROR("%s: Unexpected magic %08x\n",
2304 libcfs_id2str(id), info->pi_magic);
2308 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2309 CERROR("%s: ping w/o NI status: 0x%x\n",
2310 libcfs_id2str(id), info->pi_features);
2314 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2315 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2316 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2320 if (info->pi_nnis < n_ids)
2321 n_ids = info->pi_nnis;
2323 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2324 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2325 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2329 rc = -EFAULT; /* If I SEGV... */
2331 for (i = 0; i < n_ids; i++) {
2332 tmpid.pid = info->pi_pid;
2333 tmpid.nid = info->pi_ni[i].ns_nid;
2334 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2340 rc2 = LNetEQFree(eqh);
2342 CERROR("rc2 %d\n", rc2);
2346 LIBCFS_FREE(info, infosz);