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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LNET
34 #include <linux/log2.h>
35 #include <linux/ktime.h>
37 #include <lnet/lib-lnet.h>
39 #define D_LNI D_CONSOLE
41 lnet_t the_lnet; /* THE state of the network */
42 EXPORT_SYMBOL(the_lnet);
44 static char *ip2nets = "";
45 module_param(ip2nets, charp, 0444);
46 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
48 static char *networks = "";
49 module_param(networks, charp, 0444);
50 MODULE_PARM_DESC(networks, "local networks");
52 static char *routes = "";
53 module_param(routes, charp, 0444);
54 MODULE_PARM_DESC(routes, "routes to non-local networks");
56 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
57 module_param(rnet_htable_size, int, 0444);
58 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
60 static int use_tcp_bonding = false;
61 module_param(use_tcp_bonding, int, 0444);
62 MODULE_PARM_DESC(use_tcp_bonding,
63 "Set to 1 to use socklnd bonding. 0 to use Multi-Rail");
66 * This sequence number keeps track of how many times DLC was used to
67 * update the configuration. It is incremented on any DLC update and
68 * checked when sending a message to determine if there is a need to
69 * re-run the selection algorithm to handle configuration change.
70 * Look at lnet_select_pathway() for more details on its usage.
72 static atomic_t lnet_dlc_seq_no = ATOMIC_INIT(0);
74 static int lnet_ping(lnet_process_id_t id, signed long timeout,
75 lnet_process_id_t __user *ids, int n_ids);
84 lnet_get_networks(void)
89 if (*networks != 0 && *ip2nets != 0) {
90 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
91 "'ip2nets' but not both at once\n");
96 rc = lnet_parse_ip2nets(&nets, ip2nets);
97 return (rc == 0) ? nets : NULL;
107 lnet_init_locks(void)
109 spin_lock_init(&the_lnet.ln_eq_wait_lock);
110 init_waitqueue_head(&the_lnet.ln_eq_waitq);
111 init_waitqueue_head(&the_lnet.ln_rc_waitq);
112 mutex_init(&the_lnet.ln_lnd_mutex);
113 mutex_init(&the_lnet.ln_api_mutex);
117 lnet_fini_locks(void)
121 struct kmem_cache *lnet_mes_cachep; /* MEs kmem_cache */
122 struct kmem_cache *lnet_small_mds_cachep; /* <= LNET_SMALL_MD_SIZE bytes
126 lnet_descriptor_setup(void)
128 /* create specific kmem_cache for MEs and small MDs (i.e., originally
129 * allocated in <size-xxx> kmem_cache).
131 lnet_mes_cachep = kmem_cache_create("lnet_MEs", sizeof(lnet_me_t),
133 if (!lnet_mes_cachep)
136 lnet_small_mds_cachep = kmem_cache_create("lnet_small_MDs",
137 LNET_SMALL_MD_SIZE, 0, 0,
139 if (!lnet_small_mds_cachep)
146 lnet_descriptor_cleanup(void)
149 if (lnet_small_mds_cachep) {
150 kmem_cache_destroy(lnet_small_mds_cachep);
151 lnet_small_mds_cachep = NULL;
154 if (lnet_mes_cachep) {
155 kmem_cache_destroy(lnet_mes_cachep);
156 lnet_mes_cachep = NULL;
161 lnet_create_remote_nets_table(void)
164 struct list_head *hash;
166 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
167 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
168 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
170 CERROR("Failed to create remote nets hash table\n");
174 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
175 INIT_LIST_HEAD(&hash[i]);
176 the_lnet.ln_remote_nets_hash = hash;
181 lnet_destroy_remote_nets_table(void)
185 if (the_lnet.ln_remote_nets_hash == NULL)
188 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
189 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
191 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
192 LNET_REMOTE_NETS_HASH_SIZE *
193 sizeof(the_lnet.ln_remote_nets_hash[0]));
194 the_lnet.ln_remote_nets_hash = NULL;
198 lnet_destroy_locks(void)
200 if (the_lnet.ln_res_lock != NULL) {
201 cfs_percpt_lock_free(the_lnet.ln_res_lock);
202 the_lnet.ln_res_lock = NULL;
205 if (the_lnet.ln_net_lock != NULL) {
206 cfs_percpt_lock_free(the_lnet.ln_net_lock);
207 the_lnet.ln_net_lock = NULL;
214 lnet_create_locks(void)
218 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
219 if (the_lnet.ln_res_lock == NULL)
222 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
223 if (the_lnet.ln_net_lock == NULL)
229 lnet_destroy_locks();
233 static void lnet_assert_wire_constants(void)
235 /* Wire protocol assertions generated by 'wirecheck'
236 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
237 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
238 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
241 CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
242 CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
243 CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
244 CLASSERT(LNET_MSG_ACK == 0);
245 CLASSERT(LNET_MSG_PUT == 1);
246 CLASSERT(LNET_MSG_GET == 2);
247 CLASSERT(LNET_MSG_REPLY == 3);
248 CLASSERT(LNET_MSG_HELLO == 4);
250 /* Checks for struct lnet_handle_wire */
251 CLASSERT((int)sizeof(struct lnet_handle_wire) == 16);
252 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_interface_cookie) == 0);
253 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_interface_cookie) == 8);
254 CLASSERT((int)offsetof(struct lnet_handle_wire, wh_object_cookie) == 8);
255 CLASSERT((int)sizeof(((struct lnet_handle_wire *)0)->wh_object_cookie) == 8);
257 /* Checks for struct lnet_magicversion_t */
258 CLASSERT((int)sizeof(lnet_magicversion_t) == 8);
259 CLASSERT((int)offsetof(lnet_magicversion_t, magic) == 0);
260 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
261 CLASSERT((int)offsetof(lnet_magicversion_t, version_major) == 4);
262 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
263 CLASSERT((int)offsetof(lnet_magicversion_t, version_minor) == 6);
264 CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
266 /* Checks for struct lnet_hdr_t */
267 CLASSERT((int)sizeof(lnet_hdr_t) == 72);
268 CLASSERT((int)offsetof(lnet_hdr_t, dest_nid) == 0);
269 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
270 CLASSERT((int)offsetof(lnet_hdr_t, src_nid) == 8);
271 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
272 CLASSERT((int)offsetof(lnet_hdr_t, dest_pid) == 16);
273 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
274 CLASSERT((int)offsetof(lnet_hdr_t, src_pid) == 20);
275 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
276 CLASSERT((int)offsetof(lnet_hdr_t, type) == 24);
277 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
278 CLASSERT((int)offsetof(lnet_hdr_t, payload_length) == 28);
279 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
280 CLASSERT((int)offsetof(lnet_hdr_t, msg) == 32);
281 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
284 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
285 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
286 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
287 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
288 CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
289 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
292 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
293 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
294 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
295 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
296 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
297 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
298 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
299 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
300 CLASSERT((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
301 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
304 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
305 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
306 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
307 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
308 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
309 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
310 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
311 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
312 CLASSERT((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
313 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
316 CLASSERT((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
317 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
320 CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
321 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
322 CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
323 CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
326 static lnd_t *lnet_find_lnd_by_type(__u32 type)
329 struct list_head *tmp;
331 /* holding lnd mutex */
332 list_for_each(tmp, &the_lnet.ln_lnds) {
333 lnd = list_entry(tmp, lnd_t, lnd_list);
335 if (lnd->lnd_type == type)
342 lnet_register_lnd (lnd_t *lnd)
344 mutex_lock(&the_lnet.ln_lnd_mutex);
346 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
347 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
349 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
350 lnd->lnd_refcount = 0;
352 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
354 mutex_unlock(&the_lnet.ln_lnd_mutex);
356 EXPORT_SYMBOL(lnet_register_lnd);
359 lnet_unregister_lnd (lnd_t *lnd)
361 mutex_lock(&the_lnet.ln_lnd_mutex);
363 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
364 LASSERT(lnd->lnd_refcount == 0);
366 list_del(&lnd->lnd_list);
367 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
369 mutex_unlock(&the_lnet.ln_lnd_mutex);
371 EXPORT_SYMBOL(lnet_unregister_lnd);
374 lnet_counters_get(lnet_counters_t *counters)
376 lnet_counters_t *ctr;
379 memset(counters, 0, sizeof(*counters));
381 lnet_net_lock(LNET_LOCK_EX);
383 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
384 counters->msgs_max += ctr->msgs_max;
385 counters->msgs_alloc += ctr->msgs_alloc;
386 counters->errors += ctr->errors;
387 counters->send_count += ctr->send_count;
388 counters->recv_count += ctr->recv_count;
389 counters->route_count += ctr->route_count;
390 counters->drop_count += ctr->drop_count;
391 counters->send_length += ctr->send_length;
392 counters->recv_length += ctr->recv_length;
393 counters->route_length += ctr->route_length;
394 counters->drop_length += ctr->drop_length;
397 lnet_net_unlock(LNET_LOCK_EX);
399 EXPORT_SYMBOL(lnet_counters_get);
402 lnet_counters_reset(void)
404 lnet_counters_t *counters;
407 lnet_net_lock(LNET_LOCK_EX);
409 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
410 memset(counters, 0, sizeof(lnet_counters_t));
412 lnet_net_unlock(LNET_LOCK_EX);
416 lnet_res_type2str(int type)
421 case LNET_COOKIE_TYPE_MD:
423 case LNET_COOKIE_TYPE_ME:
425 case LNET_COOKIE_TYPE_EQ:
431 lnet_res_container_cleanup(struct lnet_res_container *rec)
435 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
438 while (!list_empty(&rec->rec_active)) {
439 struct list_head *e = rec->rec_active.next;
442 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
443 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
445 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
446 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
448 } else { /* NB: Active MEs should be attached on portals */
455 /* Found alive MD/ME/EQ, user really should unlink/free
456 * all of them before finalize LNet, but if someone didn't,
457 * we have to recycle garbage for him */
458 CERROR("%d active elements on exit of %s container\n",
459 count, lnet_res_type2str(rec->rec_type));
462 if (rec->rec_lh_hash != NULL) {
463 LIBCFS_FREE(rec->rec_lh_hash,
464 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
465 rec->rec_lh_hash = NULL;
468 rec->rec_type = 0; /* mark it as finalized */
472 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
477 LASSERT(rec->rec_type == 0);
479 rec->rec_type = type;
480 INIT_LIST_HEAD(&rec->rec_active);
482 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
484 /* Arbitrary choice of hash table size */
485 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
486 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
487 if (rec->rec_lh_hash == NULL) {
492 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
493 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
498 CERROR("Failed to setup %s resource container\n",
499 lnet_res_type2str(type));
500 lnet_res_container_cleanup(rec);
505 lnet_res_containers_destroy(struct lnet_res_container **recs)
507 struct lnet_res_container *rec;
510 cfs_percpt_for_each(rec, i, recs)
511 lnet_res_container_cleanup(rec);
513 cfs_percpt_free(recs);
516 static struct lnet_res_container **
517 lnet_res_containers_create(int type)
519 struct lnet_res_container **recs;
520 struct lnet_res_container *rec;
524 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
526 CERROR("Failed to allocate %s resource containers\n",
527 lnet_res_type2str(type));
531 cfs_percpt_for_each(rec, i, recs) {
532 rc = lnet_res_container_setup(rec, i, type);
534 lnet_res_containers_destroy(recs);
543 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
545 /* ALWAYS called with lnet_res_lock held */
546 struct list_head *head;
547 lnet_libhandle_t *lh;
550 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
553 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
554 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
556 list_for_each_entry(lh, head, lh_hash_chain) {
557 if (lh->lh_cookie == cookie)
565 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
567 /* ALWAYS called with lnet_res_lock held */
568 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
571 lh->lh_cookie = rec->rec_lh_cookie;
572 rec->rec_lh_cookie += 1 << ibits;
574 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
576 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
579 static int lnet_unprepare(void);
582 lnet_prepare(lnet_pid_t requested_pid)
584 /* Prepare to bring up the network */
585 struct lnet_res_container **recs;
588 if (requested_pid == LNET_PID_ANY) {
589 /* Don't instantiate LNET just for me */
593 LASSERT(the_lnet.ln_refcount == 0);
595 the_lnet.ln_routing = 0;
597 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
598 the_lnet.ln_pid = requested_pid;
600 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
601 INIT_LIST_HEAD(&the_lnet.ln_peers);
602 INIT_LIST_HEAD(&the_lnet.ln_remote_peer_ni_list);
603 INIT_LIST_HEAD(&the_lnet.ln_nets);
604 INIT_LIST_HEAD(&the_lnet.ln_routers);
605 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
606 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
608 rc = lnet_descriptor_setup();
612 rc = lnet_create_remote_nets_table();
617 * NB the interface cookie in wire handles guards against delayed
618 * replies and ACKs appearing valid after reboot.
620 the_lnet.ln_interface_cookie = ktime_get_real_ns();
622 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
623 sizeof(lnet_counters_t));
624 if (the_lnet.ln_counters == NULL) {
625 CERROR("Failed to allocate counters for LNet\n");
630 rc = lnet_peer_tables_create();
634 rc = lnet_msg_containers_create();
638 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
639 LNET_COOKIE_TYPE_EQ);
643 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
649 the_lnet.ln_me_containers = recs;
651 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
657 the_lnet.ln_md_containers = recs;
659 rc = lnet_portals_create();
661 CERROR("Failed to create portals for LNet: %d\n", rc);
673 lnet_unprepare (void)
675 /* NB no LNET_LOCK since this is the last reference. All LND instances
676 * have shut down already, so it is safe to unlink and free all
677 * descriptors, even those that appear committed to a network op (eg MD
678 * with non-zero pending count) */
680 lnet_fail_nid(LNET_NID_ANY, 0);
682 LASSERT(the_lnet.ln_refcount == 0);
683 LASSERT(list_empty(&the_lnet.ln_test_peers));
684 LASSERT(list_empty(&the_lnet.ln_nets));
686 lnet_portals_destroy();
688 if (the_lnet.ln_md_containers != NULL) {
689 lnet_res_containers_destroy(the_lnet.ln_md_containers);
690 the_lnet.ln_md_containers = NULL;
693 if (the_lnet.ln_me_containers != NULL) {
694 lnet_res_containers_destroy(the_lnet.ln_me_containers);
695 the_lnet.ln_me_containers = NULL;
698 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
700 lnet_msg_containers_destroy();
702 lnet_rtrpools_free(0);
704 if (the_lnet.ln_counters != NULL) {
705 cfs_percpt_free(the_lnet.ln_counters);
706 the_lnet.ln_counters = NULL;
708 lnet_destroy_remote_nets_table();
709 lnet_descriptor_cleanup();
715 lnet_net2ni_locked(__u32 net_id, int cpt)
718 struct lnet_net *net;
720 LASSERT(cpt != LNET_LOCK_EX);
722 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
723 if (net->net_id == net_id) {
724 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
734 lnet_net2ni(__u32 net)
739 ni = lnet_net2ni_locked(net, 0);
744 EXPORT_SYMBOL(lnet_net2ni);
747 lnet_get_net_locked(__u32 net_id)
749 struct lnet_net *net;
751 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
752 if (net->net_id == net_id)
760 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
765 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
770 val = hash_long(key, LNET_CPT_BITS);
771 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
775 return (unsigned int)(key + val + (val >> 1)) % number;
779 lnet_cpt_of_nid_locked(lnet_nid_t nid, struct lnet_ni *ni)
781 struct lnet_net *net;
783 /* must called with hold of lnet_net_lock */
784 if (LNET_CPT_NUMBER == 1)
785 return 0; /* the only one */
788 * If NI is provided then use the CPT identified in the NI cpt
789 * list if one exists. If one doesn't exist, then that NI is
790 * associated with all CPTs and it follows that the net it belongs
791 * to is implicitly associated with all CPTs, so just hash the nid
795 if (ni->ni_cpts != NULL)
796 return ni->ni_cpts[lnet_nid_cpt_hash(nid,
799 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
802 /* no NI provided so look at the net */
803 net = lnet_get_net_locked(LNET_NIDNET(nid));
805 if (net != NULL && net->net_cpts != NULL) {
806 return net->net_cpts[lnet_nid_cpt_hash(nid, net->net_ncpts)];
809 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
813 lnet_cpt_of_nid(lnet_nid_t nid, struct lnet_ni *ni)
818 if (LNET_CPT_NUMBER == 1)
819 return 0; /* the only one */
821 cpt = lnet_net_lock_current();
823 cpt2 = lnet_cpt_of_nid_locked(nid, ni);
825 lnet_net_unlock(cpt);
829 EXPORT_SYMBOL(lnet_cpt_of_nid);
832 lnet_islocalnet(__u32 net_id)
834 struct lnet_net *net;
838 cpt = lnet_net_lock_current();
840 net = lnet_get_net_locked(net_id);
844 lnet_net_unlock(cpt);
850 lnet_is_ni_healthy_locked(struct lnet_ni *ni)
852 if (ni->ni_state == LNET_NI_STATE_ACTIVE ||
853 ni->ni_state == LNET_NI_STATE_DEGRADED)
860 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
862 struct lnet_net *net;
865 LASSERT(cpt != LNET_LOCK_EX);
867 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
868 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
869 if (ni->ni_nid == nid)
878 lnet_nid2ni_addref(lnet_nid_t nid)
883 ni = lnet_nid2ni_locked(nid, 0);
885 lnet_ni_addref_locked(ni, 0);
890 EXPORT_SYMBOL(lnet_nid2ni_addref);
893 lnet_islocalnid(lnet_nid_t nid)
898 cpt = lnet_net_lock_current();
899 ni = lnet_nid2ni_locked(nid, cpt);
900 lnet_net_unlock(cpt);
906 lnet_count_acceptor_nets(void)
908 /* Return the # of NIs that need the acceptor. */
910 struct lnet_net *net;
913 cpt = lnet_net_lock_current();
914 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
915 /* all socklnd type networks should have the acceptor
917 if (net->net_lnd->lnd_accept != NULL)
921 lnet_net_unlock(cpt);
926 static struct lnet_ping_info *
927 lnet_ping_info_create(int num_ni)
929 struct lnet_ping_info *ping_info;
932 infosz = offsetof(struct lnet_ping_info, pi_ni[num_ni]);
933 LIBCFS_ALLOC(ping_info, infosz);
934 if (ping_info == NULL) {
935 CERROR("Can't allocate ping info[%d]\n", num_ni);
939 ping_info->pi_nnis = num_ni;
940 ping_info->pi_pid = the_lnet.ln_pid;
941 ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
942 ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
948 lnet_get_net_ni_count_locked(struct lnet_net *net)
953 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
960 lnet_get_ni_count(void)
963 struct lnet_net *net;
968 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
969 list_for_each_entry(ni, &net->net_ni_list, ni_netlist)
979 lnet_ping_info_free(struct lnet_ping_info *pinfo)
982 offsetof(struct lnet_ping_info,
983 pi_ni[pinfo->pi_nnis]));
987 lnet_ping_info_destroy(void)
989 struct lnet_net *net;
992 lnet_net_lock(LNET_LOCK_EX);
994 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
995 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
997 ni->ni_status = NULL;
1002 lnet_ping_info_free(the_lnet.ln_ping_info);
1003 the_lnet.ln_ping_info = NULL;
1005 lnet_net_unlock(LNET_LOCK_EX);
1009 lnet_ping_event_handler(lnet_event_t *event)
1011 struct lnet_ping_info *pinfo = event->md.user_ptr;
1013 if (event->unlinked)
1014 pinfo->pi_features = LNET_PING_FEAT_INVAL;
1018 lnet_ping_info_setup(struct lnet_ping_info **ppinfo, lnet_handle_md_t *md_handle,
1019 int ni_count, bool set_eq)
1021 lnet_handle_me_t me_handle;
1022 lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
1023 lnet_md_t md = {NULL};
1027 rc = LNetEQAlloc(0, lnet_ping_event_handler,
1028 &the_lnet.ln_ping_target_eq);
1030 CERROR("Can't allocate ping EQ: %d\n", rc);
1035 *ppinfo = lnet_ping_info_create(ni_count);
1036 if (*ppinfo == NULL) {
1041 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
1042 LNET_PROTO_PING_MATCHBITS, 0,
1043 LNET_UNLINK, LNET_INS_AFTER,
1046 CERROR("Can't create ping ME: %d\n", rc);
1050 /* initialize md content */
1052 md.length = offsetof(struct lnet_ping_info,
1053 pi_ni[(*ppinfo)->pi_nnis]);
1054 md.threshold = LNET_MD_THRESH_INF;
1056 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
1057 LNET_MD_MANAGE_REMOTE;
1059 md.eq_handle = the_lnet.ln_ping_target_eq;
1060 md.user_ptr = *ppinfo;
1062 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
1064 CERROR("Can't attach ping MD: %d\n", rc);
1071 rc2 = LNetMEUnlink(me_handle);
1074 lnet_ping_info_free(*ppinfo);
1078 LNetEQFree(the_lnet.ln_ping_target_eq);
1083 lnet_ping_md_unlink(struct lnet_ping_info *pinfo, lnet_handle_md_t *md_handle)
1085 sigset_t blocked = cfs_block_allsigs();
1087 LNetMDUnlink(*md_handle);
1088 LNetInvalidateHandle(md_handle);
1090 /* NB md could be busy; this just starts the unlink */
1091 while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
1092 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
1093 set_current_state(TASK_UNINTERRUPTIBLE);
1094 schedule_timeout(cfs_time_seconds(1));
1097 cfs_restore_sigs(blocked);
1101 lnet_ping_info_install_locked(struct lnet_ping_info *ping_info)
1105 struct lnet_net *net;
1106 struct lnet_ni_status *ns;
1109 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
1110 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
1111 LASSERT(i < ping_info->pi_nnis);
1113 ns = &ping_info->pi_ni[i];
1115 ns->ns_nid = ni->ni_nid;
1118 ns->ns_status = (ni->ni_status != NULL) ?
1119 ni->ni_status->ns_status :
1131 lnet_ping_target_update(struct lnet_ping_info *pinfo, lnet_handle_md_t md_handle)
1133 struct lnet_ping_info *old_pinfo = NULL;
1134 lnet_handle_md_t old_md;
1136 /* switch the NIs to point to the new ping info created */
1137 lnet_net_lock(LNET_LOCK_EX);
1139 if (!the_lnet.ln_routing)
1140 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1141 lnet_ping_info_install_locked(pinfo);
1143 if (the_lnet.ln_ping_info != NULL) {
1144 old_pinfo = the_lnet.ln_ping_info;
1145 old_md = the_lnet.ln_ping_target_md;
1147 the_lnet.ln_ping_target_md = md_handle;
1148 the_lnet.ln_ping_info = pinfo;
1150 lnet_net_unlock(LNET_LOCK_EX);
1152 if (old_pinfo != NULL) {
1153 /* unlink the old ping info */
1154 lnet_ping_md_unlink(old_pinfo, &old_md);
1155 lnet_ping_info_free(old_pinfo);
1160 lnet_ping_target_fini(void)
1164 lnet_ping_md_unlink(the_lnet.ln_ping_info,
1165 &the_lnet.ln_ping_target_md);
1167 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1170 lnet_ping_info_destroy();
1174 lnet_ni_tq_credits(lnet_ni_t *ni)
1178 LASSERT(ni->ni_ncpts >= 1);
1180 if (ni->ni_ncpts == 1)
1181 return ni->ni_net->net_tunables.lct_max_tx_credits;
1183 credits = ni->ni_net->net_tunables.lct_max_tx_credits / ni->ni_ncpts;
1184 credits = max(credits, 8 * ni->ni_net->net_tunables.lct_peer_tx_credits);
1185 credits = min(credits, ni->ni_net->net_tunables.lct_max_tx_credits);
1191 lnet_ni_unlink_locked(lnet_ni_t *ni)
1193 if (!list_empty(&ni->ni_cptlist)) {
1194 list_del_init(&ni->ni_cptlist);
1195 lnet_ni_decref_locked(ni, 0);
1198 /* move it to zombie list and nobody can find it anymore */
1199 LASSERT(!list_empty(&ni->ni_netlist));
1200 list_move(&ni->ni_netlist, &ni->ni_net->net_ni_zombie);
1201 lnet_ni_decref_locked(ni, 0);
1205 lnet_clear_zombies_nis_locked(struct lnet_net *net)
1210 struct list_head *zombie_list = &net->net_ni_zombie;
1213 * Now wait for the NIs I just nuked to show up on the zombie
1214 * list and shut them down in guaranteed thread context
1217 while (!list_empty(zombie_list)) {
1221 ni = list_entry(zombie_list->next,
1222 lnet_ni_t, ni_netlist);
1223 list_del_init(&ni->ni_netlist);
1224 /* the ni should be in deleting state. If it's not it's
1226 LASSERT(ni->ni_state == LNET_NI_STATE_DELETING);
1227 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1230 /* still busy, add it back to zombie list */
1231 list_add(&ni->ni_netlist, zombie_list);
1235 if (!list_empty(&ni->ni_netlist)) {
1236 lnet_net_unlock(LNET_LOCK_EX);
1238 if ((i & (-i)) == i) {
1240 "Waiting for zombie LNI %s\n",
1241 libcfs_nid2str(ni->ni_nid));
1243 set_current_state(TASK_UNINTERRUPTIBLE);
1244 schedule_timeout(cfs_time_seconds(1));
1245 lnet_net_lock(LNET_LOCK_EX);
1249 lnet_net_unlock(LNET_LOCK_EX);
1251 islo = ni->ni_net->net_lnd->lnd_type == LOLND;
1253 LASSERT(!in_interrupt());
1254 (net->net_lnd->lnd_shutdown)(ni);
1257 CDEBUG(D_LNI, "Removed LNI %s\n",
1258 libcfs_nid2str(ni->ni_nid));
1262 lnet_net_lock(LNET_LOCK_EX);
1266 /* shutdown down the NI and release refcount */
1268 lnet_shutdown_lndni(struct lnet_ni *ni)
1271 struct lnet_net *net = ni->ni_net;
1273 lnet_net_lock(LNET_LOCK_EX);
1274 ni->ni_state = LNET_NI_STATE_DELETING;
1275 lnet_ni_unlink_locked(ni);
1276 lnet_net_unlock(LNET_LOCK_EX);
1278 /* clear messages for this NI on the lazy portal */
1279 for (i = 0; i < the_lnet.ln_nportals; i++)
1280 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1282 /* Do peer table cleanup for this ni */
1283 lnet_peer_tables_cleanup(ni);
1285 lnet_net_lock(LNET_LOCK_EX);
1286 lnet_clear_zombies_nis_locked(net);
1287 lnet_net_unlock(LNET_LOCK_EX);
1291 lnet_shutdown_lndnet(struct lnet_net *net)
1295 lnet_net_lock(LNET_LOCK_EX);
1297 net->net_state = LNET_NET_STATE_DELETING;
1299 list_del_init(&net->net_list);
1301 while (!list_empty(&net->net_ni_list)) {
1302 ni = list_entry(net->net_ni_list.next,
1303 lnet_ni_t, ni_netlist);
1304 lnet_net_unlock(LNET_LOCK_EX);
1305 lnet_shutdown_lndni(ni);
1306 lnet_net_lock(LNET_LOCK_EX);
1310 * decrement ref count on lnd only when the entire network goes
1313 net->net_lnd->lnd_refcount--;
1315 lnet_net_unlock(LNET_LOCK_EX);
1321 lnet_shutdown_lndnets(void)
1323 struct lnet_net *net;
1325 /* NB called holding the global mutex */
1327 /* All quiet on the API front */
1328 LASSERT(!the_lnet.ln_shutdown);
1329 LASSERT(the_lnet.ln_refcount == 0);
1331 lnet_net_lock(LNET_LOCK_EX);
1332 the_lnet.ln_shutdown = 1; /* flag shutdown */
1334 while (!list_empty(&the_lnet.ln_nets)) {
1336 * move the nets to the zombie list to avoid them being
1337 * picked up for new work. LONET is also included in the
1338 * Nets that will be moved to the zombie list
1340 net = list_entry(the_lnet.ln_nets.next,
1341 struct lnet_net, net_list);
1342 list_move(&net->net_list, &the_lnet.ln_net_zombie);
1345 /* Drop the cached loopback Net. */
1346 if (the_lnet.ln_loni != NULL) {
1347 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1348 the_lnet.ln_loni = NULL;
1350 lnet_net_unlock(LNET_LOCK_EX);
1352 /* iterate through the net zombie list and delete each net */
1353 while (!list_empty(&the_lnet.ln_net_zombie)) {
1354 net = list_entry(the_lnet.ln_net_zombie.next,
1355 struct lnet_net, net_list);
1356 lnet_shutdown_lndnet(net);
1359 lnet_net_lock(LNET_LOCK_EX);
1360 the_lnet.ln_shutdown = 0;
1361 lnet_net_unlock(LNET_LOCK_EX);
1365 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_lnd_tunables *tun)
1368 struct lnet_tx_queue *tq;
1370 struct lnet_net *net = ni->ni_net;
1372 mutex_lock(&the_lnet.ln_lnd_mutex);
1375 memcpy(&ni->ni_lnd_tunables, tun, sizeof(*tun));
1376 ni->ni_lnd_tunables_set = true;
1379 rc = (net->net_lnd->lnd_startup)(ni);
1381 mutex_unlock(&the_lnet.ln_lnd_mutex);
1384 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1385 rc, libcfs_lnd2str(net->net_lnd->lnd_type));
1386 lnet_net_lock(LNET_LOCK_EX);
1387 net->net_lnd->lnd_refcount--;
1388 lnet_net_unlock(LNET_LOCK_EX);
1392 ni->ni_state = LNET_NI_STATE_ACTIVE;
1394 /* We keep a reference on the loopback net through the loopback NI */
1395 if (net->net_lnd->lnd_type == LOLND) {
1397 LASSERT(the_lnet.ln_loni == NULL);
1398 the_lnet.ln_loni = ni;
1399 ni->ni_net->net_tunables.lct_peer_tx_credits = 0;
1400 ni->ni_net->net_tunables.lct_peer_rtr_credits = 0;
1401 ni->ni_net->net_tunables.lct_max_tx_credits = 0;
1402 ni->ni_net->net_tunables.lct_peer_timeout = 0;
1406 if (ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ||
1407 ni->ni_net->net_tunables.lct_max_tx_credits == 0) {
1408 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1409 libcfs_lnd2str(net->net_lnd->lnd_type),
1410 ni->ni_net->net_tunables.lct_peer_tx_credits == 0 ?
1412 /* shutdown the NI since if we get here then it must've already
1415 lnet_shutdown_lndni(ni);
1419 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1420 tq->tq_credits_min =
1421 tq->tq_credits_max =
1422 tq->tq_credits = lnet_ni_tq_credits(ni);
1425 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1426 libcfs_nid2str(ni->ni_nid),
1427 ni->ni_net->net_tunables.lct_peer_tx_credits,
1428 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1429 ni->ni_net->net_tunables.lct_peer_rtr_credits,
1430 ni->ni_net->net_tunables.lct_peer_timeout);
1439 lnet_startup_lndnet(struct lnet_net *net, struct lnet_lnd_tunables *tun)
1442 struct lnet_net *net_l = NULL;
1443 struct list_head local_ni_list;
1449 net->net_tunables.lct_peer_timeout;
1451 net->net_tunables.lct_max_tx_credits;
1452 int peerrtrcredits =
1453 net->net_tunables.lct_peer_rtr_credits;
1455 INIT_LIST_HEAD(&local_ni_list);
1458 * make sure that this net is unique. If it isn't then
1459 * we are adding interfaces to an already existing network, and
1460 * 'net' is just a convenient way to pass in the list.
1461 * if it is unique we need to find the LND and load it if
1464 if (lnet_net_unique(net->net_id, &the_lnet.ln_nets, &net_l)) {
1465 lnd_type = LNET_NETTYP(net->net_id);
1467 LASSERT(libcfs_isknown_lnd(lnd_type));
1469 if (lnd_type == CIBLND || lnd_type == OPENIBLND ||
1470 lnd_type == IIBLND || lnd_type == VIBLND) {
1471 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type));
1476 mutex_lock(&the_lnet.ln_lnd_mutex);
1477 lnd = lnet_find_lnd_by_type(lnd_type);
1480 mutex_unlock(&the_lnet.ln_lnd_mutex);
1481 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1482 mutex_lock(&the_lnet.ln_lnd_mutex);
1484 lnd = lnet_find_lnd_by_type(lnd_type);
1486 mutex_unlock(&the_lnet.ln_lnd_mutex);
1487 CERROR("Can't load LND %s, module %s, rc=%d\n",
1488 libcfs_lnd2str(lnd_type),
1489 libcfs_lnd2modname(lnd_type), rc);
1490 #ifndef HAVE_MODULE_LOADING_SUPPORT
1491 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1492 "compiled with kernel module "
1493 "loading support.");
1500 lnet_net_lock(LNET_LOCK_EX);
1501 lnd->lnd_refcount++;
1502 lnet_net_unlock(LNET_LOCK_EX);
1506 mutex_unlock(&the_lnet.ln_lnd_mutex);
1512 * net_l: if the network being added is unique then net_l
1513 * will point to that network
1514 * if the network being added is not unique then
1515 * net_l points to the existing network.
1517 * When we enter the loop below, we'll pick NIs off he
1518 * network beign added and start them up, then add them to
1519 * a local ni list. Once we've successfully started all
1520 * the NIs then we join the local NI list (of started up
1521 * networks) with the net_l->net_ni_list, which should
1522 * point to the correct network to add the new ni list to
1524 * If any of the new NIs fail to start up, then we want to
1525 * iterate through the local ni list, which should include
1526 * any NIs which were successfully started up, and shut
1529 * After than we want to delete the network being added,
1530 * to avoid a memory leak.
1534 * When a network uses TCP bonding then all its interfaces
1535 * must be specified when the network is first defined: the
1536 * TCP bonding code doesn't allow for interfaces to be added
1539 if (net_l != net && net_l != NULL && use_tcp_bonding &&
1540 LNET_NETTYP(net_l->net_id) == SOCKLND) {
1545 while (!list_empty(&net->net_ni_added)) {
1546 ni = list_entry(net->net_ni_added.next, struct lnet_ni,
1548 list_del_init(&ni->ni_netlist);
1550 /* make sure that the the NI we're about to start
1551 * up is actually unique. if it's not fail. */
1552 if (!lnet_ni_unique_net(&net_l->net_ni_list,
1553 ni->ni_interfaces[0])) {
1558 /* adjust the pointer the parent network, just in case it
1559 * the net is a duplicate */
1562 rc = lnet_startup_lndni(ni, tun);
1564 LASSERT(ni->ni_net->net_tunables.lct_peer_timeout <= 0 ||
1565 ni->ni_net->net_lnd->lnd_query != NULL);
1571 list_add_tail(&ni->ni_netlist, &local_ni_list);
1576 lnet_net_lock(LNET_LOCK_EX);
1577 list_splice_tail(&local_ni_list, &net_l->net_ni_list);
1578 lnet_incr_dlc_seq();
1579 lnet_net_unlock(LNET_LOCK_EX);
1581 /* if the network is not unique then we don't want to keep
1582 * it around after we're done. Free it. Otherwise add that
1583 * net to the global the_lnet.ln_nets */
1584 if (net_l != net && net_l != NULL) {
1586 * TODO - note. currently the tunables can not be updated
1591 net->net_state = LNET_NET_STATE_ACTIVE;
1593 * restore tunables after it has been overwitten by the
1596 if (peer_timeout != -1)
1597 net->net_tunables.lct_peer_timeout = peer_timeout;
1598 if (maxtxcredits != -1)
1599 net->net_tunables.lct_max_tx_credits = maxtxcredits;
1600 if (peerrtrcredits != -1)
1601 net->net_tunables.lct_peer_rtr_credits = peerrtrcredits;
1603 lnet_net_lock(LNET_LOCK_EX);
1604 list_add_tail(&net->net_list, &the_lnet.ln_nets);
1605 lnet_net_unlock(LNET_LOCK_EX);
1612 * shutdown the new NIs that are being started up
1613 * free the NET being started
1615 while (!list_empty(&local_ni_list)) {
1616 ni = list_entry(local_ni_list.next, struct lnet_ni,
1619 lnet_shutdown_lndni(ni);
1629 lnet_startup_lndnets(struct list_head *netlist)
1631 struct lnet_net *net;
1635 while (!list_empty(netlist)) {
1636 net = list_entry(netlist->next, struct lnet_net, net_list);
1637 list_del_init(&net->net_list);
1639 rc = lnet_startup_lndnet(net, NULL);
1649 lnet_shutdown_lndnets();
1655 * Initialize LNet library.
1657 * Automatically called at module loading time. Caller has to call
1658 * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
1659 * latter returned 0. It must be called exactly once.
1661 * \retval 0 on success
1662 * \retval -ve on failures.
1664 int lnet_lib_init(void)
1668 lnet_assert_wire_constants();
1670 memset(&the_lnet, 0, sizeof(the_lnet));
1672 /* refer to global cfs_cpt_table for now */
1673 the_lnet.ln_cpt_table = cfs_cpt_table;
1674 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1676 LASSERT(the_lnet.ln_cpt_number > 0);
1677 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1678 /* we are under risk of consuming all lh_cookie */
1679 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1680 "please change setting of CPT-table and retry\n",
1681 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1685 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1686 the_lnet.ln_cpt_bits++;
1688 rc = lnet_create_locks();
1690 CERROR("Can't create LNet global locks: %d\n", rc);
1694 the_lnet.ln_refcount = 0;
1695 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1696 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1697 INIT_LIST_HEAD(&the_lnet.ln_net_zombie);
1698 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1699 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1701 /* The hash table size is the number of bits it takes to express the set
1702 * ln_num_routes, minus 1 (better to under estimate than over so we
1703 * don't waste memory). */
1704 if (rnet_htable_size <= 0)
1705 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1706 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1707 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1708 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1709 order_base_2(rnet_htable_size) - 1);
1711 /* All LNDs apart from the LOLND are in separate modules. They
1712 * register themselves when their module loads, and unregister
1713 * themselves when their module is unloaded. */
1714 lnet_register_lnd(&the_lolnd);
1719 * Finalize LNet library.
1721 * \pre lnet_lib_init() called with success.
1722 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1724 void lnet_lib_exit(void)
1726 LASSERT(the_lnet.ln_refcount == 0);
1728 while (!list_empty(&the_lnet.ln_lnds))
1729 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1731 lnet_destroy_locks();
1735 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1737 * Users must call this function at least once before any other functions.
1738 * For each successful call there must be a corresponding call to
1739 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1742 * The PID used by LNet may be different from the one requested.
1745 * \param requested_pid PID requested by the caller.
1747 * \return >= 0 on success, and < 0 error code on failures.
1750 LNetNIInit(lnet_pid_t requested_pid)
1752 int im_a_router = 0;
1755 struct lnet_ping_info *pinfo;
1756 lnet_handle_md_t md_handle;
1757 struct list_head net_head;
1758 struct lnet_net *net;
1760 INIT_LIST_HEAD(&net_head);
1762 mutex_lock(&the_lnet.ln_api_mutex);
1764 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1766 if (the_lnet.ln_refcount > 0) {
1767 rc = the_lnet.ln_refcount++;
1768 mutex_unlock(&the_lnet.ln_api_mutex);
1772 rc = lnet_prepare(requested_pid);
1774 mutex_unlock(&the_lnet.ln_api_mutex);
1778 /* create a network for Loopback network */
1779 net = lnet_net_alloc(LNET_MKNET(LOLND, 0), &net_head);
1782 goto err_empty_list;
1785 /* Add in the loopback NI */
1786 if (lnet_ni_alloc(net, NULL, NULL) == NULL) {
1788 goto err_empty_list;
1791 /* If LNet is being initialized via DLC it is possible
1792 * that the user requests not to load module parameters (ones which
1793 * are supported by DLC) on initialization. Therefore, make sure not
1794 * to load networks, routes and forwarding from module parameters
1795 * in this case. On cleanup in case of failure only clean up
1796 * routes if it has been loaded */
1797 if (!the_lnet.ln_nis_from_mod_params) {
1798 rc = lnet_parse_networks(&net_head, lnet_get_networks(),
1801 goto err_empty_list;
1804 ni_count = lnet_startup_lndnets(&net_head);
1807 goto err_empty_list;
1810 if (!the_lnet.ln_nis_from_mod_params) {
1811 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1813 goto err_shutdown_lndnis;
1815 rc = lnet_check_routes();
1817 goto err_destroy_routes;
1819 rc = lnet_rtrpools_alloc(im_a_router);
1821 goto err_destroy_routes;
1824 rc = lnet_acceptor_start();
1826 goto err_destroy_routes;
1828 the_lnet.ln_refcount = 1;
1829 /* Now I may use my own API functions... */
1831 rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1833 goto err_acceptor_stop;
1835 lnet_ping_target_update(pinfo, md_handle);
1837 rc = lnet_router_checker_start();
1844 mutex_unlock(&the_lnet.ln_api_mutex);
1849 lnet_ping_target_fini();
1851 the_lnet.ln_refcount = 0;
1852 lnet_acceptor_stop();
1854 if (!the_lnet.ln_nis_from_mod_params)
1855 lnet_destroy_routes();
1856 err_shutdown_lndnis:
1857 lnet_shutdown_lndnets();
1861 mutex_unlock(&the_lnet.ln_api_mutex);
1862 while (!list_empty(&net_head)) {
1863 struct lnet_net *net;
1865 net = list_entry(net_head.next, struct lnet_net, net_list);
1866 list_del_init(&net->net_list);
1871 EXPORT_SYMBOL(LNetNIInit);
1874 * Stop LNet interfaces, routing, and forwarding.
1876 * Users must call this function once for each successful call to LNetNIInit().
1877 * Once the LNetNIFini() operation has been started, the results of pending
1878 * API operations are undefined.
1880 * \return always 0 for current implementation.
1885 mutex_lock(&the_lnet.ln_api_mutex);
1887 LASSERT(the_lnet.ln_refcount > 0);
1889 if (the_lnet.ln_refcount != 1) {
1890 the_lnet.ln_refcount--;
1892 LASSERT(!the_lnet.ln_niinit_self);
1897 lnet_router_checker_stop();
1898 lnet_ping_target_fini();
1900 /* Teardown fns that use my own API functions BEFORE here */
1901 the_lnet.ln_refcount = 0;
1903 lnet_acceptor_stop();
1904 lnet_destroy_routes();
1905 lnet_shutdown_lndnets();
1909 mutex_unlock(&the_lnet.ln_api_mutex);
1912 EXPORT_SYMBOL(LNetNIFini);
1915 * Grabs the ni data from the ni structure and fills the out
1918 * \param[in] ni network interface structure
1919 * \param[out] cpt_count the number of cpts the ni is on
1920 * \param[out] nid Network Interface ID
1921 * \param[out] peer_timeout NI peer timeout
1922 * \param[out] peer_tx_crdits NI peer transmit credits
1923 * \param[out] peer_rtr_credits NI peer router credits
1924 * \param[out] max_tx_credits NI max transmit credit
1925 * \param[out] net_config Network configuration
1928 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_data *config)
1930 struct lnet_ioctl_net_config *net_config;
1931 struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
1932 size_t min_size, tunable_size = 0;
1938 net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
1942 BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
1943 ARRAY_SIZE(net_config->ni_interfaces));
1945 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1946 if (!ni->ni_interfaces[i])
1949 strncpy(net_config->ni_interfaces[i],
1950 ni->ni_interfaces[i],
1951 sizeof(net_config->ni_interfaces[i]));
1954 config->cfg_nid = ni->ni_nid;
1955 config->cfg_config_u.cfg_net.net_peer_timeout =
1956 ni->ni_net->net_tunables.lct_peer_timeout;
1957 config->cfg_config_u.cfg_net.net_max_tx_credits =
1958 ni->ni_net->net_tunables.lct_max_tx_credits;
1959 config->cfg_config_u.cfg_net.net_peer_tx_credits =
1960 ni->ni_net->net_tunables.lct_peer_tx_credits;
1961 config->cfg_config_u.cfg_net.net_peer_rtr_credits =
1962 ni->ni_net->net_tunables.lct_peer_rtr_credits;
1964 net_config->ni_status = ni->ni_status->ns_status;
1967 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
1969 for (i = 0; i < num_cpts; i++)
1970 net_config->ni_cpts[i] = ni->ni_cpts[i];
1972 config->cfg_ncpts = num_cpts;
1976 * See if user land tools sent in a newer and larger version
1977 * of struct lnet_tunables than what the kernel uses.
1979 min_size = sizeof(*config) + sizeof(*net_config);
1981 if (config->cfg_hdr.ioc_len > min_size)
1982 tunable_size = config->cfg_hdr.ioc_len - min_size;
1984 /* Don't copy too much data to user space */
1985 min_size = min(tunable_size, sizeof(ni->ni_lnd_tunables));
1986 lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
1988 if (lnd_cfg && min_size) {
1989 memcpy(&lnd_cfg->lt_tun, &ni->ni_lnd_tunables, min_size);
1990 config->cfg_config_u.cfg_net.net_interface_count = 1;
1992 /* Tell user land that kernel side has less data */
1993 if (tunable_size > sizeof(ni->ni_lnd_tunables)) {
1994 min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
1995 config->cfg_hdr.ioc_len -= min_size;
2001 lnet_get_ni_idx_locked(int idx)
2004 struct lnet_net *net;
2006 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2007 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2017 lnet_get_next_ni_locked(struct lnet_net *mynet, struct lnet_ni *prev)
2020 struct lnet_net *net = mynet;
2024 net = list_entry(the_lnet.ln_nets.next, struct lnet_net,
2026 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2032 if (prev->ni_netlist.next == &prev->ni_net->net_ni_list) {
2033 /* if you reached the end of the ni list and the net is
2034 * specified, then there are no more nis in that net */
2038 /* we reached the end of this net ni list. move to the
2040 if (prev->ni_net->net_list.next == &the_lnet.ln_nets)
2041 /* no more nets and no more NIs. */
2044 /* get the next net */
2045 net = list_entry(prev->ni_net->net_list.next, struct lnet_net,
2047 /* get the ni on it */
2048 ni = list_entry(net->net_ni_list.next, struct lnet_ni,
2054 /* there are more nis left */
2055 ni = list_entry(prev->ni_netlist.next, struct lnet_ni, ni_netlist);
2061 lnet_get_net_config(struct lnet_ioctl_config_data *config)
2066 int idx = config->cfg_count;
2068 cpt = lnet_net_lock_current();
2070 ni = lnet_get_ni_idx_locked(idx);
2075 lnet_fill_ni_info(ni, config);
2079 lnet_net_unlock(cpt);
2084 lnet_dyn_add_ni(lnet_pid_t requested_pid, struct lnet_ioctl_config_data *conf)
2086 char *nets = conf->cfg_config_u.cfg_net.net_intf;
2087 struct lnet_ping_info *pinfo;
2088 lnet_handle_md_t md_handle;
2089 struct lnet_net *net;
2090 struct list_head net_head;
2092 lnet_remotenet_t *rnet;
2094 int num_acceptor_nets;
2096 struct lnet_ioctl_config_lnd_tunables *lnd_tunables = NULL;
2098 INIT_LIST_HEAD(&net_head);
2100 if (conf && conf->cfg_hdr.ioc_len > sizeof(*conf))
2101 lnd_tunables = (struct lnet_ioctl_config_lnd_tunables *)conf->cfg_bulk;
2103 /* Create a net/ni structures for the network string */
2104 rc = lnet_parse_networks(&net_head, nets, use_tcp_bonding);
2106 return rc == 0 ? -EINVAL : rc;
2108 mutex_lock(&the_lnet.ln_api_mutex);
2111 rc = -EINVAL; /* only add one network per call */
2115 net = list_entry(net_head.next, struct lnet_net, net_list);
2117 lnet_net_lock(LNET_LOCK_EX);
2118 rnet = lnet_find_rnet_locked(net->net_id);
2119 lnet_net_unlock(LNET_LOCK_EX);
2120 /* make sure that the net added doesn't invalidate the current
2121 * configuration LNet is keeping */
2123 CERROR("Adding net %s will invalidate routing configuration\n",
2130 * make sure you calculate the correct number of slots in the ping
2131 * info. Since the ping info is a flattened list of all the NIs,
2132 * we should allocate enough slots to accomodate the number of NIs
2133 * which will be added.
2135 * We can use lnet_get_net_ni_count_locked() since the net is not
2136 * on a public list yet, so locking is not a problem
2138 net_ni_count = lnet_get_net_ni_count_locked(net);
2140 rc = lnet_ping_info_setup(&pinfo, &md_handle,
2141 net_ni_count + lnet_get_ni_count(),
2146 list_del_init(&net->net_list);
2149 memcpy(&net->net_tunables,
2150 &lnd_tunables->lt_cmn, sizeof(lnd_tunables->lt_cmn));
2153 * before starting this network get a count of the current TCP
2154 * networks which require the acceptor thread running. If that
2155 * count is == 0 before we start up this network, then we'd want to
2156 * start up the acceptor thread after starting up this network
2158 num_acceptor_nets = lnet_count_acceptor_nets();
2161 * lnd_startup_lndnet() can deallocate 'net' even if it it returns
2162 * success, because we endded up adding interfaces to an existing
2163 * network. So grab the net_type now
2165 net_type = LNET_NETTYP(net->net_id);
2167 rc = lnet_startup_lndnet(net,
2168 (lnd_tunables) ? &lnd_tunables->lt_tun : NULL);
2173 * Start the acceptor thread if this is the first network
2174 * being added that requires the thread.
2176 if (net_type == SOCKLND && num_acceptor_nets == 0)
2178 rc = lnet_acceptor_start();
2180 /* shutdown the net that we just started */
2181 CERROR("Failed to start up acceptor thread\n");
2183 * Note that if we needed to start the acceptor
2184 * thread, then 'net' must have been the first TCP
2185 * network, therefore was unique, and therefore
2186 * wasn't deallocated by lnet_startup_lndnet()
2188 lnet_shutdown_lndnet(net);
2193 lnet_ping_target_update(pinfo, md_handle);
2194 mutex_unlock(&the_lnet.ln_api_mutex);
2199 lnet_ping_md_unlink(pinfo, &md_handle);
2200 lnet_ping_info_free(pinfo);
2202 mutex_unlock(&the_lnet.ln_api_mutex);
2203 while (!list_empty(&net_head)) {
2204 net = list_entry(net_head.next, struct lnet_net, net_list);
2205 list_del_init(&net->net_list);
2212 lnet_dyn_del_ni(__u32 net_id)
2214 struct lnet_net *net;
2215 struct lnet_ping_info *pinfo;
2216 lnet_handle_md_t md_handle;
2220 /* don't allow userspace to shutdown the LOLND */
2221 if (LNET_NETTYP(net_id) == LOLND)
2224 mutex_lock(&the_lnet.ln_api_mutex);
2228 net = lnet_get_net_locked(net_id);
2234 net_ni_count = lnet_get_net_ni_count_locked(net);
2238 /* create and link a new ping info, before removing the old one */
2239 rc = lnet_ping_info_setup(&pinfo, &md_handle,
2240 lnet_get_ni_count() - net_ni_count, false);
2244 lnet_shutdown_lndnet(net);
2246 if (lnet_count_acceptor_nets() == 0)
2247 lnet_acceptor_stop();
2249 lnet_ping_target_update(pinfo, md_handle);
2252 mutex_unlock(&the_lnet.ln_api_mutex);
2257 void lnet_incr_dlc_seq(void)
2259 atomic_inc(&lnet_dlc_seq_no);
2262 __u32 lnet_get_dlc_seq_locked(void)
2264 return atomic_read(&lnet_dlc_seq_no);
2268 * LNet ioctl handler.
2272 LNetCtl(unsigned int cmd, void *arg)
2274 struct libcfs_ioctl_data *data = arg;
2275 struct lnet_ioctl_config_data *config;
2276 lnet_process_id_t id = {0};
2280 BUILD_BUG_ON(sizeof(struct lnet_ioctl_net_config) +
2281 sizeof(struct lnet_ioctl_config_data) > LIBCFS_IOC_DATA_MAX);
2284 case IOC_LIBCFS_GET_NI:
2285 rc = LNetGetId(data->ioc_count, &id);
2286 data->ioc_nid = id.nid;
2289 case IOC_LIBCFS_FAIL_NID:
2290 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
2292 case IOC_LIBCFS_ADD_ROUTE:
2295 if (config->cfg_hdr.ioc_len < sizeof(*config))
2298 mutex_lock(&the_lnet.ln_api_mutex);
2299 rc = lnet_add_route(config->cfg_net,
2300 config->cfg_config_u.cfg_route.rtr_hop,
2302 config->cfg_config_u.cfg_route.
2305 rc = lnet_check_routes();
2307 lnet_del_route(config->cfg_net,
2310 mutex_unlock(&the_lnet.ln_api_mutex);
2313 case IOC_LIBCFS_DEL_ROUTE:
2316 if (config->cfg_hdr.ioc_len < sizeof(*config))
2319 mutex_lock(&the_lnet.ln_api_mutex);
2320 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
2321 mutex_unlock(&the_lnet.ln_api_mutex);
2324 case IOC_LIBCFS_GET_ROUTE:
2327 if (config->cfg_hdr.ioc_len < sizeof(*config))
2330 return lnet_get_route(config->cfg_count,
2332 &config->cfg_config_u.cfg_route.rtr_hop,
2334 &config->cfg_config_u.cfg_route.rtr_flags,
2335 &config->cfg_config_u.cfg_route.
2338 case IOC_LIBCFS_GET_NET: {
2339 size_t total = sizeof(*config) +
2340 sizeof(struct lnet_ioctl_net_config);
2343 if (config->cfg_hdr.ioc_len < total)
2346 return lnet_get_net_config(config);
2349 case IOC_LIBCFS_GET_LNET_STATS:
2351 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
2353 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
2356 lnet_counters_get(&lnet_stats->st_cntrs);
2360 case IOC_LIBCFS_CONFIG_RTR:
2363 if (config->cfg_hdr.ioc_len < sizeof(*config))
2366 mutex_lock(&the_lnet.ln_api_mutex);
2367 if (config->cfg_config_u.cfg_buffers.buf_enable) {
2368 rc = lnet_rtrpools_enable();
2369 mutex_unlock(&the_lnet.ln_api_mutex);
2372 lnet_rtrpools_disable();
2373 mutex_unlock(&the_lnet.ln_api_mutex);
2376 case IOC_LIBCFS_ADD_BUF:
2379 if (config->cfg_hdr.ioc_len < sizeof(*config))
2382 mutex_lock(&the_lnet.ln_api_mutex);
2383 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
2385 config->cfg_config_u.cfg_buffers.
2387 config->cfg_config_u.cfg_buffers.
2389 mutex_unlock(&the_lnet.ln_api_mutex);
2392 case IOC_LIBCFS_GET_BUF: {
2393 struct lnet_ioctl_pool_cfg *pool_cfg;
2394 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2398 if (config->cfg_hdr.ioc_len < total)
2401 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2402 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2405 case IOC_LIBCFS_ADD_PEER_NI: {
2406 struct lnet_ioctl_peer_cfg *cfg = arg;
2408 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2411 return lnet_add_peer_ni_to_peer(cfg->prcfg_key_nid,
2412 cfg->prcfg_cfg_nid);
2415 case IOC_LIBCFS_DEL_PEER_NI: {
2416 struct lnet_ioctl_peer_cfg *cfg = arg;
2418 if (cfg->prcfg_hdr.ioc_len < sizeof(*cfg))
2421 return lnet_del_peer_ni_from_peer(cfg->prcfg_key_nid,
2422 cfg->prcfg_cfg_nid);
2425 case IOC_LIBCFS_GET_PEER_INFO: {
2426 struct lnet_ioctl_peer *peer_info = arg;
2428 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2431 return lnet_get_peer_ni_info(
2432 peer_info->pr_count,
2434 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2435 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2436 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2437 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2438 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2439 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2440 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2441 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2444 case IOC_LIBCFS_GET_PEER_NI: {
2445 struct lnet_ioctl_peer_cfg *cfg = arg;
2446 struct lnet_peer_ni_credit_info *lpni_cri;
2447 size_t total = sizeof(*cfg) + sizeof(*lpni_cri);
2449 if (cfg->prcfg_hdr.ioc_len < total)
2452 lpni_cri = (struct lnet_peer_ni_credit_info*) cfg->prcfg_bulk;
2454 return lnet_get_peer_info(cfg->prcfg_idx, &cfg->prcfg_key_nid,
2455 &cfg->prcfg_cfg_nid, lpni_cri);
2458 case IOC_LIBCFS_NOTIFY_ROUTER: {
2459 unsigned long jiffies_passed;
2461 jiffies_passed = ktime_get_real_seconds() - data->ioc_u64[0];
2462 jiffies_passed = cfs_time_seconds(jiffies_passed);
2464 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2465 jiffies - jiffies_passed);
2468 case IOC_LIBCFS_LNET_DIST:
2469 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2470 if (rc < 0 && rc != -EHOSTUNREACH)
2473 data->ioc_u32[0] = rc;
2476 case IOC_LIBCFS_TESTPROTOCOMPAT:
2477 lnet_net_lock(LNET_LOCK_EX);
2478 the_lnet.ln_testprotocompat = data->ioc_flags;
2479 lnet_net_unlock(LNET_LOCK_EX);
2482 case IOC_LIBCFS_LNET_FAULT:
2483 return lnet_fault_ctl(data->ioc_flags, data);
2485 case IOC_LIBCFS_PING: {
2486 signed long timeout;
2488 id.nid = data->ioc_nid;
2489 id.pid = data->ioc_u32[0];
2491 /* Don't block longer than 2 minutes */
2492 if (data->ioc_u32[1] > 120 * MSEC_PER_SEC)
2495 /* If timestamp is negative then disable timeout */
2496 if ((s32)data->ioc_u32[1] < 0)
2497 timeout = MAX_SCHEDULE_TIMEOUT;
2499 timeout = msecs_to_jiffies(data->ioc_u32[1]);
2501 rc = lnet_ping(id, timeout, data->ioc_pbuf1,
2502 data->ioc_plen1 / sizeof(lnet_process_id_t));
2505 data->ioc_count = rc;
2509 ni = lnet_net2ni(data->ioc_net);
2513 if (ni->ni_net->net_lnd->lnd_ctl == NULL)
2516 rc = ni->ni_net->net_lnd->lnd_ctl(ni, cmd, arg);
2522 EXPORT_SYMBOL(LNetCtl);
2524 void LNetDebugPeer(lnet_process_id_t id)
2526 lnet_debug_peer(id.nid);
2528 EXPORT_SYMBOL(LNetDebugPeer);
2531 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2532 * all interfaces share a same PID, as requested by LNetNIInit().
2534 * \param index Index of the interface to look up.
2535 * \param id On successful return, this location will hold the
2536 * lnet_process_id_t ID of the interface.
2538 * \retval 0 If an interface exists at \a index.
2539 * \retval -ENOENT If no interface has been found.
2542 LNetGetId(unsigned int index, lnet_process_id_t *id)
2545 struct lnet_net *net;
2549 LASSERT(the_lnet.ln_refcount > 0);
2551 cpt = lnet_net_lock_current();
2553 list_for_each_entry(net, &the_lnet.ln_nets, net_list) {
2554 list_for_each_entry(ni, &net->net_ni_list, ni_netlist) {
2558 id->nid = ni->ni_nid;
2559 id->pid = the_lnet.ln_pid;
2565 lnet_net_unlock(cpt);
2568 EXPORT_SYMBOL(LNetGetId);
2571 * Print a string representation of handle \a h into buffer \a str of
2575 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2577 snprintf(str, len, "%#llx", h.cookie);
2579 EXPORT_SYMBOL(LNetSnprintHandle);
2581 static int lnet_ping(lnet_process_id_t id, signed long timeout,
2582 lnet_process_id_t __user *ids, int n_ids)
2584 lnet_handle_eq_t eqh;
2585 lnet_handle_md_t mdh;
2587 lnet_md_t md = { NULL };
2591 const signed long a_long_time = msecs_to_jiffies(60 * MSEC_PER_SEC);
2593 struct lnet_ping_info *info;
2594 lnet_process_id_t tmpid;
2601 infosz = offsetof(struct lnet_ping_info, pi_ni[n_ids]);
2603 /* n_ids limit is arbitrary */
2604 if (n_ids <= 0 || n_ids > 20 || id.nid == LNET_NID_ANY)
2607 if (id.pid == LNET_PID_ANY)
2608 id.pid = LNET_PID_LUSTRE;
2610 LIBCFS_ALLOC(info, infosz);
2614 /* NB 2 events max (including any unlink event) */
2615 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2617 CERROR("Can't allocate EQ: %d\n", rc);
2621 /* initialize md content */
2624 md.threshold = 2; /*GET/REPLY*/
2626 md.options = LNET_MD_TRUNCATE;
2630 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2632 CERROR("Can't bind MD: %d\n", rc);
2636 rc = LNetGet(LNET_NID_ANY, mdh, id,
2637 LNET_RESERVED_PORTAL,
2638 LNET_PROTO_PING_MATCHBITS, 0);
2641 /* Don't CERROR; this could be deliberate! */
2643 rc2 = LNetMDUnlink(mdh);
2646 /* NB must wait for the UNLINK event below... */
2648 timeout = a_long_time;
2652 /* MUST block for unlink to complete */
2654 blocked = cfs_block_allsigs();
2656 rc2 = LNetEQPoll(&eqh, 1, timeout, &event, &which);
2659 cfs_restore_sigs(blocked);
2661 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2662 (rc2 <= 0) ? -1 : event.type,
2663 (rc2 <= 0) ? -1 : event.status,
2664 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2666 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
2668 if (rc2 <= 0 || event.status != 0) {
2669 /* timeout or error */
2670 if (!replied && rc == 0)
2671 rc = (rc2 < 0) ? rc2 :
2672 (rc2 == 0) ? -ETIMEDOUT :
2676 /* Ensure completion in finite time... */
2678 /* No assertion (racing with network) */
2680 timeout = a_long_time;
2681 } else if (rc2 == 0) {
2682 /* timed out waiting for unlink */
2683 CWARN("ping %s: late network completion\n",
2686 } else if (event.type == LNET_EVENT_REPLY) {
2691 } while (rc2 <= 0 || !event.unlinked);
2695 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2702 LASSERT(nob >= 0 && nob <= infosz);
2704 rc = -EPROTO; /* if I can't parse... */
2707 /* can't check magic/version */
2708 CERROR("%s: ping info too short %d\n",
2709 libcfs_id2str(id), nob);
2713 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2714 lnet_swap_pinginfo(info);
2715 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2716 CERROR("%s: Unexpected magic %08x\n",
2717 libcfs_id2str(id), info->pi_magic);
2721 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2722 CERROR("%s: ping w/o NI status: 0x%x\n",
2723 libcfs_id2str(id), info->pi_features);
2727 if (nob < offsetof(struct lnet_ping_info, pi_ni[0])) {
2728 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2729 nob, (int)offsetof(struct lnet_ping_info, pi_ni[0]));
2733 if (info->pi_nnis < n_ids)
2734 n_ids = info->pi_nnis;
2736 if (nob < offsetof(struct lnet_ping_info, pi_ni[n_ids])) {
2737 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2738 nob, (int)offsetof(struct lnet_ping_info, pi_ni[n_ids]));
2742 rc = -EFAULT; /* If I SEGV... */
2744 memset(&tmpid, 0, sizeof(tmpid));
2745 for (i = 0; i < n_ids; i++) {
2746 tmpid.pid = info->pi_pid;
2747 tmpid.nid = info->pi_ni[i].ns_nid;
2748 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2754 rc2 = LNetEQFree(eqh);
2756 CERROR("rc2 %d\n", rc2);
2760 LIBCFS_FREE(info, infosz);