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 <linux/log2.h>
39 #include <lnet/lib-lnet.h>
41 #define D_LNI D_CONSOLE
43 lnet_t the_lnet; /* THE state of the network */
44 EXPORT_SYMBOL(the_lnet);
46 static char *ip2nets = "";
47 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
48 "LNET network <- IP table");
50 static char *networks = "";
51 CFS_MODULE_PARM(networks, "s", charp, 0444,
54 static char *routes = "";
55 CFS_MODULE_PARM(routes, "s", charp, 0444,
56 "routes to non-local networks");
58 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
59 CFS_MODULE_PARM(rnet_htable_size, "i", int, 0444,
60 "size of remote network hash table");
62 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
63 lnet_process_id_t *ids, int n_ids);
72 lnet_get_networks(void)
77 if (*networks != 0 && *ip2nets != 0) {
78 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
79 "'ip2nets' but not both at once\n");
84 rc = lnet_parse_ip2nets(&nets, ip2nets);
85 return (rc == 0) ? nets : NULL;
97 spin_lock_init(&the_lnet.ln_eq_wait_lock);
98 init_waitqueue_head(&the_lnet.ln_eq_waitq);
99 init_waitqueue_head(&the_lnet.ln_rc_waitq);
100 mutex_init(&the_lnet.ln_lnd_mutex);
101 mutex_init(&the_lnet.ln_api_mutex);
105 lnet_fini_locks(void)
110 lnet_create_remote_nets_table(void)
113 struct list_head *hash;
115 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
116 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
117 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
119 CERROR("Failed to create remote nets hash table\n");
123 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
124 INIT_LIST_HEAD(&hash[i]);
125 the_lnet.ln_remote_nets_hash = hash;
130 lnet_destroy_remote_nets_table(void)
134 if (the_lnet.ln_remote_nets_hash == NULL)
137 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
138 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
140 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
141 LNET_REMOTE_NETS_HASH_SIZE *
142 sizeof(the_lnet.ln_remote_nets_hash[0]));
143 the_lnet.ln_remote_nets_hash = NULL;
147 lnet_destroy_locks(void)
149 if (the_lnet.ln_res_lock != NULL) {
150 cfs_percpt_lock_free(the_lnet.ln_res_lock);
151 the_lnet.ln_res_lock = NULL;
154 if (the_lnet.ln_net_lock != NULL) {
155 cfs_percpt_lock_free(the_lnet.ln_net_lock);
156 the_lnet.ln_net_lock = NULL;
163 lnet_create_locks(void)
167 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
168 if (the_lnet.ln_res_lock == NULL)
171 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
172 if (the_lnet.ln_net_lock == NULL)
178 lnet_destroy_locks();
182 static void lnet_assert_wire_constants(void)
184 /* Wire protocol assertions generated by 'wirecheck'
185 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
186 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
187 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
190 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
191 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
192 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
193 CLASSERT (LNET_MSG_ACK == 0);
194 CLASSERT (LNET_MSG_PUT == 1);
195 CLASSERT (LNET_MSG_GET == 2);
196 CLASSERT (LNET_MSG_REPLY == 3);
197 CLASSERT (LNET_MSG_HELLO == 4);
199 /* Checks for struct ptl_handle_wire_t */
200 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
201 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
202 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
203 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
204 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
206 /* Checks for struct lnet_magicversion_t */
207 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
208 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
209 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
210 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
211 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
212 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
213 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
215 /* Checks for struct lnet_hdr_t */
216 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
217 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
218 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
219 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
220 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
221 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
222 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
223 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
224 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
225 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
226 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
227 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
228 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
229 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
230 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
233 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
234 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
235 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
236 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
237 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
238 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
241 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
242 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
243 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
244 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
245 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
246 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
247 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
248 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
249 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
250 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
253 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
254 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
255 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
256 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
257 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
258 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
259 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
260 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
261 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
262 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
265 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
266 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
269 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
270 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
271 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
272 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
275 static lnd_t *lnet_find_lnd_by_type(__u32 type)
278 struct list_head *tmp;
280 /* holding lnd mutex */
281 list_for_each(tmp, &the_lnet.ln_lnds) {
282 lnd = list_entry(tmp, lnd_t, lnd_list);
284 if (lnd->lnd_type == type)
291 lnet_register_lnd (lnd_t *lnd)
293 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
295 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
296 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
298 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
299 lnd->lnd_refcount = 0;
301 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
303 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
305 EXPORT_SYMBOL(lnet_register_lnd);
308 lnet_unregister_lnd (lnd_t *lnd)
310 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
312 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
313 LASSERT(lnd->lnd_refcount == 0);
315 list_del(&lnd->lnd_list);
316 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
318 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
320 EXPORT_SYMBOL(lnet_unregister_lnd);
323 lnet_counters_get(lnet_counters_t *counters)
325 lnet_counters_t *ctr;
328 memset(counters, 0, sizeof(*counters));
330 lnet_net_lock(LNET_LOCK_EX);
332 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
333 counters->msgs_max += ctr->msgs_max;
334 counters->msgs_alloc += ctr->msgs_alloc;
335 counters->errors += ctr->errors;
336 counters->send_count += ctr->send_count;
337 counters->recv_count += ctr->recv_count;
338 counters->route_count += ctr->route_count;
339 counters->drop_count += ctr->drop_count;
340 counters->send_length += ctr->send_length;
341 counters->recv_length += ctr->recv_length;
342 counters->route_length += ctr->route_length;
343 counters->drop_length += ctr->drop_length;
346 lnet_net_unlock(LNET_LOCK_EX);
348 EXPORT_SYMBOL(lnet_counters_get);
351 lnet_counters_reset(void)
353 lnet_counters_t *counters;
356 lnet_net_lock(LNET_LOCK_EX);
358 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
359 memset(counters, 0, sizeof(lnet_counters_t));
361 lnet_net_unlock(LNET_LOCK_EX);
364 static __u64 lnet_create_interface_cookie(void)
366 /* NB the interface cookie in wire handles guards against delayed
367 * replies and ACKs appearing valid after reboot. Initialisation time,
368 * even if it's only implemented to millisecond resolution is probably
369 * easily good enough. */
372 do_gettimeofday(&tv);
375 cookie += tv.tv_usec;
380 lnet_res_type2str(int type)
385 case LNET_COOKIE_TYPE_MD:
387 case LNET_COOKIE_TYPE_ME:
389 case LNET_COOKIE_TYPE_EQ:
395 lnet_res_container_cleanup(struct lnet_res_container *rec)
399 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
402 while (!list_empty(&rec->rec_active)) {
403 struct list_head *e = rec->rec_active.next;
406 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
407 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
409 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
410 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
412 } else { /* NB: Active MEs should be attached on portals */
419 /* Found alive MD/ME/EQ, user really should unlink/free
420 * all of them before finalize LNet, but if someone didn't,
421 * we have to recycle garbage for him */
422 CERROR("%d active elements on exit of %s container\n",
423 count, lnet_res_type2str(rec->rec_type));
426 if (rec->rec_lh_hash != NULL) {
427 LIBCFS_FREE(rec->rec_lh_hash,
428 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
429 rec->rec_lh_hash = NULL;
432 rec->rec_type = 0; /* mark it as finalized */
436 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
441 LASSERT(rec->rec_type == 0);
443 rec->rec_type = type;
444 INIT_LIST_HEAD(&rec->rec_active);
446 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
448 /* Arbitrary choice of hash table size */
449 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
450 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
451 if (rec->rec_lh_hash == NULL) {
456 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
457 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
462 CERROR("Failed to setup %s resource container\n",
463 lnet_res_type2str(type));
464 lnet_res_container_cleanup(rec);
469 lnet_res_containers_destroy(struct lnet_res_container **recs)
471 struct lnet_res_container *rec;
474 cfs_percpt_for_each(rec, i, recs)
475 lnet_res_container_cleanup(rec);
477 cfs_percpt_free(recs);
480 static struct lnet_res_container **
481 lnet_res_containers_create(int type)
483 struct lnet_res_container **recs;
484 struct lnet_res_container *rec;
488 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
490 CERROR("Failed to allocate %s resource containers\n",
491 lnet_res_type2str(type));
495 cfs_percpt_for_each(rec, i, recs) {
496 rc = lnet_res_container_setup(rec, i, type);
498 lnet_res_containers_destroy(recs);
507 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
509 /* ALWAYS called with lnet_res_lock held */
510 struct list_head *head;
511 lnet_libhandle_t *lh;
514 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
517 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
518 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
520 list_for_each_entry(lh, head, lh_hash_chain) {
521 if (lh->lh_cookie == cookie)
529 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
531 /* ALWAYS called with lnet_res_lock held */
532 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
535 lh->lh_cookie = rec->rec_lh_cookie;
536 rec->rec_lh_cookie += 1 << ibits;
538 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
540 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
543 static int lnet_unprepare(void);
546 lnet_prepare(lnet_pid_t requested_pid)
548 /* Prepare to bring up the network */
549 struct lnet_res_container **recs;
552 if (requested_pid == LNET_PID_ANY) {
553 /* Don't instantiate LNET just for me */
557 LASSERT(the_lnet.ln_refcount == 0);
559 the_lnet.ln_routing = 0;
561 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
562 the_lnet.ln_pid = requested_pid;
564 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
565 INIT_LIST_HEAD(&the_lnet.ln_nis);
566 INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
567 INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
568 INIT_LIST_HEAD(&the_lnet.ln_routers);
569 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
570 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
572 rc = lnet_create_remote_nets_table();
576 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
578 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
579 sizeof(lnet_counters_t));
580 if (the_lnet.ln_counters == NULL) {
581 CERROR("Failed to allocate counters for LNet\n");
586 rc = lnet_peer_tables_create();
590 rc = lnet_msg_containers_create();
594 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
595 LNET_COOKIE_TYPE_EQ);
599 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
605 the_lnet.ln_me_containers = recs;
607 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
613 the_lnet.ln_md_containers = recs;
615 rc = lnet_portals_create();
617 CERROR("Failed to create portals for LNet: %d\n", rc);
629 lnet_unprepare (void)
631 /* NB no LNET_LOCK since this is the last reference. All LND instances
632 * have shut down already, so it is safe to unlink and free all
633 * descriptors, even those that appear committed to a network op (eg MD
634 * with non-zero pending count) */
636 lnet_fail_nid(LNET_NID_ANY, 0);
638 LASSERT(the_lnet.ln_refcount == 0);
639 LASSERT(list_empty(&the_lnet.ln_test_peers));
640 LASSERT(list_empty(&the_lnet.ln_nis));
641 LASSERT(list_empty(&the_lnet.ln_nis_cpt));
642 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
644 lnet_portals_destroy();
646 if (the_lnet.ln_md_containers != NULL) {
647 lnet_res_containers_destroy(the_lnet.ln_md_containers);
648 the_lnet.ln_md_containers = NULL;
651 if (the_lnet.ln_me_containers != NULL) {
652 lnet_res_containers_destroy(the_lnet.ln_me_containers);
653 the_lnet.ln_me_containers = NULL;
656 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
658 lnet_msg_containers_destroy();
659 lnet_peer_tables_destroy();
660 lnet_rtrpools_free(0);
662 if (the_lnet.ln_counters != NULL) {
663 cfs_percpt_free(the_lnet.ln_counters);
664 the_lnet.ln_counters = NULL;
666 lnet_destroy_remote_nets_table();
672 lnet_net2ni_locked(__u32 net, int cpt)
674 struct list_head *tmp;
677 LASSERT(cpt != LNET_LOCK_EX);
679 list_for_each(tmp, &the_lnet.ln_nis) {
680 ni = list_entry(tmp, lnet_ni_t, ni_list);
682 if (LNET_NIDNET(ni->ni_nid) == net) {
683 lnet_ni_addref_locked(ni, cpt);
692 lnet_net2ni(__u32 net)
697 ni = lnet_net2ni_locked(net, 0);
702 EXPORT_SYMBOL(lnet_net2ni);
705 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
710 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
715 val = hash_long(key, LNET_CPT_BITS);
716 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
720 return (unsigned int)(key + val + (val >> 1)) % number;
724 lnet_cpt_of_nid_locked(lnet_nid_t nid)
728 /* must called with hold of lnet_net_lock */
729 if (LNET_CPT_NUMBER == 1)
730 return 0; /* the only one */
732 /* take lnet_net_lock(any) would be OK */
733 if (!list_empty(&the_lnet.ln_nis_cpt)) {
734 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
735 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
738 LASSERT(ni->ni_cpts != NULL);
739 return ni->ni_cpts[lnet_nid_cpt_hash
740 (nid, ni->ni_ncpts)];
744 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
748 lnet_cpt_of_nid(lnet_nid_t nid)
753 if (LNET_CPT_NUMBER == 1)
754 return 0; /* the only one */
756 if (list_empty(&the_lnet.ln_nis_cpt))
757 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
759 cpt = lnet_net_lock_current();
760 cpt2 = lnet_cpt_of_nid_locked(nid);
761 lnet_net_unlock(cpt);
765 EXPORT_SYMBOL(lnet_cpt_of_nid);
768 lnet_islocalnet(__u32 net)
773 cpt = lnet_net_lock_current();
775 ni = lnet_net2ni_locked(net, cpt);
777 lnet_ni_decref_locked(ni, cpt);
779 lnet_net_unlock(cpt);
785 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
788 struct list_head *tmp;
790 LASSERT(cpt != LNET_LOCK_EX);
792 list_for_each(tmp, &the_lnet.ln_nis) {
793 ni = list_entry(tmp, lnet_ni_t, ni_list);
795 if (ni->ni_nid == nid) {
796 lnet_ni_addref_locked(ni, cpt);
805 lnet_islocalnid(lnet_nid_t nid)
810 cpt = lnet_net_lock_current();
811 ni = lnet_nid2ni_locked(nid, cpt);
813 lnet_ni_decref_locked(ni, cpt);
814 lnet_net_unlock(cpt);
820 lnet_count_acceptor_nis (void)
822 /* Return the # of NIs that need the acceptor. */
824 struct list_head *tmp;
828 cpt = lnet_net_lock_current();
829 list_for_each(tmp, &the_lnet.ln_nis) {
830 ni = list_entry(tmp, lnet_ni_t, ni_list);
832 if (ni->ni_lnd->lnd_accept != NULL)
836 lnet_net_unlock(cpt);
841 static lnet_ping_info_t *
842 lnet_ping_info_create(int num_ni)
844 lnet_ping_info_t *ping_info;
847 infosz = offsetof(lnet_ping_info_t, pi_ni[num_ni]);
848 LIBCFS_ALLOC(ping_info, infosz);
849 if (ping_info == NULL) {
850 CERROR("Can't allocate ping info[%d]\n", num_ni);
854 ping_info->pi_nnis = num_ni;
855 ping_info->pi_pid = the_lnet.ln_pid;
856 ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
857 ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
863 lnet_get_ni_count(void)
870 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list)
879 lnet_ping_info_free(lnet_ping_info_t *pinfo)
882 offsetof(lnet_ping_info_t,
883 pi_ni[pinfo->pi_nnis]));
887 lnet_ping_info_destroy(void)
891 lnet_net_lock(LNET_LOCK_EX);
893 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
895 ni->ni_status = NULL;
899 lnet_ping_info_free(the_lnet.ln_ping_info);
900 the_lnet.ln_ping_info = NULL;
902 lnet_net_unlock(LNET_LOCK_EX);
906 lnet_ping_event_handler(lnet_event_t *event)
908 lnet_ping_info_t *pinfo = event->md.user_ptr;
911 pinfo->pi_features = LNET_PING_FEAT_INVAL;
915 lnet_ping_info_setup(lnet_ping_info_t **ppinfo, lnet_handle_md_t *md_handle,
916 int ni_count, bool set_eq)
918 lnet_handle_me_t me_handle;
919 lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
920 lnet_md_t md = {NULL};
924 rc = LNetEQAlloc(0, lnet_ping_event_handler,
925 &the_lnet.ln_ping_target_eq);
927 CERROR("Can't allocate ping EQ: %d\n", rc);
932 *ppinfo = lnet_ping_info_create(ni_count);
933 if (*ppinfo == NULL) {
938 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
939 LNET_PROTO_PING_MATCHBITS, 0,
940 LNET_UNLINK, LNET_INS_AFTER,
943 CERROR("Can't create ping ME: %d\n", rc);
947 /* initialize md content */
949 md.length = offsetof(lnet_ping_info_t,
950 pi_ni[(*ppinfo)->pi_nnis]);
951 md.threshold = LNET_MD_THRESH_INF;
953 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
954 LNET_MD_MANAGE_REMOTE;
956 md.eq_handle = the_lnet.ln_ping_target_eq;
957 md.user_ptr = *ppinfo;
959 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
961 CERROR("Can't attach ping MD: %d\n", rc);
968 rc2 = LNetMEUnlink(me_handle);
971 lnet_ping_info_free(*ppinfo);
975 LNetEQFree(the_lnet.ln_ping_target_eq);
980 lnet_ping_md_unlink(lnet_ping_info_t *pinfo, lnet_handle_md_t *md_handle)
982 sigset_t blocked = cfs_block_allsigs();
984 LNetMDUnlink(*md_handle);
985 LNetInvalidateHandle(md_handle);
987 /* NB md could be busy; this just starts the unlink */
988 while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
989 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
990 cfs_pause(cfs_time_seconds(1));
993 cfs_restore_sigs(blocked);
997 lnet_ping_info_install_locked(lnet_ping_info_t *ping_info)
1001 lnet_ni_status_t *ns;
1004 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1005 LASSERT(i < ping_info->pi_nnis);
1007 ns = &ping_info->pi_ni[i];
1009 ns->ns_nid = ni->ni_nid;
1012 ns->ns_status = (ni->ni_status != NULL) ?
1013 ni->ni_status->ns_status : LNET_NI_STATUS_UP;
1022 lnet_ping_target_update(lnet_ping_info_t *pinfo, lnet_handle_md_t md_handle)
1024 lnet_ping_info_t *old_pinfo = NULL;
1025 lnet_handle_md_t old_md;
1027 /* switch the NIs to point to the new ping info created */
1028 lnet_net_lock(LNET_LOCK_EX);
1030 if (!the_lnet.ln_routing)
1031 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1032 lnet_ping_info_install_locked(pinfo);
1034 if (the_lnet.ln_ping_info != NULL) {
1035 old_pinfo = the_lnet.ln_ping_info;
1036 old_md = the_lnet.ln_ping_target_md;
1038 the_lnet.ln_ping_target_md = md_handle;
1039 the_lnet.ln_ping_info = pinfo;
1041 lnet_net_unlock(LNET_LOCK_EX);
1043 if (old_pinfo != NULL) {
1044 /* unlink the old ping info */
1045 lnet_ping_md_unlink(old_pinfo, &old_md);
1046 lnet_ping_info_free(old_pinfo);
1051 lnet_ping_target_fini(void)
1055 lnet_ping_md_unlink(the_lnet.ln_ping_info,
1056 &the_lnet.ln_ping_target_md);
1058 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1061 lnet_ping_info_destroy();
1065 lnet_ni_tq_credits(lnet_ni_t *ni)
1069 LASSERT(ni->ni_ncpts >= 1);
1071 if (ni->ni_ncpts == 1)
1072 return ni->ni_maxtxcredits;
1074 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1075 credits = max(credits, 8 * ni->ni_peertxcredits);
1076 credits = min(credits, ni->ni_maxtxcredits);
1082 lnet_ni_unlink_locked(lnet_ni_t *ni)
1084 if (!list_empty(&ni->ni_cptlist)) {
1085 list_del_init(&ni->ni_cptlist);
1086 lnet_ni_decref_locked(ni, 0);
1089 /* move it to zombie list and nobody can find it anymore */
1090 LASSERT(!list_empty(&ni->ni_list));
1091 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1092 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
1096 lnet_clear_zombies_nis_locked(void)
1102 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1103 * and shut them down in guaranteed thread context */
1105 while (!list_empty(&the_lnet.ln_nis_zombie)) {
1109 ni = list_entry(the_lnet.ln_nis_zombie.next,
1110 lnet_ni_t, ni_list);
1111 list_del_init(&ni->ni_list);
1112 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1115 /* still busy, add it back to zombie list */
1116 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1120 if (!list_empty(&ni->ni_list)) {
1121 lnet_net_unlock(LNET_LOCK_EX);
1123 if ((i & (-i)) == i) {
1125 "Waiting for zombie LNI %s\n",
1126 libcfs_nid2str(ni->ni_nid));
1128 cfs_pause(cfs_time_seconds(1));
1129 lnet_net_lock(LNET_LOCK_EX);
1133 ni->ni_lnd->lnd_refcount--;
1134 lnet_net_unlock(LNET_LOCK_EX);
1136 islo = ni->ni_lnd->lnd_type == LOLND;
1138 LASSERT(!in_interrupt());
1139 (ni->ni_lnd->lnd_shutdown)(ni);
1141 /* can't deref lnd anymore now; it might have unregistered
1145 CDEBUG(D_LNI, "Removed LNI %s\n",
1146 libcfs_nid2str(ni->ni_nid));
1150 lnet_net_lock(LNET_LOCK_EX);
1155 lnet_shutdown_lndnis(void)
1160 /* NB called holding the global mutex */
1162 /* All quiet on the API front */
1163 LASSERT(!the_lnet.ln_shutdown);
1164 LASSERT(the_lnet.ln_refcount == 0);
1165 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
1167 lnet_net_lock(LNET_LOCK_EX);
1168 the_lnet.ln_shutdown = 1; /* flag shutdown */
1170 /* Unlink NIs from the global table */
1171 while (!list_empty(&the_lnet.ln_nis)) {
1172 ni = list_entry(the_lnet.ln_nis.next,
1173 lnet_ni_t, ni_list);
1174 lnet_ni_unlink_locked(ni);
1177 /* Drop the cached loopback NI. */
1178 if (the_lnet.ln_loni != NULL) {
1179 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1180 the_lnet.ln_loni = NULL;
1183 lnet_net_unlock(LNET_LOCK_EX);
1185 /* Clear lazy portals and drop delayed messages which hold refs
1186 * on their lnet_msg_t::msg_rxpeer */
1187 for (i = 0; i < the_lnet.ln_nportals; i++)
1188 LNetClearLazyPortal(i);
1190 /* Clear the peer table and wait for all peers to go (they hold refs on
1192 lnet_peer_tables_cleanup(NULL);
1194 lnet_net_lock(LNET_LOCK_EX);
1196 lnet_clear_zombies_nis_locked();
1197 the_lnet.ln_shutdown = 0;
1198 lnet_net_unlock(LNET_LOCK_EX);
1201 /* shutdown down the NI and release refcount */
1203 lnet_shutdown_lndni(struct lnet_ni *ni)
1207 lnet_net_lock(LNET_LOCK_EX);
1208 lnet_ni_unlink_locked(ni);
1209 lnet_net_unlock(LNET_LOCK_EX);
1211 /* clear messages for this NI on the lazy portal */
1212 for (i = 0; i < the_lnet.ln_nportals; i++)
1213 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1215 /* Do peer table cleanup for this ni */
1216 lnet_peer_tables_cleanup(ni);
1218 lnet_net_lock(LNET_LOCK_EX);
1219 lnet_clear_zombies_nis_locked();
1220 lnet_net_unlock(LNET_LOCK_EX);
1224 lnet_startup_lndni(struct lnet_ni *ni, __s32 peer_timeout,
1225 __s32 peer_cr, __s32 peer_buf_cr, __s32 credits)
1230 struct lnet_tx_queue *tq;
1233 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1235 LASSERT(libcfs_isknown_lnd(lnd_type));
1237 if (lnd_type == CIBLND || lnd_type == OPENIBLND ||
1238 lnd_type == IIBLND || lnd_type == VIBLND) {
1239 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type));
1243 /* Make sure this new NI is unique. */
1244 lnet_net_lock(LNET_LOCK_EX);
1245 rc = lnet_net_unique(LNET_NIDNET(ni->ni_nid), &the_lnet.ln_nis);
1246 lnet_net_unlock(LNET_LOCK_EX);
1249 if (lnd_type == LOLND) {
1254 CERROR("Net %s is not unique\n",
1255 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
1261 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1262 lnd = lnet_find_lnd_by_type(lnd_type);
1265 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1266 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1267 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1269 lnd = lnet_find_lnd_by_type(lnd_type);
1271 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1272 CERROR("Can't load LND %s, module %s, rc=%d\n",
1273 libcfs_lnd2str(lnd_type),
1274 libcfs_lnd2modname(lnd_type), rc);
1275 #ifndef HAVE_MODULE_LOADING_SUPPORT
1276 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1277 "compiled with kernel module "
1278 "loading support.");
1285 lnet_net_lock(LNET_LOCK_EX);
1286 lnd->lnd_refcount++;
1287 lnet_net_unlock(LNET_LOCK_EX);
1291 rc = (lnd->lnd_startup)(ni);
1293 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1296 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1297 rc, libcfs_lnd2str(lnd->lnd_type));
1298 lnet_net_lock(LNET_LOCK_EX);
1299 lnd->lnd_refcount--;
1300 lnet_net_unlock(LNET_LOCK_EX);
1304 /* If given some LND tunable parameters, parse those now to
1305 * override the values in the NI structure. */
1306 if (peer_buf_cr >= 0)
1307 ni->ni_peerrtrcredits = peer_buf_cr;
1308 if (peer_timeout >= 0)
1309 ni->ni_peertimeout = peer_timeout;
1312 * Note: For now, don't allow the user to change
1313 * peertxcredits as this number is used in the
1314 * IB LND to control queue depth.
1315 * if (peer_cr != -1)
1316 * ni->ni_peertxcredits = peer_cr;
1319 ni->ni_maxtxcredits = credits;
1321 LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1323 lnet_net_lock(LNET_LOCK_EX);
1324 /* refcount for ln_nis */
1325 lnet_ni_addref_locked(ni, 0);
1326 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1327 if (ni->ni_cpts != NULL) {
1328 lnet_ni_addref_locked(ni, 0);
1329 list_add_tail(&ni->ni_cptlist, &the_lnet.ln_nis_cpt);
1332 lnet_net_unlock(LNET_LOCK_EX);
1334 if (lnd->lnd_type == LOLND) {
1336 LASSERT(the_lnet.ln_loni == NULL);
1337 the_lnet.ln_loni = ni;
1341 if (ni->ni_peertxcredits == 0 || ni->ni_maxtxcredits == 0) {
1342 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1343 libcfs_lnd2str(lnd->lnd_type),
1344 ni->ni_peertxcredits == 0 ?
1346 /* shutdown the NI since if we get here then it must've already
1349 lnet_shutdown_lndni(ni);
1353 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1354 tq->tq_credits_min =
1355 tq->tq_credits_max =
1356 tq->tq_credits = lnet_ni_tq_credits(ni);
1359 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1360 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1361 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1362 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1371 lnet_startup_lndnis(struct list_head *nilist)
1377 while (!list_empty(nilist)) {
1378 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
1379 list_del(&ni->ni_list);
1380 rc = lnet_startup_lndni(ni, -1, -1, -1, -1);
1390 lnet_shutdown_lndnis();
1396 * Initialize LNet library.
1398 * Automatically called at module loading time. Caller has to call
1399 * lnet_fini() after a call to lnet_init(), if and only if the latter
1400 * returned 0. It must be called exactly once.
1402 * \return 0 on success, and -ve on failures.
1409 lnet_assert_wire_constants();
1411 memset(&the_lnet, 0, sizeof(the_lnet));
1413 /* refer to global cfs_cpt_table for now */
1414 the_lnet.ln_cpt_table = cfs_cpt_table;
1415 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1417 LASSERT(the_lnet.ln_cpt_number > 0);
1418 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1419 /* we are under risk of consuming all lh_cookie */
1420 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1421 "please change setting of CPT-table and retry\n",
1422 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1426 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1427 the_lnet.ln_cpt_bits++;
1429 rc = lnet_create_locks();
1431 CERROR("Can't create LNet global locks: %d\n", rc);
1435 the_lnet.ln_refcount = 0;
1436 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1437 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1438 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1439 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1441 /* The hash table size is the number of bits it takes to express the set
1442 * ln_num_routes, minus 1 (better to under estimate than over so we
1443 * don't waste memory). */
1444 if (rnet_htable_size <= 0)
1445 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1446 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1447 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1448 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1449 order_base_2(rnet_htable_size) - 1);
1451 /* All LNDs apart from the LOLND are in separate modules. They
1452 * register themselves when their module loads, and unregister
1453 * themselves when their module is unloaded. */
1454 lnet_register_lnd(&the_lolnd);
1459 * Finalize LNet library.
1461 * \pre lnet_init() called with success.
1462 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1467 LASSERT(the_lnet.ln_refcount == 0);
1469 while (!list_empty(&the_lnet.ln_lnds))
1470 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1472 lnet_destroy_locks();
1476 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1478 * Users must call this function at least once before any other functions.
1479 * For each successful call there must be a corresponding call to
1480 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1483 * The PID used by LNet may be different from the one requested.
1486 * \param requested_pid PID requested by the caller.
1488 * \return >= 0 on success, and < 0 error code on failures.
1491 LNetNIInit(lnet_pid_t requested_pid)
1493 int im_a_router = 0;
1496 lnet_ping_info_t *pinfo;
1497 lnet_handle_md_t md_handle;
1498 struct list_head net_head;
1500 INIT_LIST_HEAD(&net_head);
1502 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1504 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1506 if (the_lnet.ln_refcount > 0) {
1507 rc = the_lnet.ln_refcount++;
1508 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1512 rc = lnet_prepare(requested_pid);
1514 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1518 /* Add in the loopback network */
1519 if (lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, &net_head) == NULL) {
1524 /* If LNet is being initialized via DLC it is possible
1525 * that the user requests not to load module parameters (ones which
1526 * are supported by DLC) on initialization. Therefore, make sure not
1527 * to load networks, routes and forwarding from module parameters
1528 * in this case. On cleanup in case of failure only clean up
1529 * routes if it has been loaded */
1530 if (!the_lnet.ln_nis_from_mod_params) {
1531 rc = lnet_parse_networks(&net_head,
1532 lnet_get_networks());
1537 ni_count = lnet_startup_lndnis(&net_head);
1543 if (!the_lnet.ln_nis_from_mod_params) {
1544 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1548 rc = lnet_check_routes();
1552 rc = lnet_rtrpools_alloc(im_a_router);
1557 rc = lnet_acceptor_start();
1560 the_lnet.ln_refcount = 1;
1561 /* Now I may use my own API functions... */
1563 rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1567 lnet_ping_target_update(pinfo, md_handle);
1569 rc = lnet_router_checker_start();
1576 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1581 lnet_ping_target_fini();
1583 the_lnet.ln_refcount = 0;
1584 lnet_acceptor_stop();
1586 if (!the_lnet.ln_nis_from_mod_params)
1587 lnet_destroy_routes();
1589 lnet_shutdown_lndnis();
1593 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1594 while (!list_empty(&net_head)) {
1596 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1597 list_del_init(&ni->ni_list);
1602 EXPORT_SYMBOL(LNetNIInit);
1605 * Stop LNet interfaces, routing, and forwarding.
1607 * Users must call this function once for each successful call to LNetNIInit().
1608 * Once the LNetNIFini() operation has been started, the results of pending
1609 * API operations are undefined.
1611 * \return always 0 for current implementation.
1616 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1618 LASSERT (the_lnet.ln_refcount > 0);
1620 if (the_lnet.ln_refcount != 1) {
1621 the_lnet.ln_refcount--;
1623 LASSERT(!the_lnet.ln_niinit_self);
1628 lnet_router_checker_stop();
1629 lnet_ping_target_fini();
1631 /* Teardown fns that use my own API functions BEFORE here */
1632 the_lnet.ln_refcount = 0;
1634 lnet_acceptor_stop();
1635 lnet_destroy_routes();
1636 lnet_shutdown_lndnis();
1640 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1643 EXPORT_SYMBOL(LNetNIFini);
1646 * Grabs the ni data from the ni structure and fills the out
1649 * \param[in] ni network interface structure
1650 * \param[out] cpt_count the number of cpts the ni is on
1651 * \param[out] nid Network Interface ID
1652 * \param[out] peer_timeout NI peer timeout
1653 * \param[out] peer_tx_crdits NI peer transmit credits
1654 * \param[out] peer_rtr_credits NI peer router credits
1655 * \param[out] max_tx_credits NI max transmit credit
1656 * \param[out] net_config Network configuration
1659 lnet_fill_ni_info(struct lnet_ni *ni, __u32 *cpt_count, __u64 *nid,
1660 int *peer_timeout, int *peer_tx_credits,
1661 int *peer_rtr_credits, int *max_tx_credits,
1662 struct lnet_ioctl_net_config *net_config)
1669 if (net_config == NULL)
1672 CLASSERT(ARRAY_SIZE(ni->ni_interfaces) ==
1673 ARRAY_SIZE(net_config->ni_interfaces));
1675 if (ni->ni_interfaces[0] != NULL) {
1676 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1677 if (ni->ni_interfaces[i] != NULL) {
1678 strncpy(net_config->ni_interfaces[i],
1679 ni->ni_interfaces[i],
1680 sizeof(net_config->ni_interfaces[i]));
1686 *peer_timeout = ni->ni_peertimeout;
1687 *peer_tx_credits = ni->ni_peertxcredits;
1688 *peer_rtr_credits = ni->ni_peerrtrcredits;
1689 *max_tx_credits = ni->ni_maxtxcredits;
1691 net_config->ni_status = ni->ni_status->ns_status;
1694 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
1695 i < LNET_MAX_SHOW_NUM_CPT;
1697 net_config->ni_cpts[i] = ni->ni_cpts[i];
1699 *cpt_count = ni->ni_ncpts;
1703 lnet_get_net_config(int idx, __u32 *cpt_count, __u64 *nid, int *peer_timeout,
1704 int *peer_tx_credits, int *peer_rtr_credits,
1705 int *max_tx_credits,
1706 struct lnet_ioctl_net_config *net_config)
1709 struct list_head *tmp;
1713 cpt = lnet_net_lock_current();
1715 list_for_each(tmp, &the_lnet.ln_nis) {
1716 ni = list_entry(tmp, lnet_ni_t, ni_list);
1720 lnet_fill_ni_info(ni, cpt_count, nid, peer_timeout,
1721 peer_tx_credits, peer_rtr_credits,
1722 max_tx_credits, net_config);
1728 lnet_net_unlock(cpt);
1733 lnet_dyn_add_ni(lnet_pid_t requested_pid, char *nets,
1734 __s32 peer_timeout, __s32 peer_cr, __s32 peer_buf_cr,
1737 lnet_ping_info_t *pinfo;
1738 lnet_handle_md_t md_handle;
1740 struct list_head net_head;
1742 lnet_remotenet_t *rnet;
1744 INIT_LIST_HEAD(&net_head);
1746 /* Create a ni structure for the network string */
1747 rc = lnet_parse_networks(&net_head, nets);
1749 return rc == 0 ? -EINVAL : rc;
1751 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1754 rc = -EINVAL; /* only add one interface per call */
1758 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1760 lnet_net_lock(LNET_LOCK_EX);
1761 rnet = lnet_find_net_locked(LNET_NIDNET(ni->ni_nid));
1762 lnet_net_unlock(LNET_LOCK_EX);
1763 /* make sure that the net added doesn't invalidate the current
1764 * configuration LNet is keeping */
1766 CERROR("Adding net %s will invalidate routing configuration\n",
1772 rc = lnet_ping_info_setup(&pinfo, &md_handle, 1 + lnet_get_ni_count(),
1777 list_del_init(&ni->ni_list);
1779 rc = lnet_startup_lndni(ni, peer_timeout, peer_cr,
1780 peer_buf_cr, credits);
1784 if (ni->ni_lnd->lnd_accept != NULL) {
1785 rc = lnet_acceptor_start();
1787 /* shutdown the ni that we just started */
1788 CERROR("Failed to start up acceptor thread\n");
1789 lnet_shutdown_lndni(ni);
1794 lnet_ping_target_update(pinfo, md_handle);
1795 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1800 lnet_ping_md_unlink(pinfo, &md_handle);
1801 lnet_ping_info_free(pinfo);
1803 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1804 while (!list_empty(&net_head)) {
1805 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1806 list_del_init(&ni->ni_list);
1813 lnet_dyn_del_ni(__u32 net)
1816 lnet_ping_info_t *pinfo;
1817 lnet_handle_md_t md_handle;
1820 /* don't allow userspace to shutdown the LOLND */
1821 if (LNET_NETTYP(net) == LOLND)
1824 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1825 /* create and link a new ping info, before removing the old one */
1826 rc = lnet_ping_info_setup(&pinfo, &md_handle,
1827 lnet_get_ni_count() - 1, false);
1831 ni = lnet_net2ni(net);
1837 /* decrement the reference counter taken by lnet_net2ni() */
1838 lnet_ni_decref_locked(ni, 0);
1840 lnet_shutdown_lndni(ni);
1842 if (lnet_count_acceptor_nis() == 0)
1843 lnet_acceptor_stop();
1845 lnet_ping_target_update(pinfo, md_handle);
1848 lnet_ping_md_unlink(pinfo, &md_handle);
1849 lnet_ping_info_free(pinfo);
1851 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1857 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1858 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1859 * internal ioctl handler.
1861 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1863 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1864 * The data will be printed to system console. Don't use it excessively.
1865 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1867 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1870 LNetCtl(unsigned int cmd, void *arg)
1872 struct libcfs_ioctl_data *data = arg;
1873 struct lnet_ioctl_config_data *config;
1874 lnet_process_id_t id = {0};
1878 CLASSERT(LIBCFS_IOC_DATA_MAX >= sizeof(struct lnet_ioctl_net_config) +
1879 sizeof(struct lnet_ioctl_config_data));
1882 case IOC_LIBCFS_GET_NI:
1883 rc = LNetGetId(data->ioc_count, &id);
1884 data->ioc_nid = id.nid;
1887 case IOC_LIBCFS_FAIL_NID:
1888 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1890 case IOC_LIBCFS_ADD_ROUTE:
1893 if (config->cfg_hdr.ioc_len < sizeof(*config))
1896 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1897 rc = lnet_add_route(config->cfg_net,
1898 config->cfg_config_u.cfg_route.rtr_hop,
1900 config->cfg_config_u.cfg_route.
1903 rc = lnet_check_routes();
1905 lnet_del_route(config->cfg_net,
1908 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1911 case IOC_LIBCFS_DEL_ROUTE:
1914 if (config->cfg_hdr.ioc_len < sizeof(*config))
1917 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1918 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
1919 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1922 case IOC_LIBCFS_GET_ROUTE:
1925 if (config->cfg_hdr.ioc_len < sizeof(*config))
1928 return lnet_get_route(config->cfg_count,
1930 &config->cfg_config_u.cfg_route.rtr_hop,
1932 &config->cfg_config_u.cfg_route.rtr_flags,
1933 &config->cfg_config_u.cfg_route.
1936 case IOC_LIBCFS_GET_NET: {
1937 struct lnet_ioctl_net_config *net_config;
1938 size_t total = sizeof(*config) + sizeof(*net_config);
1942 if (config->cfg_hdr.ioc_len < total)
1945 net_config = (struct lnet_ioctl_net_config *)
1947 if (config == NULL || net_config == NULL)
1950 return lnet_get_net_config(config->cfg_count,
1953 &config->cfg_config_u.
1954 cfg_net.net_peer_timeout,
1955 &config->cfg_config_u.cfg_net.
1956 net_peer_tx_credits,
1957 &config->cfg_config_u.cfg_net.
1958 net_peer_rtr_credits,
1959 &config->cfg_config_u.cfg_net.
1964 case IOC_LIBCFS_GET_LNET_STATS:
1966 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
1968 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
1971 lnet_counters_get(&lnet_stats->st_cntrs);
1975 case IOC_LIBCFS_CONFIG_RTR:
1978 if (config->cfg_hdr.ioc_len < sizeof(*config))
1981 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1982 if (config->cfg_config_u.cfg_buffers.buf_enable) {
1983 rc = lnet_rtrpools_enable();
1984 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1987 lnet_rtrpools_disable();
1988 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1991 case IOC_LIBCFS_ADD_BUF:
1994 if (config->cfg_hdr.ioc_len < sizeof(*config))
1997 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1998 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
2000 config->cfg_config_u.cfg_buffers.
2002 config->cfg_config_u.cfg_buffers.
2004 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2007 case IOC_LIBCFS_GET_BUF: {
2008 struct lnet_ioctl_pool_cfg *pool_cfg;
2009 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2013 if (config->cfg_hdr.ioc_len < total)
2016 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2017 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2020 case IOC_LIBCFS_GET_PEER_INFO: {
2021 struct lnet_ioctl_peer *peer_info = arg;
2023 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2026 return lnet_get_peer_info(
2027 peer_info->pr_count,
2029 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2030 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2031 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2032 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2033 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2034 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2035 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2036 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2039 case IOC_LIBCFS_NOTIFY_ROUTER:
2040 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2041 cfs_time_current() -
2042 cfs_time_seconds(cfs_time_current_sec() -
2043 (time_t)data->ioc_u64[0]));
2045 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
2046 /* This can be removed once lustre stops calling it */
2049 case IOC_LIBCFS_LNET_DIST:
2050 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2051 if (rc < 0 && rc != -EHOSTUNREACH)
2054 data->ioc_u32[0] = rc;
2057 case IOC_LIBCFS_TESTPROTOCOMPAT:
2058 lnet_net_lock(LNET_LOCK_EX);
2059 the_lnet.ln_testprotocompat = data->ioc_flags;
2060 lnet_net_unlock(LNET_LOCK_EX);
2063 case IOC_LIBCFS_LNET_FAULT:
2064 return lnet_fault_ctl(data->ioc_flags, data);
2066 case IOC_LIBCFS_PING:
2067 id.nid = data->ioc_nid;
2068 id.pid = data->ioc_u32[0];
2069 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
2070 (lnet_process_id_t __user *)data->ioc_pbuf1,
2071 data->ioc_plen1/sizeof(lnet_process_id_t));
2074 data->ioc_count = rc;
2077 case IOC_LIBCFS_DEBUG_PEER: {
2078 /* CAVEAT EMPTOR: this one designed for calling directly; not
2080 id = *((lnet_process_id_t *) arg);
2082 lnet_debug_peer(id.nid);
2084 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2086 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
2088 if (ni->ni_lnd->lnd_ctl == NULL) {
2089 CDEBUG(D_WARNING, "No ctl for %s\n",
2092 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2101 ni = lnet_net2ni(data->ioc_net);
2105 if (ni->ni_lnd->lnd_ctl == NULL)
2108 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2115 EXPORT_SYMBOL(LNetCtl);
2118 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2119 * all interfaces share a same PID, as requested by LNetNIInit().
2121 * \param index Index of the interface to look up.
2122 * \param id On successful return, this location will hold the
2123 * lnet_process_id_t ID of the interface.
2125 * \retval 0 If an interface exists at \a index.
2126 * \retval -ENOENT If no interface has been found.
2129 LNetGetId(unsigned int index, lnet_process_id_t *id)
2132 struct list_head *tmp;
2136 LASSERT(the_lnet.ln_refcount > 0);
2138 cpt = lnet_net_lock_current();
2140 list_for_each(tmp, &the_lnet.ln_nis) {
2144 ni = list_entry(tmp, lnet_ni_t, ni_list);
2146 id->nid = ni->ni_nid;
2147 id->pid = the_lnet.ln_pid;
2152 lnet_net_unlock(cpt);
2155 EXPORT_SYMBOL(LNetGetId);
2158 * Print a string representation of handle \a h into buffer \a str of
2162 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2164 snprintf(str, len, LPX64, h.cookie);
2166 EXPORT_SYMBOL(LNetSnprintHandle);
2169 lnet_ping(lnet_process_id_t id, int timeout_ms, lnet_process_id_t __user *ids,
2172 lnet_handle_eq_t eqh;
2173 lnet_handle_md_t mdh;
2175 lnet_md_t md = { NULL };
2179 const int a_long_time = 60000; /* mS */
2181 lnet_ping_info_t *info;
2182 lnet_process_id_t tmpid;
2189 infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
2192 id.nid == LNET_NID_ANY ||
2193 timeout_ms > 500000 || /* arbitrary limit! */
2194 n_ids > 20) /* arbitrary limit! */
2197 if (id.pid == LNET_PID_ANY)
2198 id.pid = LNET_PID_LUSTRE;
2200 LIBCFS_ALLOC(info, infosz);
2204 /* NB 2 events max (including any unlink event) */
2205 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2207 CERROR("Can't allocate EQ: %d\n", rc);
2211 /* initialize md content */
2214 md.threshold = 2; /*GET/REPLY*/
2216 md.options = LNET_MD_TRUNCATE;
2220 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2222 CERROR("Can't bind MD: %d\n", rc);
2226 rc = LNetGet(LNET_NID_ANY, mdh, id,
2227 LNET_RESERVED_PORTAL,
2228 LNET_PROTO_PING_MATCHBITS, 0);
2231 /* Don't CERROR; this could be deliberate! */
2233 rc2 = LNetMDUnlink(mdh);
2236 /* NB must wait for the UNLINK event below... */
2238 timeout_ms = a_long_time;
2242 /* MUST block for unlink to complete */
2244 blocked = cfs_block_allsigs();
2246 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
2249 cfs_restore_sigs(blocked);
2251 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2252 (rc2 <= 0) ? -1 : event.type,
2253 (rc2 <= 0) ? -1 : event.status,
2254 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2256 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
2258 if (rc2 <= 0 || event.status != 0) {
2259 /* timeout or error */
2260 if (!replied && rc == 0)
2261 rc = (rc2 < 0) ? rc2 :
2262 (rc2 == 0) ? -ETIMEDOUT :
2266 /* Ensure completion in finite time... */
2268 /* No assertion (racing with network) */
2270 timeout_ms = a_long_time;
2271 } else if (rc2 == 0) {
2272 /* timed out waiting for unlink */
2273 CWARN("ping %s: late network completion\n",
2276 } else if (event.type == LNET_EVENT_REPLY) {
2281 } while (rc2 <= 0 || !event.unlinked);
2285 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2292 LASSERT(nob >= 0 && nob <= infosz);
2294 rc = -EPROTO; /* if I can't parse... */
2297 /* can't check magic/version */
2298 CERROR("%s: ping info too short %d\n",
2299 libcfs_id2str(id), nob);
2303 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2304 lnet_swap_pinginfo(info);
2305 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2306 CERROR("%s: Unexpected magic %08x\n",
2307 libcfs_id2str(id), info->pi_magic);
2311 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2312 CERROR("%s: ping w/o NI status: 0x%x\n",
2313 libcfs_id2str(id), info->pi_features);
2317 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2318 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2319 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2323 if (info->pi_nnis < n_ids)
2324 n_ids = info->pi_nnis;
2326 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2327 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2328 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2332 rc = -EFAULT; /* If I SEGV... */
2334 for (i = 0; i < n_ids; i++) {
2335 tmpid.pid = info->pi_pid;
2336 tmpid.nid = info->pi_ni[i].ns_nid;
2337 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2343 rc2 = LNetEQFree(eqh);
2345 CERROR("rc2 %d\n", rc2);
2349 LIBCFS_FREE(info, infosz);