4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <lnet/lib-lnet.h>
39 #include <lnet/lib-dlc.h>
40 #include <linux/log2.h>
42 #define D_LNI D_CONSOLE
44 lnet_t the_lnet; /* THE state of the network */
45 EXPORT_SYMBOL(the_lnet);
47 static char *ip2nets = "";
48 CFS_MODULE_PARM(ip2nets, "s", charp, 0444,
49 "LNET network <- IP table");
51 static char *networks = "";
52 CFS_MODULE_PARM(networks, "s", charp, 0444,
55 static char *routes = "";
56 CFS_MODULE_PARM(routes, "s", charp, 0444,
57 "routes to non-local networks");
59 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
60 CFS_MODULE_PARM(rnet_htable_size, "i", int, 0444,
61 "size of remote network hash table");
63 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
64 lnet_process_id_t *ids, int n_ids);
73 lnet_get_networks(void)
78 if (*networks != 0 && *ip2nets != 0) {
79 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or "
80 "'ip2nets' but not both at once\n");
85 rc = lnet_parse_ip2nets(&nets, ip2nets);
86 return (rc == 0) ? nets : NULL;
98 spin_lock_init(&the_lnet.ln_eq_wait_lock);
99 init_waitqueue_head(&the_lnet.ln_eq_waitq);
100 init_waitqueue_head(&the_lnet.ln_rc_waitq);
101 mutex_init(&the_lnet.ln_lnd_mutex);
102 mutex_init(&the_lnet.ln_api_mutex);
106 lnet_fini_locks(void)
111 lnet_create_remote_nets_table(void)
114 struct list_head *hash;
116 LASSERT(the_lnet.ln_remote_nets_hash == NULL);
117 LASSERT(the_lnet.ln_remote_nets_hbits > 0);
118 LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
120 CERROR("Failed to create remote nets hash table\n");
124 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
125 INIT_LIST_HEAD(&hash[i]);
126 the_lnet.ln_remote_nets_hash = hash;
131 lnet_destroy_remote_nets_table(void)
135 if (the_lnet.ln_remote_nets_hash == NULL)
138 for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
139 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
141 LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
142 LNET_REMOTE_NETS_HASH_SIZE *
143 sizeof(the_lnet.ln_remote_nets_hash[0]));
144 the_lnet.ln_remote_nets_hash = NULL;
148 lnet_destroy_locks(void)
150 if (the_lnet.ln_res_lock != NULL) {
151 cfs_percpt_lock_free(the_lnet.ln_res_lock);
152 the_lnet.ln_res_lock = NULL;
155 if (the_lnet.ln_net_lock != NULL) {
156 cfs_percpt_lock_free(the_lnet.ln_net_lock);
157 the_lnet.ln_net_lock = NULL;
164 lnet_create_locks(void)
168 the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
169 if (the_lnet.ln_res_lock == NULL)
172 the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
173 if (the_lnet.ln_net_lock == NULL)
179 lnet_destroy_locks();
183 static void lnet_assert_wire_constants(void)
185 /* Wire protocol assertions generated by 'wirecheck'
186 * running on Linux robert.bartonsoftware.com 2.6.8-1.521
187 * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
188 * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7) */
191 CLASSERT (LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
192 CLASSERT (LNET_PROTO_TCP_VERSION_MAJOR == 1);
193 CLASSERT (LNET_PROTO_TCP_VERSION_MINOR == 0);
194 CLASSERT (LNET_MSG_ACK == 0);
195 CLASSERT (LNET_MSG_PUT == 1);
196 CLASSERT (LNET_MSG_GET == 2);
197 CLASSERT (LNET_MSG_REPLY == 3);
198 CLASSERT (LNET_MSG_HELLO == 4);
200 /* Checks for struct ptl_handle_wire_t */
201 CLASSERT ((int)sizeof(lnet_handle_wire_t) == 16);
202 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
203 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
204 CLASSERT ((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
205 CLASSERT ((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
207 /* Checks for struct lnet_magicversion_t */
208 CLASSERT ((int)sizeof(lnet_magicversion_t) == 8);
209 CLASSERT ((int)offsetof(lnet_magicversion_t, magic) == 0);
210 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
211 CLASSERT ((int)offsetof(lnet_magicversion_t, version_major) == 4);
212 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
213 CLASSERT ((int)offsetof(lnet_magicversion_t, version_minor) == 6);
214 CLASSERT ((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
216 /* Checks for struct lnet_hdr_t */
217 CLASSERT ((int)sizeof(lnet_hdr_t) == 72);
218 CLASSERT ((int)offsetof(lnet_hdr_t, dest_nid) == 0);
219 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
220 CLASSERT ((int)offsetof(lnet_hdr_t, src_nid) == 8);
221 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
222 CLASSERT ((int)offsetof(lnet_hdr_t, dest_pid) == 16);
223 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
224 CLASSERT ((int)offsetof(lnet_hdr_t, src_pid) == 20);
225 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
226 CLASSERT ((int)offsetof(lnet_hdr_t, type) == 24);
227 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
228 CLASSERT ((int)offsetof(lnet_hdr_t, payload_length) == 28);
229 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
230 CLASSERT ((int)offsetof(lnet_hdr_t, msg) == 32);
231 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
234 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
235 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
236 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
237 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
238 CLASSERT ((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
239 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
242 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
243 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
244 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
245 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
246 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
247 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
248 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
249 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
250 CLASSERT ((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
251 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
254 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
255 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
256 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
257 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
258 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
259 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
260 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
261 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
262 CLASSERT ((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
263 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
266 CLASSERT ((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
267 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
270 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
271 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
272 CLASSERT ((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
273 CLASSERT ((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
276 static lnd_t *lnet_find_lnd_by_type(__u32 type)
279 struct list_head *tmp;
281 /* holding lnd mutex */
282 list_for_each(tmp, &the_lnet.ln_lnds) {
283 lnd = list_entry(tmp, lnd_t, lnd_list);
285 if (lnd->lnd_type == type)
292 lnet_register_lnd (lnd_t *lnd)
294 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
296 LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
297 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == NULL);
299 list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
300 lnd->lnd_refcount = 0;
302 CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
304 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
306 EXPORT_SYMBOL(lnet_register_lnd);
309 lnet_unregister_lnd (lnd_t *lnd)
311 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
313 LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
314 LASSERT(lnd->lnd_refcount == 0);
316 list_del(&lnd->lnd_list);
317 CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
319 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
321 EXPORT_SYMBOL(lnet_unregister_lnd);
324 lnet_counters_get(lnet_counters_t *counters)
326 lnet_counters_t *ctr;
329 memset(counters, 0, sizeof(*counters));
331 lnet_net_lock(LNET_LOCK_EX);
333 cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
334 counters->msgs_max += ctr->msgs_max;
335 counters->msgs_alloc += ctr->msgs_alloc;
336 counters->errors += ctr->errors;
337 counters->send_count += ctr->send_count;
338 counters->recv_count += ctr->recv_count;
339 counters->route_count += ctr->route_count;
340 counters->drop_count += ctr->drop_count;
341 counters->send_length += ctr->send_length;
342 counters->recv_length += ctr->recv_length;
343 counters->route_length += ctr->route_length;
344 counters->drop_length += ctr->drop_length;
347 lnet_net_unlock(LNET_LOCK_EX);
349 EXPORT_SYMBOL(lnet_counters_get);
352 lnet_counters_reset(void)
354 lnet_counters_t *counters;
357 lnet_net_lock(LNET_LOCK_EX);
359 cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
360 memset(counters, 0, sizeof(lnet_counters_t));
362 lnet_net_unlock(LNET_LOCK_EX);
364 EXPORT_SYMBOL(lnet_counters_reset);
366 static __u64 lnet_create_interface_cookie(void)
368 /* NB the interface cookie in wire handles guards against delayed
369 * replies and ACKs appearing valid after reboot. Initialisation time,
370 * even if it's only implemented to millisecond resolution is probably
371 * easily good enough. */
374 do_gettimeofday(&tv);
377 cookie += tv.tv_usec;
382 lnet_res_type2str(int type)
387 case LNET_COOKIE_TYPE_MD:
389 case LNET_COOKIE_TYPE_ME:
391 case LNET_COOKIE_TYPE_EQ:
397 lnet_res_container_cleanup(struct lnet_res_container *rec)
401 if (rec->rec_type == 0) /* not set yet, it's uninitialized */
404 while (!list_empty(&rec->rec_active)) {
405 struct list_head *e = rec->rec_active.next;
408 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
409 lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
411 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
412 lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
414 } else { /* NB: Active MEs should be attached on portals */
421 /* Found alive MD/ME/EQ, user really should unlink/free
422 * all of them before finalize LNet, but if someone didn't,
423 * we have to recycle garbage for him */
424 CERROR("%d active elements on exit of %s container\n",
425 count, lnet_res_type2str(rec->rec_type));
428 if (rec->rec_lh_hash != NULL) {
429 LIBCFS_FREE(rec->rec_lh_hash,
430 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
431 rec->rec_lh_hash = NULL;
434 rec->rec_type = 0; /* mark it as finalized */
438 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
443 LASSERT(rec->rec_type == 0);
445 rec->rec_type = type;
446 INIT_LIST_HEAD(&rec->rec_active);
448 rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
450 /* Arbitrary choice of hash table size */
451 LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
452 LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
453 if (rec->rec_lh_hash == NULL) {
458 for (i = 0; i < LNET_LH_HASH_SIZE; i++)
459 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
464 CERROR("Failed to setup %s resource container\n",
465 lnet_res_type2str(type));
466 lnet_res_container_cleanup(rec);
471 lnet_res_containers_destroy(struct lnet_res_container **recs)
473 struct lnet_res_container *rec;
476 cfs_percpt_for_each(rec, i, recs)
477 lnet_res_container_cleanup(rec);
479 cfs_percpt_free(recs);
482 static struct lnet_res_container **
483 lnet_res_containers_create(int type)
485 struct lnet_res_container **recs;
486 struct lnet_res_container *rec;
490 recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
492 CERROR("Failed to allocate %s resource containers\n",
493 lnet_res_type2str(type));
497 cfs_percpt_for_each(rec, i, recs) {
498 rc = lnet_res_container_setup(rec, i, type);
500 lnet_res_containers_destroy(recs);
509 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
511 /* ALWAYS called with lnet_res_lock held */
512 struct list_head *head;
513 lnet_libhandle_t *lh;
516 if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
519 hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
520 head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
522 list_for_each_entry(lh, head, lh_hash_chain) {
523 if (lh->lh_cookie == cookie)
531 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
533 /* ALWAYS called with lnet_res_lock held */
534 unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
537 lh->lh_cookie = rec->rec_lh_cookie;
538 rec->rec_lh_cookie += 1 << ibits;
540 hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
542 list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
545 static int lnet_unprepare(void);
548 lnet_prepare(lnet_pid_t requested_pid)
550 /* Prepare to bring up the network */
551 struct lnet_res_container **recs;
554 if (requested_pid == LNET_PID_ANY) {
555 /* Don't instantiate LNET just for me */
559 LASSERT(the_lnet.ln_refcount == 0);
561 the_lnet.ln_routing = 0;
563 LASSERT((requested_pid & LNET_PID_USERFLAG) == 0);
564 the_lnet.ln_pid = requested_pid;
566 INIT_LIST_HEAD(&the_lnet.ln_test_peers);
567 INIT_LIST_HEAD(&the_lnet.ln_nis);
568 INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
569 INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
570 INIT_LIST_HEAD(&the_lnet.ln_routers);
571 INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
572 INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
574 rc = lnet_create_remote_nets_table();
578 the_lnet.ln_interface_cookie = lnet_create_interface_cookie();
580 the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
581 sizeof(lnet_counters_t));
582 if (the_lnet.ln_counters == NULL) {
583 CERROR("Failed to allocate counters for LNet\n");
588 rc = lnet_peer_tables_create();
592 rc = lnet_msg_containers_create();
596 rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
597 LNET_COOKIE_TYPE_EQ);
601 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
607 the_lnet.ln_me_containers = recs;
609 recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
615 the_lnet.ln_md_containers = recs;
617 rc = lnet_portals_create();
619 CERROR("Failed to create portals for LNet: %d\n", rc);
631 lnet_unprepare (void)
633 /* NB no LNET_LOCK since this is the last reference. All LND instances
634 * have shut down already, so it is safe to unlink and free all
635 * descriptors, even those that appear committed to a network op (eg MD
636 * with non-zero pending count) */
638 lnet_fail_nid(LNET_NID_ANY, 0);
640 LASSERT(the_lnet.ln_refcount == 0);
641 LASSERT(list_empty(&the_lnet.ln_test_peers));
642 LASSERT(list_empty(&the_lnet.ln_nis));
643 LASSERT(list_empty(&the_lnet.ln_nis_cpt));
644 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
646 lnet_portals_destroy();
648 if (the_lnet.ln_md_containers != NULL) {
649 lnet_res_containers_destroy(the_lnet.ln_md_containers);
650 the_lnet.ln_md_containers = NULL;
653 if (the_lnet.ln_me_containers != NULL) {
654 lnet_res_containers_destroy(the_lnet.ln_me_containers);
655 the_lnet.ln_me_containers = NULL;
658 lnet_res_container_cleanup(&the_lnet.ln_eq_container);
660 lnet_msg_containers_destroy();
661 lnet_peer_tables_destroy();
662 lnet_rtrpools_free(0);
664 if (the_lnet.ln_counters != NULL) {
665 cfs_percpt_free(the_lnet.ln_counters);
666 the_lnet.ln_counters = NULL;
668 lnet_destroy_remote_nets_table();
674 lnet_net2ni_locked(__u32 net, int cpt)
676 struct list_head *tmp;
679 LASSERT(cpt != LNET_LOCK_EX);
681 list_for_each(tmp, &the_lnet.ln_nis) {
682 ni = list_entry(tmp, lnet_ni_t, ni_list);
684 if (LNET_NIDNET(ni->ni_nid) == net) {
685 lnet_ni_addref_locked(ni, cpt);
694 lnet_net2ni(__u32 net)
699 ni = lnet_net2ni_locked(net, 0);
704 EXPORT_SYMBOL(lnet_net2ni);
707 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
712 LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
717 val = hash_long(key, LNET_CPT_BITS);
718 /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
722 return (unsigned int)(key + val + (val >> 1)) % number;
726 lnet_cpt_of_nid_locked(lnet_nid_t nid)
730 /* must called with hold of lnet_net_lock */
731 if (LNET_CPT_NUMBER == 1)
732 return 0; /* the only one */
734 /* take lnet_net_lock(any) would be OK */
735 if (!list_empty(&the_lnet.ln_nis_cpt)) {
736 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
737 if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
740 LASSERT(ni->ni_cpts != NULL);
741 return ni->ni_cpts[lnet_nid_cpt_hash
742 (nid, ni->ni_ncpts)];
746 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
750 lnet_cpt_of_nid(lnet_nid_t nid)
755 if (LNET_CPT_NUMBER == 1)
756 return 0; /* the only one */
758 if (list_empty(&the_lnet.ln_nis_cpt))
759 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
761 cpt = lnet_net_lock_current();
762 cpt2 = lnet_cpt_of_nid_locked(nid);
763 lnet_net_unlock(cpt);
767 EXPORT_SYMBOL(lnet_cpt_of_nid);
770 lnet_islocalnet(__u32 net)
775 cpt = lnet_net_lock_current();
777 ni = lnet_net2ni_locked(net, cpt);
779 lnet_ni_decref_locked(ni, cpt);
781 lnet_net_unlock(cpt);
787 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
790 struct list_head *tmp;
792 LASSERT(cpt != LNET_LOCK_EX);
794 list_for_each(tmp, &the_lnet.ln_nis) {
795 ni = list_entry(tmp, lnet_ni_t, ni_list);
797 if (ni->ni_nid == nid) {
798 lnet_ni_addref_locked(ni, cpt);
807 lnet_islocalnid(lnet_nid_t nid)
812 cpt = lnet_net_lock_current();
813 ni = lnet_nid2ni_locked(nid, cpt);
815 lnet_ni_decref_locked(ni, cpt);
816 lnet_net_unlock(cpt);
822 lnet_count_acceptor_nis (void)
824 /* Return the # of NIs that need the acceptor. */
826 struct list_head *tmp;
830 cpt = lnet_net_lock_current();
831 list_for_each(tmp, &the_lnet.ln_nis) {
832 ni = list_entry(tmp, lnet_ni_t, ni_list);
834 if (ni->ni_lnd->lnd_accept != NULL)
838 lnet_net_unlock(cpt);
843 static lnet_ping_info_t *
844 lnet_ping_info_create(int num_ni)
846 lnet_ping_info_t *ping_info;
849 infosz = offsetof(lnet_ping_info_t, pi_ni[num_ni]);
850 LIBCFS_ALLOC(ping_info, infosz);
851 if (ping_info == NULL) {
852 CERROR("Can't allocate ping info[%d]\n", num_ni);
856 ping_info->pi_nnis = num_ni;
857 ping_info->pi_pid = the_lnet.ln_pid;
858 ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
859 ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
865 lnet_get_ni_count(void)
872 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list)
881 lnet_ping_info_free(lnet_ping_info_t *pinfo)
884 offsetof(lnet_ping_info_t,
885 pi_ni[pinfo->pi_nnis]));
889 lnet_ping_info_destroy(void)
893 lnet_net_lock(LNET_LOCK_EX);
895 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
897 ni->ni_status = NULL;
901 lnet_ping_info_free(the_lnet.ln_ping_info);
902 the_lnet.ln_ping_info = NULL;
904 lnet_net_unlock(LNET_LOCK_EX);
908 lnet_ping_event_handler(lnet_event_t *event)
910 lnet_ping_info_t *pinfo = event->md.user_ptr;
913 pinfo->pi_features = LNET_PING_FEAT_INVAL;
917 lnet_ping_info_setup(lnet_ping_info_t **ppinfo, lnet_handle_md_t *md_handle,
918 int ni_count, bool set_eq)
920 lnet_handle_me_t me_handle;
921 lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
922 lnet_md_t md = {NULL};
926 rc = LNetEQAlloc(0, lnet_ping_event_handler,
927 &the_lnet.ln_ping_target_eq);
929 CERROR("Can't allocate ping EQ: %d\n", rc);
934 *ppinfo = lnet_ping_info_create(ni_count);
935 if (*ppinfo == NULL) {
940 rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
941 LNET_PROTO_PING_MATCHBITS, 0,
942 LNET_UNLINK, LNET_INS_AFTER,
945 CERROR("Can't create ping ME: %d\n", rc);
949 /* initialize md content */
951 md.length = offsetof(lnet_ping_info_t,
952 pi_ni[(*ppinfo)->pi_nnis]);
953 md.threshold = LNET_MD_THRESH_INF;
955 md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
956 LNET_MD_MANAGE_REMOTE;
958 md.eq_handle = the_lnet.ln_ping_target_eq;
959 md.user_ptr = *ppinfo;
961 rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
963 CERROR("Can't attach ping MD: %d\n", rc);
970 rc2 = LNetMEUnlink(me_handle);
973 lnet_ping_info_free(*ppinfo);
977 LNetEQFree(the_lnet.ln_ping_target_eq);
982 lnet_ping_md_unlink(lnet_ping_info_t *pinfo, lnet_handle_md_t *md_handle)
984 sigset_t blocked = cfs_block_allsigs();
986 LNetMDUnlink(*md_handle);
987 LNetInvalidateHandle(md_handle);
989 /* NB md could be busy; this just starts the unlink */
990 while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
991 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
992 cfs_pause(cfs_time_seconds(1));
995 cfs_restore_sigs(blocked);
999 lnet_ping_info_install_locked(lnet_ping_info_t *ping_info)
1003 lnet_ni_status_t *ns;
1006 list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
1007 LASSERT(i < ping_info->pi_nnis);
1009 ns = &ping_info->pi_ni[i];
1011 ns->ns_nid = ni->ni_nid;
1014 ns->ns_status = (ni->ni_status != NULL) ?
1015 ni->ni_status->ns_status : LNET_NI_STATUS_UP;
1024 lnet_ping_target_update(lnet_ping_info_t *pinfo, lnet_handle_md_t md_handle)
1026 lnet_ping_info_t *old_pinfo = NULL;
1027 lnet_handle_md_t old_md;
1029 /* switch the NIs to point to the new ping info created */
1030 lnet_net_lock(LNET_LOCK_EX);
1032 if (!the_lnet.ln_routing)
1033 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1034 lnet_ping_info_install_locked(pinfo);
1036 if (the_lnet.ln_ping_info != NULL) {
1037 old_pinfo = the_lnet.ln_ping_info;
1038 old_md = the_lnet.ln_ping_target_md;
1040 the_lnet.ln_ping_target_md = md_handle;
1041 the_lnet.ln_ping_info = pinfo;
1043 lnet_net_unlock(LNET_LOCK_EX);
1045 if (old_pinfo != NULL) {
1046 /* unlink the old ping info */
1047 lnet_ping_md_unlink(old_pinfo, &old_md);
1048 lnet_ping_info_free(old_pinfo);
1053 lnet_ping_target_fini(void)
1057 lnet_ping_md_unlink(the_lnet.ln_ping_info,
1058 &the_lnet.ln_ping_target_md);
1060 rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1063 lnet_ping_info_destroy();
1067 lnet_ni_tq_credits(lnet_ni_t *ni)
1071 LASSERT(ni->ni_ncpts >= 1);
1073 if (ni->ni_ncpts == 1)
1074 return ni->ni_maxtxcredits;
1076 credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1077 credits = max(credits, 8 * ni->ni_peertxcredits);
1078 credits = min(credits, ni->ni_maxtxcredits);
1084 lnet_ni_unlink_locked(lnet_ni_t *ni)
1086 if (!list_empty(&ni->ni_cptlist)) {
1087 list_del_init(&ni->ni_cptlist);
1088 lnet_ni_decref_locked(ni, 0);
1091 /* move it to zombie list and nobody can find it anymore */
1092 LASSERT(!list_empty(&ni->ni_list));
1093 list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1094 lnet_ni_decref_locked(ni, 0); /* drop ln_nis' ref */
1098 lnet_clear_zombies_nis_locked(void)
1104 /* Now wait for the NI's I just nuked to show up on ln_zombie_nis
1105 * and shut them down in guaranteed thread context */
1107 while (!list_empty(&the_lnet.ln_nis_zombie)) {
1111 ni = list_entry(the_lnet.ln_nis_zombie.next,
1112 lnet_ni_t, ni_list);
1113 list_del_init(&ni->ni_list);
1114 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1117 /* still busy, add it back to zombie list */
1118 list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1122 if (!list_empty(&ni->ni_list)) {
1123 lnet_net_unlock(LNET_LOCK_EX);
1125 if ((i & (-i)) == i) {
1127 "Waiting for zombie LNI %s\n",
1128 libcfs_nid2str(ni->ni_nid));
1130 cfs_pause(cfs_time_seconds(1));
1131 lnet_net_lock(LNET_LOCK_EX);
1135 ni->ni_lnd->lnd_refcount--;
1136 lnet_net_unlock(LNET_LOCK_EX);
1138 islo = ni->ni_lnd->lnd_type == LOLND;
1140 LASSERT(!in_interrupt());
1141 (ni->ni_lnd->lnd_shutdown)(ni);
1143 /* can't deref lnd anymore now; it might have unregistered
1147 CDEBUG(D_LNI, "Removed LNI %s\n",
1148 libcfs_nid2str(ni->ni_nid));
1152 lnet_net_lock(LNET_LOCK_EX);
1157 lnet_shutdown_lndnis(void)
1162 /* NB called holding the global mutex */
1164 /* All quiet on the API front */
1165 LASSERT(!the_lnet.ln_shutdown);
1166 LASSERT(the_lnet.ln_refcount == 0);
1167 LASSERT(list_empty(&the_lnet.ln_nis_zombie));
1169 lnet_net_lock(LNET_LOCK_EX);
1170 the_lnet.ln_shutdown = 1; /* flag shutdown */
1172 /* Unlink NIs from the global table */
1173 while (!list_empty(&the_lnet.ln_nis)) {
1174 ni = list_entry(the_lnet.ln_nis.next,
1175 lnet_ni_t, ni_list);
1176 lnet_ni_unlink_locked(ni);
1179 /* Drop the cached loopback NI. */
1180 if (the_lnet.ln_loni != NULL) {
1181 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1182 the_lnet.ln_loni = NULL;
1185 lnet_net_unlock(LNET_LOCK_EX);
1187 /* Clear lazy portals and drop delayed messages which hold refs
1188 * on their lnet_msg_t::msg_rxpeer */
1189 for (i = 0; i < the_lnet.ln_nportals; i++)
1190 LNetClearLazyPortal(i);
1192 /* Clear the peer table and wait for all peers to go (they hold refs on
1194 lnet_peer_tables_cleanup(NULL);
1196 lnet_net_lock(LNET_LOCK_EX);
1198 lnet_clear_zombies_nis_locked();
1199 the_lnet.ln_shutdown = 0;
1200 lnet_net_unlock(LNET_LOCK_EX);
1203 /* shutdown down the NI and release refcount */
1205 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 /* Do peer table cleanup for this ni */
1212 lnet_peer_tables_cleanup(ni);
1214 lnet_net_lock(LNET_LOCK_EX);
1215 lnet_clear_zombies_nis_locked();
1216 lnet_net_unlock(LNET_LOCK_EX);
1220 lnet_startup_lndni(struct lnet_ni *ni, __s32 peer_timeout,
1221 __s32 peer_cr, __s32 peer_buf_cr, __s32 credits)
1226 struct lnet_tx_queue *tq;
1229 lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1231 LASSERT(libcfs_isknown_lnd(lnd_type));
1233 if (lnd_type == CIBLND || lnd_type == OPENIBLND ||
1234 lnd_type == IIBLND || lnd_type == VIBLND) {
1235 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type));
1239 /* Make sure this new NI is unique. */
1240 lnet_net_lock(LNET_LOCK_EX);
1241 rc = lnet_net_unique(LNET_NIDNET(ni->ni_nid), &the_lnet.ln_nis);
1242 lnet_net_unlock(LNET_LOCK_EX);
1245 if (lnd_type == LOLND) {
1250 CERROR("Net %s is not unique\n",
1251 libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
1257 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1258 lnd = lnet_find_lnd_by_type(lnd_type);
1261 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1262 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1263 LNET_MUTEX_LOCK(&the_lnet.ln_lnd_mutex);
1265 lnd = lnet_find_lnd_by_type(lnd_type);
1267 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1268 CERROR("Can't load LND %s, module %s, rc=%d\n",
1269 libcfs_lnd2str(lnd_type),
1270 libcfs_lnd2modname(lnd_type), rc);
1271 #ifndef HAVE_MODULE_LOADING_SUPPORT
1272 LCONSOLE_ERROR_MSG(0x104, "Your kernel must be "
1273 "compiled with kernel module "
1274 "loading support.");
1281 lnet_net_lock(LNET_LOCK_EX);
1282 lnd->lnd_refcount++;
1283 lnet_net_unlock(LNET_LOCK_EX);
1287 rc = (lnd->lnd_startup)(ni);
1289 LNET_MUTEX_UNLOCK(&the_lnet.ln_lnd_mutex);
1292 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1293 rc, libcfs_lnd2str(lnd->lnd_type));
1294 lnet_net_lock(LNET_LOCK_EX);
1295 lnd->lnd_refcount--;
1296 lnet_net_unlock(LNET_LOCK_EX);
1300 /* If given some LND tunable parameters, parse those now to
1301 * override the values in the NI structure. */
1302 if (peer_buf_cr >= 0)
1303 ni->ni_peerrtrcredits = peer_buf_cr;
1304 if (peer_timeout >= 0)
1305 ni->ni_peertimeout = peer_timeout;
1308 * Note: For now, don't allow the user to change
1309 * peertxcredits as this number is used in the
1310 * IB LND to control queue depth.
1311 * if (peer_cr != -1)
1312 * ni->ni_peertxcredits = peer_cr;
1315 ni->ni_maxtxcredits = credits;
1317 LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query != NULL);
1319 lnet_net_lock(LNET_LOCK_EX);
1320 /* refcount for ln_nis */
1321 lnet_ni_addref_locked(ni, 0);
1322 list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1323 if (ni->ni_cpts != NULL) {
1324 lnet_ni_addref_locked(ni, 0);
1325 list_add_tail(&ni->ni_cptlist, &the_lnet.ln_nis_cpt);
1328 lnet_net_unlock(LNET_LOCK_EX);
1330 if (lnd->lnd_type == LOLND) {
1332 LASSERT(the_lnet.ln_loni == NULL);
1333 the_lnet.ln_loni = ni;
1337 if (ni->ni_peertxcredits == 0 || ni->ni_maxtxcredits == 0) {
1338 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1339 libcfs_lnd2str(lnd->lnd_type),
1340 ni->ni_peertxcredits == 0 ?
1342 /* shutdown the NI since if we get here then it must've already
1345 lnet_shutdown_lndni(ni);
1349 cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1350 tq->tq_credits_min =
1351 tq->tq_credits_max =
1352 tq->tq_credits = lnet_ni_tq_credits(ni);
1355 CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1356 libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1357 lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1358 ni->ni_peerrtrcredits, ni->ni_peertimeout);
1367 lnet_startup_lndnis(struct list_head *nilist)
1373 while (!list_empty(nilist)) {
1374 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
1375 list_del(&ni->ni_list);
1376 rc = lnet_startup_lndni(ni, -1, -1, -1, -1);
1386 lnet_shutdown_lndnis();
1392 * Initialize LNet library.
1394 * Automatically called at module loading time. Caller has to call
1395 * lnet_fini() after a call to lnet_init(), if and only if the latter
1396 * returned 0. It must be called exactly once.
1398 * \return 0 on success, and -ve on failures.
1405 lnet_assert_wire_constants();
1407 memset(&the_lnet, 0, sizeof(the_lnet));
1409 /* refer to global cfs_cpt_table for now */
1410 the_lnet.ln_cpt_table = cfs_cpt_table;
1411 the_lnet.ln_cpt_number = cfs_cpt_number(cfs_cpt_table);
1413 LASSERT(the_lnet.ln_cpt_number > 0);
1414 if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1415 /* we are under risk of consuming all lh_cookie */
1416 CERROR("Can't have %d CPTs for LNet (max allowed is %d), "
1417 "please change setting of CPT-table and retry\n",
1418 the_lnet.ln_cpt_number, LNET_CPT_MAX);
1422 while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1423 the_lnet.ln_cpt_bits++;
1425 rc = lnet_create_locks();
1427 CERROR("Can't create LNet global locks: %d\n", rc);
1431 the_lnet.ln_refcount = 0;
1432 LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1433 INIT_LIST_HEAD(&the_lnet.ln_lnds);
1434 INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1435 INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1437 /* The hash table size is the number of bits it takes to express the set
1438 * ln_num_routes, minus 1 (better to under estimate than over so we
1439 * don't waste memory). */
1440 if (rnet_htable_size <= 0)
1441 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1442 else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1443 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1444 the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1445 order_base_2(rnet_htable_size) - 1);
1447 /* All LNDs apart from the LOLND are in separate modules. They
1448 * register themselves when their module loads, and unregister
1449 * themselves when their module is unloaded. */
1450 lnet_register_lnd(&the_lolnd);
1455 * Finalize LNet library.
1457 * \pre lnet_init() called with success.
1458 * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1463 LASSERT(the_lnet.ln_refcount == 0);
1465 while (!list_empty(&the_lnet.ln_lnds))
1466 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1468 lnet_destroy_locks();
1472 * Set LNet PID and start LNet interfaces, routing, and forwarding.
1474 * Users must call this function at least once before any other functions.
1475 * For each successful call there must be a corresponding call to
1476 * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1479 * The PID used by LNet may be different from the one requested.
1482 * \param requested_pid PID requested by the caller.
1484 * \return >= 0 on success, and < 0 error code on failures.
1487 LNetNIInit(lnet_pid_t requested_pid)
1489 int im_a_router = 0;
1492 lnet_ping_info_t *pinfo;
1493 lnet_handle_md_t md_handle;
1494 struct list_head net_head;
1496 INIT_LIST_HEAD(&net_head);
1498 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1500 CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1502 if (the_lnet.ln_refcount > 0) {
1503 rc = the_lnet.ln_refcount++;
1504 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1508 rc = lnet_prepare(requested_pid);
1510 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1514 /* Add in the loopback network */
1515 if (lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, &net_head) == NULL) {
1520 /* If LNet is being initialized via DLC it is possible
1521 * that the user requests not to load module parameters (ones which
1522 * are supported by DLC) on initialization. Therefore, make sure not
1523 * to load networks, routes and forwarding from module parameters
1524 * in this case. On cleanup in case of failure only clean up
1525 * routes if it has been loaded */
1526 if (!the_lnet.ln_nis_from_mod_params) {
1527 rc = lnet_parse_networks(&net_head,
1528 lnet_get_networks());
1533 ni_count = lnet_startup_lndnis(&net_head);
1539 if (!the_lnet.ln_nis_from_mod_params) {
1540 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1544 rc = lnet_check_routes();
1548 rc = lnet_rtrpools_alloc(im_a_router);
1553 rc = lnet_acceptor_start();
1556 the_lnet.ln_refcount = 1;
1557 /* Now I may use my own API functions... */
1559 rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1563 lnet_ping_target_update(pinfo, md_handle);
1565 rc = lnet_router_checker_start();
1572 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1577 lnet_ping_target_fini();
1579 the_lnet.ln_refcount = 0;
1580 lnet_acceptor_stop();
1582 if (!the_lnet.ln_nis_from_mod_params)
1583 lnet_destroy_routes();
1585 lnet_shutdown_lndnis();
1589 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1590 while (!list_empty(&net_head)) {
1592 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1593 list_del_init(&ni->ni_list);
1598 EXPORT_SYMBOL(LNetNIInit);
1601 * Stop LNet interfaces, routing, and forwarding.
1603 * Users must call this function once for each successful call to LNetNIInit().
1604 * Once the LNetNIFini() operation has been started, the results of pending
1605 * API operations are undefined.
1607 * \return always 0 for current implementation.
1612 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1614 LASSERT (the_lnet.ln_refcount > 0);
1616 if (the_lnet.ln_refcount != 1) {
1617 the_lnet.ln_refcount--;
1619 LASSERT(!the_lnet.ln_niinit_self);
1624 lnet_router_checker_stop();
1625 lnet_ping_target_fini();
1627 /* Teardown fns that use my own API functions BEFORE here */
1628 the_lnet.ln_refcount = 0;
1630 lnet_acceptor_stop();
1631 lnet_destroy_routes();
1632 lnet_shutdown_lndnis();
1636 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1639 EXPORT_SYMBOL(LNetNIFini);
1642 * Grabs the ni data from the ni structure and fills the out
1645 * \param[in] ni network interface structure
1646 * \param[out] cpt_count the number of cpts the ni is on
1647 * \param[out] nid Network Interface ID
1648 * \param[out] peer_timeout NI peer timeout
1649 * \param[out] peer_tx_crdits NI peer transmit credits
1650 * \param[out] peer_rtr_credits NI peer router credits
1651 * \param[out] max_tx_credits NI max transmit credit
1652 * \param[out] net_config Network configuration
1655 lnet_fill_ni_info(struct lnet_ni *ni, __u32 *cpt_count, __u64 *nid,
1656 int *peer_timeout, int *peer_tx_credits,
1657 int *peer_rtr_credits, int *max_tx_credits,
1658 struct lnet_ioctl_net_config *net_config)
1665 if (net_config == NULL)
1668 CLASSERT(ARRAY_SIZE(ni->ni_interfaces) ==
1669 ARRAY_SIZE(net_config->ni_interfaces));
1671 if (ni->ni_interfaces[0] != NULL) {
1672 for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1673 if (ni->ni_interfaces[i] != NULL) {
1674 strncpy(net_config->ni_interfaces[i],
1675 ni->ni_interfaces[i],
1676 sizeof(net_config->ni_interfaces[i]));
1682 *peer_timeout = ni->ni_peertimeout;
1683 *peer_tx_credits = ni->ni_peertxcredits;
1684 *peer_rtr_credits = ni->ni_peerrtrcredits;
1685 *max_tx_credits = ni->ni_maxtxcredits;
1687 net_config->ni_status = ni->ni_status->ns_status;
1690 ni->ni_cpts != NULL && i < ni->ni_ncpts &&
1691 i < LNET_MAX_SHOW_NUM_CPT;
1693 net_config->ni_cpts[i] = ni->ni_cpts[i];
1695 *cpt_count = ni->ni_ncpts;
1699 lnet_get_net_config(int idx, __u32 *cpt_count, __u64 *nid, int *peer_timeout,
1700 int *peer_tx_credits, int *peer_rtr_credits,
1701 int *max_tx_credits,
1702 struct lnet_ioctl_net_config *net_config)
1705 struct list_head *tmp;
1709 cpt = lnet_net_lock_current();
1711 list_for_each(tmp, &the_lnet.ln_nis) {
1712 ni = list_entry(tmp, lnet_ni_t, ni_list);
1716 lnet_fill_ni_info(ni, cpt_count, nid, peer_timeout,
1717 peer_tx_credits, peer_rtr_credits,
1718 max_tx_credits, net_config);
1724 lnet_net_unlock(cpt);
1729 lnet_dyn_add_ni(lnet_pid_t requested_pid, char *nets,
1730 __s32 peer_timeout, __s32 peer_cr, __s32 peer_buf_cr,
1733 lnet_ping_info_t *pinfo;
1734 lnet_handle_md_t md_handle;
1736 struct list_head net_head;
1738 lnet_remotenet_t *rnet;
1740 INIT_LIST_HEAD(&net_head);
1742 /* Create a ni structure for the network string */
1743 rc = lnet_parse_networks(&net_head, nets);
1745 return rc == 0 ? -EINVAL : rc;
1747 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1750 rc = -EINVAL; /* only add one interface per call */
1754 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1756 lnet_net_lock(LNET_LOCK_EX);
1757 rnet = lnet_find_net_locked(LNET_NIDNET(ni->ni_nid));
1758 lnet_net_unlock(LNET_LOCK_EX);
1759 /* make sure that the net added doesn't invalidate the current
1760 * configuration LNet is keeping */
1762 CERROR("Adding net %s will invalidate routing configuration\n",
1768 rc = lnet_ping_info_setup(&pinfo, &md_handle, 1 + lnet_get_ni_count(),
1773 list_del_init(&ni->ni_list);
1775 rc = lnet_startup_lndni(ni, peer_timeout, peer_cr,
1776 peer_buf_cr, credits);
1780 if (ni->ni_lnd->lnd_accept != NULL) {
1781 rc = lnet_acceptor_start();
1783 /* shutdown the ni that we just started */
1784 CERROR("Failed to start up acceptor thread\n");
1785 lnet_shutdown_lndni(ni);
1790 lnet_ping_target_update(pinfo, md_handle);
1791 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1796 lnet_ping_md_unlink(pinfo, &md_handle);
1797 lnet_ping_info_free(pinfo);
1799 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1800 while (!list_empty(&net_head)) {
1801 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1802 list_del_init(&ni->ni_list);
1809 lnet_dyn_del_ni(__u32 net)
1812 lnet_ping_info_t *pinfo;
1813 lnet_handle_md_t md_handle;
1816 /* don't allow userspace to shutdown the LOLND */
1817 if (LNET_NETTYP(net) == LOLND)
1820 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1821 /* create and link a new ping info, before removing the old one */
1822 rc = lnet_ping_info_setup(&pinfo, &md_handle,
1823 lnet_get_ni_count() - 1, false);
1827 ni = lnet_net2ni(net);
1833 /* decrement the reference counter taken by lnet_net2ni() */
1834 lnet_ni_decref_locked(ni, 0);
1836 lnet_shutdown_lndni(ni);
1838 if (lnet_count_acceptor_nis() == 0)
1839 lnet_acceptor_stop();
1841 lnet_ping_target_update(pinfo, md_handle);
1844 lnet_ping_md_unlink(pinfo, &md_handle);
1845 lnet_ping_info_free(pinfo);
1847 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1853 * This is an ugly hack to export IOC_LIBCFS_DEBUG_PEER and
1854 * IOC_LIBCFS_PORTALS_COMPATIBILITY commands to users, by tweaking the LNet
1855 * internal ioctl handler.
1857 * IOC_LIBCFS_PORTALS_COMPATIBILITY is now deprecated, don't use it.
1859 * \param cmd IOC_LIBCFS_DEBUG_PEER to print debugging data about a peer.
1860 * The data will be printed to system console. Don't use it excessively.
1861 * \param arg A pointer to lnet_process_id_t, process ID of the peer.
1863 * \return Always return 0 when called by users directly (i.e., not via ioctl).
1866 LNetCtl(unsigned int cmd, void *arg)
1868 struct libcfs_ioctl_data *data = arg;
1869 struct lnet_ioctl_config_data *config;
1870 lnet_process_id_t id = {0};
1874 CLASSERT(LIBCFS_IOC_DATA_MAX >= sizeof(struct lnet_ioctl_net_config) +
1875 sizeof(struct lnet_ioctl_config_data));
1878 case IOC_LIBCFS_GET_NI:
1879 rc = LNetGetId(data->ioc_count, &id);
1880 data->ioc_nid = id.nid;
1883 case IOC_LIBCFS_FAIL_NID:
1884 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1886 case IOC_LIBCFS_ADD_ROUTE:
1889 if (config->cfg_hdr.ioc_len < sizeof(*config))
1892 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1893 rc = lnet_add_route(config->cfg_net,
1894 config->cfg_config_u.cfg_route.rtr_hop,
1896 config->cfg_config_u.cfg_route.
1899 rc = lnet_check_routes();
1901 lnet_del_route(config->cfg_net,
1904 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1907 case IOC_LIBCFS_DEL_ROUTE:
1910 if (config->cfg_hdr.ioc_len < sizeof(*config))
1913 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1914 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
1915 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1918 case IOC_LIBCFS_GET_ROUTE:
1921 if (config->cfg_hdr.ioc_len < sizeof(*config))
1924 return lnet_get_route(config->cfg_count,
1926 &config->cfg_config_u.cfg_route.rtr_hop,
1928 &config->cfg_config_u.cfg_route.rtr_flags,
1929 &config->cfg_config_u.cfg_route.
1932 case IOC_LIBCFS_GET_NET: {
1933 struct lnet_ioctl_net_config *net_config;
1934 size_t total = sizeof(*config) + sizeof(*net_config);
1938 if (config->cfg_hdr.ioc_len < total)
1941 net_config = (struct lnet_ioctl_net_config *)
1943 if (config == NULL || net_config == NULL)
1946 return lnet_get_net_config(config->cfg_count,
1949 &config->cfg_config_u.
1950 cfg_net.net_peer_timeout,
1951 &config->cfg_config_u.cfg_net.
1952 net_peer_tx_credits,
1953 &config->cfg_config_u.cfg_net.
1954 net_peer_rtr_credits,
1955 &config->cfg_config_u.cfg_net.
1960 case IOC_LIBCFS_GET_LNET_STATS:
1962 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
1964 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
1967 lnet_counters_get(&lnet_stats->st_cntrs);
1971 case IOC_LIBCFS_CONFIG_RTR:
1974 if (config->cfg_hdr.ioc_len < sizeof(*config))
1977 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1978 if (config->cfg_config_u.cfg_buffers.buf_enable) {
1979 rc = lnet_rtrpools_enable();
1980 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1983 lnet_rtrpools_disable();
1984 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
1987 case IOC_LIBCFS_ADD_BUF:
1990 if (config->cfg_hdr.ioc_len < sizeof(*config))
1993 LNET_MUTEX_LOCK(&the_lnet.ln_api_mutex);
1994 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.
1996 config->cfg_config_u.cfg_buffers.
1998 config->cfg_config_u.cfg_buffers.
2000 LNET_MUTEX_UNLOCK(&the_lnet.ln_api_mutex);
2003 case IOC_LIBCFS_GET_BUF: {
2004 struct lnet_ioctl_pool_cfg *pool_cfg;
2005 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2009 if (config->cfg_hdr.ioc_len < total)
2012 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2013 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2016 case IOC_LIBCFS_GET_PEER_INFO: {
2017 struct lnet_ioctl_peer *peer_info = arg;
2019 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2022 return lnet_get_peer_info(
2023 peer_info->pr_count,
2025 peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2026 &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2027 &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2028 &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2029 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2030 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2031 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2032 &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2035 case IOC_LIBCFS_NOTIFY_ROUTER:
2036 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2037 cfs_time_current() -
2038 cfs_time_seconds(cfs_time_current_sec() -
2039 (time_t)data->ioc_u64[0]));
2041 case IOC_LIBCFS_PORTALS_COMPATIBILITY:
2042 /* This can be removed once lustre stops calling it */
2045 case IOC_LIBCFS_LNET_DIST:
2046 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2047 if (rc < 0 && rc != -EHOSTUNREACH)
2050 data->ioc_u32[0] = rc;
2053 case IOC_LIBCFS_TESTPROTOCOMPAT:
2054 lnet_net_lock(LNET_LOCK_EX);
2055 the_lnet.ln_testprotocompat = data->ioc_flags;
2056 lnet_net_unlock(LNET_LOCK_EX);
2059 case IOC_LIBCFS_LNET_FAULT:
2060 return lnet_fault_ctl(data->ioc_flags, data);
2062 case IOC_LIBCFS_PING:
2063 id.nid = data->ioc_nid;
2064 id.pid = data->ioc_u32[0];
2065 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
2066 (lnet_process_id_t __user *)data->ioc_pbuf1,
2067 data->ioc_plen1/sizeof(lnet_process_id_t));
2070 data->ioc_count = rc;
2073 case IOC_LIBCFS_DEBUG_PEER: {
2074 /* CAVEAT EMPTOR: this one designed for calling directly; not
2076 id = *((lnet_process_id_t *) arg);
2078 lnet_debug_peer(id.nid);
2080 ni = lnet_net2ni(LNET_NIDNET(id.nid));
2082 CDEBUG(D_WARNING, "No NI for %s\n", libcfs_id2str(id));
2084 if (ni->ni_lnd->lnd_ctl == NULL) {
2085 CDEBUG(D_WARNING, "No ctl for %s\n",
2088 (void)ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2097 ni = lnet_net2ni(data->ioc_net);
2101 if (ni->ni_lnd->lnd_ctl == NULL)
2104 rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2111 EXPORT_SYMBOL(LNetCtl);
2114 * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2115 * all interfaces share a same PID, as requested by LNetNIInit().
2117 * \param index Index of the interface to look up.
2118 * \param id On successful return, this location will hold the
2119 * lnet_process_id_t ID of the interface.
2121 * \retval 0 If an interface exists at \a index.
2122 * \retval -ENOENT If no interface has been found.
2125 LNetGetId(unsigned int index, lnet_process_id_t *id)
2128 struct list_head *tmp;
2132 LASSERT(the_lnet.ln_refcount > 0);
2134 cpt = lnet_net_lock_current();
2136 list_for_each(tmp, &the_lnet.ln_nis) {
2140 ni = list_entry(tmp, lnet_ni_t, ni_list);
2142 id->nid = ni->ni_nid;
2143 id->pid = the_lnet.ln_pid;
2148 lnet_net_unlock(cpt);
2151 EXPORT_SYMBOL(LNetGetId);
2154 * Print a string representation of handle \a h into buffer \a str of
2158 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2160 snprintf(str, len, LPX64, h.cookie);
2162 EXPORT_SYMBOL(LNetSnprintHandle);
2165 lnet_ping(lnet_process_id_t id, int timeout_ms, lnet_process_id_t __user *ids,
2168 lnet_handle_eq_t eqh;
2169 lnet_handle_md_t mdh;
2171 lnet_md_t md = { NULL };
2175 const int a_long_time = 60000; /* mS */
2177 lnet_ping_info_t *info;
2178 lnet_process_id_t tmpid;
2185 infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
2188 id.nid == LNET_NID_ANY ||
2189 timeout_ms > 500000 || /* arbitrary limit! */
2190 n_ids > 20) /* arbitrary limit! */
2193 if (id.pid == LNET_PID_ANY)
2194 id.pid = LNET_PID_LUSTRE;
2196 LIBCFS_ALLOC(info, infosz);
2200 /* NB 2 events max (including any unlink event) */
2201 rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2203 CERROR("Can't allocate EQ: %d\n", rc);
2207 /* initialize md content */
2210 md.threshold = 2; /*GET/REPLY*/
2212 md.options = LNET_MD_TRUNCATE;
2216 rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2218 CERROR("Can't bind MD: %d\n", rc);
2222 rc = LNetGet(LNET_NID_ANY, mdh, id,
2223 LNET_RESERVED_PORTAL,
2224 LNET_PROTO_PING_MATCHBITS, 0);
2227 /* Don't CERROR; this could be deliberate! */
2229 rc2 = LNetMDUnlink(mdh);
2232 /* NB must wait for the UNLINK event below... */
2234 timeout_ms = a_long_time;
2238 /* MUST block for unlink to complete */
2240 blocked = cfs_block_allsigs();
2242 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
2245 cfs_restore_sigs(blocked);
2247 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2248 (rc2 <= 0) ? -1 : event.type,
2249 (rc2 <= 0) ? -1 : event.status,
2250 (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2252 LASSERT(rc2 != -EOVERFLOW); /* can't miss anything */
2254 if (rc2 <= 0 || event.status != 0) {
2255 /* timeout or error */
2256 if (!replied && rc == 0)
2257 rc = (rc2 < 0) ? rc2 :
2258 (rc2 == 0) ? -ETIMEDOUT :
2262 /* Ensure completion in finite time... */
2264 /* No assertion (racing with network) */
2266 timeout_ms = a_long_time;
2267 } else if (rc2 == 0) {
2268 /* timed out waiting for unlink */
2269 CWARN("ping %s: late network completion\n",
2272 } else if (event.type == LNET_EVENT_REPLY) {
2277 } while (rc2 <= 0 || !event.unlinked);
2281 CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2288 LASSERT(nob >= 0 && nob <= infosz);
2290 rc = -EPROTO; /* if I can't parse... */
2293 /* can't check magic/version */
2294 CERROR("%s: ping info too short %d\n",
2295 libcfs_id2str(id), nob);
2299 if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2300 lnet_swap_pinginfo(info);
2301 } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2302 CERROR("%s: Unexpected magic %08x\n",
2303 libcfs_id2str(id), info->pi_magic);
2307 if ((info->pi_features & LNET_PING_FEAT_NI_STATUS) == 0) {
2308 CERROR("%s: ping w/o NI status: 0x%x\n",
2309 libcfs_id2str(id), info->pi_features);
2313 if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2314 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2315 nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2319 if (info->pi_nnis < n_ids)
2320 n_ids = info->pi_nnis;
2322 if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2323 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2324 nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2328 rc = -EFAULT; /* If I SEGV... */
2330 for (i = 0; i < n_ids; i++) {
2331 tmpid.pid = info->pi_pid;
2332 tmpid.nid = info->pi_ni[i].ns_nid;
2333 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2339 rc2 = LNetEQFree(eqh);
2341 CERROR("rc2 %d\n", rc2);
2345 LIBCFS_FREE(info, infosz);